1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Parser\UrlParser; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class HostClient |
8
|
|
|
* |
9
|
|
|
* @package vipnytt\RobotsTxtParser\Client\Directives |
10
|
|
|
*/ |
11
|
|
|
class HostClient implements ClientInterface |
12
|
|
|
{ |
13
|
|
|
use UrlParser; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base Uri |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $base; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Host |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $host; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Parent directive |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $parent; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* HostClient constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $base |
37
|
|
|
* @param string[] $host |
38
|
|
|
* @param string|null $parentDirective |
39
|
|
|
*/ |
40
|
|
|
public function __construct($base, array $host, $parentDirective = null) |
41
|
|
|
{ |
42
|
|
|
$this->base = $base; |
43
|
|
|
$this->host = $host; |
44
|
|
|
$this->parent = $parentDirective; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check |
49
|
|
|
* |
50
|
|
|
* @param string $url |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function isListed($url) |
54
|
|
|
{ |
55
|
|
|
$url = mb_strtolower($this->urlEncode($url)); |
56
|
|
|
$parts = [ |
57
|
|
|
'scheme' => parse_url($url, PHP_URL_SCHEME), |
58
|
|
|
'host' => parse_url($url, PHP_URL_HOST), |
59
|
|
|
]; |
60
|
|
|
$parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp'); |
61
|
|
|
$cases = [ |
62
|
|
|
$parts['host'], |
63
|
|
|
$parts['host'] . ':' . $parts['port'], |
64
|
|
|
$parts['scheme'] . '://' . $parts['host'], |
65
|
|
|
$parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port'] |
66
|
|
|
]; |
67
|
|
|
foreach ($this->host as $host) { |
68
|
|
|
if (in_array($host, $cases)) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Preferred host |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isPreferred() |
81
|
|
|
{ |
82
|
|
|
return empty($this->host) ? true : mb_stripos($this->urlBase($this->urlEncode($this->base)), $this->get()) !== false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get |
87
|
|
|
* |
88
|
|
|
* @return string|null |
89
|
|
|
*/ |
90
|
|
|
public function get() |
91
|
|
|
{ |
92
|
|
|
return isset($this->host[0]) ? $this->host[0] : null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Export |
97
|
|
|
* |
98
|
|
|
* @return string[]|string|null |
99
|
|
|
*/ |
100
|
|
|
public function export() |
101
|
|
|
{ |
102
|
|
|
if ($this->parent === null) { |
103
|
|
|
return isset($this->host[0]) ? $this->host[0] : null; |
104
|
|
|
} |
105
|
|
|
return $this->host; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|