PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/library/Zend/Validate/File/MimeType.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 432 lines | 220 code | 47 blank | 165 comment | 40 complexity | 45a1014c6a5a7907c84bc34aaed10e89 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 25175 2012-12-22 20:47:08Z rob $
  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. * Note that for PHP 5.3.0 or higher, we don't use $_ENV['MAGIC'] or try to
  138. * find a magic file in a common location as PHP now has a built-in internal
  139. * magic file.
  140. *
  141. * @return string
  142. */
  143. public function getMagicFile()
  144. {
  145. if (version_compare(PHP_VERSION, '5.3.0', '<')
  146. && null === $this->_magicfile) {
  147. if (!empty($_ENV['MAGIC'])) {
  148. $this->setMagicFile($_ENV['MAGIC']);
  149. } elseif (
  150. !(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)
  151. && $this->shouldTryCommonMagicFiles() // @see ZF-11784
  152. ) {
  153. require_once 'Zend/Validate/Exception.php';
  154. foreach ($this->_magicFiles as $file) {
  155. // supressing errors which are thrown due to openbase_dir restrictions
  156. try {
  157. $this->setMagicFile($file);
  158. if ($this->_magicfile !== null) {
  159. break;
  160. }
  161. } catch (Zend_Validate_Exception $e) {
  162. // Intentionally, catch and fall through
  163. }
  164. }
  165. }
  166. if ($this->_magicfile === null) {
  167. $this->_magicfile = false;
  168. }
  169. }
  170. return $this->_magicfile;
  171. }
  172. /**
  173. * Sets the magicfile to use
  174. * if null, the MAGIC constant from php is used
  175. * if the MAGIC file is errorous, no file will be set
  176. *
  177. * @param string $file
  178. * @throws Zend_Validate_Exception When finfo can not read the magicfile
  179. * @return Zend_Validate_File_MimeType Provides fluid interface
  180. */
  181. public function setMagicFile($file)
  182. {
  183. if (empty($file)) {
  184. $this->_magicfile = null;
  185. } else if (!(class_exists('finfo', false))) {
  186. $this->_magicfile = null;
  187. require_once 'Zend/Validate/Exception.php';
  188. throw new Zend_Validate_Exception('Magicfile can not be set. There is no finfo extension installed');
  189. } else if (!is_file($file) || !is_readable($file)) {
  190. require_once 'Zend/Validate/Exception.php';
  191. throw new Zend_Validate_Exception('The given magicfile can not be read');
  192. } else {
  193. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  194. $this->_finfo = @finfo_open($const, $file);
  195. if (empty($this->_finfo)) {
  196. $this->_finfo = null;
  197. require_once 'Zend/Validate/Exception.php';
  198. throw new Zend_Validate_Exception('The given magicfile is not accepted by finfo');
  199. } else {
  200. $this->_magicfile = $file;
  201. }
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Enables or disables attempts to try the common magic file locations
  207. * specified by Zend_Validate_File_MimeType::_magicFiles
  208. *
  209. * @param boolean $flag
  210. * @return Zend_Validate_File_MimeType Provides fluent interface
  211. * @see http://framework.zend.com/issues/browse/ZF-11784
  212. */
  213. public function setTryCommonMagicFilesFlag($flag = true)
  214. {
  215. $this->_tryCommonMagicFiles = (boolean) $flag;
  216. return $this;
  217. }
  218. /**
  219. * Accessor for Zend_Validate_File_MimeType::_magicFiles
  220. *
  221. * @return boolean
  222. * @see http://framework.zend.com/issues/browse/ZF-11784
  223. */
  224. public function shouldTryCommonMagicFiles()
  225. {
  226. return $this->_tryCommonMagicFiles;
  227. }
  228. /**
  229. * Returns the Header Check option
  230. *
  231. * @return boolean
  232. */
  233. public function getHeaderCheck()
  234. {
  235. return $this->_headerCheck;
  236. }
  237. /**
  238. * Defines if the http header should be used
  239. * Note that this is unsave and therefor the default value is false
  240. *
  241. * @param boolean $checkHeader
  242. * @return Zend_Validate_File_MimeType Provides fluid interface
  243. */
  244. public function enableHeaderCheck($headerCheck = true)
  245. {
  246. $this->_headerCheck = (boolean) $headerCheck;
  247. return $this;
  248. }
  249. /**
  250. * Returns the set mimetypes
  251. *
  252. * @param boolean $asArray Returns the values as array, when false an concated string is returned
  253. * @return string|array
  254. */
  255. public function getMimeType($asArray = false)
  256. {
  257. $asArray = (bool) $asArray;
  258. $mimetype = (string) $this->_mimetype;
  259. if ($asArray) {
  260. $mimetype = explode(',', $mimetype);
  261. }
  262. return $mimetype;
  263. }
  264. /**
  265. * Sets the mimetypes
  266. *
  267. * @param string|array $mimetype The mimetypes to validate
  268. * @return Zend_Validate_File_Extension Provides a fluent interface
  269. */
  270. public function setMimeType($mimetype)
  271. {
  272. $this->_mimetype = null;
  273. $this->addMimeType($mimetype);
  274. return $this;
  275. }
  276. /**
  277. * Adds the mimetypes
  278. *
  279. * @param string|array $mimetype The mimetypes to add for validation
  280. * @return Zend_Validate_File_Extension Provides a fluent interface
  281. */
  282. public function addMimeType($mimetype)
  283. {
  284. $mimetypes = $this->getMimeType(true);
  285. if (is_string($mimetype)) {
  286. $mimetype = explode(',', $mimetype);
  287. } elseif (!is_array($mimetype)) {
  288. require_once 'Zend/Validate/Exception.php';
  289. throw new Zend_Validate_Exception("Invalid options to validator provided");
  290. }
  291. if (isset($mimetype['magicfile'])) {
  292. unset($mimetype['magicfile']);
  293. }
  294. foreach ($mimetype as $content) {
  295. if (empty($content) || !is_string($content)) {
  296. continue;
  297. }
  298. $mimetypes[] = trim($content);
  299. }
  300. $mimetypes = array_unique($mimetypes);
  301. // Sanity check to ensure no empty values
  302. foreach ($mimetypes as $key => $mt) {
  303. if (empty($mt)) {
  304. unset($mimetypes[$key]);
  305. }
  306. }
  307. $this->_mimetype = implode(',', $mimetypes);
  308. return $this;
  309. }
  310. /**
  311. * Defined by Zend_Validate_Interface
  312. *
  313. * Returns true if the mimetype of the file matches the given ones. Also parts
  314. * of mimetypes can be checked. If you give for example "image" all image
  315. * mime types will be accepted like "image/gif", "image/jpeg" and so on.
  316. *
  317. * @param string $value Real file to check for mimetype
  318. * @param array $file File data from Zend_File_Transfer
  319. * @return boolean
  320. */
  321. public function isValid($value, $file = null)
  322. {
  323. if ($file === null) {
  324. $file = array(
  325. 'type' => null,
  326. 'name' => $value
  327. );
  328. }
  329. // Is file readable ?
  330. require_once 'Zend/Loader.php';
  331. if (!Zend_Loader::isReadable($value)) {
  332. return $this->_throw($file, self::NOT_READABLE);
  333. }
  334. $mimefile = $this->getMagicFile();
  335. if (class_exists('finfo', false)) {
  336. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  337. if (!empty($mimefile) && empty($this->_finfo)) {
  338. $this->_finfo = @finfo_open($const, $mimefile);
  339. }
  340. if (empty($this->_finfo)) {
  341. $this->_finfo = @finfo_open($const);
  342. }
  343. $this->_type = null;
  344. if (!empty($this->_finfo)) {
  345. $this->_type = finfo_file($this->_finfo, $value);
  346. }
  347. }
  348. if (empty($this->_type) &&
  349. (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
  350. $this->_type = mime_content_type($value);
  351. }
  352. if (empty($this->_type) && $this->_headerCheck) {
  353. $this->_type = $file['type'];
  354. }
  355. if (empty($this->_type)) {
  356. return $this->_throw($file, self::NOT_DETECTED);
  357. }
  358. $mimetype = $this->getMimeType(true);
  359. if (in_array($this->_type, $mimetype)) {
  360. return true;
  361. }
  362. $types = explode('/', $this->_type);
  363. $types = array_merge($types, explode('-', $this->_type));
  364. $types = array_merge($types, explode(';', $this->_type));
  365. foreach($mimetype as $mime) {
  366. if (in_array($mime, $types)) {
  367. return true;
  368. }
  369. }
  370. return $this->_throw($file, self::FALSE_TYPE);
  371. }
  372. /**
  373. * Throws an error of the given type
  374. *
  375. * @param string $file
  376. * @param string $errorType
  377. * @return false
  378. */
  379. protected function _throw($file, $errorType)
  380. {
  381. $this->_value = $file['name'];
  382. $this->_error($errorType);
  383. return false;
  384. }
  385. }