1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54:
55: class Mage_Core_Model_Url_Rewrite extends Mage_Core_Model_Abstract
56: {
57: const TYPE_CATEGORY = 1;
58: const TYPE_PRODUCT = 2;
59: const TYPE_CUSTOM = 3;
60: const REWRITE_REQUEST_PATH_ALIAS = 'rewrite_request_path';
61:
62: 63: 64: 65: 66:
67: protected $_cacheTag = false;
68:
69: protected function _construct()
70: {
71: $this->_init('core/url_rewrite');
72: }
73:
74: 75: 76: 77: 78:
79: protected function _afterSave()
80: {
81: if ($this->hasCategoryId()) {
82: $this->_cacheTag = array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG);
83: }
84:
85: parent::_afterSave();
86:
87: return $this;
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: public function loadByRequestPath($path)
98: {
99: $this->setId(null);
100: $this->_getResource()->loadByRequestPath($this, $path);
101: $this->_afterLoad();
102: $this->setOrigData();
103: $this->_hasDataChanges = false;
104: return $this;
105: }
106:
107: public function loadByIdPath($path)
108: {
109: $this->setId(null)->load($path, 'id_path');
110: return $this;
111: }
112:
113: public function loadByTags($tags)
114: {
115: $this->setId(null);
116:
117: $loadTags = is_array($tags) ? $tags : explode(',', $tags);
118:
119: $search = $this->getResourceCollection();
120: foreach ($loadTags as $k=>$t) {
121: if (!is_numeric($k)) {
122: $t = $k.'='.$t;
123: }
124: $search->addTagsFilter($t);
125: }
126: if (!is_null($this->getStoreId())) {
127: $search->addStoreFilter($this->getStoreId());
128: }
129:
130: $search->setPageSize(1)->load();
131:
132: if ($search->getSize()>0) {
133: foreach ($search as $rewrite) {
134: $this->setData($rewrite->getData());
135: }
136: }
137:
138: return $this;
139: }
140:
141: public function hasOption($key)
142: {
143: $optArr = explode(',', $this->getOptions());
144:
145: return array_search($key, $optArr) !== false;
146: }
147:
148: public function addTag($tags)
149: {
150: $curTags = $this->getTags();
151:
152: $addTags = is_array($tags) ? $tags : explode(',', $tags);
153:
154: foreach ($addTags as $k=>$t) {
155: if (!is_numeric($k)) {
156: $t = $k.'='.$t;
157: }
158: if (!in_array($t, $curTags)) {
159: $curTags[] = $t;
160: }
161: }
162:
163: $this->setTags($curTags);
164:
165: return $this;
166: }
167:
168: public function removeTag($tags)
169: {
170: $curTags = $this->getTags();
171:
172: $removeTags = is_array($tags) ? $tags : explode(',', $tags);
173:
174: foreach ($removeTags as $t) {
175: if (!is_numeric($k)) {
176: $t = $k.'='.$t;
177: }
178: if ($key = array_search($t, $curTags)) {
179: unset($curTags[$key]);
180: }
181: }
182:
183: $this->setTags(',', $curTags);
184:
185: return $this;
186: }
187:
188: 189: 190: 191: 192: 193: 194:
195: public function rewrite(Zend_Controller_Request_Http $request=null, Zend_Controller_Response_Http $response=null)
196: {
197: if (!Mage::isInstalled()) {
198: return false;
199: }
200: if (is_null($request)) {
201: $request = Mage::app()->getFrontController()->getRequest();
202: }
203: if (is_null($response)) {
204: $response = Mage::app()->getFrontController()->getResponse();
205: }
206: if (is_null($this->getStoreId()) || false===$this->getStoreId()) {
207: $this->setStoreId(Mage::app()->getStore()->getId());
208: }
209:
210: 211: 212: 213: 214:
215: $requestCases = array();
216: $pathInfo = $request->getPathInfo();
217: $origSlash = (substr($pathInfo, -1) == '/') ? '/' : '';
218: $requestPath = trim($pathInfo, '/');
219:
220:
221: $altSlash = $origSlash ? '' : '/';
222:
223: $queryString = $this->_getQueryString();
224: if ($queryString) {
225: $requestCases[] = $requestPath . $origSlash . '?' . $queryString;
226: $requestCases[] = $requestPath . $altSlash . '?' . $queryString;
227: }
228: $requestCases[] = $requestPath . $origSlash;
229: $requestCases[] = $requestPath . $altSlash;
230:
231: $this->loadByRequestPath($requestCases);
232:
233: 234: 235:
236: if (!$this->getId() && isset($_GET['___from_store'])) {
237: try {
238: $fromStoreId = Mage::app()->getStore($_GET['___from_store'])->getId();
239: }
240: catch (Exception $e) {
241: return false;
242: }
243:
244: $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
245: if (!$this->getId()) {
246: return false;
247: }
248: $currentStore = Mage::app()->getStore();
249: $this->setStoreId($currentStore->getId())->loadByIdPath($this->getIdPath());
250:
251: Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $currentStore->getCode(), true);
252: $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
253:
254: $this->_sendRedirectHeaders($targetUrl, true);
255: }
256:
257: if (!$this->getId()) {
258: return false;
259: }
260:
261:
262: $request->setAlias(self::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
263: $external = substr($this->getTargetPath(), 0, 6);
264: $isPermanentRedirectOption = $this->hasOption('RP');
265: if ($external === 'http:/' || $external === 'https:') {
266: $destinationStoreCode = Mage::app()->getStore($this->getStoreId())->getCode();
267: Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $destinationStoreCode, true);
268:
269: $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption);
270: } else {
271: $targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
272: }
273: $isRedirectOption = $this->hasOption('R');
274: if ($isRedirectOption || $isPermanentRedirectOption) {
275: if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
276: $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
277: }
278:
279: $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
280: }
281:
282: if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
283: $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
284: }
285:
286: $queryString = $this->_getQueryString();
287: if ($queryString) {
288: $targetUrl .= '?'.$queryString;
289: }
290:
291: $request->setRequestUri($targetUrl);
292: $request->setPathInfo($this->getTargetPath());
293:
294: return true;
295: }
296:
297: protected function _getQueryString()
298: {
299: if (!empty($_SERVER['QUERY_STRING'])) {
300: $queryParams = array();
301: parse_str($_SERVER['QUERY_STRING'], $queryParams);
302: $hasChanges = false;
303: foreach ($queryParams as $key=>$value) {
304: if (substr($key, 0, 3) === '___') {
305: unset($queryParams[$key]);
306: $hasChanges = true;
307: }
308: }
309: if ($hasChanges) {
310: return http_build_query($queryParams);
311: }
312: else {
313: return $_SERVER['QUERY_STRING'];
314: }
315: }
316: return false;
317: }
318:
319: public function getStoreId()
320: {
321: return $this->_getData('store_id');
322: }
323:
324: 325: 326: 327: 328: 329:
330: protected function ($url, $isPermanent = false)
331: {
332: if ($isPermanent) {
333: header('HTTP/1.1 301 Moved Permanently');
334: }
335:
336: header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
337: header('Pragma: no-cache');
338: header('Location: ' . $url);
339: exit;
340: }
341: }
342: