Browse Source

ZF-11784: User can now disable attempts to use "common" magic files


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24486 44c647ce-9c0f-0410-b52a-842ac1e357ba
hobodave 14 years ago
parent
commit
603b0666ed
2 changed files with 85 additions and 1 deletions
  1. 36 1
      library/Zend/Validate/File/MimeType.php
  2. 49 0
      tests/Zend/Validate/File/MimeTypeTest.php

+ 36 - 1
library/Zend/Validate/File/MimeType.php

@@ -103,6 +103,12 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
     );
 
     /**
+     * Indicates whether use of $_magicFiles should be attempted.
+     * @var boolean
+     */
+    protected $_tryCommonMagicFiles = true;
+
+    /**
      * Option to allow header check
      *
      * @var boolean
@@ -151,7 +157,10 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
         if (null === $this->_magicfile) {
             if (!empty($_ENV['MAGIC'])) {
                 $this->setMagicFile($_ENV['MAGIC']);
-            } elseif (!(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)) {
+            } elseif (
+                !(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)
+                && $this->shouldTryCommonMagicFiles() // @see ZF-11784
+            ) {
                 require_once 'Zend/Validate/Exception.php';
                 foreach ($this->_magicFiles as $file) {
                     // supressing errors which are thrown due to openbase_dir restrictions
@@ -210,6 +219,32 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
     }
 
     /**
+     * Enables or disables attempts to try the common magic file locations
+     * specified by Zend_Validate_File_MimeType::_magicFiles
+     *
+     * @param  boolean $flag
+     * @return Zend_Validate_File_MimeType Provides fluent interface
+     * @see http://framework.zend.com/issues/browse/ZF-11784
+     */
+    public function setTryCommonMagicFilesFlag($flag = true)
+    {
+        $this->_tryCommonMagicFiles = (boolean) $flag;
+
+        return $this;
+    }
+
+    /**
+     * Accessor for Zend_Validate_File_MimeType::_magicFiles
+     *
+     * @return boolean
+     * @see http://framework.zend.com/issues/browse/ZF-11784
+     */
+    public function shouldTryCommonMagicFiles()
+    {
+        return $this->_tryCommonMagicFiles;
+    }
+
+    /**
      * Returns the Header Check option
      *
      * @return boolean

+ 49 - 0
tests/Zend/Validate/File/MimeTypeTest.php

@@ -236,6 +236,55 @@ class Zend_Validate_File_MimeTypeTest extends PHPUnit_Framework_TestCase
             );
         }
     }
+    
+    /**
+     * @group ZF-11784
+     */
+    public function testTryCommonMagicFilesFlag()
+    {
+        $validator = new Zend_Validate_File_MimeType('image/jpeg');
+        $this->assertTrue($validator->shouldTryCommonMagicFiles());
+        
+        $validator->setTryCommonMagicFilesFlag(false);
+        $this->assertFalse($validator->shouldTryCommonMagicFiles());
+        
+        $validator->setTryCommonMagicFilesFlag(true);
+        $this->assertTrue($validator->shouldTryCommonMagicFiles());
+    }
+
+    /**
+     * @group ZF-11784
+     */
+    public function testDisablingTryCommonMagicFilesIgnoresCommonLocations()
+    {
+        $filetest = dirname(__FILE__) . '/_files/picture.jpg';
+        $files = array(
+            'name'     => 'picture.jpg',
+            'size'     => 200,
+            'tmp_name' => $filetest,
+            'error'    => 0
+        );
+        
+        $validator = new Zend_Validate_File_MimeType(array('image/jpeg', 'image/jpeg; charset=binary'));
+        
+        $goodEnvironment = $validator->isValid($filetest, $files);
+        
+        if ($goodEnvironment) {
+            /** 
+             * The tester's environment has magic files that are properly read by PHP
+             * This prevents the test from being relevant in the environment
+             */
+            $this->markTestSkipped('This test environment works as expected with the common magic files, preventing this from being testable.');
+        } else {
+            // The common magic files detected the image as application/octet-stream -- try the PHP default
+            // Note that if this  branch of code is entered then testBasic, testDualValidation,
+            // as well as Zend_Validate_File_IsCompressedTest::testBasic and Zend_Validate_File_IsImageTest::testBasic
+            // will be failing as well.
+            $validator = new Zend_Validate_File_MimeType(array('image/jpeg', 'image/jpeg; charset=binary'));
+            $validator->setTryCommonMagicFilesFlag(false);
+            $this->assertTrue($validator->isValid($filetest, $files));
+        }
+    }
 }
 
 // Call Zend_Validate_File_MimeTypeTest::main() if this source file is executed directly.