1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Parser\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\UserAgentClient; |
5
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
6
|
|
|
use vipnytt\UserAgentParser as UAStringParser; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UserAgentParser |
10
|
|
|
* |
11
|
|
|
* @package vipnytt\RobotsTxtParser\Parser\Directives |
12
|
|
|
*/ |
13
|
|
|
class UserAgentParser implements ParserInterface, RobotsTxtInterface |
14
|
|
|
{ |
15
|
|
|
use DirectiveParserCommons; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Sub directives white list |
19
|
|
|
*/ |
20
|
|
|
const SUB_DIRECTIVES = [ |
21
|
|
|
self::DIRECTIVE_ALLOW => 'allow', |
22
|
|
|
self::DIRECTIVE_CACHE_DELAY => 'cacheDelay', |
23
|
|
|
self::DIRECTIVE_COMMENT => 'comment', |
24
|
|
|
self::DIRECTIVE_CRAWL_DELAY => 'crawlDelay', |
25
|
|
|
self::DIRECTIVE_DISALLOW => 'disallow', |
26
|
|
|
self::DIRECTIVE_REQUEST_RATE => 'requestRate', |
27
|
|
|
self::DIRECTIVE_ROBOT_VERSION => 'robotVersion', |
28
|
|
|
self::DIRECTIVE_VISIT_TIME => 'visitTime', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Base Uri |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $base; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* User-agent handler |
39
|
|
|
* @var SubDirectiveHandler[] |
40
|
|
|
*/ |
41
|
|
|
private $handler = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* User-agent(s) |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
private $userAgent; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* User-agent client cache |
51
|
|
|
* @var UserAgentClient |
52
|
|
|
*/ |
53
|
|
|
private $client; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* UserAgent constructor. |
57
|
|
|
* |
58
|
|
|
* @param string $base |
59
|
|
|
*/ |
60
|
|
|
public function __construct($base) |
61
|
|
|
{ |
62
|
|
|
$this->base = $base; |
63
|
|
|
$this->set([self::USER_AGENT]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set new User-agent |
68
|
|
|
* |
69
|
|
|
* @param array $array |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function set(array $array) |
73
|
|
|
{ |
74
|
|
|
$this->userAgent = array_map('mb_strtolower', $array); |
75
|
|
|
foreach ($this->userAgent as $userAgent) { |
76
|
|
|
if (!in_array($userAgent, array_keys($this->handler))) { |
77
|
|
|
$this->handler[$userAgent] = new SubDirectiveHandler($this->base, $userAgent); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add |
85
|
|
|
* |
86
|
|
|
* @param string $line |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function add($line) |
90
|
|
|
{ |
91
|
|
|
$result = []; |
92
|
|
|
if (($pair = $this->generateRulePair($line, array_keys(self::SUB_DIRECTIVES))) === false) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
foreach ($this->userAgent as $userAgent) { |
96
|
|
|
$result[] = $this->handler[$userAgent]->{self::SUB_DIRECTIVES[$pair['directive']]}()->add($pair['value']); |
97
|
|
|
} |
98
|
|
|
return in_array(true, $result, true); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Render |
103
|
|
|
* |
104
|
|
|
* @return string[] |
105
|
|
|
*/ |
106
|
|
|
public function render() |
107
|
|
|
{ |
108
|
|
|
$userAgents = $this->getUserAgents(); |
109
|
|
|
$result = []; |
110
|
|
|
foreach ($userAgents as $userAgent) { |
111
|
|
|
$current = array_merge( |
112
|
|
|
$this->handler[$userAgent]->robotVersion()->render(), |
113
|
|
|
$this->handler[$userAgent]->visitTime()->render(), |
114
|
|
|
$this->handler[$userAgent]->disallow()->render(), |
115
|
|
|
$this->handler[$userAgent]->allow()->render(), |
116
|
|
|
$this->handler[$userAgent]->crawlDelay()->render(), |
117
|
|
|
$this->handler[$userAgent]->cacheDelay()->render(), |
118
|
|
|
$this->handler[$userAgent]->requestRate()->render(), |
119
|
|
|
$this->handler[$userAgent]->comment()->render() |
120
|
|
|
); |
121
|
|
|
if (!empty($current)) { |
122
|
|
|
$result = array_merge($result, [self::DIRECTIVE_USER_AGENT . ':' . $userAgent], $current); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* User-agent list |
130
|
|
|
* |
131
|
|
|
* @return string[] |
132
|
|
|
*/ |
133
|
|
|
public function getUserAgents() |
134
|
|
|
{ |
135
|
|
|
$list = array_keys($this->handler); |
136
|
|
|
sort($list); |
137
|
|
|
return $list; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Export |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function export() |
146
|
|
|
{ |
147
|
|
|
$array = []; |
148
|
|
|
foreach ($this->getUserAgents() as $userAgent) { |
149
|
|
|
$array[$userAgent] = $this->client($userAgent)->export(); |
150
|
|
|
} |
151
|
|
|
return $array; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Client |
156
|
|
|
* |
157
|
|
|
* @param string $userAgent |
158
|
|
|
* @param int|null $statusCode |
159
|
|
|
* @return UserAgentClient |
160
|
|
|
*/ |
161
|
|
|
public function client($userAgent = self::USER_AGENT, $statusCode = null) |
162
|
|
|
{ |
163
|
|
|
if (isset($this->client[$userAgent])) { |
164
|
|
|
return $this->client[$userAgent]; |
165
|
|
|
} |
166
|
|
|
$userAgent = mb_strtolower($userAgent); |
167
|
|
|
$userAgentParser = new UAStringParser($userAgent); |
168
|
|
|
if (($userAgentMatch = $userAgentParser->match($this->getUserAgents())) === false) { |
169
|
|
|
$userAgentMatch = self::USER_AGENT; |
170
|
|
|
} |
171
|
|
|
return $this->client[$userAgent] = new UserAgentClient($this->handler[$userAgentMatch], $this->base, $statusCode); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|