| @@ -1,14 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AcyMailing\Helpers; |
| 4 | 4 | |
| 5 | +defined('ABSPATH') || die('Restricted Access'); | |
| 6 | + | |
| 5 | 7 | use AcyMailing\Core\AcymObject; |
| 6 | 8 | use AcyMailing\Types\CharsetType; |
| 7 | 9 | |
| 8 | 10 | class EncodingHelper extends AcymObject |
| 9 | 11 | { |
| 10 | - public function change($data, $inputCharset, $outputCharset) | |
| 12 | + public function change(string $data, string $inputCharset, string $outputCharset): string | |
| 11 | 13 | { |
| 12 | 14 | $inputCharset = strtoupper(trim($inputCharset)); |
| 13 | 15 | $outputCharset = strtoupper(trim($outputCharset)); |
| 14 | 16 | |
| @@ -51,12 +53,14 @@ | ||
| 51 | 53 | $data = str_replace(['€', '„', '“'], ['EUR', '"', '"'], $data); |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | 56 | if (function_exists('iconv')) { |
| 55 | - set_error_handler('AcyMailing\Helpers\acym_error_handler_encoding'); | |
| 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'); | |
| 56 | 59 | $encodedData = iconv($inputCharset, $outputCharset.'//IGNORE', $data); |
| 57 | 60 | restore_error_handler(); |
| 58 | - if (!empty($encodedData) && !acym_errorHandlerEncoding('result')) { | |
| 61 | + //Sometimes the function does not return error but remove the whole content... | |
| 62 | + if (!empty($encodedData) && !acym_errorHandlerEncoding(-1)) { | |
| 59 | 63 | return $encodedData; |
| 60 | 64 | } |
| 61 | 65 | } |
| 62 | 66 | |
| @@ -74,9 +78,9 @@ | ||
| 74 | 78 | |
| 75 | 79 | return $data; |
| 76 | 80 | } |
| 77 | 81 | |
| 78 | - public function detectEncoding(&$content) | |
| 82 | + public function detectEncoding(string &$content): string | |
| 79 | 83 | { |
| 80 | 84 | if (!function_exists('mb_check_encoding')) { |
| 81 | 85 | return ''; |
| 82 | 86 | } |
| @@ -81,11 +85,13 @@ | ||
| 81 | 85 | return ''; |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | 88 | $toTest = ['UTF-8']; |
| 89 | + //Depending on the current language, we test based on other charset... | |
| 85 | 90 | |
| 86 | 91 | $tag = acym_getLanguageTag(); |
| 87 | 92 | |
| 93 | + // If you're greek, we check based on the greek charset first! | |
| 88 | 94 | if ($tag == 'el-GR') { |
| 89 | 95 | $toTest[] = 'ISO-8859-7'; |
| 90 | 96 | } |
| 91 | 97 | $toTest[] = 'ISO-8859-1'; |
| @@ -97,22 +103,15 @@ | ||
| 97 | 103 | return $oneEncoding; |
| 98 | 104 | } |
| 99 | 105 | } |
| 100 | 106 | |
| 107 | + // Could not detect it... | |
| 101 | 108 | return ''; |
| 102 | 109 | } |
| 103 | 110 | |
| 104 | - public function encodingField($name, $selected, $attribs = null) | |
| 111 | + public function encodingField(string $name, string $selected): void | |
| 105 | 112 | { |
| 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( | |
| 113 | + acym_select( | |
| 115 | 114 | [ |
| 116 | 115 | 'binary' => 'Binary', |
| 117 | 116 | 'quoted' => 'Quoted-printable', |
| 118 | 117 | '7bit' => '7 Bit', |
| @@ -120,37 +119,56 @@ | ||
| 120 | 119 | 'base64' => 'Base 64', |
| 121 | 120 | ], |
| 122 | 121 | $name, |
| 123 | 122 | $selected, |
| 124 | - $attribs, | |
| 123 | + [ | |
| 124 | + 'class' => 'acym__select', | |
| 125 | + 'acym-data-infinite' => '', | |
| 126 | + 'style' => 'max-width: 200px;', | |
| 127 | + ], | |
| 125 | 128 | '', |
| 126 | 129 | '', |
| 127 | - 'config_encoding' | |
| 130 | + 'config_encoding', | |
| 131 | + false, | |
| 132 | + true | |
| 128 | 133 | ); |
| 129 | 134 | } |
| 130 | 135 | |
| 131 | - public function charsetField($name, $selected, $attribs = null) | |
| 136 | + public function charsetField(string $name, string $selected, array $attribs = [], bool $display = false): string | |
| 132 | 137 | { |
| 133 | 138 | $charsetType = new CharsetType(); |
| 134 | 139 | |
| 135 | - return acym_select($charsetType->charsets, $name, $selected, $attribs, '', ''); | |
| 140 | + return acym_select( | |
| 141 | + $charsetType->charsets, | |
| 142 | + $name, | |
| 143 | + $selected, | |
| 144 | + $attribs, | |
| 145 | + '', | |
| 146 | + '', | |
| 147 | + null, | |
| 148 | + false, | |
| 149 | + $display | |
| 150 | + ); | |
| 136 | 151 | } |
| 137 | 152 | } |
| 138 | 153 | |
| 139 | -function acym_errorHandlerEncoding($errno, $errstr = '') | |
| 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 | |
| 140 | 160 | { |
| 141 | 161 | static $error = false; |
| 142 | - if (is_string($errno) && $errno == 'result') { | |
| 162 | + if ($errno === -1) { | |
| 163 | + //return the value of the static flag and re init it | |
| 143 | 164 | $currentError = $error; |
| 144 | 165 | $error = false; |
| 145 | 166 | |
| 146 | 167 | return $currentError; |
| 147 | 168 | } |
| 169 | + //set the satic flag | |
| 148 | 170 | $error = true; |
| 149 | 171 | |
| 172 | + //no need to display the notice | |
| 150 | 173 | return true; |
| 151 | -} | |
| 152 | - | |
| 153 | -function acym_error_handler_encoding($errno, $errstr = '') | |
| 154 | -{ | |
| 155 | - return acym_errorHandlerEncoding($errno, $errstr = ''); | |
| 156 | 174 | } |