redirect.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * @deprecated
  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. Redirect::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. if (!empty($url)) {
  54. self::navigate($url);
  55. } elseif ($logging_in || (isset($_REQUEST['sso_referer']) && !empty($_REQUEST['sso_referer']))) {
  56. if (isset($user_id)) {
  57. // Make sure we use the appropriate role redirection in case one has been defined
  58. $user_status = api_get_user_status($user_id);
  59. switch ($user_status) {
  60. case COURSEMANAGER:
  61. $redir = api_get_setting('teacher_page_after_login');
  62. if (!empty($redir)) {
  63. self::navigate(api_get_path(WEB_PATH) . $redir);
  64. }
  65. break;
  66. case STUDENT:
  67. $redir = api_get_setting('student_page_after_login');
  68. if (!empty($redir)) {
  69. self::navigate(api_get_path(WEB_PATH) . $redir);
  70. }
  71. break;
  72. case DRH:
  73. $redir = api_get_setting('drh_page_after_login');
  74. if (!empty($redir)) {
  75. self::navigate(api_get_path(WEB_PATH) . $redir);
  76. }
  77. break;
  78. case SESSIONADMIN:
  79. $redir = api_get_setting('sessionadmin_page_after_login');
  80. if (!empty($redir)) {
  81. self::navigate(api_get_path(WEB_PATH) . $redir);
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. $redirect = api_get_setting('admin.redirect_admin_to_courses_list');
  89. if ($redirect !== 'true') {
  90. // If the user is a platform admin, redirect to the main admin page
  91. if (api_is_multiple_url_enabled()) {
  92. // if multiple URLs are enabled, make sure he's admin of the
  93. // current URL before redirecting
  94. $url = api_get_current_access_url_id();
  95. if (api_is_platform_admin_by_id($user_id, $url)) {
  96. self::navigate(api_get_path(WEB_CODE_PATH).'admin/index.php');
  97. }
  98. } else {
  99. // if no multiple URL, then it's enough to be platform admin
  100. if (api_is_platform_admin_by_id($user_id)) {
  101. self::navigate(api_get_path(WEB_CODE_PATH).'admin/index.php');
  102. }
  103. }
  104. }
  105. $page_after_login = api_get_setting('page_after_login');
  106. if (!empty($page_after_login)) {
  107. self::navigate(api_get_path(WEB_PATH) . $page_after_login);
  108. }
  109. }
  110. }
  111. /**
  112. * Sends the user to the web root of Chamilo (e.g. http://my.chamiloportal.com/ )
  113. */
  114. public static function home()
  115. {
  116. $www = self::www();
  117. self::navigate($www);
  118. }
  119. /**
  120. * Sends the user to the user_portal.php page
  121. */
  122. public static function user_home()
  123. {
  124. $www = self::www();
  125. self::navigate("$www/user_portal.php");
  126. }
  127. /**
  128. * Redirects the user to a given URL through the header('location: ...') function
  129. * @param string $url
  130. */
  131. protected static function navigate($url)
  132. {
  133. session_write_close(); //should not be needed
  134. header("Location: $url");
  135. exit;
  136. }
  137. }