-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite3SessionHandler.php
More file actions
132 lines (120 loc) · 3.62 KB
/
Copy pathSQLite3SessionHandler.php
File metadata and controls
132 lines (120 loc) · 3.62 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
<?php
namespace Detain\SessionSamurai;
class SQLite3SessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
private \SQLite3 $db;
private string $table = 'sessions';
private int $lifetime = 1440;
/**
* {@inheritdoc}
*/
public function open(string $savePath, string $sessionName): bool
{
$this->db = new \SQLite3($savePath . '/' . $sessionName . '.db');
$this->db->exec("CREATE TABLE IF NOT EXISTS {$this->table} (id TEXT PRIMARY KEY, data TEXT, timestamp INTEGER)");
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
$this->db->close();
return true;
}
/**
* {@inheritdoc}
*/
public function read(string $sessionId): string
{
$stmt = $this->db->prepare("SELECT data FROM {$this->table} WHERE id = :id AND timestamp >= :timestamp");
if ($stmt === false) {
return '';
}
$stmt->bindValue(':id', $sessionId, SQLITE3_TEXT);
$stmt->bindValue(':timestamp', time() - $this->lifetime, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($result === false) {
return '';
}
$row = $result->fetchArray(SQLITE3_ASSOC);
if (!is_array($row) || !isset($row['data']) || !is_string($row['data'])) {
return '';
}
return $row['data'];
}
/**
* {@inheritdoc}
*/
public function write(string $sessionId, string $data): bool
{
$stmt = $this->db->prepare("REPLACE INTO {$this->table} (id, data, timestamp) VALUES (:id, :data, :timestamp)");
if ($stmt === false) {
return false;
}
$stmt->bindValue(':id', $sessionId, SQLITE3_TEXT);
$stmt->bindValue(':data', $data, SQLITE3_TEXT);
$stmt->bindValue(':timestamp', time(), SQLITE3_INTEGER);
$stmt->execute();
return true;
}
/**
* {@inheritdoc}
*/
public function destroy(string $sessionId): bool
{
$stmt = $this->db->prepare("DELETE FROM {$this->table} WHERE id = :id");
if ($stmt === false) {
return false;
}
$stmt->bindValue(':id', $sessionId, SQLITE3_TEXT);
$stmt->execute();
return true;
}
/**
* {@inheritdoc}
*/
public function gc(int $maxlifetime): int|false
{
$stmt = $this->db->prepare("DELETE FROM {$this->table} WHERE timestamp < :timestamp");
if ($stmt === false) {
return false;
}
$stmt->bindValue(':timestamp', time() - $maxlifetime, SQLITE3_INTEGER);
$stmt->execute();
return $this->db->changes();
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
return bin2hex(random_bytes(32));
}
/**
* {@inheritdoc}
*/
public function validateId(string $sessionId): bool
{
return (bool) preg_match('/^[a-f0-9]{64}$/', $sessionId);
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $sessionId, string $sessionData): bool
{
$stmt = $this->db->prepare("UPDATE {$this->table} SET timestamp = :timestamp WHERE id = :id");
if ($stmt === false) {
return false;
}
$stmt->bindValue(':id', $sessionId, SQLITE3_TEXT);
$stmt->bindValue(':timestamp', time(), SQLITE3_INTEGER);
$stmt->execute();
return true;
}
public function setLifetime(int $lifetime): void
{
$this->lifetime = $lifetime;
}
}