1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace JAQB; |
12
|
|
|
|
13
|
|
|
use PDOStatement; |
14
|
|
|
use Pimple\Container; |
15
|
|
|
use SessionHandlerInterface; |
16
|
|
|
|
17
|
|
|
class Session implements SessionHandlerInterface |
18
|
|
|
{ |
19
|
|
|
const TABLENAME = 'Sessions'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Pimple\Container |
23
|
|
|
*/ |
24
|
|
|
private $app; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Starts the session using this handler. |
28
|
|
|
* |
29
|
|
|
* @param Session $handler |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public static function registerHandler(Session $handler) |
34
|
|
|
{ |
35
|
|
|
return session_set_save_handler($handler, true); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new session handler. |
40
|
|
|
* |
41
|
|
|
* @param \Pimple\Container $app |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Container $app) |
44
|
|
|
{ |
45
|
|
|
$this->app = $app; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Installs schema for handling sessions in a database. |
50
|
|
|
* |
51
|
|
|
* @return bool success |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function install() |
54
|
|
|
{ |
55
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `'.self::TABLENAME.'` (`id` varchar(32) NOT NULL, PRIMARY KEY (`id`), `session_data` longtext NULL, `access` int(10) NULL);'; |
56
|
|
|
|
57
|
|
|
return $this->app['db'] |
58
|
|
|
->raw($sql) |
59
|
|
|
->execute() instanceof PDOStatement; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Reads a session. |
64
|
|
|
* |
65
|
|
|
* @param int $id session ID |
66
|
|
|
* |
67
|
|
|
* @return string data |
68
|
|
|
*/ |
69
|
|
|
public function read($id) |
70
|
|
|
{ |
71
|
|
|
return $this->app['db'] |
72
|
|
|
->select('session_data') |
73
|
|
|
->from(self::TABLENAME) |
74
|
|
|
->where('id', $id) |
75
|
|
|
->scalar(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Writes a session. |
80
|
|
|
* |
81
|
|
|
* @param int $id session ID |
82
|
|
|
* @param string $data session data |
83
|
|
|
* |
84
|
|
|
* @return bool success |
85
|
|
|
*/ |
86
|
|
|
public function write($id, $data) |
87
|
|
|
{ |
88
|
|
|
$this->app['db'] |
89
|
|
|
->delete(self::TABLENAME) |
90
|
|
|
->where('id', $id) |
91
|
|
|
->execute(); |
92
|
|
|
|
93
|
|
|
return $this->app['db'] |
94
|
|
|
->insert([ |
95
|
|
|
'id' => $id, |
96
|
|
|
'access' => time(), |
97
|
|
|
'session_data' => $data, ]) |
98
|
|
|
->into(self::TABLENAME) |
99
|
|
|
->execute() instanceof PDOStatement; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Destroys a session. |
104
|
|
|
* |
105
|
|
|
* @param int $id session ID |
106
|
|
|
* |
107
|
|
|
* @return bool success |
108
|
|
|
*/ |
109
|
|
|
public function destroy($id) |
110
|
|
|
{ |
111
|
|
|
return $this->app['db'] |
112
|
|
|
->delete(self::TABLENAME) |
113
|
|
|
->where('id', $id) |
114
|
|
|
->execute() instanceof PDOStatement; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Performs garbage collection on sessions. |
119
|
|
|
* |
120
|
|
|
* @param int $max maximum number of seconds a session can live |
121
|
|
|
* |
122
|
|
|
* @return bool success |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function gc($max) |
125
|
|
|
{ |
126
|
|
|
// delete sessions older than max TTL |
127
|
|
|
$ttl = time() - $max; |
128
|
|
|
|
129
|
|
|
return $this->app['db'] |
130
|
|
|
->delete(self::TABLENAME) |
131
|
|
|
->where('access', $ttl, '<') |
132
|
|
|
->execute() instanceof PDOStatement; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* These functions are all noops for various reasons... |
137
|
|
|
* open() and close() have no practical meaning in terms of database connections. |
138
|
|
|
*/ |
139
|
|
|
public function open($path, $name) |
140
|
|
|
{ |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function close() |
145
|
|
|
{ |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|