From e3a6e9e169644d93fd02877e545a99ca1963b59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 28 May 2012 00:32:30 +0200 Subject: [PATCH 1/2] Adding uploadError() and mimeType() to the Validation class --- lib/Cake/Test/Case/Utility/ValidationTest.php | 26 ++++++++++++++ lib/Cake/Utility/Validation.php | 34 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 90de95ca4..35f057ab0 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -2166,4 +2166,30 @@ class ValidationTest extends CakeTestCase { $this->assertFalse(Validation::datetime('31 11 2006 1:00pm', 'dmy')); } +/** + * testMimeType method + * + * @return void + */ + public function testMimeType() { + $file = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif'; + $this->assertTrue(Validation::mimeType($file, array('image/gif'))); + $this->assertTrue(Validation::mimeType(array('tmp_name' => $file), array('image/gif'))); + + $this->assertFalse(Validation::mimeType($file, array('image/png'))); + $this->assertFalse(Validation::mimeType(array('tmp_name' => $file), array('image/png'))); + } + +/** + * testMimeType method + * + * @return void + */ + public function testUploadError() { + $this->assertTrue(Validation::uploadError(0)); + $this->assertTrue(Validation::uploadError(array('error' => 0))); + + $this->assertFalse(Validation::uploadError(2)); + $this->assertFalse(Validation::uploadError(array('error' => 2))); + } } diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 373adb33f..4311e94e0 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -18,6 +18,7 @@ */ App::uses('Multibyte', 'I18n'); +App::uses('File', 'Utility'); // Load multibyte if the extension is missing. if (!function_exists('mb_strlen')) { class_exists('Multibyte'); @@ -858,6 +859,39 @@ class Validation { return ($sum % 10 == 0); } +/** + * Checks the mime type of a file + * + * @param string|array $check + * @param array $mimeTypes to check for + * @return boolean Success + */ + public static function mimeType($check, $mimeTypes = array()) { + if (is_array($check) && isset($check['tmp_name'])) { + $check = $check['tmp_name']; + } + + $File = new File($check); + $info = $File->info(); + + return in_array($info['mime'], $mimeTypes); + } + +/** + * Checking for upload errors + * + * @param string|array $check + * @retrun boolean + * @see http://www.php.net/manual/en/features.file-upload.errors.php + */ + public static function uploadError($check) { + if (is_array($check) && isset($check['error'])) { + $check = $check['error']; + } + + return $check === UPLOAD_ERR_OK; + } + /** * Lazily populate the IP address patterns used for validations * From fea4ae65231a144f28b1d9a4f19ca302c5c3ac79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 28 May 2012 00:48:22 +0200 Subject: [PATCH 2/2] Changing File->info() to File->mime() in Validation::mimeType() --- lib/Cake/Utility/Validation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 4311e94e0..f88a31dde 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -872,9 +872,9 @@ class Validation { } $File = new File($check); - $info = $File->info(); + $mime = $File->mime(); - return in_array($info['mime'], $mimeTypes); + return in_array($mime, $mimeTypes); } /**