|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Crawler Detect - the web crawler detection library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mark Beech <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jaybizzle\CrawlerDetect; |
|
13
|
|
|
|
|
14
|
|
|
use Jaybizzle\CrawlerDetect\Detectors\IpDetector; |
|
15
|
|
|
use Jaybizzle\CrawlerDetect\Detectors\AgentDetector; |
|
16
|
|
|
|
|
17
|
|
|
class CrawlerDetect |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Check agent setting. |
|
21
|
|
|
* |
|
22
|
|
|
* @var null|bool |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $checkAgent = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Check IP setting. |
|
28
|
|
|
* |
|
29
|
|
|
* @var null|bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $checkIp = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Array of detectors. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $detectors = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Check the user agent. |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function agent($userAgent = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->checkAgent = true; |
|
48
|
|
|
|
|
49
|
|
|
if (! isset($detectors['agent'])) { |
|
|
|
|
|
|
50
|
|
|
$this->detectors['agent'] = new AgentDetector($userAgent); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check the IP address. |
|
58
|
|
|
* |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
|
|
public function ip($userIp) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->checkIp = true; |
|
64
|
|
|
|
|
65
|
|
|
if (! isset($detectors['ip'])) { |
|
|
|
|
|
|
66
|
|
|
$this->detectors['ip'] = new IpDetector($userIp); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check user agent string against the regex. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string|null $userAgent |
|
|
|
|
|
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function isCrawler() |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->checkAgent && ! $this->checkIp) { |
|
|
|
|
|
|
82
|
|
|
return $this->detectors['agent']->check(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($this->checkIp && ! $this->checkAgent) { |
|
|
|
|
|
|
86
|
|
|
return $this->detectors['ip']->check(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (is_null($this->checkIp) && is_null($this->checkAgent)) { |
|
90
|
|
|
$this->ip()->agent(); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
return $this->detectors['agent']->check() && $this->detectors['ip']->check(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return the matches. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string|null |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getMatches() |
|
102
|
|
|
{ |
|
103
|
|
|
return isset($this->matches[0]) ? $this->matches[0] : null; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for calls to
isset(...)orempty()on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.