/
url.php
252 lines (221 loc) · 5.86 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
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* URL helper class.
*
* $Id: url.php 4029 2009-03-03 12:39:32Z Shadowhand $
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class url_Core {
/**
* Fetches the current URI.
*
* @param boolean include the query string
* @return string
*/
public static function current($qs = FALSE)
{
return ($qs === TRUE) ? Router::$complete_uri : Router::$current_uri;
}
/**
* Base URL, with or without the index page.
*
* If protocol (and core.site_protocol) and core.site_domain are both empty,
* then
*
* @param boolean include the index page
* @param boolean non-default protocol
* @return string
*/
public static function base($index = FALSE, $protocol = FALSE)
{
if ($protocol == FALSE)
{
// Use the default configured protocol
$protocol = Kohana::config('core.site_protocol');
}
// Load the site domain
$site_domain = (string) Kohana::config('core.site_domain', TRUE);
if ($protocol == FALSE)
{
if ($site_domain === '' OR $site_domain[0] === '/')
{
// Use the configured site domain
$base_url = $site_domain;
}
else
{
// Guess the protocol to provide full http://domain/path URL
$base_url = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$site_domain;
}
}
else
{
if ($site_domain === '' OR $site_domain[0] === '/')
{
// Guess the server name if the domain starts with slash
$base_url = $protocol.'://'.$_SERVER['HTTP_HOST'].$site_domain;
}
else
{
// Use the configured site domain
$base_url = $protocol.'://'.$site_domain;
}
}
if ($index === TRUE AND $index = Kohana::config('core.index_page'))
{
// Append the index page
$base_url = $base_url.$index;
}
// Force a slash on the end of the URL
return rtrim($base_url, '/').'/';
}
/**
* Fetches an absolute site URL based on a URI segment.
*
* @param string site URI to convert
* @param string non-default protocol
* @return string
*/
public static function site($uri = '', $protocol = FALSE)
{
if ($path = trim(parse_url($uri, PHP_URL_PATH), '/'))
{
// Add path suffix
$path .= Kohana::config('core.url_suffix');
}
if ($query = parse_url($uri, PHP_URL_QUERY))
{
// ?query=string
$query = '?'.$query;
}
if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
{
// #fragment
$fragment = '#'.$fragment;
}
// Concat the URL
return url::base(TRUE, $protocol).$path.$query.$fragment;
}
/**
* Return the URL to a file. Absolute filenames and relative filenames
* are allowed.
*
* @param string filename
* @param boolean include the index page
* @return string
*/
public static function file($file, $index = FALSE)
{
if (strpos($file, '://') === FALSE)
{
// Add the base URL to the filename
$file = url::base($index).$file;
}
return $file;
}
/**
* Merges an array of arguments with the current URI and query string to
* overload, instead of replace, the current query string.
*
* @param array associative array of arguments
* @return string
*/
public static function merge(array $arguments)
{
if ($_GET === $arguments)
{
$query = Router::$query_string;
}
elseif ($query = http_build_query(array_merge($_GET, $arguments)))
{
$query = '?'.$query;
}
// Return the current URI with the arguments merged into the query string
return Router::$current_uri.$query;
}
/**
* Convert a phrase to a URL-safe title.
*
* @param string phrase to convert
* @param string word separator (- or _)
* @return string
*/
public static function title($title, $separator = '-')
{
$separator = ($separator === '-') ? '-' : '_';
// Replace accented characters by their unaccented equivalents
$title = utf8::transliterate_to_ascii($title);
// Remove all characters that are not the separator, a-z, 0-9, or whitespace
$title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
// Trim separators from the beginning and end
return trim($title, $separator);
}
/**
* Sends a page redirect header and runs the system.redirect Event.
*
* @param mixed string site URI or URL to redirect to, or array of strings if method is 300
* @param string HTTP method of redirect
* @return void
*/
public static function redirect($uri = '', $method = '302')
{
if (Event::has_run('system.send_headers'))
{
return FALSE;
}
$codes = array
(
'refresh' => 'Refresh',
'300' => 'Multiple Choices',
'301' => 'Moved Permanently',
'302' => 'Found',
'303' => 'See Other',
'304' => 'Not Modified',
'305' => 'Use Proxy',
'307' => 'Temporary Redirect'
);
// Validate the method and default to 302
$method = isset($codes[$method]) ? (string) $method : '302';
if ($method === '300')
{
$uri = (array) $uri;
$output = '<ul>';
foreach ($uri as $link)
{
$output .= '<li>'.html::anchor($link).'</li>';
}
$output .= '</ul>';
// The first URI will be used for the Location header
$uri = $uri[0];
}
else
{
$output = '<p>'.html::anchor($uri).'</p>';
}
// Run the redirect event
Event::run('system.redirect', $uri);
if (strpos($uri, '://') === FALSE)
{
// HTTP headers expect absolute URLs
$uri = url::site($uri, request::protocol());
}
if ($method === 'refresh')
{
header('Refresh: 0; url='.$uri);
}
else
{
header('HTTP/1.1 '.$method.' '.$codes[$method]);
header('Location: '.$uri);
}
// We are about to exit, so run the send_headers event
Event::run('system.send_headers');
exit('<h1>'.$method.' - '.$codes[$method].'</h1>'.$output);
}
} // End url