| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Helpers; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('Restricted Access'); |
| 6 |
|
| 7 |
use AcyMailing\Core\AcymObject; |
| 8 |
use AcyMailing\Types\CharsetType; |
| 9 |
|
| 10 |
class EncodingHelper extends AcymObject |
| 11 |
{ |
| 12 |
public function change(string $data, string $inputCharset, string $outputCharset): string |
| 13 |
{ |
| 14 |
$inputCharset = strtoupper(trim($inputCharset)); |
| 15 |
$outputCharset = strtoupper(trim($outputCharset)); |
| 16 |
|
| 17 |
$supportedEncodings = [ |
| 18 |
'BIG5', |
| 19 |
'ISO-8859-1', |
| 20 |
'ISO-8859-2', |
| 21 |
'ISO-8859-3', |
| 22 |
'ISO-8859-4', |
| 23 |
'ISO-8859-5', |
| 24 |
'ISO-8859-6', |
| 25 |
'ISO-8859-7', |
| 26 |
'ISO-8859-8', |
| 27 |
'ISO-8859-9', |
| 28 |
'ISO-8859-10', |
| 29 |
'ISO-8859-13', |
| 30 |
'ISO-8859-14', |
| 31 |
'ISO-8859-15', |
| 32 |
'ISO-2022-JP', |
| 33 |
'US-ASCII', |
| 34 |
'UTF-7', |
| 35 |
'UTF-8', |
| 36 |
'UTF-16', |
| 37 |
'WINDOWS-1251', |
| 38 |
'WINDOWS-1252', |
| 39 |
'ARMSCII-8', |
| 40 |
'ISO-8859-16', |
| 41 |
]; |
| 42 |
if (!in_array($inputCharset, $supportedEncodings)) { |
| 43 |
acym_enqueueMessage(acym_translationSprintf('ACYM_ENCODING_NOT_SUPPORTED_X', $inputCharset), 'error'); |
| 44 |
} elseif (!in_array($outputCharset, $supportedEncodings)) { |
| 45 |
acym_enqueueMessage(acym_translationSprintf('ACYM_ENCODING_NOT_SUPPORTED_X', $outputCharset), 'error'); |
| 46 |
} |
| 47 |
|
| 48 |
if ($inputCharset == $outputCharset) { |
| 49 |
return $data; |
| 50 |
} |
| 51 |
|
| 52 |
if ($inputCharset === 'UTF-8' && $outputCharset === 'ISO-8859-1') { |
| 53 |
$data = str_replace(['€', '„', '“'], ['EUR', '"', '"'], $data); |
| 54 |
} |
| 55 |
|
| 56 |
if (function_exists('iconv')) { |
| 57 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Catching iconv errors for clean handling. |
| 58 |
set_error_handler('AcyMailing\Helpers\acym_errorHandlerEncoding'); |
| 59 |
$encodedData = iconv($inputCharset, $outputCharset.'//IGNORE', $data); |
| 60 |
restore_error_handler(); |
| 61 |
//Sometimes the function does not return error but remove the whole content... |
| 62 |
if (!empty($encodedData) && !acym_errorHandlerEncoding(-1)) { |
| 63 |
return $encodedData; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (function_exists('mb_convert_encoding')) { |
| 68 |
return mb_convert_encoding($data, $outputCharset, $inputCharset); |
| 69 |
} |
| 70 |
|
| 71 |
if ($inputCharset === 'UTF-8' && $outputCharset === 'ISO-8859-1') { |
| 72 |
return acym_utf8Decode($data); |
| 73 |
} |
| 74 |
|
| 75 |
if ($inputCharset === 'ISO-8859-1' && $outputCharset === 'UTF-8') { |
| 76 |
return acym_utf8Encode($data); |
| 77 |
} |
| 78 |
|
| 79 |
return $data; |
| 80 |
} |
| 81 |
|
| 82 |
public function detectEncoding(string &$content): string |
| 83 |
{ |
| 84 |
if (!function_exists('mb_check_encoding')) { |
| 85 |
return ''; |
| 86 |
} |
| 87 |
|
| 88 |
$toTest = ['UTF-8']; |
| 89 |
//Depending on the current language, we test based on other charset... |
| 90 |
|
| 91 |
$tag = acym_getLanguageTag(); |
| 92 |
|
| 93 |
// If you're greek, we check based on the greek charset first! |
| 94 |
if ($tag == 'el-GR') { |
| 95 |
$toTest[] = 'ISO-8859-7'; |
| 96 |
} |
| 97 |
$toTest[] = 'ISO-8859-1'; |
| 98 |
$toTest[] = 'ISO-8859-2'; |
| 99 |
$toTest[] = 'Windows-1252'; |
| 100 |
|
| 101 |
foreach ($toTest as $oneEncoding) { |
| 102 |
if (mb_check_encoding($content, $oneEncoding)) { |
| 103 |
return $oneEncoding; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// Could not detect it... |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
public function encodingField(string $name, string $selected): void |
| 112 |
{ |
| 113 |
acym_select( |
| 114 |
[ |
| 115 |
'binary' => 'Binary', |
| 116 |
'quoted' => 'Quoted-printable', |
| 117 |
'7bit' => '7 Bit', |
| 118 |
'8bit' => '8 Bit', |
| 119 |
'base64' => 'Base 64', |
| 120 |
], |
| 121 |
$name, |
| 122 |
$selected, |
| 123 |
[ |
| 124 |
'class' => 'acym__select', |
| 125 |
'acym-data-infinite' => '', |
| 126 |
'style' => 'max-width: 200px;', |
| 127 |
], |
| 128 |
'', |
| 129 |
'', |
| 130 |
'config_encoding', |
| 131 |
false, |
| 132 |
true |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
public function charsetField(string $name, string $selected, array $attribs = [], bool $display = false): string |
| 137 |
{ |
| 138 |
$charsetType = new CharsetType(); |
| 139 |
|
| 140 |
return acym_select( |
| 141 |
$charsetType->charsets, |
| 142 |
$name, |
| 143 |
$selected, |
| 144 |
$attribs, |
| 145 |
'', |
| 146 |
'', |
| 147 |
null, |
| 148 |
false, |
| 149 |
$display |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Logs the errors happening in the iconv function in order to know if we can use its result or not |
| 156 |
* |
| 157 |
* @return bool true if an error occurred |
| 158 |
*/ |
| 159 |
function acym_errorHandlerEncoding(int $errno, string $errstr = ''): bool |
| 160 |
{ |
| 161 |
static $error = false; |
| 162 |
if ($errno === -1) { |
| 163 |
//return the value of the static flag and re init it |
| 164 |
$currentError = $error; |
| 165 |
$error = false; |
| 166 |
|
| 167 |
return $currentError; |
| 168 |
} |
| 169 |
//set the satic flag |
| 170 |
$error = true; |
| 171 |
|
| 172 |
//no need to display the notice |
| 173 |
return true; |
| 174 |
} |
| 175 |
|