1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: url.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Fetches the current URI.
17 * @param boolean include the query string
20 public static function current($qs = FALSE)
22 return ($qs === TRUE) ? Router
::$complete_uri : Router
::$current_uri;
26 * Base URL, with or without the index page.
28 * If protocol (and core.site_protocol) and core.site_domain are both empty,
31 * @param boolean include the index page
32 * @param boolean non-default protocol
35 public static function base($index = FALSE, $protocol = FALSE)
37 static $default_protocol;
38 if ($protocol == FALSE)
40 if($default_protocol) {
41 // Use the default configured protocol
42 $protocol = $default_protocol;
44 $default_protocol = $protocol = Kohana
::config('core.site_protocol');
48 // Load the site domain
51 $site_domain = (string) Kohana
::config('core.site_domain', TRUE);
54 if ($protocol == FALSE)
56 if ($site_domain === '' OR $site_domain[0] === '/')
58 // Use the configured site domain
59 $base_url = $site_domain;
63 // Guess the protocol to provide full http://domain/path URL
64 $base_url = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ?
'http' : 'https').'://'.$site_domain;
69 if ($site_domain === '' OR $site_domain[0] === '/')
71 // Guess the server name if the domain starts with slash
72 $base_url = $protocol.'://'.$_SERVER['HTTP_HOST'].$site_domain;
76 // Use the configured site domain
77 $base_url = $protocol.'://'.$site_domain;
81 static $core_index_page;
82 if(!$core_index_page) {
83 $core_index_page = Kohana
::config('core.index_page');
85 if ($index === TRUE AND $index = $core_index_page)
87 // Append the index page
88 $base_url = $base_url.$index;
91 // Force a slash on the end of the URL
92 return rtrim($base_url, '/').'/';
96 * Fetches an absolute site URL based on a URI segment.
98 * @param string site URI to convert
99 * @param string non-default protocol
102 public static function site($uri = '', $protocol = FALSE)
104 // This function might get a relative path that contains all sorts of
105 // weird characters (colon is an interesting case) that causes
106 // parse_url to crash and burn as it tries to handle it as a complete
107 // URL. If that is the case, we might get away with prepending
108 // the domain to turn it into a complete URL.
109 if (@parse_url
($uri) === false)
110 $uri = url
::base(TRUE, 'http') . $uri;
112 if ($path = trim(parse_url($uri, PHP_URL_PATH
), '/'))
114 static $core_url_suffix;
115 if(!$core_url_suffix) {
116 $core_url_suffix = Kohana
::config('core.url_suffix');
119 $path .= $core_url_suffix;
122 if ($query = parse_url($uri, PHP_URL_QUERY
))
128 if ($fragment = parse_url($uri, PHP_URL_FRAGMENT
))
131 $fragment = '#'.$fragment;
135 return url
::base(TRUE, $protocol).$path.$query.$fragment;
139 * Return the URL to a file. Absolute filenames and relative filenames
142 * @param string filename
143 * @param boolean include the index page
146 public static function file($file, $index = FALSE)
148 if (strpos($file, '://') === FALSE)
150 // Add the base URL to the filename
151 $file = url
::base($index).$file;
158 * Merges an array of arguments with the current URI and query string to
159 * overload, instead of replace, the current query string.
161 * @param array associative array of arguments
164 public static function merge(array $arguments)
166 if ($_GET === $arguments)
168 $query = Router
::$query_string;
170 elseif ($query = http_build_query(array_merge($_GET, $arguments)))
175 // Return the current URI with the arguments merged into the query string
176 return Router
::$current_uri.$query;
180 * Convert a phrase to a URL-safe title.
182 * @param string phrase to convert
183 * @param string word separator (- or _)
186 public static function title($title, $separator = '-')
188 $separator = ($separator === '-') ?
'-' : '_';
190 // Replace accented characters by their unaccented equivalents
191 $title = utf8
::transliterate_to_ascii($title);
193 // Remove all characters that are not the separator, a-z, 0-9, or whitespace
194 $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
196 // Replace all separator characters and whitespace by a single separator
197 $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
199 // Trim separators from the beginning and end
200 return trim($title, $separator);
204 * Sends a page redirect header and runs the system.redirect Event.
206 * @param mixed string site URI or URL to redirect to, or array of strings if method is 300
207 * @param string HTTP method of redirect
210 public static function redirect($uri = '', $method = '302')
212 if (Event
::has_run('system.send_headers'))
219 'refresh' => 'Refresh',
220 '300' => 'Multiple Choices',
221 '301' => 'Moved Permanently',
223 '303' => 'See Other',
224 '304' => 'Not Modified',
225 '305' => 'Use Proxy',
226 '307' => 'Temporary Redirect'
229 // Validate the method and default to 302
230 $method = isset($codes[$method]) ?
(string) $method : '302';
232 if ($method === '300')
237 foreach ($uri as $link)
239 $output .= '<li>'.html
::anchor($link).'</li>';
243 // The first URI will be used for the Location header
248 $output = '<p>'.html
::anchor($uri).'</p>';
251 // Run the redirect event
252 Event
::run('system.redirect', $uri);
254 if (strpos($uri, '://') === FALSE)
256 // HTTP headers expect absolute URLs
257 $uri = url
::site($uri, request
::protocol());
260 if ($method === 'refresh')
262 header('Refresh: 0; url='.$uri);
266 header('HTTP/1.1 '.$method.' '.$codes[$method]);
267 header('Location: '.$uri);
270 exit('<h1>'.$method.' - '.$codes[$method].'</h1>'.$output);