| 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($data, $inputCharset, $outputCharset) |
| 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_error_handler_encoding'); |
| 56 |
$encodedData = iconv($inputCharset, $outputCharset.'//IGNORE', $data); |
| 57 |
restore_error_handler(); |
| 58 |
if (!empty($encodedData) && !acym_errorHandlerEncoding('result')) { |
| 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(&$content) |
| 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($name, $selected, $attribs = null) |
| 105 |
{ |
| 106 |
if ($attribs === null) { |
| 107 |
$attribs = [ |
| 108 |
'class' => 'acym__select', |
| 109 |
'acym-data-infinite' => '', |
| 110 |
]; |
| 111 |
} |
| 112 |
$attribs['style'] = empty($attribs['style']) ? 'max-width:200px;' : 'max-width:200px;'.$attribs['style']; |
| 113 |
|
| 114 |
echo acym_select( |
| 115 |
[ |
| 116 |
'binary' => 'Binary', |
| 117 |
'quoted' => 'Quoted-printable', |
| 118 |
'7bit' => '7 Bit', |
| 119 |
'8bit' => '8 Bit', |
| 120 |
'base64' => 'Base 64', |
| 121 |
], |
| 122 |
$name, |
| 123 |
$selected, |
| 124 |
$attribs, |
| 125 |
'', |
| 126 |
'', |
| 127 |
'config_encoding' |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
public function charsetField($name, $selected, $attribs = null) |
| 132 |
{ |
| 133 |
$charsetType = new CharsetType(); |
| 134 |
|
| 135 |
return acym_select($charsetType->charsets, $name, $selected, $attribs, '', ''); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
function acym_errorHandlerEncoding($errno, $errstr = '') |
| 140 |
{ |
| 141 |
static $error = false; |
| 142 |
if (is_string($errno) && $errno == 'result') { |
| 143 |
$currentError = $error; |
| 144 |
$error = false; |
| 145 |
|
| 146 |
return $currentError; |
| 147 |
} |
| 148 |
$error = true; |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
function acym_error_handler_encoding($errno, $errstr = '') |
| 154 |
{ |
| 155 |
return acym_errorHandlerEncoding($errno, $errstr = ''); |
| 156 |
} |
| 157 |
|