/vendor/magento/module-catalog/Model/Product/Option/Type/File/Validator.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 201 lines · 127 code · 14 blank · 60 comment · 16 complexity · 63d55dd598e3b849dc31e05c98041e13 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\Product\Option\Type\File;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. abstract class Validator
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  15. */
  16. protected $scopeConfig;
  17. /**
  18. * @var \Magento\Framework\File\Size
  19. */
  20. protected $fileSize;
  21. /**
  22. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  23. */
  24. protected $rootDirectory;
  25. /**
  26. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  27. * @param \Magento\Framework\Filesystem $filesystem
  28. * @param \Magento\Framework\File\Size $fileSize
  29. */
  30. public function __construct(
  31. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  32. \Magento\Framework\Filesystem $filesystem,
  33. \Magento\Framework\File\Size $fileSize
  34. ) {
  35. $this->scopeConfig = $scopeConfig;
  36. $this->rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
  37. $this->fileSize = $fileSize;
  38. }
  39. /**
  40. * Store Config value
  41. *
  42. * @param string $key Config value key
  43. * @return string
  44. */
  45. protected function getConfigData($key)
  46. {
  47. return $this->scopeConfig->getValue(
  48. 'catalog/custom_options/' . $key,
  49. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  50. );
  51. }
  52. /**
  53. * Get Error messages for validator Errors
  54. *
  55. * @param string[] $errors Array of validation failure message codes @see \Zend_Validate::getErrors()
  56. * @param array $fileInfo File info
  57. * @param \Magento\Catalog\Model\Product\Option $option
  58. * @return string[] Array of error messages
  59. * @see \Magento\Catalog\Model\Product\Option\Type\File::_getValidatorErrors
  60. */
  61. protected function getValidatorErrors($errors, $fileInfo, $option)
  62. {
  63. $result = [];
  64. foreach ($errors as $errorCode) {
  65. switch ($errorCode) {
  66. case \Zend_Validate_File_ExcludeExtension::FALSE_EXTENSION:
  67. $result[] = __(
  68. "The file '%1' for '%2' has an invalid extension.",
  69. $fileInfo['title'],
  70. $option->getTitle()
  71. );
  72. break;
  73. case \Zend_Validate_File_Extension::FALSE_EXTENSION:
  74. $result[] = __(
  75. "The file '%1' for '%2' has an invalid extension.",
  76. $fileInfo['title'],
  77. $option->getTitle()
  78. );
  79. break;
  80. case \Zend_Validate_File_ImageSize::WIDTH_TOO_BIG:
  81. case \Zend_Validate_File_ImageSize::HEIGHT_TOO_BIG:
  82. $result[] = __(
  83. "The maximum allowed image size for '%1' is %2x%3 px.",
  84. $option->getTitle(),
  85. $option->getImageSizeX(),
  86. $option->getImageSizeY()
  87. );
  88. break;
  89. case \Zend_Validate_File_FilesSize::TOO_BIG:
  90. $result[] = __(
  91. "The file '%1' you uploaded is larger than the %2 megabytes allowed by our server.",
  92. $fileInfo['title'],
  93. $this->fileSize->getMaxFileSizeInMb()
  94. );
  95. break;
  96. case \Zend_Validate_File_ImageSize::NOT_DETECTED:
  97. $result[] = __(
  98. "The file '%1' is empty. Please choose another one",
  99. $fileInfo['title']
  100. );
  101. break;
  102. default:
  103. $result[] = __(
  104. "The file '%1' is invalid. Please choose another one",
  105. $fileInfo['title']
  106. );
  107. }
  108. }
  109. return $result;
  110. }
  111. /**
  112. * Parse file extensions string with various separators
  113. *
  114. * @param string $extensions String to parse
  115. * @return array|null
  116. * @see \Magento\Catalog\Model\Product\Option\Type\File::_parseExtensionsString
  117. */
  118. protected function parseExtensionsString($extensions)
  119. {
  120. if (preg_match_all('/(?<extension>[a-z0-9]+)/si', strtolower($extensions), $matches)) {
  121. return $matches['extension'] ?: null;
  122. }
  123. return null;
  124. }
  125. /**
  126. * @param \Zend_File_Transfer_Adapter_Http|\Zend_Validate $object
  127. * @param \Magento\Catalog\Model\Product\Option $option
  128. * @param array $fileFullPath
  129. * @return \Zend_File_Transfer_Adapter_Http|\Zend_Validate $object
  130. * @throws \Magento\Framework\Exception\InputException
  131. */
  132. protected function buildImageValidator($object, $option, $fileFullPath = null)
  133. {
  134. $dimensions = [];
  135. if ($option->getImageSizeX() > 0) {
  136. $dimensions['maxwidth'] = $option->getImageSizeX();
  137. }
  138. if ($option->getImageSizeY() > 0) {
  139. $dimensions['maxheight'] = $option->getImageSizeY();
  140. }
  141. if (count($dimensions) > 0) {
  142. if ($fileFullPath !== null && !$this->isImage($fileFullPath)) {
  143. throw new \Magento\Framework\Exception\InputException(
  144. __('File \'%1\' is not an image.', $option->getTitle())
  145. );
  146. }
  147. $object->addValidator(new \Zend_Validate_File_ImageSize($dimensions));
  148. }
  149. // File extension
  150. $allowed = $this->parseExtensionsString($option->getFileExtension());
  151. if ($allowed !== null) {
  152. $object->addValidator(new \Zend_Validate_File_Extension($allowed));
  153. } else {
  154. $forbidden = $this->parseExtensionsString($this->getConfigData('forbidden_extensions'));
  155. if ($forbidden !== null) {
  156. $object->addValidator(new \Zend_Validate_File_ExcludeExtension($forbidden));
  157. }
  158. }
  159. $object->addValidator(
  160. new \Zend_Validate_File_FilesSize(['max' => $this->fileSize->getMaxFileSize()])
  161. );
  162. return $object;
  163. }
  164. /**
  165. * Simple check if file is image
  166. *
  167. * @param array|string $fileInfo - either file data from \Zend_File_Transfer or file path
  168. * @return boolean
  169. * @see \Magento\Catalog\Model\Product\Option\Type\File::_isImage
  170. */
  171. protected function isImage($fileInfo)
  172. {
  173. // Maybe array with file info came in
  174. if (is_array($fileInfo)) {
  175. return strstr($fileInfo['type'], 'image/');
  176. }
  177. // File path came in - check the physical file
  178. if (!$this->rootDirectory->isReadable($this->rootDirectory->getRelativePath($fileInfo))) {
  179. return false;
  180. }
  181. $imageInfo = getimagesize($fileInfo);
  182. if (!$imageInfo) {
  183. return false;
  184. }
  185. return true;
  186. }
  187. }