unzipfiles.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Processor file for UpgradeMODX extra
  4. *
  5. * Copyright 2015-2018 by Bob Ray <https://bobsguides.com>
  6. * Created on 07-16-2018
  7. *
  8. * UpgradeMODX is free software; you can redistribute it and/or modify it under the
  9. * terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * UpgradeMODX is distributed in the hope that it will be useful, but WITHOUT ANY
  14. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  15. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * UpgradeMODX; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. * Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * @package upgrademodx
  22. * @subpackage processors
  23. */
  24. /* @var $modx modX */
  25. include_once 'ugmprocessor.class.php';
  26. class UpgradeMODXUnzipfilesProcessor extends UgmProcessor {
  27. public $languageTopics = array('upgrademodx:default');
  28. public $source;
  29. public $destination;
  30. public $forcePclZip = false;
  31. function initialize() {
  32. /* Initialization here */
  33. parent::initialize();
  34. $this->log($this->modx->lexicon('ugm_unzipping_files'));
  35. $this->forcePclZip = $this->modx->getOption('force_pcl_zip', null ,false, true);
  36. $this->name = 'Unzip Files Processor';
  37. $this->source = $this->tempDir . $this->zipFileName;
  38. $this->destination = $this->tempDir . 'unzipped';
  39. $this->log(" Source: " . $this->source);
  40. $this->log(" Destination: " . $this->destination);
  41. return true;
  42. }
  43. /** Make sure $source and $destination are usable
  44. * @throws Exception
  45. * @var $source string
  46. * @var $destination string
  47. */
  48. public function validate($source, $destination) {
  49. clearstatcache();
  50. if (!file_exists($source)) {
  51. throw new Exception($this->modx->lexicon('ugm_no_downloaded_file') . ': ' . $source);
  52. }
  53. if (!is_dir($destination)) {
  54. @$this->mmkDir($destination);
  55. }
  56. if (!is_dir($destination)) {
  57. throw new Exception($this->modx->lexicon('ugm_could_not_create_directory') . ': ' . $destination);
  58. } else {
  59. if (!is_writable($destination)) {
  60. throw new Exception($this->modx->lexicon('ugm_directory_not_writable') . ': ' . $destination);
  61. }
  62. }
  63. }
  64. /** @throws Exception
  65. */
  66. public function unZip($forcePclZip = false) {
  67. $source = $this->source;
  68. $destination = $this->destination;
  69. $this->log(' Zip File Name ' . $this->zipFileName);
  70. $base = str_replace('.zip', '', $this->zipFileName);
  71. try {
  72. $this->validate($source, $destination);
  73. } catch (Exception$e) {
  74. $this->addError($e->getMessage());
  75. return false;
  76. }
  77. $corePath = $this->modxCorePath;
  78. $status = true;
  79. if ((!$forcePclZip) && class_exists('ZipArchive', false)) {
  80. $zip = new ZipArchive;
  81. if ($zip instanceof ZipArchive) {
  82. $open = $zip->open($source, ZipArchive::CHECKCONS);
  83. $fileCount = (int) ($zip->numFiles);
  84. $fileCount -= 7;
  85. $this->log(' ' . $this->modx->lexicon('ugm_files_to_extract') . ' ' . $fileCount);
  86. $this->log(' ' . $this->modx->lexicon('ugm_destination') . ': ' . $destination);
  87. $this->log(' ' . $this->modx->lexicon('ugm_source') . ': ' . $source);
  88. // throw new Exception('Aborting');
  89. if ($open === true) {
  90. $result = $zip->extractTo($destination);
  91. if ($result === false) {
  92. /* Yes, this is fucking nuts, but it's necessary on some platforms */
  93. $result = $zip->extractTo($destination);
  94. if ($result === false) {
  95. $msg = $zip->getStatusString();
  96. throw new Exception($msg);
  97. }
  98. }
  99. $zip->close();
  100. } else {
  101. $status = 'Could not open ZipArchive ' . $source . ' ' . $zip->getStatusString();
  102. }
  103. } else {
  104. $status = 'Could not instantiate ZipArchive';
  105. }
  106. } else {
  107. $zipClass = $corePath . 'xpdo/compression/pclzip.lib.php';
  108. if (file_exists($zipClass)) {
  109. include $corePath . 'xpdo/compression/pclzip.lib.php';
  110. $archive = new PclZip($source);
  111. if ($archive->extract(PCLZIP_OPT_PATH, $destination) == 0) {
  112. $status = 'Extraction with PclZip failed - Error : ' . $archive->errorInfo(true);
  113. }
  114. } else {
  115. $status = 'Neither ZipArchive, nor PclZip were available to unzip the archive';
  116. }
  117. }
  118. if ($status === true) {
  119. $msg = $this->modx->lexicon('ugm_unzipped') .
  120. ' ' . 'modx.zip' . ' -> ' . $destination;
  121. $this->log($msg);
  122. }
  123. return $status;
  124. }
  125. public function process() {
  126. try {
  127. $this->unZip($this->forcePclZip);
  128. } catch (Exception $e) {
  129. $this->addError($e->getMessage());
  130. }
  131. return $this->prepareResponse($this->modx->lexicon('ugm_copying_files'));
  132. }
  133. }
  134. return 'UpgradeMODXUnzipfilesProcessor';