1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* This file is part of the Shieldon package.
|
4
|
|
*
|
5
|
|
* (c) Terry L. <contact@terryl.in>
|
6
|
|
*
|
7
|
|
* For the full copyright and license information, please view the LICENSE
|
8
|
|
* file that was distributed with this source code.
|
9
|
|
*
|
10
|
|
* php version 7.1.0
|
11
|
|
*
|
12
|
|
* @category Web-security
|
13
|
|
* @package Shieldon
|
14
|
|
* @author Terry Lin <contact@terryl.in>
|
15
|
|
* @copyright 2019 terrylinooo
|
16
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
|
17
|
|
* @link https://github.com/terrylinooo/shieldon
|
18
|
|
* @see https://shieldon.io
|
19
|
|
*/
|
20
|
|
|
21
|
|
declare(strict_types=1);
|
22
|
|
|
23
|
|
namespace Shieldon\Firewall\Driver;
|
24
|
|
|
25
|
|
use Shieldon\Firewall\Driver\DriverInterface;
|
26
|
|
|
27
|
|
/**
|
28
|
|
* Abstract Driver.
|
29
|
|
*/
|
30
|
|
abstract class AbstractDriver implements DriverInterface
|
31
|
|
{
|
32
|
|
/**
|
33
|
|
* {@inheritDoc}
|
34
|
|
*
|
35
|
|
* @param string $ip The data id of the entry to fetch.
|
36
|
|
* @param string $type The type of data table. accepts: filter | session | rule
|
37
|
|
*
|
38
|
|
* @return array
|
39
|
|
*/
|
40
|
3
|
public function get(string $ip, string $type = 'filter'): array
|
41
|
|
{
|
42
|
3
|
return $this->doFetch($ip, $type);
|
43
|
|
}
|
44
|
|
|
45
|
|
/**
|
46
|
|
* {@inheritDoc}
|
47
|
|
*
|
48
|
|
* @param string $type The type of data table. accepts: filter | session | rule
|
49
|
|
*
|
50
|
|
* @return array
|
51
|
|
*/
|
52
|
3
|
public function getAll(string $type = 'filter'): array
|
53
|
|
{
|
54
|
3
|
return $this->doFetchAll($type);
|
55
|
|
}
|
56
|
|
|
57
|
|
/**
|
58
|
|
* {@inheritDoc}
|
59
|
|
*
|
60
|
|
* @param string $ip The IP address as well as the data id.
|
61
|
|
* @param string $type The type of data table. accepts: filter | session | rule
|
62
|
|
*
|
63
|
|
* @return bool
|
64
|
|
*/
|
65
|
3
|
public function has(string $ip, string $type = 'filter'): bool
|
66
|
|
{
|
67
|
3
|
return $this->checkExist($ip, $type);
|
68
|
|
}
|
69
|
|
|
70
|
|
/**
|
71
|
|
* {@inheritDoc}
|
72
|
|
*
|
73
|
|
* @param string $ip The IP address as well as the data id.
|
74
|
|
* @param array $data The data.
|
75
|
|
* @param string $type The type of data table. accepts: filter | session | rule
|
76
|
|
* @param int $expire The data will be deleted after expiring.
|
77
|
|
*
|
78
|
|
* @return bool
|
79
|
|
*/
|
80
|
3
|
public function save(string $ip, array $data, string $type = 'filter', int $expire = 0): bool
|
81
|
|
{
|
82
|
3
|
return $this->doSave($ip, $data, $type, $expire);
|
83
|
|
}
|
84
|
|
|
85
|
|
/**
|
86
|
|
* {@inheritDoc}
|
87
|
|
*
|
88
|
|
* @param string $ip The IP address as well as the data id.
|
89
|
|
* @param string $type The type of data table. accepts: filter | session | rule
|
90
|
|
*
|
91
|
|
* @return bool true if the data entry is deleted successfully.
|
92
|
|
* deleting a non-existing entry is considered successful.
|
93
|
|
* return false overwise.
|
94
|
|
*/
|
95
|
3
|
public function delete(string $ip, string $type = 'filter'): bool
|
96
|
|
{
|
97
|
3
|
return $this->doDelete($ip, $type);
|
98
|
|
}
|
99
|
|
|
100
|
|
/**
|
101
|
|
* {@inheritDoc}
|
102
|
|
*
|
103
|
|
* @return bool
|
104
|
|
*/
|
105
|
3
|
public function rebuild(): bool
|
106
|
|
{
|
107
|
3
|
return $this->doRebuild();
|
108
|
|
}
|
109
|
|
|
110
|
|
/**
|
111
|
|
* {@inheritDoc}
|
112
|
|
*
|
113
|
|
* @param bool $dbCheck This is for creating data tables automatically
|
114
|
|
*
|
115
|
|
* @return void
|
116
|
|
*/
|
117
|
3
|
public function init(bool $dbCheck = true): void
|
118
|
|
{
|
119
|
3
|
$this->doInitialize($dbCheck);
|
120
|
|
}
|
121
|
|
|
122
|
|
/**
|
123
|
|
* Implement fetch.
|
124
|
|
*
|
125
|
|
* @param string $ip The data id of the entry to fetch.
|
126
|
|
* @param string $type The type of the data table.
|
127
|
|
*
|
128
|
|
* @return array The data or an empty array.
|
129
|
|
*/
|
130
|
|
abstract protected function doFetch(string $ip, string $type = 'filter'): array;
|
131
|
|
|
132
|
|
/**
|
133
|
|
* Implement fetch all.
|
134
|
|
*
|
135
|
|
* @param string $type The data type.
|
136
|
|
*
|
137
|
|
* @return array The data or an empty array.
|
138
|
|
*/
|
139
|
|
abstract protected function doFetchAll(string $type = 'filter'): array;
|
140
|
|
|
141
|
|
/**
|
142
|
|
* Implement has.
|
143
|
|
*
|
144
|
|
* @param string $ip The data id of the entry to check for.
|
145
|
|
* @param string $type The type of the data table.
|
146
|
|
*
|
147
|
|
* @return bool
|
148
|
|
*/
|
149
|
|
abstract protected function checkExist(string $ip, string $type = 'filter'): bool;
|
150
|
|
|
151
|
|
/**
|
152
|
|
* Implement save.
|
153
|
|
*
|
154
|
|
* @param string $ip The data id.
|
155
|
|
* @param array $data The data.
|
156
|
|
* @param string $type The type of the data table.
|
157
|
|
* @param int $expire The data will be deleted after expiring.
|
158
|
|
*
|
159
|
|
* @return bool
|
160
|
|
*/
|
161
|
|
abstract protected function doSave(string $ip, array $data, string $type = 'filter', $expire = 0): bool;
|
162
|
|
|
163
|
|
/**
|
164
|
|
* Implement delete.
|
165
|
|
*
|
166
|
|
* @param string $ip The data id.
|
167
|
|
* @param string $type The type of the data table.
|
168
|
|
*
|
169
|
|
* @return bool
|
170
|
|
*/
|
171
|
|
abstract protected function doDelete(string $ip, string $type = 'filter'): bool;
|
172
|
|
|
173
|
|
/**
|
174
|
|
* Rebuild data tables.
|
175
|
|
*
|
176
|
|
* @return bool
|
177
|
|
*/
|
178
|
|
abstract protected function doRebuild(): bool;
|
179
|
|
|
180
|
|
/**
|
181
|
|
* Initial data tables.
|
182
|
|
*
|
183
|
|
* @param bool $dbCheck This is for creating data tables automatically
|
184
|
|
* Turn it off, if you don't want to check data tables every pageview.
|
185
|
|
*
|
186
|
|
* @return void
|
187
|
|
*/
|
188
|
|
abstract protected function doInitialize(bool $dbCheck = true): void;
|
189
|
|
}
|