PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/rxwandc/system/classes/kohana/url.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 213 lines | 94 code | 22 blank | 97 comment | 13 complexity | aa3ad363907df227708b7d7dd94d63ad 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-2012 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. if ( ! $protocol->secure())
  45. {
  46. // Use the current protocol
  47. list($protocol) = explode('/', strtolower($protocol->protocol()));
  48. }
  49. else
  50. {
  51. $protocol = 'https';
  52. }
  53. }
  54. if ( ! $protocol)
  55. {
  56. // Use the configured default protocol
  57. $protocol = parse_url($base_url, PHP_URL_SCHEME);
  58. }
  59. if ($index === TRUE AND ! empty(Kohana::$index_file))
  60. {
  61. // Add the index file to the URL
  62. $base_url .= Kohana::$index_file.'/';
  63. }
  64. if (is_string($protocol))
  65. {
  66. if ($port = parse_url($base_url, PHP_URL_PORT))
  67. {
  68. // Found a port, make it usable for the URL
  69. $port = ':'.$port;
  70. }
  71. if ($domain = parse_url($base_url, PHP_URL_HOST))
  72. {
  73. // Remove everything but the path from the URL
  74. $base_url = parse_url($base_url, PHP_URL_PATH);
  75. }
  76. else
  77. {
  78. // Attempt to use HTTP_HOST and fallback to SERVER_NAME
  79. $domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  80. }
  81. // Add the protocol and domain to the base URL
  82. $base_url = $protocol.'://'.$domain.$port.$base_url;
  83. }
  84. return $base_url;
  85. }
  86. /**
  87. * Fetches an absolute site URL based on a URI segment.
  88. *
  89. * echo URL::site('foo/bar');
  90. *
  91. * @param string $uri Site URI to convert
  92. * @param mixed $protocol Protocol string or [Request] class to use protocol from
  93. * @param boolean $index Include the index_page in the URL
  94. * @return string
  95. * @uses URL::base
  96. */
  97. public static function site($uri = '', $protocol = NULL, $index = TRUE)
  98. {
  99. // Chop off possible scheme, host, port, user and pass parts
  100. $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
  101. if ( ! UTF8::is_ascii($path))
  102. {
  103. // Encode all non-ASCII characters, as per RFC 1738
  104. $path = preg_replace_callback('~([^/]+)~', 'URL::_rawurlencode_callback', $path);
  105. }
  106. // Concat the URL
  107. return URL::base($protocol, $index).$path;
  108. }
  109. /**
  110. * Callback used for encoding all non-ASCII characters, as per RFC 1738
  111. * Used by URL::site()
  112. *
  113. * @param array $matches Array of matches from preg_replace_callback()
  114. * @return string Encoded string
  115. */
  116. protected static function _rawurlencode_callback($matches)
  117. {
  118. return rawurlencode($matches[0]);
  119. }
  120. /**
  121. * Merges the current GET parameters with an array of new or overloaded
  122. * parameters and returns the resulting query string.
  123. *
  124. * // Returns "?sort=title&limit=10" combined with any existing GET values
  125. * $query = URL::query(array('sort' => 'title', 'limit' => 10));
  126. *
  127. * Typically you would use this when you are sorting query results,
  128. * or something similar.
  129. *
  130. * [!!] Parameters with a NULL value are left out.
  131. *
  132. * @param array $params Array of GET parameters
  133. * @param boolean $use_get Include current request GET parameters
  134. * @return string
  135. */
  136. public static function query(array $params = NULL, $use_get = TRUE)
  137. {
  138. if ($use_get)
  139. {
  140. if ($params === NULL)
  141. {
  142. // Use only the current parameters
  143. $params = $_GET;
  144. }
  145. else
  146. {
  147. // Merge the current and new parameters
  148. $params = Arr::merge($_GET, $params);
  149. }
  150. }
  151. if (empty($params))
  152. {
  153. // No query parameters
  154. return '';
  155. }
  156. // Note: http_build_query returns an empty string for a params array with only NULL values
  157. $query = http_build_query($params, '', '&');
  158. // Don't prepend '?' to an empty string
  159. return ($query === '') ? '' : ('?'.$query);
  160. }
  161. /**
  162. * Convert a phrase to a URL-safe title.
  163. *
  164. * echo URL::title('My Blog Post'); // "my-blog-post"
  165. *
  166. * @param string $title Phrase to convert
  167. * @param string $separator Word separator (any single character)
  168. * @param boolean $ascii_only Transliterate to ASCII?
  169. * @return string
  170. * @uses UTF8::transliterate_to_ascii
  171. */
  172. public static function title($title, $separator = '-', $ascii_only = FALSE)
  173. {
  174. if ($ascii_only === TRUE)
  175. {
  176. // Transliterate non-ASCII characters
  177. $title = UTF8::transliterate_to_ascii($title);
  178. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  179. $title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
  180. }
  181. else
  182. {
  183. // Remove all characters that are not the separator, letters, numbers, or whitespace
  184. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
  185. }
  186. // Replace all separator characters and whitespace by a single separator
  187. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  188. // Trim separators from the beginning and end
  189. return trim($title, $separator);
  190. }
  191. } // End url