Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Viewfile.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Customer\Api\Data\AddressInterfaceFactory;
13 use Magento\Customer\Api\Data\CustomerInterfaceFactory;
17 use Magento\Framework\DataObjectFactory;
18 
23 {
27  protected $resultRawFactory;
28 
32  protected $urlDecoder;
33 
65  public function __construct(
66  \Magento\Backend\App\Action\Context $context,
67  \Magento\Framework\Registry $coreRegistry,
68  \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
69  \Magento\Customer\Model\CustomerFactory $customerFactory,
70  \Magento\Customer\Model\AddressFactory $addressFactory,
71  \Magento\Customer\Model\Metadata\FormFactory $formFactory,
72  \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
73  \Magento\Customer\Helper\View $viewHelper,
74  \Magento\Framework\Math\Random $random,
76  \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
80  CustomerInterfaceFactory $customerDataFactory,
81  AddressInterfaceFactory $addressDataFactory,
82  \Magento\Customer\Model\Customer\Mapper $customerMapper,
83  \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
85  DataObjectFactory $objectFactory,
86  \Magento\Framework\View\LayoutFactory $layoutFactory,
87  \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
88  \Magento\Framework\View\Result\PageFactory $resultPageFactory,
89  \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
90  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
91  \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
92  \Magento\Framework\Url\DecoderInterface $urlDecoder
93  ) {
94  parent::__construct(
95  $context,
96  $coreRegistry,
97  $fileFactory,
100  $formFactory,
101  $subscriberFactory,
102  $viewHelper,
103  $random,
105  $extensibleDataObjectConverter,
114  $objectFactory,
120  );
121  $this->resultRawFactory = $resultRawFactory;
122  $this->urlDecoder = $urlDecoder;
123  }
124 
133  public function execute()
134  {
135  list($file, $plain) = $this->getFileParams();
136 
138  $filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
139  $directory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
140  $fileName = CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER . '/' . ltrim($file, '/');
141  $path = $directory->getAbsolutePath($fileName);
142  if (mb_strpos($path, '..') !== false || (!$directory->isFile($fileName)
143  && !$this->_objectManager->get(\Magento\MediaStorage\Helper\File\Storage::class)->processStorageFile($path))
144  ) {
145  throw new NotFoundException(__('Page not found.'));
146  }
147 
148  if ($plain) {
149  $extension = pathinfo($path, PATHINFO_EXTENSION);
150  switch (strtolower($extension)) {
151  case 'gif':
152  $contentType = 'image/gif';
153  break;
154  case 'jpg':
155  $contentType = 'image/jpeg';
156  break;
157  case 'png':
158  $contentType = 'image/png';
159  break;
160  default:
161  $contentType = 'application/octet-stream';
162  break;
163  }
164  $stat = $directory->stat($fileName);
165  $contentLength = $stat['size'];
166  $contentModify = $stat['mtime'];
167 
169  $resultRaw = $this->resultRawFactory->create();
170  $resultRaw->setHttpResponseCode(200)
171  ->setHeader('Pragma', 'public', true)
172  ->setHeader('Content-type', $contentType, true)
173  ->setHeader('Content-Length', $contentLength)
174  ->setHeader('Last-Modified', date('r', $contentModify));
175  $resultRaw->setContents($directory->readFile($fileName));
176  return $resultRaw;
177  } else {
178  $name = pathinfo($path, PATHINFO_BASENAME);
179  $this->_fileFactory->create(
180  $name,
181  ['type' => 'filename', 'value' => $fileName],
182  DirectoryList::MEDIA
183  );
184  }
185  }
186 
193  private function getFileParams()
194  {
195  $file = null;
196  $plain = false;
197  if ($this->getRequest()->getParam('file')) {
198  // download file
199  $file = $this->urlDecoder->decode(
200  $this->getRequest()->getParam('file')
201  );
202  } elseif ($this->getRequest()->getParam('image')) {
203  // show plain image
204  $file = $this->urlDecoder->decode(
205  $this->getRequest()->getParam('image')
206  );
207  $plain = true;
208  } else {
209  throw new NotFoundException(__('Page not found.'));
210  }
211 
212  return [$file, $plain];
213  }
214 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$customerRepository
$fileName
Definition: translate.phtml:15
$addressFactory
Definition: quote.php:20
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\App\Response\Http\FileFactory $fileFactory, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Model\AddressFactory $addressFactory, \Magento\Customer\Model\Metadata\FormFactory $formFactory, \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory, \Magento\Customer\Helper\View $viewHelper, \Magento\Framework\Math\Random $random, CustomerRepositoryInterface $customerRepository, \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter, Mapper $addressMapper, AccountManagementInterface $customerAccountManagement, AddressRepositoryInterface $addressRepository, CustomerInterfaceFactory $customerDataFactory, AddressInterfaceFactory $addressDataFactory, \Magento\Customer\Model\Customer\Mapper $customerMapper, \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, DataObjectFactory $objectFactory, \Magento\Framework\View\LayoutFactory $layoutFactory, \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, \Magento\Framework\Url\DecoderInterface $urlDecoder)
Definition: Viewfile.php:65
$filesystem
if(!isset($_GET['name'])) $name
Definition: log.php:14