1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\ObjectTools; |
5
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class UserAgent |
9
|
|
|
* |
10
|
|
|
* @package vipnytt\RobotsTxtParser\Directives |
11
|
|
|
*/ |
12
|
|
|
class UserAgent implements DirectiveInterface, RobotsTxtInterface |
13
|
|
|
{ |
14
|
|
|
use ObjectTools; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Sub directives white list |
18
|
|
|
*/ |
19
|
|
|
const SUB_DIRECTIVES = [ |
20
|
|
|
self::DIRECTIVE_ALLOW, |
21
|
|
|
self::DIRECTIVE_CACHE_DELAY, |
22
|
|
|
self::DIRECTIVE_CRAWL_DELAY, |
23
|
|
|
self::DIRECTIVE_DISALLOW, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Directive |
28
|
|
|
*/ |
29
|
|
|
const DIRECTIVE = 'User-agent'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Current User-agent(s) |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $userAgent = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* All User-agents declared |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $userAgents = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Rule array |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $array = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sub-directive Allow |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $allow = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sub-directive Cache-delay |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $cacheDelay = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sub-directive Crawl-delay |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $crawlDelay = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sub-directive Disallow |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $disallow = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* UserAgent constructor. |
75
|
|
|
* |
76
|
|
|
* @param null $parent |
77
|
|
|
*/ |
78
|
|
|
public function __construct($parent = null) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->set(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set new User-agent |
85
|
|
|
* |
86
|
|
|
* @param string $line |
87
|
|
|
* @param bool $append |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function set($line = self::USER_AGENT, $append = false) |
91
|
|
|
{ |
92
|
|
|
if (!$append) { |
93
|
|
|
$this->userAgent = []; |
94
|
|
|
} |
95
|
|
|
$this->userAgent[] = $line; |
96
|
|
|
if (!in_array($line, $this->userAgents)) { |
97
|
|
|
$this->allow[$line] = new DisAllow(self::DIRECTIVE_ALLOW); |
98
|
|
|
$this->cacheDelay[$line] = new CrawlDelay(self::DIRECTIVE_CACHE_DELAY); |
99
|
|
|
$this->crawlDelay[$line] = new CrawlDelay(self::DIRECTIVE_CRAWL_DELAY); |
100
|
|
|
$this->disallow[$line] = new DisAllow(self::DIRECTIVE_DISALLOW); |
101
|
|
|
$this->userAgents[] = $line; |
102
|
|
|
} |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add |
108
|
|
|
* |
109
|
|
|
* @param string $line |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function add($line) |
113
|
|
|
{ |
114
|
|
|
$result = false; |
115
|
|
|
$pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
116
|
|
|
foreach ($this->userAgent as $userAgent) { |
117
|
|
|
switch ($pair['directive']) { |
118
|
|
|
case self::DIRECTIVE_ALLOW: |
119
|
|
|
$result = $this->allow[$userAgent]->add($pair['value']); |
120
|
|
|
break; |
121
|
|
|
case self::DIRECTIVE_CACHE_DELAY: |
122
|
|
|
$result = $this->cacheDelay[$userAgent]->add($pair['value']); |
123
|
|
|
break; |
124
|
|
|
case self::DIRECTIVE_CRAWL_DELAY: |
125
|
|
|
$result = $this->crawlDelay[$userAgent]->add($pair['value']); |
126
|
|
|
break; |
127
|
|
|
case self::DIRECTIVE_DISALLOW: |
128
|
|
|
$result = $this->disallow[$userAgent]->add($pair['value']); |
129
|
|
|
break; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
return isset($result) ? $result : false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check |
137
|
|
|
* |
138
|
|
|
* @param string $url - URL to check |
139
|
|
|
* @param string $type - directive to check |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function check($url, $type) |
143
|
|
|
{ |
144
|
|
|
$result = ($type === self::DIRECTIVE_ALLOW); |
145
|
|
|
foreach ([self::DIRECTIVE_DISALLOW, self::DIRECTIVE_ALLOW] as $directive) { |
146
|
|
|
if ($this->$directive->check($url)) { |
147
|
|
|
$result = ($type === $directive); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Export |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public function export() |
159
|
|
|
{ |
160
|
|
|
$result = []; |
161
|
|
|
foreach ($this->userAgents as $userAgent) { |
162
|
|
|
$current = $this->allow[$userAgent]->export() |
163
|
|
|
+ $this->cacheDelay[$userAgent]->export() |
164
|
|
|
+ $this->crawlDelay[$userAgent]->export() |
165
|
|
|
+ $this->disallow[$userAgent]->export(); |
166
|
|
|
if (!empty($current)) { |
167
|
|
|
$result[$userAgent] = $current; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
return empty($result) ? [] : [self::DIRECTIVE => $result]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.