| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\Profiler;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Storage for profiler using files.
|
|
|
16 |
*
|
|
|
17 |
* @author Alexandre Salomé <alexandre.salome@gmail.com>
|
|
|
18 |
*/
|
|
|
19 |
class FileProfilerStorage implements ProfilerStorageInterface
|
|
|
20 |
{
|
|
|
21 |
/**
|
|
|
22 |
* Folder where profiler data are stored.
|
|
|
23 |
*/
|
|
|
24 |
private string $folder;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Constructs the file storage using a "dsn-like" path.
|
|
|
28 |
*
|
|
|
29 |
* Example : "file:/path/to/the/storage/folder"
|
|
|
30 |
*
|
|
|
31 |
* @throws \RuntimeException
|
|
|
32 |
*/
|
|
|
33 |
public function __construct(string $dsn)
|
|
|
34 |
{
|
|
|
35 |
if (!str_starts_with($dsn, 'file:')) {
|
|
|
36 |
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use FileStorage with an invalid dsn "%s". The expected format is "file:/path/to/the/storage/folder".', $dsn));
|
|
|
37 |
}
|
|
|
38 |
$this->folder = substr($dsn, 5);
|
|
|
39 |
|
|
|
40 |
if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
|
|
|
41 |
throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, int $start = null, int $end = null, string $statusCode = null): array
|
|
|
46 |
{
|
|
|
47 |
$file = $this->getIndexFilename();
|
|
|
48 |
|
|
|
49 |
if (!file_exists($file)) {
|
|
|
50 |
return [];
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$file = fopen($file, 'r');
|
|
|
54 |
fseek($file, 0, \SEEK_END);
|
|
|
55 |
|
|
|
56 |
$result = [];
|
|
|
57 |
while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
|
|
|
58 |
$values = str_getcsv($line);
|
|
|
59 |
[$csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
|
|
|
60 |
$csvTime = (int) $csvTime;
|
|
|
61 |
|
|
|
62 |
if ($ip && !str_contains($csvIp, $ip) || $url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
|
|
|
63 |
continue;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (!empty($start) && $csvTime < $start) {
|
|
|
67 |
continue;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (!empty($end) && $csvTime > $end) {
|
|
|
71 |
continue;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$result[$csvToken] = [
|
|
|
75 |
'token' => $csvToken,
|
|
|
76 |
'ip' => $csvIp,
|
|
|
77 |
'method' => $csvMethod,
|
|
|
78 |
'url' => $csvUrl,
|
|
|
79 |
'time' => $csvTime,
|
|
|
80 |
'parent' => $csvParent,
|
|
|
81 |
'status_code' => $csvStatusCode,
|
|
|
82 |
];
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
fclose($file);
|
|
|
86 |
|
|
|
87 |
return array_values($result);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
public function purge()
|
|
|
91 |
{
|
|
|
92 |
$flags = \FilesystemIterator::SKIP_DOTS;
|
|
|
93 |
$iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
|
|
|
94 |
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
|
|
|
95 |
|
|
|
96 |
foreach ($iterator as $file) {
|
|
|
97 |
if (is_file($file)) {
|
|
|
98 |
unlink($file);
|
|
|
99 |
} else {
|
|
|
100 |
rmdir($file);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function read(string $token): ?Profile
|
|
|
106 |
{
|
|
|
107 |
return $this->doRead($token);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* @throws \RuntimeException
|
|
|
112 |
*/
|
|
|
113 |
public function write(Profile $profile): bool
|
|
|
114 |
{
|
|
|
115 |
$file = $this->getFilename($profile->getToken());
|
|
|
116 |
|
|
|
117 |
$profileIndexed = is_file($file);
|
|
|
118 |
if (!$profileIndexed) {
|
|
|
119 |
// Create directory
|
|
|
120 |
$dir = \dirname($file);
|
|
|
121 |
if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
|
|
|
122 |
throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
$profileToken = $profile->getToken();
|
|
|
127 |
// when there are errors in sub-requests, the parent and/or children tokens
|
|
|
128 |
// may equal the profile token, resulting in infinite loops
|
|
|
129 |
$parentToken = $profile->getParentToken() !== $profileToken ? $profile->getParentToken() : null;
|
|
|
130 |
$childrenToken = array_filter(array_map(function (Profile $p) use ($profileToken) {
|
|
|
131 |
return $profileToken !== $p->getToken() ? $p->getToken() : null;
|
|
|
132 |
}, $profile->getChildren()));
|
|
|
133 |
|
|
|
134 |
// Store profile
|
|
|
135 |
$data = [
|
|
|
136 |
'token' => $profileToken,
|
|
|
137 |
'parent' => $parentToken,
|
|
|
138 |
'children' => $childrenToken,
|
|
|
139 |
'data' => $profile->getCollectors(),
|
|
|
140 |
'ip' => $profile->getIp(),
|
|
|
141 |
'method' => $profile->getMethod(),
|
|
|
142 |
'url' => $profile->getUrl(),
|
|
|
143 |
'time' => $profile->getTime(),
|
|
|
144 |
'status_code' => $profile->getStatusCode(),
|
|
|
145 |
];
|
|
|
146 |
|
|
|
147 |
$data = serialize($data);
|
|
|
148 |
|
|
|
149 |
if (\function_exists('gzencode')) {
|
|
|
150 |
$data = gzencode($data, 3);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
if (false === file_put_contents($file, $data, \LOCK_EX)) {
|
|
|
154 |
return false;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
if (!$profileIndexed) {
|
|
|
158 |
// Add to index
|
|
|
159 |
if (false === $file = fopen($this->getIndexFilename(), 'a')) {
|
|
|
160 |
return false;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
fputcsv($file, [
|
|
|
164 |
$profile->getToken(),
|
|
|
165 |
$profile->getIp(),
|
|
|
166 |
$profile->getMethod(),
|
|
|
167 |
$profile->getUrl(),
|
|
|
168 |
$profile->getTime(),
|
|
|
169 |
$profile->getParentToken(),
|
|
|
170 |
$profile->getStatusCode(),
|
|
|
171 |
]);
|
|
|
172 |
fclose($file);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
return true;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Gets filename to store data, associated to the token.
|
|
|
180 |
*/
|
|
|
181 |
protected function getFilename(string $token): string
|
|
|
182 |
{
|
|
|
183 |
// Uses 4 last characters, because first are mostly the same.
|
|
|
184 |
$folderA = substr($token, -2, 2);
|
|
|
185 |
$folderB = substr($token, -4, 2);
|
|
|
186 |
|
|
|
187 |
return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Gets the index filename.
|
|
|
192 |
*/
|
|
|
193 |
protected function getIndexFilename(): string
|
|
|
194 |
{
|
|
|
195 |
return $this->folder.'/index.csv';
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Reads a line in the file, backward.
|
|
|
200 |
*
|
|
|
201 |
* This function automatically skips the empty lines and do not include the line return in result value.
|
|
|
202 |
*
|
|
|
203 |
* @param resource $file The file resource, with the pointer placed at the end of the line to read
|
|
|
204 |
*/
|
|
|
205 |
protected function readLineFromFile($file): mixed
|
|
|
206 |
{
|
|
|
207 |
$line = '';
|
|
|
208 |
$position = ftell($file);
|
|
|
209 |
|
|
|
210 |
if (0 === $position) {
|
|
|
211 |
return null;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
while (true) {
|
|
|
215 |
$chunkSize = min($position, 1024);
|
|
|
216 |
$position -= $chunkSize;
|
|
|
217 |
fseek($file, $position);
|
|
|
218 |
|
|
|
219 |
if (0 === $chunkSize) {
|
|
|
220 |
// bof reached
|
|
|
221 |
break;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
$buffer = fread($file, $chunkSize);
|
|
|
225 |
|
|
|
226 |
if (false === ($upTo = strrpos($buffer, "\n"))) {
|
|
|
227 |
$line = $buffer.$line;
|
|
|
228 |
continue;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
$position += $upTo;
|
|
|
232 |
$line = substr($buffer, $upTo + 1).$line;
|
|
|
233 |
fseek($file, max(0, $position), \SEEK_SET);
|
|
|
234 |
|
|
|
235 |
if ('' !== $line) {
|
|
|
236 |
break;
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
return '' === $line ? null : $line;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
protected function createProfileFromData(string $token, array $data, Profile $parent = null)
|
|
|
244 |
{
|
|
|
245 |
$profile = new Profile($token);
|
|
|
246 |
$profile->setIp($data['ip']);
|
|
|
247 |
$profile->setMethod($data['method']);
|
|
|
248 |
$profile->setUrl($data['url']);
|
|
|
249 |
$profile->setTime($data['time']);
|
|
|
250 |
$profile->setStatusCode($data['status_code']);
|
|
|
251 |
$profile->setCollectors($data['data']);
|
|
|
252 |
|
|
|
253 |
if (!$parent && $data['parent']) {
|
|
|
254 |
$parent = $this->read($data['parent']);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
if ($parent) {
|
|
|
258 |
$profile->setParent($parent);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
foreach ($data['children'] as $token) {
|
|
|
262 |
if (null !== $childProfile = $this->doRead($token, $profile)) {
|
|
|
263 |
$profile->addChild($childProfile);
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
return $profile;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
private function doRead($token, Profile $profile = null): ?Profile
|
|
|
271 |
{
|
|
|
272 |
if (!$token || !file_exists($file = $this->getFilename($token))) {
|
|
|
273 |
return null;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
$h = fopen($file, 'r');
|
|
|
277 |
flock($h, \LOCK_SH);
|
|
|
278 |
$data = stream_get_contents($h);
|
|
|
279 |
flock($h, \LOCK_UN);
|
|
|
280 |
fclose($h);
|
|
|
281 |
|
|
|
282 |
if (\function_exists('gzdecode')) {
|
|
|
283 |
$data = @gzdecode($data) ?: $data;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
if (!$data = unserialize($data)) {
|
|
|
287 |
return null;
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
return $this->createProfileFromData($token, $data, $profile);
|
|
|
291 |
}
|
|
|
292 |
}
|