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