Subversion Repositories cheapmusic

Rev

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