PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/core/model/modx/modmanagercontrollerdeprecated.class.php

http://github.com/modxcms/revolution
PHP | 276 lines | 143 code | 27 blank | 106 comment | 35 complexity | 8421c9ee900d99c3d350b1cbc88430b3 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. /**
  11. * Handles backwards compatibility with pre-Revo 2.2 controllers
  12. *
  13. * @package modx
  14. */
  15. class modManagerControllerDeprecated extends modManagerController {
  16. public $body = '';
  17. /**
  18. * Overrides modManagerController::process to provide custom backwards-compatible support for controllers that
  19. * were made prior to Revolution 2.2.x.
  20. *
  21. * @param array $scriptProperties
  22. * @see modManagerController::process()
  23. * @return string
  24. */
  25. public function process(array $scriptProperties = array()) {
  26. $this->modx->deprecated('2.2.0', '', 'modManagerControllerDeprecated support');
  27. $modx =& $this->modx;
  28. $theme = $this->modx->getOption('manager_theme',null,'default');
  29. /* get template path */
  30. $templatePath = $this->modx->getOption('manager_path') . 'templates/'.$theme.'/';
  31. if (!file_exists($templatePath)) {
  32. $templatePath = $this->modx->getOption('manager_path') . 'templates/default/';
  33. $this->modx->config['manager_theme'] = 'default';
  34. $this->setPlaceholder('_config',$this->modx->config);
  35. }
  36. /* assign custom action topics to smarty, so can load custom topics for each page */
  37. $this->modx->lexicon->load('action');
  38. $topics = explode(',',$this->config['lang_topics']);
  39. foreach ($topics as $topic) { $this->modx->lexicon->load($topic); }
  40. $this->setPlaceholder('_lang_topics',$this->config['lang_topics']);
  41. $this->setPlaceholder('_lang',$this->modx->lexicon->fetch());
  42. $this->setPlaceholder('_ctx',$this->modx->context->get('key'));
  43. $this->registerBaseScripts(!empty($this->config['haslayout']) ? true : false);
  44. $this->body = '';
  45. $f = $this->prepareNamespacePath($this->config['controller'],$theme);
  46. $f = $this->getControllerFilename($f);
  47. if (file_exists($f)) {
  48. $this->modx->invokeEvent('OnBeforeManagerPageInit',array(
  49. 'action' => $this->config,
  50. 'filename' => $f,
  51. ));
  52. if (!empty($this->config['namespace']) && $this->config['namespace'] != 'core') {
  53. $this->modx->smarty->setTemplatePath($this->config['namespace_path']);
  54. }
  55. $cbody = include $f;
  56. } else {
  57. if (!empty($this->config['namespace_path'])) {
  58. $f = str_replace($this->config['namespace_path'], '', $f);
  59. }
  60. $cbody = 'Could not find action file at: '.$f;
  61. }
  62. if (!empty($this->ruleOutput)) {
  63. $this->addHtml($this->ruleOutput);
  64. }
  65. $this->registerCssJs();
  66. /* assign later to allow for css/js registering */
  67. if (is_array($cbody)) {
  68. $this->setPlaceholder('_e', $cbody);
  69. $cbody = $this->fetchTemplate('error.tpl');
  70. }
  71. $this->body .= $cbody;
  72. return $this->body;
  73. }
  74. /**
  75. * Check whether the active user has access to view this page
  76. * @return bool True if the user passes permission checks
  77. */
  78. public function checkPermissions() { return true; }
  79. /**
  80. * The page title for this controller
  81. * @return string The string title of the page
  82. */
  83. public function getPageTitle() { return ''; }
  84. /**
  85. * Loads any page-specific CSS/JS for the controller
  86. * @return void
  87. */
  88. public function loadCustomCssJs() { return; }
  89. /**
  90. * Load the appropriate language topics for this page
  91. *
  92. * @return array
  93. */
  94. public function getLanguageTopics() { return array(); }
  95. /**
  96. * Specify the location of the template file
  97. * @return string The absolute path to the template file
  98. */
  99. public function getTemplateFile() { return ''; }
  100. /**
  101. * Prepares the Namespace Path for usage
  102. *
  103. * @access protected
  104. * @param string $controller
  105. * @param string $theme
  106. * @return string The formatted Namespace path
  107. */
  108. protected function prepareNamespacePath($controller,$theme = 'default') {
  109. /* set context url and path */
  110. $this->modx->config['namespace_path'] = $controller;
  111. /* find context path */
  112. if (isset($this->config['namespace']) && $this->config['namespace'] != 'core') {
  113. /* if a custom 3rd party path */
  114. $f = $this->config['namespace_path'].$controller;
  115. } else {
  116. $f = $this->config['namespace_path'].'controllers/'.$theme.'/'.$controller;
  117. /* if custom theme doesnt have controller, go to default theme */
  118. if (!file_exists($f.'.php')) {
  119. $f = $this->config['namespace_path'].'controllers/default/'.$controller;
  120. }
  121. }
  122. return $f;
  123. }
  124. /**
  125. * Gets the parsed controller filename and checks for its existence.
  126. *
  127. * @access protected
  128. * @param string $f The filename to parse.
  129. * @return mixed The parsed filename, or boolean false if invalid.
  130. */
  131. protected function getControllerFilename($f = '') {
  132. if (empty($f)) return false;
  133. /* if action is a directory, load base index.php */
  134. if (substr($f,strlen($f)-1,1) == '/') { $f .= 'index'; }
  135. /* append .php */
  136. if (file_exists($f.'.php')) {
  137. $f = $f.'.php';
  138. /* for actions that don't have trailing / but reference index */
  139. } elseif (file_exists($f.'/index.php')) {
  140. $f = $f.'/index.php';
  141. } else {
  142. $this->modx->log(modX::LOG_LEVEL_ERROR,'Could not find action file at: '.$f);
  143. $f = $f.'.php';
  144. }
  145. return $f;
  146. }
  147. /**
  148. * Grabs a stripped version of modx to prevent caching of JS after upgrades
  149. *
  150. * @access private
  151. * @return string The parsed version string
  152. */
  153. private function _prepareVersionPostfix() {
  154. $version = $this->modx->getVersionData();
  155. return str_replace(array('.','-'),'',$version['full_version']);
  156. }
  157. /**
  158. * Appends a version postfix to a script tag
  159. *
  160. * @access private
  161. * @param string $str The script tag to append the version to
  162. * @param string $version The version to append
  163. * @return string The adjusted script tag
  164. */
  165. private function _postfixVersionToScript($str,$version) {
  166. $pos = strpos($str,'.js');
  167. $pos2 = strpos($str,'src="'); /* only apply to externals */
  168. if ($pos && $pos2) {
  169. $s = substr($str,0,strpos($str,'"></script>'));
  170. if (!empty($s) && substr($s,strlen($s)-3,strlen($s)) == '.js') {
  171. $str = $s.'?v='.$version.'"></script>';
  172. }
  173. }
  174. return $str;
  175. }
  176. /**
  177. * Registers CSS/JS to manager interface
  178. */
  179. public function registerCssJs() {
  180. $versionPostFix = $this->_prepareVersionPostfix();
  181. /* if true, use compressed JS */
  182. if ($this->modx->getOption('compress_js',null,false)) {
  183. foreach ($this->modx->sjscripts as &$scr) {
  184. $pos = strpos($scr,'.js');
  185. if ($pos) {
  186. $newUrl = substr($scr,0,$pos).'-min'.substr($scr,$pos,strlen($scr));
  187. } else { continue; }
  188. $pos = strpos($newUrl,'modext/');
  189. if ($pos) {
  190. $pos = $pos+7;
  191. $newUrl = substr($newUrl,0,$pos).'build/'.substr($newUrl,$pos,strlen($newUrl));
  192. }
  193. $path = str_replace(array(
  194. $this->modx->getOption('manager_url').'assets/modext/',
  195. '<script type="text/javascript" src="',
  196. '"></script>',
  197. ),'',$newUrl);
  198. if (file_exists($this->modx->getOption('manager_path').'assets/modext/'.$path)) {
  199. $scr = $newUrl;
  200. }
  201. /* append version string */
  202. $scr = $this->_postfixVersionToScript($scr,$versionPostFix);
  203. }
  204. } else {
  205. foreach ($this->modx->sjscripts as &$scr) {
  206. $scr = $this->_postfixVersionToScript($scr,$versionPostFix);
  207. }
  208. }
  209. /* assign css/js to header */
  210. $this->setPlaceholder('cssjs',$this->modx->sjscripts);
  211. if (!empty($this->head['js']) || !empty($this->head['lastjs']) || !empty($this->head['css']) ||
  212. !empty($this->head['html'])
  213. ) {
  214. // Some plugin probably registered additional assets (ie. OnBeforeManagerPageInit), let's load them
  215. parent::registerCssJs();
  216. }
  217. }
  218. /**
  219. * Adds a lexicon topic to this page's language topics to load. Will load
  220. * the topic as well.
  221. *
  222. * @param string $topic The topic to load, in standard namespace:topic format
  223. * @return boolean True if successful
  224. */
  225. public function addLangTopic($topic) {
  226. return $this->addLexiconTopic($topic);
  227. }
  228. /**
  229. * Adds a lexicon topic to this page's language topics to load
  230. *
  231. * @return boolean True if successful
  232. */
  233. public function getLangTopics() {
  234. $topics = $this->modx->smarty->get_template_vars('_lang_topics');
  235. return explode(',',$topics);
  236. }
  237. /**
  238. * Sets the language topics for this page
  239. *
  240. * @param array $topics The array of topics to set
  241. * @return boolean True if successful
  242. */
  243. public function setLangTopics(array $topics = array()) {
  244. if (!is_array($topics) || empty($topics)) return false;
  245. $topics = array_unique($topics);
  246. $topics = implode(',',$topics);
  247. return $this->setPlaceholder('_lang_topics',$topics);
  248. }
  249. }