-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.php
204 lines (179 loc) · 6.5 KB
/
url.php
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
<?php
# implements a url object, that when in the context of a string, prints its URL
#
# $u = new url($path, $components, $whatever);
# $u->secure(true); # turn on https
# $u->authentication($user, $pass);
# $u->domain($newdom); # set the authority domain
# $u->absolute(true); # generate an absolute URL
# $u['get'] = 'variable'; # set GET variables
# $u->merge(array(key=>value)); # merge the passed array into the get variables
#
# url::$ABSOLUTE
# url::$HTTPS passed in to the constructor will set those to true
#
# strval($u) # return as a string
# or use in a string context
#
# TODO support all URL components
# support port number
# support protocol-less absolute paths (to make it easier to
# support both http and https with the same page content)
# improve support for https protocol
# https support should really work via changing the protocol
# rather than a boolean flag
class URLOPT { /* an empty class used to pass options into the url constructor */ }
class url implements Countable, ArrayAccess, Iterator {
private $getvars = array();
private $path = array();
private $absolute = false;
private $domain = false;
private $secure = false;
private $includeprotocol = false;
private $authuser;
private $authpass;
public static $ABSOLUTE = NULL;
public static $HTTPS = NULL;
public function __construct() {
# FIXME verify this CGI var, only sure for Apache
$this->secure = isset($_SERVER['HTTPS']); # set default, use current context
$this->domain = $_SERVER['SERVER_NAME']; # set default, use current context
$p = func_get_args();
$this->path($p);
}
public function path() {
$p = array_values_recursive(func_get_args());
$np = array();
foreach ($p as $pc) {
if (empty($pc)) {
continue;
} elseif (is_object($pc)) {
if ($pc === url::$ABSOLUTE) {
$this->absolute(true);
continue;
}
if ($pc === url::$HTTPS) {
$this->secure(true);
continue;
}
$pc = get_class($pc);
if (preg_match('/^controller_(\w+)$/', $pc, $m)) {
$pc = $m[1];
} else {
throw new Exception("$pc does not appear to be a controller class name");
}
} elseif (is_string($pc) && preg_match('/^controller_(\w+)$/', $pc, $m)) {
# could pass in the result of get_class or __CLASS__;
# in that case, strip off the prefixing controller_ part
$pc = $m[1];
} elseif (!(strpos($pc, '/') === false)) {
$pc = trim($pc, " \t\n\r\x0B/\0");
$pc = explode('/', $pc);
while(count($pc) > 1) {
array_push($np, array_shift($pc));
}
if ($pc) $pc = array_shift($pc);
}
$np[] = trim($pc, " \t\n\r\x0B/\0");
}
if (count($np) == 1) {
$np[] = '';
}
$this->path = $np;
}
public function absolute($v = true) {
$this->absolute = !(!$v);
}
public function authentication($user, $pass) {
$this->authuser = strval($user);
$this->authpass = strval($pass);
}
public function domain($d) {
$this->domain = strval($d);
}
public function secure($s = true) {
$this->absolute = true;
$this->secure = !(!$s);
}
public function merge($a) {
foreach ($a as $k=>$v) {
$this[$k] = $v;
}
}
public function __toString() {
if ($this->authuser && $this->authpass) {
$this->absolute = true;
}
if (is_array($_SERVER['default_controller']) && $this->path === $_SERVER['default_controller']) {
$v = $_SERVER['uribase'];
} else {
$o = array_values_recursive(array(rtrim($_SERVER['uribase'], '/'), $this->path));
$v = join('/', $o);
# if we are generating an empty path (to the root of the site)
# then set it to the base uri
if (!$v) {
$v = $_SERVER['uribase'];
}
$v = preg_replace('@/\./@', '/', $v);
}
$v = preg_replace('!/+!', '/', $v);
if ($this->absolute) {
$v = $this->make_absolute($v);
}
if ($this->authuser && $this->authpass) {
$v = $this->add_auth($v);
}
if ($this->getvars) {
$v .= '?'.http_build_query($this->getvars);
}
return $v;
}
private function make_absolute($u) {
if (!$u) { return $u; }
# FIXME if it is already absolute with a protocol, it won't be made secure
if (preg_match('@^https?://@', $u)) {
return $u;
}
if (!preg_match('@^/@', $u)) {
$u = "/$u";
}
$secure = $this->secure ? 's' : '';
$u = sprintf('http%s://%s%s', $secure, $this->domain, $u);
return $u;
}
private function add_auth($u) {
return preg_replace('-^((https?:)?//)(\w)-', '$1'.urlencode($this->authuser).':'.urlencode($this->authpass).'@$3', $u);
}
# Countable interface
public function count() {
return count($this->getvars);
}
# ArrayAccess interface
public function offsetExists($offset) {
return (isset($this->getvars[$offset]));
}
public function offsetGet($offset) {
if (!isset($this->getvars[$offset])) {
return NULL;
}
return $this->getvars[$offset];
}
public function offsetSet($offset, $value) {
if (!isset($offset)) {
$this->path[] = $value;
} else {
$this->getvars[$offset] = $value;
}
}
public function offsetUnset($offset) {
unset($this->getvars[$offset]);
}
# Iterator interface
public function current() { return current($this->getvars); }
public function rewind() { return reset($this->getvars); }
public function key() { return key($this->getvars); }
public function next() { return next($this->getvars); }
public function valid() { return current($this->getvars) ? true : false; }
}
if (!isset(url::$ABSOLUTE)) url::$ABSOLUTE = new URLOPT();
if (!isset(url::$HTTPS)) url::$HTTPS = new URLOPT();