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 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 3
    public function __construct()
90
    {
91 3
        $this->headers = get_request()->getHeaders();
92 3
        $this->deniedList = [];
93
    }
94

95
    /**
96
     * {@inheritDoc}
97
     * 
98
     * @return bool
99
     */
100 3
    public function isDenied(): bool
101
    {
102 3
        if (!empty($this->deniedList)) {
103 3
            $intersect = array_intersect_key($this->deniedList, $this->headers);
104

105 3
            foreach ($intersect as $headerName => $headerValue) {
106 3
                $requestHeader = get_request()->getHeaderLine($headerName);
107

108
                // When found a header field contains a prohibited string.
109 3
                if (stripos($requestHeader, $headerValue) !== false) {
110 3
                    return true;
111
                }
112
            }
113
        }
114

115 3
        if ($this->strictMode) {
116

117 3
            foreach ($this->commonHeaderFileds as $fieldName) {
118
                // If strict mode is on, this value must be found.
119 3
                if (!isset($this->headers[$fieldName]) && empty($this->headers['referer'])) {
120 3
                    return true;
121
                }
122
            }
123
        }
124

125 3
        return false;
126
    }
127

128
    /**
129
     * All request headers.
130
     *
131
     * @return array
132
     */
133 3
    public function getHeaders(): array
134
    {
135 3
        return $this->headers;
136
    }
137

138
    /**
139
     * Unique deny status code.
140
     *
141
     * @return int
142
     */
143 3
    public function getDenyStatusCode(): int
144
    {
145 3
        return self::STATUS_CODE;
146
    }
147
}

Read our documentation on viewing source code .

Loading