Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Validate_File_MimeType Class Reference
Inheritance diagram for Zend_Validate_File_MimeType:
Zend_Validate_Abstract Zend_Validate_Interface Zend_Validate_File_ExcludeMimeType Zend_Validate_File_IsCompressed Zend_Validate_File_IsImage IsImage

Public Member Functions

 __construct ($mimetype)
 
 getMagicFile ()
 
 setMagicFile ($file)
 
 setTryCommonMagicFilesFlag ($flag=true)
 
 shouldTryCommonMagicFiles ()
 
 getHeaderCheck ()
 
 enableHeaderCheck ($headerCheck=true)
 
 getMimeType ($asArray=false)
 
 setMimeType ($mimetype)
 
 addMimeType ($mimetype)
 
 isValid ($value, $file=null)
 
- Public Member Functions inherited from Zend_Validate_Abstract
 getMessages ()
 
 getMessageVariables ()
 
 getMessageTemplates ()
 
 setMessage ($messageString, $messageKey=null)
 
 setMessages (array $messages)
 
 __get ($property)
 
 getErrors ()
 
 setObscureValue ($flag)
 
 getObscureValue ()
 
 setTranslator ($translator=null)
 
 getTranslator ()
 
 hasTranslator ()
 
 setDisableTranslator ($flag)
 
 translatorIsDisabled ()
 
- Public Member Functions inherited from Zend_Validate_Interface
 isValid ($value)
 

Data Fields

const FALSE_TYPE = 'fileMimeTypeFalse'
 
const NOT_DETECTED = 'fileMimeTypeNotDetected'
 
const NOT_READABLE = 'fileMimeTypeNotReadable'
 

Protected Member Functions

 _throw ($file, $errorType)
 
 _detectMimeType ($file)
 
 _errorHandler ($errno, $errstr, $errfile, $errline)
 
- Protected Member Functions inherited from Zend_Validate_Abstract
 _createMessage ($messageKey, $value)
 
 _implodeRecursive (array $pieces)
 
 _error ($messageKey, $value=null)
 
 _setValue ($value)
 

Protected Attributes

 $_messageTemplates
 
 $_messageVariables
 
 $_type
 
 $_mimetype
 
 $_magicfile
 
 $_finfo
 
 $_magicFiles
 
 $_tryCommonMagicFiles = true
 
 $_headerCheck = false
 
 $_finfoError
 
- Protected Attributes inherited from Zend_Validate_Abstract
 $_value
 
 $_messageVariables = array()
 
 $_messageTemplates = array()
 
 $_messages = array()
 
 $_obscureValue = false
 
 $_errors = array()
 
 $_translator
 
 $_translatorDisabled = false
 

Additional Inherited Members

- Static Public Member Functions inherited from Zend_Validate_Abstract
static setDefaultTranslator ($translator=null)
 
static getDefaultTranslator ()
 
static hasDefaultTranslator ()
 
static getMessageLength ()
 
static setMessageLength ($length=-1)
 
- Static Protected Attributes inherited from Zend_Validate_Abstract
static $_defaultTranslator
 
static $_messageLength = -1
 

Detailed Description

Definition at line 35 of file MimeType.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $mimetype)

Sets validator options

Mimetype to accept

Parameters
string | array$mimetypeMimeType
Exceptions
Zend_Validate_Exception

Definition at line 132 of file MimeType.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
enableHeaderCheck($headerCheck=true)
Definition: MimeType.php:281

Member Function Documentation

◆ _detectMimeType()

_detectMimeType (   $file)
protected

Try to detect mime type of given file.

Parameters
string$fileFile which mime type should be detected
Returns
string File mime type or null if not detected

Definition at line 431 of file MimeType.php.

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  }
$type
Definition: item.phtml:13

◆ _errorHandler()

_errorHandler (   $errno,
  $errstr,
  $errfile,
  $errline 
)
protected

Saves the provided error information by finfo_open to this instance

Parameters
integer$errno
string$errstr
string$errfile
integer$errline

Definition at line 472 of file MimeType.php.

473  {
474  $this->_finfoError = new ErrorException($errstr, $errno, 0, $errfile, $errline);
475  }

◆ _throw()

_throw (   $file,
  $errorType 
)
protected

Throws an error of the given type

Parameters
string$file
string$errorType
Returns
false

Definition at line 419 of file MimeType.php.

420  {
421  $this->_value = $file['name'];
422  $this->_error($errorType);
423  return false;
424  }
_error($messageKey, $value=null)
Definition: Abstract.php:284

◆ addMimeType()

addMimeType (   $mimetype)

Adds the mimetypes

Parameters
string | array$mimetypeThe mimetypes to add for validation
Exceptions
Zend_Validate_Exception
Returns
Zend_Validate_File_Extension Provides a fluent interface

Definition at line 324 of file MimeType.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getMimeType($asArray=false)
Definition: MimeType.php:293

◆ enableHeaderCheck()

enableHeaderCheck (   $headerCheck = true)

Defines if the http header should be used Note that this is unsave and therefor the default value is false

Parameters
boolean$headerCheck
Returns
Zend_Validate_File_MimeType Provides a fluent interface

Definition at line 281 of file MimeType.php.

282  {
283  $this->_headerCheck = (boolean) $headerCheck;
284  return $this;
285  }

◆ getHeaderCheck()

getHeaderCheck ( )

Returns the Header Check option

Returns
boolean

Definition at line 269 of file MimeType.php.

270  {
271  return $this->_headerCheck;
272  }

◆ getMagicFile()

getMagicFile ( )

Returns the actual set magicfile

Note that for PHP 5.3.0 or higher, we don't use $_ENV['MAGIC'] or try to find a magic file in a common location as PHP now has a built-in internal magic file.

Returns
string

Definition at line 165 of file MimeType.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getMimeType()

getMimeType (   $asArray = false)

Returns the set mimetypes

Parameters
boolean$asArrayReturns the values as array, when false an concated string is returned
Returns
string|array

Definition at line 293 of file MimeType.php.

294  {
295  $asArray = (bool) $asArray;
296  $mimetype = (string) $this->_mimetype;
297  if ($asArray) {
298  $mimetype = explode(',', $mimetype);
299  }
300 
301  return $mimetype;
302  }

◆ isValid()

isValid (   $value,
  $file = null 
)

Defined by Zend_Validate_Interface

Returns true if the mimetype of the file matches the given ones. Also parts of mimetypes can be checked. If you give for example "image" all image mime types will be accepted like "image/gif", "image/jpeg" and so on.

Parameters
string$valueReal file to check for mimetype
array$fileFile data from Zend_File_Transfer
Returns
boolean

Definition at line 370 of file MimeType.php.

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  }
_throw($file, $errorType)
Definition: MimeType.php:419
static isReadable($filename)
Definition: Loader.php:162
$value
Definition: gender.phtml:16
getMimeType($asArray=false)
Definition: MimeType.php:293

◆ setMagicFile()

setMagicFile (   $file)

Sets the magicfile to use if null, the MAGIC constant from php is used if the MAGIC file is errorous, no file will be set

Parameters
string$file
Exceptions
Zend_Validate_ExceptionWhen finfo can not read the magicfile
Returns
Zend_Validate_File_MimeType Provides a fluent interface

Definition at line 206 of file MimeType.php.

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  }

◆ setMimeType()

setMimeType (   $mimetype)

Sets the mimetypes

Parameters
string | array$mimetypeThe mimetypes to validate
Returns
Zend_Validate_File_Extension Provides a fluent interface

Definition at line 310 of file MimeType.php.

311  {
312  $this->_mimetype = null;
313  $this->addMimeType($mimetype);
314  return $this;
315  }

◆ setTryCommonMagicFilesFlag()

setTryCommonMagicFilesFlag (   $flag = true)

Enables or disables attempts to try the common magic file locations specified by Zend_Validate_File_MimeType::_magicFiles

Parameters
boolean$flag
Returns
Zend_Validate_File_MimeType Provides fluent interface
See also
http://framework.zend.com/issues/browse/ZF-11784

Definition at line 246 of file MimeType.php.

247  {
248  $this->_tryCommonMagicFiles = (boolean) $flag;
249 
250  return $this;
251  }

◆ shouldTryCommonMagicFiles()

shouldTryCommonMagicFiles ( )

Accessor for Zend_Validate_File_MimeType::_magicFiles

Returns
boolean
See also
http://framework.zend.com/issues/browse/ZF-11784

Definition at line 259 of file MimeType.php.

260  {
262  }

Field Documentation

◆ $_finfo

$_finfo
protected

Definition at line 86 of file MimeType.php.

◆ $_finfoError

$_finfoError
protected

Definition at line 122 of file MimeType.php.

◆ $_headerCheck

$_headerCheck = false
protected

Definition at line 115 of file MimeType.php.

◆ $_magicfile

$_magicfile
protected

Definition at line 79 of file MimeType.php.

◆ $_magicFiles

$_magicFiles
protected
Initial value:
= array(
'/usr/share/misc/magic',
'/usr/share/misc/magic.mime',
'/usr/share/misc/magic.mgc',
'/usr/share/mime/magic',
'/usr/share/mime/magic.mime',
'/usr/share/mime/magic.mgc',
'/usr/share/file/magic',
'/usr/share/file/magic.mime',
'/usr/share/file/magic.mgc',
)

Definition at line 92 of file MimeType.php.

◆ $_messageTemplates

$_messageTemplates
protected
Initial value:
= array(
self::FALSE_TYPE => "File '%value%' has a false mimetype of '%type%'",
self::NOT_DETECTED => "The mimetype of file '%value%' could not be detected",
self::NOT_READABLE => "File '%value%' is not readable or does not exist",
)

Definition at line 47 of file MimeType.php.

◆ $_messageVariables

$_messageVariables
protected
Initial value:
= array(
'type' => '_type'
)

Definition at line 56 of file MimeType.php.

◆ $_mimetype

$_mimetype
protected

Definition at line 72 of file MimeType.php.

◆ $_tryCommonMagicFiles

$_tryCommonMagicFiles = true
protected

Definition at line 108 of file MimeType.php.

◆ $_type

$_type
protected

Definition at line 63 of file MimeType.php.

◆ FALSE_TYPE

const FALSE_TYPE = 'fileMimeTypeFalse'

@const Error type constants

Definition at line 40 of file MimeType.php.

◆ NOT_DETECTED

const NOT_DETECTED = 'fileMimeTypeNotDetected'

Definition at line 41 of file MimeType.php.

◆ NOT_READABLE

const NOT_READABLE = 'fileMimeTypeNotReadable'

Definition at line 42 of file MimeType.php.


The documentation for this class was generated from the following file: