Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Download.php
Go to the documentation of this file.
1 <?php
8 
12 
18 {
22  const LINK_TYPE_URL = 'url';
23 
27  const LINK_TYPE_FILE = 'file';
28 
32  const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition';
33 
40 
46  protected $_resourceFile = null;
47 
53  protected $_handle = null;
54 
60  protected $_urlHeaders = [];
61 
67  protected $_contentType = 'application/octet-stream';
68 
74  protected $_fileName = 'download';
75 
82 
88  protected $_downloadableFile;
89 
93  protected $_filesystem;
94 
98  protected $fileReadFactory;
99 
105 
109  protected $_session;
110 
119  public function __construct(
120  \Magento\Framework\App\Helper\Context $context,
121  \Magento\Downloadable\Helper\File $downloadableFile,
122  \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
123  \Magento\Framework\Filesystem $filesystem,
124  \Magento\Framework\Session\SessionManagerInterface $session,
125  \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory
126  ) {
127  parent::__construct($context);
128  $this->_downloadableFile = $downloadableFile;
129  $this->_coreFileStorageDb = $coreFileStorageDb;
130  $this->_filesystem = $filesystem;
131  $this->_session = $session;
132  $this->fileReadFactory = $fileReadFactory;
133  }
134 
141  protected function _getHandle()
142  {
143  if (!$this->_resourceFile) {
144  throw new CoreException(__('Please set resource file and link type.'));
145  }
146 
147  if ($this->_handle === null) {
148  if ($this->_linkType == self::LINK_TYPE_URL) {
150  $protocol = strtolower(parse_url($path, PHP_URL_SCHEME));
151  if ($protocol) {
152  // Strip down protocol from path
153  $path = preg_replace('#.+://#', '', $path);
154  }
155  $this->_handle = $this->fileReadFactory->create($path, $protocol);
156  } elseif ($this->_linkType == self::LINK_TYPE_FILE) {
157  $this->_workingDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
158  $fileExists = $this->_downloadableFile->ensureFileInFilesystem($this->_resourceFile);
159  if ($fileExists) {
160  $this->_handle = $this->_workingDirectory->openFile($this->_resourceFile);
161  } else {
162  throw new CoreException(__('Invalid download link type.'));
163  }
164  } else {
165  throw new CoreException(__('Invalid download link type.'));
166  }
167  }
168  return $this->_handle;
169  }
170 
176  public function getFileSize()
177  {
178  return $this->_getHandle()->stat($this->_resourceFile)['size'];
179  }
180 
186  public function getContentType()
187  {
188  $this->_getHandle();
189  if ($this->_linkType == self::LINK_TYPE_FILE) {
190  if (function_exists(
191  'mime_content_type'
192  ) && ($contentType = mime_content_type(
193  $this->_workingDirectory->getAbsolutePath($this->_resourceFile)
194  ))
195  ) {
196  return $contentType;
197  } else {
198  return $this->_downloadableFile->getFileType($this->_resourceFile);
199  }
200  } elseif ($this->_linkType == self::LINK_TYPE_URL) {
201  return $this->_handle->stat($this->_resourceFile)['type'];
202  }
203  return $this->_contentType;
204  }
205 
211  public function getFilename()
212  {
213  $this->_getHandle();
214  if ($this->_linkType == self::LINK_TYPE_FILE) {
215  return pathinfo($this->_resourceFile, PATHINFO_BASENAME);
216  } elseif ($this->_linkType == self::LINK_TYPE_URL) {
217  $stat = $this->_handle->stat($this->_resourceFile);
218  if (isset($stat['disposition'])) {
219  $contentDisposition = explode('; ', $stat['disposition']);
220  if (!empty($contentDisposition[1]) && preg_match(
221  '/filename=([^ ]+)/',
222  $contentDisposition[1],
223  $matches
224  )
225  ) {
226  return $matches[1];
227  }
228  }
229  $fileName = @pathinfo($this->_resourceFile, PATHINFO_BASENAME);
230  if ($fileName) {
231  return $fileName;
232  }
233  }
234  return $this->_fileName;
235  }
236 
245  public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE)
246  {
247  if (self::LINK_TYPE_FILE == $linkType) {
248  //check LFI protection
249  if (preg_match('#\.\.[\\\/]#', $resourceFile)) {
250  throw new \InvalidArgumentException(
251  'Requested file may not include parent directory traversal ("../", "..\\" notation)'
252  );
253  }
254  }
255 
256  $this->_resourceFile = $resourceFile;
257  $this->_linkType = $linkType;
258 
259  return $this;
260  }
261 
267  public function output()
268  {
269  $handle = $this->_getHandle();
270  $this->_session->writeClose();
271  while (true == ($buffer = $handle->read(1024))) {
272  echo $buffer; //@codingStandardsIgnoreLine
273  }
274  }
275 
283  public function getContentDisposition($store = null)
284  {
285  return $this->scopeConfig->getValue(
286  self::XML_PATH_CONTENT_DISPOSITION,
287  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
288  $store
289  );
290  }
291 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$fileName
Definition: translate.phtml:15
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Downloadable\Helper\File $downloadableFile, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory)
Definition: Download.php:119
setResource($resourceFile, $linkType=self::LINK_TYPE_FILE)
Definition: Download.php:245
$handle
$filesystem