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

/htdocs/zend/1.10.2-minapp/library/Zend/Validate/File/MimeType.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 359 lines | 184 code | 40 blank | 135 comment | 31 complexity | 1eca3ee6239ca71f3e2aca010a233034 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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-2010 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 20505 2010-01-21 21:40:23Z thomas $
  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-2010 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%' 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. unset($mimetype['magicfile']);
  116. }
  117. if (isset($mimetype['headerCheck'])) {
  118. $this->enableHeaderCheck($mimetype['headerCheck']);
  119. unset($mimetype['headerCheck']);
  120. }
  121. $this->setMimeType($mimetype);
  122. }
  123. /**
  124. * Returns the actual set magicfile
  125. *
  126. * @return string
  127. */
  128. public function getMagicFile()
  129. {
  130. if (null === $this->_magicfile) {
  131. if (!empty($_ENV['MAGIC'])) {
  132. $this->setMagicFile($_ENV['MAGIC']);
  133. } elseif (!(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)) {
  134. foreach ($this->_magicFiles as $file) {
  135. // supressing errors which are thrown due to openbase_dir restrictions
  136. if (@file_exists($file)) {
  137. $this->setMagicFile($file);
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. return $this->_magicfile;
  144. }
  145. /**
  146. * Sets the magicfile to use
  147. * if null, the MAGIC constant from php is used
  148. *
  149. * @param string $file
  150. * @return Zend_Validate_File_MimeType Provides fluid interface
  151. */
  152. public function setMagicFile($file)
  153. {
  154. if (empty($file)) {
  155. $this->_magicfile = null;
  156. } else if (!is_readable($file)) {
  157. require_once 'Zend/Validate/Exception.php';
  158. throw new Zend_Validate_Exception('The given magicfile can not be read');
  159. } else {
  160. $this->_magicfile = (string) $file;
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Returns the Header Check option
  166. *
  167. * @return boolean
  168. */
  169. public function getHeaderCheck()
  170. {
  171. return $this->_headerCheck;
  172. }
  173. /**
  174. * Defines if the http header should be used
  175. * Note that this is unsave and therefor the default value is false
  176. *
  177. * @param boolean $checkHeader
  178. * @return Zend_Validate_File_MimeType Provides fluid interface
  179. */
  180. public function enableHeaderCheck($headerCheck = true)
  181. {
  182. $this->_headerCheck = (boolean) $headerCheck;
  183. return $this;
  184. }
  185. /**
  186. * Returns the set mimetypes
  187. *
  188. * @param boolean $asArray Returns the values as array, when false an concated string is returned
  189. * @return string|array
  190. */
  191. public function getMimeType($asArray = false)
  192. {
  193. $asArray = (bool) $asArray;
  194. $mimetype = (string) $this->_mimetype;
  195. if ($asArray) {
  196. $mimetype = explode(',', $mimetype);
  197. }
  198. return $mimetype;
  199. }
  200. /**
  201. * Sets the mimetypes
  202. *
  203. * @param string|array $mimetype The mimetypes to validate
  204. * @return Zend_Validate_File_Extension Provides a fluent interface
  205. */
  206. public function setMimeType($mimetype)
  207. {
  208. $this->_mimetype = null;
  209. $this->addMimeType($mimetype);
  210. return $this;
  211. }
  212. /**
  213. * Adds the mimetypes
  214. *
  215. * @param string|array $mimetype The mimetypes to add for validation
  216. * @return Zend_Validate_File_Extension Provides a fluent interface
  217. */
  218. public function addMimeType($mimetype)
  219. {
  220. $mimetypes = $this->getMimeType(true);
  221. if (is_string($mimetype)) {
  222. $mimetype = explode(',', $mimetype);
  223. } elseif (!is_array($mimetype)) {
  224. require_once 'Zend/Validate/Exception.php';
  225. throw new Zend_Validate_Exception("Invalid options to validator provided");
  226. }
  227. if (isset($mimetype['magicfile'])) {
  228. unset($mimetype['magicfile']);
  229. }
  230. foreach ($mimetype as $content) {
  231. if (empty($content) || !is_string($content)) {
  232. continue;
  233. }
  234. $mimetypes[] = trim($content);
  235. }
  236. $mimetypes = array_unique($mimetypes);
  237. // Sanity check to ensure no empty values
  238. foreach ($mimetypes as $key => $mt) {
  239. if (empty($mt)) {
  240. unset($mimetypes[$key]);
  241. }
  242. }
  243. $this->_mimetype = implode(',', $mimetypes);
  244. return $this;
  245. }
  246. /**
  247. * Defined by Zend_Validate_Interface
  248. *
  249. * Returns true if the mimetype of the file matches the given ones. Also parts
  250. * of mimetypes can be checked. If you give for example "image" all image
  251. * mime types will be accepted like "image/gif", "image/jpeg" and so on.
  252. *
  253. * @param string $value Real file to check for mimetype
  254. * @param array $file File data from Zend_File_Transfer
  255. * @return boolean
  256. */
  257. public function isValid($value, $file = null)
  258. {
  259. if ($file === null) {
  260. $file = array(
  261. 'type' => null,
  262. 'name' => $value
  263. );
  264. }
  265. // Is file readable ?
  266. require_once 'Zend/Loader.php';
  267. if (!Zend_Loader::isReadable($value)) {
  268. return $this->_throw($file, self::NOT_READABLE);
  269. }
  270. $mimefile = $this->getMagicFile();
  271. if (class_exists('finfo', false)) {
  272. $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
  273. if (!empty($mimefile)) {
  274. $mime = new finfo($const, $mimefile);
  275. } else {
  276. $mime = new finfo($const);
  277. }
  278. if ($mime !== false) {
  279. $this->_type = $mime->file($value);
  280. }
  281. unset($mime);
  282. }
  283. if (empty($this->_type) &&
  284. (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
  285. $this->_type = mime_content_type($value);
  286. }
  287. if (empty($this->_type) && $this->_headerCheck) {
  288. $this->_type = $file['type'];
  289. }
  290. if (empty($this->_type)) {
  291. return $this->_throw($file, self::NOT_DETECTED);
  292. }
  293. $mimetype = $this->getMimeType(true);
  294. if (in_array($this->_type, $mimetype)) {
  295. return true;
  296. }
  297. $types = explode('/', $this->_type);
  298. $types = array_merge($types, explode('-', $this->_type));
  299. foreach($mimetype as $mime) {
  300. if (in_array($mime, $types)) {
  301. return true;
  302. }
  303. }
  304. return $this->_throw($file, self::FALSE_TYPE);
  305. }
  306. /**
  307. * Throws an error of the given type
  308. *
  309. * @param string $file
  310. * @param string $errorType
  311. * @return false
  312. */
  313. protected function _throw($file, $errorType)
  314. {
  315. $this->_value = $file['name'];
  316. $this->_error($errorType);
  317. return false;
  318. }
  319. }