-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.php
53 lines (48 loc) · 1.74 KB
/
Storage.php
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
<?php
const RULES_FILE_NAME = "data/configurate.csv";
const RULES_ONLY_ACTIVE = true;
const RULES_ALL = false;
class Storage {
function readRules(bool $onlyActive, ?string $clientFilter = NULL): array {
if (($handle = @fopen(RULES_FILE_NAME, "r")) === FALSE) {
return [];
}
$result = [];
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$row = [
'ip' => $data[0],
'proxy' => $data[1],
'port' => $data[2],
'timeEnd' => $data[3],
'pattern' => $data[4],
'duration' => $data[5],
];
if ($clientFilter !== NULL && $clientFilter != $row['ip']) {
continue;
}
if ($onlyActive && $row['timeEnd'] < time()) {
continue;
}
$result[] = $row;
}
fclose($handle);
return $result;
}
function addRule(string $ip, string $proxy, string $port, int $duration, string $pattern, $since = NULL) {
$existing = $this->readRules(RULES_ALL);
if (($handle = fopen(RULES_FILE_NAME, "w")) === FALSE) {
return;
}
$since = $since ?? time();
$timeEnd = $since + $duration;
$write = [$ip, $proxy, $port, $timeEnd, $pattern, $duration];
fputcsv($handle, $write);
foreach ($existing as $rule) {
if ($ip == $rule['ip'] && $proxy == $rule['proxy'] && $port == $rule['port'] && $pattern == $rule['pattern']) {
continue;
}
fputcsv($handle, [$rule['ip'], $rule['proxy'], $rule['port'], $rule['timeEnd'], $rule['pattern'], $rule['duration']]);
}
fclose($handle);
}
}