Subversion Repositories cheapmusic

Rev

Rev 26 | Rev 31 | 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
 
16
        if ($set_tab && !is_null($set_tab) && $set_tab > 0) {
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
 
64
    public function open($savePath, $sessionName)
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
 
94
        if ($result = mysqli_query(self::$_sess_db, $sql))
95
        {
96
            if (mysqli_num_rows($result))
14 - 97
            {
7 - 98
                $record = mysqli_fetch_assoc($result);
99
                $decrypted = Cryptor::Decrypt($record['data']);
20 - 100
                $_SESSION = json_decode($decrypted, true);
101
                if (isset($_SESSION) && !empty($_SESSION) && $_SESSION != null) {
102
                    return session_encode();
103
                }
7 - 104
            }
20 - 105
        } else {
106
            error_log("MySQL Read Session Error: " . mysqli_error(self::$_sess_db));
7 - 107
        }
108
 
109
        return '';
110
    }
111
 
112
    public function write($id, $data)
113
    {
22 - 114
        return true; // see commit()
21 - 115
    }
116
 
117
    public static function commit($id)
118
    {
7 - 119
        $id = mysqli_real_escape_string(self::$_sess_db, $id);
120
        $access = mysqli_real_escape_string(self::$_sess_db, time());
21 - 121
        $encrypted = Cryptor::Encrypt(json_encode($_SESSION));
7 - 122
 
123
        $sql = "REPLACE
124
                INTO    sessions
28 - 125
                VALUES  ('$id', '" . self::getSessionTab() . "', '$access', '$encrypted', NULL)";  //bugbug userId
7 - 126
 
20 - 127
        if (!($result = mysqli_query(self::$_sess_db, $sql))) {
128
            error_log("MySQL Write Session Error: " . mysqli_error(self::$_sess_db));
129
        }
130
 
131
        return $result;
7 - 132
    }
133
 
134
    public function destroy($id)
135
    {
136
        $id = mysqli_real_escape_string(self::$_sess_db, $id);
137
 
138
        $sql = "DELETE
139
                FROM   sessions
20 - 140
                WHERE  id = '$id' and tab = '" . self::getSessionTab() . "'";
7 - 141
 
20 - 142
        if (!($result = mysqli_query(self::$_sess_db, $sql))) {
143
            error_log("MySQL Destroy Session: " . mysqli_error(self::$_sess_db));
144
        }
145
 
146
        return $result;
7 - 147
    }
148
 
149
    public function gc($maxlifetime)
150
    {
151
        $old = mysqli_real_escape_string(self::$_sess_db, time() - $maxlifetime);
152
 
153
    $sql = "DELETE
154
            FROM   sessions
155
            WHERE  access < '$old'";
156
 
20 - 157
        if (!($result = mysqli_query(self::$_sess_db, $sql))) {
158
            error_log("MySQL Session GC Error: " . mysqli_error(self::$_sess_db));
159
        }
160
 
161
        return $result;
7 - 162
    }
163
}
164