-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRedis.php
More file actions
135 lines (107 loc) · 3.38 KB
/
Copy pathRedis.php
File metadata and controls
135 lines (107 loc) · 3.38 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
<?php
declare(strict_types=1);
namespace Rancoud\Session;
use Predis\Client as Predis;
class Redis implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
protected Predis $redis;
protected int $lifetime = 1440;
protected int $lengthSessionID = 127;
public function setNewRedis(array|string $configuration): void
{
$this->redis = new Predis($configuration);
}
public function setCurrentRedis(Predis $redis): void
{
$this->redis = $redis;
}
public function setLifetime(int $lifetime): void
{
$this->lifetime = $lifetime;
}
/** @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;
}
public function open(string $path, string $name): bool
{
return true;
}
public function close(): bool
{
return true;
}
public function read(string $id): string
{
return (string) $this->redis->get($id);
}
public function write(string $id, string $data): bool
{
$this->redis->set($id, $data);
$this->redis->expireat($id, \time() + $this->lifetime);
return true;
}
public function destroy(string $id): bool
{
$this->redis->del([$id]);
return true;
}
#[\ReturnTypeWillChange]
public function gc(int $max_lifetime): bool
{
return true;
}
/** Checks format and id exists, if not session_id will be regenerate. */
public function validateId(string $id): bool
{
if (\preg_match('/^[a-zA-Z0-9-]{' . $this->lengthSessionID . '}+$/', $id) !== 1) {
return false;
}
$exist = $this->redis->exists($id);
return $exist === 1;
}
/** Updates the timestamp of a session when its data didn't change. */
public function updateTimestamp(string $id, string $data): bool
{
return $this->write($id, $data);
}
/**
* @throws SessionException
*
* @noinspection PhpMethodNamingConventionInspection
*/
public function create_sid(): string
{
$string = '';
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-';
try {
$countCharacters = 62;
for ($i = 0; $i < $this->lengthSessionID; ++$i) {
$string .= $characters[\random_int(0, $countCharacters)];
}
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
/* If an appropriate source of randomness cannot be found, an Exception will be thrown.
* The list of randomness: https://www.php.net/manual/en/function.random-int.php
*/
throw new SessionException('could not create sid: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
// @codeCoverageIgnoreEnd
}
$exist = $this->redis->exists($string);
if ($exist !== 0) {
// @codeCoverageIgnoreStart
// Could not reach this statement without mocking the function
return $this->create_sid();
// @codeCoverageIgnoreEnd
}
return $string;
}
}