PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/front/GetFileController.php

https://gitlab.com/staging06/myproject
PHP | 332 lines | 268 code | 25 blank | 39 comment | 47 complexity | dbbfb417f26818c52775569fc5b23880 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class GetFileControllerCore extends FrontController
  27. {
  28. protected $display_header = false;
  29. protected $display_footer = false;
  30. public function init()
  31. {
  32. if (isset($this->context->employee) && $this->context->employee->isLoggedBack() && Tools::getValue('file')) {
  33. // Admin can directly access to file
  34. $filename = Tools::getValue('file');
  35. if (!Validate::isSha1($filename)) {
  36. die(Tools::displayError());
  37. }
  38. $file = _PS_DOWNLOAD_DIR_.strval(preg_replace('/\.{2,}/', '.', $filename));
  39. $filename = ProductDownload::getFilenameFromFilename(Tools::getValue('file'));
  40. if (empty($filename)) {
  41. $newFileName = Tools::getValue('filename');
  42. if (!empty($newFileName)) {
  43. $filename = Tools::getValue('filename');
  44. } else {
  45. $filename = 'file';
  46. }
  47. }
  48. if (!file_exists($file)) {
  49. Tools::redirect('index.php');
  50. }
  51. } else {
  52. if (!($key = Tools::getValue('key'))) {
  53. $this->displayCustomError('Invalid key.');
  54. }
  55. Tools::setCookieLanguage();
  56. if (!$this->context->customer->isLogged() && !Tools::getValue('secure_key') && !Tools::getValue('id_order')) {
  57. Tools::redirect('index.php?controller=authentication&back=get-file.php&key='.$key);
  58. } elseif (!$this->context->customer->isLogged() && Tools::getValue('secure_key') && Tools::getValue('id_order')) {
  59. $order = new Order((int)Tools::getValue('id_order'));
  60. if (!Validate::isLoadedObject($order)) {
  61. $this->displayCustomError('Invalid key.');
  62. }
  63. if ($order->secure_key != Tools::getValue('secure_key')) {
  64. $this->displayCustomError('Invalid key.');
  65. }
  66. }
  67. /* Key format: <sha1-filename>-<hashOrder> */
  68. $tmp = explode('-', $key);
  69. if (count($tmp) != 2) {
  70. $this->displayCustomError('Invalid key.');
  71. }
  72. $filename = $tmp[0];
  73. $hash = $tmp[1];
  74. if (!($info = OrderDetail::getDownloadFromHash($hash))) {
  75. $this->displayCustomError('This product does not exist in our store.');
  76. }
  77. /* Product no more present in catalog */
  78. if (!isset($info['id_product_download']) || empty($info['id_product_download'])) {
  79. $this->displayCustomError('This product has been deleted.');
  80. }
  81. if (!Validate::isFileName($info['filename']) || !file_exists(_PS_DOWNLOAD_DIR_.$info['filename'])) {
  82. $this->displayCustomError('This file no longer exists.');
  83. }
  84. if (isset($info['product_quantity_refunded']) && isset($info['product_quantity_return']) &&
  85. ($info['product_quantity_refunded'] > 0 || $info['product_quantity_return'] > 0)) {
  86. $this->displayCustomError('This product has been refunded.');
  87. }
  88. $now = time();
  89. $product_deadline = strtotime($info['download_deadline']);
  90. if ($now > $product_deadline && $info['download_deadline'] != '0000-00-00 00:00:00') {
  91. $this->displayCustomError('The product deadline is in the past.');
  92. }
  93. $customer_deadline = strtotime($info['date_expiration']);
  94. if ($now > $customer_deadline && $info['date_expiration'] != '0000-00-00 00:00:00') {
  95. $this->displayCustomError('Expiration date has passed, you cannot download this product');
  96. }
  97. if ($info['download_nb'] >= $info['nb_downloadable'] && $info['nb_downloadable']) {
  98. $this->displayCustomError('You have reached the maximum number of allowed downloads.');
  99. }
  100. /* Access is authorized -> increment download value for the customer */
  101. OrderDetail::incrementDownload($info['id_order_detail']);
  102. $file = _PS_DOWNLOAD_DIR_.$info['filename'];
  103. $filename = $info['display_filename'];
  104. }
  105. /* Detect mime content type */
  106. $mimeType = false;
  107. if (function_exists('finfo_open')) {
  108. $finfo = @finfo_open(FILEINFO_MIME);
  109. $mimeType = @finfo_file($finfo, $file);
  110. @finfo_close($finfo);
  111. } elseif (function_exists('mime_content_type')) {
  112. $mimeType = @mime_content_type($file);
  113. } elseif (function_exists('exec')) {
  114. $mimeType = trim(@exec('file -b --mime-type '.escapeshellarg($file)));
  115. if (!$mimeType) {
  116. $mimeType = trim(@exec('file --mime '.escapeshellarg($file)));
  117. }
  118. if (!$mimeType) {
  119. $mimeType = trim(@exec('file -bi '.escapeshellarg($file)));
  120. }
  121. }
  122. if (empty($mimeType)) {
  123. $bName = basename($filename);
  124. $bName = explode('.', $bName);
  125. $bName = strtolower($bName[count($bName) - 1]);
  126. $mimeTypes = array(
  127. 'ez' => 'application/andrew-inset',
  128. 'hqx' => 'application/mac-binhex40',
  129. 'cpt' => 'application/mac-compactpro',
  130. 'doc' => 'application/msword',
  131. 'oda' => 'application/oda',
  132. 'pdf' => 'application/pdf',
  133. 'ai' => 'application/postscript',
  134. 'eps' => 'application/postscript',
  135. 'ps' => 'application/postscript',
  136. 'smi' => 'application/smil',
  137. 'smil' => 'application/smil',
  138. 'wbxml' => 'application/vnd.wap.wbxml',
  139. 'wmlc' => 'application/vnd.wap.wmlc',
  140. 'wmlsc' => 'application/vnd.wap.wmlscriptc',
  141. 'bcpio' => 'application/x-bcpio',
  142. 'vcd' => 'application/x-cdlink',
  143. 'pgn' => 'application/x-chess-pgn',
  144. 'cpio' => 'application/x-cpio',
  145. 'csh' => 'application/x-csh',
  146. 'dcr' => 'application/x-director',
  147. 'dir' => 'application/x-director',
  148. 'dxr' => 'application/x-director',
  149. 'dvi' => 'application/x-dvi',
  150. 'spl' => 'application/x-futuresplash',
  151. 'gtar' => 'application/x-gtar',
  152. 'hdf' => 'application/x-hdf',
  153. 'js' => 'application/x-javascript',
  154. 'skp' => 'application/x-koan',
  155. 'skd' => 'application/x-koan',
  156. 'skt' => 'application/x-koan',
  157. 'skm' => 'application/x-koan',
  158. 'latex' => 'application/x-latex',
  159. 'nc' => 'application/x-netcdf',
  160. 'cdf' => 'application/x-netcdf',
  161. 'sh' => 'application/x-sh',
  162. 'shar' => 'application/x-shar',
  163. 'swf' => 'application/x-shockwave-flash',
  164. 'sit' => 'application/x-stuffit',
  165. 'sv4cpio' => 'application/x-sv4cpio',
  166. 'sv4crc' => 'application/x-sv4crc',
  167. 'tar' => 'application/x-tar',
  168. 'tcl' => 'application/x-tcl',
  169. 'tex' => 'application/x-tex',
  170. 'texinfo' => 'application/x-texinfo',
  171. 'texi' => 'application/x-texinfo',
  172. 't' => 'application/x-troff',
  173. 'tr' => 'application/x-troff',
  174. 'roff' => 'application/x-troff',
  175. 'man' => 'application/x-troff-man',
  176. 'me' => 'application/x-troff-me',
  177. 'ms' => 'application/x-troff-ms',
  178. 'ustar' => 'application/x-ustar',
  179. 'src' => 'application/x-wais-source',
  180. 'xhtml' => 'application/xhtml+xml',
  181. 'xht' => 'application/xhtml+xml',
  182. 'zip' => 'application/zip',
  183. 'au' => 'audio/basic',
  184. 'snd' => 'audio/basic',
  185. 'mid' => 'audio/midi',
  186. 'midi' => 'audio/midi',
  187. 'kar' => 'audio/midi',
  188. 'mpga' => 'audio/mpeg',
  189. 'mp2' => 'audio/mpeg',
  190. 'mp3' => 'audio/mpeg',
  191. 'aif' => 'audio/x-aiff',
  192. 'aiff' => 'audio/x-aiff',
  193. 'aifc' => 'audio/x-aiff',
  194. 'm3u' => 'audio/x-mpegurl',
  195. 'ram' => 'audio/x-pn-realaudio',
  196. 'rm' => 'audio/x-pn-realaudio',
  197. 'rpm' => 'audio/x-pn-realaudio-plugin',
  198. 'ra' => 'audio/x-realaudio',
  199. 'wav' => 'audio/x-wav',
  200. 'pdb' => 'chemical/x-pdb',
  201. 'xyz' => 'chemical/x-xyz',
  202. 'bmp' => 'image/bmp',
  203. 'gif' => 'image/gif',
  204. 'ief' => 'image/ief',
  205. 'jpeg' => 'image/jpeg',
  206. 'jpg' => 'image/jpeg',
  207. 'jpe' => 'image/jpeg',
  208. 'png' => 'image/png',
  209. 'tiff' => 'image/tiff',
  210. 'tif' => 'image/tif',
  211. 'djvu' => 'image/vnd.djvu',
  212. 'djv' => 'image/vnd.djvu',
  213. 'wbmp' => 'image/vnd.wap.wbmp',
  214. 'ras' => 'image/x-cmu-raster',
  215. 'pnm' => 'image/x-portable-anymap',
  216. 'pbm' => 'image/x-portable-bitmap',
  217. 'pgm' => 'image/x-portable-graymap',
  218. 'ppm' => 'image/x-portable-pixmap',
  219. 'rgb' => 'image/x-rgb',
  220. 'xbm' => 'image/x-xbitmap',
  221. 'xpm' => 'image/x-xpixmap',
  222. 'xwd' => 'image/x-windowdump',
  223. 'igs' => 'model/iges',
  224. 'iges' => 'model/iges',
  225. 'msh' => 'model/mesh',
  226. 'mesh' => 'model/mesh',
  227. 'silo' => 'model/mesh',
  228. 'wrl' => 'model/vrml',
  229. 'vrml' => 'model/vrml',
  230. 'css' => 'text/css',
  231. 'html' => 'text/html',
  232. 'htm' => 'text/html',
  233. 'asc' => 'text/plain',
  234. 'txt' => 'text/plain',
  235. 'rtx' => 'text/richtext',
  236. 'rtf' => 'text/rtf',
  237. 'sgml' => 'text/sgml',
  238. 'sgm' => 'text/sgml',
  239. 'tsv' => 'text/tab-seperated-values',
  240. 'wml' => 'text/vnd.wap.wml',
  241. 'wmls' => 'text/vnd.wap.wmlscript',
  242. 'etx' => 'text/x-setext',
  243. 'xml' => 'text/xml',
  244. 'xsl' => 'text/xml',
  245. 'mpeg' => 'video/mpeg',
  246. 'mpg' => 'video/mpeg',
  247. 'mpe' => 'video/mpeg',
  248. 'qt' => 'video/quicktime',
  249. 'mov' => 'video/quicktime',
  250. 'mxu' => 'video/vnd.mpegurl',
  251. 'avi' => 'video/x-msvideo',
  252. 'movie' => 'video/x-sgi-movie',
  253. 'ice' => 'x-conference-xcooltalk');
  254. if (isset($mimeTypes[$bName])) {
  255. $mimeType = $mimeTypes[$bName];
  256. } else {
  257. $mimeType = 'application/octet-stream';
  258. }
  259. }
  260. if (ob_get_level() && ob_get_length() > 0) {
  261. ob_end_clean();
  262. }
  263. /* Set headers for download */
  264. header('Content-Transfer-Encoding: binary');
  265. header('Content-Type: '.$mimeType);
  266. header('Content-Length: '.filesize($file));
  267. header('Content-Disposition: attachment; filename="'.$filename.'"');
  268. //prevents max execution timeout, when reading large files
  269. @set_time_limit(0);
  270. $fp = fopen($file, 'rb');
  271. if ($fp && is_resource($fp)) {
  272. while (!feof($fp)) {
  273. echo fgets($fp, 16384);
  274. }
  275. }
  276. exit;
  277. }
  278. /**
  279. * Display an error message with js
  280. * and redirect using js function
  281. *
  282. * @param string $msg
  283. */
  284. protected function displayCustomError($msg)
  285. {
  286. $translations = array(
  287. 'Invalid key.' => Tools::displayError('Invalid key.'),
  288. 'This product does not exist in our store.' => Tools::displayError('This product does not exist in our store.'),
  289. 'This product has been deleted.' => Tools::displayError('This product has been deleted.'),
  290. 'This file no longer exists.' => Tools::displayError('This file no longer exists.'),
  291. 'This product has been refunded.' => Tools::displayError('This product has been refunded.'),
  292. 'The product deadline is in the past.' => Tools::displayError('The product deadline is in the past.'),
  293. 'Expiration date exceeded' => Tools::displayError('The product expiration date has passed, preventing you from download this product.'),
  294. 'Expiration date has passed, you cannot download this product' => Tools::displayError('Expiration date has passed, you cannot download this product.'),
  295. 'You have reached the maximum number of allowed downloads.' => Tools::displayError('You have reached the maximum number of downloads allowed.'));
  296. ?>
  297. <script type="text/javascript">
  298. //<![CDATA[
  299. alert("<?php echo isset($translations[$msg]) ? html_entity_decode($translations[$msg], ENT_QUOTES, 'utf-8') : html_entity_decode($msg, ENT_QUOTES, 'utf-8');
  300. ?>");
  301. window.location.href = '<?php echo __PS_BASE_URI__ ?>';
  302. //]]>
  303. </script>
  304. <?php
  305. exit();
  306. }
  307. }