Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MimeType.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
36 {
40  const FALSE_TYPE = 'fileMimeTypeFalse';
41  const NOT_DETECTED = 'fileMimeTypeNotDetected';
42  const NOT_READABLE = 'fileMimeTypeNotReadable';
43 
47  protected $_messageTemplates = array(
48  self::FALSE_TYPE => "File '%value%' has a false mimetype of '%type%'",
49  self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
50  self::NOT_READABLE => "File '%value%' is not readable or does not exist",
51  );
52 
56  protected $_messageVariables = array(
57  'type' => '_type'
58  );
59 
63  protected $_type;
64 
72  protected $_mimetype;
73 
79  protected $_magicfile;
80 
86  protected $_finfo;
87 
92  protected $_magicFiles = array(
93  '/usr/share/misc/magic',
94  '/usr/share/misc/magic.mime',
95  '/usr/share/misc/magic.mgc',
96  '/usr/share/mime/magic',
97  '/usr/share/mime/magic.mime',
98  '/usr/share/mime/magic.mgc',
99  '/usr/share/file/magic',
100  '/usr/share/file/magic.mime',
101  '/usr/share/file/magic.mgc',
102  );
103 
108  protected $_tryCommonMagicFiles = true;
109 
115  protected $_headerCheck = false;
116 
122  protected $_finfoError;
123 
132  public function __construct($mimetype)
133  {
134  if ($mimetype instanceof Zend_Config) {
135  $mimetype = $mimetype->toArray();
136  } elseif (is_string($mimetype)) {
137  $mimetype = explode(',', $mimetype);
138  } elseif (!is_array($mimetype)) {
139  #require_once 'Zend/Validate/Exception.php';
140  throw new Zend_Validate_Exception("Invalid options to validator provided");
141  }
142 
143  if (isset($mimetype['magicfile'])) {
144  $this->setMagicFile($mimetype['magicfile']);
145  unset($mimetype['magicfile']);
146  }
147 
148  if (isset($mimetype['headerCheck'])) {
149  $this->enableHeaderCheck($mimetype['headerCheck']);
150  unset($mimetype['headerCheck']);
151  }
152 
153  $this->setMimeType($mimetype);
154  }
155 
165  public function getMagicFile()
166  {
167  if (version_compare(PHP_VERSION, '5.3.0', '<')
168  && null === $this->_magicfile) {
169  if (!empty($_ENV['MAGIC'])) {
170  $this->setMagicFile($_ENV['MAGIC']);
171  } elseif (
172  !(@ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1)
173  && $this->shouldTryCommonMagicFiles() // @see ZF-11784
174  ) {
175  #require_once 'Zend/Validate/Exception.php';
176  foreach ($this->_magicFiles as $file) {
177  // supressing errors which are thrown due to openbase_dir restrictions
178  try {
179  $this->setMagicFile($file);
180  if ($this->_magicfile !== null) {
181  break;
182  }
183  } catch (Zend_Validate_Exception $e) {
184  // Intentionally, catch and fall through
185  }
186  }
187  }
188 
189  if ($this->_magicfile === null) {
190  $this->_magicfile = false;
191  }
192  }
193 
194  return $this->_magicfile;
195  }
196 
206  public function setMagicFile($file)
207  {
208  if (empty($file)) {
209  $this->_magicfile = null;
210  } else if (!(class_exists('finfo', false))) {
211  $this->_magicfile = null;
212  #require_once 'Zend/Validate/Exception.php';
213  throw new Zend_Validate_Exception('Magicfile can not be set. There is no finfo extension installed');
214  } else if (!is_file($file) || !is_readable($file)) {
215  #require_once 'Zend/Validate/Exception.php';
216  throw new Zend_Validate_Exception('The given magicfile can not be read');
217  } else {
218  $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
219  set_error_handler(array($this, '_errorHandler'), E_NOTICE | E_WARNING);
220  $this->_finfo = finfo_open($const, $file);
221  restore_error_handler();
222  if (empty($this->_finfo)) {
223  $this->_finfo = null;
224  #require_once 'Zend/Validate/Exception.php';
225  throw new Zend_Validate_Exception(
226  sprintf('The given magicfile ("%s") is not accepted by finfo', $file),
227  null,
228  $this->_finfoError
229  );
230  } else {
231  $this->_magicfile = $file;
232  }
233  }
234 
235  return $this;
236  }
237 
246  public function setTryCommonMagicFilesFlag($flag = true)
247  {
248  $this->_tryCommonMagicFiles = (boolean) $flag;
249 
250  return $this;
251  }
252 
259  public function shouldTryCommonMagicFiles()
260  {
262  }
263 
269  public function getHeaderCheck()
270  {
271  return $this->_headerCheck;
272  }
273 
281  public function enableHeaderCheck($headerCheck = true)
282  {
283  $this->_headerCheck = (boolean) $headerCheck;
284  return $this;
285  }
286 
293  public function getMimeType($asArray = false)
294  {
295  $asArray = (bool) $asArray;
296  $mimetype = (string) $this->_mimetype;
297  if ($asArray) {
298  $mimetype = explode(',', $mimetype);
299  }
300 
301  return $mimetype;
302  }
303 
310  public function setMimeType($mimetype)
311  {
312  $this->_mimetype = null;
313  $this->addMimeType($mimetype);
314  return $this;
315  }
316 
324  public function addMimeType($mimetype)
325  {
326  $mimetypes = $this->getMimeType(true);
327 
328  if (is_string($mimetype)) {
329  $mimetype = explode(',', $mimetype);
330  } elseif (!is_array($mimetype)) {
331  #require_once 'Zend/Validate/Exception.php';
332  throw new Zend_Validate_Exception("Invalid options to validator provided");
333  }
334 
335  if (isset($mimetype['magicfile'])) {
336  unset($mimetype['magicfile']);
337  }
338 
339  foreach ($mimetype as $content) {
340  if (empty($content) || !is_string($content)) {
341  continue;
342  }
343  $mimetypes[] = trim($content);
344  }
345  $mimetypes = array_unique($mimetypes);
346 
347  // Sanity check to ensure no empty values
348  foreach ($mimetypes as $key => $mt) {
349  if (empty($mt)) {
350  unset($mimetypes[$key]);
351  }
352  }
353 
354  $this->_mimetype = implode(',', $mimetypes);
355 
356  return $this;
357  }
358 
370  public function isValid($value, $file = null)
371  {
372  if ($file === null) {
373  $file = array(
374  'type' => null,
375  'name' => $value
376  );
377  }
378 
379  // Is file readable ?
380  #require_once 'Zend/Loader.php';
382  return $this->_throw($file, self::NOT_READABLE);
383  }
384 
385  $this->_type = $this->_detectMimeType($value);
386 
387  if (empty($this->_type) && $this->_headerCheck) {
388  $this->_type = $file['type'];
389  }
390 
391  if (empty($this->_type)) {
392  return $this->_throw($file, self::NOT_DETECTED);
393  }
394 
395  $mimetype = $this->getMimeType(true);
396  if (in_array($this->_type, $mimetype)) {
397  return true;
398  }
399 
400  $types = explode('/', $this->_type);
401  $types = array_merge($types, explode('-', $this->_type));
402  $types = array_merge($types, explode(';', $this->_type));
403  foreach($mimetype as $mime) {
404  if (in_array($mime, $types)) {
405  return true;
406  }
407  }
408 
409  return $this->_throw($file, self::FALSE_TYPE);
410  }
411 
419  protected function _throw($file, $errorType)
420  {
421  $this->_value = $file['name'];
422  $this->_error($errorType);
423  return false;
424  }
425 
431  protected function _detectMimeType($file)
432  {
433  $mimefile = $this->getMagicFile();
434  $type = null;
435 
436  if (class_exists('finfo', false)) {
437  $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
438 
439  if (!empty($mimefile) && empty($this->_finfo)) {
440  set_error_handler(array($this, '_errorHandler'), E_NOTICE | E_WARNING);
441  $this->_finfo = finfo_open($const, $mimefile);
442  restore_error_handler();
443  }
444 
445  if (empty($this->_finfo)) {
446  set_error_handler(array($this, '_errorHandler'), E_NOTICE | E_WARNING);
447  $this->_finfo = finfo_open($const);
448  restore_error_handler();
449  }
450 
451  if (!empty($this->_finfo)) {
452  $type = finfo_file($this->_finfo, $file);
453  }
454  }
455 
456  if (empty($type) &&
457  (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
458  $type = mime_content_type($file);
459  }
460 
461  return $type;
462  }
463 
472  protected function _errorHandler($errno, $errstr, $errfile, $errline)
473  {
474  $this->_finfoError = new ErrorException($errstr, $errno, 0, $errfile, $errline);
475  }
476 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
enableHeaderCheck($headerCheck=true)
Definition: MimeType.php:281
setTryCommonMagicFilesFlag($flag=true)
Definition: MimeType.php:246
_throw($file, $errorType)
Definition: MimeType.php:419
_error($messageKey, $value=null)
Definition: Abstract.php:284
isValid($value, $file=null)
Definition: MimeType.php:370
static isReadable($filename)
Definition: Loader.php:162
$type
Definition: item.phtml:13
_errorHandler($errno, $errstr, $errfile, $errline)
Definition: MimeType.php:472
$value
Definition: gender.phtml:16
getMimeType($asArray=false)
Definition: MimeType.php:293