|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staticus\Auth\SaveHandlers; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Session\SaveHandler\SaveHandlerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Session save handler for redis.io |
|
9
|
|
|
*/ |
|
10
|
|
|
class Redis implements SaveHandlerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Redis |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $redis; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Session Save Path |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $sessionSavePath; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Session Name |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $sessionName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Lifetime |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $lifetime; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor |
|
40
|
|
|
* @param string $host |
|
41
|
|
|
* @param string $port |
|
42
|
|
|
* @param string $password |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($host, $port, $password) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->redis = new \Redis(); |
|
47
|
|
|
$this->redis->connect($host, $port); |
|
48
|
|
|
$this->redis->auth($password); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Open Session - retrieve resources |
|
53
|
|
|
* @param string $savePath |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function open($savePath, $name) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->sessionSavePath = $savePath; |
|
60
|
|
|
$this->sessionName = $name; |
|
61
|
|
|
$this->lifetime = (int)ini_get('session.gc_maxlifetime'); |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Close Session - free resources |
|
67
|
|
|
* |
|
68
|
|
|
*/ |
|
69
|
|
|
public function close() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->redis->close(); |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Read session data |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $identifier |
|
79
|
|
|
* @return bool|string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function read($identifier) |
|
82
|
|
|
{ |
|
83
|
|
|
return (string)$this->redis->get($this->getSessionKey($identifier)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Read sessions for user |
|
88
|
|
|
* @param $userId |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function readByUser($userId) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->redis->sMembers($this->getUserKey($userId)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Write Session - commit data to resource |
|
98
|
|
|
* @param string $identifier |
|
99
|
|
|
* @param mixed $data |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function write($identifier, $data) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->redis->setex( |
|
105
|
|
|
$this->getSessionKey($identifier), |
|
106
|
|
|
$this->lifetime, |
|
107
|
|
|
$data |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Write session ids for user |
|
113
|
|
|
* @param $sessionId |
|
114
|
|
|
* @param $userId |
|
115
|
|
|
*/ |
|
116
|
|
|
public function writeByUser($sessionId, $userId) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->redis->sAdd($this->getUserKey($userId), $sessionId); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Destroy Session and remove data from user's container |
|
123
|
|
|
* @param string $sessionId |
|
124
|
|
|
* @param null $userId |
|
125
|
|
|
*/ |
|
126
|
|
|
public function destroy($sessionId, $userId = null) |
|
127
|
|
|
{ |
|
128
|
|
|
$sessionKey = $this->getSessionKey($sessionId); |
|
129
|
|
|
if ($this->redis->exists($sessionKey)) { |
|
130
|
|
|
$this->redis->delete($sessionKey); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/* Remove session id from user's container */ |
|
134
|
|
|
if (null !== $userId) { |
|
135
|
|
|
$userKey = $this->getUserKey($userId); |
|
136
|
|
|
if ($this->redis->sIsMember($userKey, $sessionId)) { |
|
137
|
|
|
$this->redis->sRemove($userKey, $sessionId); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Destroy all sessions by user |
|
144
|
|
|
* @param $userId |
|
145
|
|
|
*/ |
|
146
|
|
|
public function destroyByUser($userId) |
|
147
|
|
|
{ |
|
148
|
|
|
foreach ($this->readByUser($userId) as $sessionId) { |
|
149
|
|
|
$this->destroy($sessionId); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$userKey = $this->getUserKey($userId); |
|
153
|
|
|
if ($this->redis->exists($userKey)) { |
|
154
|
|
|
$this->redis->delete($userKey); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Add prefix for session's key |
|
160
|
|
|
* @param $sessionId |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getSessionKey($sessionId) |
|
164
|
|
|
{ |
|
165
|
|
|
return 'session_' . $sessionId; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Add prefix for user's key |
|
170
|
|
|
* @param $userId |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getUserKey($userId) |
|
174
|
|
|
{ |
|
175
|
|
|
return 'user_' . $userId; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Garbage Collection - remove old session data older |
|
180
|
|
|
* than $maxlifetime (in seconds) |
|
181
|
|
|
* |
|
182
|
|
|
* @param int $maxlifetime |
|
183
|
|
|
* @return bool|void |
|
184
|
|
|
*/ |
|
185
|
|
|
public function gc($maxlifetime) |
|
|
|
|
|
|
186
|
|
|
{ |
|
187
|
|
|
// TODO: Implement gc() method. |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.