Passed
Push — master ( 3254a3...a20484 )
by William
02:32
created

logged-in-user.php ➔ ip_is_private()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 3
nop 0
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
function ip_is_private() {
4
    $pri_addrs = array(
5
        '10.0.0.0|10.255.255.255',
6
    );
7
8
    $long_ip = ip2long($_SERVER['REMOTE_ADDR']);
9
    if ($long_ip != -1) {
10
        foreach ($pri_addrs AS $pri_addr) {
11
            list ($start, $end) = explode('|', $pri_addr);
12
13
            // IF IS PRIVATE
14
            if ($long_ip >= ip2long($start) && $long_ip <= ip2long($end)) {
15
                return true;
16
            }
17
        }
18
    }
19
    return false;
20
}
21
22
$u = new User();
23
global $u;
24
25
if ($u->isLoggedIn()) {
26
    echo '<p class="welcome-username">Welcome ' . $u->getUserName() . '</p>';
27
} else {
28
    echo '<p class="welcome-username"><a href="/login">Click here to Login</a></p>';
29
30
    $page = Page::getCurrentPage();
31
32
    if ($page->getCollectionName() == 'Home') {
33
34
        $ip = $_SERVER['REMOTE_ADDR'];
35
        if (ip_is_private() === FALSE) {
36
            header('Location: /login');
37
            die();
38
        }
39
    }
40
}
41
if ($u->inGroup(Group::getByName('Students'))) {
42
    header('Location: /dashboard/permissions');
43
    die();
44
}
45
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...