-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile.php
More file actions
147 lines (118 loc) · 4.29 KB
/
Copy pathFile.php
File metadata and controls
147 lines (118 loc) · 4.29 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
<?php
declare(strict_types=1);
namespace Rancoud\Session;
class File implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
protected ?string $savePath = null;
protected string $prefix = 'sess_';
protected int $lengthSessionID = 127;
public function setPrefix(string $prefix): void
{
$this->prefix = $prefix;
}
/** @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 open(string $path, string $name): bool
{
$this->savePath = $path;
if (!\is_dir($this->savePath) && !\mkdir($this->savePath, 0o750) && !\is_dir($this->savePath)) {
// @codeCoverageIgnoreStart
// Could not reach this statement without mocking the filesystem
throw new SessionException(\sprintf('Directory "%s" was not created', $this->savePath));
// @codeCoverageIgnoreEnd
}
return true;
}
public function close(): bool
{
return true;
}
public function read(string $id): string
{
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $id;
if (\file_exists($filename) && \is_file($filename)) {
return (string) \file_get_contents($filename);
}
return '';
}
public function write(string $id, string $data): bool
{
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $id;
return !(\file_put_contents($filename, $data) === false);
}
public function destroy(string $id): bool
{
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $id;
if (\file_exists($filename) && \is_file($filename)) {
\unlink($filename);
}
return true;
}
#[\ReturnTypeWillChange]
public function gc(int $max_lifetime): bool
{
$pattern = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . '*';
foreach (\glob($pattern) as $file) {
if (\file_exists($file) && \is_file($file) && \filemtime($file) + $max_lifetime < \time()) {
\unlink($file);
}
}
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;
}
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $id;
return \file_exists($filename);
}
/** 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
}
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $string;
if (\file_exists($filename)) {
// @codeCoverageIgnoreStart
// Could not reach this statement without mocking the filesystem
return $this->create_sid();
// @codeCoverageIgnoreEnd
}
return $string;
}
}