download.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. * This file is part of the Fred package.
  4. *
  5. * Copyright (c) MODX, LLC
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once dirname(dirname(dirname(__FILE__))) . '/index.class.php';
  11. /**
  12. * @package fred
  13. * @subpackage controllers
  14. */
  15. class FredThemeDownloadManagerController extends FredBaseManagerController
  16. {
  17. public function process(array $scriptProperties = [])
  18. {
  19. $themeId = (int)$_GET['theme'];
  20. if(empty($themeId)){
  21. die($this->modx->lexicon('fred.err.build_ns_theme'));
  22. }
  23. $theme = $this->modx->getObject('FredTheme', ['id' => $themeId]);
  24. if (!$theme){
  25. die($this->modx->lexicon('fred.err.build_ns_theme'));
  26. }
  27. $config = $theme->get('config');
  28. if (empty($config) || !is_array($config)) {
  29. die($this->modx->lexicon('fred.err.theme_no_build'));
  30. }
  31. if (empty($config['name']) || empty($config['version']) || empty($config['release'])) {
  32. die($this->modx->lexicon('fred.err.theme_no_build'));
  33. }
  34. $corePath = $this->modx->getOption('core_path');
  35. $packagesDir = $corePath . 'packages/';
  36. $fileName = "{$config['name']}-{$config['version']}-{$config['release']}.transport.zip";
  37. $absolutePath = $packagesDir . $fileName;
  38. if (!file_exists($absolutePath)) {
  39. die($this->modx->lexicon('fred.err.theme_no_built_file'));
  40. }
  41. $fileContent = @file_get_contents($absolutePath);
  42. if($fileContent !== FALSE) {
  43. session_write_close();
  44. ob_clean();
  45. header('Pragma: public'); // required
  46. header('Expires: 0'); // no cache
  47. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  48. header('Cache-Control: private', false);
  49. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($absolutePath)) . ' GMT');
  50. header('Content-Description: File Transfer');
  51. header('Content-Type:'); //added to fix ZIP file corruption
  52. header('Content-Type: "application/force-download"');
  53. header('Content-Disposition: attachment; filename="' . $fileName . '"');
  54. header('Content-Transfer-Encoding: binary');
  55. header('Content-Length: ' . (string) (filesize($absolutePath))); // provide file size
  56. header('Connection: close');
  57. echo $fileContent;
  58. die();
  59. } else {
  60. die($this->modx->lexicon('fred.err.theme_read_file'));
  61. }
  62. }
  63. public function getPageTitle()
  64. {
  65. return $this->modx->lexicon('fred.themes.download');
  66. }
  67. }