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
|
|
|
* Allowed trait. |
32
|
|
|
*/ |
33
|
|
|
trait AllowedTrait |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Public methods | Desctiotion |
37
|
|
|
* ----------------------|--------------------------------------------- |
38
|
|
|
* setAllowedItems | Add items to the whitelist pool. |
39
|
|
|
* setAllowedItem | Add an item to the whitelist pool. |
40
|
|
|
* getAllowedItems | Get items from the whitelist pool. |
41
|
|
|
* getAllowedItem | Get an item from the whitelist pool. |
42
|
|
|
* removeAllowedItem | Remove an allowed item if exists. |
43
|
|
|
* removeAllowedItems | Remove all allowed items. |
44
|
|
|
* hasAllowedItem | Check if an allowed item exists. |
45
|
|
|
* getAllowByPrefix | Check if an allowed item exists have the same prefix. |
46
|
|
|
* removeAllowByPrefix | Remove allowed items with the same prefix. |
47
|
|
|
* isAllowed | Check if an item is allowed? |
48
|
|
|
* ----------------------|--------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Data pool for hard whitelist. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $allowedList = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add items to the whitelist pool. |
60
|
|
|
* |
61
|
|
|
* @param array $itemList String list. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function setAllowedItems(array $itemList): void |
66
|
|
|
{ |
67
|
|
|
$this->allowedList = $itemList; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add an item to the whitelist 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
|
|
|
public function setAllowedItem($value, string $key = ''): void |
79
|
|
|
{ |
80
|
|
|
if (!empty($key)) { |
81
|
|
|
$this->allowedList[$key] = $value; |
82
|
|
|
|
83
|
|
|
} elseif (!in_array($value, $this->allowedList)) { |
84
|
|
|
array_push($this->allowedList, $value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get items from the whitelist pool. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getAllowedItems(): array |
94
|
|
|
{ |
95
|
|
|
return $this->allowedList; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get an item from the whitelist pool. |
100
|
|
|
* |
101
|
|
|
* @return string|array |
102
|
|
|
*/ |
103
|
|
|
public function getAllowedItem(string $key) |
104
|
|
|
{ |
105
|
|
|
return $this->allowedList[$key] ?? ''; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove an allowed item if exists. |
110
|
|
|
* |
111
|
|
|
* @param string $key The key of the data. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function removeAllowedItem(string $key): void |
116
|
|
|
{ |
117
|
|
|
unset($this->allowedList[$key]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Remove all items. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function removeAllowedItems(): void |
126
|
|
|
{ |
127
|
|
|
$this->allowedList = []; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if an allowed item exists. |
132
|
|
|
* |
133
|
|
|
* @param string $key The key of the data. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function hasAllowedItem(string $key): bool |
138
|
|
|
{ |
139
|
|
|
return isset($this->allowedList[$key]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if an allowed item exists have the same prefix. |
144
|
|
|
* |
145
|
|
|
* @param string $key The key of the data. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getAllowByPrefix(string $key): array |
150
|
|
|
{ |
151
|
|
|
$temp = []; |
152
|
|
|
foreach ($this->allowedList as $keyName => $value) { |
153
|
|
|
if (strpos($keyName, $key) === 0) { |
154
|
|
|
$temp[$keyName] = $value; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
return $temp; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Remove allowed items with the same prefix. |
162
|
|
|
* |
163
|
|
|
* @param string $key The key of the data. |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function removeAllowByPrefix(string $key): void |
168
|
|
|
{ |
169
|
|
|
foreach (array_keys($this->allowedList) as $keyName) { |
170
|
|
|
if (strpos($keyName, $key) === 0) { |
171
|
|
|
unset($this->allowedList[$keyName]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Is allowed? |
178
|
|
|
* This method should adjust in extended class if need. |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function isAllowed(): bool |
183
|
|
|
{ |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|