Header   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 24
c 2
b 0
f 0
dl 0
loc 112
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHeaders() 0 3 1
A getDenyStatusCode() 0 3 1
B isDenied() 0 25 8
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 Shieldon\Firewall\Component\ComponentProvider;
26
use Shieldon\Firewall\Component\DeniedTrait;
27
use Shieldon\Firewall\IpTrait;
28
use function Shieldon\Firewall\get_request;
29
30
/**
31
 * Header component.
32
 */
33
class Header extends ComponentProvider
34
{
35
    /**
36
     *   Public methods       | Desctiotion
37
     *  ----------------------|---------------------------------------------
38
     *   setIp                | Set an IP address.
39
     *   getIp                | Get current set IP.
40
     *   setRdns              | Set a RDNS record for the check.
41
     *   getRdns              | Get IP resolved hostname.
42
     *  ----------------------|---------------------------------------------
43
     */
44
    use IpTrait;
45
46
    /**
47
     *   Public methods       | Desctiotion
48
     *  ----------------------|---------------------------------------------
49
     *   setDeniedItems       | Add items to the blacklist pool.
50
     *   setDeniedItem        | Add an item to the blacklist pool.
51
     *   getDeniedItems       | Get items from the blacklist pool.
52
     *   getDeniedItem        | Get items from the blacklist pool.
53
     *   removeDeniedItem     | Remove a denied item if exists.
54
     *   removeDeniedItems    | Remove all denied items.
55
     *   hasDeniedItem        | Check if a denied item exists.
56
     *   getDenyWithPrefix    | Check if a denied item exists have the same prefix.
57
     *   removeDenyWithPrefix | Remove denied items with the same prefix.
58
     *   isDenied             | Check if an item is denied?
59
     *  ----------------------|---------------------------------------------
60
     */
61
    use DeniedTrait;
62
63
    /**
64
     * Constant
65
     */
66
    const STATUS_CODE = 83;
67
68
    /**
69
     * Very common requests from normal users.
70
     *
71
     * @var array
72
     */
73
    protected $commonHeaderFileds = [
74
        'Accept',
75
        'Accept-Language',
76
        'Accept-Encoding',
77
    ];
78
79
    /**
80
     * Header information.
81
     *
82
     * @var array
83
     */
84
    protected $headers = [];
85
86
    /**
87
     * Header component constructor.
88
     */
89 96
    public function __construct()
90
    {
91 96
        $this->headers = get_request()->getHeaders();
92 96
        $this->deniedList = [];
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     *
98
     * @return bool
99
     */
100 12
    public function isDenied(): bool
101
    {
102 12
        if (!empty($this->deniedList)) {
103 1
            $intersect = array_intersect_key($this->deniedList, $this->headers);
104
105 1
            foreach ($intersect as $headerName => $headerValue) {
106 1
                $requestHeader = get_request()->getHeaderLine($headerName);
107
108
                // When found a header field contains a prohibited string.
109 1
                if (stripos($requestHeader, $headerValue) !== false) {
110 1
                    return true;
111
                }
112
            }
113
        }
114
115 12
        if ($this->strictMode) {
116
            foreach ($this->commonHeaderFileds as $fieldName) {
117 5
                // If strict mode is on, this value must be found.
118
                if (!isset($this->headers[$fieldName]) && empty($this->headers['referer'])) {
119 5
                    return true;
120 5
                }
121
            }
122
        }
123
124
        return false;
125 8
    }
126
127
    /**
128
     * All request headers.
129
     *
130
     * @return array
131
     */
132
    public function getHeaders(): array
133 1
    {
134
        return $this->headers;
135 1
    }
136
137
    /**
138
     * Unique deny status code.
139
     *
140
     * @return int
141
     */
142
    public function getDenyStatusCode(): int
143 5
    {
144
        return self::STATUS_CODE;
145 5
    }
146
}
147