Session.php 4.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 5/4/16
 * Time: 1:34 PM
 */

namespace qfq;


class Session {
13

14
15
16
17
18
19
    private static $instance = null;
    private static $phpUnit = null;
    private static $sessionLocal = array();

    /**
     * @param bool|false $phpUnit
Carsten  Rose's avatar
Carsten Rose committed
20
     * @throws CodeException
21
22
23
24
25
26
27
     */
    private function __construct($phpUnit = false) {
        if (self::$phpUnit !== null)
            throw new CodeException("Try to set flag phpunit again - that should not happen.", ERROR_CODE_SHOULD_NOT_HAPPEN);

        self::$phpUnit = $phpUnit;

28
        if (self::$phpUnit === true) {
29
30
            self::$sessionLocal = array();
        } else {
31
            session_name(SESSION_NAME);
32
33
            session_start();
        }
34
        self::checkFeUserUid();
35
36
37
    }

    /**
38
39
40
41
42
43
44
     * Check if the feUserUid is stored in the session (even with 'false' which indicates not logged in user).
     *   If not, clear the session and save the feUser, feUserUid in the session.
     * Check if the recent logged in feUserUid is equal to the one stored in session: If different, invalidate (clear) the session and
     *   save the new feUser, feUserUid in the session.
     * If isset($GLOBALS["TSFE"]), than we're in a T3 environment, else we are called as API classes and need to fake
     *   feUser / feUserUid from previous stored session.
     * It's neccessary to have feUser / feUserUid available in API classes, due to dynamic update which might reload data based on feUser / feUserUid.
45
     */
46
    private static function checkFeUserUid() {
47

48
49
        $feUserUidSession = Session::get(SESSION_FE_USER_UID);
        $feUserSession = Session::get(SESSION_FE_USER);
50
        $feUserGroup = false;
51

Carsten  Rose's avatar
Carsten Rose committed
52
        if (isset($GLOBALS["TSFE"])) {
53
            // if noone is logged in: 0
Carsten  Rose's avatar
Carsten Rose committed
54
            $feUidLoggedIn = isset($GLOBALS["TSFE"]->fe_user->user["uid"]) ? $GLOBALS["TSFE"]->fe_user->user["uid"] : false;
55
            $feUserSession = isset($GLOBALS["TSFE"]->fe_user->user["username"]) ? $GLOBALS["TSFE"]->fe_user->user["username"] : false;
56
            $feUserGroup = isset($GLOBALS["TSFE"]->fe_user->user["usergroup"]) ? $GLOBALS["TSFE"]->fe_user->user["usergroup"] : false;
Carsten  Rose's avatar
Carsten Rose committed
57
        } else {
58
59
            // If we are called through API there is no T3 environment. Assume nothing has changed, and fake the following check to always 'no change'.
            $feUidLoggedIn = $feUserUidSession;
Carsten  Rose's avatar
Carsten Rose committed
60
61
        }

62
        if ($feUidLoggedIn !== $feUserUidSession) {
63
            // destroy existing session store
Carsten  Rose's avatar
#2067    
Carsten Rose committed
64
            Session::clearAll();
65

66
            // save new feUserUid, feUserName
67
            Session::set(SESSION_FE_USER_UID, $feUidLoggedIn);
68
            Session::set(SESSION_FE_USER, $feUserSession);
69
            Session::set(SESSION_FE_USER_GROUP, $feUserGroup);
70
71
72

//            throw new UserFormException("FYI: Session has been cleared. Reload this page. ".
//                "feUserUidSession:'$feUserUidSession',  feUserSession:'$feUserSession' isset(TSFE):'" . isset($GLOBALS["TSFE"]) ? 'true' : 'false' );
73
        }
74
75
76
    }

    /**
77
78
79
     * Return content to given $key (=SIP).
     * Return 'false' if not found.
     *
80
81
82
83
84
85
86
87
88
89
90
     * @param $key
     * @return bool
     */
    public static function get($key) {

        if (self::$phpUnit) {
            if (isset(self::$sessionLocal[$key]))
                $value = self::$sessionLocal[$key];
            else
                $value = false;
        } else {
91
92
            if (isset($_SESSION[SESSION_NAME][$key]))
                $value = $_SESSION[SESSION_NAME][$key];
93
94
95
96
97
98
99
100
            else
                $value = false;

        }

        return $value;
    }

101
102
103
    /**
     *
     */
Carsten  Rose's avatar
#2067    
Carsten Rose committed
104
    public static function clearAll() {
105
106
107
108
109
110
111
112

        if (self::$phpUnit) {
            self::$sessionLocal = array();
        } else {
            $_SESSION[SESSION_NAME] = array();
        }
    }

113
114
115
116
117
118
119
120
121
    /**
     * @param $key
     * @param $value
     */
    public static function set($key, $value) {

        if (self::$phpUnit) {
            self::$sessionLocal[$key] = $value;
        } else {
122
            $_SESSION[SESSION_NAME][$key] = $value;
123
124
125
        }
    }

Carsten  Rose's avatar
#2067    
Carsten Rose committed
126
127
128
129
130
131
132
133
134
135
136
137
138
    /**
     * Unset the given $key
     *
     * @param $key
     */
    public static function unsetItem($key) {

        if (isset($_SESSION[SESSION_NAME][$key])) {
            unset($_SESSION[SESSION_NAME][$key]);
        }

    }

139
    /**
140
     * @param bool|false $phpUnit
141
     * @return Session class
142
     */
143
    public static function getInstance($phpUnit = false) {
144

145
146
147
        // Design Pattern: Singleton
        if (self::$instance === null) {
            self::$instance = new self($phpUnit);
148
        }
149
150

        return self::$instance;
151
152
153
    }

}