File indexing completed on 2024-12-22 05:37:11

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category  Zend
0016  * @package   Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version   $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_File_MimeType
0024  */
0025 // require_once 'Zend/Validate/File/MimeType.php';
0026 
0027 /**
0028  * Validator for the mime type of a file
0029  *
0030  * @category  Zend
0031  * @package   Zend_Validate
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Validate_File_ExcludeMimeType extends Zend_Validate_File_MimeType
0036 {
0037     const FALSE_TYPE   = 'fileExcludeMimeTypeFalse';
0038     const NOT_DETECTED = 'fileExcludeMimeTypeNotDetected';
0039     const NOT_READABLE = 'fileExcludeMimeTypeNotReadable';
0040 
0041     /**
0042      * @var array Error message templates
0043      */
0044     protected $_messageTemplates = array(
0045         self::FALSE_TYPE   => "File '%value%' has a false mimetype of '%type%'",
0046         self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
0047         self::NOT_READABLE => "File '%value%' is not readable or does not exist",
0048     );
0049 
0050     /**
0051      * Defined by Zend_Validate_Interface
0052      *
0053      * Returns true if the mimetype of the file does not matche the given ones. Also parts
0054      * of mimetypes can be checked. If you give for example "image" all image
0055      * mime types will not be accepted like "image/gif", "image/jpeg" and so on.
0056      *
0057      * @param  string $value Real file to check for mimetype
0058      * @param  array  $file  File data from Zend_File_Transfer
0059      * @return boolean
0060      */
0061     public function isValid($value, $file = null)
0062     {
0063         if ($file === null) {
0064             $file = array(
0065                 'type' => null,
0066                 'name' => $value
0067             );
0068         }
0069 
0070         // Is file readable ?
0071         // require_once 'Zend/Loader.php';
0072         if (!Zend_Loader::isReadable($value)) {
0073             return $this->_throw($file, self::NOT_READABLE);
0074         }
0075 
0076         $this->_type = $this->_detectMimeType($value);
0077 
0078         if (empty($this->_type) && $this->_headerCheck) {
0079             $this->_type = $file['type'];
0080         }
0081 
0082         if (empty($this->_type)) {
0083             return $this->_throw($file, self::NOT_DETECTED);
0084         }
0085 
0086         $mimetype = $this->getMimeType(true);
0087         if (in_array($this->_type, $mimetype)) {
0088             return $this->_throw($file, self::FALSE_TYPE);
0089         }
0090 
0091         $types = explode('/', $this->_type);
0092         $types = array_merge($types, explode('-', $this->_type));
0093         foreach($mimetype as $mime) {
0094             if (in_array($mime, $types)) {
0095                 return $this->_throw($file, self::FALSE_TYPE);
0096             }
0097         }
0098 
0099         return true;
0100     }
0101 }