Skip to content

Commit d869d4e

Browse files
committed
Make the session handlers all compatible with SessionHandlerInterface
1 parent 148d78a commit d869d4e

File tree

6 files changed

+229
-290
lines changed

6 files changed

+229
-290
lines changed

system/Session/Handlers/ArrayHandler.php

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace CodeIgniter\Session\Handlers;
1313

14-
use Exception;
14+
use ReturnTypeWillChange;
1515

1616
/**
1717
* Session handler using static array for storage.
@@ -22,78 +22,70 @@ class ArrayHandler extends BaseHandler
2222
protected static $cache = [];
2323

2424
/**
25-
* Open
25+
* Re-initialize existing session, or creates a new one.
2626
*
27-
* Ensures we have an initialized database connection.
28-
*
29-
* @param string $savePath Path to session files' directory
30-
* @param string $name Session cookie name
31-
*
32-
* @throws Exception
27+
* @param string $path The path where to store/retrieve the session
28+
* @param string $name The session name
3329
*/
34-
public function open($savePath, $name): bool
30+
public function open($path, $name): bool
3531
{
3632
return true;
3733
}
3834

3935
/**
40-
* Read
36+
* Reads the session data from the session storage, and returns the results.
4137
*
42-
* Reads session data and acquires a lock
38+
* @param string $id The session ID
4339
*
44-
* @param string $sessionID Session ID
45-
*
46-
* @return string Serialized session data
40+
* @return false|string Returns an encoded string of the read data.
41+
* If nothing was read, it must return false.
4742
*/
48-
public function read($sessionID): string
43+
#[ReturnTypeWillChange]
44+
public function read($id)
4945
{
5046
return '';
5147
}
5248

5349
/**
54-
* Write
55-
*
56-
* Writes (create / update) session data
50+
* Writes the session data to the session storage.
5751
*
58-
* @param string $sessionID Session ID
59-
* @param string $sessionData Serialized session data
52+
* @param string $id The session ID
53+
* @param string $data The encoded session data
6054
*/
61-
public function write($sessionID, $sessionData): bool
55+
public function write($id, $data): bool
6256
{
6357
return true;
6458
}
6559

6660
/**
67-
* Close
68-
*
69-
* Releases locks and closes file descriptor.
61+
* Closes the current session.
7062
*/
7163
public function close(): bool
7264
{
7365
return true;
7466
}
7567

7668
/**
77-
* Destroy
78-
*
79-
* Destroys the current session.
69+
* Destroys a session
8070
*
81-
* @param string $sessionID
71+
* @param string $id The session ID being destroyed
8272
*/
83-
public function destroy($sessionID): bool
73+
public function destroy($id): bool
8474
{
8575
return true;
8676
}
8777

8878
/**
89-
* Garbage Collector
79+
* Cleans up expired sessions.
9080
*
91-
* Deletes expired sessions
81+
* @param int $max_lifetime Sessions that have not updated
82+
* for the last max_lifetime seconds will be removed.
9283
*
93-
* @param int $maxlifetime Maximum lifetime of sessions
84+
* @return false|int Returns the number of deleted sessions on success, or false on failure.
9485
*/
95-
public function gc($maxlifetime): bool
86+
#[ReturnTypeWillChange]
87+
public function gc($max_lifetime)
9688
{
97-
return true;
89+
return 1;
9890
}
9991
}

system/Session/Handlers/BaseHandler.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ abstract class BaseHandler implements SessionHandlerInterface
100100
*/
101101
protected $ipAddress;
102102

103-
/**
104-
* Constructor
105-
*/
106103
public function __construct(AppConfig $config, string $ipAddress)
107104
{
108105
$this->cookiePrefix = $config->cookiePrefix;
@@ -155,12 +152,11 @@ protected function releaseLock(): bool
155152
}
156153

157154
/**
158-
* Fail
159-
*
160155
* Drivers other than the 'files' one don't (need to) use the
161156
* session.save_path INI setting, but that leads to confusing
162157
* error messages emitted by PHP when open() or write() fail,
163158
* as the message contains session.save_path ...
159+
*
164160
* To work around the problem, the drivers will call this method
165161
* so that the INI is set just in time for the error message to
166162
* be properly generated.

system/Session/Handlers/DatabaseHandler.php

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use CodeIgniter\Session\Exceptions\SessionException;
1616
use Config\App as AppConfig;
1717
use Config\Database;
18-
use Exception;
18+
use ReturnTypeWillChange;
1919

2020
/**
2121
* Session handler using current Database for storage
@@ -58,27 +58,24 @@ class DatabaseHandler extends BaseHandler
5858
protected $rowExists = false;
5959

6060
/**
61-
* Constructor
61+
* @throws SessionException
6262
*/
6363
public function __construct(AppConfig $config, string $ipAddress)
6464
{
6565
parent::__construct($config, $ipAddress);
66-
67-
// Determine Table
6866
$this->table = $config->sessionSavePath;
6967

7068
if (empty($this->table)) {
7169
throw SessionException::forMissingDatabaseTable();
7270
}
7371

74-
// Get DB Connection
7572
// @phpstan-ignore-next-line
7673
$this->DBGroup = $config->sessionDBGroup ?? config(Database::class)->defaultGroup;
7774

7875
$this->db = Database::connect($this->DBGroup);
7976

80-
// Determine Database type
8177
$driver = strtolower(get_class($this->db));
78+
8279
if (strpos($driver, 'mysql') !== false) {
8380
$this->platform = 'mysql';
8481
} elseif (strpos($driver, 'postgre') !== false) {
@@ -87,16 +84,12 @@ public function __construct(AppConfig $config, string $ipAddress)
8784
}
8885

8986
/**
90-
* Open
91-
*
92-
* Ensures we have an initialized database connection.
93-
*
94-
* @param string $savePath Path to session files' directory
95-
* @param string $name Session cookie name
87+
* Re-initialize existing session, or creates a new one.
9688
*
97-
* @throws Exception
89+
* @param string $path The path where to store/retrieve the session
90+
* @param string $name The session name
9891
*/
99-
public function open($savePath, $name): bool
92+
public function open($path, $name): bool
10093
{
10194
if (empty($this->db->connID)) {
10295
$this->db->initialize();
@@ -106,30 +99,29 @@ public function open($savePath, $name): bool
10699
}
107100

108101
/**
109-
* Read
102+
* Reads the session data from the session storage, and returns the results.
110103
*
111-
* Reads session data and acquires a lock
104+
* @param string $id The session ID
112105
*
113-
* @param string $sessionID Session ID
114-
*
115-
* @return string Serialized session data
106+
* @return false|string Returns an encoded string of the read data.
107+
* If nothing was read, it must return false.
116108
*/
117-
public function read($sessionID): string
109+
#[ReturnTypeWillChange]
110+
public function read($id)
118111
{
119-
if ($this->lockSession($sessionID) === false) {
112+
if ($this->lockSession($id) === false) {
120113
$this->fingerprint = md5('');
121114

122115
return '';
123116
}
124117

125-
// Needed by write() to detect session_regenerate_id() calls
126118
if (! isset($this->sessionID)) {
127-
$this->sessionID = $sessionID;
119+
$this->sessionID = $id;
128120
}
129121

130122
$builder = $this->db->table($this->table)
131123
->select($this->platform === 'postgre' ? "encode(data, 'base64') AS data" : 'data')
132-
->where('id', $sessionID);
124+
->where('id', $id);
133125

134126
if ($this->matchIP) {
135127
$builder = $builder->where('ip_address', $this->ipAddress);
@@ -160,87 +152,78 @@ public function read($sessionID): string
160152
}
161153

162154
/**
163-
* Write
164-
*
165-
* Writes (create / update) session data
155+
* Writes the session data to the session storage.
166156
*
167-
* @param string $sessionID Session ID
168-
* @param string $sessionData Serialized session data
157+
* @param string $id The session ID
158+
* @param string $data The encoded session data
169159
*/
170-
public function write($sessionID, $sessionData): bool
160+
public function write($id, $data): bool
171161
{
172162
if ($this->lock === false) {
173163
return $this->fail();
174164
}
175165

176-
// Was the ID regenerated?
177-
if ($sessionID !== $this->sessionID) {
166+
if ($this->sessionID !== $id) {
178167
$this->rowExists = false;
179-
$this->sessionID = $sessionID;
168+
$this->sessionID = $id;
180169
}
181170

182171
if ($this->rowExists === false) {
183172
$insertData = [
184-
'id' => $sessionID,
173+
'id' => $id,
185174
'ip_address' => $this->ipAddress,
186175
'timestamp' => 'now()',
187-
'data' => $this->platform === 'postgre' ? '\x' . bin2hex($sessionData) : $sessionData,
176+
'data' => $this->platform === 'postgre' ? '\x' . bin2hex($data) : $data,
188177
];
189178

190179
if (! $this->db->table($this->table)->insert($insertData)) {
191180
return $this->fail();
192181
}
193182

194-
$this->fingerprint = md5($sessionData);
183+
$this->fingerprint = md5($data);
195184
$this->rowExists = true;
196185

197186
return true;
198187
}
199188

200-
$builder = $this->db->table($this->table)->where('id', $sessionID);
189+
$builder = $this->db->table($this->table)->where('id', $id);
201190

202191
if ($this->matchIP) {
203192
$builder = $builder->where('ip_address', $this->ipAddress);
204193
}
205194

206-
$updateData = [
207-
'timestamp' => 'now()',
208-
];
195+
$updateData = ['timestamp' => 'now()'];
209196

210-
if ($this->fingerprint !== md5($sessionData)) {
211-
$updateData['data'] = ($this->platform === 'postgre') ? '\x' . bin2hex($sessionData) : $sessionData;
197+
if ($this->fingerprint !== md5($data)) {
198+
$updateData['data'] = ($this->platform === 'postgre') ? '\x' . bin2hex($data) : $data;
212199
}
213200

214201
if (! $builder->update($updateData)) {
215202
return $this->fail();
216203
}
217204

218-
$this->fingerprint = md5($sessionData);
205+
$this->fingerprint = md5($data);
219206

220207
return true;
221208
}
222209

223210
/**
224-
* Close
225-
*
226-
* Releases locks and closes file descriptor.
211+
* Closes the current session.
227212
*/
228213
public function close(): bool
229214
{
230215
return ($this->lock && ! $this->releaseLock()) ? $this->fail() : true;
231216
}
232217

233218
/**
234-
* Destroy
235-
*
236-
* Destroys the current session.
219+
* Destroys a session
237220
*
238-
* @param string $sessionID
221+
* @param string $id The session ID being destroyed
239222
*/
240-
public function destroy($sessionID): bool
223+
public function destroy($id): bool
241224
{
242225
if ($this->lock) {
243-
$builder = $this->db->table($this->table)->where('id', $sessionID);
226+
$builder = $this->db->table($this->table)->where('id', $id);
244227

245228
if ($this->matchIP) {
246229
$builder = $builder->where('ip_address', $this->ipAddress);
@@ -261,18 +244,20 @@ public function destroy($sessionID): bool
261244
}
262245

263246
/**
264-
* Garbage Collector
247+
* Cleans up expired sessions.
265248
*
266-
* Deletes expired sessions
249+
* @param int $max_lifetime Sessions that have not updated
250+
* for the last max_lifetime seconds will be removed.
267251
*
268-
* @param int $maxlifetime Maximum lifetime of sessions
252+
* @return false|int Returns the number of deleted sessions on success, or false on failure.
269253
*/
270-
public function gc($maxlifetime): bool
254+
#[ReturnTypeWillChange]
255+
public function gc($max_lifetime)
271256
{
272257
$separator = $this->platform === 'postgre' ? '\'' : ' ';
273-
$interval = implode($separator, ['', "{$maxlifetime} second", '']);
258+
$interval = implode($separator, ['', "{$max_lifetime} second", '']);
274259

275-
return $this->db->table($this->table)->delete("timestamp < now() - INTERVAL {$interval}") ? true : $this->fail();
260+
return $this->db->table($this->table)->delete("timestamp < now() - INTERVAL {$interval}") ? 1 : $this->fail();
276261
}
277262

278263
/**

0 commit comments

Comments
 (0)