NotProtectedExtension.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MediaStorage\Model\File\Validator;
  7. /**
  8. * Validator for check not protected file extensions
  9. */
  10. class NotProtectedExtension extends \Zend_Validate_Abstract
  11. {
  12. /**
  13. * Protected extension message key
  14. */
  15. const PROTECTED_EXTENSION = 'protectedExtension';
  16. /**
  17. * Protected files config path
  18. */
  19. const XML_PATH_PROTECTED_FILE_EXTENSIONS = 'general/file/protected_extensions';
  20. /**
  21. * The file extension
  22. *
  23. * @var string
  24. */
  25. protected $_value;
  26. /**
  27. * Protected file types
  28. *
  29. * @var string[]
  30. */
  31. protected $_protectedFileExtensions = [];
  32. /**
  33. * Core store config
  34. *
  35. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  36. */
  37. protected $_scopeConfig;
  38. /**
  39. * Init validator
  40. *
  41. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  42. */
  43. public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
  44. {
  45. $this->_scopeConfig = $scopeConfig;
  46. $this->_initMessageTemplates();
  47. $this->_initProtectedFileExtensions();
  48. }
  49. /**
  50. * Initialize message templates with translating
  51. *
  52. * @return $this
  53. */
  54. protected function _initMessageTemplates()
  55. {
  56. if (!$this->_messageTemplates) {
  57. $this->_messageTemplates = [
  58. self::PROTECTED_EXTENSION => __('File with an extension "%value%" is protected and cannot be uploaded'),
  59. ];
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Initialize protected file extensions
  65. *
  66. * @return $this
  67. */
  68. protected function _initProtectedFileExtensions()
  69. {
  70. if (!$this->_protectedFileExtensions) {
  71. $extensions = $this->getProtectedFileExtensions();
  72. if (is_string($extensions)) {
  73. $extensions = explode(',', $extensions);
  74. }
  75. foreach ($extensions as &$ext) {
  76. $ext = strtolower(trim($ext));
  77. }
  78. $this->_protectedFileExtensions = (array)$extensions;
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Return list with protected file extensions
  84. *
  85. * @param \Magento\Store\Model\Store|string|int $store
  86. * @return string|string[]
  87. */
  88. public function getProtectedFileExtensions($store = null)
  89. {
  90. return $this->_scopeConfig->getValue(
  91. self::XML_PATH_PROTECTED_FILE_EXTENSIONS,
  92. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  93. $store
  94. );
  95. }
  96. /**
  97. * Returns true if and only if $value meets the validation requirements
  98. *
  99. * If $value fails validation, then this method returns false, and
  100. * getMessages() will return an array of messages that explain why the
  101. * validation failed.
  102. *
  103. * @param string $value Extension of file
  104. * @return bool
  105. */
  106. public function isValid($value)
  107. {
  108. $value = strtolower(trim($value));
  109. $this->_setValue($value);
  110. if (in_array($this->_value, $this->_protectedFileExtensions)) {
  111. $this->_error(self::PROTECTED_EXTENSION, $this->_value);
  112. return false;
  113. }
  114. return true;
  115. }
  116. }