1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Parser; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Exceptions\StatusCodeException; |
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\DirectiveParserCommons; |
6
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class StatusCodeParser |
10
|
|
|
* |
11
|
|
|
* @package vipnytt\RobotsTxtParser\Parser |
12
|
|
|
*/ |
13
|
|
|
class StatusCodeParser implements RobotsTxtInterface |
14
|
|
|
{ |
15
|
|
|
use DirectiveParserCommons; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Valid schemes |
19
|
|
|
*/ |
20
|
|
|
const VALID_SCHEME = [ |
21
|
|
|
'http', |
22
|
|
|
'https', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Replacement coded |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $unofficialCodes = [ |
30
|
|
|
522 => 408, // CloudFlare could not negotiate a TCP handshake with the origin server. |
31
|
|
|
523 => 404, // CloudFlare could not reach the origin server; for example, if the DNS records for the baseUrl server are incorrect. |
32
|
|
|
524 => 408, // CloudFlare was able to complete a TCP connection to the origin server, but did not receive a timely HTTP response. |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Status code |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $code; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Scheme |
43
|
|
|
* @var string|false |
44
|
|
|
*/ |
45
|
|
|
private $scheme; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Applicable |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private $applicable; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor |
55
|
|
|
* |
56
|
|
|
* @param int|null $code - HTTP status code |
57
|
|
|
* @param string|false $scheme |
58
|
|
|
* @throws StatusCodeException |
59
|
|
|
*/ |
60
|
|
|
public function __construct($code, $scheme) |
61
|
|
|
{ |
62
|
|
|
$this->code = $code; |
63
|
|
|
$this->scheme = $scheme; |
64
|
|
|
$this->applicable = $this->isApplicable(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if URL is Applicable for Status code parsing |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
* @throws StatusCodeException |
72
|
|
|
*/ |
73
|
|
|
private function isApplicable() |
74
|
|
|
{ |
75
|
|
|
if ( |
76
|
|
|
!in_array($this->scheme, self::VALID_SCHEME) || |
77
|
|
|
$this->code === null |
78
|
|
|
) { |
79
|
|
|
return false; |
80
|
|
|
} elseif ( |
81
|
|
|
$this->code < 100 || |
82
|
|
|
$this->code > 599 |
83
|
|
|
) { |
84
|
|
|
throw new StatusCodeException('Invalid HTTP status code'); |
85
|
|
|
} |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Replace unofficial code |
91
|
|
|
* |
92
|
|
|
* @param int[] $codePairs |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
public function codeOverride($codePairs = []) |
96
|
|
|
{ |
97
|
|
|
$pairs = empty($codePairs) ? $this->unofficialCodes : $codePairs; |
98
|
|
|
while (in_array($this->code, array_keys($pairs))) { |
99
|
|
|
$this->code = $pairs[$this->code]; |
100
|
|
|
} |
101
|
|
|
return $this->code; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check |
106
|
|
|
* |
107
|
|
|
* @return string|null |
108
|
|
|
*/ |
109
|
|
|
public function accessOverrideCheck() |
110
|
|
|
{ |
111
|
|
|
if (!$this->applicable) { |
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
switch (floor($this->code / 100) * 100) { |
115
|
|
|
case 300: |
116
|
|
|
case 400: |
117
|
|
|
return self::DIRECTIVE_ALLOW; |
118
|
|
|
case 500: |
119
|
|
|
return self::DIRECTIVE_DISALLOW; |
120
|
|
|
} |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|