Redirect::www()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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