Download.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Helper;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Exception\LocalizedException as CoreException;
  10. /**
  11. * Downloadable Products Download Helper
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Download extends \Magento\Framework\App\Helper\AbstractHelper
  15. {
  16. /**
  17. * Link type url
  18. */
  19. const LINK_TYPE_URL = 'url';
  20. /**
  21. * Link type file
  22. */
  23. const LINK_TYPE_FILE = 'file';
  24. /**
  25. * Config path to content disposition
  26. */
  27. const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition';
  28. /**
  29. * Type of link
  30. *
  31. * @var string
  32. */
  33. protected $_linkType = self::LINK_TYPE_FILE;
  34. /**
  35. * Resource file
  36. *
  37. * @var string
  38. */
  39. protected $_resourceFile = null;
  40. /**
  41. * Resource open handle
  42. *
  43. * @var \Magento\Framework\Filesystem\File\ReadInterface
  44. */
  45. protected $_handle = null;
  46. /**
  47. * Remote server headers
  48. *
  49. * @var array
  50. */
  51. protected $_urlHeaders = [];
  52. /**
  53. * MIME Content-type for a file
  54. *
  55. * @var string
  56. */
  57. protected $_contentType = 'application/octet-stream';
  58. /**
  59. * File name
  60. *
  61. * @var string
  62. */
  63. protected $_fileName = 'download';
  64. /**
  65. * Core file storage database
  66. *
  67. * @var \Magento\MediaStorage\Helper\File\Storage\Database
  68. */
  69. protected $_coreFileStorageDb;
  70. /**
  71. * Downloadable file
  72. *
  73. * @var \Magento\Downloadable\Helper\File
  74. */
  75. protected $_downloadableFile;
  76. /**
  77. * @var \Magento\Framework\Filesystem
  78. */
  79. protected $_filesystem;
  80. /**
  81. * @var \Magento\Framework\Filesystem\File\ReadFactory
  82. */
  83. protected $fileReadFactory;
  84. /**
  85. * Working Directory (valid for LINK_TYPE_FILE only).
  86. * @var \Magento\Framework\Filesystem\Directory\Read
  87. */
  88. protected $_workingDirectory;
  89. /**
  90. * @var \Magento\Framework\Session\SessionManagerInterface
  91. */
  92. protected $_session;
  93. /**
  94. * @param \Magento\Framework\App\Helper\Context $context
  95. * @param File $downloadableFile
  96. * @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
  97. * @param Filesystem $filesystem
  98. * @param \Magento\Framework\Session\SessionManagerInterface $session
  99. * @param Filesystem\File\ReadFactory $fileReadFactory
  100. */
  101. public function __construct(
  102. \Magento\Framework\App\Helper\Context $context,
  103. \Magento\Downloadable\Helper\File $downloadableFile,
  104. \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
  105. \Magento\Framework\Filesystem $filesystem,
  106. \Magento\Framework\Session\SessionManagerInterface $session,
  107. \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory
  108. ) {
  109. parent::__construct($context);
  110. $this->_downloadableFile = $downloadableFile;
  111. $this->_coreFileStorageDb = $coreFileStorageDb;
  112. $this->_filesystem = $filesystem;
  113. $this->_session = $session;
  114. $this->fileReadFactory = $fileReadFactory;
  115. }
  116. /**
  117. * Retrieve Resource file handle (socket, file pointer etc)
  118. *
  119. * @return \Magento\Framework\Filesystem\File\ReadInterface
  120. * @throws CoreException|\Exception
  121. */
  122. protected function _getHandle()
  123. {
  124. if (!$this->_resourceFile) {
  125. throw new CoreException(__('Please set resource file and link type.'));
  126. }
  127. if ($this->_handle === null) {
  128. if ($this->_linkType == self::LINK_TYPE_URL) {
  129. $path = $this->_resourceFile;
  130. $protocol = strtolower(parse_url($path, PHP_URL_SCHEME));
  131. if ($protocol) {
  132. // Strip down protocol from path
  133. $path = preg_replace('#.+://#', '', $path);
  134. }
  135. $this->_handle = $this->fileReadFactory->create($path, $protocol);
  136. } elseif ($this->_linkType == self::LINK_TYPE_FILE) {
  137. $this->_workingDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
  138. $fileExists = $this->_downloadableFile->ensureFileInFilesystem($this->_resourceFile);
  139. if ($fileExists) {
  140. $this->_handle = $this->_workingDirectory->openFile($this->_resourceFile);
  141. } else {
  142. throw new CoreException(__('Invalid download link type.'));
  143. }
  144. } else {
  145. throw new CoreException(__('Invalid download link type.'));
  146. }
  147. }
  148. return $this->_handle;
  149. }
  150. /**
  151. * Retrieve file size in bytes
  152. *
  153. * @return int
  154. */
  155. public function getFileSize()
  156. {
  157. return $this->_getHandle()->stat($this->_resourceFile)['size'];
  158. }
  159. /**
  160. * Return MIME type of a file.
  161. *
  162. * @return string
  163. */
  164. public function getContentType()
  165. {
  166. $this->_getHandle();
  167. if ($this->_linkType == self::LINK_TYPE_FILE) {
  168. if (function_exists(
  169. 'mime_content_type'
  170. ) && ($contentType = mime_content_type(
  171. $this->_workingDirectory->getAbsolutePath($this->_resourceFile)
  172. ))
  173. ) {
  174. return $contentType;
  175. } else {
  176. return $this->_downloadableFile->getFileType($this->_resourceFile);
  177. }
  178. } elseif ($this->_linkType == self::LINK_TYPE_URL) {
  179. return $this->_handle->stat($this->_resourceFile)['type'];
  180. }
  181. return $this->_contentType;
  182. }
  183. /**
  184. * Return name of the file
  185. *
  186. * @return string
  187. */
  188. public function getFilename()
  189. {
  190. $this->_getHandle();
  191. if ($this->_linkType == self::LINK_TYPE_FILE) {
  192. return pathinfo($this->_resourceFile, PATHINFO_BASENAME);
  193. } elseif ($this->_linkType == self::LINK_TYPE_URL) {
  194. $stat = $this->_handle->stat($this->_resourceFile);
  195. if (isset($stat['disposition'])) {
  196. $contentDisposition = explode('; ', $stat['disposition']);
  197. if (!empty($contentDisposition[1]) && preg_match(
  198. '/filename=([^ ]+)/',
  199. $contentDisposition[1],
  200. $matches
  201. )
  202. ) {
  203. return $matches[1];
  204. }
  205. }
  206. $fileName = @pathinfo($this->_resourceFile, PATHINFO_BASENAME);
  207. if ($fileName) {
  208. return $fileName;
  209. }
  210. }
  211. return $this->_fileName;
  212. }
  213. /**
  214. * Set resource file for download
  215. *
  216. * @param string $resourceFile
  217. * @param string $linkType
  218. * @return $this
  219. * @throws \InvalidArgumentException
  220. */
  221. public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE)
  222. {
  223. if (self::LINK_TYPE_FILE == $linkType) {
  224. //check LFI protection
  225. if (preg_match('#\.\.[\\\/]#', $resourceFile)) {
  226. throw new \InvalidArgumentException(
  227. 'Requested file may not include parent directory traversal ("../", "..\\" notation)'
  228. );
  229. }
  230. }
  231. $this->_resourceFile = $resourceFile;
  232. $this->_linkType = $linkType;
  233. return $this;
  234. }
  235. /**
  236. * Output file contents
  237. *
  238. * @return void
  239. */
  240. public function output()
  241. {
  242. $handle = $this->_getHandle();
  243. $this->_session->writeClose();
  244. while (true == ($buffer = $handle->read(1024))) {
  245. echo $buffer; //@codingStandardsIgnoreLine
  246. }
  247. }
  248. /**
  249. * Use Content-Disposition: attachment
  250. *
  251. * @param mixed $store
  252. * @return bool
  253. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  254. */
  255. public function getContentDisposition($store = null)
  256. {
  257. return $this->scopeConfig->getValue(
  258. self::XML_PATH_CONTENT_DISPOSITION,
  259. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  260. $store
  261. );
  262. }
  263. }