preparesetup.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 UpgradeMODXPreparesetupProcessor extends UgmProcessor {
  27. public $languageTopics = array('upgrademodx:default');
  28. function initialize() {
  29. /* Initialization here */
  30. parent::initialize();
  31. $this->name = 'Prepare Setup Processor';
  32. $this->log($this->modx->lexicon('ugm_preparing_setup'));
  33. return true;
  34. }
  35. /**
  36. * Copy root config.core.php to setup/includes
  37. * after adding setup key
  38. * @throws Exception
  39. */
  40. public function prepareSetup() {
  41. sleep(2);
  42. $rootCoreConfig = $this->basePath. 'config.core.php';
  43. $success = true;
  44. if (file_exists($rootCoreConfig)) {
  45. $newStr = "define('MODX_SETUP_KEY', '@traditional@');\n?>";
  46. $content = file_get_contents($rootCoreConfig);
  47. if (strpos($content, 'MODX_SETUP_KEY') === false) {
  48. if (strpos($content, '?>') !== false) {
  49. $content = str_replace('?>', $newStr, $content);
  50. } else {
  51. $content .= "\n" . $newStr;
  52. }
  53. $setup = 'setup/includes/config.core.php';
  54. $target = $this->devMode ? $this->tempDir . 'test/' . $setup : $this->basePath . $setup;
  55. if (!file_put_contents($target, $content)) {
  56. throw new Exception($this->modx->lexicon('ugm_could_not_write') . ' ' .
  57. $this->modx->lexicon('ugm_to') .
  58. ' ' . $target);
  59. }
  60. }
  61. /* Log out all users before launching the setup - code left in case it's necessary */
  62. /*if (false) { //(if (! $devModx)) {
  63. $sessionTable = $this->modx->getTableName('modSession');
  64. if ($this->modx->query("TRUNCATE TABLE {$sessionTable}") == false) {
  65. $flushed = false;
  66. } else {
  67. // $modx->user->endSession();
  68. }
  69. }*/
  70. } else {
  71. throw new Exception($this->modx->lexicon('ugm_no_root_config_core'));
  72. }
  73. }
  74. /**
  75. * @return array|mixed|string
  76. * @throws \GuzzleHttp\Exception\GuzzleException
  77. */
  78. public function process() {
  79. try {
  80. $this->prepareSetup();
  81. } catch (Exception $e) {
  82. $this->addError($e->getMessage());
  83. }
  84. if (!$this->hasErrors()) {
  85. $this->log($this->modx->lexicon('ugm_setup_prepared'));
  86. }
  87. return $this->prepareResponse($this->modx->lexicon('ugm_deleting_temp_files'));
  88. }
  89. }
  90. return 'UpgradeMODXPreparesetupProcessor';