Redirect::session_request_uri()   F
last analyzed

Complexity

Conditions 26
Paths 1538

Size

Total Lines 82
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 55
nc 1538
nop 2
dl 0
loc 82
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, $userInfo = null)
55
    {
56
        $no_redirection = isset($_SESSION['noredirection']) ? $_SESSION['noredirection'] : false;
57
58
        if ($no_redirection) {
59
            unset($_SESSION['noredirection']);
60
61
            return;
62
        }
63
64
        $url = isset($_SESSION['request_uri']) ? Security::remove_XSS($_SESSION['request_uri']) : '';
65
        unset($_SESSION['request_uri']);
66
67
        $afterLogin = Session::read('redirect_after_not_allow_page');
68
69
        if (!empty($afterLogin) && isset($_GET['redirect_after_not_allow_page'])) {
70
            Session::erase('redirect_after_not_allow_page');
71
            self::navigate($afterLogin);
72
        }
73
        if (!empty($url)) {
74
            self::navigate($url);
75
        } elseif ($logging_in ||
76
            (isset($_REQUEST['sso_referer']) && !empty($_REQUEST['sso_referer']))
77
        ) {
78
            if (isset($userInfo) && !empty($userInfo)) {
79
                $userId = $userInfo['user_id'];
80
                $allow = ('true' === api_get_setting('workflows.plugin_redirection_enabled'));
81
                if ($allow) {
82
                    RedirectionPlugin::redirectUser($userId);
83
                }
84
85
                // Make sure we use the appropriate role redirection in case one has been defined
86
                $user_status = $userInfo['status'];
87
                switch ($user_status) {
88
                    case COURSEMANAGER:
89
                        $redir = api_get_setting('teacher_page_after_login');
90
                        if (!empty($redir)) {
91
                            self::navigate(api_get_path(WEB_PATH).$redir);
0 ignored issues
show
Bug introduced by
Are you sure $redir of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                            self::navigate(api_get_path(WEB_PATH)./** @scrutinizer ignore-type */ $redir);
Loading history...
92
                        }
93
                        break;
94
                    case STUDENT:
95
                        $redir = api_get_setting('student_page_after_login');
96
                        if (!empty($redir)) {
97
                            self::navigate(api_get_path(WEB_PATH).$redir);
98
                        }
99
                        break;
100
                    case DRH:
101
                        $redir = api_get_setting('drh_page_after_login');
102
                        if (!empty($redir)) {
103
                            self::navigate(api_get_path(WEB_PATH).$redir);
104
                        }
105
                        break;
106
                    case SESSIONADMIN:
107
                        $redir = api_get_setting('sessionadmin_page_after_login');
108
                        if (!empty($redir)) {
109
                            self::navigate(api_get_path(WEB_PATH).$redir);
110
                        }
111
                        break;
112
                    default:
113
                        break;
114
                }
115
            }
116
            $redirect = api_get_setting('redirect_admin_to_courses_list');
117
            if ('true' !== $redirect) {
118
                // If the user is a platform admin, redirect to the main admin page
119
                if (api_is_multiple_url_enabled()) {
0 ignored issues
show
Deprecated Code introduced by
The function api_is_multiple_url_enabled() has been deprecated: Use AccessUrlUtil::isMultiple ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
                if (/** @scrutinizer ignore-deprecated */ api_is_multiple_url_enabled()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
                    // if multiple URLs are enabled, make sure he's admin of the
121
                    // current URL before redirecting
122
                    $url = api_get_current_access_url_id();
123
                    if (api_is_platform_admin_by_id($userId, $url)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userId does not seem to be defined for all execution paths leading up to this point.
Loading history...
124
                        self::navigate(api_get_path(WEB_CODE_PATH).'admin/index.php');
125
                    }
126
                } else {
127
                    // if no multiple URL, then it's enough to be platform admin
128
                    if (api_is_platform_admin_by_id($userId)) {
129
                        self::navigate(api_get_path(WEB_CODE_PATH).'admin/index.php');
130
                    }
131
                }
132
            }
133
            $page_after_login = api_get_setting('page_after_login');
134
            if (!empty($page_after_login)) {
135
                self::navigate(api_get_path(WEB_PATH).$page_after_login);
0 ignored issues
show
Bug introduced by
Are you sure $page_after_login of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
                self::navigate(api_get_path(WEB_PATH)./** @scrutinizer ignore-type */ $page_after_login);
Loading history...
136
            }
137
        }
138
    }
139
140
    /**
141
     * Sends the user to the web root of Chamilo (e.g. http://my.chamiloportal.com/ ).
142
     */
143
    public static function home()
144
    {
145
        $www = self::www();
146
        self::navigate($www);
147
    }
148
149
    /**
150
     * Sends the user to the user_portal.php page.
151
     */
152
    public static function user_home()
153
    {
154
        $www = self::www();
155
        self::navigate("$www/user_portal.php");
156
    }
157
158
    /**
159
     * Redirects the user to a given URL through the header('location: ...') function.
160
     *
161
     * @param string $url
162
     */
163
    protected static function navigate($url)
164
    {
165
        session_write_close(); //should not be needed
166
        header("Location: $url");
167
        exit;
168
    }
169
}
170