-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathurl.php
389 lines (351 loc) · 8.93 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
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
<?php
/**
* URL plugin for the PHP Fat-Free Framework
* Allows for easy building and manipulation of complicated URLs.
*
* PHP version 5
*
* @category Plugin
* @package URL
* @author Steven Bredenberg <[email protected]>
* @copyright 2010 Steven Bredenberg
* @license GPL 2.0
* @link http://github.com/killsaw/f3-plugins
*/
/**
* URL plugin.
*
* @category Plugin
* @package URL
* @author Steven Bredenberg <[email protected]>
* @license GPL 2.0
* @link http://github.com/killsaw/f3-plugins
*/
class URL extends Core
{
//! Minimum framework version required to run
const F3_Minimum='1.4.0';
//! Locale-specific error/exception messages
const
TEXT_GENERIC_ERROR='An error occurred. {@CONTEXT}',
TEXT_NO_PROPERTY="Property '{@CONTEXT}' does not exist.",
TEXT_NO_METHOD="Method '{@CONTEXT}' does not exist.";
/**
* URL Scheme (e.g. 'http')
* @type string
* @access protected
*/
protected $scheme = null;
/**
* URL Host (e.g. 'www.cnn.com')
* @type string
* @access protected
*/
protected $host = null;
/**
* URL Port (e.g. 80)
* @type int
* @access protected
*/
protected $port = null;
/**
* URL HTTP auth password.
* @type string
* @access protected
*/
protected $user = null;
/**
* URL HTTP auth username.
* @type string
* @access protected
*/
protected $pass = null;
/**
* URL Path (e.g. '/index.php')
* @type string
* @access protected
*/
protected $path = null;
/**
* URL query params (e.g. ('id'=>1, 's'=>'search'))
* @type array
* @access protected
*/
protected $query = array();
/**
* URL Fragment (e.g. '#page-top')
* @type string
* @access protected
*/
protected $fragment = null;
/**
* Static constructor alias.
*
* @param string $url Optional string URL to build object from.
*
* @return URL
* @access public
*/
public static function url($url=null)
{
return new URL($url);
}
/**
* Set username and password for URL
*
* @param string $user HTTP auth username
* @param string $pass HTTP auth password
*
* @return URL
* @access public
*/
public function setLogin($user, $pass)
{
$this->user = $user;
$this->pass = $pass;
return $this;
}
/**
* Set query parameters for URL
*
* @param string|array $query Query string or key=>val array.
*
* @return URL
* @access public
*/
public function setQuery($query)
{
if (!is_array($query)) {
$parts = explode('&', urldecode($query));
$pairs = array();
foreach ($parts as $p) {
list($k, $v) = explode('=', $p, 2);
$pairs[$k] = $v;
}
$query = $pairs;
}
$this->query = $query;
return $this;
}
/**
* Set a single query parameter for URL.
*
* @param string $name Query param key.
* @param string $value Query param value.
*
* @return URL
* @access public
*/
public function setParam($name, $value)
{
$this->query[$name] = $value;
return $this;
}
/**
* Get a query parameter value.
*
* @param string $name Query param key.
*
* @return string|null String if value set, null otherwise.
* @access public
*/
public function getParam($name)
{
if (array_key_exists($name, $this->query)) {
return $this->query[$name];
}
return null;
}
/**
Retrieves data from URL.
@param $cookies array Cookies for request.
@param $referer string Referring URL.
@param $agent string User-agent for request.
@return string
@public
**/
/**
* Retrieves data from URL.
*
* @param array $cookies Optional array of key=>value cookies.
* @param string $referer Optional HTTP Referer
* @param string $agent Optional User-agent (e.g. Internet Explorer 6.0)
*
* @return string|bool FALSE on failure, string otherwise.
* @access public
*/
public function get($cookies=array(), $referer=null, $agent=null)
{
$headers = '';
if (is_null($agent)) {
$agent = 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)';
}
if (!is_null($referer)) {
$headers .= "Referer: $referer\r\n";
}
if (count($cookies)) {
$cookie_pairs = array();
foreach ($cookies as $k=>$v) {
$cookie_pairs[] = sprintf("%s=%s", $k, $v);
}
$headers .= "Cookies: ".join("; ", $cookie_pairs)."\r\n";
}
$opts = array(
'http'=>array(
'method'=>'GET',
'user_agent'=>$agent,
'headers'=>$headers
)
);
return file_get_contents(
$this->toString(), null,
stream_context_create($opts)
);
}
/**
* Returns HTML link from current URL.
*
* @param string $with_name Optional label for link.
*
* @return string
* @access public
*/
public function toLink($with_name=null)
{
if (is_null($with_name)) {
$with_name = $this->toString();
}
return sprintf('<a href="%s">%s</a>', $this->toString(), $with_name);
}
/**
* Returns URL without path, query, or fragment.
*
* @return string
* @access public
*/
public function getBaseURL()
{
return $this->toString($just_base = true);
}
/**
* Returns full string representation of URL.
*
* @param boolean $just_base Return part or entirety of URL.
*
* @return string
* @access public
*/
public function toString($just_base=false)
{
if (empty($this->scheme)) {
$this->scheme = 'http';
}
if (empty($this->path) || $this->path[0] !== '/') {
$this->path = '/'.$this->path;
}
$login = '';
if (!empty($this->user) && !empty($this->pass)) {
$login = sprintf("%s:%s@", $this->user, $this->pass);
}
$url = sprintf("%s://%s%s", $this->scheme, $login, $this->host);
if (!empty($this->port) && $this->port != 80) {
$url .= ':'.$this->port;
}
// Skip the path, query and fragment.
if ($just_base) {
return $url;
}
$url .= $this->path;
if (count($this->query)) {
$url .= '?'.http_build_query($this->query);
}
if (!empty($this->fragment)) {
$url .= '#'.$this->fragment;
}
return $url;
}
/**
* Returns string representation of object.
*
* @return string
* @access public
* @magic
*/
public function __toString()
{
return $this->toString();
}
/**
* Assign value to class property.
*
* @param string $name Name of property
* @param mixed $value Value to assign to property.
*
* @return string
* @access public
*/
public function __set($name, $value)
{
if (!property_exists($this, $name)) {
self::$global['CONTEXT'] = $name;
trigger_error(self::TEXT_NO_PROPERTY);
}
$this->$name = $value;
return $value;
}
/**
* Return value of class property.
*
* @param string $name Name of property
*
* @return string
* @access public
*/
public function __get($name)
{
if (!property_exists($this, $name)) {
self::$global['CONTEXT'] = $name;
trigger_error(self::TEXT_NO_PROPERTY);
}
return $this->$name;
}
/**
* Intercepts method calls to this class.
*
* @param string $name Name of method
* @param array $args Arguments passed to method
*
* @return URL
* @access public
* @magic
*/
public function __call($name, $args)
{
if (preg_match('/^set(.+)/', $name, $matches)) {
$property = strtolower($matches[1]);
if (property_exists($this, $property)) {
$this->$property = $args[0];
return $this;
}
} else {
self::$global['CONTEXT'] = $name;
trigger_error(self::TEXT_NO_METHOD);
}
}
/**
* Class constructor
*
* @param string $url URL string to build object from.
*
* @access public
* @magic
*/
public function __construct($url=null)
{
if (!is_null($url)) {
foreach (parse_url($url) as $k=>$v) {
call_user_func(array($this, "set$k"), $v);
}
}
}
}