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
|
|
|
* |
54
|
|
|
* @param string $savePath |
55
|
|
|
* @param string $name |
56
|
|
|
*/ |
57
|
|
|
public function open($savePath, $name) |
58
|
|
|
{ |
59
|
|
|
$this->sessionSavePath = $savePath; |
60
|
|
|
$this->sessionName = $name; |
61
|
|
|
$this->lifetime = ini_get('session.gc_maxlifetime'); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Close Session - free resources |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function close() |
69
|
|
|
{ |
70
|
|
|
$this->redis->close(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Read session data |
75
|
|
|
* |
76
|
|
|
* @param string $identifier |
77
|
|
|
* @return bool|string |
78
|
|
|
*/ |
79
|
|
|
public function read($identifier) |
80
|
|
|
{ |
81
|
|
|
return $this->redis->get($this->getSessionKey($identifier)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Read sessions for user |
86
|
|
|
* @param $userId |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function readByUser($userId) |
90
|
|
|
{ |
91
|
|
|
return $this->redis->sMembers($this->getUserKey($userId)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Write Session - commit data to resource |
96
|
|
|
* |
97
|
|
|
* @param string $identifier |
98
|
|
|
* @param mixed $data |
99
|
|
|
*/ |
100
|
|
|
public function write($identifier, $data) |
101
|
|
|
{ |
102
|
|
|
$this->redis->setex( |
103
|
|
|
$this->getSessionKey($identifier), |
104
|
|
|
$this->lifetime, |
105
|
|
|
$data |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Write session ids for user |
111
|
|
|
* @param $sessionId |
112
|
|
|
* @param $userId |
113
|
|
|
*/ |
114
|
|
|
public function writeByUser($sessionId, $userId) |
115
|
|
|
{ |
116
|
|
|
$this->redis->sAdd($this->getUserKey($userId), $sessionId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Destroy Session and remove data from user's container |
121
|
|
|
* @param string $sessionId |
122
|
|
|
* @param null $userId |
123
|
|
|
*/ |
124
|
|
|
public function destroy($sessionId, $userId = null) |
125
|
|
|
{ |
126
|
|
|
$sessionKey = $this->getSessionKey($sessionId); |
127
|
|
|
if ($this->redis->exists($sessionKey)) { |
128
|
|
|
$this->redis->delete($sessionKey); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/* Remove session id from user's container */ |
132
|
|
|
if (null !== $userId) { |
133
|
|
|
$userKey = $this->getUserKey($userId); |
134
|
|
|
if ($this->redis->sIsMember($userKey, $sessionId)) { |
135
|
|
|
$this->redis->sRemove($userKey, $sessionId); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Destroy all sessions by user |
142
|
|
|
* @param $userId |
143
|
|
|
*/ |
144
|
|
|
public function destroyByUser($userId) |
145
|
|
|
{ |
146
|
|
|
foreach ($this->readByUser($userId) as $sessionId) { |
147
|
|
|
$this->destroy($sessionId); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$userKey = $this->getUserKey($userId); |
151
|
|
|
if ($this->redis->exists($userKey)) { |
152
|
|
|
$this->redis->delete($userKey); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add prefix for session's key |
158
|
|
|
* @param $sessionId |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
protected function getSessionKey($sessionId) |
162
|
|
|
{ |
163
|
|
|
return 'session_' . $sessionId; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Add prefix for user's key |
168
|
|
|
* @param $userId |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
protected function getUserKey($userId) |
172
|
|
|
{ |
173
|
|
|
return 'user_' . $userId; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Garbage Collection - remove old session data older |
178
|
|
|
* than $maxlifetime (in seconds) |
179
|
|
|
* |
180
|
|
|
* @param int $maxlifetime |
181
|
|
|
*/ |
182
|
|
|
public function gc($maxlifetime) |
183
|
|
|
{ |
184
|
|
|
// TODO: Implement gc() method. |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.