1212namespace Pdp ;
1313
1414/**
15- * A Wrapper around INTL IDNA function
15+ * @internal Domain name validator
1616 *
1717 * @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
1818 */
@@ -63,51 +63,60 @@ private static function getIdnErrors(int $error_bit): string
6363 *
6464 * This method returns the string converted to IDN ASCII form
6565 *
66- * @param string $host
66+ * @param string $domain
67+ *
6768 * @throws Exception if the string can not be converted to ASCII using IDN UTS46 algorithm
6869 *
6970 * @return string
7071 */
71- private function idnToAscii (string $ host ): string
72+ private function idnToAscii (string $ domain ): string
7273 {
7374 static $ pattern = '/[^\x20-\x7f]/ ' ;
74- if (!preg_match ($ pattern , $ host )) {
75- return $ host ;
75+ if (!preg_match ($ pattern , $ domain )) {
76+ return $ domain ;
7677 }
7778
78- $ output = idn_to_ascii ($ host , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
79+ $ output = idn_to_ascii ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
7980 if (!$ arr ['errors ' ]) {
8081 return $ output ;
8182 }
8283
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 ' ])));
8485 }
8586
8687 /**
8788 * Converts the input to its IDNA UNICODE form.
8889 *
8990 * This method returns the string converted to IDN UNICODE form
9091 *
91- * @param string $host
92+ * @param string $domain
93+ *
9294 * @throws Exception if the string can not be converted to UNICODE using IDN UTS46 algorithm
9395 *
9496 * @return string
9597 */
96- private function idnToUnicode (string $ host ): string
98+ private function idnToUnicode (string $ domain ): string
9799 {
98- $ output = idn_to_utf8 ($ host , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
100+ $ output = idn_to_utf8 ($ domain , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
99101 if (!$ arr ['errors ' ]) {
100102 return $ output ;
101103 }
102104
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 ' ])));
104106 }
105107
106108 /**
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']];
108115 *
109116 * @param string|null $domain
110117 *
118+ * @throws Exception If the domain is invalid
119+ *
111120 * @return array
112121 */
113122 private function setDomain (string $ domain = null ): array
@@ -127,14 +136,14 @@ private function setDomain(string $domain = null): array
127136 $ formatted_domain = strtolower (rawurldecode ($ domain ));
128137
129138 // Note that unreserved is purposely missing . as it is used to separate labels.
130- static $ reg_name = '/(?(DEFINE)
139+ static $ domain_name = '/(?(DEFINE)
131140 (?<unreserved>[a-z0-9_~\-])
132141 (?<sub_delims>[!$& \'()*+,;=])
133142 (?<encoded>%[A-F0-9]{2})
134143 (?<reg_name>(?:(?&unreserved)|(?&sub_delims)|(?&encoded)){1,63})
135144 )
136145 ^(?:(?®_name)\.){0,126}(?®_name)\.?$/ix ' ;
137- if (preg_match ($ reg_name , $ formatted_domain )) {
146+ if (preg_match ($ domain_name , $ formatted_domain )) {
138147 return [$ formatted_domain , array_reverse (explode ('. ' , $ formatted_domain ))];
139148 }
140149
0 commit comments