PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php

https://github.com/arush/desparation-deprecated
PHP | 460 lines | 307 code | 55 blank | 98 comment | 68 complexity | 4659d8d465a227962fefbfa8baba54d2 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Core_Controller_Varien_Router_Standard extends Mage_Core_Controller_Varien_Router_Abstract
  27. {
  28. protected $_modules = array();
  29. protected $_routes = array();
  30. protected $_dispatchData = array();
  31. public function collectRoutes($configArea, $useRouterName)
  32. {
  33. $routers = array();
  34. $routersConfigNode = Mage::getConfig()->getNode($configArea.'/routers');
  35. if($routersConfigNode) {
  36. $routers = $routersConfigNode->children();
  37. }
  38. foreach ($routers as $routerName=>$routerConfig) {
  39. $use = (string)$routerConfig->use;
  40. if ($use == $useRouterName) {
  41. $modules = array((string)$routerConfig->args->module);
  42. if ($routerConfig->args->modules) {
  43. foreach ($routerConfig->args->modules->children() as $customModule) {
  44. if ($customModule) {
  45. if ($before = $customModule->getAttribute('before')) {
  46. $position = array_search($before, $modules);
  47. if ($position === false) {
  48. $position = 0;
  49. }
  50. array_splice($modules, $position, 0, (string)$customModule);
  51. } elseif ($after = $customModule->getAttribute('after')) {
  52. $position = array_search($after, $modules);
  53. if ($position === false) {
  54. $position = count($modules);
  55. }
  56. array_splice($modules, $position+1, 0, (string)$customModule);
  57. } else {
  58. $modules[] = (string)$customModule;
  59. }
  60. }
  61. }
  62. }
  63. $frontName = (string)$routerConfig->args->frontName;
  64. $this->addModule($frontName, $modules, $routerName);
  65. }
  66. }
  67. }
  68. public function fetchDefault()
  69. {
  70. $this->getFront()->setDefault(array(
  71. 'module' => 'core',
  72. 'controller' => 'index',
  73. 'action' => 'index'
  74. ));
  75. }
  76. /**
  77. * checking if this admin if yes then we don't use this router
  78. *
  79. * @return bool
  80. */
  81. protected function _beforeModuleMatch()
  82. {
  83. if (Mage::app()->getStore()->isAdmin()) {
  84. return false;
  85. }
  86. return true;
  87. }
  88. /**
  89. * dummy call to pass through checking
  90. *
  91. * @return bool
  92. */
  93. protected function _afterModuleMatch()
  94. {
  95. return true;
  96. }
  97. /**
  98. * Match the request
  99. *
  100. * @param Zend_Controller_Request_Http $request
  101. * @return boolean
  102. */
  103. public function match(Zend_Controller_Request_Http $request)
  104. {
  105. //checking before even try to find out that current module
  106. //should use this router
  107. if (!$this->_beforeModuleMatch()) {
  108. return false;
  109. }
  110. $this->fetchDefault();
  111. $front = $this->getFront();
  112. $path = trim($request->getPathInfo(), '/');
  113. if ($path) {
  114. $p = explode('/', $path);
  115. } else {
  116. $p = explode('/', $this->_getDefaultPath());
  117. }
  118. // get module name
  119. if ($request->getModuleName()) {
  120. $module = $request->getModuleName();
  121. } else {
  122. if (!empty($p[0])) {
  123. $module = $p[0];
  124. } else {
  125. $module = $this->getFront()->getDefault('module');
  126. $request->setAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS, '');
  127. }
  128. }
  129. if (!$module) {
  130. if (Mage::app()->getStore()->isAdmin()) {
  131. $module = 'admin';
  132. } else {
  133. return false;
  134. }
  135. }
  136. /**
  137. * Searching router args by module name from route using it as key
  138. */
  139. $modules = $this->getModuleByFrontName($module);
  140. if ($modules === false) {
  141. return false;
  142. }
  143. //checkings after we foundout that this router should be used for current module
  144. if (!$this->_afterModuleMatch()) {
  145. return false;
  146. }
  147. /**
  148. * Going through modules to find appropriate controller
  149. */
  150. $found = false;
  151. foreach ($modules as $realModule) {
  152. $request->setRouteName($this->getRouteByFrontName($module));
  153. // get controller name
  154. if ($request->getControllerName()) {
  155. $controller = $request->getControllerName();
  156. } else {
  157. if (!empty($p[1])) {
  158. $controller = $p[1];
  159. } else {
  160. $controller = $front->getDefault('controller');
  161. $request->setAlias(
  162. Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
  163. ltrim($request->getOriginalPathInfo(), '/')
  164. );
  165. }
  166. }
  167. // get action name
  168. if (empty($action)) {
  169. if ($request->getActionName()) {
  170. $action = $request->getActionName();
  171. } else {
  172. $action = !empty($p[2]) ? $p[2] : $front->getDefault('action');
  173. }
  174. }
  175. //checking if this place should be secure
  176. $this->_checkShouldBeSecure($request, '/'.$module.'/'.$controller.'/'.$action);
  177. $controllerClassName = $this->_validateControllerClassName($realModule, $controller);
  178. if (!$controllerClassName) {
  179. continue;
  180. }
  181. // instantiate controller class
  182. $controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());
  183. if (!$controllerInstance->hasAction($action)) {
  184. continue;
  185. }
  186. $found = true;
  187. break;
  188. }
  189. /**
  190. * if we did not found any siutibul
  191. */
  192. if (!$found) {
  193. if ($this->_noRouteShouldBeApplied()) {
  194. $controller = 'index';
  195. $action = 'noroute';
  196. $controllerClassName = $this->_validateControllerClassName($realModule, $controller);
  197. if (!$controllerClassName) {
  198. return false;
  199. }
  200. // instantiate controller class
  201. $controllerInstance = Mage::getControllerInstance($controllerClassName, $request,
  202. $front->getResponse());
  203. if (!$controllerInstance->hasAction($action)) {
  204. return false;
  205. }
  206. } else {
  207. return false;
  208. }
  209. }
  210. // set values only after all the checks are done
  211. $request->setModuleName($module);
  212. $request->setControllerName($controller);
  213. $request->setActionName($action);
  214. $request->setControllerModule($realModule);
  215. // set parameters from pathinfo
  216. for ($i = 3, $l = sizeof($p); $i < $l; $i += 2) {
  217. $request->setParam($p[$i], isset($p[$i+1]) ? urldecode($p[$i+1]) : '');
  218. }
  219. // dispatch action
  220. $request->setDispatched(true);
  221. $controllerInstance->dispatch($action);
  222. return true;
  223. }
  224. /**
  225. * Get router default request path
  226. * @return string
  227. */
  228. protected function _getDefaultPath()
  229. {
  230. return Mage::getStoreConfig('web/default/front');
  231. }
  232. /**
  233. * Allow to control if we need to enable no route functionality in current router
  234. *
  235. * @return bool
  236. */
  237. protected function _noRouteShouldBeApplied()
  238. {
  239. return false;
  240. }
  241. /**
  242. * Generating and validating class file name,
  243. * class and if evrything ok do include if needed and return of class name
  244. *
  245. * @return mixed
  246. */
  247. protected function _validateControllerClassName($realModule, $controller)
  248. {
  249. $controllerFileName = $this->getControllerFileName($realModule, $controller);
  250. if (!$this->validateControllerFileName($controllerFileName)) {
  251. return false;
  252. }
  253. $controllerClassName = $this->getControllerClassName($realModule, $controller);
  254. if (!$controllerClassName) {
  255. return false;
  256. }
  257. // include controller file if needed
  258. if (!$this->_includeControllerClass($controllerFileName, $controllerClassName)) {
  259. return false;
  260. }
  261. return $controllerClassName;
  262. }
  263. /**
  264. * @deprecated
  265. * @see _includeControllerClass()
  266. */
  267. protected function _inludeControllerClass($controllerFileName, $controllerClassName)
  268. {
  269. return $this->_includeControllerClass($controllerFileName, $controllerClassName);
  270. }
  271. /**
  272. * Include the file containing controller class if this class is not defined yet
  273. *
  274. * @param string $controllerFileName
  275. * @param string $controllerClassName
  276. * @return bool
  277. */
  278. protected function _includeControllerClass($controllerFileName, $controllerClassName)
  279. {
  280. if (!class_exists($controllerClassName, false)) {
  281. if (!file_exists($controllerFileName)) {
  282. return false;
  283. }
  284. include $controllerFileName;
  285. if (!class_exists($controllerClassName, false)) {
  286. throw Mage::exception('Mage_Core', Mage::helper('core')->__('Controller file was loaded but class does not exist'));
  287. }
  288. }
  289. return true;
  290. }
  291. public function addModule($frontName, $moduleName, $routeName)
  292. {
  293. $this->_modules[$frontName] = $moduleName;
  294. $this->_routes[$routeName] = $frontName;
  295. return $this;
  296. }
  297. public function getModuleByFrontName($frontName)
  298. {
  299. if (isset($this->_modules[$frontName])) {
  300. return $this->_modules[$frontName];
  301. }
  302. return false;
  303. }
  304. public function getModuleByName($moduleName, $modules)
  305. {
  306. foreach ($modules as $module) {
  307. if ($moduleName === $module || (is_array($module)
  308. && $this->getModuleByName($moduleName, $module))) {
  309. return true;
  310. }
  311. }
  312. return false;
  313. }
  314. public function getFrontNameByRoute($routeName)
  315. {
  316. if (isset($this->_routes[$routeName])) {
  317. return $this->_routes[$routeName];
  318. }
  319. return false;
  320. }
  321. public function getRouteByFrontName($frontName)
  322. {
  323. return array_search($frontName, $this->_routes);
  324. }
  325. public function getControllerFileName($realModule, $controller)
  326. {
  327. $parts = explode('_', $realModule);
  328. $realModule = implode('_', array_splice($parts, 0, 2));
  329. $file = Mage::getModuleDir('controllers', $realModule);
  330. if (count($parts)) {
  331. $file .= DS . implode(DS, $parts);
  332. }
  333. $file .= DS.uc_words($controller, DS).'Controller.php';
  334. return $file;
  335. }
  336. public function validateControllerFileName($fileName)
  337. {
  338. if ($fileName && is_readable($fileName) && false===strpos($fileName, '//')) {
  339. return true;
  340. }
  341. return false;
  342. }
  343. public function getControllerClassName($realModule, $controller)
  344. {
  345. $class = $realModule.'_'.uc_words($controller).'Controller';
  346. return $class;
  347. }
  348. public function rewrite(array $p)
  349. {
  350. $rewrite = Mage::getConfig()->getNode('global/rewrite');
  351. if ($module = $rewrite->{$p[0]}) {
  352. if (!$module->children()) {
  353. $p[0] = trim((string)$module);
  354. }
  355. }
  356. if (isset($p[1]) && ($controller = $rewrite->{$p[0]}->{$p[1]})) {
  357. if (!$controller->children()) {
  358. $p[1] = trim((string)$controller);
  359. }
  360. }
  361. if (isset($p[2]) && ($action = $rewrite->{$p[0]}->{$p[1]}->{$p[2]})) {
  362. if (!$action->children()) {
  363. $p[2] = trim((string)$action);
  364. }
  365. }
  366. #echo "<pre>".print_r($p,1)."</pre>";
  367. return $p;
  368. }
  369. /**
  370. * Check if request URL should be secure
  371. *
  372. * Function redirects user to correct URL if needed
  373. *
  374. * @param Mage_Core_Controller_Request_Http $request
  375. * @param string $path
  376. * @return null
  377. */
  378. protected function _checkShouldBeSecure($request, $path = '')
  379. {
  380. if (!Mage::isInstalled() || $request->getPost()) {
  381. return;
  382. }
  383. if ($this->_shouldBeSecure($path) && !$request->isSecure()) {
  384. $url = $this->_getCurrentSecureUrl($request);
  385. Mage::app()->getFrontController()->getResponse()
  386. ->setRedirect($url)
  387. ->sendResponse();
  388. exit();
  389. }
  390. }
  391. protected function _getCurrentSecureUrl($request)
  392. {
  393. if ($alias = $request->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS)) {
  394. return Mage::getBaseUrl('link', true).ltrim($alias, '/');
  395. }
  396. return Mage::getBaseUrl('link', true).ltrim($request->getPathInfo(), '/');
  397. }
  398. protected function _shouldBeSecure($path)
  399. {
  400. return substr(Mage::getStoreConfig('web/unsecure/base_url'),0,5)==='https'
  401. || Mage::getStoreConfigFlag('web/secure/use_in_frontend')
  402. && substr(Mage::getStoreConfig('web/secure/base_url'),0,5)=='https'
  403. && Mage::getConfig()->shouldUrlBeSecure($path);
  404. }
  405. }