1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
5
|
|
|
use vipnytt\RobotsTxtParser\UrlToolbox; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Host |
9
|
|
|
* |
10
|
|
|
* @package vipnytt\RobotsTxtParser\Directives |
11
|
|
|
*/ |
12
|
|
|
final class Host implements DirectiveInterface, RobotsTxtInterface |
13
|
|
|
{ |
14
|
|
|
use UrlToolbox; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Directive |
18
|
|
|
*/ |
19
|
|
|
const DIRECTIVE = self::DIRECTIVE_HOST; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Host array |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $array = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Host constructor. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add |
36
|
|
|
* |
37
|
|
|
* @param string $line |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function add($line) |
41
|
|
|
{ |
42
|
|
|
if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
$line = isset($parsed['host']) ? $parsed['host'] : $parsed['path']; |
46
|
|
|
if ( |
47
|
|
|
!$this->urlValidateHost($line) || |
48
|
|
|
( |
49
|
|
|
isset($parsed['scheme']) && |
50
|
|
|
!$this->urlValidateScheme($parsed['scheme']) |
51
|
|
|
) |
52
|
|
|
) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
$scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : ''; |
56
|
|
|
$port = isset($parsed['port']) ? ':' . $parsed['port'] : ''; |
57
|
|
|
|
58
|
|
|
$host = $scheme . $line . $port; |
59
|
|
|
if (in_array($host, $this->array)) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
$this->array[] = $host; |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check |
68
|
|
|
* |
69
|
|
|
* @param string $url |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function check($url) |
73
|
|
|
{ |
74
|
|
|
if (empty($this->array)) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
$url = mb_strtolower($this->urlEncode($url)); |
78
|
|
|
$parts = [ |
79
|
|
|
'scheme' => parse_url($url, PHP_URL_SCHEME), |
80
|
|
|
'host' => parse_url($url, PHP_URL_HOST), |
81
|
|
|
]; |
82
|
|
|
$parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp'); |
83
|
|
|
$cases = [ |
84
|
|
|
$parts['host'], |
85
|
|
|
$parts['host'] . ':' . $parts['port'], |
86
|
|
|
$parts['scheme'] . '://' . $parts['host'], |
87
|
|
|
$parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port'] |
88
|
|
|
]; |
89
|
|
|
if (in_array($this->array[0], $cases)) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Export |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function export() |
101
|
|
|
{ |
102
|
|
|
return empty($this->array) ? [] : [self::DIRECTIVE => $this->array]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function optimize($url) |
106
|
|
|
{ |
107
|
|
|
//TODO: Change scheme, Host and port |
108
|
|
|
//$parsed = parse_url($url); |
|
|
|
|
109
|
|
|
|
110
|
|
|
return $url; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.