This repository was archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrl.php
More file actions
130 lines (130 loc) · 3.86 KB
/
Url.php
File metadata and controls
130 lines (130 loc) · 3.86 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
<?php
namespace RedCat\Route;
class Url {
protected $baseHref;
protected $suffixHref;
protected $server;
function __construct($server=null){
if(!$server)
$server = &$_SERVER;
$this->server = $server;
}
function setBaseHref($href){
$this->baseHref = $href;
}
function getServerHttps(){
return isset($this->server['HTTPS'])?$this->server['HTTPS']:null;
}
function getServerPort(){
return isset($this->server['SERVER_PORT'])?$this->server['SERVER_PORT']:null;
}
function getProtocolHref(){
return 'http'.($this->getServerHttps()=="on"?'s':'').'://';
}
function getServerHref(){
return $this->server['SERVER_NAME'];
}
function getPortHref(){
$ssl = $this->getServerHttps()=="on";
return $this->getServerPort()&&((!$ssl&&(int)$this->getServerPort()!=80)||($ssl&&(int)$this->getServerPort()!=443))?':'.$this->getServerPort():'';
}
function getBaseHref(){
if(!isset($this->baseHref)){
$this->setBaseHref($this->getProtocolHref().$this->getServerHref().$this->getPortHref().'/');
}
return $this->baseHref.$this->getSuffixHref();
}
function setSuffixHref($href){
$this->suffixHref = $href;
}
function getSuffixHref(){
if(!isset($this->suffixHref)){
if(isset($this->server['REDCAT_URI'])){
$this->suffixHref = ltrim($this->server['REDCAT_URI'],'/');
}
else{
$docRoot = $this->server['DOCUMENT_ROOT'].'/';
//$docRoot = dirname($this->server['SCRIPT_FILENAME']).'/';
if(defined('REDCAT_CWD'))
$cwd = REDCAT_CWD;
else
$cwd = getcwd();
if($docRoot!=$cwd&&strpos($cwd,$docRoot)===0)
$this->suffixHref = substr($cwd,strlen($docRoot));
}
}
return $this->suffixHref;
}
function getUriPath(){
return parse_url(ltrim($this->server['REQUEST_URI'],'/'), \PHP_URL_PATH);
}
function getUri(){
return ltrim($this->server['REQUEST_URI'],'/');
}
function getLocation(){
return $this->getBaseHref().$this->getUri();
}
function getDomain(){
$x = explode('.',$this->getServerHref());
$e = array_pop($x);
return end($x).'.'.$e;
}
function getCanonical($domains=null,$httpSubstitution=false,$isStatic=false){
if(empty($domains))
$domains = $this->getDomain();
if(!is_array($domains))
$domains = (array)$domains;
reset($domains);
$mainDomain = current($domains);
$serverHref = $this->getServerHref();
$canonical = $this->getProtocolHref();
if(in_array($serverHref,$domains))
$canonical .= $serverHref;
else
$canonical .= $mainDomain;
$canonical .= $this->getPortHref().'/'.$this->getSuffixHref();
if(is_string($httpSubstitution)){
$canonical .= $httpSubstitution;
}
elseif($httpSubstitution&&http_response_code()!==200){
$canonical .= http_response_code();
}
else{
if($isStatic)
$uri = $this->getUriPath();
else
$uri = $this->getUri();
$uri = substr($uri,strlen($this->getSuffixHref()));
$canonical .= $uri;
}
return $canonical;
}
function getSubdomainHref($sub=''){
$lg = $this->getSubdomainLang();
$server = $this->getServerHref();
if($lg)
$server = substr($server,strlen($lg)+1);
if($sub)
$sub .= '.';
return $this->getProtocolHref().$sub.$server.$this->getPortHref().'/'.$this->getSuffixHref();
}
function getSubdomainLang($domain=null){
if(!isset($domain))
$domain = $this->getServerHref();
$urlParts = explode('.', $domain);
if(count($urlParts)>2&&strlen($urlParts[0])==2)
return $urlParts[0];
else
return null;
}
static function slug($string){
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
function addParamsToUri($uri, $params){
$sep = strpos($uri,'?')===false?'?':'&';
return $uri.$sep.http_build_query($params);
}
function getUriWithParams($params=[]){
return $this->addParamsToUri($this->getUri(),$params);
}
}