00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class Mage_Core_Model_Url_Rewrite extends Mage_Core_Model_Abstract
00037 {
00038 const TYPE_CATEGORY = 1;
00039 const TYPE_PRODUCT = 2;
00040 const TYPE_CUSTOM = 3;
00041 const REWRITE_REQUEST_PATH_ALIAS = 'rewrite_request_path';
00042
00043 protected function _construct()
00044 {
00045 $this->_init('core/url_rewrite');
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 public function loadByRequestPath($path)
00057 {
00058 $this->setId(null);
00059
00060 if (is_array($path)) {
00061 foreach ($path as $pathInfo) {
00062 $this->load($pathInfo, 'request_path');
00063 if ($this->getId()) {
00064 return $this;
00065 }
00066 }
00067 }
00068 else {
00069 $this->load($path, 'request_path');
00070 }
00071 return $this;
00072 }
00073
00074 public function loadByIdPath($path)
00075 {
00076 $this->setId(null)->load($path, 'id_path');
00077 return $this;
00078 }
00079
00080 public function loadByTags($tags)
00081 {
00082 $this->setId(null);
00083
00084 $loadTags = is_array($tags) ? $tags : explode(',', $tags);
00085
00086 $search = $this->getResourceCollection();
00087 foreach ($loadTags as $k=>$t) {
00088 if (!is_numeric($k)) {
00089 $t = $k.'='.$t;
00090 }
00091 $search->addTagsFilter($t);
00092 }
00093 if (!is_null($this->getStoreId())) {
00094 $search->addStoreFilter($this->getStoreId());
00095 }
00096
00097 $search->setPageSize(1)->load();
00098
00099 if ($search->getSize()>0) {
00100 foreach ($search as $rewrite) {
00101 $this->setData($rewrite->getData());
00102 }
00103 }
00104
00105 return $this;
00106 }
00107
00108 public function hasOption($key)
00109 {
00110 $optArr = explode(',', $this->getOptions());
00111
00112 return array_search($key, $optArr) !== false;
00113 }
00114
00115 public function addTag($tags)
00116 {
00117 $curTags = $this->getTags();
00118
00119 $addTags = is_array($tags) ? $tags : explode(',', $tags);
00120
00121 foreach ($addTags as $k=>$t) {
00122 if (!is_numeric($k)) {
00123 $t = $k.'='.$t;
00124 }
00125 if (!in_array($t, $curTags)) {
00126 $curTags[] = $t;
00127 }
00128 }
00129
00130 $this->setTags($curTags);
00131
00132 return $this;
00133 }
00134
00135 public function removeTag($tags)
00136 {
00137 $curTags = $this->getTags();
00138
00139 $removeTags = is_array($tags) ? $tags : explode(',', $tags);
00140
00141 foreach ($removeTags as $t) {
00142 if (!is_numeric($k)) {
00143 $t = $k.'='.$t;
00144 }
00145 if ($key = array_search($t, $curTags)) {
00146 unset($curTags[$key]);
00147 }
00148 }
00149
00150 $this->setTags(',', $curTags);
00151
00152 return $this;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162 public function rewrite(Zend_Controller_Request_Http $request=null, Zend_Controller_Response_Http $response=null)
00163 {
00164 if (!Mage::isInstalled()) {
00165 return false;
00166 }
00167 if (is_null($request)) {
00168 $request = Mage::app()->getFrontController()->getRequest();
00169 }
00170 if (is_null($response)) {
00171 $response = Mage::app()->getFrontController()->getResponse();
00172 }
00173 if (is_null($this->getStoreId()) || false===$this->getStoreId()) {
00174 $this->setStoreId(Mage::app()->getStore()->getId());
00175 }
00176
00177 $requestCases = array();
00178 $requestPath = trim($request->getPathInfo(), '/');
00179
00180
00181
00182
00183
00184 if ($queryString = $this->_getQueryString()) {
00185 $requestCases[] = $requestPath .'?'.$queryString;
00186 $requestCases[] = $requestPath;
00187 }
00188 else {
00189 $requestCases[] = $requestPath;
00190 }
00191
00192 $this->loadByRequestPath($requestCases);
00193
00194
00195
00196
00197 if (!$this->getId() && isset($_GET['___from_store'])) {
00198 try {
00199 $fromStoreId = Mage::app()->getStore($_GET['___from_store']);
00200 }
00201 catch (Exception $e) {
00202 return false;
00203 }
00204
00205 $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
00206 if (!$this->getId()) {
00207 return false;
00208 }
00209 $this->setStoreId(Mage::app()->getStore()->getId())->loadByIdPath($this->getIdPath());
00210 }
00211
00212 if (!$this->getId()) {
00213 return false;
00214 }
00215
00216
00217 $request->setAlias(self::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
00218 $external = substr($this->getTargetPath(), 0, 6);
00219 $isPermanentRedirectOption = $this->hasOption('RP');
00220 if ($external === 'http:/' || $external === 'https:') {
00221 if ($isPermanentRedirectOption) {
00222 header('HTTP/1.1 301 Moved Permanently');
00223 }
00224 header("Location: ".$this->getTargetPath());
00225 exit;
00226 } else {
00227 $targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
00228 }
00229 $isRedirectOption = $this->hasOption('R');
00230 if ($isRedirectOption || $isPermanentRedirectOption) {
00231 if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
00232 $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
00233 }
00234 if ($isPermanentRedirectOption) {
00235 header('HTTP/1.1 301 Moved Permanently');
00236 }
00237 header('Location: '.$targetUrl);
00238 exit;
00239 }
00240
00241 if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
00242 $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
00243 }
00244
00245 if ($queryString = $this->_getQueryString()) {
00246 $targetUrl .= '?'.$queryString;
00247 }
00248
00249 $request->setRequestUri($targetUrl);
00250 $request->setPathInfo($this->getTargetPath());
00251
00252 return true;
00253 }
00254
00255 protected function _getQueryString()
00256 {
00257 if (!empty($_SERVER['QUERY_STRING'])) {
00258 $queryParams = array();
00259 parse_str($_SERVER['QUERY_STRING'], $queryParams);
00260 $hasChanges = false;
00261 foreach ($queryParams as $key=>$value) {
00262 if (substr($key, 0, 3) === '___') {
00263 unset($queryParams[$key]);
00264 $hasChanges = true;
00265 }
00266 }
00267 if ($hasChanges) {
00268 return http_build_query($queryParams);
00269 }
00270 else {
00271 return $_SERVER['QUERY_STRING'];
00272 }
00273 }
00274 return false;
00275 }
00276
00277 public function getStoreId()
00278 {
00279 return $this->_getData('store_id');
00280 }
00281
00282 }