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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 class Mage_Core_Model_Url extends Varien_Object
00080 {
00081 const DEFAULT_CONTROLLER_NAME = 'index';
00082 const DEFAULT_ACTION_NAME = 'index';
00083
00084 const XML_PATH_UNSECURE_URL = 'web/unsecure/base_url';
00085 const XML_PATH_SECURE_URL = 'web/secure/base_url';
00086
00087 const XML_PATH_SECURE_IN_ADMIN = 'web/secure/use_in_adminhtml';
00088 const XML_PATH_SECURE_IN_FRONT = 'web/secure/use_in_frontend';
00089
00090 static protected $_configDataCache;
00091 static protected $_encryptedSessionId;
00092
00093
00094
00095
00096
00097
00098 protected $_reservedRouteParams = array(
00099 '_store', '_type', '_secure', '_forced_secure', '_use_rewrite', '_nosid',
00100 '_absolute', '_current', '_direct', '_fragment', '_escape', '_query',
00101 '_store_to_url'
00102 );
00103
00104
00105
00106
00107
00108
00109 protected $_request;
00110
00111
00112
00113
00114
00115
00116 protected $_useSession;
00117
00118 protected function _construct()
00119 {
00120 $this->setStore(null);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 public function parseUrl($url)
00130 {
00131 $data = parse_url($url);
00132 $parts = array(
00133 'scheme'=>'setScheme',
00134 'host' =>'setHost',
00135 'port' =>'setPort',
00136 'user' =>'setUser',
00137 'pass' =>'setPassword',
00138 'path' =>'setPath',
00139 'query' =>'setQuery',
00140 'fragment'=>'setFragment');
00141
00142 foreach ($parts as $component=>$method) {
00143 if (isset($data[$component])) {
00144 $this->$method($data[$component]);
00145 }
00146 }
00147 return $this;
00148 }
00149
00150
00151
00152
00153
00154
00155 public function getDefaultControllerName()
00156 {
00157 return self::DEFAULT_CONTROLLER_NAME;
00158 }
00159
00160 public function setUseUrlCache($flag)
00161 {
00162 $this->setData('use_url_cache', $flag);
00163 return $this;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 public function setUseSession($useSession)
00173 {
00174 $this->_useSession = (bool)$useSession;
00175 return $this;
00176 }
00177
00178 public function setRouteFrontName($name)
00179 {
00180 $this->setData('route_front_name', $name);
00181 return $this;
00182 }
00183
00184
00185
00186
00187
00188
00189 public function getUseSession()
00190 {
00191 if (is_null($this->_useSession)) {
00192 $this->_useSession = Mage::app()->getUseSessionInUrl();
00193 }
00194 return $this->_useSession;
00195 }
00196
00197
00198
00199
00200
00201
00202 public function getDefaultActionName()
00203 {
00204 return self::DEFAULT_ACTION_NAME;
00205 }
00206
00207 public function getConfigData($key, $prefix=null)
00208 {
00209 if (is_null($prefix)) {
00210 $prefix = 'web/'.($this->getSecure() ? 'secure' : 'unsecure').'/';
00211 }
00212 $path = $prefix.$key;
00213
00214 $cacheId = $this->getStore()->getCode().'/'.$path;
00215 if (!isset(self::$_configDataCache[$cacheId])) {
00216 $data = $this->getStore()->getConfig($path);
00217 self::$_configDataCache[$cacheId] = $data;
00218 }
00219
00220 return self::$_configDataCache[$cacheId];
00221 }
00222
00223 public function setRequest(Zend_Controller_Request_Http $request)
00224 {
00225 $this->_request = $request;
00226 return $this;
00227 }
00228
00229
00230
00231
00232
00233
00234 public function getRequest()
00235 {
00236 if (!$this->_request) {
00237 $this->_request = Mage::app()->getRequest();
00238 }
00239 return $this->_request;
00240 }
00241
00242 public function getType()
00243 {
00244 if (!$this->hasData('type')) {
00245 $this->setData('type', Mage_Core_Model_Store::URL_TYPE_LINK);
00246 }
00247 return $this->_getData('type');
00248 }
00249
00250
00251
00252
00253
00254
00255 public function getSecure()
00256 {
00257 if ($this->hasData('secure_is_forced')) {
00258 return $this->getData('secure');
00259 }
00260
00261 $store = $this->getStore();
00262
00263 if ($store->isAdmin() && !$store->isAdminUrlSecure()) {
00264 return false;
00265 }
00266 if (!$store->isAdmin() && !$store->isFrontUrlSecure()) {
00267 return false;
00268 }
00269
00270 if (!$this->hasData('secure')) {
00271 if ($this->getType() == Mage_Core_Model_Store::URL_TYPE_LINK) {
00272 $pathSecure = Mage::getConfig()->shouldUrlBeSecure('/'.$this->getActionPath());
00273 $this->setData('secure', $pathSecure);
00274 } else {
00275 $this->setData('secure', $store->isCurrentlySecure());
00276 }
00277 }
00278 return $this->getData('secure');
00279 }
00280
00281 public function setStore($data)
00282 {
00283 $this->setData('store', Mage::app()->getStore($data));
00284 return $this;
00285 }
00286
00287
00288
00289
00290
00291
00292 public function getStore()
00293 {
00294 if (!$this->hasData('store')) {
00295 $this->setStore(null);
00296 }
00297 return $this->_getData('store');
00298 }
00299
00300
00301
00302
00303
00304
00305
00306 public function getBaseUrl($params = array())
00307 {
00308 if (isset($params['_store'])) {
00309 $this->setStore($params['_store']);
00310 }
00311 if (isset($params['_type'])) {
00312 $this->setType($params['_type']);
00313 }
00314 if (isset($params['_secure'])) {
00315 $this->setSecure($params['_secure']);
00316 }
00317
00318
00319
00320
00321 if ($this->getType() == Mage_Core_Model_Store::URL_TYPE_LINK
00322 && Mage::app()->getRequest()->isDirectAccessFrontendName($this->getRouteFrontName())) {
00323 $this->setType(Mage_Core_Model_Store::URL_TYPE_DIRECT_LINK);
00324 }
00325
00326 return $this->getStore()->getBaseUrl($this->getType(), $this->getSecure());
00327 }
00328
00329
00330
00331
00332
00333
00334
00335 public function setRoutePath($data)
00336 {
00337 if ($this->_getData('route_path')==$data) {
00338 return $this;
00339 }
00340
00341 $a = explode('/', $data);
00342
00343 $route = array_shift($a);
00344 if ('*'===$route) {
00345 $route = $this->getRequest()->getRouteName();
00346 }
00347 $this->setRouteName($route);
00348 $routePath = $route.'/';
00349
00350 if (!empty($a)) {
00351 $controller = array_shift($a);
00352 if ('*'===$controller) {
00353 $controller = $this->getRequest()->getControllerName();
00354 }
00355 $this->setControllerName($controller);
00356 $routePath .= $controller.'/';
00357 }
00358
00359 if (!empty($a)) {
00360 $action = array_shift($a);
00361 if ('*'===$action) {
00362 $action = $this->getRequest()->getActionName();
00363 }
00364 $this->setActionName($action);
00365 $routePath .= $action.'/';
00366 }
00367
00368 if (!empty($a)) {
00369 $this->unsetData('route_params');
00370 while (!empty($a)) {
00371 $key = array_shift($a);
00372 if (!empty($a)) {
00373 $value = array_shift($a);
00374 $this->setRouteParam($key, $value);
00375 #$routePath .= $key.'/'.urlencode($value).'/';
00376 $routePath .= $key.'/'.$value.'/';
00377 }
00378 }
00379 }
00380
00381 #$this->setData('route_path', $routePath);
00382
00383 return $this;
00384 }
00385
00386 public function getActionPath()
00387 {
00388 if (!$this->getRouteName()) {
00389 return '';
00390 }
00391
00392 $hasParams = (bool)$this->getRouteParams();
00393 $path = $this->getRouteFrontName() . '/';
00394
00395 if ($this->getControllerName()) {
00396 $path .= $this->getControllerName() . '/';
00397 } elseif ($hasParams) {
00398 $path .= $this->getDefaultControllerName() . '/';
00399 }
00400 if ($this->getActionName()) {
00401 $path .= $this->getActionName() . '/';
00402 } elseif ($hasParams) {
00403 $path .= $this->getDefaultActionName() . '/';
00404 }
00405
00406 return $path;
00407 }
00408
00409 public function getRoutePath($routeParams=array())
00410 {
00411 if (!$this->hasData('route_path')) {
00412 $routePath = $this->getRequest()->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS);
00413 if (!empty($routeParams['_use_rewrite'])
00414 && ($routePath !== null)) {
00415 $this->setData('route_path', $routePath);
00416 return $routePath;
00417 }
00418 $routePath = $this->getActionPath();
00419 if ($this->getRouteParams()) {
00420 foreach ($this->getRouteParams() as $key=>$value) {
00421 if (is_null($value) || false===$value || ''===$value || !is_scalar($value)) {
00422 continue;
00423 }
00424 $routePath .= $key.'/'.$value.'/';
00425 }
00426 }
00427 if ($routePath != '' && substr($routePath, -1, 1) !== '/') {
00428 $routePath.= '/';
00429 }
00430 $this->setData('route_path', $routePath);
00431 }
00432 return $this->_getData('route_path');
00433 }
00434
00435 public function setRouteName($data)
00436 {
00437 if ($this->_getData('route_name')==$data) {
00438 return $this;
00439 }
00440 $this->unsetData('route_front_name')
00441 ->unsetData('route_path')
00442 ->unsetData('controller_name')
00443 ->unsetData('action_name')
00444 ->unsetData('secure');
00445 return $this->setData('route_name', $data);
00446 }
00447
00448 public function getRouteFrontName()
00449 {
00450 if (!$this->hasData('route_front_name')) {
00451 $routeName = $this->getRouteName();
00452 $route = Mage::app()->getFrontController()->getRouterByRoute($routeName);
00453 $frontName = $route->getFrontNameByRoute($routeName);
00454
00455 $this->setRouteFrontName($frontName);
00456 }
00457
00458 return $this->_getData('route_front_name');
00459 }
00460
00461 public function getRouteName()
00462 {
00463 return $this->_getData('route_name');
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473 public function setControllerName($data)
00474 {
00475 if ($this->_getData('controller_name')==$data) {
00476 return $this;
00477 }
00478 $this->unsetData('route_path')->unsetData('action_name')->unsetData('secure');
00479 return $this->setData('controller_name', $data);
00480 }
00481
00482 public function getControllerName()
00483 {
00484 return $this->_getData('controller_name');
00485 }
00486
00487
00488
00489
00490
00491
00492
00493
00494 public function setActionName($data)
00495 {
00496 if ($this->_getData('action_name') == $data) {
00497 return $this;
00498 }
00499 $this->unsetData('route_path');
00500 return $this->setData('action_name', $data)->unsetData('secure');
00501 }
00502
00503 public function getActionName()
00504 {
00505 return $this->_getData('action_name');
00506 }
00507
00508 public function setRouteParams(array $data, $unsetOldParams=true)
00509 {
00510 if (isset($data['_type'])) {
00511 $this->setType($data['_type']);
00512 unset($data['_type']);
00513 }
00514
00515 if (isset($data['_store'])) {
00516 $this->setStore($data['_store']);
00517 unset($data['_store']);
00518 }
00519
00520 if (isset($data['_forced_secure'])) {
00521 $this->setSecure((bool)$data['_forced_secure']);
00522 $this->setSecureIsForced(true);
00523 unset($data['_forced_secure']);
00524 } else {
00525 if (isset($data['_secure'])) {
00526 $this->setSecure((bool)$data['_secure']);
00527 unset($data['_secure']);
00528 }
00529 }
00530
00531 if (isset($data['_absolute'])) {
00532 unset($data['_absolute']);
00533 }
00534
00535 if ($unsetOldParams) {
00536 $this->unsetData('route_params');
00537 }
00538
00539 $this->setUseUrlCache(true);
00540 if (isset($data['_current'])) {
00541 if (is_array($data['_current'])) {
00542 foreach ($data['_current'] as $key) {
00543 if (array_key_exists($key, $data) || !$this->getRequest()->getUserParam($key)) {
00544 continue;
00545 }
00546 $data[$key] = $this->getRequest()->getUserParam($key);
00547 }
00548 } elseif ($data['_current']) {
00549 foreach ($this->getRequest()->getUserParams() as $key=>$value) {
00550 if (array_key_exists($key, $data) || $this->getRouteParam($key)) {
00551 continue;
00552 }
00553 $data[$key] = $value;
00554 }
00555 foreach ($this->getRequest()->getQuery() as $key=>$value) {
00556 $this->setQueryParam($key, $value);
00557 }
00558 $this->setUseUrlCache(false);
00559 }
00560 unset($data['_current']);
00561 }
00562
00563 if (isset($data['_use_rewrite'])) {
00564 unset($data['_use_rewrite']);
00565 }
00566
00567 if (isset($data['_store_to_url']) && (bool)$data['_store_to_url'] === true) {
00568 if (!Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getStore())
00569 && !Mage::app()->isSingleStoreMode()
00570 ) {
00571 $this->setQueryParam('___store', $this->getStore()->getCode());
00572 }
00573 unset($data['_store_to_url']);
00574 }
00575
00576 foreach ($data as $k=>$v) {
00577 $this->setRouteParam($k, $v);
00578 }
00579
00580 return $this;
00581 }
00582
00583 public function getRouteParams()
00584 {
00585 return $this->_getData('route_params');
00586 }
00587
00588 public function setRouteParam($key, $data)
00589 {
00590 $params = $this->_getData('route_params');
00591 if (isset($params[$key]) && $params[$key]==$data) {
00592 return $this;
00593 }
00594 $params[$key] = $data;
00595 $this->unsetData('route_path');
00596 return $this->setData('route_params', $params);
00597 }
00598
00599 public function getRouteParam($key)
00600 {
00601 return $this->_getData('route_params', $key);
00602 }
00603
00604 public function getRouteUrl($routePath=null, $routeParams=null)
00605 {
00606 $this->unsetData('route_params');
00607
00608 if (isset($routeParams['_direct'])) {
00609 return $this->getBaseUrl().$routeParams['_direct'];
00610 }
00611
00612 if (!is_null($routePath)) {
00613 $this->setRoutePath($routePath);
00614 }
00615 if (is_array($routeParams)) {
00616 $this->setRouteParams($routeParams, false);
00617 }
00618
00619 $url = $this->getBaseUrl().$this->getRoutePath($routeParams);
00620 return $url;
00621 }
00622
00623
00624
00625
00626
00627
00628 public function checkCookieDomains()
00629 {
00630 $hostArr = explode(':', $this->getRequest()->getServer('HTTP_HOST'));
00631 if ($hostArr[0]!==$this->getHost()) {
00632 $session = Mage::getSingleton('core/session');
00633 if (!$session->isValidForHost($this->getHost())) {
00634 if (!self::$_encryptedSessionId) {
00635 $helper = Mage::helper('core');
00636 if (!$helper) {
00637 return $this;
00638 }
00639 self::$_encryptedSessionId = $session->getEncryptedSessionId();
00640 }
00641 $this->setQueryParam(
00642 $session->getSessionIdQueryParam(),
00643 self::$_encryptedSessionId
00644 );
00645 }
00646 }
00647 return $this;
00648 }
00649
00650 public function addSessionParam()
00651 {
00652 $session = Mage::getSingleton('core/session');
00653
00654 if (!self::$_encryptedSessionId) {
00655 $helper = Mage::helper('core');
00656 if (!$helper) {
00657 return $this;
00658 }
00659 self::$_encryptedSessionId = $session->getEncryptedSessionId();
00660 }
00661 $this->setQueryParam(
00662 $session->getSessionIdQueryParam(),
00663 self::$_encryptedSessionId
00664 );
00665 return $this;
00666 }
00667
00668
00669
00670
00671
00672
00673
00674 public function setQuery($data)
00675 {
00676 if ($this->_getData('query') == $data) {
00677 return $this;
00678 }
00679 $this->unsetData('query_params');
00680 return $this->setData('query', $data);
00681 }
00682
00683 public function getQuery($escape = false)
00684 {
00685 if (!$this->hasData('query')) {
00686 $query = '';
00687 if (is_array($this->getQueryParams())) {
00688 $query = http_build_query($this->getQueryParams(), '', $escape ? '&' : '&');
00689 }
00690 $this->setData('query', $query);
00691 }
00692 return $this->_getData('query');
00693 }
00694
00695 public function setQueryParams(array $data, $useCurrent = false)
00696 {
00697 $this->unsetData('query');
00698 if ($useCurrent) {
00699 $params = $this->_getData('query_params');
00700 foreach ($data as $param => $value) {
00701 $params[$param] = $value;
00702 }
00703 $this->setData('query_params', $params);
00704 return $this;
00705 }
00706
00707 if ($this->_getData('query_params')==$data) {
00708 return $this;
00709 }
00710 return $this->setData('query_params', $data);
00711 }
00712
00713 public function getQueryParams()
00714 {
00715 if (!$this->hasData('query_params')) {
00716 $params = array();
00717 if ($this->_getData('query')) {
00718 foreach (explode('&', $this->_getData('query')) as $param) {
00719 $paramArr = explode('=', $param);
00720 $params[$paramArr[0]] = urldecode($paramArr[1]);
00721 }
00722 }
00723 $this->setData('query_params', $params);
00724 }
00725 return $this->_getData('query_params');
00726 }
00727
00728 public function setQueryParam($key, $data)
00729 {
00730 $params = $this->getQueryParams();
00731 if (isset($params[$key]) && $params[$key]==$data) {
00732 return $this;
00733 }
00734 $params[$key] = $data;
00735 $this->unsetData('query');
00736 return $this->setData('query_params', $params);
00737 }
00738
00739 public function getQueryParam($key)
00740 {
00741 if (!$this->hasData('query_params')) {
00742 $this->getQueryParams();
00743 }
00744 return $this->_getData('query_params', $key);
00745 }
00746
00747
00748
00749
00750
00751
00752
00753 public function setFragment($data)
00754 {
00755 return $this->setData('fragment', $data);
00756 }
00757
00758 public function getFragment()
00759 {
00760 return $this->_getData('fragment');
00761 }
00762
00763
00764
00765
00766
00767
00768
00769
00770 public function getUrl($routePath=null, $routeParams=null)
00771 {
00772 $escapeQuery = false;
00773
00774
00775
00776
00777
00778
00779 if (isset($routeParams['_fragment'])) {
00780 $this->setFragment($routeParams['_fragment']);
00781 unset($routeParams['_fragment']);
00782 }
00783
00784 if (isset($routeParams['_escape'])) {
00785 $escapeQuery = $routeParams['_escape'];
00786 unset($routeParams['_escape']);
00787 }
00788
00789 $query = null;
00790 if (isset($routeParams['_query'])) {
00791 $query = $routeParams['_query'];
00792 unset($routeParams['_query']);
00793 }
00794
00795 $noSid = null;
00796 if (isset($routeParams['_nosid'])) {
00797 $noSid = (bool)$routeParams['_nosid'];
00798 unset($routeParams['_nosid']);
00799 }
00800 $url = $this->getRouteUrl($routePath, $routeParams);
00801
00802
00803
00804 if ($query !== null) {
00805 if (is_string($query)) {
00806 $this->setQuery($query);
00807 } elseif (is_array($query)) {
00808 $this->setQueryParams($query, !empty($routeParams['_current']));
00809 }
00810 if ($query === false) {
00811 $this->setQueryParams(array());
00812 }
00813 }
00814
00815 if ($noSid !== true) {
00816 $this->_prepareSessionUrl($url);
00817 }
00818
00819 if ($query = $this->getQuery($escapeQuery)) {
00820 $url .= '?'.$query;
00821 }
00822
00823 if ($this->getFragment()) {
00824 $url .= '#'.$this->getFragment();
00825 }
00826
00827 return $this->escape($url);
00828 }
00829
00830
00831
00832
00833
00834
00835
00836 protected function _prepareSessionUrl($url)
00837 {
00838 if (!$this->getUseSession()) {
00839 return $this;
00840 }
00841 $session = Mage::getSingleton('core/session');
00842
00843 if (Mage::app()->getUseSessionVar()) {
00844
00845 if ($this->getSecure()) {
00846 $this->setQueryParam('___SID', 'S');
00847 }
00848 else {
00849 $this->setQueryParam('___SID', 'U');
00850 }
00851 }
00852 else {
00853 if ($sessionId = $session->getSessionIdForHost($url)) {
00854 $this->setQueryParam($session->getSessionIdQueryParam(), $sessionId);
00855 }
00856 }
00857 return $this;
00858 }
00859
00860
00861
00862
00863
00864
00865
00866 public function escape($value)
00867 {
00868 $value = str_replace('"', '%22', $value);
00869 $value = str_replace("'", '%27', $value);
00870 $value = str_replace('>', '%3E', $value);
00871 $value = str_replace('<', '%3C', $value);
00872 return $value;
00873 }
00874
00875
00876
00877
00878
00879
00880
00881
00882 public function getDirectUrl($url, $params = array()) {
00883 $params['_direct'] = $url;
00884 return $this->getUrl('', $params);
00885 }
00886
00887
00888
00889
00890
00891
00892
00893 public function sessionUrlVar($html)
00894 {
00895 return preg_replace_callback('#(\?|&|&)___SID=([SU])(&|&)?#', array($this, "sessionVarCallback"), $html);
00896 }
00897
00898
00899
00900
00901
00902
00903
00904 public function useSessionIdForUrl($secure = false)
00905 {
00906 $key = 'use_session_id_for_url_' . (int)$secure;
00907 if (is_null($this->getData($key))) {
00908 $httpHost = Mage::app()->getFrontController()->getRequest()->getHttpHost();
00909 $urlHost = parse_url(Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, $secure), PHP_URL_HOST);
00910
00911 if ($httpHost != $urlHost) {
00912 $this->setData($key, true);
00913 }
00914 else {
00915 $this->setData($key, false);
00916 }
00917 }
00918 return $this->getData($key);
00919 }
00920
00921
00922
00923
00924
00925
00926
00927 public function sessionVarCallback($match)
00928 {
00929 if ($this->useSessionIdForUrl($match[2] == 'S' ? true : false)) {
00930 $session = Mage::getSingleton('core/session');
00931
00932 return $match[1]
00933 . $session->getSessionIdQueryParam()
00934 . '=' . $session->getEncryptedSessionId()
00935 . (isset($match[3]) ? $match[3] : '');
00936 }
00937 else {
00938 if ($match[1] == '?' && isset($match[3])) {
00939 return '?';
00940 }
00941 elseif ($match[1] == '?' && !isset($match[3])) {
00942 return '';
00943 }
00944 elseif (($match[1] == '&' || $match[1] == '&') && !isset($match[3])) {
00945 return '';
00946 }
00947 elseif (($match[1] == '&' || $match[1] == '&') && isset($match[3])) {
00948 return $match[3];
00949 }
00950 }
00951 return '';
00952 }
00953 }