| @@ -1,147 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace AcyMailing\Helpers; | |
| 4 | - | |
| 5 | -use AcyMailing\Core\AcymObject; | |
| 6 | -use AcyMailing\Types\CharsetType; | |
| 7 | - | |
| 8 | -class EncodingHelper extends AcymObject | |
| 9 | -{ | |
| 10 | - public function change(string $data, string $inputCharset, string $outputCharset): string | |
| 11 | - { | |
| 12 | - $inputCharset = strtoupper(trim($inputCharset)); | |
| 13 | - $outputCharset = strtoupper(trim($outputCharset)); | |
| 14 | - | |
| 15 | - $supportedEncodings = [ | |
| 16 | - 'BIG5', | |
| 17 | - 'ISO-8859-1', | |
| 18 | - 'ISO-8859-2', | |
| 19 | - 'ISO-8859-3', | |
| 20 | - 'ISO-8859-4', | |
| 21 | - 'ISO-8859-5', | |
| 22 | - 'ISO-8859-6', | |
| 23 | - 'ISO-8859-7', | |
| 24 | - 'ISO-8859-8', | |
| 25 | - 'ISO-8859-9', | |
| 26 | - 'ISO-8859-10', | |
| 27 | - 'ISO-8859-13', | |
| 28 | - 'ISO-8859-14', | |
| 29 | - 'ISO-8859-15', | |
| 30 | - 'ISO-2022-JP', | |
| 31 | - 'US-ASCII', | |
| 32 | - 'UTF-7', | |
| 33 | - 'UTF-8', | |
| 34 | - 'UTF-16', | |
| 35 | - 'WINDOWS-1251', | |
| 36 | - 'WINDOWS-1252', | |
| 37 | - 'ARMSCII-8', | |
| 38 | - 'ISO-8859-16', | |
| 39 | - ]; | |
| 40 | - if (!in_array($inputCharset, $supportedEncodings)) { | |
| 41 | - acym_enqueueMessage(acym_translationSprintf('ACYM_ENCODING_NOT_SUPPORTED_X', $inputCharset), 'error'); | |
| 42 | - } elseif (!in_array($outputCharset, $supportedEncodings)) { | |
| 43 | - acym_enqueueMessage(acym_translationSprintf('ACYM_ENCODING_NOT_SUPPORTED_X', $outputCharset), 'error'); | |
| 44 | - } | |
| 45 | - | |
| 46 | - if ($inputCharset == $outputCharset) { | |
| 47 | - return $data; | |
| 48 | - } | |
| 49 | - | |
| 50 | - if ($inputCharset === 'UTF-8' && $outputCharset === 'ISO-8859-1') { | |
| 51 | - $data = str_replace(['€', '„', '“'], ['EUR', '"', '"'], $data); | |
| 52 | - } | |
| 53 | - | |
| 54 | - if (function_exists('iconv')) { | |
| 55 | - set_error_handler('AcyMailing\Helpers\acym_errorHandlerEncoding'); | |
| 56 | - $encodedData = iconv($inputCharset, $outputCharset.'//IGNORE', $data); | |
| 57 | - restore_error_handler(); | |
| 58 | - if (!empty($encodedData) && !acym_errorHandlerEncoding(-1)) { | |
| 59 | - return $encodedData; | |
| 60 | - } | |
| 61 | - } | |
| 62 | - | |
| 63 | - if (function_exists('mb_convert_encoding')) { | |
| 64 | - return mb_convert_encoding($data, $outputCharset, $inputCharset); | |
| 65 | - } | |
| 66 | - | |
| 67 | - if ($inputCharset === 'UTF-8' && $outputCharset === 'ISO-8859-1') { | |
| 68 | - return acym_utf8Decode($data); | |
| 69 | - } | |
| 70 | - | |
| 71 | - if ($inputCharset === 'ISO-8859-1' && $outputCharset === 'UTF-8') { | |
| 72 | - return acym_utf8Encode($data); | |
| 73 | - } | |
| 74 | - | |
| 75 | - return $data; | |
| 76 | - } | |
| 77 | - | |
| 78 | - public function detectEncoding(string &$content): string | |
| 79 | - { | |
| 80 | - if (!function_exists('mb_check_encoding')) { | |
| 81 | - return ''; | |
| 82 | - } | |
| 83 | - | |
| 84 | - $toTest = ['UTF-8']; | |
| 85 | - | |
| 86 | - $tag = acym_getLanguageTag(); | |
| 87 | - | |
| 88 | - if ($tag == 'el-GR') { | |
| 89 | - $toTest[] = 'ISO-8859-7'; | |
| 90 | - } | |
| 91 | - $toTest[] = 'ISO-8859-1'; | |
| 92 | - $toTest[] = 'ISO-8859-2'; | |
| 93 | - $toTest[] = 'Windows-1252'; | |
| 94 | - | |
| 95 | - foreach ($toTest as $oneEncoding) { | |
| 96 | - if (mb_check_encoding($content, $oneEncoding)) { | |
| 97 | - return $oneEncoding; | |
| 98 | - } | |
| 99 | - } | |
| 100 | - | |
| 101 | - return ''; | |
| 102 | - } | |
| 103 | - | |
| 104 | - public function encodingField(string $name, string $selected): void | |
| 105 | - { | |
| 106 | - echo acym_select( | |
| 107 | - [ | |
| 108 | - 'binary' => 'Binary', | |
| 109 | - 'quoted' => 'Quoted-printable', | |
| 110 | - '7bit' => '7 Bit', | |
| 111 | - '8bit' => '8 Bit', | |
| 112 | - 'base64' => 'Base 64', | |
| 113 | - ], | |
| 114 | - $name, | |
| 115 | - $selected, | |
| 116 | - [ | |
| 117 | - 'class' => 'acym__select', | |
| 118 | - 'acym-data-infinite' => '', | |
| 119 | - 'style' => 'max-width: 200px;', | |
| 120 | - ], | |
| 121 | - '', | |
| 122 | - '', | |
| 123 | - 'config_encoding' | |
| 124 | - ); | |
| 125 | - } | |
| 126 | - | |
| 127 | - public function charsetField(string $name, string $selected, array $attribs = []): string | |
| 128 | - { | |
| 129 | - $charsetType = new CharsetType(); | |
| 130 | - | |
| 131 | - return acym_select($charsetType->charsets, $name, $selected, $attribs, '', ''); | |
| 132 | - } | |
| 133 | -} | |
| 134 | - | |
| 135 | -function acym_errorHandlerEncoding(int $errno, string $errstr = ''): bool | |
| 136 | -{ | |
| 137 | - static $error = false; | |
| 138 | - if ($errno === -1) { | |
| 139 | - $currentError = $error; | |
| 140 | - $error = false; | |
| 141 | - | |
| 142 | - return $currentError; | |
| 143 | - } | |
| 144 | - $error = true; | |
| 145 | - | |
| 146 | - return true; | |
| 147 | -} | |