PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url.php

https://github.com/MHordecki/milionkostek
PHP | 228 lines | 129 code | 28 blank | 71 comment | 19 complexity | b946f242cb99c0404e7bb2c936345ddc MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * URL helper class.
  4. *
  5. * $Id: url.php 2735 2008-06-02 15:24:41Z 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 = Config::item('core.site_protocol');
  39. }
  40. // Load the site domain
  41. $site_domain = (string) Config::item('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 = Config::item('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 .= Config::item('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. * Merges an array of arguments with the current URI and query string to
  105. * overload, instead of replace, the current query string.
  106. *
  107. * @param array associative array of arguments
  108. * @return string
  109. */
  110. public static function merge(array $arguments)
  111. {
  112. if ($_GET === $arguments)
  113. {
  114. $query = Router::$query_string;
  115. }
  116. elseif ($query = http_build_query(array_merge($_GET, $arguments)))
  117. {
  118. $query = '?'.$query;
  119. }
  120. // Return the current URI with the arguments merged into the query string
  121. return Router::$current_uri.$query;
  122. }
  123. /**
  124. * Convert a phrase to a URL-safe title.
  125. *
  126. * @param string phrase to convert
  127. * @param string word separator (- or _)
  128. * @return string
  129. */
  130. public static function title($title, $separator = '-')
  131. {
  132. $separator = ($separator === '-') ? '-' : '_';
  133. // Replace accented characters by their unaccented equivalents
  134. $title = utf8::transliterate_to_ascii($title);
  135. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  136. $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
  137. // Replace all separator characters and whitespace by a single separator
  138. $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
  139. // Trim separators from the beginning and end
  140. return trim($title, $separator);
  141. }
  142. /**
  143. * Sends a page redirect header.
  144. *
  145. * @param mixed string site URI or URL to redirect to, or array of strings if method is 300
  146. * @param string HTTP method of redirect
  147. * @return void
  148. */
  149. public static function redirect($uri = '', $method = '302')
  150. {
  151. if (Event::has_run('system.send_headers'))
  152. return;
  153. $uri = (array) $uri;
  154. for ($i = 0, $count_uri = count($uri); $i < $count_uri; $i++)
  155. {
  156. if (strpos($uri[$i], '://') === FALSE)
  157. {
  158. $uri[$i] = url::site($uri[$i]);
  159. }
  160. }
  161. if ($method == '300')
  162. {
  163. if ($count_uri > 0)
  164. {
  165. header('HTTP/1.1 300 Multiple Choices');
  166. header('Location: '.$uri[0]);
  167. $choices = '';
  168. foreach ($uri as $href)
  169. {
  170. $choices .= '<li><a href="'.$href.'">'.$href.'</a></li>';
  171. }
  172. exit('<h1>301 - Multiple Choices:</h1><ul>'.$choices.'</ul>');
  173. }
  174. }
  175. else
  176. {
  177. $uri = $uri[0];
  178. if ($method == 'refresh')
  179. {
  180. header('Refresh: 0; url='.$uri);
  181. }
  182. else
  183. {
  184. $codes = array
  185. (
  186. '301' => 'Moved Permanently',
  187. '302' => 'Found',
  188. '303' => 'See Other',
  189. '304' => 'Not Modified',
  190. '305' => 'Use Proxy',
  191. '307' => 'Temporary Redirect'
  192. );
  193. $method = isset($codes[$method]) ? $method : '302';
  194. header('HTTP/1.1 '.$method.' '.$codes[$method]);
  195. header('Location: '.$uri);
  196. }
  197. exit('<h1>'.$method.' - '.$codes[$method].'</h1><p><a href="'.$uri.'">'.$uri.'</a></p>');
  198. }
  199. }
  200. } // End url