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

namespace qfq;


class Session {
    private static $instance = null;
    private static $phpUnit = null;
    private static $sessionLocal = array();

    /**
     * @param bool|false $phpUnit
Carsten  Rose's avatar
Carsten Rose committed
19
     * @throws CodeException
20
21
22
23
24
25
26
     */
    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;

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

    /**
37
     *
38
     */
39
    private static function checkFeUserUid() {
40

41
        $feUidSession = Session::get(SESSION_FE_USER_UID);
42

Carsten  Rose's avatar
Carsten Rose committed
43
44
45
46
47
48
49
50
        if (isset($GLOBALS["TSFE"])) {
            $feUidLoggedIn = isset($GLOBALS["TSFE"]->fe_user->user["uid"]) ? $GLOBALS["TSFE"]->fe_user->user["uid"] : false;
        } else {
            // If we are called through API there is no T3 FE User logged in. Fake this test.
            $feUidLoggedIn = $feUidSession;
        }


51
52
53
54
55
56
57
        if ($feUidLoggedIn !== $feUidSession) {
            // destroy existing session store
            Session::clear();

            // save new feUserUid
            Session::set(SESSION_FE_USER_UID, $feUidLoggedIn);
        }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    }

    /**
     * @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 {
72
73
            if (isset($_SESSION[SESSION_NAME][$key]))
                $value = $_SESSION[SESSION_NAME][$key];
74
75
76
77
78
79
80
81
            else
                $value = false;

        }

        return $value;
    }

82
83
84
85
86
87
88
89
90
91
92
93
    /**
     *
     */
    public static function clear() {

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

94
95
96
97
98
99
100
101
102
    /**
     * @param $key
     * @param $value
     */
    public static function set($key, $value) {

        if (self::$phpUnit) {
            self::$sessionLocal[$key] = $value;
        } else {
103
            $_SESSION[SESSION_NAME][$key] = $value;
104
105
106
107
        }
    }

    /**
108
109
     * @param bool|false $phpUnit
     * @return null|\qfq\Store
110
     */
111
    public static function getInstance($phpUnit = false) {
112

113
114
115
        // Design Pattern: Singleton
        if (self::$instance === null) {
            self::$instance = new self($phpUnit);
116
        }
117
118

        return self::$instance;
119
120
121
    }

}