PageRenderTime 102ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/prestashop_1.5.6.2/prestashop/controllers/front/GetFileController.php

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