PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/Validate/File/MimeType.php

https://bitbucket.org/simukti/zf1
PHP | 427 lines | 219 code | 47 blank | 161 comment | 39 complexity | b24a509226d97d33dec76630a06ce74a 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-2012 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 24593 2012-01-05 20:35:02Z 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-2012 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 => "File '%value%' has a false mimetype of '%type%'",
  47. self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
  48. self::NOT_READABLE => "File '%value%' is not readable or does not exist",
  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. * Finfo object to use
  76. *
  77. * @var resource
  78. */
  79. protected $_finfo;
  80. /**
  81. * If no $_ENV['MAGIC'] is set, try and autodiscover it based on common locations
  82. * @var array
  83. */
  84. protected $_magicFiles = array(
  85. '/usr/share/misc/magic',
  86. '/usr/share/misc/magic.mime',
  87. '/usr/share/misc/magic.mgc',
  88. '/usr/share/mime/magic',
  89. '/usr/share/mime/magic.mime',
  90. '/usr/share/mime/magic.mgc',
  91. '/usr/share/file/magic',
  92. '/usr/share/file/magic.mime',
  93. '/usr/share/file/magic.mgc',
  94. );
  95. /**
  96. * Indicates whether use of $_magicFiles should be attempted.
  97. * @var boolean
  98. */
  99. protected $_tryCommonMagicFiles = true;
  100. /**
  101. * Option to allow header check
  102. *
  103. * @var boolean
  104. */
  105. protected $_headerCheck = false;
  106. /**
  107. * Sets validator options
  108. *
  109. * Mimetype to accept
  110. *
  111. * @param string|array $mimetype MimeType
  112. * @return void
  113. */
  114. public function __construct($mimetype)
  115. {
  116. if ($mimetype instanceof Zend_Config) {
  117. $mimetype = $mimetype->toArray();
  118. } elseif (is_string($mimetype)) {
  119. $mimetype = explode(',', $mimetype);
  120. } elseif (!is_array($mimetype)) {
  121. require_once 'Zend/Validate/Exception.php';
  122. throw new Zend_Validate_Exception("Invalid options to validator provided");
  123. }
  124. if (isset($mimetype['magicfile'])) {
  125. $this->setMagicFile($mimetype['magicfile']);
  126. unset($mimetype['magicfile']);
  127. }
  128. if (isset($mimetype['headerCheck'])) {
  129. $this->enableHeaderCheck($mimetype['headerCheck']);
  130. unset($mimetype['headerCheck']);
  131. }
  132. $this->setMimeType($mimetype);
  133. }
  134. /**
  135. * Returns the actual set magicfile
  136. *
  137. * @return string
  138. */
  139. public function getMagicFile()
  140. {
  141. if (null === $this->_magicfile) {
  142. if (!empty($_ENV['MAGIC'])) {
  143. $this->setMagicFile($_ENV['MAGIC']);
  144. } elseif (
  145. !(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)
  146. && $this->shouldTryCommonMagicFiles() // @see ZF-11784
  147. ) {
  148. require_once 'Zend/Validate/Exception.php';
  149. foreach ($this->_magicFiles as $file) {
  150. // supressing errors which are thrown due to openbase_dir restrictions
  151. try {
  152. $this->setMagicFile($file);
  153. if ($this->_magicfile !== null) {
  154. break;
  155. }
  156. } catch (Zend_Validate_Exception $e) {
  157. // Intentionally, catch and fall through
  158. }
  159. }
  160. }
  161. if ($this->_magicfile === null) {
  162. $this->_magicfile = false;
  163. }
  164. }
  165. return $this->_magicfile;
  166. }
  167. /**
  168. * Sets the magicfile to use
  169. * if null, the MAGIC constant from php is used
  170. * if the MAGIC file is errorous, no file will be set
  171. *
  172. * @param string $file
  173. * @throws Zend_Validate_Exception When finfo can not read the magicfile
  174. * @return Zend_Validate_File_MimeType Provides fluid interface
  175. */
  176. public function setMagicFile($file)
  177. {
  178. if (empty($file)) {
  179. $this->_magicfile = null;
  180. } else if (!(class_exists('finfo', false))) {
  181. $this->_magicfile = null;
  182. require_once 'Zend/Validate/Exception.php';
  183. throw new Zend_Validate_Exception('Magicfile can not be set. There is no finfo extension installed');
  184. } else if (!is_file($file) || !is_readable($file)) {
  185. require_once 'Zend/Validate/Exception.php';
  186. throw new Zend_Validate_Exception('The given magicfile can not be read');
  187. } else {
  188. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  189. $this->_finfo = @finfo_open($const, $file);
  190. if (empty($this->_finfo)) {
  191. $this->_finfo = null;
  192. require_once 'Zend/Validate/Exception.php';
  193. throw new Zend_Validate_Exception('The given magicfile is not accepted by finfo');
  194. } else {
  195. $this->_magicfile = $file;
  196. }
  197. }
  198. return $this;
  199. }
  200. /**
  201. * Enables or disables attempts to try the common magic file locations
  202. * specified by Zend_Validate_File_MimeType::_magicFiles
  203. *
  204. * @param boolean $flag
  205. * @return Zend_Validate_File_MimeType Provides fluent interface
  206. * @see http://framework.zend.com/issues/browse/ZF-11784
  207. */
  208. public function setTryCommonMagicFilesFlag($flag = true)
  209. {
  210. $this->_tryCommonMagicFiles = (boolean) $flag;
  211. return $this;
  212. }
  213. /**
  214. * Accessor for Zend_Validate_File_MimeType::_magicFiles
  215. *
  216. * @return boolean
  217. * @see http://framework.zend.com/issues/browse/ZF-11784
  218. */
  219. public function shouldTryCommonMagicFiles()
  220. {
  221. return $this->_tryCommonMagicFiles;
  222. }
  223. /**
  224. * Returns the Header Check option
  225. *
  226. * @return boolean
  227. */
  228. public function getHeaderCheck()
  229. {
  230. return $this->_headerCheck;
  231. }
  232. /**
  233. * Defines if the http header should be used
  234. * Note that this is unsave and therefor the default value is false
  235. *
  236. * @param boolean $checkHeader
  237. * @return Zend_Validate_File_MimeType Provides fluid interface
  238. */
  239. public function enableHeaderCheck($headerCheck = true)
  240. {
  241. $this->_headerCheck = (boolean) $headerCheck;
  242. return $this;
  243. }
  244. /**
  245. * Returns the set mimetypes
  246. *
  247. * @param boolean $asArray Returns the values as array, when false an concated string is returned
  248. * @return string|array
  249. */
  250. public function getMimeType($asArray = false)
  251. {
  252. $asArray = (bool) $asArray;
  253. $mimetype = (string) $this->_mimetype;
  254. if ($asArray) {
  255. $mimetype = explode(',', $mimetype);
  256. }
  257. return $mimetype;
  258. }
  259. /**
  260. * Sets the mimetypes
  261. *
  262. * @param string|array $mimetype The mimetypes to validate
  263. * @return Zend_Validate_File_Extension Provides a fluent interface
  264. */
  265. public function setMimeType($mimetype)
  266. {
  267. $this->_mimetype = null;
  268. $this->addMimeType($mimetype);
  269. return $this;
  270. }
  271. /**
  272. * Adds the mimetypes
  273. *
  274. * @param string|array $mimetype The mimetypes to add for validation
  275. * @return Zend_Validate_File_Extension Provides a fluent interface
  276. */
  277. public function addMimeType($mimetype)
  278. {
  279. $mimetypes = $this->getMimeType(true);
  280. if (is_string($mimetype)) {
  281. $mimetype = explode(',', $mimetype);
  282. } elseif (!is_array($mimetype)) {
  283. require_once 'Zend/Validate/Exception.php';
  284. throw new Zend_Validate_Exception("Invalid options to validator provided");
  285. }
  286. if (isset($mimetype['magicfile'])) {
  287. unset($mimetype['magicfile']);
  288. }
  289. foreach ($mimetype as $content) {
  290. if (empty($content) || !is_string($content)) {
  291. continue;
  292. }
  293. $mimetypes[] = trim($content);
  294. }
  295. $mimetypes = array_unique($mimetypes);
  296. // Sanity check to ensure no empty values
  297. foreach ($mimetypes as $key => $mt) {
  298. if (empty($mt)) {
  299. unset($mimetypes[$key]);
  300. }
  301. }
  302. $this->_mimetype = implode(',', $mimetypes);
  303. return $this;
  304. }
  305. /**
  306. * Defined by Zend_Validate_Interface
  307. *
  308. * Returns true if the mimetype of the file matches the given ones. Also parts
  309. * of mimetypes can be checked. If you give for example "image" all image
  310. * mime types will be accepted like "image/gif", "image/jpeg" and so on.
  311. *
  312. * @param string $value Real file to check for mimetype
  313. * @param array $file File data from Zend_File_Transfer
  314. * @return boolean
  315. */
  316. public function isValid($value, $file = null)
  317. {
  318. if ($file === null) {
  319. $file = array(
  320. 'type' => null,
  321. 'name' => $value
  322. );
  323. }
  324. // Is file readable ?
  325. require_once 'Zend/Loader.php';
  326. if (!Zend_Loader::isReadable($value)) {
  327. return $this->_throw($file, self::NOT_READABLE);
  328. }
  329. $mimefile = $this->getMagicFile();
  330. if (class_exists('finfo', false)) {
  331. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  332. if (!empty($mimefile) && empty($this->_finfo)) {
  333. $this->_finfo = @finfo_open($const, $mimefile);
  334. }
  335. if (empty($this->_finfo)) {
  336. $this->_finfo = @finfo_open($const);
  337. }
  338. $this->_type = null;
  339. if (!empty($this->_finfo)) {
  340. $this->_type = finfo_file($this->_finfo, $value);
  341. }
  342. }
  343. if (empty($this->_type) &&
  344. (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
  345. $this->_type = mime_content_type($value);
  346. }
  347. if (empty($this->_type) && $this->_headerCheck) {
  348. $this->_type = $file['type'];
  349. }
  350. if (empty($this->_type)) {
  351. return $this->_throw($file, self::NOT_DETECTED);
  352. }
  353. $mimetype = $this->getMimeType(true);
  354. if (in_array($this->_type, $mimetype)) {
  355. return true;
  356. }
  357. $types = explode('/', $this->_type);
  358. $types = array_merge($types, explode('-', $this->_type));
  359. $types = array_merge($types, explode(';', $this->_type));
  360. foreach($mimetype as $mime) {
  361. if (in_array($mime, $types)) {
  362. return true;
  363. }
  364. }
  365. return $this->_throw($file, self::FALSE_TYPE);
  366. }
  367. /**
  368. * Throws an error of the given type
  369. *
  370. * @param string $file
  371. * @param string $errorType
  372. * @return false
  373. */
  374. protected function _throw($file, $errorType)
  375. {
  376. $this->_value = $file['name'];
  377. $this->_error($errorType);
  378. return false;
  379. }
  380. }