uri.class.php 2.9 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. {
  22. $result = api_get_path(WEB_PATH);
  23. }
  24. return $result;
  25. }
  26. public static function here($params = array(), $html = true)
  27. {
  28. $protocol = Request::server()->server_protocol();
  29. $protocol = stripos($protocol, 'https') !== false ? 'https' : 'http';
  30. $host = Request::server()->server_name();
  31. $host = $host ? $host : Request::server()->server_addr();
  32. $here = Request::server()->request_uri();
  33. $here = explode('?', $here);
  34. $here = reset($here);
  35. $here = $protocol . '://' . $host . $here;
  36. return self::url($here, $params, $html);
  37. }
  38. /**
  39. * Returns a full url from local/absolute path and parameters.
  40. * Append the root as required for relative urls.
  41. *
  42. * @param string $path
  43. * @param array $params
  44. * @return string
  45. */
  46. public static function url($path = '', $params = array(), $html = true)
  47. {
  48. $result = $path;
  49. if (strpos($result, 'http') !== 0)
  50. {
  51. $result = ltrim($result, '/');
  52. $result = self::www() . $result;
  53. }
  54. if ($params)
  55. {
  56. $result = rtrim($result, '?');
  57. $result = $result . '?' . self::params($params, $html);
  58. }
  59. return $result;
  60. }
  61. /**
  62. * Format url parameters
  63. *
  64. * @param array $params
  65. * @return string
  66. */
  67. public static function params($params = array(), $html = true)
  68. {
  69. $result = array();
  70. foreach ($params as $key => $value)
  71. {
  72. $result[] = $key . '=' . urlencode($value);
  73. }
  74. $result = implode($html ? '&amp;' : '&', $result);
  75. return $result;
  76. }
  77. /**
  78. * Returns the course parameters. If null default to the current user parameters.
  79. *
  80. * @param string $course_code
  81. * @param string|int $session_id
  82. * @param string|int $group_id
  83. * @return type
  84. */
  85. public static function course_params($course_code = null, $session_id = null, $group_id = null)
  86. {
  87. $course_code = is_null($course_code) ? api_get_course_id() : $course_code;
  88. $session_id = is_null($session_id) ? api_get_session_id() : $session_id;
  89. $session_id = $session_id ? $session_id : '0';
  90. $group_id = is_null($group_id) ? '' : $group_id;
  91. $group_id = $group_id ? $group_id : '0';
  92. return array('cidReq' => $course_code, 'id_session' => $session_id, 'gidReq' => $group_id);
  93. }
  94. }