Explorar el Código

[ZF-9278] Zend_Validate:

- reworked finfo() detection

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21471 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas hace 16 años
padre
commit
9268ae0092

+ 12 - 2
library/Zend/Validate/File/MimeType.php

@@ -152,13 +152,16 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
             if (!empty($_ENV['MAGIC'])) {
                 $this->setMagicFile($_ENV['MAGIC']);
             } elseif (!(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)) {
+                require_once 'Zend/Validate/Exception.php';
                 foreach ($this->_magicFiles as $file) {
                     // supressing errors which are thrown due to openbase_dir restrictions
-                    if (@file_exists($file)) {
+                    try {
                         $this->setMagicFile($file);
                         if ($this->_magicfile !== null) {
                             break;
                         }
+                    } catch (Zend_Validate_Exception $e) {
+                        // Intentionally, catch and fall through
                     }
                 }
             }
@@ -177,12 +180,17 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
      * if the MAGIC file is errorous, no file will be set
      *
      * @param  string $file
+     * @throws Zend_Validate_Exception When finfo can not read the magicfile
      * @return Zend_Validate_File_MimeType Provides fluid interface
      */
     public function setMagicFile($file)
     {
-        if (empty($file) || !(class_exists('finfo', false))) {
+        if (empty($file)) {
             $this->_magicfile = null;
+        } else if (!(class_exists('finfo', false))) {
+            $this->_magicfile = null;
+            require_once 'Zend/Validate/Exception.php';
+            throw new Zend_Validate_Exception('Magicfile can not be set. There is no finfo extension installed');
         } else if (!is_readable($file)) {
             require_once 'Zend/Validate/Exception.php';
             throw new Zend_Validate_Exception('The given magicfile can not be read');
@@ -191,6 +199,8 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
             $this->_finfo = @finfo_open($const, $file);
             if ($this->_finfo === false) {
                 $this->_finfo = null;
+                require_once 'Zend/Validate/Exception.php';
+                throw new Zend_Validate_Exception('The given magicfile is not accepted by finfo');
             } else {
                 $this->_magicfile = $file;
             }

+ 7 - 11
tests/Zend/Validate/File/MimeTypeTest.php

@@ -175,19 +175,18 @@ class Zend_Validate_File_MimeTypeTest extends PHPUnit_Framework_TestCase
         try {
             $validator->setMagicFile('/unknown/magic/file');
         } catch (Zend_Validate_Exception $e) {
-            $this->assertContains('can not be read', $e->getMessage());
+            $this->assertContains('can not be', $e->getMessage());
         }
-
-        $validator->setMagicFile(__FILE__);
-        // @ZF-9320: False Magic File is not allowed to be set
-        $this->assertNotEquals(__FILE__, $validator->getMagicFile());
     }
 
     public function testSetMagicFileWithinConstructor()
     {
-        $validator = new Zend_Validate_File_MimeType(array('image/gif', 'magicfile' => __FILE__));
-        // @ZF-9320: False Magic File is not allowed to be set
-        $this->assertNotEquals(__FILE__, $validator->getMagicFile());
+        require_once 'Zend/Validate/Exception.php';
+        try {
+            $validator = new Zend_Validate_File_MimeType(array('image/gif', 'magicfile' => __FILE__));
+        } catch (Zend_Validate_Exception $e) {
+            // @ZF-9320: False Magic File is not allowed to be set
+        }
     }
 
     public function testOptionsAtConstructor()
@@ -195,11 +194,8 @@ class Zend_Validate_File_MimeTypeTest extends PHPUnit_Framework_TestCase
         $validator = new Zend_Validate_File_MimeType(array(
             'image/gif',
             'image/jpg',
-            'magicfile' => __FILE__,
             'headerCheck' => true));
 
-        // @ZF-9320: False Magic File is not allowed to be set
-        $this->assertNotEquals(__FILE__, $validator->getMagicFile());
         $this->assertTrue($validator->getHeaderCheck());
         $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
     }