1 <?php defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: url.php 4029 2009-03-03 12:39:32Z Shadowhand $
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 if ($protocol == FALSE)
39 // Use the default configured protocol
40 $protocol = Kohana::config('core.site_protocol');
43 // Load the site domain
44 $site_domain = (string) Kohana::config('core.site_domain', TRUE);
46 if ($protocol == FALSE)
48 if ($site_domain === '' OR $site_domain[0] === '/')
50 // Use the configured site domain
51 $base_url = $site_domain;
55 // Guess the protocol to provide full http://domain/path URL
56 $base_url = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$site_domain;
61 if ($site_domain === '' OR $site_domain[0] === '/')
63 // Guess the server name if the domain starts with slash
64 $base_url = $protocol.'://'.$_SERVER['HTTP_HOST'].$site_domain;
68 // Use the configured site domain
69 $base_url = $protocol.'://'.$site_domain;
73 if ($index === TRUE AND $index = Kohana::config('core.index_page'))
75 // Append the index page
76 $base_url = $base_url.$index;
79 // Force a slash on the end of the URL
80 return rtrim($base_url, '/').'/';
84 * Fetches an absolute site URL based on a URI segment.
86 * @param string site URI to convert
87 * @param string non-default protocol
90 public static function site($uri = '', $protocol = FALSE)
92 if ($path = trim(parse_url($uri, PHP_URL_PATH), '/'))
95 $path .= Kohana::config('core.url_suffix');
98 if ($query = parse_url($uri, PHP_URL_QUERY))
104 if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
107 $fragment = '#'.$fragment;
111 return url::base(TRUE, $protocol).$path.$query.$fragment;
115 * Return the URL to a file. Absolute filenames and relative filenames
118 * @param string filename
119 * @param boolean include the index page
122 public static function file($file, $index = FALSE)
124 if (strpos($file, '://') === FALSE)
126 // Add the base URL to the filename
127 $file = url::base($index).$file;
134 * Merges an array of arguments with the current URI and query string to
135 * overload, instead of replace, the current query string.
137 * @param array associative array of arguments
140 public static function merge(array $arguments)
142 if ($_GET === $arguments)
144 $query = Router::$query_string;
146 elseif ($query = http_build_query(array_merge($_GET, $arguments)))
151 // Return the current URI with the arguments merged into the query string
152 return Router::$current_uri.$query;
156 * Convert a phrase to a URL-safe title.
158 * @param string phrase to convert
159 * @param string word separator (- or _)
162 public static function title($title, $separator = '-')
164 $separator = ($separator === '-') ? '-' : '_';
166 // Replace accented characters by their unaccented equivalents
167 $title = utf8::transliterate_to_ascii($title);
169 // Remove all characters that are not the separator, a-z, 0-9, or whitespace
170 $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
172 // Replace all separator characters and whitespace by a single separator
173 $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
175 // Trim separators from the beginning and end
176 return trim($title, $separator);
180 * Sends a page redirect header and runs the system.redirect Event.
182 * @param mixed string site URI or URL to redirect to, or array of strings if method is 300
183 * @param string HTTP method of redirect
186 public static function redirect($uri = '', $method = '302')
188 if (Event::has_run('system.send_headers'))
195 'refresh' => 'Refresh',
196 '300' => 'Multiple Choices',
197 '301' => 'Moved Permanently',
199 '303' => 'See Other',
200 '304' => 'Not Modified',
201 '305' => 'Use Proxy',
202 '307' => 'Temporary Redirect'
205 // Validate the method and default to 302
206 $method = isset($codes[$method]) ? (string) $method : '302';
208 if ($method === '300')
213 foreach ($uri as $link)
215 $output .= '<li>'.html::anchor($link).'</li>';
219 // The first URI will be used for the Location header
224 $output = '<p>'.html::anchor($uri).'</p>';
227 // Run the redirect event
228 Event::run('system.redirect', $uri);
230 if (strpos($uri, '://') === FALSE)
232 // HTTP headers expect absolute URLs
233 $uri = url::site($uri, request::protocol());
236 if ($method === 'refresh')
238 header('Refresh: 0; url='.$uri);
242 header('HTTP/1.1 '.$method.' '.$codes[$method]);
243 header('Location: '.$uri);
246 // We are about to exit, so run the send_headers event
247 Event::run('system.send_headers');
249 exit('<h1>'.$method.' - '.$codes[$method].'</h1>'.$output);