PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url.php

https://github.com/Toushi/flow
PHP | 247 lines | 137 code | 30 blank | 80 comment | 20 complexity | dc5e721728922fbdddbe2feb04e18e76 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * URL helper class.
  4. *
  5. * $Id: url.php 3238 2008-07-30 15:42:28Z Shadowhand $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class url_Core {
  13. /**
  14. * Fetches the current URI.
  15. *
  16. * @param boolean include the query string
  17. * @return string
  18. */
  19. public static function current($qs = FALSE)
  20. {
  21. return ($qs === TRUE) ? Router::$complete_uri : Router::$current_uri;
  22. }
  23. /**
  24. * Base URL, with or without the index page.
  25. *
  26. * If protocol (and core.site_protocol) and core.site_domain are both empty,
  27. * then
  28. *
  29. * @param boolean include the index page
  30. * @param boolean non-default protocol
  31. * @return string
  32. */
  33. public static function base($index = FALSE, $protocol = FALSE)
  34. {
  35. if ($protocol == FALSE)
  36. {
  37. // Use the default configured protocol
  38. $protocol = Kohana::config('core.site_protocol');
  39. }
  40. // Load the site domain
  41. $site_domain = (string) Kohana::config('core.site_domain', TRUE);
  42. if ($protocol == FALSE)
  43. {
  44. if ($site_domain === '' OR $site_domain[0] === '/')
  45. {
  46. // Use the configured site domain
  47. $base_url = $site_domain;
  48. }
  49. else
  50. {
  51. // Guess the protocol to provide full http://domain/path URL
  52. $base_url = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$site_domain;
  53. }
  54. }
  55. else
  56. {
  57. if ($site_domain === '' OR $site_domain[0] === '/')
  58. {
  59. // Guess the server name if the domain starts with slash
  60. $base_url = $protocol.'://'.$_SERVER['HTTP_HOST'].$site_domain;
  61. }
  62. else
  63. {
  64. // Use the configured site domain
  65. $base_url = $protocol.'://'.$site_domain;
  66. }
  67. }
  68. if ($index === TRUE AND $index = Kohana::config('core.index_page'))
  69. {
  70. // Append the index page
  71. $base_url = $base_url.$index;
  72. }
  73. // Force a slash on the end of the URL
  74. return rtrim($base_url, '/').'/';
  75. }
  76. /**
  77. * Fetches an absolute site URL based on a URI segment.
  78. *
  79. * @param string site URI to convert
  80. * @param string non-default protocol
  81. * @return string
  82. */
  83. public static function site($uri = '', $protocol = FALSE)
  84. {
  85. if ($path = trim(parse_url($uri, PHP_URL_PATH), '/'))
  86. {
  87. // Add path suffix
  88. $path .= Kohana::config('core.url_suffix');
  89. }
  90. if ($query = parse_url($uri, PHP_URL_QUERY))
  91. {
  92. // ?query=string
  93. $query = '?'.$query;
  94. }
  95. if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
  96. {
  97. // #fragment
  98. $fragment = '#'.$fragment;
  99. }
  100. // Concat the URL
  101. return url::base(TRUE, $protocol).$path.$query.$fragment;
  102. }
  103. /**
  104. * Return the URL to a file. Absolute filenames and relative filenames
  105. * are allowed.
  106. *
  107. * @param string filename
  108. * @param boolean include the index page
  109. * @return string
  110. */
  111. public static function file($file, $index = FALSE)
  112. {
  113. if (strpos($file, '://') === FALSE)
  114. {
  115. // Add the base URL to the filename
  116. $file = url::base($index).$file;
  117. }
  118. return $file;
  119. }
  120. /**
  121. * Merges an array of arguments with the current URI and query string to
  122. * overload, instead of replace, the current query string.
  123. *
  124. * @param array associative array of arguments
  125. * @return string
  126. */
  127. public static function merge(array $arguments)
  128. {
  129. if ($_GET === $arguments)
  130. {
  131. $query = Router::$query_string;
  132. }
  133. elseif ($query = http_build_query(array_merge($_GET, $arguments)))
  134. {
  135. $query = '?'.$query;
  136. }
  137. // Return the current URI with the arguments merged into the query string
  138. return Router::$current_uri.$query;
  139. }
  140. /**
  141. * Convert a phrase to a URL-safe title.
  142. *
  143. * @param string phrase to convert
  144. * @param string word separator (- or _)
  145. * @return string
  146. */
  147. public static function title($title, $separator = '-')
  148. {
  149. $separator = ($separator === '-') ? '-' : '_';
  150. // Replace accented characters by their unaccented equivalents
  151. $title = utf8::transliterate_to_ascii($title);
  152. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  153. $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
  154. // Replace all separator characters and whitespace by a single separator
  155. $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
  156. // Trim separators from the beginning and end
  157. return trim($title, $separator);
  158. }
  159. /**
  160. * Sends a page redirect header.
  161. *
  162. * @param mixed string site URI or URL to redirect to, or array of strings if method is 300
  163. * @param string HTTP method of redirect
  164. * @return void
  165. */
  166. public static function redirect($uri = '', $method = '302')
  167. {
  168. if (Event::has_run('system.send_headers'))
  169. return;
  170. $uri = (array) $uri;
  171. for ($i = 0, $count_uri = count($uri); $i < $count_uri; $i++)
  172. {
  173. if (strpos($uri[$i], '://') === FALSE)
  174. {
  175. $uri[$i] = url::site($uri[$i]);
  176. }
  177. }
  178. if ($method == '300')
  179. {
  180. if ($count_uri > 0)
  181. {
  182. header('HTTP/1.1 300 Multiple Choices');
  183. header('Location: '.$uri[0]);
  184. $choices = '';
  185. foreach ($uri as $href)
  186. {
  187. $choices .= '<li><a href="'.$href.'">'.$href.'</a></li>';
  188. }
  189. exit('<h1>301 - Multiple Choices:</h1><ul>'.$choices.'</ul>');
  190. }
  191. }
  192. else
  193. {
  194. $uri = $uri[0];
  195. if ($method == 'refresh')
  196. {
  197. header('Refresh: 0; url='.$uri);
  198. }
  199. else
  200. {
  201. $codes = array
  202. (
  203. '301' => 'Moved Permanently',
  204. '302' => 'Found',
  205. '303' => 'See Other',
  206. '304' => 'Not Modified',
  207. '305' => 'Use Proxy',
  208. '307' => 'Temporary Redirect'
  209. );
  210. $method = isset($codes[$method]) ? $method : '302';
  211. header('HTTP/1.1 '.$method.' '.$codes[$method]);
  212. header('Location: '.$uri);
  213. }
  214. exit('<h1>'.$method.' - '.$codes[$method].'</h1><p><a href="'.$uri.'">'.$uri.'</a></p>');
  215. }
  216. }
  217. } // End url