Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/EncodingAliases.php

raw symbols SCIP
1 <?php
2 /*
3 * File: EncodingAliases.php
4 * Category: -
5 * Author: S. Todorov (https://github.com/todorowww)
6 * Created: 23.04.18 14:16
7 * Updated: -
8 *
9 * Description:
10 * Contains email encoding aliases, thta can occur when fetching emails. These sometimes can break icvon()
11 * This file attempts to correct this by using a list of aliases and their mappings to supported iconv() encodings
12 */
13
14 namespace Webklex\PHPIMAP;
15
16 /**
17 * Class EncodingAliases
18 *
19 * @package Webklex\PHPIMAP
20 */
21 class EncodingAliases {
22
23 /**
24 * Contains email encoding mappings
25 *
26 * @var array
27 */
28 private static array $aliases = [
29 /*
30 |--------------------------------------------------------------------------
31 | Email encoding aliases
32 |--------------------------------------------------------------------------
33 |
34 | Email encoding aliases used to convert to iconv supported charsets
35 |
36 |
37 | This Source Code Form is subject to the terms of the Mozilla Public
38 | License, v. 2.0. If a copy of the MPL was not distributed with this
39 | file, You can obtain one at http://mozilla.org/MPL/2.0/.
40 |
41 | This Original Code has been modified by IBM Corporation.
42 | Modifications made by IBM described herein are
43 | Copyright (c) International Business Machines
44 | Corporation, 1999
45 |
46 | Modifications to Mozilla code or documentation
47 | identified per MPL Section 3.3
48 |
49 | Date Modified by Description of modification
50 | 12/09/1999 IBM Corp. Support for IBM codepages - 850,852,855,857,862,864
51 |
52 | Rule of this file:
53 | 1. key should always be in lower case ascii so we can do case insensitive
54 | comparison in the code faster.
55 | 2. value should be the one used in unicode converter
56 |
57 | 3. If the charset is not used for document charset, but font charset
58 | (e.g. XLFD charset- such as JIS x0201, JIS x0208), don't put here
59 |
60 */
61 "ascii" => "us-ascii",
62 "us-ascii" => "us-ascii",
63 "ansi_x3.4-1968" => "us-ascii",
64 "646" => "us-ascii",
65 "iso-8859-1" => "ISO-8859-1",
66 "iso-8859-2" => "ISO-8859-2",
67 "iso-8859-3" => "ISO-8859-3",
68 "iso-8859-4" => "ISO-8859-4",
69 "iso-8859-5" => "ISO-8859-5",
70 "iso-8859-6" => "ISO-8859-6",
71 "iso-8859-6-i" => "ISO-8859-6-I",
72 "iso-8859-6-e" => "ISO-8859-6-E",
73 "iso-8859-7" => "ISO-8859-7",
74 "iso-8859-8" => "ISO-8859-8",
75 "iso-8859-8-i" => "ISO-8859-8-I",
76 "iso-8859-8-e" => "ISO-8859-8-E",
77 "iso-8859-9" => "ISO-8859-9",
78 "iso-8859-10" => "ISO-8859-10",
79 "iso-8859-11" => "ISO-8859-11",
80 "iso-8859-13" => "ISO-8859-13",
81 "iso-8859-14" => "ISO-8859-14",
82 "iso-8859-15" => "ISO-8859-15",
83 "iso-8859-16" => "ISO-8859-16",
84 "iso-ir-111" => "ISO-IR-111",
85 "iso-2022-cn" => "ISO-2022-CN",
86 "iso-2022-cn-ext" => "ISO-2022-CN",
87 "iso-2022-kr" => "ISO-2022-KR",
88 "iso-2022-jp" => "ISO-2022-JP",
89 "utf-16be" => "UTF-16BE",
90 "utf-16le" => "UTF-16LE",
91 "utf-16" => "UTF-16",
92 "windows-1250" => "windows-1250",
93 "windows-1251" => "windows-1251",
94 "windows-1252" => "windows-1252",
95 "windows-1253" => "windows-1253",
96 "windows-1254" => "windows-1254",
97 "windows-1255" => "windows-1255",
98 "windows-1256" => "windows-1256",
99 "windows-1257" => "windows-1257",
100 "windows-1258" => "windows-1258",
101 "ibm866" => "IBM866",
102 "ibm850" => "IBM850",
103 "ibm852" => "IBM852",
104 "ibm855" => "IBM855",
105 "ibm857" => "IBM857",
106 "ibm862" => "IBM862",
107 "ibm864" => "IBM864",
108 "utf-8" => "UTF-8",
109 "utf-7" => "UTF-7",
110 "utf-7-imap" => "UTF7-IMAP",
111 "utf7-imap" => "UTF7-IMAP",
112 "shift_jis" => "Shift_JIS",
113 "big5" => "Big5",
114 "euc-jp" => "EUC-JP",
115 "euc-kr" => "EUC-KR",
116 "gb2312" => "GB2312",
117 "gb18030" => "gb18030",
118 "viscii" => "VISCII",
119 "koi8-r" => "KOI8-R",
120 "koi8_r" => "KOI8-R",
121 "cskoi8r" => "KOI8-R",
122 "koi" => "KOI8-R",
123 "koi8" => "KOI8-R",
124 "koi8-u" => "KOI8-U",
125 "tis-620" => "TIS-620",
126 "t.61-8bit" => "T.61-8bit",
127 "hz-gb-2312" => "HZ-GB-2312",
128 "big5-hkscs" => "Big5-HKSCS",
129 "gbk" => "gbk",
130 "cns11643" => "x-euc-tw",
131 //
132 // Aliases for ISO-8859-1
133 //
134 "latin1" => "ISO-8859-1",
135 "iso_8859-1" => "ISO-8859-1",
136 "iso8859-1" => "ISO-8859-1",
137 "iso8859-2" => "ISO-8859-2",
138 "iso8859-3" => "ISO-8859-3",
139 "iso8859-4" => "ISO-8859-4",
140 "iso8859-5" => "ISO-8859-5",
141 "iso8859-6" => "ISO-8859-6",
142 "iso8859-7" => "ISO-8859-7",
143 "iso8859-8" => "ISO-8859-8",
144 "iso8859-9" => "ISO-8859-9",
145 "iso8859-10" => "ISO-8859-10",
146 "iso8859-11" => "ISO-8859-11",
147 "iso8859-13" => "ISO-8859-13",
148 "iso8859-14" => "ISO-8859-14",
149 "iso8859-15" => "ISO-8859-15",
150 "iso_8859-1:1987" => "ISO-8859-1",
151 "iso-ir-100" => "ISO-8859-1",
152 "l1" => "ISO-8859-1",
153 "ibm819" => "ISO-8859-1",
154 "cp819" => "ISO-8859-1",
155 "csisolatin1" => "ISO-8859-1",
156 //
157 // Aliases for ISO-8859-2
158 //
159 "latin2" => "ISO-8859-2",
160 "iso_8859-2" => "ISO-8859-2",
161 "iso_8859-2:1987" => "ISO-8859-2",
162 "iso-ir-101" => "ISO-8859-2",
163 "l2" => "ISO-8859-2",
164 "csisolatin2" => "ISO-8859-2",
165 //
166 // Aliases for ISO-8859-3
167 //
168 "latin3" => "ISO-8859-3",
169 "iso_8859-3" => "ISO-8859-3",
170 "iso_8859-3:1988" => "ISO-8859-3",
171 "iso-ir-109" => "ISO-8859-3",
172 "l3" => "ISO-8859-3",
173 "csisolatin3" => "ISO-8859-3",
174 //
175 // Aliases for ISO-8859-4
176 //
177 "latin4" => "ISO-8859-4",
178 "iso_8859-4" => "ISO-8859-4",
179 "iso_8859-4:1988" => "ISO-8859-4",
180 "iso-ir-110" => "ISO-8859-4",
181 "l4" => "ISO-8859-4",
182 "csisolatin4" => "ISO-8859-4",
183 //
184 // Aliases for ISO-8859-5
185 //
186 "cyrillic" => "ISO-8859-5",
187 "iso_8859-5" => "ISO-8859-5",
188 "iso_8859-5:1988" => "ISO-8859-5",
189 "iso-ir-144" => "ISO-8859-5",
190 "csisolatincyrillic" => "ISO-8859-5",
191 //
192 // Aliases for ISO-8859-6
193 //
194 "arabic" => "ISO-8859-6",
195 "iso_8859-6" => "ISO-8859-6",
196 "iso_8859-6:1987" => "ISO-8859-6",
197 "iso-ir-127" => "ISO-8859-6",
198 "ecma-114" => "ISO-8859-6",
199 "asmo-708" => "ISO-8859-6",
200 "csisolatinarabic" => "ISO-8859-6",
201 //
202 // Aliases for ISO-8859-6-I
203 //
204 "csiso88596i" => "ISO-8859-6-I",
205 //
206 // Aliases for ISO-8859-6-E",
207 //
208 "csiso88596e" => "ISO-8859-6-E",
209 //
210 // Aliases for ISO-8859-7",
211 //
212 "greek" => "ISO-8859-7",
213 "greek8" => "ISO-8859-7",
214 "sun_eu_greek" => "ISO-8859-7",
215 "iso_8859-7" => "ISO-8859-7",
216 "iso_8859-7:1987" => "ISO-8859-7",
217 "iso-ir-126" => "ISO-8859-7",
218 "elot_928" => "ISO-8859-7",
219 "ecma-118" => "ISO-8859-7",
220 "csisolatingreek" => "ISO-8859-7",
221 //
222 // Aliases for ISO-8859-8",
223 //
224 "hebrew" => "ISO-8859-8",
225 "iso_8859-8" => "ISO-8859-8",
226 "visual" => "ISO-8859-8",
227 "iso_8859-8:1988" => "ISO-8859-8",
228 "iso-ir-138" => "ISO-8859-8",
229 "csisolatinhebrew" => "ISO-8859-8",
230 //
231 // Aliases for ISO-8859-8-I",
232 //
233 "csiso88598i" => "ISO-8859-8-I",
234 "iso-8859-8i" => "ISO-8859-8-I",
235 "logical" => "ISO-8859-8-I",
236 //
237 // Aliases for ISO-8859-8-E",
238 //
239 "csiso88598e" => "ISO-8859-8-E",
240 //
241 // Aliases for ISO-8859-9",
242 //
243 "latin5" => "ISO-8859-9",
244 "iso_8859-9" => "ISO-8859-9",
245 "iso_8859-9:1989" => "ISO-8859-9",
246 "iso-ir-148" => "ISO-8859-9",
247 "l5" => "ISO-8859-9",
248 "csisolatin5" => "ISO-8859-9",
249 //
250 // Aliases for UTF-8",
251 //
252 "unicode-1-1-utf-8" => "UTF-8",
253 // nl_langinfo(CODESET) in HP/UX returns 'utf8' under UTF-8 locales",
254 "utf8" => "UTF-8",
255 //
256 // Aliases for Shift_JIS",
257 //
258 "x-sjis" => "Shift_JIS",
259 "shift-jis" => "Shift_JIS",
260 "ms_kanji" => "Shift_JIS",
261 "csshiftjis" => "Shift_JIS",
262 "windows-31j" => "Shift_JIS",
263 "cp932" => "Shift_JIS",
264 "sjis" => "Shift_JIS",
265 //
266 // Aliases for EUC_JP",
267 //
268 "cseucpkdfmtjapanese" => "EUC-JP",
269 "x-euc-jp" => "EUC-JP",
270 //
271 // Aliases for ISO-2022-JP",
272 //
273 "csiso2022jp" => "ISO-2022-JP",
274 // The following are really not aliases ISO-2022-JP, but sharing the same decoder",
275 "iso-2022-jp-2" => "ISO-2022-JP",
276 "csiso2022jp2" => "ISO-2022-JP",
277 //
278 // Aliases for Big5",
279 //
280 "csbig5" => "Big5",
281 "cn-big5" => "Big5",
282 // x-x-big5 is not really a alias for Big5, add it only for MS FrontPage",
283 "x-x-big5" => "Big5",
284 // Sun Solaris",
285 "zh_tw-big5" => "Big5",
286 //
287 // Aliases for EUC-KR",
288 //
289 "cseuckr" => "EUC-KR",
290 "ks_c_5601-1987" => "EUC-KR",
291 "iso-ir-149" => "EUC-KR",
292 "ks_c_5601-1989" => "EUC-KR",
293 "ksc_5601" => "EUC-KR",
294 "ksc5601" => "EUC-KR",
295 "korean" => "EUC-KR",
296 "csksc56011987" => "EUC-KR",
297 "5601" => "EUC-KR",
298 "windows-949" => "EUC-KR",
299 //
300 // Aliases for GB2312",
301 //
302 // The following are really not aliases GB2312, add them only for MS FrontPage",
303 "gb_2312-80" => "GB2312",
304 "iso-ir-58" => "GB2312",
305 "chinese" => "GB2312",
306 "csiso58gb231280" => "GB2312",
307 "csgb2312" => "GB2312",
308 "zh_cn.euc" => "GB2312",
309 // Sun Solaris",
310 "gb_2312" => "GB2312",
311 //
312 // Aliases for windows-125x ",
313 //
314 "x-cp1250" => "windows-1250",
315 "x-cp1251" => "windows-1251",
316 "x-cp1252" => "windows-1252",
317 "x-cp1253" => "windows-1253",
318 "x-cp1254" => "windows-1254",
319 "x-cp1255" => "windows-1255",
320 "x-cp1256" => "windows-1256",
321 "x-cp1257" => "windows-1257",
322 "x-cp1258" => "windows-1258",
323 //
324 // Aliases for windows-874 ",
325 //
326 "windows-874" => "windows-874",
327 "ibm874" => "windows-874",
328 "dos-874" => "windows-874",
329 //
330 // Aliases for macintosh",
331 //
332 "macintosh" => "macintosh",
333 "x-mac-roman" => "macintosh",
334 "mac" => "macintosh",
335 "csmacintosh" => "macintosh",
336 //
337 // Aliases for IBM866",
338 //
339 "cp866" => "IBM866",
340 "cp-866" => "IBM866",
341 "866" => "IBM866",
342 "csibm866" => "IBM866",
343 //
344 // Aliases for IBM850",
345 //
346 "cp850" => "IBM850",
347 "850" => "IBM850",
348 "csibm850" => "IBM850",
349 //
350 // Aliases for IBM852",
351 //
352 "cp852" => "IBM852",
353 "852" => "IBM852",
354 "csibm852" => "IBM852",
355 //
356 // Aliases for IBM855",
357 //
358 "cp855" => "IBM855",
359 "855" => "IBM855",
360 "csibm855" => "IBM855",
361 //
362 // Aliases for IBM857",
363 //
364 "cp857" => "IBM857",
365 "857" => "IBM857",
366 "csibm857" => "IBM857",
367 //
368 // Aliases for IBM862",
369 //
370 "cp862" => "IBM862",
371 "862" => "IBM862",
372 "csibm862" => "IBM862",
373 //
374 // Aliases for IBM864",
375 //
376 "cp864" => "IBM864",
377 "864" => "IBM864",
378 "csibm864" => "IBM864",
379 "ibm-864" => "IBM864",
380 //
381 // Aliases for T.61-8bit",
382 //
383 "t.61" => "T.61-8bit",
384 "iso-ir-103" => "T.61-8bit",
385 "csiso103t618bit" => "T.61-8bit",
386 //
387 // Aliases for UTF-7",
388 //
389 "x-unicode-2-0-utf-7" => "UTF-7",
390 "unicode-2-0-utf-7" => "UTF-7",
391 "unicode-1-1-utf-7" => "UTF-7",
392 "csunicode11utf7" => "UTF-7",
393 //
394 // Aliases for ISO-10646-UCS-2",
395 //
396 "csunicode" => "UTF-16BE",
397 "csunicode11" => "UTF-16BE",
398 "iso-10646-ucs-basic" => "UTF-16BE",
399 "csunicodeascii" => "UTF-16BE",
400 "iso-10646-unicode-latin1" => "UTF-16BE",
401 "csunicodelatin1" => "UTF-16BE",
402 "iso-10646" => "UTF-16BE",
403 "iso-10646-j-1" => "UTF-16BE",
404 //
405 // Aliases for ISO-8859-10",
406 //
407 "latin6" => "ISO-8859-10",
408 "iso-ir-157" => "ISO-8859-10",
409 "l6" => "ISO-8859-10",
410 // Currently .properties cannot handle : in key",
411 //iso_8859-10:1992" => "ISO-8859-10",
412 "csisolatin6" => "ISO-8859-10",
413 //
414 // Aliases for ISO-8859-15",
415 //
416 "iso_8859-15" => "ISO-8859-15",
417 "csisolatin9" => "ISO-8859-15",
418 "l9" => "ISO-8859-15",
419 //
420 // Aliases for ISO-IR-111",
421 //
422 "ecma-cyrillic" => "ISO-IR-111",
423 "csiso111ecmacyrillic" => "ISO-IR-111",
424 //
425 // Aliases for ISO-2022-KR",
426 //
427 "csiso2022kr" => "ISO-2022-KR",
428 //
429 // Aliases for VISCII",
430 //
431 "csviscii" => "VISCII",
432 //
433 // Aliases for x-euc-tw",
434 //
435 "zh_tw-euc" => "x-euc-tw",
436 //
437 // Following names appears in unix nl_langinfo(CODESET)",
438 // They can be compiled as platform specific if necessary",
439 // DONT put things here if it does not look generic enough (like hp15CN)",
440 //
441 "iso88591" => "ISO-8859-1",
442 "iso88592" => "ISO-8859-2",
443 "iso88593" => "ISO-8859-3",
444 "iso88594" => "ISO-8859-4",
445 "iso88595" => "ISO-8859-5",
446 "iso88596" => "ISO-8859-6",
447 "iso88597" => "ISO-8859-7",
448 "iso88598" => "ISO-8859-8",
449 "iso88599" => "ISO-8859-9",
450 "iso885910" => "ISO-8859-10",
451 "iso885911" => "ISO-8859-11",
452 "iso885912" => "ISO-8859-12",
453 "iso885913" => "ISO-8859-13",
454 "iso885914" => "ISO-8859-14",
455 "iso885915" => "ISO-8859-15",
456 "cp1250" => "windows-1250",
457 "cp1251" => "windows-1251",
458 "cp1252" => "windows-1252",
459 "cp1253" => "windows-1253",
460 "cp1254" => "windows-1254",
461 "cp1255" => "windows-1255",
462 "cp1256" => "windows-1256",
463 "cp1257" => "windows-1257",
464 "cp1258" => "windows-1258",
465 "x-gbk" => "gbk",
466 "windows-936" => "gbk",
467 "ansi-1251" => "windows-1251",
468 ];
469
470 /**
471 * Returns proper encoding mapping, if exists. If it doesn't, return unchanged $encoding
472 * @param string|null $encoding
473 * @param string|null $fallback
474 *
475 * @return string
476 */
477 public static function get(?string $encoding, ?string $fallback = null): string {
478 if (isset(self::$aliases[strtolower($encoding ?? '')])) {
479 return self::$aliases[strtolower($encoding ?? '')];
480 }
481 return $fallback ?: $encoding;
482 }
483
484
485 /**
486 * Convert the encoding of a string
487 * @param $str
488 * @param string $from
489 * @param string $to
490 *
491 * @return mixed
492 */
493 public static function convert($str, string $from = "ISO-8859-2", string $to = "UTF-8"): mixed {
494 $from = self::get($from, self::detectEncoding($str));
495 $to = self::get($to, self::detectEncoding($str));
496
497 if ($from === $to) {
498 return $str;
499 }
500
501 // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
502 // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
503 // https://stackoverflow.com/a/11303410
504 //
505 // us-ascii is the same as ASCII:
506 // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
507 // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
508 // based on the typographical symbols predominantly in use there.
509 // https://en.wikipedia.org/wiki/ASCII
510 //
511 // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
512 if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') {
513 return $str;
514 }
515
516 try {
517 if (function_exists('iconv') && !self::isUtf7($from) && !self::isUtf7($to)) {
518 return iconv($from, $to, $str);
519 }
520 if (!$from) {
521 return mb_convert_encoding($str, $to);
522 }
523 return mb_convert_encoding($str, $to, $from);
524 } catch (\Exception $e) {
525 if (str_contains($from, '-')) {
526 $from = str_replace('-', '', $from);
527 return self::convert($str, $from, $to);
528 }
529 return $str;
530 }
531 }
532
533 /**
534 * Attempts to detect the encoding of a string
535 * @param string $string
536 *
537 * @return string
538 */
539 public static function detectEncoding(string $string): string {
540 $encoding = mb_detect_encoding($string, array_filter(self::getEncodings(), function($value){
541 return !in_array($value, [
542 'ISO-8859-6-I', 'ISO-8859-6-E', 'ISO-8859-8-I', 'ISO-8859-8-E',
543 'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', 'ISO-IR-111',"ISO-2022-CN",
544 "windows-1250", "windows-1253", "windows-1255", "windows-1256", "windows-1257", "windows-1258",
545 "IBM852", "IBM855", "IBM857", "IBM866", "IBM864", "IBM862", "KOI8-R", "KOI8-U",
546 "TIS-620", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4",
547 "VISCII", "T.61-8bit", "Big5-HKSCS", "windows-874", "macintosh", "ISO-8859-12", "ISO-8859-7",
548 "IMAP-UTF-7"
549 ]);
550 }), true);
551 if ($encoding === false) {
552 $encoding = 'UTF-8';
553 }
554 return $encoding;
555 }
556
557 /**
558 * Returns all available encodings
559 *
560 * @return array
561 */
562 public static function getEncodings(): array {
563 $encodings = [];
564 foreach (self::$aliases as $encoding) {
565 if (!in_array($encoding, $encodings)) {
566 $encodings[] = $encoding;
567 }
568 }
569 return $encodings;
570 }
571
572 /**
573 * Returns true if the encoding is UTF-7 like
574 * @param string $encoding
575 *
576 * @return bool
577 */
578 public static function isUtf7(string $encoding): bool {
579 return str_contains(str_replace("-", "", strtolower($encoding)), "utf7");
580 }
581
582 /**
583 * Check if an encoding is supported
584 * @param string $encoding
585 *
586 * @return bool
587 */
588 public static function has(string $encoding): bool {
589 return isset(self::$aliases[strtolower($encoding)]);
590 }
591 }