-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.php
More file actions
213 lines (179 loc) · 6.4 KB
/
Database.php
File metadata and controls
213 lines (179 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/** @noinspection SqlResolve */
declare(strict_types=1);
namespace Rancoud\Session;
use Rancoud\Database\Configurator;
use Rancoud\Database\Database as DB;
use Rancoud\Database\DatabaseException;
class Database implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
protected DB $db;
protected ?int $userId = null;
protected int $lengthSessionID = 127;
/** @throws SessionException */
public function setNewDatabase(array|Configurator $configuration): void
{
try {
if ($configuration instanceof Configurator) {
$this->db = new DB($configuration);
} else {
$this->db = new DB(new Configurator($configuration));
}
} catch (DatabaseException $e) {
throw new SessionException('could not set database: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
public function setCurrentDatabase(DB $database): void
{
$this->db = $database;
}
public function setUserId(?int $userId): void
{
$this->userId = $userId;
}
/** @throws SessionException */
public function setLengthSessionID(int $length): void
{
if ($length < 32) {
throw new SessionException('could not set length session ID below 32');
}
$this->lengthSessionID = $length;
}
public function getLengthSessionID(): int
{
return $this->lengthSessionID;
}
/** @throws SessionException */
public function deleteUserSessions(int $userID): void
{
try {
$sql = 'DELETE FROM sessions WHERE id_user = :id_user';
$params = ['id_user' => $userID];
$this->db->delete($sql, $params);
} catch (DatabaseException $e) {
throw new SessionException('could not delete sessions: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
/** @throws SessionException */
public function deleteAnonymousSessions(): void
{
try {
$sql = 'DELETE FROM sessions WHERE id_user IS NULL';
$this->db->delete($sql);
} catch (DatabaseException $e) {
throw new SessionException('could not delete sessions: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
public function open(string $path, string $name): bool
{
return true;
}
public function close(): bool
{
return true;
}
/** @throws SessionException */
public function read(string $id): string
{
try {
$sql = 'SELECT content FROM sessions WHERE id = :id';
$params = ['id' => $id];
return (string) $this->db->selectVar($sql, $params);
} catch (DatabaseException $e) {
throw new SessionException('could not read session: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
/** @throws SessionException */
public function write(string $id, string $data): bool
{
try {
$sql = 'REPLACE INTO sessions VALUES(:id, :id_user, UTC_TIMESTAMP(), :content)';
$params = ['id' => $id, 'id_user' => $this->userId, 'content' => $data];
$this->db->exec($sql, $params);
return true;
} catch (DatabaseException $e) {
throw new SessionException('could not update session: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
/** @throws SessionException */
public function destroy(string $id): bool
{
try {
$sql = 'DELETE FROM sessions WHERE id = :id';
$params = ['id' => $id];
$this->db->delete($sql, $params);
return true;
} catch (DatabaseException $e) {
throw new SessionException('could not delete session: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
/** @throws SessionException */
#[\ReturnTypeWillChange]
public function gc(int $max_lifetime): bool
{
try {
$sql = 'DELETE FROM sessions WHERE DATE_ADD(last_access, INTERVAL :seconds second) < UTC_TIMESTAMP()';
$params = ['seconds' => $max_lifetime];
$this->db->delete($sql, $params);
return true;
} catch (DatabaseException $e) {
throw new SessionException('could not clean old sessions: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
/**
* Checks format and id exists, if not session_id will be regenerate.
*
* @throws SessionException
*/
public function validateId(string $id): bool
{
try {
if (\preg_match('/^[a-zA-Z0-9-]{' . $this->lengthSessionID . '}+$/', $id) !== 1) {
return false;
}
$sql = 'SELECT COUNT(id) FROM sessions WHERE id=:id';
$params = ['id' => $id];
$count = $this->db->count($sql, $params);
return $count === 1;
} catch (DatabaseException $e) {
throw new SessionException('could not validate id: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
/**
* Updates the timestamp of a session when its data didn't change.
*
* @throws SessionException
*/
public function updateTimestamp(string $id, string $data): bool
{
return $this->write($id, $data);
}
/**
* @throws SessionException
*
* @noinspection PhpMethodNamingConventionInspection
*/
public function create_sid(): string
{
try {
$string = '';
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-';
$countCharacters = 62;
for ($i = 0; $i < $this->lengthSessionID; ++$i) {
$string .= $characters[\random_int(0, $countCharacters)];
}
$sql = 'SELECT COUNT(id) FROM sessions WHERE id=:id';
$params = ['id' => $string];
$count = $this->db->count($sql, $params);
if ($count !== 0) {
// @codeCoverageIgnoreStart
// Could not reach this statement without mocking the function
return $this->create_sid();
// @codeCoverageIgnoreEnd
}
return $string;
} catch (\Exception $e) {
throw new SessionException('could not create sid: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
}
}
}