redirect.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Send a redirect to the user agent and exist
  5. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  6. */
  7. class Redirect
  8. {
  9. /**
  10. * Returns the result of api_get_path() (a web path to the root of Chamilo)
  11. * @return string
  12. */
  13. public static function www()
  14. {
  15. return api_get_path(WEB_PATH);
  16. }
  17. /**
  18. * Checks whether the given URL contains "http". If not, prepend the web
  19. * root of Chamilo and send the browser there (HTTP redirect)
  20. * @param string $url
  21. */
  22. public static function go($url = '')
  23. {
  24. if (empty($url)) {
  25. self::session_request_uri();
  26. $www = self::www();
  27. self::navigate($www);
  28. }
  29. $is_full_uri = (strpos($url, 'http') === 0);
  30. if ($is_full_uri) {
  31. self::navigate($url);
  32. }
  33. $url = self::www().$url;
  34. self::navigate($url);
  35. }
  36. /**
  37. * Redirect to the current session's "request uri" if it is defined, or
  38. * check sso_referer, user's role and page_after_login settings to send
  39. * the user to some predefined URL
  40. * @param bool Whether the user just logged in (in this case, use page_after_login rules)
  41. * @param int The user_id, if defined. Otherwise just send to where the page_after_login setting says
  42. */
  43. public static function session_request_uri($logging_in = false, $user_id = null)
  44. {
  45. $no_redirection = isset($_SESSION['noredirection']) ? $_SESSION['noredirection'] : false;
  46. if ($no_redirection) {
  47. unset($_SESSION['noredirection']);
  48. return;
  49. }
  50. $url = isset($_SESSION['request_uri']) ? Security::remove_XSS($_SESSION['request_uri']) : '';
  51. unset($_SESSION['request_uri']);
  52. if (!empty($url)) {
  53. self::navigate($url);
  54. } elseif ($logging_in || (isset($_REQUEST['sso_referer']) && !empty($_REQUEST['sso_referer']))) {
  55. if (isset($user_id)) {
  56. // Make sure we use the appropriate role redirection in case one has been defined
  57. $user_status = api_get_user_status($user_id);
  58. switch ($user_status) {
  59. case COURSEMANAGER:
  60. $redir = api_get_setting('teacher_page_after_login');
  61. if (!empty($redir)) {
  62. self::navigate(api_get_path(WEB_PATH).$redir);
  63. }
  64. break;
  65. case STUDENT:
  66. $redir = api_get_setting('student_page_after_login');
  67. if (!empty($redir)) {
  68. self::navigate(api_get_path(WEB_PATH).$redir);
  69. }
  70. break;
  71. case DRH:
  72. $redir = api_get_setting('drh_page_after_login');
  73. if (!empty($redir)) {
  74. self::navigate(api_get_path(WEB_PATH).$redir);
  75. }
  76. break;
  77. case SESSIONADMIN:
  78. $redir = api_get_setting('sessionadmin_page_after_login');
  79. if (!empty($redir)) {
  80. self::navigate(api_get_path(WEB_PATH).$redir);
  81. }
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. $redirect = api_get_setting('redirect_admin_to_courses_list');
  88. if ($redirect !== 'true') {
  89. // If the user is a platform admin, redirect to the main admin page
  90. if (api_is_multiple_url_enabled()) {
  91. // if multiple URLs are enabled, make sure he's admin of the
  92. // current URL before redirecting
  93. $url = api_get_current_access_url_id();
  94. if (api_is_platform_admin_by_id($user_id, $url)) {
  95. self::navigate(api_get_path(WEB_CODE_PATH).'admin/index.php');
  96. }
  97. } else {
  98. // if no multiple URL, then it's enough to be platform admin
  99. if (api_is_platform_admin_by_id($user_id)) {
  100. self::navigate(api_get_path(WEB_CODE_PATH).'admin/index.php');
  101. }
  102. }
  103. }
  104. $page_after_login = api_get_setting('page_after_login');
  105. if (!empty($page_after_login)) {
  106. self::navigate(api_get_path(WEB_PATH).$page_after_login);
  107. }
  108. }
  109. }
  110. /**
  111. * Sends the user to the web root of Chamilo (e.g. http://my.chamiloportal.com/ )
  112. */
  113. public static function home()
  114. {
  115. $www = self::www();
  116. self::navigate($www);
  117. }
  118. /**
  119. * Sends the user to the user_portal.php page
  120. */
  121. public static function user_home()
  122. {
  123. $www = self::www();
  124. self::navigate("$www/user_portal.php");
  125. }
  126. /**
  127. * Redirects the user to a given URL through the header('location: ...') function
  128. * @param string $url
  129. */
  130. protected static function navigate($url)
  131. {
  132. session_write_close(); //should not be needed
  133. header("Location: $url");
  134. exit;
  135. }
  136. }