redirect.class.php 5.9 KB

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