Subversion Repositories cheapmusic

Rev

Rev 28 | Rev 35 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 - 1
<?php
2
class MySessionHandler implements SessionHandlerInterface {
3
    private static $instance = null;
4
    private static $sessionTab = 0;
5
    private static $_sess_db = null;
6
    private static $db_user = null;
7
    private static $db_pass = null;
8
    private static $db_name = null;
9
    private static $db_host = null;
10
 
11
    public static function getInstance($set_tab = null, $mysqlConfig = null){
12
        if (is_null(self::$instance)){
13
            self::$instance = new self();
14
        }
15
 
31 - 16
        if ($set_tab && !is_null($set_tab) && ($set_tab > 0 || $set_tab == 'login')) {
7 - 17
            self::setSessionTab($set_tab);
18
        } else {
19
            self::setSessionTab();
20
        }
21
 
22
        // store mysqli configuration
23
        if (!is_null($mysqlConfig)) {
24
            if (isset($mysqlConfig['db_user']) && !empty($mysqlConfig['db_user'])) {
25
                self::$db_user = $mysqlConfig['db_user'];
26
            } else {
27
                throw new \Exception("MySessionHandler:: - db_user not set in configuration");
28
            }
29
 
30
            if (isset($mysqlConfig['db_pass']) && !empty($mysqlConfig['db_pass'])) {
31
                self::$db_pass = $mysqlConfig['db_pass'];
32
            } else {
33
                throw new \Exception("MySessionHandler:: - db_pass not set in configuration");
34
            }
35
 
36
            if (isset($mysqlConfig['db_name']) && !empty($mysqlConfig['db_name'])) {
37
                self::$db_name = $mysqlConfig['db_name'];
38
            } else {
39
                throw new \Exception("MySessionHandler:: - db_name not set in configuration");
40
            }
41
 
42
            if (isset($mysqlConfig['db_host']) && !empty($mysqlConfig['db_host'])) {
43
                self::$db_host = $mysqlConfig['db_host'];
44
            } else {
45
                throw new \Exception("MySessionHandler:: - db_host not set in configuration");
46
            }
47
        }
48
 
49
        return self::$instance;
50
    }
51
 
8 - 52
    public static function getDBSessionId(){
53
        return self::$_sess_db;
54
    }
55
 
7 - 56
    public static function getSessionTab(){
57
        return self::$sessionTab;
58
    }
59
 
60
    public static function setSessionTab($tab = 0){
9 - 61
        self::$sessionTab = $tab ? (int)$tab : (int)rand(1, pow(10, 9) - 1);
7 - 62
    }
63
 
31 - 64
    public function open($savePath = null, $sessionName = null)
7 - 65
    {
66
        if(is_null(self::$sessionTab)){
67
            self::setSessionTab();
68
        }
69
 
70
        self::$_sess_db = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name);
71
 
72
        if (mysqli_connect_errno())
73
        {
20 - 74
            error_log("Failed to connect to MySQL: " . mysqli_connect_error());
7 - 75
            return false;
76
        }
77
 
78
        return true;
79
    }
80
 
81
    public function close()
82
    {
20 - 83
       return mysqli_close(self::$_sess_db);
7 - 84
    }
85
 
86
    public function read($id)
87
    {
88
        $id = mysqli_real_escape_string(self::$_sess_db, $id);
89
 
90
        $sql = "SELECT data
91
                FROM   sessions
20 - 92
                WHERE  id = '$id' and tab = '" . self::getSessionTab() . "'";
7 - 93
 
31 - 94
        $_SESSION = [];
95
 
7 - 96
        if ($result = mysqli_query(self::$_sess_db, $sql))
97
        {
98
            if (mysqli_num_rows($result))
14 - 99
            {
7 - 100
                $record = mysqli_fetch_assoc($result);
101
                $decrypted = Cryptor::Decrypt($record['data']);
20 - 102
                $_SESSION = json_decode($decrypted, true);
7 - 103
            }
20 - 104
        } else {
105
            error_log("MySQL Read Session Error: " . mysqli_error(self::$_sess_db));
7 - 106
        }
107
 
31 - 108
        if (self::getSessionTab() != 0) {
109
            $sql = "SELECT data
110
                    FROM   sessions
111
                    WHERE  id = '$id' and tab = '0'";
112
 
113
            if ($result = mysqli_query(self::$_sess_db, $sql))
114
            {
115
                if (mysqli_num_rows($result))
116
                {
117
                    $record = mysqli_fetch_assoc($result);
118
                    $decrypted = Cryptor::Decrypt($record['data']);
119
                    $temp = json_decode($decrypted, true);
120
                    $_SESSION = array_merge($_SESSION, $temp);
121
                }
122
            } else {
123
                error_log("MySQL Login Read Session Error: " . mysqli_error(self::$_sess_db)); // bugbug
124
            }
125
        }
126
 
127
        if (isset($_SESSION) && !empty($_SESSION) && $_SESSION != null) {
128
            return session_encode();
129
        }
130
 
7 - 131
        return '';
132
    }
133
 
134
    public function write($id, $data)
135
    {
22 - 136
        return true; // see commit()
21 - 137
    }
138
 
139
    public static function commit($id)
140
    {
7 - 141
        $id = mysqli_real_escape_string(self::$_sess_db, $id);
142
        $access = mysqli_real_escape_string(self::$_sess_db, time());
21 - 143
        $encrypted = Cryptor::Encrypt(json_encode($_SESSION));
7 - 144
 
145
        $sql = "REPLACE
146
                INTO    sessions
28 - 147
                VALUES  ('$id', '" . self::getSessionTab() . "', '$access', '$encrypted', NULL)";  //bugbug userId
7 - 148
 
20 - 149
        if (!($result = mysqli_query(self::$_sess_db, $sql))) {
150
            error_log("MySQL Write Session Error: " . mysqli_error(self::$_sess_db));
151
        }
152
 
153
        return $result;
7 - 154
    }
155
 
156
    public function destroy($id)
157
    {
158
        $id = mysqli_real_escape_string(self::$_sess_db, $id);
159
 
160
        $sql = "DELETE
161
                FROM   sessions
20 - 162
                WHERE  id = '$id' and tab = '" . self::getSessionTab() . "'";
7 - 163
 
20 - 164
        if (!($result = mysqli_query(self::$_sess_db, $sql))) {
165
            error_log("MySQL Destroy Session: " . mysqli_error(self::$_sess_db));
166
        }
167
 
168
        return $result;
7 - 169
    }
170
 
171
    public function gc($maxlifetime)
172
    {
173
        $old = mysqli_real_escape_string(self::$_sess_db, time() - $maxlifetime);
174
 
175
    $sql = "DELETE
176
            FROM   sessions
177
            WHERE  access < '$old'";
178
 
20 - 179
        if (!($result = mysqli_query(self::$_sess_db, $sql))) {
180
            error_log("MySQL Session GC Error: " . mysqli_error(self::$_sess_db));
181
        }
182
 
183
        return $result;
7 - 184
    }
185
}
186