1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Exceptions\ClientException; |
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\DirectiveParserCommons; |
6
|
|
|
use vipnytt\RobotsTxtParser\Parser\UrlParser; |
7
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
8
|
|
|
|
9
|
|
|
class DisAllowClient implements ClientInterface, RobotsTxtInterface |
10
|
|
|
{ |
11
|
|
|
use DirectiveParserCommons; |
12
|
|
|
use UrlParser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Paths |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $paths; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Host |
22
|
|
|
* @var HostClient |
23
|
|
|
*/ |
24
|
|
|
private $host; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Clean-param |
28
|
|
|
* @var CleanParamClient |
29
|
|
|
*/ |
30
|
|
|
private $cleanParam; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* DisAllowClient constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $paths |
36
|
|
|
* @param HostClient $host |
37
|
|
|
* @param CleanParamClient $cleanParam |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $paths, HostClient $host, CleanParamClient $cleanParam) |
40
|
|
|
{ |
41
|
|
|
$this->paths = $paths; |
42
|
|
|
$this->host = $host; |
43
|
|
|
$this->cleanParam = $cleanParam; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check |
48
|
|
|
* |
49
|
|
|
* @param string $url |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function isListed($url) |
53
|
|
|
{ |
54
|
|
|
$path = $this->getPath($url); |
55
|
|
|
return ( |
56
|
|
|
$this->checkPath($path, $this->paths) || |
57
|
|
|
$this->cleanParam->isListed($path) || |
58
|
|
|
$this->host->isListed($url) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get path and query |
64
|
|
|
* |
65
|
|
|
* @param string $url |
66
|
|
|
* @return string |
67
|
|
|
* @throws ClientException |
68
|
|
|
*/ |
69
|
|
|
private function getPath($url) |
70
|
|
|
{ |
71
|
|
|
// Encode |
72
|
|
|
$url = mb_split('#', $this->urlEncode($url))[0]; |
73
|
|
|
if (mb_stripos($url, '/') === 0) { |
74
|
|
|
// URL already an path |
75
|
|
|
return $url; |
76
|
|
|
} |
77
|
|
|
if (!$this->urlValidate($url)) { |
78
|
|
|
throw new ClientException('Invalid URL'); |
79
|
|
|
} |
80
|
|
|
$path = (($path = parse_url($url, PHP_URL_PATH)) === null) ? '/' : $path; |
81
|
|
|
$query = (($query = parse_url($url, PHP_URL_QUERY)) === null) ? '' : '?' . $query; |
82
|
|
|
return $path . $query; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Export |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function export() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'host' => $this->host->export(), |
94
|
|
|
'path' => $this->paths, |
95
|
|
|
'clean-param' => $this->cleanParam->export(), |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|