|
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
|
|
|
* php version 7.1.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @category Web-security |
|
13
|
|
|
* @package Shieldon |
|
14
|
|
|
* @author Terry Lin <[email protected]> |
|
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\Component; |
|
24
|
|
|
|
|
25
|
|
|
use function array_keys; |
|
26
|
|
|
use function array_push; |
|
27
|
|
|
use function in_array; |
|
28
|
|
|
use function strpos; |
|
29
|
|
|
|
|
30
|
|
|
/* |
|
31
|
|
|
* Denied trait. |
|
32
|
|
|
*/ |
|
33
|
|
|
trait DeniedTrait |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Public methods | Desctiotion |
|
37
|
|
|
* ----------------------|--------------------------------------------- |
|
38
|
|
|
* setDeniedItems | Add items to the blacklist pool. |
|
39
|
|
|
* setDeniedItem | Add an item to the blacklist pool. |
|
40
|
|
|
* getDeniedItems | Get items from the blacklist pool. |
|
41
|
|
|
* getDeniedItem | Get an item from the blacklist pool. |
|
42
|
|
|
* removeDeniedItem | Remove a denied item if exists. |
|
43
|
|
|
* removeDeniedItems | Remove all denied items. |
|
44
|
|
|
* hasDeniedItem | Check if a denied item exists. |
|
45
|
|
|
* getDenyWithPrefix | Check if a denied items exist with the same prefix. |
|
46
|
|
|
* removeDenyWithPrefix | Remove denied items with the same prefix. |
|
47
|
|
|
* isDenied | Check if an item is denied? |
|
48
|
|
|
* ----------------------|--------------------------------------------- |
|
49
|
|
|
*/ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Data pool for hard blacklist. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $deniedList = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Add items to the blacklist pool. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $itemList String list. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
87 |
|
public function setDeniedItems(array $itemList): void |
|
66
|
|
|
{ |
|
67
|
87 |
|
$this->deniedList = $itemList; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Add an item to the blacklist pool. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string|array $value The value of the data. |
|
74
|
|
|
* @param string $key The key of the data. |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function setDeniedItem($value, string $key = ''): void |
|
79
|
|
|
{ |
|
80
|
9 |
|
if (!empty($key)) { |
|
81
|
2 |
|
$this->deniedList[$key] = $value; |
|
82
|
|
|
} elseif (!in_array($value, $this->deniedList)) { |
|
83
|
7 |
|
array_push($this->deniedList, $value); |
|
84
|
7 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get items from the blacklist pool. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getDeniedItems(): array |
|
93
|
18 |
|
{ |
|
94
|
|
|
return $this->deniedList; |
|
95
|
18 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get an item from the blacklist pool. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $key The key of the data field. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string|array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getDeniedItem(string $key) |
|
105
|
1 |
|
{ |
|
106
|
|
|
return $this->deniedList[$key] ?? ''; |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Remove a denied item if exists. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $key The key of the data. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function removeDeniedItem(string $key): void |
|
117
|
3 |
|
{ |
|
118
|
|
|
unset($this->deniedList[$key]); |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Remove all denied items. |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
public function removeDeniedItems(): void |
|
127
|
2 |
|
{ |
|
128
|
|
|
$this->deniedList = []; |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Check if a denied item exists. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $key The key of the data. |
|
135
|
|
|
* |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
public function hasDeniedItem(string $key): bool |
|
139
|
1 |
|
{ |
|
140
|
|
|
return isset($this->deniedList[$key]); |
|
141
|
1 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Check if a denied items exist with the same prefix. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $key The key of the data. |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getDenyWithPrefix(string $key): array |
|
151
|
1 |
|
{ |
|
152
|
|
|
$temp = []; |
|
153
|
1 |
|
foreach ($this->deniedList as $keyName => $value) { |
|
154
|
1 |
|
if (strpos($keyName, $key) === 0) { |
|
155
|
1 |
|
$temp[$keyName] = $value; |
|
156
|
1 |
|
} |
|
157
|
|
|
} |
|
158
|
|
|
return $temp; |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Remove denied items with the same prefix. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $key The key of the data. |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
public function removeDenyWithPrefix(string $key): void |
|
169
|
1 |
|
{ |
|
170
|
|
|
foreach (array_keys($this->deniedList) as $keyName) { |
|
171
|
1 |
|
if (strpos($keyName, $key) === 0) { |
|
172
|
1 |
|
unset($this->deniedList[$keyName]); |
|
173
|
1 |
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// @codeCoverageIgnoreStart |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Is denied? |
|
181
|
|
|
* This method should adjust in extended class if need. |
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
public function isDenied(): bool |
|
186
|
|
|
{ |
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// @codeCoverageIgnoreEnd |
|
191
|
|
|
} |
|
192
|
|
|
|