18
18
*/
19
19
trait IDNAConverterTrait
20
20
{
21
- /**
22
- * IDNA errors
23
- *
24
- * @see http://icu-project.org/apiref/icu4j/com/ibm/icu/text/IDNA.Error.html
25
- * @var array
26
- */
27
- private static $ idn_errors = [
28
- IDNA_ERROR_EMPTY_LABEL => 'a non-final domain name label (or the whole domain name) is empty ' ,
29
- IDNA_ERROR_LABEL_TOO_LONG => 'a domain name label is longer than 63 bytes ' ,
30
- IDNA_ERROR_DOMAIN_NAME_TOO_LONG => 'a domain name is longer than 255 bytes in its storage form ' ,
31
- IDNA_ERROR_LEADING_HYPHEN => 'a label starts with a hyphen-minus ("-") ' ,
32
- IDNA_ERROR_TRAILING_HYPHEN => 'a label ends with a hyphen-minus ("-") ' ,
33
- IDNA_ERROR_HYPHEN_3_4 => 'a label contains hyphen-minus ("-") in the third and fourth positions ' ,
34
- IDNA_ERROR_LEADING_COMBINING_MARK => 'a label starts with a combining mark ' ,
35
- IDNA_ERROR_DISALLOWED => 'a label or domain name contains disallowed characters ' ,
36
- IDNA_ERROR_PUNYCODE => 'a label starts with "xn--" but does not contain valid Punycode ' ,
37
- IDNA_ERROR_LABEL_HAS_DOT => 'a label contains a dot=full stop ' ,
38
- IDNA_ERROR_INVALID_ACE_LABEL => 'An ACE label does not contain a valid label string ' ,
39
- IDNA_ERROR_BIDI => 'a label does not meet the IDNA BiDi requirements (for right-to-left characters) ' ,
40
- IDNA_ERROR_CONTEXTJ => 'a label does not meet the IDNA CONTEXTJ requirements ' ,
41
- ];
42
-
43
21
/**
44
22
* Get and format IDN conversion error message
45
23
*
@@ -49,8 +27,29 @@ trait IDNAConverterTrait
49
27
*/
50
28
private static function getIdnErrors (int $ error_bit ): string
51
29
{
30
+ /**
31
+ * IDNA errors
32
+ *
33
+ * @see http://icu-project.org/apiref/icu4j/com/ibm/icu/text/IDNA.Error.html
34
+ */
35
+ static $ idn_errors = [
36
+ IDNA_ERROR_EMPTY_LABEL => 'a non-final domain name label (or the whole domain name) is empty ' ,
37
+ IDNA_ERROR_LABEL_TOO_LONG => 'a domain name label is longer than 63 bytes ' ,
38
+ IDNA_ERROR_DOMAIN_NAME_TOO_LONG => 'a domain name is longer than 255 bytes in its storage form ' ,
39
+ IDNA_ERROR_LEADING_HYPHEN => 'a label starts with a hyphen-minus ("-") ' ,
40
+ IDNA_ERROR_TRAILING_HYPHEN => 'a label ends with a hyphen-minus ("-") ' ,
41
+ IDNA_ERROR_HYPHEN_3_4 => 'a label contains hyphen-minus ("-") in the third and fourth positions ' ,
42
+ IDNA_ERROR_LEADING_COMBINING_MARK => 'a label starts with a combining mark ' ,
43
+ IDNA_ERROR_DISALLOWED => 'a label or domain name contains disallowed characters ' ,
44
+ IDNA_ERROR_PUNYCODE => 'a label starts with "xn--" but does not contain valid Punycode ' ,
45
+ IDNA_ERROR_LABEL_HAS_DOT => 'a label contains a dot=full stop ' ,
46
+ IDNA_ERROR_INVALID_ACE_LABEL => 'An ACE label does not contain a valid label string ' ,
47
+ IDNA_ERROR_BIDI => 'a label does not meet the IDNA BiDi requirements (for right-to-left characters) ' ,
48
+ IDNA_ERROR_CONTEXTJ => 'a label does not meet the IDNA CONTEXTJ requirements ' ,
49
+ ];
50
+
52
51
$ res = [];
53
- foreach (self :: $ idn_errors as $ error => $ reason ) {
52
+ foreach ($ idn_errors as $ error => $ reason ) {
54
53
if ($ error_bit & $ error ) {
55
54
$ res [] = $ reason ;
56
55
}
@@ -75,7 +74,9 @@ private function idnToAscii(string $host): string
75
74
$ host = rawurldecode ($ host );
76
75
}
77
76
78
- if (!preg_match ('/[\pL]+/u ' , $ host )) {
77
+ $ host = strtolower ($ host );
78
+ static $ pattern = '/[\pL]+/u ' ;
79
+ if (!preg_match ($ pattern , $ host )) {
79
80
return $ host ;
80
81
}
81
82
@@ -99,14 +100,6 @@ private function idnToAscii(string $host): string
99
100
*/
100
101
private function idnToUnicode (string $ host ): string
101
102
{
102
- if (false !== strpos ($ host , '% ' )) {
103
- $ host = $ this ->idnToAscii ($ host );
104
- }
105
-
106
- if (false === strpos ($ host , 'xn-- ' )) {
107
- return $ host ;
108
- }
109
-
110
103
$ output = idn_to_utf8 ($ host , 0 , INTL_IDNA_VARIANT_UTS46 , $ arr );
111
104
if (!$ arr ['errors ' ]) {
112
105
return $ output ;
0 commit comments