1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\HostClient; |
5
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\SitemapClient; |
6
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\UserAgentClient; |
7
|
|
|
use vipnytt\RobotsTxtParser\Client\Encoding\EncodingConverter; |
8
|
|
|
use vipnytt\RobotsTxtParser\Parser\RobotsTxtParser; |
9
|
|
|
use vipnytt\RobotsTxtParser\Parser\UrlParser; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Input |
13
|
|
|
* |
14
|
|
|
* @link https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt |
15
|
|
|
* @link https://yandex.com/support/webmaster/controlling-robot/robots-txt.xml |
16
|
|
|
* @link http://www.robotstxt.org/robotstxt.html |
17
|
|
|
* @link https://www.w3.org/TR/html4/appendix/notes.html#h-B.4.1.1 |
18
|
|
|
* @link http://www.conman.org/people/spc/robots2.html |
19
|
|
|
* |
20
|
|
|
* @package vipnytt\RobotsTxtParser\Client |
21
|
|
|
*/ |
22
|
|
|
class Input extends RobotsTxtParser |
23
|
|
|
{ |
24
|
|
|
use UrlParser; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Base uri |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $base; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Status code |
34
|
|
|
* @var int|null |
35
|
|
|
*/ |
36
|
|
|
protected $statusCode; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Robots.txt content |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $content; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* UserAgentClient class cache |
46
|
|
|
* @var UserAgentClient[] |
47
|
|
|
*/ |
48
|
|
|
private $userAgentClients = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Core constructor. |
52
|
|
|
* |
53
|
|
|
* @param string $baseUri |
54
|
|
|
* @param int $statusCode |
55
|
|
|
* @param string|null $content |
56
|
|
|
* @param string $encoding |
57
|
|
|
* @param int|null $byteLimit |
58
|
|
|
*/ |
59
|
|
|
public function __construct($baseUri, $statusCode, $content, $encoding = self::ENCODING, $byteLimit = self::BYTE_LIMIT) |
60
|
|
|
{ |
61
|
|
|
$this->base = $this->urlBase($this->urlEncode($baseUri)); |
62
|
|
|
$this->statusCode = $statusCode; |
63
|
|
|
$this->content = $content; |
64
|
|
|
$this->convertEncoding($encoding); |
65
|
|
|
$this->limitBytes($byteLimit); |
66
|
|
|
parent::__construct($this->content); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convert character encoding |
71
|
|
|
* |
72
|
|
|
* @param string $encoding |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
private function convertEncoding($encoding) |
76
|
|
|
{ |
77
|
|
|
mb_internal_encoding(self::ENCODING); |
78
|
|
|
$convert = new EncodingConverter($this->content, $encoding); |
79
|
|
|
if (($result = $convert->auto()) !== false) { |
80
|
|
|
return $this->content = $result; |
81
|
|
|
} |
82
|
|
|
return $this->content; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Byte limit |
87
|
|
|
* |
88
|
|
|
* @param int|null $bytes |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
private function limitBytes($bytes) |
92
|
|
|
{ |
93
|
|
|
if ($bytes === null) { |
94
|
|
|
return $this->content; |
95
|
|
|
} elseif ($bytes < 5000) { |
96
|
|
|
trigger_error('Byte limit is set dangerously low!', E_USER_WARNING); |
97
|
|
|
} |
98
|
|
|
return $this->content = mb_strcut($this->content, 0, intval($bytes)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sitemaps |
103
|
|
|
* |
104
|
|
|
* @return SitemapClient |
105
|
|
|
*/ |
106
|
|
|
public function sitemap() |
107
|
|
|
{ |
108
|
|
|
$export = $this->sitemap->export(); |
109
|
|
|
return new SitemapClient(isset($export[self::DIRECTIVE_SITEMAP]) ? $export[self::DIRECTIVE_SITEMAP] : []); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Host |
114
|
|
|
* |
115
|
|
|
* @return HostClient |
116
|
|
|
*/ |
117
|
|
|
public function host() |
118
|
|
|
{ |
119
|
|
|
$export = $this->host->export(); |
120
|
|
|
return new HostClient(isset($export[self::DIRECTIVE_HOST][0]) ? $export[self::DIRECTIVE_HOST][0] : null); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get Clean-param |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getCleanParam() |
129
|
|
|
{ |
130
|
|
|
$export = $this->cleanParam->export(); |
131
|
|
|
if (isset($export[self::DIRECTIVE_CLEAN_PARAM])) { |
132
|
|
|
return $export[self::DIRECTIVE_CLEAN_PARAM]; |
133
|
|
|
} |
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get User-agent list |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function getUserAgents() |
143
|
|
|
{ |
144
|
|
|
return $this->userAgent->userAgents; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Client User-agent specific rules |
149
|
|
|
* |
150
|
|
|
* @param string $string |
151
|
|
|
* @return UserAgentClient |
152
|
|
|
*/ |
153
|
|
|
public function userAgent($string = self::USER_AGENT) |
154
|
|
|
{ |
155
|
|
|
if (isset($this->userAgentClients[$string])) { |
156
|
|
|
return $this->userAgentClients[$string]; |
157
|
|
|
} |
158
|
|
|
$this->userAgentClients[$string] = new UserAgentClient($string, $this->userAgent, $this->base, $this->statusCode); |
159
|
|
|
return $this->userAgentClients[$string]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|