PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/kohana/url.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 176 lines | 71 code | 20 blank | 85 comment | 9 complexity | d81ccf8194109daf25877509a5416ed8 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 = FALSE)
  34. {
  35. if ($protocol === TRUE)
  36. {
  37. // Use the current protocol
  38. $protocol = Request::$protocol;
  39. }
  40. // Start with the configured base URL
  41. $base_url = Kohana::$base_url;
  42. if ($index === TRUE AND ! empty(Kohana::$index_file))
  43. {
  44. // Add the index file to the URL
  45. $base_url .= Kohana::$index_file.'/';
  46. }
  47. if (is_string($protocol))
  48. {
  49. if (parse_url($base_url, PHP_URL_HOST))
  50. {
  51. // Remove everything but the path from the URL
  52. $base_url = parse_url($base_url, PHP_URL_PATH);
  53. }
  54. // Add the protocol and domain to the base URL
  55. $base_url = $protocol.'://'.$_SERVER['HTTP_HOST'].$base_url;
  56. }
  57. return $base_url;
  58. }
  59. /**
  60. * Fetches an absolute site URL based on a URI segment.
  61. *
  62. * echo URL::site('foo/bar');
  63. *
  64. * @param string site URI to convert
  65. * @param mixed protocol string or boolean, add protocol and domain?
  66. * @return string
  67. * @uses URL::base
  68. */
  69. public static function site($uri = '', $protocol = FALSE)
  70. {
  71. // Get the path from the URI
  72. $path = trim(parse_url($uri, PHP_URL_PATH), '/');
  73. if ($query = parse_url($uri, PHP_URL_QUERY))
  74. {
  75. // ?query=string
  76. $query = '?'.$query;
  77. }
  78. if ($fragment = parse_url($uri, PHP_URL_FRAGMENT))
  79. {
  80. // #fragment
  81. $fragment = '#'.$fragment;
  82. }
  83. // Concat the URL
  84. return URL::base(TRUE, $protocol).$path.$query.$fragment;
  85. }
  86. /**
  87. * Merges the current GET parameters with an array of new or overloaded
  88. * parameters and returns the resulting query string.
  89. *
  90. * // Returns "?sort=title&limit=10" combined with any existing GET values
  91. * $query = URL::query(array('sort' => 'title', 'limit' => 10));
  92. *
  93. * Typically you would use this when you are sorting query results,
  94. * or something similar.
  95. *
  96. * [!!] Parameters with a NULL value are left out.
  97. *
  98. * @param array array of GET parameters
  99. * @return string
  100. */
  101. public static function query(array $params = NULL)
  102. {
  103. if ($params === NULL)
  104. {
  105. // Use only the current parameters
  106. $params = $_GET;
  107. }
  108. else
  109. {
  110. // Merge the current and new parameters
  111. $params = array_merge($_GET, $params);
  112. }
  113. if (empty($params))
  114. {
  115. // No query parameters
  116. return '';
  117. }
  118. $query = http_build_query($params, '', '&');
  119. // Don't prepend '?' to an empty string
  120. return ($query === '') ? '' : '?'.$query;
  121. }
  122. /**
  123. * Convert a phrase to a URL-safe title.
  124. *
  125. * echo URL::title('My Blog Post'); // "my-blog-post"
  126. *
  127. * @param string phrase to convert
  128. * @param string word separator (any single character)
  129. * @param boolean transliterate to ASCII?
  130. * @return string
  131. * @uses UTF8::transliterate_to_ascii
  132. */
  133. public static function title($title, $separator = '-', $ascii_only = FALSE)
  134. {
  135. if ($ascii_only === TRUE)
  136. {
  137. // Transliterate non-ASCII characters
  138. $title = UTF8::transliterate_to_ascii($title);
  139. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  140. $title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
  141. }
  142. else
  143. {
  144. // Remove all characters that are not the separator, letters, numbers, or whitespace
  145. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
  146. }
  147. // Replace all separator characters and whitespace by a single separator
  148. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  149. // Trim separators from the beginning and end
  150. return trim($title, $separator);
  151. }
  152. final private function __construct()
  153. {
  154. // This is a static class
  155. }
  156. } // End url