Skip to content
Permalink
Browse files

Validating mimetypes now require ext/fileinfo

Validating files must be done carefully, and File::mime() is too helpful
to be realiable. This also removes the dependency from Validation to
Filesystem
  • Loading branch information...
lorenzo committed Aug 31, 2014
1 parent 33e5a35 commit 1eafb8e0206909768ac8a8f1ed768a1917477766
Showing with 17 additions and 6 deletions.
  1. +16 −5 src/Validation/Validation.php
  2. +1 −1 tests/TestCase/Validation/ValidationTest.php
@@ -909,19 +909,30 @@ public static function luhn($check, $deep = false) {
* @param string|array $check Value to check.
* @param array|string $mimeTypes Array of mime types or regex pattern to check.
* @return bool Success
* @throws \Cake\Core\Exception\Exception when mime type can not be determined.
* @throws \RuntimeException when mime type can not be determined.
* @throws \LogicException when ext/fileinfo is missing
*/
public static function mimeType($check, $mimeTypes = array()) {
if (is_array($check) && isset($check['tmp_name'])) {
$check = $check['tmp_name'];
}
$File = new File($check);
$mime = $File->mime();
if (!function_exists('finfo_open')) {
throw new LogicException('ext/fileinfo is required for validating file mime types');
}
if (!is_file($check)) {
throw new RuntimeException('Cannot validate mimetype for a missing file');
}
$finfo = finfo_open(FILEINFO_MIME);
$finfo = finfo_file($finfo, $check);
if ($mime === false) {
throw new Exception('Can not determine the mimetype.');
if (!$finfo) {
throw new RuntimeException('Can not determine the mimetype.');
}
list($mime) = explode(';', $finfo);
if (is_string($mimeTypes)) {
return self::_check($mime, $mimeTypes);
@@ -2378,7 +2378,7 @@ public function testMimeType() {
/**
* testMimeTypeFalse method
*
* @expectedException \Cake\Core\Exception\Exception
* @expectedException \RuntimeException
* @return void
*/
public function testMimeTypeFalse() {

0 comments on commit 1eafb8e

Please sign in to comment.
You can’t perform that action at this time.