DownloadCustomOption.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Download;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\App\Action\Context;
  11. use Magento\Catalog\Model\Product\Type\AbstractType;
  12. use Magento\Framework\Controller\Result\ForwardFactory;
  13. /**
  14. * Class DownloadCustomOption
  15. *
  16. * @package Magento\Sales\Controller\Download
  17. */
  18. class DownloadCustomOption extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
  19. {
  20. /**
  21. * @var ForwardFactory
  22. */
  23. protected $resultForwardFactory;
  24. /**
  25. * @var \Magento\Sales\Model\Download
  26. */
  27. protected $download;
  28. /**
  29. * @var \Magento\Framework\Unserialize\Unserialize
  30. * @deprecated 101.0.0
  31. */
  32. protected $unserialize;
  33. /**
  34. * @var \Magento\Framework\Serialize\Serializer\Json
  35. */
  36. private $serializer;
  37. /**
  38. * @param Context $context
  39. * @param ForwardFactory $resultForwardFactory
  40. * @param \Magento\Sales\Model\Download $download
  41. * @param \Magento\Framework\Unserialize\Unserialize $unserialize
  42. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  43. */
  44. public function __construct(
  45. Context $context,
  46. ForwardFactory $resultForwardFactory,
  47. \Magento\Sales\Model\Download $download,
  48. \Magento\Framework\Unserialize\Unserialize $unserialize,
  49. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  50. ) {
  51. parent::__construct($context);
  52. $this->resultForwardFactory = $resultForwardFactory;
  53. $this->download = $download;
  54. $this->unserialize = $unserialize;
  55. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(
  56. \Magento\Framework\Serialize\Serializer\Json::class
  57. );
  58. }
  59. /**
  60. * Custom options download action
  61. *
  62. * @return void|\Magento\Framework\Controller\Result\Forward
  63. *
  64. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  65. */
  66. public function execute()
  67. {
  68. $quoteItemOptionId = $this->getRequest()->getParam('id');
  69. /** @var $option \Magento\Quote\Model\Quote\Item\Option */
  70. $option = $this->_objectManager->create(
  71. \Magento\Quote\Model\Quote\Item\Option::class
  72. )->load($quoteItemOptionId);
  73. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  74. $resultForward = $this->resultForwardFactory->create();
  75. if (!$option->getId()) {
  76. return $resultForward->forward('noroute');
  77. }
  78. $optionId = null;
  79. if (strpos($option->getCode(), AbstractType::OPTION_PREFIX) === 0) {
  80. $optionId = str_replace(AbstractType::OPTION_PREFIX, '', $option->getCode());
  81. if ((int)$optionId != $optionId) {
  82. $optionId = null;
  83. }
  84. }
  85. $productOption = null;
  86. if ($optionId) {
  87. /** @var $productOption \Magento\Catalog\Model\Product\Option */
  88. $productOption = $this->_objectManager->create(
  89. \Magento\Catalog\Model\Product\Option::class
  90. );
  91. $productOption->load($optionId);
  92. }
  93. if ($productOption->getId() && $productOption->getType() != 'file') {
  94. return $resultForward->forward('noroute');
  95. }
  96. try {
  97. $info = $this->serializer->unserialize($option->getValue());
  98. if ($this->getRequest()->getParam('key') != $info['secret_key']) {
  99. return $resultForward->forward('noroute');
  100. }
  101. $this->download->downloadFile($info);
  102. } catch (\Exception $e) {
  103. return $resultForward->forward('noroute');
  104. }
  105. $this->endExecute();
  106. }
  107. /**
  108. * Ends execution process
  109. *
  110. * @return void
  111. * @SuppressWarnings(PHPMD.ExitExpression)
  112. */
  113. protected function endExecute()
  114. {
  115. exit(0);
  116. }
  117. }