ContentValidator.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\File;
  7. use Magento\Downloadable\Api\Data\File\ContentInterface;
  8. use Magento\Framework\Exception\InputException;
  9. class ContentValidator
  10. {
  11. /**
  12. * Check if gallery entry content is valid
  13. *
  14. * @param ContentInterface $fileContent
  15. * @throws InputException
  16. * @return bool
  17. */
  18. public function isValid(ContentInterface $fileContent)
  19. {
  20. $decodedContent = @base64_decode($fileContent->getFileData(), true);
  21. if (empty($decodedContent)) {
  22. throw new InputException(__('Provided content must be valid base64 encoded data.'));
  23. }
  24. if (!$this->isFileNameValid($fileContent->getName())) {
  25. throw new InputException(__('Provided file name contains forbidden characters.'));
  26. }
  27. return true;
  28. }
  29. /**
  30. * Check if given filename is valid
  31. *
  32. * @param string $fileName
  33. * @return bool
  34. */
  35. protected function isFileNameValid($fileName)
  36. {
  37. // Cannot contain \ / : * ? " < > |
  38. if (!preg_match('/^[^\\/?*:";<>()|{}\\\\]+$/', $fileName)) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. }