PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/url.php

https://bitbucket.org/rlm3/mrs
PHP | 194 lines | 83 code | 21 blank | 90 comment | 12 complexity | d7ce2f40befbe3749a77339f9e63d446 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * URL helper class.
  4. *
  5. * @package Kohana
  6. * @category Helpers
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2011 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_URL {
  12. /**
  13. * Gets the base URL to the application.
  14. * To specify a protocol, provide the protocol as a string or request object.
  15. * If a protocol is used, a complete URL will be generated using the
  16. * `$_SERVER['HTTP_HOST']` variable.
  17. *
  18. * // Absolute URL path with no host or protocol
  19. * echo URL::base();
  20. *
  21. * // Absolute URL path with host, https protocol and index.php if set
  22. * echo URL::base('https', TRUE);
  23. *
  24. * // Absolute URL path with host and protocol from $request
  25. * echo URL::base($request);
  26. *
  27. * @param mixed $protocol Protocol string, [Request], or boolean
  28. * @param boolean $index Add index file to URL?
  29. * @return string
  30. * @uses Kohana::$index_file
  31. * @uses Request::protocol()
  32. */
  33. public static function base($protocol = NULL, $index = FALSE)
  34. {
  35. // Start with the configured base URL
  36. $base_url = Kohana::$base_url;
  37. if ($protocol === TRUE)
  38. {
  39. // Use the initial request to get the protocol
  40. $protocol = Request::$initial;
  41. }
  42. if ($protocol instanceof Request)
  43. {
  44. // Use the current protocol
  45. $protocol = $protocol->protocol();
  46. }
  47. if ( ! $protocol)
  48. {
  49. // Use the configured default protocol
  50. $protocol = parse_url($base_url, PHP_URL_SCHEME);
  51. }
  52. if ($index === TRUE AND ! empty(Kohana::$index_file))
  53. {
  54. // Add the index file to the URL
  55. $base_url .= Kohana::$index_file.'/';
  56. }
  57. if (is_string($protocol))
  58. {
  59. if ($port = parse_url($base_url, PHP_URL_PORT))
  60. {
  61. // Found a port, make it usable for the URL
  62. $port = ':'.$port;
  63. }
  64. if ($domain = parse_url($base_url, PHP_URL_HOST))
  65. {
  66. // Remove everything but the path from the URL
  67. $base_url = parse_url($base_url, PHP_URL_PATH);
  68. }
  69. else
  70. {
  71. // Attempt to use HTTP_HOST and fallback to SERVER_NAME
  72. $domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  73. }
  74. // Add the protocol and domain to the base URL
  75. $base_url = $protocol.'://'.$domain.$port.$base_url;
  76. }
  77. return $base_url;
  78. }
  79. /**
  80. * Fetches an absolute site URL based on a URI segment.
  81. *
  82. * echo URL::site('foo/bar');
  83. *
  84. * @param string $uri Site URI to convert
  85. * @param mixed $protocol Protocol string or [Request] class to use protocol from
  86. * @param boolean $index Include the index_page in the URL
  87. * @return string
  88. * @uses URL::base
  89. */
  90. public static function site($uri = '', $protocol = NULL, $index = TRUE)
  91. {
  92. // Chop off possible scheme, host, port, user and pass parts
  93. $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
  94. if ( ! UTF8::is_ascii($path))
  95. {
  96. // Encode all non-ASCII characters, as per RFC 1738
  97. $path = preg_replace('~([^/]+)~e', 'rawurlencode("$1")', $path);
  98. }
  99. // Concat the URL
  100. return URL::base($protocol, $index).$path;
  101. }
  102. /**
  103. * Merges the current GET parameters with an array of new or overloaded
  104. * parameters and returns the resulting query string.
  105. *
  106. * // Returns "?sort=title&limit=10" combined with any existing GET values
  107. * $query = URL::query(array('sort' => 'title', 'limit' => 10));
  108. *
  109. * Typically you would use this when you are sorting query results,
  110. * or something similar.
  111. *
  112. * [!!] Parameters with a NULL value are left out.
  113. *
  114. * @param array $params Array of GET parameters
  115. * @param boolean $use_get Include current request GET parameters
  116. * @return string
  117. */
  118. public static function query(array $params = NULL, $use_get = TRUE)
  119. {
  120. if ($use_get)
  121. {
  122. if ($params === NULL)
  123. {
  124. // Use only the current parameters
  125. $params = $_GET;
  126. }
  127. else
  128. {
  129. // Merge the current and new parameters
  130. $params = array_merge($_GET, $params);
  131. }
  132. }
  133. if (empty($params))
  134. {
  135. // No query parameters
  136. return '';
  137. }
  138. // Note: http_build_query returns an empty string for a params array with only NULL values
  139. $query = http_build_query($params, '', '&');
  140. // Don't prepend '?' to an empty string
  141. return ($query === '') ? '' : ('?'.$query);
  142. }
  143. /**
  144. * Convert a phrase to a URL-safe title.
  145. *
  146. * echo URL::title('My Blog Post'); // "my-blog-post"
  147. *
  148. * @param string $title Phrase to convert
  149. * @param string $separator Word separator (any single character)
  150. * @param boolean $ascii_only Transliterate to ASCII?
  151. * @return string
  152. * @uses UTF8::transliterate_to_ascii
  153. */
  154. public static function title($title, $separator = '-', $ascii_only = FALSE)
  155. {
  156. if ($ascii_only === TRUE)
  157. {
  158. // Transliterate non-ASCII characters
  159. $title = UTF8::transliterate_to_ascii($title);
  160. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  161. $title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
  162. }
  163. else
  164. {
  165. // Remove all characters that are not the separator, letters, numbers, or whitespace
  166. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
  167. }
  168. // Replace all separator characters and whitespace by a single separator
  169. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  170. // Trim separators from the beginning and end
  171. return trim($title, $separator);
  172. }
  173. } // End url