Viewfile.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml\Index;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\Customer\Api\CustomerMetadataInterface;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  12. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  13. use Magento\Customer\Model\Address\Mapper;
  14. use Magento\Framework\Exception\NotFoundException;
  15. use Magento\Framework\App\Filesystem\DirectoryList;
  16. use Magento\Framework\DataObjectFactory;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class Viewfile extends \Magento\Customer\Controller\Adminhtml\Index
  21. {
  22. /**
  23. * @var \Magento\Framework\Controller\Result\RawFactory
  24. */
  25. protected $resultRawFactory;
  26. /**
  27. * @var \Magento\Framework\Url\DecoderInterface
  28. */
  29. protected $urlDecoder;
  30. /**
  31. * @param \Magento\Backend\App\Action\Context $context
  32. * @param \Magento\Framework\Registry $coreRegistry
  33. * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
  34. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  35. * @param \Magento\Customer\Model\AddressFactory $addressFactory
  36. * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
  37. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  38. * @param \Magento\Customer\Helper\View $viewHelper
  39. * @param \Magento\Framework\Math\Random $random
  40. * @param CustomerRepositoryInterface $customerRepository
  41. * @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
  42. * @param Mapper $addressMapper
  43. * @param AccountManagementInterface $customerAccountManagement
  44. * @param AddressRepositoryInterface $addressRepository
  45. * @param CustomerInterfaceFactory $customerDataFactory
  46. * @param AddressInterfaceFactory $addressDataFactory
  47. * @param \Magento\Customer\Model\Customer\Mapper $customerMapper
  48. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  49. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  50. * @param DataObjectFactory $objectFactory
  51. * @param \Magento\Framework\View\LayoutFactory $layoutFactory
  52. * @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
  53. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  54. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  55. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  56. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  57. * @param \Magento\Framework\Url\DecoderInterface $urlDecoder
  58. *
  59. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  60. */
  61. public function __construct(
  62. \Magento\Backend\App\Action\Context $context,
  63. \Magento\Framework\Registry $coreRegistry,
  64. \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
  65. \Magento\Customer\Model\CustomerFactory $customerFactory,
  66. \Magento\Customer\Model\AddressFactory $addressFactory,
  67. \Magento\Customer\Model\Metadata\FormFactory $formFactory,
  68. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  69. \Magento\Customer\Helper\View $viewHelper,
  70. \Magento\Framework\Math\Random $random,
  71. CustomerRepositoryInterface $customerRepository,
  72. \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
  73. Mapper $addressMapper,
  74. AccountManagementInterface $customerAccountManagement,
  75. AddressRepositoryInterface $addressRepository,
  76. CustomerInterfaceFactory $customerDataFactory,
  77. AddressInterfaceFactory $addressDataFactory,
  78. \Magento\Customer\Model\Customer\Mapper $customerMapper,
  79. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  80. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  81. DataObjectFactory $objectFactory,
  82. \Magento\Framework\View\LayoutFactory $layoutFactory,
  83. \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
  84. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  85. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  86. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  87. \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
  88. \Magento\Framework\Url\DecoderInterface $urlDecoder
  89. ) {
  90. parent::__construct(
  91. $context,
  92. $coreRegistry,
  93. $fileFactory,
  94. $customerFactory,
  95. $addressFactory,
  96. $formFactory,
  97. $subscriberFactory,
  98. $viewHelper,
  99. $random,
  100. $customerRepository,
  101. $extensibleDataObjectConverter,
  102. $addressMapper,
  103. $customerAccountManagement,
  104. $addressRepository,
  105. $customerDataFactory,
  106. $addressDataFactory,
  107. $customerMapper,
  108. $dataObjectProcessor,
  109. $dataObjectHelper,
  110. $objectFactory,
  111. $layoutFactory,
  112. $resultLayoutFactory,
  113. $resultPageFactory,
  114. $resultForwardFactory,
  115. $resultJsonFactory
  116. );
  117. $this->resultRawFactory = $resultRawFactory;
  118. $this->urlDecoder = $urlDecoder;
  119. }
  120. /**
  121. * Customer view file action
  122. *
  123. * @return \Magento\Framework\Controller\ResultInterface|void
  124. * @throws NotFoundException
  125. *
  126. * @SuppressWarnings(PHPMD.ExitExpression)
  127. */
  128. public function execute()
  129. {
  130. list($file, $plain) = $this->getFileParams();
  131. /** @var \Magento\Framework\Filesystem $filesystem */
  132. $filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
  133. $directory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
  134. $fileName = CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER . '/' . ltrim($file, '/');
  135. $path = $directory->getAbsolutePath($fileName);
  136. if (mb_strpos($path, '..') !== false || (!$directory->isFile($fileName)
  137. && !$this->_objectManager->get(\Magento\MediaStorage\Helper\File\Storage::class)->processStorageFile($path))
  138. ) {
  139. throw new NotFoundException(__('Page not found.'));
  140. }
  141. if ($plain) {
  142. $extension = pathinfo($path, PATHINFO_EXTENSION);
  143. switch (strtolower($extension)) {
  144. case 'gif':
  145. $contentType = 'image/gif';
  146. break;
  147. case 'jpg':
  148. $contentType = 'image/jpeg';
  149. break;
  150. case 'png':
  151. $contentType = 'image/png';
  152. break;
  153. default:
  154. $contentType = 'application/octet-stream';
  155. break;
  156. }
  157. $stat = $directory->stat($fileName);
  158. $contentLength = $stat['size'];
  159. $contentModify = $stat['mtime'];
  160. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  161. $resultRaw = $this->resultRawFactory->create();
  162. $resultRaw->setHttpResponseCode(200)
  163. ->setHeader('Pragma', 'public', true)
  164. ->setHeader('Content-type', $contentType, true)
  165. ->setHeader('Content-Length', $contentLength)
  166. ->setHeader('Last-Modified', date('r', $contentModify));
  167. $resultRaw->setContents($directory->readFile($fileName));
  168. return $resultRaw;
  169. } else {
  170. $name = pathinfo($path, PATHINFO_BASENAME);
  171. $this->_fileFactory->create(
  172. $name,
  173. ['type' => 'filename', 'value' => $fileName],
  174. DirectoryList::MEDIA
  175. );
  176. }
  177. }
  178. /**
  179. * Get parameters from request.
  180. *
  181. * @return array
  182. * @throws NotFoundException
  183. */
  184. private function getFileParams()
  185. {
  186. $file = null;
  187. $plain = false;
  188. if ($this->getRequest()->getParam('file')) {
  189. // download file
  190. $file = $this->urlDecoder->decode(
  191. $this->getRequest()->getParam('file')
  192. );
  193. } elseif ($this->getRequest()->getParam('image')) {
  194. // show plain image
  195. $file = $this->urlDecoder->decode(
  196. $this->getRequest()->getParam('image')
  197. );
  198. $plain = true;
  199. } else {
  200. throw new NotFoundException(__('Page not found.'));
  201. }
  202. return [$file, $plain];
  203. }
  204. }