PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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