download.class.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Download
  4. *
  5. * @package backupmodx
  6. * @subpackage processor
  7. */
  8. class BackupMODXDownloadProcessor extends modProcessor
  9. {
  10. /** @var BackupMODX $backupmodx */
  11. public $backupmodx;
  12. /**
  13. * BackupMODXGetProcessor constructor.
  14. * @param modX $modx A reference to the modX instance
  15. * @param array $properties An array of properties
  16. */
  17. public function __construct(modX $modx, array $properties)
  18. {
  19. parent::__construct($modx, $properties);
  20. $corePath = $modx->getOption('backupmodx.core_path', null, $modx->getOption('core_path') . 'components/backupmodx/');
  21. $this->backupmodx = $modx->getService('backupmodx', 'BackupMODX', $corePath . 'model/backupmodx/', array(
  22. 'core_path' => $corePath
  23. ));
  24. }
  25. public function process()
  26. {
  27. $file = $this->getProperty('file', '');
  28. $folder = $this->getProperty('folder', '');
  29. $download = $this->backupmodx->getFile($folder, $file);
  30. if (file_exists($download)) {
  31. switch (pathinfo($file, PATHINFO_EXTENSION)) {
  32. case 'sql':
  33. header('Content-Type: application/sql', false);
  34. break;
  35. case 'txt':
  36. header('Content-Type: text/plain', false);
  37. break;
  38. case 'zip':
  39. header('Content-Type: application/zip', false);
  40. break;
  41. default:
  42. die();
  43. }
  44. header('Content-Disposition: attachment; filename="' . $file . '"');
  45. header('Content-Length: ' . @filesize($download));
  46. $chunksize = 1*(1024*1024); // how many bytes per chunk
  47. $handle = fopen($download, 'rb');
  48. if ($handle === false) {
  49. return false;
  50. }
  51. while (!feof($handle)) {
  52. $buffer = fread($handle, $chunksize);
  53. print $buffer;
  54. }
  55. fclose($handle);
  56. die();
  57. }
  58. }
  59. }
  60. return 'BackupMODXDownloadProcessor';