1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Uri\UriHelper;
13
14 JLoader::register('idna_convert', JPATH_LIBRARIES . '/idna_convert/idna_convert.class.php');
15
16 17 18 19 20 21 22 23 24
25 abstract class JStringPunycode
26 {
27 28 29 30 31 32 33 34 35
36 public static function toPunycode($utfString)
37 {
38 $idn = new idna_convert;
39
40 return $idn->encode($utfString);
41 }
42
43 44 45 46 47 48 49 50 51
52 public static function fromPunycode($punycodeString)
53 {
54 $idn = new idna_convert;
55
56 return $idn->decode($punycodeString);
57 }
58
59 60 61 62 63 64 65 66 67
68 public static function urlToPunycode($uri)
69 {
70 $parsed = UriHelper::parse_url($uri);
71
72 if (!isset($parsed['host']) || $parsed['host'] == '')
73 {
74
75 return $uri;
76 }
77
78 $host = $parsed['host'];
79 $hostExploded = explode('.', $host);
80 $newhost = '';
81
82 foreach ($hostExploded as $hostex)
83 {
84 $hostex = static::toPunycode($hostex);
85 $newhost .= $hostex . '.';
86 }
87
88 $newhost = substr($newhost, 0, -1);
89 $newuri = '';
90
91 if (!empty($parsed['scheme']))
92 {
93
94 $newuri .= $parsed['scheme'] . '://';
95 }
96
97 if (!empty($newhost))
98 {
99 $newuri .= $newhost;
100 }
101
102 if (!empty($parsed['port']))
103 {
104 $newuri .= ':' . $parsed['port'];
105 }
106
107 if (!empty($parsed['path']))
108 {
109 $newuri .= $parsed['path'];
110 }
111
112 if (!empty($parsed['query']))
113 {
114 $newuri .= '?' . $parsed['query'];
115 }
116
117 if (!empty($parsed['fragment']))
118 {
119 $newuri .= '#' . $parsed['fragment'];
120 }
121
122 return $newuri;
123 }
124
125 126 127 128 129 130 131 132 133
134 public static function urlToUTF8($uri)
135 {
136 if (empty($uri))
137 {
138 return;
139 }
140
141 $parsed = UriHelper::parse_url($uri);
142
143 if (!isset($parsed['host']) || $parsed['host'] == '')
144 {
145
146 return $uri;
147 }
148
149 $host = $parsed['host'];
150 $hostExploded = explode('.', $host);
151 $newhost = '';
152
153 foreach ($hostExploded as $hostex)
154 {
155 $hostex = self::fromPunycode($hostex);
156 $newhost .= $hostex . '.';
157 }
158
159 $newhost = substr($newhost, 0, -1);
160 $newuri = '';
161
162 if (!empty($parsed['scheme']))
163 {
164
165 $newuri .= $parsed['scheme'] . '://';
166 }
167
168 if (!empty($newhost))
169 {
170 $newuri .= $newhost;
171 }
172
173 if (!empty($parsed['port']))
174 {
175 $newuri .= ':' . $parsed['port'];
176 }
177
178 if (!empty($parsed['path']))
179 {
180 $newuri .= $parsed['path'];
181 }
182
183 if (!empty($parsed['query']))
184 {
185 $newuri .= '?' . $parsed['query'];
186 }
187
188 if (!empty($parsed['fragment']))
189 {
190 $newuri .= '#' . $parsed['fragment'];
191 }
192
193 return $newuri;
194 }
195
196 197 198 199 200 201 202 203 204 205
206 public static function emailToPunycode($email)
207 {
208 $explodedAddress = explode('@', $email);
209
210
211 $newEmail = $explodedAddress[0];
212
213 if (!empty($explodedAddress[1]))
214 {
215 $domainExploded = explode('.', $explodedAddress[1]);
216 $newdomain = '';
217
218 foreach ($domainExploded as $domainex)
219 {
220 $domainex = static::toPunycode($domainex);
221 $newdomain .= $domainex . '.';
222 }
223
224 $newdomain = substr($newdomain, 0, -1);
225 $newEmail = $newEmail . '@' . $newdomain;
226 }
227
228 return $newEmail;
229 }
230
231 232 233 234 235 236 237 238 239 240
241 public static function emailToUTF8($email)
242 {
243 $explodedAddress = explode('@', $email);
244
245
246 $newEmail = $explodedAddress[0];
247
248 if (!empty($explodedAddress[1]))
249 {
250 $domainExploded = explode('.', $explodedAddress[1]);
251 $newdomain = '';
252
253 foreach ($domainExploded as $domainex)
254 {
255 $domainex = static::fromPunycode($domainex);
256 $newdomain .= $domainex . '.';
257 }
258
259 $newdomain = substr($newdomain, 0, -1);
260 $newEmail = $newEmail . '@' . $newdomain;
261 }
262
263 return $newEmail;
264 }
265 }
266