/system/classes/kohana/url.php

https://code.google.com/p/php-blackops-rcon/ · PHP · 175 lines · 72 code · 18 blank · 85 comment · 8 complexity · 89b7595300c83ed623d2ce3a5e1821a3 MD5 · raw file

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