Subversion Repositories cheapmusic

Rev

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