url_class.php
14 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* URL操作类
*
* @author Tiny
* @class Url
*/
class Url
{
private static $_requestUri;
private static $_baseUri;
private static $_baseDir;
private static $_host;
private static $_urlFormat;
private static $_route;
private static $_scriptFile;
public static function requestUri()
{
if (self::$_requestUri) return self::$_requestUri;
if (isset($_SERVER['HTTP_X_REWRITE_URL']))
{
$uri = $_SERVER['HTTP_X_REWRITE_URL'];
}
elseif (isset($_SERVER['REQUEST_URI']))
{
$uri = $_SERVER['REQUEST_URI'];
}
elseif (isset($_SERVER['ORIG_PATH_INFO']))
{
$uri = $_SERVER['ORIG_PATH_INFO'];
if (! empty($_SERVER['QUERY_STRING']))
{
$uri .= '?' . $_SERVER['QUERY_STRING'];
}
}
else
{
$uri = '';
}
self::$_requestUri = $uri;
return $uri;
}
//取得脚本执行
static function scriptFile()
{
if(self::$_scriptFile) return self::$_scriptFile;
$filename = basename($_SERVER['SCRIPT_FILENAME']);
if (basename($_SERVER['SCRIPT_NAME']) === $filename)
{
$url = $_SERVER['SCRIPT_NAME'];
}
elseif (basename($_SERVER['PHP_SELF']) === $filename)
{
$url = $_SERVER['PHP_SELF'];
}
elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename)
{
$url = $_SERVER['ORIG_SCRIPT_NAME'];
}
else
{
$path = $_SERVER['PHP_SELF'];
$segs = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/'));
$segs = array_reverse($segs);
$index = 0;
$last = count($segs);
$url = '';
do{
$seg = $segs[$index];
$url = '/' . $seg . $url;
++ $index;
} while (($last > $index) && (false !== ($pos = strpos($path, $url))) && (0 != $pos));
}
return $url;
}
static function baseUri()
{
if (self::$_baseUri) return self::$_baseUri;
$url = self::scriptFile();
$request_uri = self::requestUri();
//修正get方式下的不规范Url格式
if (self::getUrlFormat()=='get')
{
self::$_baseUri = $url;
return self::$_baseUri;
}else if(self::getUrlFormat()=='path')
{
$url = rtrim(dirname($url), '/') . '/';
self::$_baseUri = trim($url,'\\');
return self::$_baseUri;
}
if (! strpos($request_uri, basename($url))) return '';
if ((strlen($request_uri) >= strlen($url)) && ((false !== ($pos = strpos($request_uri, $url))) && ($pos !== 0)))
{
$url = substr($request_uri, 0, $pos + strlen($url));
}
self::$_baseUri = $url;
return self::$_baseUri;
}
static function baseDir()
{
if (self::$_baseDir) return self::$_baseDir;
$base_uri = self::baseUri();
if (substr($base_uri, - 1, 1) == '/')
{
$base_dir = $base_uri;
}
else
{
$base_dir = dirname($base_uri);
}
self::$_baseDir = rtrim($base_dir, '/\\') . '/';
return self::$_baseDir;
}
static function pathinfo()
{
if (!empty($_SERVER['PATH_INFO'])) return $_SERVER['PATH_INFO'];
$base_url = self::baseUri();
if (null === ($request_uri = self::requestUri())) return '';
$pos = strpos($request_uri, '?');
if ($pos!==false)
{
$request_uri = substr($request_uri, 0, $pos);
}
if ((null !== $base_url) && (false === ($pathinfo = substr($request_uri, strlen($base_url)))))
{
$pathinfo = '';
}
elseif (null === $base_url)
{
$pathinfo = $request_uri;
}
return $pathinfo;
}
/**
*取得网站的host地址
*/
public static function getHost($http='http')
{
if(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=='on') $http = "https";
if(self::$_host!==null) return self::$_host;
if(isset($_SERVER['HTTP_HOST']))
self::$_host = $http.'://'.$_SERVER['HTTP_HOST'];
else
self::$_host = $http.'://'.$_SERVER['SERVER_NAME'];
return self::$_host;
}
/**
*取得入口文件的Url地址
*/
public static function getEntryUrl()
{
return self::getHost().$_SERVER['SCRIPT_NAME'];
}
/**
*取得前一页的路由地址
*/
public static function getRefRoute()
{
//if(isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] & self::getEntryUrl()) == self::getEntryUrl()) return substr($_SERVER['HTTP_REFERER'],strlen(self::getEntryUrl()));
//else return '';
if(isset($_SERVER['HTTP_REFERER'])){
return substr($_SERVER['HTTP_REFERER'],strlen(self::getHost().self::baseUri()));
}else{
return '';
}
}
public static function urlReWrite()
{
$requestUrl = self::requestUri();
$baseUrl = self::baseUri();
$scriptFile = self::scriptFile();
if(false!==stripos($requestUrl, $scriptFile)){
$baseUrl = $scriptFile;
}
else if(false===stripos($requestUrl, $baseUrl))
{
$format = self::getUrlFormat();
if($format=='get'){
$baseUrl = rtrim(dirname($baseUrl), '/') . '/';
}else{
$baseUrl = self::scriptFile();
}
}
$request = substr($requestUrl,strlen($baseUrl));
if(strlen($request)>=1)
{
$first = substr($request,0,1);
if($first != '?')
{
if(($index=strpos($request,'?')) !== false)
{
$request = substr($request,0,$index);
}
$request = trim($request,'/');
if($request!='')
{
$request_Arr=explode('/',$request);
$len=count($request_Arr);
$routes = self::getRoute();
if($routes !== null)
{
$route = new Route($routes);
$params = $route->parseRoute('/'.$request);
if($params === null)
{
$_GET['con']=isset($request_Arr[0])?$request_Arr[0]:null;
$_GET['act']=isset($request_Arr[1])?$request_Arr[1]:null;
}
else
{
foreach($params as $key=>$param)
{
$_GET[$key]=isset($params[$key])?$params[$key]:null;
}
}
}
else
{
$_GET['con']=isset($request_Arr[0])?$request_Arr[0]:null;
$_GET['act']=isset($request_Arr[1])?$request_Arr[1]:null;
}
if($_GET['act']=='run') $_GET['act']='';
if($len>2)
{
for($i=2;$i<$len;$i++)if($i%2==1)$_GET[$request_Arr[$i-1]]=$request_Arr[$i];
}
}
}else{
if(isset($_GET['con']) && $_GET['con']=='') $_GET['con'] = null;
if(isset($_GET['act']) && $_GET['act']=='') $_GET['act'] = null;
}
}
}
/**
*路径格式化处理
*/
static function urlFormat($path)
{
if($path=='') return self::baseDir();
if(preg_match('@[/\@#*!]?(https?://.+)$@i', $path,$matches)) return $matches[1];
switch(substr($path,0,1))
{
case '/':
{
$path = self::createUrl($path);
return rtrim(self::baseUri(),'/').$path;//解释成绝对路由地址
}
case '@': return self::baseDir().substr($path,1);//解析成绝对路径
case '#': //解析成主题的绝对地址
{
if(Tiny::app()->getTheme()!==null)
{
return Tiny::app()->getTheme()->getBaseUrl().'/'.substr($path,1);
}
else return self::baseDir().substr($path,1);
}
case '*'://解析成皮肤的路径
{
if(Tiny::app()->getTheme()!==null && Tiny::app()->getSkin()!==null)
{
$theme = Tiny::app()->getTheme();
return $theme->getBaseUrl().'/skins/'.Tiny::app()->getSkin().'/'.substr($path,1);
}
else if( Tiny::app()->getSkin()!==null){
return self::baseDir().'skins/'.Tiny::app()->getSkin().'/'.substr($path,1);
}
else return self::urlFormat('#'.substr($path,1));
}
case '!'://运行目录路径
{
return Tiny::app()->getRuntimeUrl().'/'.substr($path,1);
}
default:
{
$q = Req::get();
$url = '/'.$q['con'].'/'.$q['act'];
unset($q['con'],$q['act']);
$query = explode('/',trim($path,'/'));
$new_q=array();
$len = count($query);
for($i=0;$i<$len;$i++)if($i%2==1)$new_q[$query[$i-1]]=$query[$i];
$q = array_merge($q,$new_q);
foreach($q as $k=>$v)
{
if(is_string($k))$url .= '/'.$k.'/'.$v;
}
$path = self::createUrl($url);
return rtrim(self::baseUri(),'/').$path;//解释成绝对路由地址
}
}
}
//完整路径的format
static function fullUrlFormat($path)
{
$path = trim($path);
if(preg_match('@[/\@#*!]?(https?://.+)$@i', $path,$matches)) return $matches[1];
return self::getHost().self::urlFormat($path);
}
public static function createUrl($_url)
{
$routes = self::getRoute();
$route = new Route($routes);
$url = $_url;
$param_key = array();
if(self::getUrlFormat()=='get' || $routes !== null)
{
$param = '';
if(strpos($url,'?')!==false)
{
$url_arr = explode('?',$url);
$url = $url_arr[0];
$param = $url_arr[1];
}
$param_arr = array();
if(substr($url,0,1)=='/')
{
$tem = explode('/',substr($url,1));
$param_arr['con'] = isset($tem[0])?$tem[0]:null;
$param_arr['act'] = isset($tem[1])?$tem[1]:null;
$len = count($tem);
if($len>2)
{
for($i=2;$i<$len;$i=$i+2)
{
$param_arr[$tem[$i]] = isset($tem[$i+1])?$tem[$i+1]:null;
$param_key[$tem[$i]] = $tem[$i];
}
}
}
else
{
$tem = explode('/',$url);
$len = count($tem);
if($len>0)
{
for($i=0;$i<$len;$i=$i+2)
{
$param_arr[$tem[$i]] = isset($tem[$i+1])?$tem[$i+1]:null;
}
}
}
if($param!=''){
$args = explode('&', $param);
foreach ($args as $arg) {
$tem = explode('=',$arg);
$param_arr[$tem[0]] = $tem[1];
}
}
//$param_arr = array_merge($param_arr,$param);
if(self::getUrlFormat()=='get')$url = '?'.http_build_query($param_arr);//.($param==''?'':'&'.$param);
else
{
$param_key = implode("/",$param_key);
//$param_arr = array_merge($param_arr,$param);
$url = $route->createRoute($param_arr,$param_key);
if($url!==null)
{
// if(stripos($url,'?')!==false){
// $url.=($param==''?'':'&'.$param);
// }
// else $url.=($param==''?'':'?'.$param);
}
else
{
if(isset($param_arr['con']) && isset($param_arr['act'])){
$_url = "/".$param_arr['con']."/".$param_arr['act'];
unset($param_arr['con'],$param_arr['act']);
foreach ($param_arr as $key => $value) {
$_url .= '/'.$key.'/'.$value;
}
}
return $_url;
}
}
}
return $url;
}
//解析Url
public function resolveUrl($url)
{
}
public static function getUrlFormat()
{
return self::$_urlFormat;
}
public static function setUrlFormat($value)
{
if($value == 'get' || $value == 'path') self::$_urlFormat = $value;
}
public static function getRoute()
{
return self::$_route;
}
public static function setRoute($route)
{
if(is_array($route)) self::$_route = $route;
}
}