PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url.php

https://github.com/lmorchard/friendfeedarchiver
PHP | 147 lines | 74 code | 23 blank | 50 comment | 10 complexity | a47a67550a5a356d7039753487bd6914 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * URL helper class.
  4. *
  5. * $Id: url.php 2020 2008-02-10 10:41:19Z Geert $
  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. * Base URL, with or without the index page.
  15. *
  16. * @param boolean include the index page
  17. * @param boolean non-default protocol
  18. * @return string
  19. */
  20. public static function base($index = FALSE, $protocol = FALSE)
  21. {
  22. $protocol = ($protocol == FALSE) ? Config::item('core.site_protocol') : strtolower($protocol);
  23. $base_url = $protocol.'://'.Config::item('core.site_domain', TRUE);
  24. if ($index == TRUE AND $index = Config::item('core.index_page'))
  25. {
  26. $base_url = $base_url.$index.'/';
  27. }
  28. return $base_url;
  29. }
  30. /**
  31. * Fetches a site URL based on a URI segment.
  32. *
  33. * @param string site URI to convert
  34. * @param string non-default protocol
  35. * @return string
  36. */
  37. public static function site($uri = '', $protocol = FALSE)
  38. {
  39. $uri = trim($uri, '/');
  40. $qs = ''; // anchor?query=string
  41. $id = ''; // anchor#id
  42. if (strpos($uri, '?') !== FALSE)
  43. {
  44. list ($uri, $qs) = explode('?', $uri, 2);
  45. $qs = '?'.$qs;
  46. }
  47. if (strpos($uri, '#') !== FALSE)
  48. {
  49. list ($uri, $id) = explode('#', $uri, 2);
  50. $id = '#'.$id;
  51. }
  52. $index_page = Config::item('core.index_page', TRUE);
  53. $url_suffix = ($uri != '') ? Config::item('core.url_suffix') : '';
  54. return url::base(FALSE, $protocol).$index_page.$uri.$url_suffix.$qs.$id;
  55. }
  56. /**
  57. * Fetches the current URI.
  58. *
  59. * @param boolean include the query string
  60. * @return string
  61. */
  62. public static function current($qs = FALSE)
  63. {
  64. return Router::$current_uri.($qs === TRUE ? Router::$query_string : '');
  65. }
  66. /**
  67. * Convert a phrase to a URL-safe title.
  68. *
  69. * @param string phrase to convert
  70. * @param string word separator (- or _)
  71. * @return string
  72. */
  73. public static function title($title, $separator = '-')
  74. {
  75. $separator = ($separator === '-') ? '-' : '_';
  76. // Replace accented characters by their unaccented equivalents
  77. $title = utf8::transliterate_to_ascii($title);
  78. // Remove all characters that are not the separator, a-z, 0-9, or whitespace
  79. $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
  80. // Replace all separator characters and whitespace by a single separator
  81. $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
  82. // Trim separators from the beginning and end
  83. return trim($title, $separator);
  84. }
  85. /**
  86. * Sends a page redirect header.
  87. *
  88. * @param string site URI or URL to redirect to
  89. * @param string HTTP method of redirect
  90. * @return A HTML anchor, but sends HTTP headers. The anchor should never be seen
  91. * by the user, unless their browser does not understand the headers sent.
  92. */
  93. public static function redirect($uri = '', $method = '302')
  94. {
  95. if (Event::has_run('system.send_headers'))
  96. return;
  97. if (strpos($uri, '://') === FALSE)
  98. {
  99. $uri = url::site($uri);
  100. }
  101. if ($method == 'refresh')
  102. {
  103. header('Refresh: 0; url='.$uri);
  104. }
  105. else
  106. {
  107. $codes = array
  108. (
  109. '300' => 'Multiple Choices',
  110. '301' => 'Moved Permanently',
  111. '302' => 'Found',
  112. '303' => 'See Other',
  113. '304' => 'Not Modified',
  114. '305' => 'Use Proxy',
  115. '307' => 'Temporary Redirect'
  116. );
  117. $method = isset($codes[$method]) ? $method : '302';
  118. header('HTTP/1.1 '.$method.' '.$codes[$method]);
  119. header('Location: '.$uri);
  120. }
  121. // Last resort, exit and display the URL
  122. exit('<a href="'.$uri.'">'.$uri.'</a>');
  123. }
  124. } // End url