redirect.class.php 3.5 KB

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