redirect.class.php 5.5 KB

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