34 {
35 $length = strlen($string);
36
37
38
39
40
41
42 if ($string === '') {
43 return '';
44 }
45 if ($length > 1 && $string[0] === '[' && $string[$length - 1] === ']') {
46
47 $ip = substr($string, 1, $length - 2);
50 return false;
51 }
53 }
54
55
56 $ipv4 = $this->ipv4->validate($string,
$config, $context);
57 if (
$ipv4 !==
false) {
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 $underscore =
$config->get(
'Core.AllowHostnameUnderscore') ?
'_' :
'';
78
79
80
81
82 $a = '[a-z]';
83 $an = '[a-z0-9]';
84 $and = "[a-z0-9-$underscore]";
85
86 $domainlabel = "$an(?:$and*$an)?";
87
88
89
90 $toplabel = "$an(?:$and*$an)?";
91
92 if (preg_match("/^(?:$domainlabel\.)*($toplabel)\.?$/i", $string, $matches)) {
93 if (!ctype_digit($matches[1])) {
94 return $string;
95 }
96 }
97
98
99 if (function_exists('idn_to_ascii')) {
100 $string = idn_to_ascii($string);
101
102
103
104
105 } elseif (
$config->get(
'Core.EnableIDNA')) {
106 $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true));
107
108 $parts = explode('.', $string);
109 try {
110 $new_parts = array();
111 foreach ($parts as $part) {
112 $encodable = false;
113 for (
$i = 0, $c = strlen($part);
$i < $c;
$i++) {
114 if (ord($part[
$i]) > 0x7a) {
115 $encodable = true;
116 break;
117 }
118 }
119 if (!$encodable) {
120 $new_parts[] = $part;
121 } else {
122 $new_parts[] = $idna->encode($part);
123 }
124 }
125 $string = implode('.', $new_parts);
126 } catch (Exception $e) {
127
128 }
129 }
130
131 if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
132 return $string;
133 }
134 return false;
135 }