Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
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\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 allowed items exist with 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 | 3 |
public function setAllowedItems(array $itemList): void |
66 |
{
|
|
67 | 3 |
$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 | 3 |
public function setAllowedItem($value, string $key = ''): void |
79 |
{
|
|
80 | 3 |
if (!empty($key)) { |
81 | 3 |
$this->allowedList[$key] = $value; |
82 |
|
|
83 | 3 |
} elseif (!in_array($value, $this->allowedList)) { |
84 | 3 |
array_push($this->allowedList, $value); |
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Get items from the whitelist pool.
|
|
90 |
*
|
|
91 |
* @return array
|
|
92 |
*/
|
|
93 | 3 |
public function getAllowedItems(): array |
94 |
{
|
|
95 | 3 |
return $this->allowedList; |
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Get an item from the whitelist pool.
|
|
100 |
*
|
|
101 |
* @return string|array
|
|
102 |
*/
|
|
103 | 3 |
public function getAllowedItem(string $key) |
104 |
{
|
|
105 | 3 |
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 | 3 |
public function removeAllowedItem(string $key): void |
116 |
{
|
|
117 | 3 |
unset($this->allowedList[$key]); |
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Remove all items.
|
|
122 |
*
|
|
123 |
* @return void
|
|
124 |
*/
|
|
125 | 3 |
public function removeAllowedItems(): void |
126 |
{
|
|
127 | 3 |
$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 | 3 |
public function hasAllowedItem(string $key): bool |
138 |
{
|
|
139 | 3 |
return isset($this->allowedList[$key]); |
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* Check if allowed items exist with the same prefix.
|
|
144 |
*
|
|
145 |
* @param string $key The key of the data.
|
|
146 |
*
|
|
147 |
* @return array
|
|
148 |
*/
|
|
149 | 3 |
public function getAllowByPrefix(string $key): array |
150 |
{
|
|
151 | 3 |
$temp = []; |
152 | 3 |
foreach ($this->allowedList as $keyName => $value) { |
153 | 3 |
if (strpos($keyName, $key) === 0) { |
154 | 3 |
$temp[$keyName] = $value; |
155 |
}
|
|
156 |
}
|
|
157 | 3 |
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 | 3 |
public function removeAllowByPrefix(string $key): void |
168 |
{
|
|
169 | 3 |
foreach (array_keys($this->allowedList) as $keyName) { |
170 | 3 |
if (strpos($keyName, $key) === 0) { |
171 | 3 |
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 | 3 |
public function isAllowed(): bool |
183 |
{
|
|
184 | 3 |
return false; |
185 |
}
|
|
186 |
}
|
Read our documentation on viewing source code .