|
1
|
|
|
<?php |
|
2
|
|
|
namespace vipnytt\RobotsTxtParser\Modules\Directives; |
|
3
|
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Modules\Toolbox; |
|
5
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class UserAgent |
|
9
|
|
|
* |
|
10
|
|
|
* @package vipnytt\RobotsTxtParser\Modules\Directives |
|
11
|
|
|
*/ |
|
12
|
|
|
class UserAgent implements DirectiveInterface, RobotsTxtInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use Toolbox; |
|
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 = self::DIRECTIVE_USER_AGENT; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* All User-agents declared |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
public $userAgents = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Sub-directive Allow |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
public $allow = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Sub-directive Cache-delay |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
public $cacheDelay = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Sub-directive Crawl-delay |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
public $crawlDelay = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sub-directive Disallow |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
public $disallow = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Current User-agent(s) |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $userAgent = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* UserAgent constructor. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->set(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set new User-agent |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $array |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function set($array = [self::USER_AGENT]) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->userAgent = $array; |
|
84
|
|
|
foreach ($this->userAgent as $userAgent) { |
|
85
|
|
|
if (!in_array($userAgent, $this->userAgents)) { |
|
86
|
|
|
$this->allow[$userAgent] = new DisAllow(self::DIRECTIVE_ALLOW); |
|
87
|
|
|
$this->cacheDelay[$userAgent] = new CrawlDelay(self::DIRECTIVE_CACHE_DELAY); |
|
88
|
|
|
$this->crawlDelay[$userAgent] = new CrawlDelay(self::DIRECTIVE_CRAWL_DELAY); |
|
89
|
|
|
$this->disallow[$userAgent] = new DisAllow(self::DIRECTIVE_DISALLOW); |
|
90
|
|
|
$this->userAgents[] = $userAgent; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $line |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function add($line) |
|
103
|
|
|
{ |
|
104
|
|
|
$result = false; |
|
105
|
|
|
$pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
|
106
|
|
|
foreach ($this->userAgent as $userAgent) { |
|
107
|
|
|
switch ($pair['directive']) { |
|
108
|
|
|
case self::DIRECTIVE_ALLOW: |
|
109
|
|
|
$result = $this->allow[$userAgent]->add($pair['value']); |
|
110
|
|
|
break; |
|
111
|
|
|
case self::DIRECTIVE_CACHE_DELAY: |
|
112
|
|
|
$result = $this->cacheDelay[$userAgent]->add($pair['value']); |
|
113
|
|
|
break; |
|
114
|
|
|
case self::DIRECTIVE_CRAWL_DELAY: |
|
115
|
|
|
$result = $this->crawlDelay[$userAgent]->add($pair['value']); |
|
116
|
|
|
break; |
|
117
|
|
|
case self::DIRECTIVE_DISALLOW: |
|
118
|
|
|
$result = $this->disallow[$userAgent]->add($pair['value']); |
|
119
|
|
|
break; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return isset($result) ? $result : false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Export |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function export() |
|
131
|
|
|
{ |
|
132
|
|
|
$result = []; |
|
133
|
|
|
foreach ($this->userAgents as $userAgent) { |
|
134
|
|
|
$current = $this->allow[$userAgent]->export() |
|
135
|
|
|
+ $this->cacheDelay[$userAgent]->export() |
|
136
|
|
|
+ $this->crawlDelay[$userAgent]->export() |
|
137
|
|
|
+ $this->disallow[$userAgent]->export(); |
|
138
|
|
|
if (!empty($current)) { |
|
139
|
|
|
$result[$userAgent] = $current; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
return empty($result) ? [] : [self::DIRECTIVE => $result]; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|