Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/File/MimeType.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00035 class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
00036 {
00040     const FALSE_TYPE   = 'fileMimeTypeFalse';
00041     const NOT_DETECTED = 'fileMimeTypeNotDetected';
00042     const NOT_READABLE = 'fileMimeTypeNotReadable';
00048     protected $_messageTemplates = array(
00049         self::FALSE_TYPE   => "File '%value%' has a false mimetype of '%type%'",
00050         self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
00051         self::NOT_READABLE => "File '%value%' can not be read",
00052     );
00053 
00057     protected $_messageVariables = array(
00058         'type' => '_type'
00059     );
00060 
00064     protected $_type;
00065 
00073     protected $_mimetype;
00074 
00080     protected $_magicfile;
00081 
00087     protected $_finfo;
00088 
00093     protected $_magicFiles = array(
00094         '/usr/share/misc/magic',
00095         '/usr/share/misc/magic.mime',
00096         '/usr/share/misc/magic.mgc',
00097         '/usr/share/mime/magic',
00098         '/usr/share/mime/magic.mime',
00099         '/usr/share/mime/magic.mgc',
00100         '/usr/share/file/magic',
00101         '/usr/share/file/magic.mime',
00102         '/usr/share/file/magic.mgc',
00103     );
00104 
00110     protected $_headerCheck = false;
00111 
00120     public function __construct($mimetype)
00121     {
00122         if ($mimetype instanceof Zend_Config) {
00123             $mimetype = $mimetype->toArray();
00124         } elseif (is_string($mimetype)) {
00125             $mimetype = explode(',', $mimetype);
00126         } elseif (!is_array($mimetype)) {
00127             require_once 'Zend/Validate/Exception.php';
00128             throw new Zend_Validate_Exception("Invalid options to validator provided");
00129         }
00130 
00131         if (isset($mimetype['magicfile'])) {
00132             $this->setMagicFile($mimetype['magicfile']);
00133             unset($mimetype['magicfile']);
00134         }
00135 
00136         if (isset($mimetype['headerCheck'])) {
00137             $this->enableHeaderCheck($mimetype['headerCheck']);
00138             unset($mimetype['headerCheck']);
00139         }
00140 
00141         $this->setMimeType($mimetype);
00142     }
00143 
00149     public function getMagicFile()
00150     {
00151         if (null === $this->_magicfile) {
00152             if (!empty($_ENV['MAGIC'])) {
00153                 $this->setMagicFile($_ENV['MAGIC']);
00154             } elseif (!(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)) {
00155                 require_once 'Zend/Validate/Exception.php';
00156                 foreach ($this->_magicFiles as $file) {
00157                     // supressing errors which are thrown due to openbase_dir restrictions
00158                     try {
00159                         $this->setMagicFile($file);
00160                         if ($this->_magicfile !== null) {
00161                             break;
00162                         }
00163                     } catch (Zend_Validate_Exception $e) {
00164                         // Intentionally, catch and fall through
00165                     }
00166                 }
00167             }
00168 
00169             if ($this->_magicfile === null) {
00170                 $this->_magicfile = false;
00171             }
00172         }
00173 
00174         return $this->_magicfile;
00175     }
00176 
00186     public function setMagicFile($file)
00187     {
00188         if (empty($file)) {
00189             $this->_magicfile = null;
00190         } else if (!(class_exists('finfo', false))) {
00191             $this->_magicfile = null;
00192             require_once 'Zend/Validate/Exception.php';
00193             throw new Zend_Validate_Exception('Magicfile can not be set. There is no finfo extension installed');
00194         } else if (!is_readable($file)) {
00195             require_once 'Zend/Validate/Exception.php';
00196             throw new Zend_Validate_Exception('The given magicfile can not be read');
00197         } else {
00198             $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
00199             $this->_finfo = @finfo_open($const, $file);
00200             if (empty($this->_finfo)) {
00201                 $this->_finfo = null;
00202                 require_once 'Zend/Validate/Exception.php';
00203                 throw new Zend_Validate_Exception('The given magicfile is not accepted by finfo');
00204             } else {
00205                 $this->_magicfile = $file;
00206             }
00207         }
00208 
00209         return $this;
00210     }
00211 
00217     public function getHeaderCheck()
00218     {
00219         return $this->_headerCheck;
00220     }
00221 
00229     public function enableHeaderCheck($headerCheck = true)
00230     {
00231         $this->_headerCheck = (boolean) $headerCheck;
00232         return $this;
00233     }
00234 
00241     public function getMimeType($asArray = false)
00242     {
00243         $asArray   = (bool) $asArray;
00244         $mimetype = (string) $this->_mimetype;
00245         if ($asArray) {
00246             $mimetype = explode(',', $mimetype);
00247         }
00248 
00249         return $mimetype;
00250     }
00251 
00258     public function setMimeType($mimetype)
00259     {
00260         $this->_mimetype = null;
00261         $this->addMimeType($mimetype);
00262         return $this;
00263     }
00264 
00271     public function addMimeType($mimetype)
00272     {
00273         $mimetypes = $this->getMimeType(true);
00274 
00275         if (is_string($mimetype)) {
00276             $mimetype = explode(',', $mimetype);
00277         } elseif (!is_array($mimetype)) {
00278             require_once 'Zend/Validate/Exception.php';
00279             throw new Zend_Validate_Exception("Invalid options to validator provided");
00280         }
00281 
00282         if (isset($mimetype['magicfile'])) {
00283             unset($mimetype['magicfile']);
00284         }
00285 
00286         foreach ($mimetype as $content) {
00287             if (empty($content) || !is_string($content)) {
00288                 continue;
00289             }
00290             $mimetypes[] = trim($content);
00291         }
00292         $mimetypes = array_unique($mimetypes);
00293 
00294         // Sanity check to ensure no empty values
00295         foreach ($mimetypes as $key => $mt) {
00296             if (empty($mt)) {
00297                 unset($mimetypes[$key]);
00298             }
00299         }
00300 
00301         $this->_mimetype = implode(',', $mimetypes);
00302 
00303         return $this;
00304     }
00305 
00317     public function isValid($value, $file = null)
00318     {
00319         if ($file === null) {
00320             $file = array(
00321                 'type' => null,
00322                 'name' => $value
00323             );
00324         }
00325 
00326         // Is file readable ?
00327         require_once 'Zend/Loader.php';
00328         if (!Zend_Loader::isReadable($value)) {
00329             return $this->_throw($file, self::NOT_READABLE);
00330         }
00331 
00332         $mimefile = $this->getMagicFile();
00333         if (class_exists('finfo', false)) {
00334             $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
00335             if (!empty($mimefile) && empty($this->_finfo)) {
00336                 $this->_finfo = @finfo_open($const, $mimefile);
00337             }
00338 
00339             if (empty($this->_finfo)) {
00340                 $this->_finfo = @finfo_open($const);
00341             }
00342 
00343             $this->_type = null;
00344             if (!empty($this->_finfo)) {
00345                 $this->_type = finfo_file($this->_finfo, $value);
00346             }
00347         }
00348 
00349         if (empty($this->_type) &&
00350             (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
00351                 $this->_type = mime_content_type($value);
00352         }
00353 
00354         if (empty($this->_type) && $this->_headerCheck) {
00355             $this->_type = $file['type'];
00356         }
00357 
00358         if (empty($this->_type)) {
00359             return $this->_throw($file, self::NOT_DETECTED);
00360         }
00361 
00362         $mimetype = $this->getMimeType(true);
00363         if (in_array($this->_type, $mimetype)) {
00364             return true;
00365         }
00366 
00367         $types = explode('/', $this->_type);
00368         $types = array_merge($types, explode('-', $this->_type));
00369         $types = array_merge($types, explode(';', $this->_type));
00370         foreach($mimetype as $mime) {
00371             if (in_array($mime, $types)) {
00372                 return true;
00373             }
00374         }
00375 
00376         return $this->_throw($file, self::FALSE_TYPE);
00377     }
00378 
00386     protected function _throw($file, $errorType)
00387     {
00388         $this->_value = $file['name'];
00389         $this->_error($errorType);
00390         return false;
00391     }
00392 }
 All Data Structures Namespaces Files Functions Variables Enumerations