1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* vipnytt/RobotsTxtParser |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser; |
10
|
|
|
|
11
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\CleanParamClient; |
12
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\HostClient; |
13
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\SitemapClient; |
14
|
|
|
use vipnytt\RobotsTxtParser\Client\Directives\UserAgentClient; |
15
|
|
|
use vipnytt\RobotsTxtParser\Handler\EncodingHandler; |
16
|
|
|
use vipnytt\RobotsTxtParser\Parser\RobotsTxtParser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class TxtClient |
20
|
|
|
* |
21
|
|
|
* @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/TxtClient.md for documentation |
22
|
|
|
* @package vipnytt\RobotsTxtParser |
23
|
|
|
*/ |
24
|
|
|
class TxtClient extends RobotsTxtParser |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Status code |
28
|
|
|
* @var int|null |
29
|
|
|
*/ |
30
|
|
|
private $statusCode; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Robots.txt content |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $content; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Encoding |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $encoding; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* TxtClient constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $baseUri |
48
|
|
|
* @param int|null $statusCode |
49
|
|
|
* @param string $content |
50
|
|
|
* @param string|null $encoding |
51
|
|
|
* @param string|null $effectiveUri |
52
|
|
|
* @param int|null $byteLimit |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
$baseUri, |
56
|
|
|
$statusCode, |
57
|
|
|
$content, |
58
|
|
|
$encoding = self::ENCODING, |
59
|
|
|
$effectiveUri = null, |
60
|
|
|
$byteLimit = self::BYTE_LIMIT |
61
|
|
|
) |
62
|
|
|
{ |
63
|
|
|
$this->statusCode = $statusCode; |
64
|
|
|
$this->content = $content; |
65
|
|
|
$this->encoding = ($encoding === null ? self::ENCODING : $encoding); |
66
|
|
|
$this->convertEncoding(); |
67
|
|
|
$this->limitBytes($byteLimit); |
68
|
|
|
parent::__construct($baseUri, $this->content, $effectiveUri); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Convert character encoding |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
private function convertEncoding() |
77
|
|
|
{ |
78
|
|
|
$convert = new EncodingHandler($this->content, $this->encoding); |
79
|
|
|
if (($result = $convert->auto()) !== false) { |
80
|
|
|
$this->encoding = self::ENCODING; |
81
|
|
|
mb_internal_encoding(self::ENCODING); |
82
|
|
|
return $this->content = $result; |
83
|
|
|
} |
84
|
|
|
mb_internal_encoding(self::ENCODING); |
85
|
|
|
return $this->content; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Byte limit |
90
|
|
|
* |
91
|
|
|
* @param int|null $bytes |
92
|
|
|
* @return string |
93
|
|
|
* @throws \InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
private function limitBytes($bytes) |
96
|
|
|
{ |
97
|
|
|
if ($bytes === null) { |
98
|
|
|
return $this->content; |
99
|
|
|
} elseif (intval($bytes) < (self::BYTE_LIMIT * 0.046875)) { |
100
|
|
|
// less than 24 kilobytes (512 kilobytes * 0.046875) |
101
|
|
|
throw new \InvalidArgumentException('Byte limit is set dangerously low! Default value=' . self::BYTE_LIMIT); |
102
|
|
|
} |
103
|
|
|
return $this->content = mb_strcut($this->content, 0, intval($bytes)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get User-agent list |
108
|
|
|
* |
109
|
|
|
* @return string[] |
110
|
|
|
*/ |
111
|
|
|
public function getUserAgents() |
112
|
|
|
{ |
113
|
|
|
return $this->handler->userAgent->getUserAgents(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Clean-param |
118
|
|
|
* |
119
|
|
|
* @return CleanParamClient |
120
|
|
|
*/ |
121
|
|
|
public function cleanParam() |
122
|
|
|
{ |
123
|
|
|
return $this->handler->cleanParam->client(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Host |
128
|
|
|
* |
129
|
|
|
* @return HostClient |
130
|
|
|
*/ |
131
|
|
|
public function host() |
132
|
|
|
{ |
133
|
|
|
return $this->handler->host->client(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sitemaps |
138
|
|
|
* |
139
|
|
|
* @return SitemapClient |
140
|
|
|
*/ |
141
|
|
|
public function sitemap() |
142
|
|
|
{ |
143
|
|
|
return $this->handler->sitemap->client(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* User-agent specific rules |
148
|
|
|
* |
149
|
|
|
* @param string $product |
150
|
|
|
* @param float|int|string|null $version |
151
|
|
|
* @return UserAgentClient |
152
|
|
|
*/ |
153
|
|
|
public function userAgent($product = self::USER_AGENT, $version = null) |
154
|
|
|
{ |
155
|
|
|
return $this->handler->userAgent->client($product, $version, $this->statusCode); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|