PageRenderTime 174ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 1008 lines | 658 code | 100 blank | 250 comment | 131 complexity | 0bdc1343241a9c5ae1c446d3988a27e5 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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
  28. *
  29. * Properties:
  30. *
  31. * - request
  32. *
  33. * - relative_url: true, false
  34. * - type: 'link', 'skin', 'js', 'media'
  35. * - store: instanceof Mage_Core_Model_Store
  36. * - secure: true, false
  37. *
  38. * - scheme: 'http', 'https'
  39. * - user: 'user'
  40. * - password: 'password'
  41. * - host: 'localhost'
  42. * - port: 80, 443
  43. * - base_path: '/dev/magento/'
  44. * - base_script: 'index.php'
  45. *
  46. * - storeview_path: 'storeview/'
  47. * - route_path: 'module/controller/action/param1/value1/param2/value2'
  48. * - route_name: 'module'
  49. * - controller_name: 'controller'
  50. * - action_name: 'action'
  51. * - route_params: array('param1'=>'value1', 'param2'=>'value2')
  52. *
  53. * - query: (?)'param1=value1&param2=value2'
  54. * - query_array: array('param1'=>'value1', 'param2'=>'value2')
  55. * - fragment: (#)'fragment-anchor'
  56. *
  57. * URL structure:
  58. *
  59. * https://user:password@host:443/base_path/[base_script][storeview_path]route_name/controller_name/action_name/param1/value1?query_param=query_value#fragment
  60. * \__________A___________/\____________________________________B_____________________________________/
  61. * \__________________C___________________/ \__________________D_________________/ \_____E_____/
  62. * \_____________F______________/ \___________________________G______________________/
  63. * \___________________________________________________H____________________________________________________/
  64. *
  65. * - A: authority
  66. * - B: path
  67. * - C: absolute_base_url
  68. * - D: action_path
  69. * - E: route_params
  70. * - F: host_url
  71. * - G: route_path
  72. * - H: route_url
  73. *
  74. * @category Mage
  75. * @package Mage_Core
  76. * @author Magento Core Team <core@magentocommerce.com>
  77. */
  78. class Mage_Core_Model_Url extends Varien_Object
  79. {
  80. const DEFAULT_CONTROLLER_NAME = 'index';
  81. const DEFAULT_ACTION_NAME = 'index';
  82. const XML_PATH_UNSECURE_URL = 'web/unsecure/base_url';
  83. const XML_PATH_SECURE_URL = 'web/secure/base_url';
  84. const XML_PATH_SECURE_IN_ADMIN = 'web/secure/use_in_adminhtml';
  85. const XML_PATH_SECURE_IN_FRONT = 'web/secure/use_in_frontend';
  86. static protected $_configDataCache;
  87. static protected $_encryptedSessionId;
  88. /**
  89. * Reserved Route parametr keys
  90. *
  91. * @var array
  92. */
  93. protected $_reservedRouteParams = array(
  94. '_store', '_type', '_secure', '_forced_secure', '_use_rewrite', '_nosid',
  95. '_absolute', '_current', '_direct', '_fragment', '_escape', '_query',
  96. '_store_to_url'
  97. );
  98. /**
  99. * Controller request object
  100. *
  101. * @var Zend_Controller_Request_Http
  102. */
  103. protected $_request;
  104. /**
  105. * Use Session ID for generate URL
  106. *
  107. * @var bool
  108. */
  109. protected $_useSession;
  110. protected function _construct()
  111. {
  112. $this->setStore(null);
  113. }
  114. /**
  115. * Initialize object data from retrieved url
  116. *
  117. * @param string $url
  118. * @return Mage_Core_Model_Url
  119. */
  120. public function parseUrl($url)
  121. {
  122. $data = parse_url($url);
  123. $parts = array(
  124. 'scheme'=>'setScheme',
  125. 'host' =>'setHost',
  126. 'port' =>'setPort',
  127. 'user' =>'setUser',
  128. 'pass' =>'setPassword',
  129. 'path' =>'setPath',
  130. 'query' =>'setQuery',
  131. 'fragment'=>'setFragment');
  132. foreach ($parts as $component=>$method) {
  133. if (isset($data[$component])) {
  134. $this->$method($data[$component]);
  135. }
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Retrieve default controller name
  141. *
  142. * @return string
  143. */
  144. public function getDefaultControllerName()
  145. {
  146. return self::DEFAULT_CONTROLLER_NAME;
  147. }
  148. public function setUseUrlCache($flag)
  149. {
  150. $this->setData('use_url_cache', $flag);
  151. return $this;
  152. }
  153. /**
  154. * Set use session rule
  155. *
  156. * @param bool $useSession
  157. * @return Mage_Core_Model_Url
  158. */
  159. public function setUseSession($useSession)
  160. {
  161. $this->_useSession = (bool)$useSession;
  162. return $this;
  163. }
  164. public function setRouteFrontName($name)
  165. {
  166. $this->setData('route_front_name', $name);
  167. return $this;
  168. }
  169. /**
  170. * Retrieve use session rule
  171. *
  172. * @return bool
  173. */
  174. public function getUseSession()
  175. {
  176. if (is_null($this->_useSession)) {
  177. $this->_useSession = Mage::app()->getUseSessionInUrl();
  178. }
  179. return $this->_useSession;
  180. }
  181. /**
  182. * Retrieve default action name
  183. *
  184. * @return string
  185. */
  186. public function getDefaultActionName()
  187. {
  188. return self::DEFAULT_ACTION_NAME;
  189. }
  190. public function getConfigData($key, $prefix=null)
  191. {
  192. if (is_null($prefix)) {
  193. $prefix = 'web/'.($this->getSecure() ? 'secure' : 'unsecure').'/';
  194. }
  195. $path = $prefix.$key;
  196. $cacheId = $this->getStore()->getCode().'/'.$path;
  197. if (!isset(self::$_configDataCache[$cacheId])) {
  198. $data = $this->getStore()->getConfig($path);
  199. self::$_configDataCache[$cacheId] = $data;
  200. }
  201. return self::$_configDataCache[$cacheId];
  202. }
  203. public function setRequest(Zend_Controller_Request_Http $request)
  204. {
  205. $this->_request = $request;
  206. return $this;
  207. }
  208. /**
  209. * Zend request object
  210. *
  211. * @return Zend_Controller_Request_Http
  212. */
  213. public function getRequest()
  214. {
  215. if (!$this->_request) {
  216. $this->_request = Mage::app()->getRequest();
  217. }
  218. return $this->_request;
  219. }
  220. public function getType()
  221. {
  222. if (!$this->hasData('type')) {
  223. $this->setData('type', Mage_Core_Model_Store::URL_TYPE_LINK);
  224. }
  225. return $this->_getData('type');
  226. }
  227. /**
  228. * Retrieve is secure mode URL
  229. *
  230. * @return bool
  231. */
  232. public function getSecure()
  233. {
  234. if ($this->hasData('secure_is_forced')) {
  235. return $this->getData('secure');
  236. }
  237. $store = $this->getStore();
  238. if ($store->isAdmin() && !$store->isAdminUrlSecure()) { //!Mage::getStoreConfigFlag(self::XML_PATH_SECURE_IN_ADMIN, $this->getStore()->getId())
  239. return false;
  240. }
  241. if (!$store->isAdmin() && !$store->isFrontUrlSecure()) {//!Mage::getStoreConfigFlag(self::XML_PATH_SECURE_IN_FRONT
  242. return false;
  243. }
  244. if (!$this->hasData('secure')) {
  245. if ($this->getType() == Mage_Core_Model_Store::URL_TYPE_LINK) {
  246. $pathSecure = Mage::getConfig()->shouldUrlBeSecure('/'.$this->getActionPath());
  247. $this->setData('secure', $pathSecure);
  248. } else {
  249. $this->setData('secure', $store->isCurrentlySecure());
  250. }
  251. }
  252. return $this->getData('secure');
  253. }
  254. public function setStore($data)
  255. {
  256. $this->setData('store', Mage::app()->getStore($data));
  257. return $this;
  258. }
  259. /**
  260. * Get current store for the url instance
  261. *
  262. * @return Mage_Core_Model_Store
  263. */
  264. public function getStore()
  265. {
  266. if (!$this->hasData('store')) {
  267. $this->setStore(null);
  268. }
  269. return $this->_getData('store');
  270. }
  271. /**
  272. * Retrieve Base URL
  273. *
  274. * @param array $params
  275. * @return string
  276. */
  277. public function getBaseUrl($params = array())
  278. {
  279. if (isset($params['_store'])) {
  280. $this->setStore($params['_store']);
  281. }
  282. if (isset($params['_type'])) {
  283. $this->setType($params['_type']);
  284. }
  285. if (isset($params['_secure'])) {
  286. $this->setSecure($params['_secure']);
  287. }
  288. /**
  289. * Add availability support urls without store code
  290. */
  291. if ($this->getType() == Mage_Core_Model_Store::URL_TYPE_LINK
  292. && Mage::app()->getRequest()->isDirectAccessFrontendName($this->getRouteFrontName())) {
  293. $this->setType(Mage_Core_Model_Store::URL_TYPE_DIRECT_LINK);
  294. }
  295. return $this->getStore()->getBaseUrl($this->getType(), $this->getSecure());
  296. }
  297. /**
  298. * Set Route Parameters
  299. *
  300. * @param array $data
  301. * @return Mage_Core_Model_Url
  302. */
  303. public function setRoutePath($data)
  304. {
  305. if ($this->_getData('route_path')==$data) {
  306. return $this;
  307. }
  308. $a = explode('/', $data);
  309. $route = array_shift($a);
  310. if ('*'===$route) {
  311. $route = $this->getRequest()->getRequestedRouteName();
  312. }
  313. $this->setRouteName($route);
  314. $routePath = $route.'/';
  315. if (!empty($a)) {
  316. $controller = array_shift($a);
  317. if ('*'===$controller) {
  318. $controller = $this->getRequest()->getRequestedControllerName();
  319. }
  320. $this->setControllerName($controller);
  321. $routePath .= $controller.'/';
  322. }
  323. if (!empty($a)) {
  324. $action = array_shift($a);
  325. if ('*'===$action) {
  326. $action = $this->getRequest()->getRequestedActionName();
  327. }
  328. $this->setActionName($action);
  329. $routePath .= $action.'/';
  330. }
  331. if (!empty($a)) {
  332. $this->unsetData('route_params');
  333. while (!empty($a)) {
  334. $key = array_shift($a);
  335. if (!empty($a)) {
  336. $value = array_shift($a);
  337. $this->setRouteParam($key, $value);
  338. #$routePath .= $key.'/'.urlencode($value).'/';
  339. $routePath .= $key.'/'.$value.'/';
  340. }
  341. }
  342. }
  343. #$this->setData('route_path', $routePath);
  344. return $this;
  345. }
  346. public function getActionPath()
  347. {
  348. if (!$this->getRouteName()) {
  349. return '';
  350. }
  351. $hasParams = (bool)$this->getRouteParams();
  352. $path = $this->getRouteFrontName() . '/';
  353. if ($this->getControllerName()) {
  354. $path .= $this->getControllerName() . '/';
  355. } elseif ($hasParams) {
  356. $path .= $this->getDefaultControllerName() . '/';
  357. }
  358. if ($this->getActionName()) {
  359. $path .= $this->getActionName() . '/';
  360. } elseif ($hasParams) {
  361. $path .= $this->getDefaultActionName() . '/';
  362. }
  363. return $path;
  364. }
  365. public function getRoutePath($routeParams=array())
  366. {
  367. if (!$this->hasData('route_path')) {
  368. $routePath = $this->getRequest()->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS);
  369. if (!empty($routeParams['_use_rewrite'])
  370. && ($routePath !== null)) {
  371. $this->setData('route_path', $routePath);
  372. return $routePath;
  373. }
  374. $routePath = $this->getActionPath();
  375. if ($this->getRouteParams()) {
  376. foreach ($this->getRouteParams() as $key=>$value) {
  377. if (is_null($value) || false===$value || ''===$value || !is_scalar($value)) {
  378. continue;
  379. }
  380. $routePath .= $key.'/'.$value.'/';
  381. }
  382. }
  383. if ($routePath != '' && substr($routePath, -1, 1) !== '/') {
  384. $routePath.= '/';
  385. }
  386. $this->setData('route_path', $routePath);
  387. }
  388. return $this->_getData('route_path');
  389. }
  390. public function setRouteName($data)
  391. {
  392. if ($this->_getData('route_name')==$data) {
  393. return $this;
  394. }
  395. $this->unsetData('route_front_name')
  396. ->unsetData('route_path')
  397. ->unsetData('controller_name')
  398. ->unsetData('action_name')
  399. ->unsetData('secure');
  400. return $this->setData('route_name', $data);
  401. }
  402. public function getRouteFrontName()
  403. {
  404. if (!$this->hasData('route_front_name')) {
  405. $routeName = $this->getRouteName();
  406. $route = Mage::app()->getFrontController()->getRouterByRoute($routeName);
  407. $frontName = $route->getFrontNameByRoute($routeName);
  408. $this->setRouteFrontName($frontName);
  409. }
  410. return $this->_getData('route_front_name');
  411. }
  412. public function getRouteName()
  413. {
  414. return $this->_getData('route_name');
  415. }
  416. /**
  417. * Set Controller Name
  418. * Reset action name and route path if has change
  419. *
  420. * @param string $data
  421. * @return Mage_Core_Model_Url
  422. */
  423. public function setControllerName($data)
  424. {
  425. if ($this->_getData('controller_name')==$data) {
  426. return $this;
  427. }
  428. $this->unsetData('route_path')->unsetData('action_name')->unsetData('secure');
  429. return $this->setData('controller_name', $data);
  430. }
  431. public function getControllerName()
  432. {
  433. return $this->_getData('controller_name');
  434. }
  435. /**
  436. * Set Action name
  437. * Reseted route path if action name has change
  438. *
  439. * @param string $data
  440. * @return Mage_Core_Model_Url
  441. */
  442. public function setActionName($data)
  443. {
  444. if ($this->_getData('action_name') == $data) {
  445. return $this;
  446. }
  447. $this->unsetData('route_path');
  448. return $this->setData('action_name', $data)->unsetData('secure');
  449. }
  450. public function getActionName()
  451. {
  452. return $this->_getData('action_name');
  453. }
  454. public function setRouteParams(array $data, $unsetOldParams=true)
  455. {
  456. if (isset($data['_type'])) {
  457. $this->setType($data['_type']);
  458. unset($data['_type']);
  459. }
  460. if (isset($data['_store'])) {
  461. $this->setStore($data['_store']);
  462. unset($data['_store']);
  463. }
  464. if (isset($data['_forced_secure'])) {
  465. $this->setSecure((bool)$data['_forced_secure']);
  466. $this->setSecureIsForced(true);
  467. unset($data['_forced_secure']);
  468. } else {
  469. if (isset($data['_secure'])) {
  470. $this->setSecure((bool)$data['_secure']);
  471. unset($data['_secure']);
  472. }
  473. }
  474. if (isset($data['_absolute'])) {
  475. unset($data['_absolute']);
  476. }
  477. if ($unsetOldParams) {
  478. $this->unsetData('route_params');
  479. }
  480. $this->setUseUrlCache(true);
  481. if (isset($data['_current'])) {
  482. if (is_array($data['_current'])) {
  483. foreach ($data['_current'] as $key) {
  484. if (array_key_exists($key, $data) || !$this->getRequest()->getUserParam($key)) {
  485. continue;
  486. }
  487. $data[$key] = $this->getRequest()->getUserParam($key);
  488. }
  489. } elseif ($data['_current']) {
  490. foreach ($this->getRequest()->getUserParams() as $key=>$value) {
  491. if (array_key_exists($key, $data) || $this->getRouteParam($key)) {
  492. continue;
  493. }
  494. $data[$key] = $value;
  495. }
  496. foreach ($this->getRequest()->getQuery() as $key=>$value) {
  497. $this->setQueryParam($key, $value);
  498. }
  499. $this->setUseUrlCache(false);
  500. }
  501. unset($data['_current']);
  502. }
  503. if (isset($data['_use_rewrite'])) {
  504. unset($data['_use_rewrite']);
  505. }
  506. if (isset($data['_store_to_url']) && (bool)$data['_store_to_url'] === true) {
  507. if (!Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getStore())
  508. && !Mage::app()->isSingleStoreMode()
  509. ) {
  510. $this->setQueryParam('___store', $this->getStore()->getCode());
  511. }
  512. }
  513. unset($data['_store_to_url']);
  514. foreach ($data as $k=>$v) {
  515. $this->setRouteParam($k, $v);
  516. }
  517. return $this;
  518. }
  519. public function getRouteParams()
  520. {
  521. return $this->_getData('route_params');
  522. }
  523. public function setRouteParam($key, $data)
  524. {
  525. $params = $this->_getData('route_params');
  526. if (isset($params[$key]) && $params[$key]==$data) {
  527. return $this;
  528. }
  529. $params[$key] = $data;
  530. $this->unsetData('route_path');
  531. return $this->setData('route_params', $params);
  532. }
  533. public function getRouteParam($key)
  534. {
  535. return $this->_getData('route_params', $key);
  536. }
  537. public function getRouteUrl($routePath=null, $routeParams=null)
  538. {
  539. $this->unsetData('route_params');
  540. if (isset($routeParams['_direct'])) {
  541. if (is_array($routeParams)) {
  542. $this->setRouteParams($routeParams, false);
  543. }
  544. return $this->getBaseUrl().$routeParams['_direct'];
  545. }
  546. if (!is_null($routePath)) {
  547. $this->setRoutePath($routePath);
  548. }
  549. if (is_array($routeParams)) {
  550. $this->setRouteParams($routeParams, false);
  551. }
  552. $url = $this->getBaseUrl().$this->getRoutePath($routeParams);
  553. return $url;
  554. }
  555. /**
  556. * If the host was switched but session cookie won't recognize it - add session id to query
  557. *
  558. * @return unknown
  559. */
  560. public function checkCookieDomains()
  561. {
  562. $hostArr = explode(':', $this->getRequest()->getServer('HTTP_HOST'));
  563. if ($hostArr[0]!==$this->getHost()) {
  564. $session = Mage::getSingleton('core/session');
  565. if (!$session->isValidForHost($this->getHost())) {
  566. if (!self::$_encryptedSessionId) {
  567. $helper = Mage::helper('core');
  568. if (!$helper) {
  569. return $this;
  570. }
  571. self::$_encryptedSessionId = $session->getEncryptedSessionId();
  572. }
  573. $this->setQueryParam(
  574. $session->getSessionIdQueryParam(),
  575. self::$_encryptedSessionId
  576. );
  577. }
  578. }
  579. return $this;
  580. }
  581. public function addSessionParam()
  582. {
  583. $session = Mage::getSingleton('core/session');
  584. if (!self::$_encryptedSessionId) {
  585. $helper = Mage::helper('core');
  586. if (!$helper) {
  587. return $this;
  588. }
  589. self::$_encryptedSessionId = $session->getEncryptedSessionId();
  590. }
  591. $this->setQueryParam(
  592. $session->getSessionIdQueryParam(),
  593. self::$_encryptedSessionId
  594. );
  595. return $this;
  596. }
  597. /**
  598. * Set URL query param(s)
  599. *
  600. * @param mixed $data
  601. * @return Mage_Core_Model_Url
  602. */
  603. public function setQuery($data)
  604. {
  605. if ($this->_getData('query') == $data) {
  606. return $this;
  607. }
  608. $this->unsetData('query_params');
  609. return $this->setData('query', $data);
  610. }
  611. /**
  612. * Get query params part of url
  613. *
  614. * @param bool $escape "&" escape flag
  615. * @return string
  616. */
  617. public function getQuery($escape = false)
  618. {
  619. if (!$this->hasData('query')) {
  620. $query = '';
  621. $params = $this->getQueryParams();
  622. if (is_array($params)) {
  623. ksort($params);
  624. $query = http_build_query($params, '', $escape ? '&amp;' : '&');
  625. }
  626. $this->setData('query', $query);
  627. }
  628. return $this->_getData('query');
  629. }
  630. /**
  631. * Set query Params as array
  632. *
  633. * @param array $data
  634. * @return Mage_Core_Model_Url
  635. */
  636. public function setQueryParams(array $data)
  637. {
  638. $this->unsetData('query');
  639. if ($this->_getData('query_params') == $data) {
  640. return $this;
  641. }
  642. $params = $this->_getData('query_params');
  643. if (!is_array($params)) {
  644. $params = array();
  645. }
  646. foreach ($data as $param => $value) {
  647. $params[$param] = $value;
  648. }
  649. $this->setData('query_params', $params);
  650. return $this;
  651. }
  652. /**
  653. * Purge Query params array
  654. *
  655. * @return Mage_Core_Model_Url
  656. */
  657. public function purgeQueryParams()
  658. {
  659. $this->setData('query_params', array());
  660. return $this;
  661. }
  662. /**
  663. * Retrurn Query Params
  664. *
  665. * @return array
  666. */
  667. public function getQueryParams()
  668. {
  669. if (!$this->hasData('query_params')) {
  670. $params = array();
  671. if ($this->_getData('query')) {
  672. foreach (explode('&', $this->_getData('query')) as $param) {
  673. $paramArr = explode('=', $param);
  674. $params[$paramArr[0]] = urldecode($paramArr[1]);
  675. }
  676. }
  677. $this->setData('query_params', $params);
  678. }
  679. return $this->_getData('query_params');
  680. }
  681. public function setQueryParam($key, $data)
  682. {
  683. $params = $this->getQueryParams();
  684. if (isset($params[$key]) && $params[$key]==$data) {
  685. return $this;
  686. }
  687. $params[$key] = $data;
  688. $this->unsetData('query');
  689. return $this->setData('query_params', $params);
  690. }
  691. public function getQueryParam($key)
  692. {
  693. if (!$this->hasData('query_params')) {
  694. $this->getQueryParams();
  695. }
  696. return $this->_getData('query_params', $key);
  697. }
  698. /**
  699. * Set fragment to URL
  700. *
  701. * @param string $data
  702. * @return Mage_Core_Model_Url
  703. */
  704. public function setFragment($data)
  705. {
  706. return $this->setData('fragment', $data);
  707. }
  708. public function getFragment()
  709. {
  710. return $this->_getData('fragment');
  711. }
  712. /**
  713. * Build url by requested path and parameters
  714. *
  715. * @param string $routePath
  716. * @param array $routeParams
  717. * @return string
  718. */
  719. public function getUrl($routePath=null, $routeParams=null)
  720. {
  721. $escapeQuery = false;
  722. /**
  723. * All system params should be unseted before we call getRouteUrl
  724. * this method has condition for ading default controller anr actions names
  725. * in case when we have params
  726. */
  727. if (isset($routeParams['_fragment'])) {
  728. $this->setFragment($routeParams['_fragment']);
  729. unset($routeParams['_fragment']);
  730. }
  731. if (isset($routeParams['_escape'])) {
  732. $escapeQuery = $routeParams['_escape'];
  733. unset($routeParams['_escape']);
  734. }
  735. $query = null;
  736. if (isset($routeParams['_query'])) {
  737. $this->purgeQueryParams();
  738. $query = $routeParams['_query'];
  739. unset($routeParams['_query']);
  740. }
  741. $noSid = null;
  742. if (isset($routeParams['_nosid'])) {
  743. $noSid = (bool)$routeParams['_nosid'];
  744. unset($routeParams['_nosid']);
  745. }
  746. $url = $this->getRouteUrl($routePath, $routeParams);
  747. /**
  748. * Apply query params, need call after getRouteUrl for rewrite _current values
  749. */
  750. if ($query !== null) {
  751. if (is_string($query)) {
  752. $this->setQuery($query);
  753. } elseif (is_array($query)) {
  754. $this->setQueryParams($query, !empty($routeParams['_current']));
  755. }
  756. if ($query === false) {
  757. $this->setQueryParams(array());
  758. }
  759. }
  760. if ($noSid !== true) {
  761. $this->_prepareSessionUrl($url);
  762. }
  763. if ($query = $this->getQuery($escapeQuery)) {
  764. $url .= '?'.$query;
  765. }
  766. if ($this->getFragment()) {
  767. $url .= '#'.$this->getFragment();
  768. }
  769. return $this->escape($url);
  770. }
  771. /**
  772. * Check and add session id to URL
  773. *
  774. * @param string $url
  775. * @return Mage_Core_Model_Url
  776. */
  777. protected function _prepareSessionUrl($url)
  778. {
  779. if (!$this->getUseSession()) {
  780. return $this;
  781. }
  782. $session = Mage::getSingleton('core/session');
  783. /* @var $session Mage_Core_Model_Session */
  784. if (Mage::app()->getUseSessionVar() && !$session->getSessionIdForHost($url)) {
  785. // secure URL
  786. if ($this->getSecure()) {
  787. $this->setQueryParam('___SID', 'S');
  788. }
  789. else {
  790. $this->setQueryParam('___SID', 'U');
  791. }
  792. }
  793. else {
  794. if ($sessionId = $session->getSessionIdForHost($url)) {
  795. $this->setQueryParam($session->getSessionIdQueryParam(), $sessionId);
  796. }
  797. }
  798. return $this;
  799. }
  800. /**
  801. * Escape (enclosure) URL string
  802. *
  803. * @param string $value
  804. * @return string
  805. */
  806. public function escape($value)
  807. {
  808. $value = str_replace('"', '%22', $value);
  809. $value = str_replace("'", '%27', $value);
  810. $value = str_replace('>', '%3E', $value);
  811. $value = str_replace('<', '%3C', $value);
  812. return $value;
  813. }
  814. /**
  815. * Build url by direct url and parameters
  816. *
  817. * @param string $url
  818. * @param array $params
  819. * @return string
  820. */
  821. public function getDirectUrl($url, $params = array()) {
  822. $params['_direct'] = $url;
  823. return $this->getUrl('', $params);
  824. }
  825. /**
  826. * Replace Session ID value in URL
  827. *
  828. * @param string $html
  829. * @return string
  830. */
  831. public function sessionUrlVar($html)
  832. {
  833. return preg_replace_callback('#(\?|&amp;|&)___SID=([SU])(&amp;|&)?#', array($this, "sessionVarCallback"), $html);
  834. }
  835. /**
  836. * Check and return use SID for URL
  837. *
  838. * @param bool $secure
  839. * @return bool
  840. */
  841. public function useSessionIdForUrl($secure = false)
  842. {
  843. $key = 'use_session_id_for_url_' . (int)$secure;
  844. if (is_null($this->getData($key))) {
  845. $httpHost = Mage::app()->getFrontController()->getRequest()->getHttpHost();
  846. $urlHost = parse_url(Mage::app()->getStore()->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK, $secure), PHP_URL_HOST);
  847. if ($httpHost != $urlHost) {
  848. $this->setData($key, true);
  849. }
  850. else {
  851. $this->setData($key, false);
  852. }
  853. }
  854. return $this->getData($key);
  855. }
  856. /**
  857. * Callback function for session replace
  858. *
  859. * @param array $match
  860. * @return string
  861. */
  862. public function sessionVarCallback($match)
  863. {
  864. if ($this->useSessionIdForUrl($match[2] == 'S' ? true : false)) {
  865. $session = Mage::getSingleton('core/session');
  866. /* @var $session Mage_Core_Model_Session */
  867. return $match[1]
  868. . $session->getSessionIdQueryParam()
  869. . '=' . $session->getEncryptedSessionId()
  870. . (isset($match[3]) ? $match[3] : '');
  871. }
  872. else {
  873. if ($match[1] == '?' && isset($match[3])) {
  874. return '?';
  875. }
  876. elseif ($match[1] == '?' && !isset($match[3])) {
  877. return '';
  878. }
  879. elseif (($match[1] == '&amp;' || $match[1] == '&') && !isset($match[3])) {
  880. return '';
  881. }
  882. elseif (($match[1] == '&amp;' || $match[1] == '&') && isset($match[3])) {
  883. return $match[3];
  884. }
  885. }
  886. return '';
  887. }
  888. /**
  889. * Check if users originated URL is one of the domain URLs assigned to stores
  890. *
  891. * @return boolean
  892. */
  893. public function isOwnOriginUrl()
  894. {
  895. $storeDomains = array();
  896. $referer = parse_url(Mage::app()->getFrontController()->getRequest()->getServer('HTTP_REFERER'), PHP_URL_HOST);
  897. foreach (Mage::app()->getStores() as $store) {
  898. $storeDomains[] = parse_url($store->getBaseUrl(), PHP_URL_HOST);
  899. }
  900. $storeDomains = array_unique($storeDomains);
  901. if (empty($referer) || in_array($referer, $storeDomains)) {
  902. return true;
  903. }
  904. return false;
  905. }
  906. }