PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/includes/runner/modinstallrunner.class.php

http://github.com/modxcms/revolution
PHP | 221 lines | 155 code | 23 blank | 43 comment | 34 complexity | 8ab6576ab9c59e8eb048b68298461d79 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. abstract class modInstallRunner {
  11. const RESULT_FAILURE = 'failed';
  12. const RESULT_ERROR = 'error';
  13. const RESULT_WARNING = 'warning';
  14. const RESULT_SUCCESS = 'success';
  15. /** @var modInstall $install */
  16. public $install;
  17. /** @var xPDO $xpdo */
  18. public $xpdo;
  19. /** @var array $config */
  20. public $config = array();
  21. public $success = false;
  22. /** @var modInstallVersion $versioner */
  23. public $versioner;
  24. /** @var array $results */
  25. public $results = array();
  26. function __construct(modInstall $install,array $config = array()) {
  27. $this->install =& $install;
  28. $this->xpdo =& $install->xpdo;
  29. $this->config = array_merge(array(
  30. ),$config);
  31. }
  32. public function run($mode) {
  33. $this->initialize();
  34. $this->execute($mode);
  35. if ($this->success) {
  36. $this->cleanup();
  37. }
  38. return $this->success;
  39. }
  40. public function addResult($type,$message) {
  41. $this->results[] = array(
  42. 'class' => $type,
  43. 'msg' => $message,
  44. );
  45. }
  46. public function getResults() {
  47. return $this->results;
  48. }
  49. /**
  50. * Load version-specific installer.
  51. *
  52. * @access public
  53. * @param string $class The class to load.
  54. * @param string $path
  55. * @return modInstallVersion
  56. */
  57. public function loadVersionInstaller($class = 'modInstallVersion',$path = '') {
  58. $className = $this->install->loadClass($class,$path);
  59. if (!empty($className)) {
  60. $this->versioner = new $className($this);
  61. return $this->versioner;
  62. } else {
  63. $this->install->_fatalError($this->install->lexicon('versioner_err_nf',array('path' => $path)));
  64. }
  65. return $this->versioner;
  66. }
  67. /**
  68. * Update the workspace path
  69. * @return boolean
  70. */
  71. public function updateWorkspace() {
  72. $updated = false;
  73. /* @var modWorkspace $workspace set default workspace path */
  74. $workspace = $this->install->xpdo->getObject('modWorkspace', array (
  75. 'active' => 1
  76. ));
  77. if ($workspace) {
  78. $path = $workspace->get('path');
  79. if (!empty($path)) {
  80. $path = trim($path);
  81. }
  82. if (empty ($path) || !file_exists($path)) {
  83. $workspace->set('path', '{core_path}');
  84. if (!$workspace->save()) {
  85. $this->addResult(modInstallRunner::RESULT_ERROR,'<p class="notok">'.$this->install->lexicon('workspace_err_path').'</p>');
  86. } else {
  87. $updated = true;
  88. $this->addResult(modInstallRunner::RESULT_SUCCESS,'<p class="ok">'.$this->install->lexicon('workspace_path_updated').'</p>');
  89. }
  90. }
  91. } else {
  92. $this->addResult(modInstallRunner::RESULT_ERROR,'<p class="notok">'.$this->install->lexicon('workspace_err_nf').'</p>');
  93. }
  94. return $updated;
  95. }
  96. /**
  97. * @return bool
  98. */
  99. public function installPackage() {
  100. /* add required core data */
  101. $this->install->xpdo->loadClass('transport.xPDOTransport', XPDO_CORE_PATH, true, true);
  102. $packageDirectory = MODX_CORE_PATH . 'packages/';
  103. $packageState = $this->install->settings->get('unpacked') == 1 ? xPDOTransport::STATE_UNPACKED : xPDOTransport::STATE_PACKED;
  104. $package = xPDOTransport :: retrieve($this->install->xpdo, $packageDirectory . 'core.transport.zip', $packageDirectory, $packageState);
  105. if (!is_object($package) || !($package instanceof xPDOTransport)) {
  106. $this->addResult(modInstallRunner::RESULT_FAILURE,'<p class="notok">'.$this->install->lexicon('package_execute_err_retrieve',array('path' => $this->install->settings->get('core_path'))).'</p>');
  107. return false;
  108. }
  109. if (!defined('MODX_BASE_PATH'))
  110. define('MODX_BASE_PATH', $this->install->settings->get('context_web_path'));
  111. if (!defined('MODX_ASSETS_PATH')) {
  112. $assetsDefault = $this->install->settings->get('context_assets_path',$this->install->settings->get('context_web_path').'assets/');
  113. define('MODX_ASSETS_PATH',$assetsDefault);
  114. }
  115. if (!defined('MODX_MANAGER_PATH'))
  116. define('MODX_MANAGER_PATH', $this->install->settings->get('context_mgr_path'));
  117. if (!defined('MODX_CONNECTORS_PATH'))
  118. define('MODX_CONNECTORS_PATH', $this->install->settings->get('context_connectors_path'));
  119. if (!defined('MODX_BASE_URL'))
  120. define('MODX_BASE_URL', $this->install->settings->get('context_web_url'));
  121. if (!defined('MODX_ASSETS_URL')) {
  122. $assetsDefault = $this->install->settings->get('context_assets_url',$this->install->settings->get('context_web_url').'assets/');
  123. define('MODX_ASSETS_URL',$assetsDefault);
  124. }
  125. if (!defined('MODX_MANAGER_URL'))
  126. define('MODX_MANAGER_URL', $this->install->settings->get('context_mgr_url'));
  127. if (!defined('MODX_CONNECTORS_URL'))
  128. define('MODX_CONNECTORS_URL', $this->install->settings->get('context_connectors_url'));
  129. return $package->install(array (
  130. xPDOTransport::RESOLVE_FILES => ($this->install->settings->get('inplace') == 0 ? 1 : 0)
  131. ,xPDOTransport::INSTALL_FILES => ($this->install->settings->get('inplace') == 0 ? 1 : 0)
  132. , xPDOTransport::PREEXISTING_MODE => xPDOTransport::REMOVE_PREEXISTING
  133. ));
  134. }
  135. /**
  136. * Writes the config file.
  137. *
  138. * @return boolean Returns true if successful; false otherwise.
  139. */
  140. public function writeConfig() {
  141. $written = false;
  142. $configTpl = MODX_CORE_PATH . 'docs/config.inc.tpl';
  143. $configFile = MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
  144. /**
  145. * Sanitize MySQL Password before writing to config, escaping '
  146. * I'm sure there's a better way to do this, but this works for now.
  147. * Otherwise, we risk fatal PHP errors if the entered Password
  148. * contains any single quotes as they would escape the string.
  149. * See GitHub issue 12502 for more information. https://github.com/modxcms/revolution/issues/12502
  150. */
  151. $this->install->settings->settings['database_password'] = addslashes($this->install->settings->settings['database_password']);
  152. $settings = $this->install->settings->fetch();
  153. $settings['last_install_time'] = time();
  154. $settings['site_id'] = uniqid('modx',true);
  155. /* make UUID if not set */
  156. if (empty($settings['uuid'])) {
  157. $settings['uuid'] = $this->install->generateUUID();
  158. }
  159. if (file_exists($configTpl)) {
  160. if ($tplHandle = @ fopen($configTpl, 'rb')) {
  161. $content = @ fread($tplHandle, filesize($configTpl));
  162. @ fclose($tplHandle);
  163. if ($content) {
  164. $replace = array ();
  165. foreach ($settings as $key => $value) {
  166. if (is_scalar($value)) {
  167. $replace['{' . $key . '}'] = "{$value}";
  168. } elseif (is_array($value)) {
  169. $replace['{' . $key . '}'] = var_export($value, true);
  170. }
  171. }
  172. $content = str_replace(array_keys($replace), array_values($replace), $content);
  173. if ($configHandle = @ fopen($configFile, 'wb')) {
  174. $written = @ fwrite($configHandle, $content);
  175. @ fclose($configHandle);
  176. }
  177. }
  178. }
  179. }
  180. $perms = $this->install->settings->get('new_file_permissions', sprintf("%04o", 0666 & (0777 - umask())));
  181. if (is_string($perms)) $perms = octdec($perms);
  182. $chmodSuccess = @ chmod($configFile, $perms);
  183. if ($written) {
  184. $this->addResult(modInstallRunner::RESULT_SUCCESS,'<p class="ok">'.$this->install->lexicon('config_file_written').'</p>');
  185. } else {
  186. $this->addResult(modInstallRunner::RESULT_FAILURE,'<p class="notok">'.$this->install->lexicon('config_file_err_w').'</p>');
  187. }
  188. if ($chmodSuccess) {
  189. $this->addResult(modInstallRunner::RESULT_SUCCESS,'<p class="ok">'.$this->install->lexicon('config_file_perms_set').'</p>');
  190. } else {
  191. $this->addResult(modInstallRunner::RESULT_WARNING,'<p>'.$this->install->lexicon('config_file_perms_notset').'</p>');
  192. }
  193. return $written;
  194. }
  195. abstract public function execute($mode);
  196. abstract public function initialize();
  197. abstract public function cleanup();
  198. }