|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Driver; |
|
14
|
|
|
|
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
use function file_exists; |
|
18
|
|
|
use function file_put_contents; |
|
19
|
|
|
use function is_dir; |
|
20
|
|
|
use function mkdir; |
|
21
|
|
|
use function str_replace; |
|
22
|
|
|
use function umask; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* SQL Driver Trait |
|
26
|
|
|
*/ |
|
27
|
|
|
trait FileDriverTrait |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Create a directory for storing data files. |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function createDirectory(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
$conA = $conB = $conC = false; |
|
37
|
|
|
|
|
38
|
|
|
$checkingFile = $this->directory . '/' . $this->channel . '_' . $this->checkPoint; |
|
39
|
|
|
|
|
40
|
|
|
// If the check-point file exists, we do not create directory because they may |
|
41
|
|
|
// already exist. |
|
42
|
|
|
if (!file_exists($checkingFile)) { |
|
43
|
|
|
$originalUmask = umask(0); |
|
44
|
|
|
|
|
45
|
|
|
if (!is_dir($this->getDirectory('filter'))) { |
|
46
|
|
|
$conA = mkdir($this->getDirectory('filter'), 0777, true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!is_dir($this->getDirectory('rule'))) { |
|
50
|
|
|
$conB = mkdir($this->getDirectory('rule'), 0777, true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!is_dir($this->getDirectory('session'))) { |
|
54
|
|
|
$conC = mkdir($this->getDirectory('session'), 0777, true); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!($conA && $conB && $conC)) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
file_put_contents($checkingFile, ' '); |
|
62
|
|
|
umask($originalUmask); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Check the directory if is writable. |
|
70
|
|
|
* |
|
71
|
|
|
* Not real use in Kernel. only use it in unit tests. |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function checkDirectory(): bool |
|
76
|
|
|
{ |
|
77
|
|
|
if (!is_dir($this->directory) || !is_writable($this->directory)) { |
|
78
|
|
|
throw new RuntimeException( |
|
79
|
|
|
'The directory defined by File Driver must be writable. (' . $this->directory . ')' |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get filename. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $ip IP address. |
|
90
|
|
|
* @param string $type The table name of the data cycle. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
private function getFilename(string $ip, string $type = 'filter'): string |
|
95
|
|
|
{ |
|
96
|
|
|
$ip = str_replace(':', '-', $ip); |
|
97
|
|
|
$path = []; |
|
98
|
|
|
|
|
99
|
|
|
$path['filter'] = $this->directory . '/' . $this->tableFilterLogs . '/' . $ip . '.' . $this->extension; |
|
100
|
|
|
$path['session'] = $this->directory . '/' . $this->tableSessions . '/' . $ip . '.' . $this->extension; |
|
101
|
|
|
$path['rule'] = $this->directory . '/' . $this->tableRuleList . '/' . $ip . '.' . $this->extension; |
|
102
|
|
|
|
|
103
|
|
|
return $path[$type] ?? ''; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get directory. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $type The table name of the data cycle. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
private function getDirectory(string $type = 'filter'): string |
|
114
|
|
|
{ |
|
115
|
|
|
$path = []; |
|
116
|
|
|
|
|
117
|
|
|
$path['filter'] = $this->directory . '/' . $this->tableFilterLogs; |
|
118
|
|
|
$path['session'] = $this->directory . '/' . $this->tableSessions; |
|
119
|
|
|
$path['rule'] = $this->directory . '/' . $this->tableRuleList; |
|
120
|
|
|
|
|
121
|
|
|
return $path[$type] ?? ''; |
|
122
|
|
|
} |
|
123
|
|
|
} |