downloadfiles.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. use GuzzleHttp\Exception\GuzzleException;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\RequestOptions;
  5. /**
  6. * Processor file for UpgradeMODX extra
  7. *
  8. * Copyright 2015-2018 by Bob Ray <https://bobsguides.com>
  9. * Created on 07-16-2018
  10. *
  11. * UpgradeMODX is free software; you can redistribute it and/or modify it under the
  12. * terms of the GNU General Public License as published by the Free Software
  13. * Foundation; either version 2 of the License, or (at your option) any later
  14. * version.
  15. *
  16. * UpgradeMODX is distributed in the hope that it will be useful, but WITHOUT ANY
  17. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  18. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * UpgradeMODX; if not, write to the Free Software Foundation, Inc., 59 Temple
  22. * Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @package upgrademodx
  25. * @subpackage processors
  26. */
  27. /* @var $modx modX */
  28. include_once 'ugmprocessor.class.php';
  29. class UpgradeMODXDownloadfilesProcessor extends UgmProcessor {
  30. // public $languageTopics = array('upgrademodx:default');
  31. /** @var $client GuzzleHttp\Client */
  32. public $client = null;
  33. public $sourceUrl = '';
  34. public $destinationPath = '';
  35. public $modxTimeout = 5;
  36. function initialize() {
  37. /** @var $scriptProperties array */
  38. parent::initialize();
  39. $this->name = 'Download Files Processor';
  40. $this->modxTimeout = $this->modx->getOption('ugm_modx_timeout', null, 6, true);
  41. /* Write directly because we want to truncate the file */
  42. $fp = fopen($this->logFilePath, 'w');
  43. if ($fp) {
  44. fwrite($fp, 'UpgradeMODX Log -- ' . strftime('%A %B %C, %G %I:%M %p'));
  45. fclose($fp);
  46. } else {
  47. $this->addError($this->modx->lexicon('ugm_could_not_open') . ' ' . $this->logFilePath);
  48. }
  49. $this->log($this->modx->lexicon('ugm_downloading_files'));
  50. $corePath = $this->ugmCorePath;
  51. require_once $corePath . 'vendor/autoload.php';
  52. $version = $this->zipFileName;
  53. $this->log("Version: " . $version);
  54. $v = explode('-', $version);
  55. $shortVersion = $v[1];
  56. // Example: https://modx.s3.amazonaws.com/releases/2.6.5/modx-2.6.5-pl.zip
  57. $this->sourceUrl = 'https://modx.s3.amazonaws.com/releases/' . $shortVersion . '/' . $version;
  58. $this->log("URL: " . $this->sourceUrl);
  59. // return;
  60. $this->destinationPath = $this->tempDir . $this->zipFileName;
  61. $this->client = new Client();
  62. return true;
  63. }
  64. function remoteFilexists() {
  65. /** @var $client GuzzleHttp\Client */
  66. $client = $this->client;
  67. try {
  68. $client->head($this->sourceUrl);
  69. return true;
  70. } catch (GuzzleHttp\Exception\ClientException $e) {
  71. return false;
  72. }
  73. }
  74. /** @throws Exception
  75. * @throws GuzzleException
  76. */
  77. public function download() {
  78. /* See if the file is available for download */
  79. if (!$this->remoteFilexists()) {
  80. throw new Exception($this->modx->lexicon('ugm_no_such_version'));
  81. }
  82. $client = $this->client;
  83. $destFile = fopen($this->destinationPath, 'w');
  84. if (!$destFile) {
  85. $msg = '[Download Files Processor] ' .
  86. $this->modx->lexicon('ugm_could_not_open') . ' ' .
  87. $this->destinationPath . ' ' . $this->modx->lexicon('ugm_for_writing');
  88. throw new Exception($msg);
  89. }
  90. set_time_limit(0);
  91. $options = array(
  92. RequestOptions::SINK => $destFile, // the body of a response
  93. RequestOptions::CONNECT_TIMEOUT => $this->modxTimeout, // request
  94. RequestOptions::VERIFY => (bool) $this->sslVerifyPeer,
  95. // RequestOptions::TIMEOUT => 0.0, // response
  96. );
  97. if (!empty($this->certPath)) {
  98. $options[RequestOptions::CERT] = $this->certPath;
  99. }
  100. // $options = array();
  101. $options['headers'] = array(
  102. 'Cache-Control' => 'no-cache',
  103. 'Accept' => 'application/zip'
  104. );
  105. try {
  106. $response = $client->request('GET', $this->sourceUrl, $options);
  107. } catch (Exception $e) {
  108. fclose($destFile);
  109. unlink($this->destinationPath);
  110. throw new exception($this->modx->lexicon('ugm_download_failed') . ' -- ' . $e->getMessage());
  111. }
  112. fclose($destFile);
  113. if (filesize($this->destinationPath) === 0) {
  114. throw new exception($this->modx->lexicon('ugm_download_failed')
  115. . ' Filesize: 0');
  116. } else {
  117. $msg = $this->modx->lexicon('ugm_downloaded') . ' ' . $_SESSION['ugm_version'] .
  118. ' -> ' . $this->destinationPath;
  119. $this->log($msg);
  120. }
  121. }
  122. /**
  123. * @return array|mixed|string
  124. * @throws GuzzleException
  125. */
  126. public function process() {
  127. if ((!$this->devMode) || (!file_exists($this->tempDir . $this->zipFileName))) {
  128. try {
  129. $this->download();
  130. } catch (Exception $e) {
  131. $this->addError($e->getMessage());
  132. }
  133. }
  134. /* message for next processor */
  135. return $this->prepareResponse($this->modx->lexicon('ugm_unzipping_files'));
  136. }
  137. }
  138. return 'UpgradeMODXDownloadfilesProcessor';