uri.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Utility functions to manage uris/urls.
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. */
  8. class Uri
  9. {
  10. public static function chamilo()
  11. {
  12. return 'http://chamilo.org/';
  13. }
  14. /**
  15. * Application web root
  16. */
  17. public static function www()
  18. {
  19. static $result = false;
  20. if (empty($result)) {
  21. $result = api_get_path(WEB_PATH);
  22. }
  23. return $result;
  24. }
  25. public static function here($params = array(), $html = true)
  26. {
  27. $protocol = Request::server()->server_protocol();
  28. $protocol = stripos($protocol, 'https') !== false ? 'https' : 'http';
  29. $host = Request::server()->server_name();
  30. $host = $host ? $host : Request::server()->server_addr();
  31. $here = Request::server()->request_uri();
  32. $here = explode('?', $here);
  33. $here = reset($here);
  34. $here = $protocol . '://' . $host . $here;
  35. return self::url($here, $params, $html);
  36. }
  37. /**
  38. * Returns a full url from local/absolute path and parameters.
  39. * Append the root as required for relative urls.
  40. *
  41. * @param string $path
  42. * @param array $params
  43. * @return string
  44. */
  45. public static function url($path = '', $params = array(), $html = true)
  46. {
  47. $result = $path;
  48. if (strpos($result, 'http') !== 0)
  49. {
  50. $result = ltrim($result, '/');
  51. $result = self::www() . $result;
  52. }
  53. if ($params)
  54. {
  55. $result = rtrim($result, '?');
  56. $result = $result . '?' . self::params($params, $html);
  57. }
  58. return $result;
  59. }
  60. /**
  61. * Format url parameters
  62. *
  63. * @param array $params
  64. * @return string
  65. */
  66. public static function params($params = array(), $html = true)
  67. {
  68. $result = array();
  69. foreach ($params as $key => $value)
  70. {
  71. $result[] = $key . '=' . urlencode($value);
  72. }
  73. $result = implode($html ? '&amp;' : '&', $result);
  74. return $result;
  75. }
  76. /**
  77. * Returns the course parameters. If null default to the current user parameters.
  78. *
  79. * @param string $course_code
  80. * @param string|int $session_id
  81. * @param string|int $group_id
  82. * @return type
  83. */
  84. public static function course_params($course_code = null, $session_id = null, $group_id = null)
  85. {
  86. $course_code = is_null($course_code) ? api_get_course_id() : $course_code;
  87. $session_id = is_null($session_id) ? api_get_session_id() : $session_id;
  88. $session_id = $session_id ? $session_id : '0';
  89. $group_id = is_null($group_id) ? '' : $group_id;
  90. $group_id = $group_id ? $group_id : '0';
  91. return array('cidReq' => $course_code, 'id_session' => $session_id, 'gidReq' => $group_id);
  92. }
  93. }