1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
9
|
|
|
* @version 4.1.0 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A Trait to validate a Hostname |
18
|
|
|
* |
19
|
|
|
* @package League.uri |
20
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
21
|
|
|
* @since 4.0.0 |
22
|
|
|
*/ |
23
|
|
|
trait HostnameTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Tells whether we have a IDN or not |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $isIdn = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Format an label collection for string representation of the Host |
34
|
|
|
* |
35
|
6 |
|
* @param array $labels host labels |
36
|
|
|
* @param bool $convert should we transcode the labels into their ascii equivalent |
37
|
6 |
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
protected function convertToAscii(array $labels, $convert) |
41
|
|
|
{ |
42
|
|
|
return $convert ? array_map('idn_to_ascii', $labels) : $labels; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Validate a string only host |
47
|
|
|
* |
48
|
476 |
|
* @param string $str |
49
|
|
|
* |
50
|
476 |
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
protected function validateStringHost($str) |
53
|
|
|
{ |
54
|
|
|
if ('' === $str) { |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
$host = $this->lower($this->setIsAbsolute($str)); |
58
|
|
|
$raw_labels = explode('.', $host); |
59
|
|
|
$labels = array_map(function ($value) { |
60
|
544 |
|
return idn_to_ascii($value); |
61
|
|
|
}, $raw_labels); |
62
|
544 |
|
|
63
|
20 |
|
$this->assertValidHost($labels); |
64
|
|
|
$this->isIdn = $raw_labels !== $labels; |
65
|
534 |
|
|
66
|
534 |
|
return array_reverse(array_map(function ($label) { |
67
|
|
|
return idn_to_utf8($label); |
68
|
534 |
|
}, $labels)); |
69
|
534 |
|
} |
70
|
|
|
|
71
|
534 |
|
/** |
72
|
490 |
|
* set the FQDN property |
73
|
|
|
* |
74
|
|
|
* @param string $str |
75
|
490 |
|
* |
76
|
490 |
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
abstract protected function setIsAbsolute($str); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convert to lowercase a string without modifying unicode characters |
82
|
|
|
* |
83
|
|
|
* @param string $str |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function lower($str) |
88
|
|
|
{ |
89
|
|
|
return preg_replace_callback('/[A-Z]+/', function ($matches) { |
90
|
|
|
return strtolower($matches[0]); |
91
|
|
|
}, $str); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
534 |
|
* Validate a String Label |
96
|
|
|
* |
97
|
|
|
* @param array $labels found host labels |
98
|
14 |
|
* |
99
|
534 |
|
* @throws InvalidArgumentException If the validation fails |
100
|
|
|
*/ |
101
|
|
|
protected function assertValidHost(array $labels) |
102
|
|
|
{ |
103
|
|
|
$verifs = array_filter($labels, function ($value) { |
104
|
|
|
return '' !== trim($value); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
if ($verifs !== $labels) { |
108
|
|
|
throw new InvalidArgumentException('Invalid Hostname, empty labels are not allowed'); |
109
|
|
|
} |
110
|
|
|
|
111
|
534 |
|
$this->assertLabelsCount($labels); |
112
|
534 |
|
$this->isValidContent($labels); |
113
|
534 |
|
} |
114
|
|
|
|
115
|
534 |
|
/** |
116
|
10 |
|
* Validated the Host Label Count |
117
|
|
|
* |
118
|
|
|
* @param array $labels host labels |
119
|
524 |
|
* |
120
|
520 |
|
* @throws InvalidArgumentException If the validation fails |
121
|
490 |
|
*/ |
122
|
|
|
abstract protected function assertLabelsCount(array $labels); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Validated the Host Label Pattern |
126
|
|
|
* |
127
|
|
|
* @param array $data host labels |
128
|
|
|
* |
129
|
|
|
* @throws InvalidArgumentException If the validation fails |
130
|
|
|
*/ |
131
|
|
|
protected function isValidContent(array $data) |
132
|
|
|
{ |
133
|
|
|
if (count(preg_grep('/^[0-9a-z]([0-9a-z-]{0,61}[0-9a-z])?$/i', $data, PREG_GREP_INVERT))) { |
134
|
|
|
throw new InvalidArgumentException('Invalid Hostname, some labels contain invalid characters'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|