-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfluxDbSessionHandler.php
More file actions
114 lines (101 loc) · 2.71 KB
/
Copy pathInfluxDbSessionHandler.php
File metadata and controls
114 lines (101 loc) · 2.71 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
<?php
namespace Detain\SessionSamurai;
use InfluxDB\Client;
use InfluxDB\Point;
use SessionHandlerInterface;
use SessionIdInterface;
use SessionUpdateTimestampHandlerInterface;
class InfluxDbSessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
protected $client;
protected $database;
protected $measurement;
public function __construct(Client $client, string $database, string $measurement = 'sessions')
{
$this->client = $client;
$this->database = $database;
$this->measurement = $measurement;
}
/**
* {@inheritdoc}
*/
public function open($save_path, $session_name): bool
{
// No action necessary because connection is established in constructor
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
// No action necessary because connection is closed in destructor
return true;
}
/**
* {@inheritdoc}
*/
public function read($session_id)
{
$result = $this->client->query("SELECT * FROM {$this->measurement} WHERE session_id = '$session_id'", $this->database);
if ($result->getPoints()) {
return $result->getPoints()[0]['session_data'];
} else {
return '';
}
}
/**
* {@inheritdoc}
*/
public function write($session_id, $session_data): bool
{
$point = new Point(
$this->measurement,
null,
['session_id' => $session_id],
['session_data' => $session_data],
time()
);
$this->client->writePoints([$point], $this->database);
return true;
}
/**
* {@inheritdoc}
*/
public function destroy($session_id): bool
{
$this->client->query("DELETE FROM {$this->measurement} WHERE session_id = '$session_id'", $this->database);
return true;
}
/**
* {@inheritdoc}
*/
public function gc($maxlifetime)
{
$maxlifetime = time() - $maxlifetime;
$this->client->query("DELETE FROM {$this->measurement} WHERE time < $maxlifetime", $this->database);
return true;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid()
{
return bin2hex(random_bytes(16));
}
/**
* {@inheritdoc}
*/
public function validateId($session_id)
{
return preg_match('/^[0-9a-f]{32}$/', $session_id) === 1;
}
/**
* {@inheritdoc}
*/
public function updateTimestamp($session_id, $session_data)
{
return $this->write($session_id, $session_data);
}
}