12
12
namespace Pdp ;
13
13
14
14
/**
15
- * A Wrapper around INTL IDNA function
15
+ * @internal Domain name validator
16
16
*
17
17
* @author Ignace Nyamagana Butera <[email protected] >
18
18
*/
@@ -63,51 +63,60 @@ private static function getIdnErrors(int $error_bit): string
63
63
*
64
64
* This method returns the string converted to IDN ASCII form
65
65
*
66
- * @param string $host
66
+ * @param string $domain
67
+ *
67
68
* @throws Exception if the string can not be converted to ASCII using IDN UTS46 algorithm
68
69
*
69
70
* @return string
70
71
*/
71
- private function idnToAscii (string $ host ): string
72
+ private function idnToAscii (string $ domain ): string
72
73
{
73
74
static $ pattern = '/[^\x20-\x7f]/ ' ;
74
- if (!preg_match ($ pattern , $ host )) {
75
- return $ host ;
75
+ if (!preg_match ($ pattern , $ domain )) {
76
+ return $ domain ;
76
77
}
77
78
78
- $ output = idn_to_ascii ($ host , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
79
+ $ output = idn_to_ascii ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
79
80
if (!$ arr ['errors ' ]) {
80
81
return $ output ;
81
82
}
82
83
83
- throw new Exception (sprintf ('The host `%s` is invalid : %s ' , $ host , self ::getIdnErrors ($ arr ['errors ' ])));
84
+ throw new Exception (sprintf ('The host `%s` is invalid : %s ' , $ domain , self ::getIdnErrors ($ arr ['errors ' ])));
84
85
}
85
86
86
87
/**
87
88
* Converts the input to its IDNA UNICODE form.
88
89
*
89
90
* This method returns the string converted to IDN UNICODE form
90
91
*
91
- * @param string $host
92
+ * @param string $domain
93
+ *
92
94
* @throws Exception if the string can not be converted to UNICODE using IDN UTS46 algorithm
93
95
*
94
96
* @return string
95
97
*/
96
- private function idnToUnicode (string $ host ): string
98
+ private function idnToUnicode (string $ domain ): string
97
99
{
98
- $ output = idn_to_utf8 ($ host , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
100
+ $ output = idn_to_utf8 ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
99
101
if (!$ arr ['errors ' ]) {
100
102
return $ output ;
101
103
}
102
104
103
- throw new Exception (sprintf ('The host `%s` is invalid : %s ' , $ host , self ::getIdnErrors ($ arr ['errors ' ])));
105
+ throw new Exception (sprintf ('The host `%s` is invalid : %s ' , $ domain , self ::getIdnErrors ($ arr ['errors ' ])));
104
106
}
105
107
106
108
/**
107
- * Validate the given domain
109
+ * Filter and format the domain to ensure it is valid
110
+ *
111
+ * Returns an array containing the formatted domain name in lowercase
112
+ * with its associated labels in reverse order
113
+ *
114
+ * For example: setDomain('wWw.uLb.Ac.be') should return ['www.ulb.ac.be', ['be', 'ac', 'ulb', 'www']];
108
115
*
109
116
* @param string|null $domain
110
117
*
118
+ * @throws Exception If the domain is invalid
119
+ *
111
120
* @return array
112
121
*/
113
122
private function setDomain (string $ domain = null ): array
@@ -127,14 +136,14 @@ private function setDomain(string $domain = null): array
127
136
$ formatted_domain = strtolower (rawurldecode ($ domain ));
128
137
129
138
// Note that unreserved is purposely missing . as it is used to separate labels.
130
- static $ reg_name = '/(?(DEFINE)
139
+ static $ domain_name = '/(?(DEFINE)
131
140
(?<unreserved>[a-z0-9_~\-])
132
141
(?<sub_delims>[!$& \'()*+,;=])
133
142
(?<encoded>%[A-F0-9]{2})
134
143
(?<reg_name>(?:(?&unreserved)|(?&sub_delims)|(?&encoded)){1,63})
135
144
)
136
145
^(?:(?®_name)\.){0,126}(?®_name)\.?$/ix ' ;
137
- if (preg_match ($ reg_name , $ formatted_domain )) {
146
+ if (preg_match ($ domain_name , $ formatted_domain )) {
138
147
return [$ formatted_domain , array_reverse (explode ('. ' , $ formatted_domain ))];
139
148
}
140
149
0 commit comments