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

29
use function gethostbyname;
30
use function implode;
31
use function preg_match;
32

33
/**
34
 * Rdns component.
35
 */
36
class Rdns extends ComponentProvider
37
{
38
    /**
39
     *   Public methods       | Desctiotion
40
     *  ----------------------|---------------------------------------------
41
     *   setIp                | Set an IP address.
42
     *   getIp                | Get current set IP.
43
     *   setRdns              | Set a RDNS record for the check.
44
     *   getRdns              | Get IP resolved hostname.
45
     *  ----------------------|---------------------------------------------
46
     */
47
    use IpTrait;
48

49
    /**
50
     *   Public methods       | Desctiotion
51
     *  ----------------------|---------------------------------------------
52
     *   setDeniedItems       | Add items to the blacklist pool.
53
     *   setDeniedItem        | Add an item to the blacklist pool.
54
     *   getDeniedItems       | Get items from the blacklist pool.
55
     *   getDeniedItem        | Get items from the blacklist pool.
56
     *   removeDeniedItem     | Remove a denied item if exists.
57
     *   removeDeniedItems    | Remove all denied items.
58
     *   hasDeniedItem        | Check if a denied item exists.
59
     *   getDenyWithPrefix    | Check if a denied item exists have the same prefix.
60
     *   removeDenyWithPrefix | Remove denied items with the same prefix.
61
     *   isDenied             | Check if an item is denied?
62
     *  ----------------------|---------------------------------------------
63
     */
64
    use DeniedTrait;
65

66
    /**
67
     * Constant
68
     */
69
    const STATUS_CODE = 82;
70

71
    /**
72
     * Constructor.
73
     */
74 3
    public function __construct()
75
    {
76
        // RDNS for robot's IP address.
77 3
        $this->deniedList = [
78
            'unknown_1' => '.webcrawler.link',
79
        ]; 
80
    }
81

82
    /**
83
     * {@inheritDoc}
84
     * 
85
     * @return bool
86
     */
87 3
    public function isDenied(): bool
88
    {
89 3
        if (!empty($this->deniedList)) {
90 3
            if (preg_match('/(' . implode('|', $this->deniedList). ')/i', $this->rdns)) {
91 3
                return true;
92
            }
93
        }
94

95 3
        if ($this->strictMode) {
96

97
            // If strict mode is on, this value can not be empty.
98 3
            if (empty($this->rdns)) {
99 3
                return true;
100
            }
101

102
            // If the RDNS is an IP adress, not a FQDN.
103 3
            if ($this->ip === $this->rdns) {
104 3
                return true;
105
            }
106

107
            // confirm hostname's IP again
108 3
            $ip = gethostbyname($this->rdns);
109

110
            // If the IP is different as hostname's resolved IP.
111 3
            if ($ip !== $this->ip) {
112 3
                return true;
113
            } 
114
        }
115

116 3
        return false;
117
    }
118

119
    /**
120
     * Unique deny status code.
121
     *
122
     * @return int
123
     */
124 3
    public function getDenyStatusCode(): int
125
    {
126 3
        return self::STATUS_CODE;
127
    }
128
}

Read our documentation on viewing source code .

Loading