1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus\Wrappers; |
4
|
|
|
|
5
|
|
|
class BotWrapper extends Wrapper |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $userAgentFound = 0; |
9
|
|
|
private $score = 0; |
10
|
|
|
|
11
|
|
|
public function process() |
12
|
|
|
{ |
13
|
|
|
$this->initWrapper($this->setLocalName()); |
14
|
|
|
|
15
|
|
|
$this->setRealReference($this->getReference()); |
16
|
|
|
$this->detectBot(); |
17
|
|
|
|
18
|
|
|
if ($this->userAgentFound > 0) { |
19
|
|
|
$this->setScore($this->score); |
20
|
|
|
$this->setResult(); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function setLocalName() |
25
|
|
|
{ |
26
|
|
|
$name = str_replace(__NAMESPACE__ . '\\', '', __CLASS__); |
27
|
|
|
return str_replace('Wrapper', '', $name); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function setRealReference($reference = '') |
31
|
|
|
{ |
32
|
|
|
if ($reference == '') { |
33
|
|
|
$this->setReference($this->getUserAgent()); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function getUserAgent() |
38
|
|
|
{ |
39
|
|
|
return isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function detectBot() |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
$params = $this->getParams(); |
46
|
|
|
if (empty($params)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
foreach ($params as $param) { |
51
|
|
|
if ($param != '') { |
52
|
|
|
$data = explode(":", $param); |
53
|
|
|
if (strpos(strtolower($this->sanitizeReference()), strtolower($data[0])) !== false) { |
54
|
|
|
$this->detectedBot($data); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function detectedBot($data) |
62
|
|
|
{ |
63
|
|
|
if (strpos(strtolower($this->getReference()), $data) !== false) { |
64
|
|
|
$this->setRealScore(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function setRealScore($data = []) |
69
|
|
|
{ |
70
|
|
|
$this->overRideScore($data); |
71
|
|
|
|
72
|
|
|
if ($this->score > 0) { |
73
|
|
|
$this->userAgentFound++; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |