Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageContentValidator.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Framework\Api;
9 
13 
18 {
22  private $defaultMimeTypes = [
23  'image/jpg',
24  'image/jpeg',
25  'image/gif',
26  'image/png',
27  ];
28 
32  private $allowedMimeTypes;
33 
37  public function __construct(
38  array $allowedMimeTypes = []
39  ) {
40  $this->allowedMimeTypes = array_merge($this->defaultMimeTypes, $allowedMimeTypes);
41  }
42 
51  {
52  $fileContent = @base64_decode($imageContent->getBase64EncodedData(), true);
53  if (empty($fileContent)) {
54  throw new InputException(new Phrase('The image content must be valid base64 encoded data.'));
55  }
56  $imageProperties = @getimagesizefromstring($fileContent);
57  if (empty($imageProperties)) {
58  throw new InputException(new Phrase('The image content must be valid base64 encoded data.'));
59  }
60  $sourceMimeType = $imageProperties['mime'];
61  if ($sourceMimeType != $imageContent->getType() || !$this->isMimeTypeValid($sourceMimeType)) {
62  throw new InputException(new Phrase('The image MIME type is not valid or not supported.'));
63  }
64  if (!$this->isNameValid($imageContent->getName())) {
65  throw new InputException(new Phrase('Provided image name contains forbidden characters.'));
66  }
67  return true;
68  }
69 
76  protected function isMimeTypeValid($mimeType)
77  {
78  return in_array($mimeType, $this->allowedMimeTypes);
79  }
80 
87  protected function isNameValid($name)
88  {
89  // Cannot contain \ / : * ? " < > |
90  if (!preg_match('/^[^\\/?*:";<>()|{}\\\\]+$/', $name)) {
91  return false;
92  }
93  return true;
94  }
95 }
isValid(ImageContentInterface $imageContent)
if(!isset($_GET['name'])) $name
Definition: log.php:14