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
|
|
use function implode;
|
31
|
|
use function preg_match;
|
32
|
|
|
33
|
|
/**
|
34
|
|
* UserAgent component.
|
35
|
|
*/
|
36
|
|
class UserAgent 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 = 84;
|
70
|
|
|
71
|
|
/**
|
72
|
|
* Robot's user-agent text.
|
73
|
|
*
|
74
|
|
* @var string
|
75
|
|
*/
|
76
|
|
private $userAgent = '';
|
77
|
|
|
78
|
|
/**
|
79
|
|
* Constructor.
|
80
|
|
*/
|
81
|
3
|
public function __construct()
|
82
|
|
{
|
83
|
3
|
$this->userAgent = get_request()->getHeaderLine('user-agent');
|
84
|
|
|
85
|
|
/**
|
86
|
|
* Those robots are considered as bad behavior.
|
87
|
|
* Therefore we list them here.
|
88
|
|
*/
|
89
|
3
|
$this->deniedList = [
|
90
|
|
|
91
|
|
// Backlink crawlers
|
92
|
|
'Ahrefs', // http://ahrefs.com/robot/
|
93
|
|
'roger', // rogerbot (SEOMOZ)
|
94
|
|
'moz.com', // SEOMOZ crawlers
|
95
|
|
'MJ12bot', // Majestic crawlers
|
96
|
|
'findlinks', // http://wortschatz.uni-leipzig.de/findlinks
|
97
|
|
'Semrush', // http://www.semrush.com/bot.html
|
98
|
|
|
99
|
|
// Web information crawlers
|
100
|
|
'domain', // Domain name information crawlers.
|
101
|
|
'copyright', // Copyright information crawlers.
|
102
|
|
|
103
|
|
// Others
|
104
|
|
'archive', // Wayback machine
|
105
|
|
];
|
106
|
|
}
|
107
|
|
|
108
|
|
/**
|
109
|
|
* {@inheritDoc}
|
110
|
|
*
|
111
|
|
* @return bool
|
112
|
|
*/
|
113
|
3
|
public function isDenied(): bool
|
114
|
|
{
|
115
|
3
|
if (!empty($this->deniedList)) {
|
116
|
3
|
if (preg_match('/(' . implode('|', $this->deniedList). ')/i', $this->userAgent)) {
|
117
|
3
|
return true;
|
118
|
|
}
|
119
|
|
}
|
120
|
|
|
121
|
3
|
if ($this->strictMode) {
|
122
|
|
|
123
|
|
// If strict mode is on, this value can not be empty.
|
124
|
3
|
if (empty($this->userAgent)) {
|
125
|
3
|
return true;
|
126
|
|
}
|
127
|
|
}
|
128
|
|
|
129
|
3
|
return false;
|
130
|
|
}
|
131
|
|
|
132
|
|
/**
|
133
|
|
* {@inheritDoc}
|
134
|
|
*
|
135
|
|
* @return int
|
136
|
|
*/
|
137
|
3
|
public function getDenyStatusCode(): int
|
138
|
|
{
|
139
|
3
|
return self::STATUS_CODE;
|
140
|
|
}
|
141
|
|
}
|