PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 327 lines | 200 code | 35 blank | 92 comment | 34 complexity | 87f2daf44842ddd72806cd313c3e9935 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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_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 Fron 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. if (!$request->isStraight()) {
  144. Varien_Profiler::start('mage::dispatch::db_url_rewrite');
  145. Mage::getModel('core/url_rewrite')->rewrite();
  146. Varien_Profiler::stop('mage::dispatch::db_url_rewrite');
  147. }
  148. Varien_Profiler::start('mage::dispatch::config_url_rewrite');
  149. $this->rewrite();
  150. Varien_Profiler::stop('mage::dispatch::config_url_rewrite');
  151. Varien_Profiler::start('mage::dispatch::routers_match');
  152. $i = 0;
  153. while (!$request->isDispatched() && $i++<100) {
  154. foreach ($this->_routers as $router) {
  155. if ($router->match($this->getRequest())) {
  156. break;
  157. }
  158. }
  159. }
  160. Varien_Profiler::stop('mage::dispatch::routers_match');
  161. if ($i>100) {
  162. Mage::throwException('Front controller reached 100 router match iterations');
  163. }
  164. //This event give possibility to launch smth before sending ouptut(Allow cookie setting)
  165. Mage::dispatchEvent('controller_front_send_response_before', array('front'=>$this));
  166. Varien_Profiler::start('mage::app::dispatch::send_response');
  167. $this->getResponse()->sendResponse();
  168. Varien_Profiler::stop('mage::app::dispatch::send_response');
  169. Mage::dispatchEvent('controller_front_send_response_after', array('front'=>$this));
  170. return $this;
  171. }
  172. public function getRouterByRoute($routeName)
  173. {
  174. // empty route supplied - return base url
  175. if (empty($routeName)) {
  176. $router = $this->getRouter('standard');
  177. } elseif ($this->getRouter('admin')->getFrontNameByRoute($routeName)) {
  178. // try standard router url assembly
  179. $router = $this->getRouter('admin');
  180. } elseif ($this->getRouter('standard')->getFrontNameByRoute($routeName)) {
  181. // try standard router url assembly
  182. $router = $this->getRouter('standard');
  183. } elseif ($router = $this->getRouter($routeName)) {
  184. // try custom router url assembly
  185. } else {
  186. // get default router url
  187. $router = $this->getRouter('default');
  188. }
  189. return $router;
  190. }
  191. public function getRouterByFrontName($frontName)
  192. {
  193. // empty route supplied - return base url
  194. if (empty($frontName)) {
  195. $router = $this->getRouter('standard');
  196. } elseif ($this->getRouter('admin')->getRouteByFrontName($frontName)) {
  197. // try standard router url assembly
  198. $router = $this->getRouter('admin');
  199. } elseif ($this->getRouter('standard')->getRouteByFrontName($frontName)) {
  200. // try standard router url assembly
  201. $router = $this->getRouter('standard');
  202. } elseif ($router = $this->getRouter($frontName)) {
  203. // try custom router url assembly
  204. } else {
  205. // get default router url
  206. $router = $this->getRouter('default');
  207. }
  208. return $router;
  209. }
  210. /**
  211. * Apply configuration rewrites to current url
  212. *
  213. * @return Mage_Core_Controller_Varien_Front
  214. */
  215. public function rewrite()
  216. {
  217. $request = $this->getRequest();
  218. $config = Mage::getConfig()->getNode('global/rewrite');
  219. if (!$config) {
  220. return;
  221. }
  222. foreach ($config->children() as $rewrite) {
  223. $from = (string)$rewrite->from;
  224. $to = (string)$rewrite->to;
  225. if (empty($from) || empty($to)) {
  226. continue;
  227. }
  228. $from = $this->_processRewriteUrl($from);
  229. $to = $this->_processRewriteUrl($to);
  230. $pathInfo = preg_replace($from, $to, $request->getPathInfo());
  231. if (isset($rewrite->complete)) {
  232. $request->setPathInfo($pathInfo);
  233. } else {
  234. $request->rewritePathInfo($pathInfo);
  235. }
  236. }
  237. }
  238. /**
  239. * Replace route name placeholders in url to front name
  240. *
  241. * @param string $url
  242. * @return string
  243. */
  244. protected function _processRewriteUrl($url)
  245. {
  246. $startPos = strpos($url, '{');
  247. if ($startPos!==false) {
  248. $endPos = strpos($url, '}');
  249. $routeName = substr($url, $startPos+1, $endPos-$startPos-1);
  250. $router = $this->getRouterByRoute($routeName);
  251. if ($router) {
  252. $fronName = $router->getFrontNameByRoute($routeName);
  253. $url = str_replace('{'.$routeName.'}', $fronName, $url);
  254. }
  255. }
  256. return $url;
  257. }
  258. /**
  259. * Auto-redirect to base url (without SID) if the requested url doesn't match it.
  260. * By default this feature is enabled in configuration.
  261. *
  262. * @param Zend_Controller_Request_Http $request
  263. */
  264. protected function _checkBaseUrl($request)
  265. {
  266. if (!Mage::isInstalled() || $request->getPost()) {
  267. return;
  268. }
  269. if (!Mage::getStoreConfig('web/url/redirect_to_base')) {
  270. return;
  271. }
  272. $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, Mage::app()->getStore()->isCurrentlySecure());
  273. if (!$baseUrl) {
  274. return;
  275. }
  276. $redirectCode = 302;
  277. if (Mage::getStoreConfig('web/url/redirect_to_base')==301) {
  278. $redirectCode = 301;
  279. }
  280. $uri = @parse_url($baseUrl);
  281. $host = isset($uri['host']) ? $uri['host'] : '';
  282. $path = isset($uri['path']) ? $uri['path'] : '';
  283. $requestUri = $request->getRequestUri() ? $request->getRequestUri() : '/';
  284. if ($host && $host != $request->getHttpHost() || $path && strpos($requestUri, $path) === false)
  285. {
  286. Mage::app()->getFrontController()->getResponse()
  287. ->setRedirect($baseUrl, $redirectCode)
  288. ->sendResponse();
  289. exit;
  290. }
  291. }
  292. }