GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#889)
by Kris
01:05
created

Session   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 132
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A set() 0 4 1
A get() 0 9 2
A add() 0 4 1
A destroy() 0 4 1
A updateSessionId() 0 8 1
A isSessionBroken() 0 21 5
A userIsLoggedIn() 0 4 2
1
<?php
2
3
/**
4
 * Session class
5
 *
6
 * handles the session stuff. creates session when no one exists, sets and gets values, and closes the session
7
 * properly (=logout). Not to forget the check if the user is logged in or not.
8
 */
9
class Session
10
{
11
    /**
12
     * starts the session
13
     */
14
    public static function init()
15
    {
16
        // if no session exist, start the session
17
        if (session_id() == '') {
18
            session_start();
19
        }
20
    }
21
22
    /**
23
     * sets a specific value to a specific key of the session
24
     *
25
     * @param mixed $key key
26
     * @param mixed $value value
27
     */
28
    public static function set($key, $value)
29
    {
30
        $_SESSION[$key] = $value;
31
    }
32
33
    /**
34
     * gets/returns the value of a specific key of the session
35
     *
36
     * @param mixed $key Usually a string, right ?
37
     * @return mixed the key's value or nothing
38
     */
39
    public static function get($key)
40
    {
41
        if (isset($_SESSION[$key])) {
42
            $value = $_SESSION[$key];
43
44
            // filter the value for XSS vulnerabilities
45
            return Filter::XSSFilter($value);
46
        }
47
    }
48
49
    /**
50
     * adds a value as a new array element to the key.
51
     * useful for collecting error messages etc
52
     *
53
     * @param mixed $key
54
     * @param mixed $value
55
     */
56
    public static function add($key, $value)
57
    {
58
        $_SESSION[$key][] = $value;
59
    }
60
61
    /**
62
     * deletes the session (= logs the user out)
63
     */
64
    public static function destroy()
65
    {
66
        session_destroy();
67
    }
68
69
    /**
70
     * update session id in database
71
     *
72
     * @access public
73
     * @static static method
74
     * @param  string $userId
75
     * @param  string $sessionId
76
     */
77
    public static function updateSessionId($userId, $sessionId = null)
78
    {
79
        $database = DatabaseFactory::getFactory()->getConnection();
80
        $sql = "UPDATE users SET session_id = :session_id WHERE user_id = :user_id";
81
82
        $query = $database->prepare($sql);
83
        $query->execute(array(':session_id' => $sessionId, ":user_id" => $userId));
84
    }
85
86
    /**
87
     * checks for broken session 
88
     * Session could be broken by Session concurrency or when user is deleted / suspended
89
     * 
90
     * - Session concurrency is done as the following:
91
     * This is done as the following:
92
     * UserA logs in with his session id('123') and it will be stored in the database.
93
     * Then, UserB logs in also using the same email and password of UserA from another PC,
94
     * and also store the session id('456') in the database
95
     *
96
     * Now, Whenever UserA performs any action,
97
     * You then check the session_id() against the last one stored in the database('456'),
98
     * If they don't match then log both of them out.
99
     * 
100
     * - Check for deleted / suspended users:
101
     * Suspended/deleted users have no userSessionId anymore stored in database
102
     *
103
     * @access public
104
     * @static static method
105
     * @return bool
106
     * @see Session::updateSessionId()
107
     * @see http://stackoverflow.com/questions/6126285/php-stop-concurrent-user-logins
108
     */
109
    public static function isSessionBroken()
110
    {
111
        $session_id = session_id();
112
        $userId     = Session::get('user_id');
113
114
        if (isset($userId) && isset($session_id)) {
115
116
            $database = DatabaseFactory::getFactory()->getConnection();
117
            $sql = "SELECT session_id FROM users WHERE user_id = :user_id LIMIT 1";
118
119
            $query = $database->prepare($sql);
120
            $query->execute(array(":user_id" => $userId));
121
122
            $result = $query->fetch();
123
            $userSessionId = !empty($result)? $result->session_id: null;
124
125
            return empty($userSessionId) || $session_id !== $userSessionId;
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * Checks if the user is logged in or not
133
     *
134
     * @return bool user's login status
135
     */
136
    public static function userIsLoggedIn()
137
    {
138
        return (self::get('user_logged_in') ? true : false);
139
    }
140
}
141