Avail feature updated
[ninja.git] / system / helpers / url.php
blobc77beda3702e0695ee3ece5f9245d82daab01800
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * URL helper class.
5 * $Id: url.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class url {
14 /**
15 * Fetches the current URI.
17 * @param boolean include the query string
18 * @return string
20 public static function current($qs = FALSE)
22 return ($qs === TRUE) ? Router::$complete_uri : Router::$current_uri;
25 /**
26 * Base URL, with or without the index page.
28 * If protocol (and core.site_protocol) and core.site_domain are both empty,
29 * then
31 * @param boolean include the index page
32 * @param boolean non-default protocol
33 * @return string
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;
43 } else {
44 $default_protocol = $protocol = Kohana::config('core.site_protocol');
48 // Load the site domain
49 static $site_domain;
50 if(!$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;
61 else
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;
67 else
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;
74 else
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, '/').'/';
95 /**
96 * Fetches an absolute site URL based on a URI segment.
98 * @param string site URI to convert
99 * @param string non-default protocol
100 * @return string
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');
118 // Add path suffix
119 $path .= $core_url_suffix;
122 if ($query = parse_url($uri, PHP_URL_QUERY))
124 // ?query=string
125 $query = '?'.$query;
128 if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
130 // #fragment
131 $fragment = '#'.$fragment;
134 // Concat the URL
135 return url::base(TRUE, $protocol).$path.$query.$fragment;
139 * Return the URL to a file. Absolute filenames and relative filenames
140 * are allowed.
142 * @param string filename
143 * @param boolean include the index page
144 * @return string
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;
154 return $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
162 * @return string
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)))
172 $query = '?'.$query;
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 _)
184 * @return string
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
208 * @return void
210 public static function redirect($uri = '', $method = '302')
212 if (Event::has_run('system.send_headers'))
214 return FALSE;
217 $codes = array
219 'refresh' => 'Refresh',
220 '300' => 'Multiple Choices',
221 '301' => 'Moved Permanently',
222 '302' => 'Found',
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')
234 $uri = (array) $uri;
236 $output = '<ul>';
237 foreach ($uri as $link)
239 $output .= '<li>'.html::anchor($link).'</li>';
241 $output .= '</ul>';
243 // The first URI will be used for the Location header
244 $uri = $uri[0];
246 else
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);
264 else
266 header('HTTP/1.1 '.$method.' '.$codes[$method]);
267 header('Location: '.$uri);
270 exit('<h1>'.$method.' - '.$codes[$method].'</h1>'.$output);
273 } // End url