PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zend-validator/src/File/MimeType.php

https://bitbucket.org/Prakruti24x7/stockmanage
PHP | 404 lines | 235 code | 47 blank | 122 comment | 33 complexity | 3239e2fa6f8723e17940c3861199f7ca MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0, MIT, BitTorrent-1.0, LGPL-2.0
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator\File;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. use Zend\Stdlib\ErrorHandler;
  13. use Zend\Validator\AbstractValidator;
  14. use Zend\Validator\Exception;
  15. use Zend\Validator\File\FileInformationTrait;
  16. /**
  17. * Validator for the mime type of a file
  18. */
  19. class MimeType extends AbstractValidator
  20. {
  21. use FileInformationTrait;
  22. /**#@+
  23. * @const Error type constants
  24. */
  25. const FALSE_TYPE = 'fileMimeTypeFalse';
  26. const NOT_DETECTED = 'fileMimeTypeNotDetected';
  27. const NOT_READABLE = 'fileMimeTypeNotReadable';
  28. /**#@-*/
  29. /**
  30. * @var array Error message templates
  31. */
  32. protected $messageTemplates = [
  33. self::FALSE_TYPE => "File has an incorrect mimetype of '%type%'",
  34. self::NOT_DETECTED => "The mimetype could not be detected from the file",
  35. self::NOT_READABLE => "File is not readable or does not exist",
  36. ];
  37. /**
  38. * @var array
  39. */
  40. protected $messageVariables = [
  41. 'type' => 'type'
  42. ];
  43. /**
  44. * @var string
  45. */
  46. protected $type;
  47. /**
  48. * Finfo object to use
  49. *
  50. * @var resource
  51. */
  52. protected $finfo;
  53. /**
  54. * If no environment variable 'MAGIC' is set, try and autodiscover it based on common locations
  55. * @var array
  56. */
  57. protected $magicFiles = [
  58. '/usr/share/misc/magic',
  59. '/usr/share/misc/magic.mime',
  60. '/usr/share/misc/magic.mgc',
  61. '/usr/share/mime/magic',
  62. '/usr/share/mime/magic.mime',
  63. '/usr/share/mime/magic.mgc',
  64. '/usr/share/file/magic',
  65. '/usr/share/file/magic.mime',
  66. '/usr/share/file/magic.mgc',
  67. ];
  68. /**
  69. * Options for this validator
  70. *
  71. * @var array
  72. */
  73. protected $options = [
  74. 'enableHeaderCheck' => false, // Allow header check
  75. 'disableMagicFile' => false, // Disable usage of magicfile
  76. 'magicFile' => null, // Magicfile to use
  77. 'mimeType' => null, // Mimetype to allow
  78. ];
  79. /**
  80. * Sets validator options
  81. *
  82. * Mimetype to accept
  83. * - NULL means default PHP usage by using the environment variable 'magic'
  84. * - FALSE means disabling searching for mimetype, should be used for PHP 5.3
  85. * - A string is the mimetype file to use
  86. *
  87. * @param string|array|Traversable $options
  88. */
  89. public function __construct($options = null)
  90. {
  91. if ($options instanceof Traversable) {
  92. $options = ArrayUtils::iteratorToArray($options);
  93. } elseif (is_string($options)) {
  94. $this->setMimeType($options);
  95. $options = [];
  96. } elseif (is_array($options)) {
  97. if (isset($options['magicFile'])) {
  98. $this->setMagicFile($options['magicFile']);
  99. unset($options['magicFile']);
  100. }
  101. if (isset($options['enableHeaderCheck'])) {
  102. $this->enableHeaderCheck($options['enableHeaderCheck']);
  103. unset($options['enableHeaderCheck']);
  104. }
  105. if (array_key_exists('mimeType', $options)) {
  106. $this->setMimeType($options['mimeType']);
  107. unset($options['mimeType']);
  108. }
  109. // Handle cases where mimetypes are interspersed with options, or
  110. // options are simply an array of mime types
  111. foreach (array_keys($options) as $key) {
  112. if (! is_int($key)) {
  113. continue;
  114. }
  115. $this->addMimeType($options[$key]);
  116. unset($options[$key]);
  117. }
  118. }
  119. parent::__construct($options);
  120. }
  121. /**
  122. * Returns the actual set magicfile
  123. *
  124. * @return string
  125. */
  126. public function getMagicFile()
  127. {
  128. if (null === $this->options['magicFile']) {
  129. $magic = getenv('magic');
  130. if (! empty($magic)) {
  131. $this->setMagicFile($magic);
  132. if ($this->options['magicFile'] === null) {
  133. $this->options['magicFile'] = false;
  134. }
  135. return $this->options['magicFile'];
  136. }
  137. foreach ($this->magicFiles as $file) {
  138. try {
  139. $this->setMagicFile($file);
  140. } catch (Exception\ExceptionInterface $e) {
  141. // suppressing errors which are thrown due to open_basedir restrictions
  142. continue;
  143. }
  144. if ($this->options['magicFile'] !== null) {
  145. return $this->options['magicFile'];
  146. }
  147. }
  148. if ($this->options['magicFile'] === null) {
  149. $this->options['magicFile'] = false;
  150. }
  151. }
  152. return $this->options['magicFile'];
  153. }
  154. /**
  155. * Sets the magicfile to use
  156. * if null, the MAGIC constant from php is used
  157. * if the MAGIC file is erroneous, no file will be set
  158. * if false, the default MAGIC file from PHP will be used
  159. *
  160. * @param string $file
  161. * @throws Exception\RuntimeException When finfo can not read the magicfile
  162. * @throws Exception\InvalidArgumentException
  163. * @throws Exception\InvalidMagicMimeFileException
  164. * @return self Provides fluid interface
  165. */
  166. public function setMagicFile($file)
  167. {
  168. if ($file === false) {
  169. $this->options['magicFile'] = false;
  170. } elseif (empty($file)) {
  171. $this->options['magicFile'] = null;
  172. } elseif (! (class_exists('finfo', false))) {
  173. $this->options['magicFile'] = null;
  174. throw new Exception\RuntimeException('Magicfile can not be set; there is no finfo extension installed');
  175. } elseif (! is_file($file) || ! is_readable($file)) {
  176. throw new Exception\InvalidArgumentException(sprintf(
  177. 'The given magicfile ("%s") could not be read',
  178. $file
  179. ));
  180. } else {
  181. ErrorHandler::start(E_NOTICE | E_WARNING);
  182. $this->finfo = finfo_open(FILEINFO_MIME_TYPE, $file);
  183. $error = ErrorHandler::stop();
  184. if (empty($this->finfo)) {
  185. $this->finfo = null;
  186. throw new Exception\InvalidMagicMimeFileException(sprintf(
  187. 'The given magicfile ("%s") could not be used by ext/finfo',
  188. $file
  189. ), 0, $error);
  190. }
  191. $this->options['magicFile'] = $file;
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Disables usage of MagicFile
  197. *
  198. * @param $disable boolean False disables usage of magic file
  199. * @return self Provides fluid interface
  200. */
  201. public function disableMagicFile($disable)
  202. {
  203. $this->options['disableMagicFile'] = (bool) $disable;
  204. return $this;
  205. }
  206. /**
  207. * Is usage of MagicFile disabled?
  208. *
  209. * @return bool
  210. */
  211. public function isMagicFileDisabled()
  212. {
  213. return $this->options['disableMagicFile'];
  214. }
  215. /**
  216. * Returns the Header Check option
  217. *
  218. * @return bool
  219. */
  220. public function getHeaderCheck()
  221. {
  222. return $this->options['enableHeaderCheck'];
  223. }
  224. /**
  225. * Defines if the http header should be used
  226. * Note that this is unsafe and therefor the default value is false
  227. *
  228. * @param bool $headerCheck
  229. * @return self Provides fluid interface
  230. */
  231. public function enableHeaderCheck($headerCheck = true)
  232. {
  233. $this->options['enableHeaderCheck'] = (bool) $headerCheck;
  234. return $this;
  235. }
  236. /**
  237. * Returns the set mimetypes
  238. *
  239. * @param bool $asArray Returns the values as array, when false a concatenated string is returned
  240. * @return string|array
  241. */
  242. public function getMimeType($asArray = false)
  243. {
  244. $asArray = (bool) $asArray;
  245. $mimetype = (string) $this->options['mimeType'];
  246. if ($asArray) {
  247. $mimetype = explode(',', $mimetype);
  248. }
  249. return $mimetype;
  250. }
  251. /**
  252. * Sets the mimetypes
  253. *
  254. * @param string|array $mimetype The mimetypes to validate
  255. * @return self Provides a fluent interface
  256. */
  257. public function setMimeType($mimetype)
  258. {
  259. $this->options['mimeType'] = null;
  260. $this->addMimeType($mimetype);
  261. return $this;
  262. }
  263. /**
  264. * Adds the mimetypes
  265. *
  266. * @param string|array $mimetype The mimetypes to add for validation
  267. * @throws Exception\InvalidArgumentException
  268. * @return self Provides a fluent interface
  269. */
  270. public function addMimeType($mimetype)
  271. {
  272. $mimetypes = $this->getMimeType(true);
  273. if (is_string($mimetype)) {
  274. $mimetype = explode(',', $mimetype);
  275. } elseif (! is_array($mimetype)) {
  276. throw new Exception\InvalidArgumentException("Invalid options to validator provided");
  277. }
  278. if (isset($mimetype['magicFile'])) {
  279. unset($mimetype['magicFile']);
  280. }
  281. foreach ($mimetype as $content) {
  282. if (empty($content) || ! is_string($content)) {
  283. continue;
  284. }
  285. $mimetypes[] = trim($content);
  286. }
  287. $mimetypes = array_unique($mimetypes);
  288. // Sanity check to ensure no empty values
  289. foreach ($mimetypes as $key => $mt) {
  290. if (empty($mt)) {
  291. unset($mimetypes[$key]);
  292. }
  293. }
  294. $this->options['mimeType'] = implode(',', $mimetypes);
  295. return $this;
  296. }
  297. /**
  298. * Defined by Zend\Validator\ValidatorInterface
  299. *
  300. * Returns true if the mimetype of the file matches the given ones. Also parts
  301. * of mimetypes can be checked. If you give for example "image" all image
  302. * mime types will be accepted like "image/gif", "image/jpeg" and so on.
  303. *
  304. * @param string|array $value Real file to check for mimetype
  305. * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
  306. * @return bool
  307. */
  308. public function isValid($value, $file = null)
  309. {
  310. $fileInfo = $this->getFileInfo($value, $file, true);
  311. $this->setValue($fileInfo['filename']);
  312. // Is file readable ?
  313. if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) {
  314. $this->error(static::NOT_READABLE);
  315. return false;
  316. }
  317. $mimefile = $this->getMagicFile();
  318. if (class_exists('finfo', false)) {
  319. if (! $this->isMagicFileDisabled() && (! empty($mimefile) && empty($this->finfo))) {
  320. ErrorHandler::start(E_NOTICE | E_WARNING);
  321. $this->finfo = finfo_open(FILEINFO_MIME_TYPE, $mimefile);
  322. ErrorHandler::stop();
  323. }
  324. if (empty($this->finfo)) {
  325. ErrorHandler::start(E_NOTICE | E_WARNING);
  326. $this->finfo = finfo_open(FILEINFO_MIME_TYPE);
  327. ErrorHandler::stop();
  328. }
  329. $this->type = null;
  330. if (! empty($this->finfo)) {
  331. $this->type = finfo_file($this->finfo, $fileInfo['file']);
  332. unset($this->finfo);
  333. }
  334. }
  335. if (empty($this->type) && $this->getHeaderCheck()) {
  336. $this->type = $fileInfo['filetype'];
  337. }
  338. if (empty($this->type)) {
  339. $this->error(static::NOT_DETECTED);
  340. return false;
  341. }
  342. $mimetype = $this->getMimeType(true);
  343. if (in_array($this->type, $mimetype)) {
  344. return true;
  345. }
  346. $types = explode('/', $this->type);
  347. $types = array_merge($types, explode('-', $this->type));
  348. $types = array_merge($types, explode(';', $this->type));
  349. foreach ($mimetype as $mime) {
  350. if (in_array($mime, $types)) {
  351. return true;
  352. }
  353. }
  354. $this->error(static::FALSE_TYPE);
  355. return false;
  356. }
  357. }