| @@ -1,8 +1,10 @@ | ||
| 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 |
| @@ -51,11 +53,13 @@ | ||
| 51 | 53 | $data = str_replace(['€', '„', '“'], ['EUR', '"', '"'], $data); |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | 56 | if (function_exists('iconv')) { |
| 57 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Catching iconv errors for clean handling. | |
| 55 | 58 | set_error_handler('AcyMailing\Helpers\acym_errorHandlerEncoding'); |
| 56 | 59 | $encodedData = iconv($inputCharset, $outputCharset.'//IGNORE', $data); |
| 57 | 60 | restore_error_handler(); |
| 61 | + //Sometimes the function does not return error but remove the whole content... | |
| 58 | 62 | if (!empty($encodedData) && !acym_errorHandlerEncoding(-1)) { |
| 59 | 63 | return $encodedData; |
| 60 | 64 | } |
| 61 | 65 | } |
| @@ -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,14 +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 | 111 | public function encodingField(string $name, string $selected): void |
| 105 | 112 | { |
| 106 | - echo acym_select( | |
| 113 | + acym_select( | |
| 107 | 114 | [ |
| 108 | 115 | 'binary' => 'Binary', |
| 109 | 116 | 'quoted' => 'Quoted-printable', |
| 110 | 117 | '7bit' => '7 Bit', |
| @@ -119,29 +126,49 @@ | ||
| 119 | 126 | 'style' => 'max-width: 200px;', |
| 120 | 127 | ], |
| 121 | 128 | '', |
| 122 | 129 | '', |
| 123 | - 'config_encoding' | |
| 130 | + 'config_encoding', | |
| 131 | + false, | |
| 132 | + true | |
| 124 | 133 | ); |
| 125 | 134 | } |
| 126 | 135 | |
| 127 | - public function charsetField(string $name, string $selected, array $attribs = []): string | |
| 136 | + public function charsetField(string $name, string $selected, array $attribs = [], bool $display = false): string | |
| 128 | 137 | { |
| 129 | 138 | $charsetType = new CharsetType(); |
| 130 | 139 | |
| 131 | - 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 | + ); | |
| 132 | 151 | } |
| 133 | 152 | } |
| 134 | 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 | + */ | |
| 135 | 159 | function acym_errorHandlerEncoding(int $errno, string $errstr = ''): bool |
| 136 | 160 | { |
| 137 | 161 | static $error = false; |
| 138 | 162 | if ($errno === -1) { |
| 163 | + //return the value of the static flag and re init it | |
| 139 | 164 | $currentError = $error; |
| 140 | 165 | $error = false; |
| 141 | 166 | |
| 142 | 167 | return $currentError; |
| 143 | 168 | } |
| 169 | + //set the satic flag | |
| 144 | 170 | $error = true; |
| 145 | 171 | |
| 172 | + //no need to display the notice | |
| 146 | 173 | return true; |
| 147 | 174 | } |