-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURL.php
More file actions
191 lines (179 loc) · 5.59 KB
/
URL.php
File metadata and controls
191 lines (179 loc) · 5.59 KB
1
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage URL
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
*
* Url generation class. Includes "old" and "new" flavour
* the new flavour uses PEAR Net_URL_Mapper and is more powerful. But not used in production yet...
* "old" flavour simply creates a module/action then appends the other passed variables as a query string
*
*/
class URL {
/**
* $method string "old" or "new"
* @access public
* @static
* URL can generate urls in two flavours
*/
public static $method='old';
/**
* $bases array
* @access public
* @static
* stores current, secure and unsecure root urls for the running application.
*
*/
public static $bases;
/**
* setBases initialize the static property $bases. this method MUST be called BEFORE any URL generation
* tipically in the configuration process.
* This allows to generate URLS for the application in a regular or ssl way (for example content pages will be called using the regular http scheme, user specific pages using the ssl protocol)
* @param $bases array including one or three strings.
* examples :
* // If your application does not use SSL access
* URL::setBases(array('http://www.mydomain.tld'));
*
* // If your application uses only SSL access
* URL::setBases(array('https://secure.mydomain.tld'));
*
* // If your application is running on both http and https domains, you may way the enduser to browse the non-critical pages using http protocol,
* // and its private interface using the ssl protocol.
* URL::setBases(array('http://www.mydomain.tld','secure'=>'https://secure/mydomain.tld','unsecure'=>'http://www.mydomain.tld'));
*
*/
public static function setBases($bases) {
self::$bases = $bases;
}
/**
* returns previously initialized url bases
*/
public static function getBases()
{
return self::$bases;
}
/**
* generates and echoes an URL
* @see URL::get for parameters detail
*/
public static function e($route,$params = null,$lang=null,$isSecure=null)
{
echo self::get($route,$params,$lang,$isSecure);
}
/**
* generates and echoes a relative path.
* @see URL::getAnchor for parameters detail
*/
public static function ea($route,$params = null,$lang=null)
{
echo self::getAnchor($route,$params,$lang);
}
/**
* generates and returns an URL
* @param $route string module/action string
* @param $params array associative array of additional parameters to include in the generated URL
* @param $lang string specifies language for the generated url. If not provided uses the current language
* @param $isSecure bool wether use current (null), secure (true) or unsecure(false) base for the generated url
* @return string generated absolute URL
*/
public static function get($route,$params = null,$lang=null,$isSecure=null)
{
if($isSecure===true) {
$url = self::$bases['secure'];
} elseif($isSecure===false) {
$url = self::$bases['unsecure'];
} else {
$url = self::$bases[0];
}
return $url.self::getAnchor($route,$params,$lang);
}
/**
* generates and returns a relative path.
* @param $route string module/action string
* @param $params array associative array of additional parameters to include in the generated URL
* @param $lang string specifies language for the generated url. If not provided uses the current language
* @return string generated relative path
**/
public static function getAnchor($route,$params = null,$lang=null)
{
if(self::$method=='old') {
return self::getOldAnchor($route,$params,$lang);
}
unset($params['lang']);
unset($params['module']);
unset($params['action']);
if($lang==null) {
$lang=T::getLang();
}
$params['lang'] = $lang;
list($params['module'],$params['action']) = explode('/',$route);
if(empty($params['action'])) {$params['action'] = null;}
return Net_URL_Mapper::getInstance()->generate($params);
}
/**
* generates and returns a relative path when URL is set to the "old" method.
* @see URL::getAnchor() for parameters detail
* @return string generated relative path
**/
public static function getOldAnchor($route,$params = null,$lang=null)
{
unset($params['lang']);
unset($params['module']);
unset($params['action']);
if($lang==null) {
$lang=T::getLang();
}
if($params == null) {
return $lang.'/'.$route;
}
return $lang.'/'.$route.'?'.http_build_query($params);
}
/**
* generates and returns the currently requested URL for a specific language
* @param $lang string language
* @return string generated url
*/
public static function getFor($lang)
{
$g = $_GET;
$route = $g['module'].($g['action']?'/'.$g['action']:'');
unset($g['module']);
unset($g['action']);
return self::get($route,$g,$lang);
}
/**
* generates and returns the currently requested URL
* @return string generated url
*/
public static function getself($arr = null)
{
if(is_array($arr)) {
$arr = array_merge($_GET,$arr);
} else {
$arr = $_GET;
}
return self::get($_GET['module'].'/'.$_GET['action'],$arr);
}
/**
* generates and echoes the currently requested URL
*/
public static function eself()
{
echo URL::getself();
}
/**
* @todo
* generates and returns an url merging additional parameters to the currently requested one
* this method will replace the one used in office applications ( M_Office_Util::getQueryParams )
*/
public static function merge($add = array() , $remove = array(), $entities = false)
{
# code...
}
}