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
 * 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 3
    public function setDeniedItems(array $itemList): void
66
    {
67 3
        $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 3
    public function setDeniedItem($value, string $key = ''): void
79
    {
80 3
        if (!empty($key)) {
81 3
            $this->deniedList[$key] = $value;
82

83 3
        } elseif (!in_array($value, $this->deniedList)) {
84 3
            array_push($this->deniedList, $value);
85
        }
86
    }
87

88
    /**
89
     * Get items from the blacklist pool.
90
     *
91
     * @return array
92
     */
93 3
    public function getDeniedItems(): array
94
    {
95 3
        return $this->deniedList;
96
    }
97

98
    /**
99
     * Get an item from the blacklist pool.
100
     * 
101
     * @param string $key The key of the data field.
102
     *
103
     * @return string|array
104
     */
105 3
    public function getDeniedItem(string $key)
106
    {
107 3
        return $this->deniedList[$key] ?? '';
108
    }
109

110
    /**
111
     * Remove a denied item if exists.
112
     *
113
     * @param string $key The key of the data.
114
     *
115
     * @return string
116
     */
117 3
    public function removeDeniedItem(string $key): void
118
    {
119 3
        unset($this->deniedList[$key]);
120
    }
121

122
    /**
123
     * Remove all denied items.
124
     *
125
     * @return void
126
     */
127 3
    public function removeDeniedItems(): void
128
    {
129 3
        $this->deniedList = [];
130
    }
131

132
    /**
133
     * Check if a denied item exists.
134
     *
135
     * @param string $key The key of the data.
136
     *
137
     * @return bool
138
     */
139 3
    public function hasDeniedItem(string $key): bool
140
    {
141 3
        return isset($this->deniedList[$key]);
142
    }
143

144
    /**
145
     * Check if a denied items exist with the same prefix.
146
     *
147
     * @param string $key The key of the data.
148
     *
149
     * @return array
150
     */
151 3
    public function getDenyWithPrefix(string $key): array
152
    {
153 3
        $temp = [];
154 3
        foreach ($this->deniedList as $keyName => $value) {
155 3
            if (strpos($keyName, $key) === 0) {
156 3
                $temp[$keyName] = $value;
157
            }
158
        }
159 3
        return $temp;
160
    }
161

162
    /**
163
     * Remove denied items with the same prefix.
164
     *
165
     * @param string $key The key of the data.
166
     *
167
     * @return void
168
     */
169 3
    public function removeDenyWithPrefix(string $key): void
170
    {
171 3
        foreach (array_keys($this->deniedList) as $keyName) {
172 3
            if (strpos($keyName, $key) === 0) {
173 3
                unset($this->deniedList[$keyName]);
174
            }
175
        }
176
    }
177

178
    // @codeCoverageIgnoreStart
179

180
    /**
181
     * Is denied?
182
     * This method should adjust in extended class if need.
183
     *
184
     * @return bool
185
     */
186
    public function isDenied(): bool
187
    {
188
        return false;
189
    }
190

191
    // @codeCoverageIgnoreEnd
192
}

Read our documentation on viewing source code .

Loading