Repository navigation
Expand file tree
/
Copy pathFile.php
More file actions
167 lines (141 loc) · 3.75 KB
/
Copy pathFile.php
File metadata and controls
167 lines (141 loc) · 3.75 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
<?php
declare(strict_types=1);
namespace Rancoud\Session;
use SessionHandlerInterface;
use SessionUpdateTimestampHandlerInterface;
/**
* Class File.
*/
class File implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface
{
/** @var string|null */
protected ?string $savePath = null;
/** @var string */
protected string $prefix = 'sess_';
/**
* @param string $prefix
*/
public function setPrefix(string $prefix): void
{
$this->prefix = $prefix;
}
/**
* @param string $savePath
* @param string $sessionName
*
* @return bool
*/
public function open($savePath, $sessionName): bool
{
$this->savePath = $savePath;
if (!\is_dir($this->savePath) && !\mkdir($this->savePath, 0750) && !\is_dir($this->savePath)) {
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $this->savePath));
}
return true;
}
/**
* @return bool
*/
public function close(): bool
{
return true;
}
/**
* @param string $sessionId
*
* @return string
*/
public function read($sessionId): string
{
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $sessionId;
if (\file_exists($filename)) {
return (string) \file_get_contents($filename);
}
return '';
}
/**
* @param string $sessionId
* @param string $data
*
* @return bool
*/
public function write($sessionId, $data): bool
{
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $sessionId;
return \file_put_contents($filename, $data) === false ? false : true;
}
/**
* @param string $sessionId
*
* @return bool
*/
public function destroy($sessionId): bool
{
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $sessionId;
if (\file_exists($filename)) {
\unlink($filename);
}
return true;
}
/**
* @param int $lifetime
*
* @return bool
*/
public function gc($lifetime): bool
{
$pattern = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . '*';
foreach (\glob($pattern) as $file) {
if (\filemtime($file) + $lifetime < \time() && \file_exists($file)) {
\unlink($file);
}
}
return true;
}
/**
* Checks format and id exists, if not session_id will be regenerate.
*
* @param string $key
*
* @return bool
*/
public function validateId($key): bool
{
if (\preg_match('/^[a-zA-Z0-9-]{127}+$/', $key) !== 1) {
return false;
}
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $key;
return \file_exists($filename);
}
/**
* Updates the timestamp of a session when its data didn't change.
*
* @param string $sessionId
* @param string $sessionData
*
* @return bool
*/
public function updateTimestamp($sessionId, $sessionData): bool
{
return $this->write($sessionId, $sessionData);
}
/**
* @throws \Exception
*
* @return string
*/
public function create_sid(): string
{
$string = '';
$caracters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-';
$countCaracters = 62;
for ($i = 0; $i < 127; ++$i) {
$string .= $caracters[\random_int(0, $countCaracters)];
}
$filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $string;
if (\file_exists($filename)) {
return $this->create_sid();
}
return $string;
}
}