PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/gryzz/crystal_magento
PHP | 315 lines | 201 code | 46 blank | 68 comment | 39 complexity | cfd9acaf3b758b033f1e369725bebd87 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) 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. /**
  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. /**
  41. * Cache tag for clear cache in after save and after delete
  42. *
  43. * @var mixed | array | string | boolean
  44. */
  45. protected $_cacheTag = false;
  46. protected function _construct()
  47. {
  48. $this->_init('core/url_rewrite');
  49. }
  50. /**
  51. * Clean cache for front-end menu
  52. *
  53. * @return Mage_Core_Model_Url_Rewrite
  54. */
  55. protected function _afterSave()
  56. {
  57. if ($this->hasCategoryId()) {
  58. $this->_cacheTag = array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG);
  59. }
  60. parent::_afterSave();
  61. return $this;
  62. }
  63. /**
  64. * Load rewrite information for request
  65. *
  66. * if $path is array - that mean what we need try load for each item
  67. *
  68. * @param mixed $path
  69. * @return Mage_Core_Model_Url_Rewrite
  70. */
  71. public function loadByRequestPath($path)
  72. {
  73. $this->setId(null);
  74. if (is_array($path)) {
  75. foreach ($path as $pathInfo) {
  76. $this->load($pathInfo, 'request_path');
  77. if ($this->getId()) {
  78. return $this;
  79. }
  80. }
  81. }
  82. else {
  83. $this->load($path, 'request_path');
  84. }
  85. return $this;
  86. }
  87. public function loadByIdPath($path)
  88. {
  89. $this->setId(null)->load($path, 'id_path');
  90. return $this;
  91. }
  92. public function loadByTags($tags)
  93. {
  94. $this->setId(null);
  95. $loadTags = is_array($tags) ? $tags : explode(',', $tags);
  96. $search = $this->getResourceCollection();
  97. foreach ($loadTags as $k=>$t) {
  98. if (!is_numeric($k)) {
  99. $t = $k.'='.$t;
  100. }
  101. $search->addTagsFilter($t);
  102. }
  103. if (!is_null($this->getStoreId())) {
  104. $search->addStoreFilter($this->getStoreId());
  105. }
  106. $search->setPageSize(1)->load();
  107. if ($search->getSize()>0) {
  108. foreach ($search as $rewrite) {
  109. $this->setData($rewrite->getData());
  110. }
  111. }
  112. return $this;
  113. }
  114. public function hasOption($key)
  115. {
  116. $optArr = explode(',', $this->getOptions());
  117. return array_search($key, $optArr) !== false;
  118. }
  119. public function addTag($tags)
  120. {
  121. $curTags = $this->getTags();
  122. $addTags = is_array($tags) ? $tags : explode(',', $tags);
  123. foreach ($addTags as $k=>$t) {
  124. if (!is_numeric($k)) {
  125. $t = $k.'='.$t;
  126. }
  127. if (!in_array($t, $curTags)) {
  128. $curTags[] = $t;
  129. }
  130. }
  131. $this->setTags($curTags);
  132. return $this;
  133. }
  134. public function removeTag($tags)
  135. {
  136. $curTags = $this->getTags();
  137. $removeTags = is_array($tags) ? $tags : explode(',', $tags);
  138. foreach ($removeTags as $t) {
  139. if (!is_numeric($k)) {
  140. $t = $k.'='.$t;
  141. }
  142. if ($key = array_search($t, $curTags)) {
  143. unset($curTags[$key]);
  144. }
  145. }
  146. $this->setTags(',', $curTags);
  147. return $this;
  148. }
  149. /**
  150. * Implement logic of custom rewrites
  151. *
  152. * @param Zend_Controller_Request_Http $request
  153. * @param Zend_Controller_Response_Http $response
  154. * @return Mage_Core_Model_Url
  155. */
  156. public function rewrite(Zend_Controller_Request_Http $request=null, Zend_Controller_Response_Http $response=null)
  157. {
  158. if (!Mage::isInstalled()) {
  159. return false;
  160. }
  161. if (is_null($request)) {
  162. $request = Mage::app()->getFrontController()->getRequest();
  163. }
  164. if (is_null($response)) {
  165. $response = Mage::app()->getFrontController()->getResponse();
  166. }
  167. if (is_null($this->getStoreId()) || false===$this->getStoreId()) {
  168. $this->setStoreId(Mage::app()->getStore()->getId());
  169. }
  170. $requestCases = array();
  171. $requestPath = trim($request->getPathInfo(), '/');
  172. /**
  173. * We need try to find rewrites information for both cases
  174. * More priority has url with query params
  175. */
  176. if ($queryString = $this->_getQueryString()) {
  177. $requestCases[] = $requestPath .'?'.$queryString;
  178. $requestCases[] = $requestPath;
  179. }
  180. else {
  181. $requestCases[] = $requestPath;
  182. }
  183. $this->loadByRequestPath($requestCases);
  184. /**
  185. * Stupid dirty hack
  186. * @author igorko
  187. */
  188. switch (Mage::app()->getStore($this->getStoreId())->getCode()) {
  189. case 'ru': $from_store = 'ua'; break;
  190. case 'ua': $from_store = 'ru'; break;
  191. }
  192. /**
  193. * Try to find rewrite by request path at first, if no luck - try to find by id_path
  194. */
  195. if (!$this->getId() && isset($from_store)) {
  196. try {
  197. $fromStoreId = Mage::app()->getStore($from_store)->getId();
  198. }
  199. catch (Exception $e) {
  200. return false;
  201. }
  202. $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
  203. if (!$this->getId()) {
  204. return false;
  205. }
  206. $this->setStoreId(Mage::app()->getStore()->getId())->loadByIdPath($this->getIdPath());
  207. }
  208. if (!$this->getId()) {
  209. return false;
  210. }
  211. $request->setAlias(self::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
  212. $external = substr($this->getTargetPath(), 0, 6);
  213. $isPermanentRedirectOption = $this->hasOption('RP');
  214. if ($external === 'http:/' || $external === 'https:') {
  215. if ($isPermanentRedirectOption) {
  216. header('HTTP/1.1 301 Moved Permanently');
  217. }
  218. header("Location: ".$this->getTargetPath());
  219. exit;
  220. } else {
  221. $targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
  222. }
  223. $isRedirectOption = $this->hasOption('R');
  224. if ($isRedirectOption || $isPermanentRedirectOption) {
  225. if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
  226. $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
  227. }
  228. if ($isPermanentRedirectOption) {
  229. header('HTTP/1.1 301 Moved Permanently');
  230. }
  231. header('Location: '.$targetUrl);
  232. exit;
  233. }
  234. if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
  235. $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
  236. }
  237. if ($queryString = $this->_getQueryString()) {
  238. $targetUrl .= '?'.$queryString;
  239. }
  240. $request->setRequestUri($targetUrl);
  241. $request->setPathInfo($this->getTargetPath());
  242. return true;
  243. }
  244. protected function _getQueryString()
  245. {
  246. if (!empty($_SERVER['QUERY_STRING'])) {
  247. $queryParams = array();
  248. parse_str($_SERVER['QUERY_STRING'], $queryParams);
  249. $hasChanges = false;
  250. foreach ($queryParams as $key=>$value) {
  251. if (substr($key, 0, 3) === '___') {
  252. unset($queryParams[$key]);
  253. $hasChanges = true;
  254. }
  255. }
  256. if ($hasChanges) {
  257. return http_build_query($queryParams);
  258. }
  259. else {
  260. return $_SERVER['QUERY_STRING'];
  261. }
  262. }
  263. return false;
  264. }
  265. public function getStoreId()
  266. {
  267. return $this->_getData('store_id');
  268. }
  269. }