redirect.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Send a redirect to the user agent and exist
  4. *
  5. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  6. * @license see /license.txt
  7. *
  8. * @todo use the LoginSuccessHandler class
  9. */
  10. class Redirect {
  11. static function www() {
  12. return Uri::www();
  13. }
  14. static function go($url = '') {
  15. if (empty($url)) {
  16. Redirect::session_request_uri();
  17. $www = self::www();
  18. self::navigate($www);
  19. }
  20. $is_full_uri = (strpos($url, 'http') === 0);
  21. if ($is_full_uri) {
  22. self::navigate($url);
  23. }
  24. $url = self::www() . $url;
  25. self::navigate($url);
  26. }
  27. /**
  28. * Redirect to the session "request uri" if it exists.
  29. * @param bool Whether the user just logged in (in this case, use page_after_login rules)
  30. */
  31. static function session_request_uri($logging_in = false, $user_id = null) {
  32. $no_redirection = isset($_SESSION['noredirection']) ? $_SESSION['noredirection'] : false;
  33. if ($no_redirection) {
  34. unset($_SESSION['noredirection']);
  35. return;
  36. }
  37. $url = isset($_SESSION['request_uri']) ? $_SESSION['request_uri'] : '';
  38. unset($_SESSION['request_uri']);
  39. if (!empty($url)) {
  40. self::navigate($url);
  41. } elseif ($logging_in || (isset($_REQUEST['sso_referer']) && !empty($_REQUEST['sso_referer']))) {
  42. if (isset($user_id)) {
  43. // Make sure we use the appropriate role redirection in case one has been defined
  44. $user_status = api_get_user_status($user_id);
  45. switch ($user_status) {
  46. case COURSEMANAGER:
  47. $redir = api_get_setting('teacher_page_after_login');
  48. if (!empty($redir)) {
  49. self::navigate(api_get_path(WEB_PATH) . $redir);
  50. }
  51. break;
  52. case STUDENT:
  53. $redir = api_get_setting('student_page_after_login');
  54. if (!empty($redir)) {
  55. self::navigate(api_get_path(WEB_PATH) . $redir);
  56. }
  57. break;
  58. case DRH:
  59. $redir = api_get_setting('drh_page_after_login');
  60. if (!empty($redir)) {
  61. self::navigate(api_get_path(WEB_PATH) . $redir);
  62. }
  63. break;
  64. case SESSIONADMIN:
  65. $redir = api_get_setting('sessionadmin_page_after_login');
  66. if (!empty($redir)) {
  67. self::navigate(api_get_path(WEB_PATH) . $redir);
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. $page_after_login = api_get_setting('page_after_login');
  75. if (!empty($page_after_login)) {
  76. self::navigate(api_get_path(WEB_PATH) . $page_after_login);
  77. }
  78. }
  79. }
  80. static function home() {
  81. $www = self::www();
  82. self::navigate($www);
  83. }
  84. static function user_home() {
  85. $www = self::www();
  86. self::navigate("$www/user_portal.php");
  87. }
  88. protected static function navigate($url) {
  89. session_write_close(); //should not be neeeded
  90. header("Location: $url");
  91. exit;
  92. }
  93. }