/** * Ensures that the validator follows expected behavior * * @return void */ public function testBasic() { $valuesExpected = array(array('image/gif', true), array('image', true), array('test/notype', false), array('image/gif, image/jpeg', true), array(array('image/vasa', 'image/gif'), true), array(array('image/jpeg', 'gif'), true), array(array('image/jpeg', 'jpeg'), false)); $files = array('name' => 'testsize.mo', 'type' => 'image/gif', 'size' => 200, 'tmp_name' => dirname(__FILE__) . '/_files/testsize.mo', 'error' => 0); foreach ($valuesExpected as $element) { $validator = new Zend_Validate_File_MimeType($element[0]); $this->assertEquals($element[1], $validator->isValid(dirname(__FILE__) . '/_files/testsize.mo', $files)); } }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if the imagesize of $value is at least min and * not bigger than max * * @param string $value Real file to check for image size * @param array $file File data from Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { if ($file === null) { $file = array('type' => null, 'name' => $value); } if (isset($this->_dir)) { $value = $this->_dir . "/" . $value; } return parent::isValid($value, $file); }
/** * Ensures that the validator follows expected behavior * * @return void */ public function testBasic() { $valuesExpected = array(array(array('image/jpg', 'image/jpeg'), true), array('image', true), array('test/notype', false), array('image/gif, image/jpg, image/jpeg', true), array(array('image/vasa', 'image/jpg', 'image/jpeg'), true), array(array('image/jpg', 'image/jpeg', 'gif'), true), array(array('image/gif', 'gif'), false), array('image/jp', false), array('image/jpg2000', false), array('image/jpeg2000', false)); $filetest = dirname(__FILE__) . '/_files/picture.jpg'; $files = array('name' => 'picture.jpg', 'type' => 'image/jpg', 'size' => 200, 'tmp_name' => $filetest, 'error' => 0); foreach ($valuesExpected as $element) { $options = array_shift($element); $expected = array_shift($element); $validator = new Zend_Validate_File_MimeType($options); $validator->enableHeaderCheck(); $this->assertEquals($expected, $validator->isValid($filetest, $files), "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1) . "\nMessages: " . var_export($validator->getMessages(), 1)); } }
/** * Used to check if uploaded file mime type is valid or not * * @param array $validTypes * @access public * @return bool */ public function checkMimeType($validTypes = array()) { try { if (count($validTypes) > 0) { $validator = new Zend_Validate_File_MimeType($validTypes); return $validator->isValid($this->_file['tmp_name']); } return true; } catch (Exception $e) { return false; } }
/** * Checks if a file match the given mimetype(s) * * @param string $pathFile File to check for mimetype * @param array|string $mimeTypes Accepted mimetype(s) * @return bool TRUE if the file match the givem mimetype(s), FALSE otherwise */ function checkMimeType($pathFile, array $mimeTypes) { $mimeTypes['headerCheck'] = true; $validator = new Zend_Validate_File_MimeType($mimeTypes); if ($validator->isValid($pathFile)) { return true; } return false; }
/** * @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)); } }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if the imagesize of $value is at least min and * not bigger than max * * @param string $value Real file to check for image size * @param array $file File data from Zend_File_Transfer * @return boolean */ public function isValid($value, $file = null) { $this->_loadParams(); $mimetypes = $this->_fields[$this->_fieldId]['params']['mimetypes']; if (is_array($mimetypes) && count($mimetypes)) { foreach ($mimetypes as $mime) { $this->addMimeType($mime); } } if ($file === null) { $file = array('type' => null, 'name' => $value); } if (isset($this->_dir)) { $value = $this->_dir . "/" . $value; } return parent::isValid($value, $file); }
/** * Unzip file * * @param string $path * @return boolean */ public function unzip($path, $newDir = false) { $filePath = $this->getRealPath($path); if (!file_exists($filePath)) { $this->returnError('File does not exists ' . $path); return false; } // get the absolute path to $file $dir = pathinfo($filePath, PATHINFO_DIRNAME); $packageName = pathinfo($filePath, PATHINFO_FILENAME); if ($newDir) { $dir .= DIRECTORY_SEPARATOR . $packageName; } $zip = new ZipArchive(); $res = $zip->open($filePath); if (!$res) { $this->returnError('Cannot extract file'); return false; } $tmpDir = APPLICATION_PATH . '/../tmp/' . session_id() . '-' . $packageName; $zip->extractTo($tmpDir); $zip->close(); //check size $extractSize = $this->_helper->getDirSize($tmpDir); if ($extractSize > $this->_helper->getFreeSpace()) { $this->_rrmdir($tmpDir); $this->returnError('Not enough space on disk'); return false; } $files = array(); $this->_rlistFiles($tmpDir, $files); $mimeValidator = new Zend_Validate_File_MimeType($this->_helper->getDefaultMimeTypes()); $extValidator = new Zend_Validate_File_Extension($this->_helper->getDefaultExtensions()); foreach ($files as $currFile) { if (!$extValidator->isValid($currFile)) { $this->_rrmdir($tmpDir); $this->returnError(sprintf('File [%s] has invalid extension.', basename($currFile))); return false; } if (!$mimeValidator->isValid($currFile)) { $this->_rrmdir($tmpDir); $this->returnError(sprintf('File [%s] has invalid mime type.', basename($currFile))); return false; } } $this->_rcopy($tmpDir, $dir); $this->_rrmdir($tmpDir); return true; }