PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/app/code/core/Mage/Core/Controller/Varien/Front.php

https://bitbucket.org/mkrasuski/magento-ce
PHP | 389 lines | 230 code | 44 blank | 115 comment | 42 complexity | fa826d6cb39da808d9f2abdb6c4a3619 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Core_Controller_Varien_Front extends Varien_Object
  27. {
  28. protected $_defaults = array();
  29. /**
  30. * Available routers array
  31. *
  32. * @var array
  33. */
  34. protected $_routers = array();
  35. protected $_urlCache = array();
  36. const XML_STORE_ROUTERS_PATH = 'web/routers';
  37. public function setDefault($key, $value=null)
  38. {
  39. if (is_array($key)) {
  40. $this->_defaults = $key;
  41. } else {
  42. $this->_defaults[$key] = $value;
  43. }
  44. return $this;
  45. }
  46. public function getDefault($key=null)
  47. {
  48. if (is_null($key)) {
  49. return $this->_defaults;
  50. } elseif (isset($this->_defaults[$key])) {
  51. return $this->_defaults[$key];
  52. }
  53. return false;
  54. }
  55. /**
  56. * Retrieve request object
  57. *
  58. * @return Mage_Core_Controller_Request_Http
  59. */
  60. public function getRequest()
  61. {
  62. return Mage::app()->getRequest();
  63. }
  64. /**
  65. * Retrieve response object
  66. *
  67. * @return Zend_Controller_Response_Http
  68. */
  69. public function getResponse()
  70. {
  71. return Mage::app()->getResponse();
  72. }
  73. /**
  74. * Adding new router
  75. *
  76. * @param string $name
  77. * @param Mage_Core_Controller_Varien_Router_Abstract $router
  78. * @return Mage_Core_Controller_Varien_Front
  79. */
  80. public function addRouter($name, Mage_Core_Controller_Varien_Router_Abstract $router)
  81. {
  82. $router->setFront($this);
  83. $this->_routers[$name] = $router;
  84. return $this;
  85. }
  86. /**
  87. * Retrieve router by name
  88. *
  89. * @param string $name
  90. * @return Mage_Core_Controller_Varien_Router_Abstract
  91. */
  92. public function getRouter($name)
  93. {
  94. if (isset($this->_routers[$name])) {
  95. return $this->_routers[$name];
  96. }
  97. return false;
  98. }
  99. /**
  100. * Retrieve routers collection
  101. *
  102. * @return array
  103. */
  104. public function getRouters()
  105. {
  106. return $this->_routers;
  107. }
  108. /**
  109. * Init Front Controller
  110. *
  111. * @return Mage_Core_Controller_Varien_Front
  112. */
  113. public function init()
  114. {
  115. Mage::dispatchEvent('controller_front_init_before', array('front'=>$this));
  116. $routersInfo = Mage::app()->getStore()->getConfig(self::XML_STORE_ROUTERS_PATH);
  117. Varien_Profiler::start('mage::app::init_front_controller::collect_routers');
  118. foreach ($routersInfo as $routerCode => $routerInfo) {
  119. if (isset($routerInfo['disabled']) && $routerInfo['disabled']) {
  120. continue;
  121. }
  122. if (isset($routerInfo['class'])) {
  123. $router = new $routerInfo['class'];
  124. if (isset($routerInfo['area'])) {
  125. $router->collectRoutes($routerInfo['area'], $routerCode);
  126. }
  127. $this->addRouter($routerCode, $router);
  128. }
  129. }
  130. Varien_Profiler::stop('mage::app::init_front_controller::collect_routers');
  131. Mage::dispatchEvent('controller_front_init_routers', array('front'=>$this));
  132. // Add default router at the last
  133. $default = new Mage_Core_Controller_Varien_Router_Default();
  134. $this->addRouter('default', $default);
  135. return $this;
  136. }
  137. public function dispatch()
  138. {
  139. $request = $this->getRequest();
  140. // If pre-configured, check equality of base URL and requested URL
  141. $this->_checkBaseUrl($request);
  142. $request->setPathInfo()->setDispatched(false);
  143. $this->_getRequestRewriteController()->rewrite();
  144. Varien_Profiler::start('mage::dispatch::routers_match');
  145. $i = 0;
  146. while (!$request->isDispatched() && $i++ < 100) {
  147. foreach ($this->_routers as $router) {
  148. /** @var $router Mage_Core_Controller_Varien_Router_Abstract */
  149. if ($router->match($request)) {
  150. break;
  151. }
  152. }
  153. }
  154. Varien_Profiler::stop('mage::dispatch::routers_match');
  155. if ($i>100) {
  156. Mage::throwException('Front controller reached 100 router match iterations');
  157. }
  158. // This event gives possibility to launch something before sending output (allow cookie setting)
  159. Mage::dispatchEvent('controller_front_send_response_before', array('front'=>$this));
  160. Varien_Profiler::start('mage::app::dispatch::send_response');
  161. $this->getResponse()->sendResponse();
  162. Varien_Profiler::stop('mage::app::dispatch::send_response');
  163. Mage::dispatchEvent('controller_front_send_response_after', array('front'=>$this));
  164. return $this;
  165. }
  166. /**
  167. * Returns request rewrite instance.
  168. * Class name alias is declared in the configuration
  169. *
  170. * @return Mage_Core_Model_Url_Rewrite_Request
  171. */
  172. protected function _getRequestRewriteController()
  173. {
  174. $className = (string)Mage::getConfig()->getNode('global/request_rewrite/model');
  175. return Mage::getSingleton('core/factory')->getModel($className, array(
  176. 'routers' => $this->getRouters(),
  177. ));
  178. }
  179. /**
  180. * Returns router instance by route name
  181. *
  182. * @param string $routeName
  183. * @return Mage_Core_Controller_Varien_Router_Abstract
  184. */
  185. public function getRouterByRoute($routeName)
  186. {
  187. // empty route supplied - return base url
  188. if (empty($routeName)) {
  189. $router = $this->getRouter('standard');
  190. } elseif ($this->getRouter('admin')->getFrontNameByRoute($routeName)) {
  191. // try standard router url assembly
  192. $router = $this->getRouter('admin');
  193. } elseif ($this->getRouter('standard')->getFrontNameByRoute($routeName)) {
  194. // try standard router url assembly
  195. $router = $this->getRouter('standard');
  196. } elseif ($router = $this->getRouter($routeName)) {
  197. // try custom router url assembly
  198. } else {
  199. // get default router url
  200. $router = $this->getRouter('default');
  201. }
  202. return $router;
  203. }
  204. public function getRouterByFrontName($frontName)
  205. {
  206. // empty route supplied - return base url
  207. if (empty($frontName)) {
  208. $router = $this->getRouter('standard');
  209. } elseif ($this->getRouter('admin')->getRouteByFrontName($frontName)) {
  210. // try standard router url assembly
  211. $router = $this->getRouter('admin');
  212. } elseif ($this->getRouter('standard')->getRouteByFrontName($frontName)) {
  213. // try standard router url assembly
  214. $router = $this->getRouter('standard');
  215. } elseif ($router = $this->getRouter($frontName)) {
  216. // try custom router url assembly
  217. } else {
  218. // get default router url
  219. $router = $this->getRouter('default');
  220. }
  221. return $router;
  222. }
  223. /**
  224. * Apply configuration rewrites to current url
  225. *
  226. * @return Mage_Core_Controller_Varien_Front
  227. * @deprecated since 1.7.0.2. Refactored and moved to Mage_Core_Controller_Request_Rewrite
  228. */
  229. public function rewrite()
  230. {
  231. $request = $this->getRequest();
  232. $config = Mage::getConfig()->getNode('global/rewrite');
  233. if (!$config) {
  234. return;
  235. }
  236. foreach ($config->children() as $rewrite) {
  237. $from = (string)$rewrite->from;
  238. $to = (string)$rewrite->to;
  239. if (empty($from) || empty($to)) {
  240. continue;
  241. }
  242. $from = $this->_processRewriteUrl($from);
  243. $to = $this->_processRewriteUrl($to);
  244. $pathInfo = preg_replace($from, $to, $request->getPathInfo());
  245. if (isset($rewrite->complete)) {
  246. $request->setPathInfo($pathInfo);
  247. } else {
  248. $request->rewritePathInfo($pathInfo);
  249. }
  250. }
  251. }
  252. /**
  253. * Replace route name placeholders in url to front name
  254. *
  255. * @param string $url
  256. * @return string
  257. * @deprecated since 1.7.0.2. Refactored and moved to Mage_Core_Controller_Request_Rewrite
  258. */
  259. protected function _processRewriteUrl($url)
  260. {
  261. $startPos = strpos($url, '{');
  262. if ($startPos!==false) {
  263. $endPos = strpos($url, '}');
  264. $routeName = substr($url, $startPos+1, $endPos-$startPos-1);
  265. $router = $this->getRouterByRoute($routeName);
  266. if ($router) {
  267. $fronName = $router->getFrontNameByRoute($routeName);
  268. $url = str_replace('{'.$routeName.'}', $fronName, $url);
  269. }
  270. }
  271. return $url;
  272. }
  273. /**
  274. * Auto-redirect to base url (without SID) if the requested url doesn't match it.
  275. * By default this feature is enabled in configuration.
  276. *
  277. * @param Zend_Controller_Request_Http $request
  278. */
  279. protected function _checkBaseUrl($request)
  280. {
  281. if (!Mage::isInstalled() || $request->getPost() || strtolower($request->getMethod()) == 'post') {
  282. return;
  283. }
  284. $redirectCode = (int)Mage::getStoreConfig('web/url/redirect_to_base');
  285. if (!$redirectCode) {
  286. return;
  287. } elseif ($redirectCode != 301) {
  288. $redirectCode = 302;
  289. }
  290. if ($this->_isAdminFrontNameMatched($request)) {
  291. return;
  292. }
  293. $baseUrl = Mage::getBaseUrl(
  294. Mage_Core_Model_Store::URL_TYPE_WEB,
  295. Mage::app()->getStore()->isCurrentlySecure()
  296. );
  297. if (!$baseUrl) {
  298. return;
  299. }
  300. $uri = @parse_url($baseUrl);
  301. $requestUri = $request->getRequestUri() ? $request->getRequestUri() : '/';
  302. if (isset($uri['scheme']) && $uri['scheme'] != $request->getScheme()
  303. || isset($uri['host']) && $uri['host'] != $request->getHttpHost()
  304. || isset($uri['path']) && strpos($requestUri, $uri['path']) === false
  305. ) {
  306. Mage::app()->getFrontController()->getResponse()
  307. ->setRedirect($baseUrl, $redirectCode)
  308. ->sendResponse();
  309. exit;
  310. }
  311. }
  312. /**
  313. * Check if requested path starts with one of the admin front names
  314. *
  315. * @param Zend_Controller_Request_Http $request
  316. * @return boolean
  317. */
  318. protected function _isAdminFrontNameMatched($request)
  319. {
  320. $useCustomAdminPath = (bool)(string)Mage::getConfig()
  321. ->getNode(Mage_Adminhtml_Helper_Data::XML_PATH_USE_CUSTOM_ADMIN_PATH);
  322. $customAdminPath = (string)Mage::getConfig()->getNode(Mage_Adminhtml_Helper_Data::XML_PATH_CUSTOM_ADMIN_PATH);
  323. $adminPath = ($useCustomAdminPath) ? $customAdminPath : null;
  324. if (!$adminPath) {
  325. $adminPath = (string)Mage::getConfig()
  326. ->getNode(Mage_Adminhtml_Helper_Data::XML_PATH_ADMINHTML_ROUTER_FRONTNAME);
  327. }
  328. $adminFrontNames = array($adminPath);
  329. // Check for other modules that can use admin router (a lot of Magento extensions do that)
  330. $adminFrontNameNodes = Mage::getConfig()->getNode('admin/routers')
  331. ->xpath('*[not(self::adminhtml) and use = "admin"]/args/frontName');
  332. if (is_array($adminFrontNameNodes)) {
  333. foreach ($adminFrontNameNodes as $frontNameNode) {
  334. /** @var $frontNameNode SimpleXMLElement */
  335. array_push($adminFrontNames, (string)$frontNameNode);
  336. }
  337. }
  338. $pathPrefix = ltrim($request->getPathInfo(), '/');
  339. $urlDelimiterPos = strpos($pathPrefix, '/');
  340. if ($urlDelimiterPos) {
  341. $pathPrefix = substr($pathPrefix, 0, $urlDelimiterPos);
  342. }
  343. return in_array($pathPrefix, $adminFrontNames);
  344. }
  345. }