PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/modx/modmanagerrequest.class.php

http://github.com/modxcms/revolution
PHP | 189 lines | 87 code | 21 blank | 81 comment | 8 complexity | ae87bd8638c153ba0434f5df2be59365 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. require_once MODX_CORE_PATH . 'model/modx/modrequest.class.php';
  11. /**
  12. * Encapsulates the interaction of MODX manager with an HTTP request.
  13. *
  14. * {@inheritdoc}
  15. *
  16. * @package modx
  17. */
  18. class modManagerRequest extends modRequest {
  19. /**
  20. * @var string The action to load.
  21. * @access public
  22. */
  23. public $action= null;
  24. /**
  25. * @deprecated 2.0.0 Use $modx->error instead.
  26. * @var modError The error handler for the request.
  27. * @access public
  28. */
  29. public $error= null;
  30. /**
  31. * @var string The REQUEST parameter to load actions by.
  32. * @access public
  33. */
  34. public $actionVar = 'a';
  35. /**
  36. * @var mixed The default action to load from.
  37. * @access public
  38. */
  39. public $defaultAction = 'welcome';
  40. public $namespace = 'core';
  41. public $namespaceVar = 'namespace';
  42. /**
  43. * Instantiates a modManagerRequest object.
  44. *
  45. * @param modX $modx
  46. * @return modManagerRequest
  47. */
  48. function __construct(modX & $modx) {
  49. parent :: __construct($modx);
  50. $this->initialize();
  51. }
  52. /**
  53. * Initializes the manager request.
  54. *
  55. * @access public
  56. * @return boolean True if successful.
  57. */
  58. public function initialize() {
  59. $this->sanitizeRequest();
  60. if (!defined('MODX_INCLUDES_PATH')) {
  61. define('MODX_INCLUDES_PATH',$this->modx->getOption('manager_path').'includes/');
  62. }
  63. /* load smarty template engine */
  64. $theme = $this->modx->getOption('manager_theme',null,'default');
  65. $templatePath = $this->modx->getOption('manager_path') . 'templates/' . $theme . '/';
  66. if (!file_exists($templatePath)) { /* fallback to default */
  67. $templatePath = $this->modx->getOption('manager_path') . 'templates/default/';
  68. }
  69. $this->modx->getService('smarty', 'smarty.modSmarty', '', array(
  70. 'template_dir' => $templatePath,
  71. ));
  72. /* load context-specific cache dir */
  73. $this->modx->smarty->setCachePath($this->modx->context->get('key').'/smarty/'.$theme.'/');
  74. $this->modx->smarty->assign('_config',$this->modx->config);
  75. $this->modx->smarty->assignByRef('modx',$this->modx);
  76. if (!array_key_exists('a', $_REQUEST)) {
  77. $_REQUEST[$this->actionVar] = $this->modx->getOption('welcome_action', null, $this->defaultAction);
  78. $_REQUEST[$this->namespaceVar] = $this->modx->getOption('welcome_namespace', null, 'core');
  79. }
  80. /* send the charset header */
  81. header('Content-Type: text/html; charset='.$this->modx->getOption('modx_charset'));
  82. /*
  83. * TODO: implement destroy active sessions if installing
  84. * TODO: implement regen session if not destroyed from install
  85. */
  86. /* include version info */
  87. if ($this->modx->version === null) $this->modx->getVersionData();
  88. if ($this->modx->getOption('manager_language')) {
  89. $this->modx->setOption('cultureKey',$this->modx->getOption('manager_language'));
  90. }
  91. /* load default core cache file of lexicon strings */
  92. $this->modx->lexicon->load('core:default');
  93. return true;
  94. }
  95. /**
  96. * The primary MODX manager request handler (a.k.a. controller).
  97. *
  98. * @access public
  99. * @return boolean True if a request is handled without interruption.
  100. */
  101. public function handleRequest() {
  102. /* Load error handling class */
  103. $this->loadErrorHandler();
  104. $this->modx->invokeEvent('OnHandleRequest');
  105. /* save page to manager object. allow custom actionVar choice for extending classes. */
  106. $this->action = !empty($_REQUEST[$this->actionVar]) ? trim($_REQUEST[$this->actionVar]) : $this->defaultAction;
  107. $this->action = preg_replace("/[^A-Za-z0-9_\-\/]/",'',$this->action);
  108. $this->action = trim(trim(str_replace('//','',$this->action),'/'));
  109. $this->namespace = !empty($_REQUEST[$this->namespaceVar]) ? trim($_REQUEST[$this->namespaceVar]) : 'core';
  110. $this->namespace = preg_replace("/[^A-Za-z0-9_\-\/]/",'',$this->namespace);
  111. $this->namespace = trim(trim(str_replace('//','',$this->namespace),'/'));
  112. /* invoke OnManagerPageInit event */
  113. $this->modx->invokeEvent('OnManagerPageInit', array(
  114. 'action' => $this->action,
  115. 'namespace' => $this->namespace,
  116. ));
  117. $this->prepareResponse();
  118. }
  119. /**
  120. * This implementation adds register logging capabilities via $_POST vars
  121. * when the error handler is loaded.
  122. *
  123. * @param string $class
  124. */
  125. public function loadErrorHandler($class = 'modError') {
  126. parent :: loadErrorHandler($class);
  127. $data = array_merge($_POST, array(
  128. 'register_class' => 'registry.modFileRegister'
  129. ));
  130. $this->registerLogging($data);
  131. }
  132. /**
  133. * Loads the actionMap, and generates a cache file if necessary.
  134. *
  135. * @access public
  136. * @return boolean True if successful.
  137. */
  138. public function loadActionMap() {
  139. $loaded = false;
  140. $cacheKey= $this->modx->context->get('key') . '/actions';
  141. $map = $this->modx->cacheManager->get($cacheKey, array(
  142. xPDO::OPT_CACHE_KEY => $this->modx->getOption('cache_action_map_key', null, 'action_map'),
  143. xPDO::OPT_CACHE_HANDLER => $this->modx->getOption('cache_action_map_handler', null, $this->modx->getOption(xPDO::OPT_CACHE_HANDLER)),
  144. xPDO::OPT_CACHE_FORMAT => (integer) $this->modx->getOption('cache_action_map_format', null, $this->modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)),
  145. ));
  146. if (!$map) {
  147. $map = $this->modx->cacheManager->generateActionMap($cacheKey);
  148. }
  149. if ($map) {
  150. $this->modx->actionMap = $map;
  151. $loaded = true;
  152. }
  153. return $loaded;
  154. }
  155. /**
  156. * Prepares the MODX response to a mgr request that is being handled.
  157. *
  158. * @access public
  159. * @param array $options An array of options
  160. * @return boolean True if the response is properly prepared.
  161. */
  162. public function prepareResponse(array $options = array()) {
  163. if (!$this->modx->getResponse('modManagerResponse')) {
  164. $this->modx->log(modX::LOG_LEVEL_FATAL, 'Could not load response class.');
  165. }
  166. $this->modx->response->outputContent($options);
  167. }
  168. }