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: class Mage_Core_Helper_Url extends Mage_Core_Helper_Abstract
35: {
36:
37: 38: 39: 40: 41:
42: public function getCurrentUrl()
43: {
44: $request = Mage::app()->getRequest();
45: $port = $request->getServer('SERVER_PORT');
46: if ($port) {
47: $defaultPorts = array(
48: Mage_Core_Controller_Request_Http::DEFAULT_HTTP_PORT,
49: Mage_Core_Controller_Request_Http::DEFAULT_HTTPS_PORT
50: );
51: $port = (in_array($port, $defaultPorts)) ? '' : ':' . $port;
52: }
53: $url = $request->getScheme() . '://' . $request->getHttpHost() . $port . $request->getServer('REQUEST_URI');
54: return $url;
55:
56: }
57:
58: 59: 60: 61: 62:
63: public function getCurrentBase64Url()
64: {
65: return $this->urlEncode($this->getCurrentUrl());
66: }
67:
68: public function getEncodedUrl($url=null)
69: {
70: if (!$url) {
71: $url = $this->getCurrentUrl();
72: }
73: return $this->urlEncode($url);
74: }
75:
76: 77: 78: 79: 80:
81: public function getHomeUrl()
82: {
83: return Mage::getBaseUrl();
84: }
85:
86: protected function _prepareString($string)
87: {
88: $string = preg_replace('#[^0-9a-z]+#i', '-', $string);
89: $string = strtolower($string);
90: $string = trim($string, '-');
91:
92: return $string;
93: }
94:
95: 96: 97: 98: 99: 100: 101:
102: public function addRequestParam($url, $param)
103: {
104: $startDelimiter = (false === strpos($url,'?'))? '?' : '&';
105:
106: $arrQueryParams = array();
107: foreach($param as $key=>$value) {
108: if (is_numeric($key) || is_object($value)) {
109: continue;
110: }
111:
112: if (is_array($value)) {
113:
114: $arrQueryParams[] = $key . '[]=' . implode('&' . $key . '[]=', $value);
115: } elseif (is_null($value)) {
116: $arrQueryParams[] = $key;
117: } else {
118: $arrQueryParams[] = $key . '=' . $value;
119: }
120: }
121: $url .= $startDelimiter . implode('&', $arrQueryParams);
122:
123: return $url;
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public function removeRequestParam($url, $paramKey, $caseSensitive = false)
134: {
135: $regExpression = '/\\?[^#]*?(' . preg_quote($paramKey, '/') . '\\=[^#&]*&?)/' . ($caseSensitive ? '' : 'i');
136: while (preg_match($regExpression, $url, $mathes) != 0) {
137: $paramString = $mathes[1];
138: if (preg_match('/&$/', $paramString) == 0) {
139: $url = preg_replace('/(&|\\?)?' . preg_quote($paramString, '/') . '/', '', $url);
140: } else {
141: $url = str_replace($paramString, '', $url);
142: }
143: }
144: return $url;
145: }
146: }
147: