PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Url/Rewrite.php

https://github.com/jpratt/cal
PHP | 282 lines | 188 code | 40 blank | 54 comment | 37 complexity | 0a5689338a6400a86aaa77c668ef41ad 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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Url rewrite model class
  28. *
  29. *
  30. * @category Mage
  31. * @package Mage_Core
  32. * @author Magento Core Team <core@magentocommerce.com>
  33. */
  34. class Mage_Core_Model_Url_Rewrite extends Mage_Core_Model_Abstract
  35. {
  36. const TYPE_CATEGORY = 1;
  37. const TYPE_PRODUCT = 2;
  38. const TYPE_CUSTOM = 3;
  39. const REWRITE_REQUEST_PATH_ALIAS = 'rewrite_request_path';
  40. protected function _construct()
  41. {
  42. $this->_init('core/url_rewrite');
  43. }
  44. /**
  45. * Load rewrite information for request
  46. *
  47. * if $path is array - that mean what we need try load for each item
  48. *
  49. * @param mixed $path
  50. * @return Mage_Core_Model_Url_Rewrite
  51. */
  52. public function loadByRequestPath($path)
  53. {
  54. $this->setId(null);
  55. if (is_array($path)) {
  56. foreach ($path as $pathInfo) {
  57. $this->load($pathInfo, 'request_path');
  58. if ($this->getId()) {
  59. return $this;
  60. }
  61. }
  62. }
  63. else {
  64. $this->load($path, 'request_path');
  65. }
  66. return $this;
  67. }
  68. public function loadByIdPath($path)
  69. {
  70. $this->setId(null)->load($path, 'id_path');
  71. return $this;
  72. }
  73. public function loadByTags($tags)
  74. {
  75. $this->setId(null);
  76. $loadTags = is_array($tags) ? $tags : explode(',', $tags);
  77. $search = $this->getResourceCollection();
  78. foreach ($loadTags as $k=>$t) {
  79. if (!is_numeric($k)) {
  80. $t = $k.'='.$t;
  81. }
  82. $search->addTagsFilter($t);
  83. }
  84. if (!is_null($this->getStoreId())) {
  85. $search->addStoreFilter($this->getStoreId());
  86. }
  87. $search->setPageSize(1)->load();
  88. if ($search->getSize()>0) {
  89. foreach ($search as $rewrite) {
  90. $this->setData($rewrite->getData());
  91. }
  92. }
  93. return $this;
  94. }
  95. public function hasOption($key)
  96. {
  97. $optArr = explode(',', $this->getOptions());
  98. return array_search($key, $optArr) !== false;
  99. }
  100. public function addTag($tags)
  101. {
  102. $curTags = $this->getTags();
  103. $addTags = is_array($tags) ? $tags : explode(',', $tags);
  104. foreach ($addTags as $k=>$t) {
  105. if (!is_numeric($k)) {
  106. $t = $k.'='.$t;
  107. }
  108. if (!in_array($t, $curTags)) {
  109. $curTags[] = $t;
  110. }
  111. }
  112. $this->setTags($curTags);
  113. return $this;
  114. }
  115. public function removeTag($tags)
  116. {
  117. $curTags = $this->getTags();
  118. $removeTags = is_array($tags) ? $tags : explode(',', $tags);
  119. foreach ($removeTags as $t) {
  120. if (!is_numeric($k)) {
  121. $t = $k.'='.$t;
  122. }
  123. if ($key = array_search($t, $curTags)) {
  124. unset($curTags[$key]);
  125. }
  126. }
  127. $this->setTags(',', $curTags);
  128. return $this;
  129. }
  130. /**
  131. * Implement logic of custom rewrites
  132. *
  133. * @param Zend_Controller_Request_Http $request
  134. * @param Zend_Controller_Response_Http $response
  135. * @return Mage_Core_Model_Url
  136. */
  137. public function rewrite(Zend_Controller_Request_Http $request=null, Zend_Controller_Response_Http $response=null)
  138. {
  139. if (!Mage::isInstalled()) {
  140. return false;
  141. }
  142. if (is_null($request)) {
  143. $request = Mage::app()->getFrontController()->getRequest();
  144. }
  145. if (is_null($response)) {
  146. $response = Mage::app()->getFrontController()->getResponse();
  147. }
  148. if (is_null($this->getStoreId()) || false===$this->getStoreId()) {
  149. $this->setStoreId(Mage::app()->getStore()->getId());
  150. }
  151. $requestCases = array();
  152. $requestPath = trim($request->getPathInfo(), '/');
  153. /**
  154. * We need try to find rewrites information for both cases
  155. * More priority has url with query params
  156. */
  157. if ($queryString = $this->_getQueryString()) {
  158. $requestCases[] = $requestPath .'?'.$queryString;
  159. $requestCases[] = $requestPath;
  160. }
  161. else {
  162. $requestCases[] = $requestPath;
  163. }
  164. $this->loadByRequestPath($requestCases);
  165. /**
  166. * Try to find rewrite by request path at first, if no luck - try to find by id_path
  167. */
  168. if (!$this->getId() && isset($_GET['___from_store'])) {
  169. try {
  170. $fromStoreId = Mage::app()->getStore($_GET['___from_store']);
  171. }
  172. catch (Exception $e) {
  173. return false;
  174. }
  175. $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
  176. if (!$this->getId()) {
  177. return false;
  178. }
  179. $this->setStoreId(Mage::app()->getStore()->getId())->loadByIdPath($this->getIdPath());
  180. }
  181. if (!$this->getId()) {
  182. return false;
  183. }
  184. $request->setAlias(self::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
  185. $external = substr($this->getTargetPath(), 0, 6);
  186. $isPermanentRedirectOption = $this->hasOption('RP');
  187. if ($external === 'http:/' || $external === 'https:') {
  188. if ($isPermanentRedirectOption) {
  189. header('HTTP/1.1 301 Moved Permanently');
  190. }
  191. header("Location: ".$this->getTargetPath());
  192. exit;
  193. } else {
  194. $targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
  195. }
  196. $isRedirectOption = $this->hasOption('R');
  197. if ($isRedirectOption || $isPermanentRedirectOption) {
  198. if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
  199. $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
  200. }
  201. if ($isPermanentRedirectOption) {
  202. header('HTTP/1.1 301 Moved Permanently');
  203. }
  204. header('Location: '.$targetUrl);
  205. exit;
  206. }
  207. if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
  208. $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
  209. }
  210. if ($queryString = $this->_getQueryString()) {
  211. $targetUrl .= '?'.$queryString;
  212. }
  213. $request->setRequestUri($targetUrl);
  214. $request->setPathInfo($this->getTargetPath());
  215. return true;
  216. }
  217. protected function _getQueryString()
  218. {
  219. if (!empty($_SERVER['QUERY_STRING'])) {
  220. $queryParams = array();
  221. parse_str($_SERVER['QUERY_STRING'], $queryParams);
  222. $hasChanges = false;
  223. foreach ($queryParams as $key=>$value) {
  224. if (substr($key, 0, 3) === '___') {
  225. unset($queryParams[$key]);
  226. $hasChanges = true;
  227. }
  228. }
  229. if ($hasChanges) {
  230. return http_build_query($queryParams);
  231. }
  232. else {
  233. return $_SERVER['QUERY_STRING'];
  234. }
  235. }
  236. return false;
  237. }
  238. public function getStoreId()
  239. {
  240. return $this->_getData('store_id');
  241. }
  242. }