PageRenderTime 68ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Validate/File/MimeType.php

https://github.com/gryzz/crystal_magento
PHP | 350 lines | 173 code | 38 blank | 139 comment | 28 complexity | bde88e921200be4c6e13d6dd09bb0a7b MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: MimeType.php 18513 2009-10-12 16:17:35Z matthew $
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * Validator for the mime type of a file
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
  34. {
  35. /**#@+
  36. * @const Error type constants
  37. */
  38. const FALSE_TYPE = 'fileMimeTypeFalse';
  39. const NOT_DETECTED = 'fileMimeTypeNotDetected';
  40. const NOT_READABLE = 'fileMimeTypeNotReadable';
  41. /**#@-*/
  42. /**
  43. * @var array Error message templates
  44. */
  45. protected $_messageTemplates = array(
  46. self::FALSE_TYPE => "The file '%value%' has a false mimetype of '%type%'",
  47. self::NOT_DETECTED => "The mimetype of file '%value%' could not been detected",
  48. self::NOT_READABLE => "The file '%value%' can not be read"
  49. );
  50. /**
  51. * @var array
  52. */
  53. protected $_messageVariables = array(
  54. 'type' => '_type'
  55. );
  56. /**
  57. * @var string
  58. */
  59. protected $_type;
  60. /**
  61. * Mimetypes
  62. *
  63. * If null, there is no mimetype
  64. *
  65. * @var string|null
  66. */
  67. protected $_mimetype;
  68. /**
  69. * Magicfile to use
  70. *
  71. * @var string|null
  72. */
  73. protected $_magicfile;
  74. /**
  75. * If no $_ENV['MAGIC'] is set, try and autodiscover it based on common locations
  76. * @var array
  77. */
  78. protected $_magicFiles = array(
  79. '/usr/share/misc/magic',
  80. '/usr/share/misc/magic.mime',
  81. '/usr/share/misc/magic.mgc',
  82. '/usr/share/mime/magic',
  83. '/usr/share/mime/magic.mime',
  84. '/usr/share/mime/magic.mgc',
  85. '/usr/share/file/magic',
  86. '/usr/share/file/magic.mime',
  87. '/usr/share/file/magic.mgc',
  88. );
  89. /**
  90. * Option to allow header check
  91. *
  92. * @var boolean
  93. */
  94. protected $_headerCheck = false;
  95. /**
  96. * Sets validator options
  97. *
  98. * Mimetype to accept
  99. *
  100. * @param string|array $mimetype MimeType
  101. * @return void
  102. */
  103. public function __construct($mimetype)
  104. {
  105. if ($mimetype instanceof Zend_Config) {
  106. $mimetype = $mimetype->toArray();
  107. } elseif (is_string($mimetype)) {
  108. $mimetype = explode(',', $mimetype);
  109. } elseif (!is_array($mimetype)) {
  110. #require_once 'Zend/Validate/Exception.php';
  111. throw new Zend_Validate_Exception("Invalid options to validator provided");
  112. }
  113. if (isset($mimetype['magicfile'])) {
  114. $this->setMagicFile($mimetype['magicfile']);
  115. }
  116. if (isset($mimetype['headerCheck'])) {
  117. $this->enableHeaderCheck(true);
  118. }
  119. $this->setMimeType($mimetype);
  120. }
  121. /**
  122. * Returns the actual set magicfile
  123. *
  124. * @return string
  125. */
  126. public function getMagicFile()
  127. {
  128. if (null === $this->_magicfile && empty($_ENV['MAGIC'])) {
  129. foreach ($this->_magicFiles as $file) {
  130. if (file_exists($file)) {
  131. $this->setMagicFile($file);
  132. break;
  133. }
  134. }
  135. }
  136. return $this->_magicfile;
  137. }
  138. /**
  139. * Sets the magicfile to use
  140. * if null, the MAGIC constant from php is used
  141. *
  142. * @param string $file
  143. * @return Zend_Validate_File_MimeType Provides fluid interface
  144. */
  145. public function setMagicFile($file)
  146. {
  147. if (empty($file)) {
  148. $this->_magicfile = null;
  149. } else if (!is_readable($file)) {
  150. #require_once 'Zend/Validate/Exception.php';
  151. throw new Zend_Validate_Exception('The given magicfile can not be read');
  152. } else {
  153. $this->_magicfile = (string) $file;
  154. }
  155. return $this;
  156. }
  157. /**
  158. * Returns the Header Check option
  159. *
  160. * @return boolean
  161. */
  162. public function getHeaderCheck()
  163. {
  164. return $this->_headerCheck;
  165. }
  166. /**
  167. * Defines if the http header should be used
  168. * Note that this is unsave and therefor the default value is false
  169. *
  170. * @param boolean $checkHeader
  171. * @return Zend_Validate_File_MimeType Provides fluid interface
  172. */
  173. public function enableHeaderCheck($headerCheck = true)
  174. {
  175. $this->_headerCheck = (boolean) $headerCheck;
  176. return $this;
  177. }
  178. /**
  179. * Returns the set mimetypes
  180. *
  181. * @param boolean $asArray Returns the values as array, when false an concated string is returned
  182. * @return string|array
  183. */
  184. public function getMimeType($asArray = false)
  185. {
  186. $asArray = (bool) $asArray;
  187. $mimetype = (string) $this->_mimetype;
  188. if ($asArray) {
  189. $mimetype = explode(',', $mimetype);
  190. }
  191. return $mimetype;
  192. }
  193. /**
  194. * Sets the mimetypes
  195. *
  196. * @param string|array $mimetype The mimetypes to validate
  197. * @return Zend_Validate_File_Extension Provides a fluent interface
  198. */
  199. public function setMimeType($mimetype)
  200. {
  201. $this->_mimetype = null;
  202. $this->addMimeType($mimetype);
  203. return $this;
  204. }
  205. /**
  206. * Adds the mimetypes
  207. *
  208. * @param string|array $mimetype The mimetypes to add for validation
  209. * @return Zend_Validate_File_Extension Provides a fluent interface
  210. */
  211. public function addMimeType($mimetype)
  212. {
  213. $mimetypes = $this->getMimeType(true);
  214. if (is_string($mimetype)) {
  215. $mimetype = explode(',', $mimetype);
  216. } elseif (!is_array($mimetype)) {
  217. #require_once 'Zend/Validate/Exception.php';
  218. throw new Zend_Validate_Exception("Invalid options to validator provided");
  219. }
  220. if (isset($mimetype['magicfile'])) {
  221. unset($mimetype['magicfile']);
  222. }
  223. foreach ($mimetype as $content) {
  224. if (empty($content) || !is_string($content)) {
  225. continue;
  226. }
  227. $mimetypes[] = trim($content);
  228. }
  229. $mimetypes = array_unique($mimetypes);
  230. // Sanity check to ensure no empty values
  231. foreach ($mimetypes as $key => $mt) {
  232. if (empty($mt)) {
  233. unset($mimetypes[$key]);
  234. }
  235. }
  236. $this->_mimetype = implode(',', $mimetypes);
  237. return $this;
  238. }
  239. /**
  240. * Defined by Zend_Validate_Interface
  241. *
  242. * Returns true if the mimetype of the file matches the given ones. Also parts
  243. * of mimetypes can be checked. If you give for example "image" all image
  244. * mime types will be accepted like "image/gif", "image/jpeg" and so on.
  245. *
  246. * @param string $value Real file to check for mimetype
  247. * @param array $file File data from Zend_File_Transfer
  248. * @return boolean
  249. */
  250. public function isValid($value, $file = null)
  251. {
  252. if ($file === null) {
  253. $file = array(
  254. 'type' => null,
  255. 'name' => $value
  256. );
  257. }
  258. // Is file readable ?
  259. #require_once 'Zend/Loader.php';
  260. if (!Zend_Loader::isReadable($value)) {
  261. return $this->_throw($file, self::NOT_READABLE);
  262. }
  263. $mimefile = $this->getMagicFile();
  264. if (class_exists('finfo', false)) {
  265. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  266. if (!empty($mimefile)) {
  267. $mime = new finfo($const, $mimefile);
  268. } else {
  269. $mime = new finfo($const);
  270. }
  271. if ($mime !== false) {
  272. $this->_type = $mime->file($value);
  273. }
  274. unset($mime);
  275. }
  276. if (empty($this->_type)) {
  277. if (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
  278. $this->_type = mime_content_type($value);
  279. } elseif ($this->_headerCheck) {
  280. $this->_type = $file['type'];
  281. }
  282. }
  283. if (empty($this->_type)) {
  284. return $this->_throw($file, self::NOT_DETECTED);
  285. }
  286. $mimetype = $this->getMimeType(true);
  287. if (in_array($this->_type, $mimetype)) {
  288. return true;
  289. }
  290. $types = explode('/', $this->_type);
  291. $types = array_merge($types, explode('-', $this->_type));
  292. foreach($mimetype as $mime) {
  293. if (in_array($mime, $types)) {
  294. return true;
  295. }
  296. }
  297. return $this->_throw($file, self::FALSE_TYPE);
  298. }
  299. /**
  300. * Throws an error of the given type
  301. *
  302. * @param string $file
  303. * @param string $errorType
  304. * @return false
  305. */
  306. protected function _throw($file, $errorType)
  307. {
  308. $this->_value = $file['name'];
  309. $this->_error($errorType);
  310. return false;
  311. }
  312. }