Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Iconv often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Iconv, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Iconv |
||
| 37 | { |
||
| 38 | const ERROR_ILLEGAL_CHARACTER = 'iconv(): Detected an illegal character in input string'; |
||
| 39 | const ERROR_WRONG_CHARSET = 'iconv(): Wrong charset, conversion from `%s\' to `%s\' is not allowed'; |
||
| 40 | |||
| 41 | public static $input_encoding = 'utf-8'; |
||
| 42 | public static $output_encoding = 'utf-8'; |
||
| 43 | public static $internal_encoding = 'utf-8'; |
||
| 44 | public static $translit_map = array(); |
||
| 45 | public static $convert_map = array(); |
||
| 46 | public static $error_handler, $last_error, $is_valid_utf8; |
||
|
|
|||
| 47 | public static $ulen_mask = array( |
||
| 48 | "\xC0" => 2, |
||
| 49 | "\xD0" => 2, |
||
| 50 | "\xE0" => 3, |
||
| 51 | "\xF0" => 4, |
||
| 52 | ); |
||
| 53 | protected static $alias = array( |
||
| 54 | 'utf8' => 'utf-8', |
||
| 55 | 'ascii' => 'us-ascii', |
||
| 56 | 'tis-620' => 'iso-8859-11', |
||
| 57 | 'cp1250' => 'windows-1250', |
||
| 58 | 'cp1251' => 'windows-1251', |
||
| 59 | 'cp1252' => 'windows-1252', |
||
| 60 | 'cp1253' => 'windows-1253', |
||
| 61 | 'cp1254' => 'windows-1254', |
||
| 62 | 'cp1255' => 'windows-1255', |
||
| 63 | 'cp1256' => 'windows-1256', |
||
| 64 | 'cp1257' => 'windows-1257', |
||
| 65 | 'cp1258' => 'windows-1258', |
||
| 66 | 'shift-jis' => 'cp932', |
||
| 67 | 'shift_jis' => 'cp932', |
||
| 68 | 'latin1' => 'iso-8859-1', |
||
| 69 | 'latin2' => 'iso-8859-2', |
||
| 70 | 'latin3' => 'iso-8859-3', |
||
| 71 | 'latin4' => 'iso-8859-4', |
||
| 72 | 'latin5' => 'iso-8859-9', |
||
| 73 | 'latin6' => 'iso-8859-10', |
||
| 74 | 'latin7' => 'iso-8859-13', |
||
| 75 | 'latin8' => 'iso-8859-14', |
||
| 76 | 'latin9' => 'iso-8859-15', |
||
| 77 | 'latin10' => 'iso-8859-16', |
||
| 78 | 'iso8859-1' => 'iso-8859-1', |
||
| 79 | 'iso8859-2' => 'iso-8859-2', |
||
| 80 | 'iso8859-3' => 'iso-8859-3', |
||
| 81 | 'iso8859-4' => 'iso-8859-4', |
||
| 82 | 'iso8859-5' => 'iso-8859-5', |
||
| 83 | 'iso8859-6' => 'iso-8859-6', |
||
| 84 | 'iso8859-7' => 'iso-8859-7', |
||
| 85 | 'iso8859-8' => 'iso-8859-8', |
||
| 86 | 'iso8859-9' => 'iso-8859-9', |
||
| 87 | 'iso8859-10' => 'iso-8859-10', |
||
| 88 | 'iso8859-11' => 'iso-8859-11', |
||
| 89 | 'iso8859-12' => 'iso-8859-12', |
||
| 90 | 'iso8859-13' => 'iso-8859-13', |
||
| 91 | 'iso8859-14' => 'iso-8859-14', |
||
| 92 | 'iso8859-15' => 'iso-8859-15', |
||
| 93 | 'iso8859-16' => 'iso-8859-16', |
||
| 94 | 'iso_8859-1' => 'iso-8859-1', |
||
| 95 | 'iso_8859-2' => 'iso-8859-2', |
||
| 96 | 'iso_8859-3' => 'iso-8859-3', |
||
| 97 | 'iso_8859-4' => 'iso-8859-4', |
||
| 98 | 'iso_8859-5' => 'iso-8859-5', |
||
| 99 | 'iso_8859-6' => 'iso-8859-6', |
||
| 100 | 'iso_8859-7' => 'iso-8859-7', |
||
| 101 | 'iso_8859-8' => 'iso-8859-8', |
||
| 102 | 'iso_8859-9' => 'iso-8859-9', |
||
| 103 | 'iso_8859-10' => 'iso-8859-10', |
||
| 104 | 'iso_8859-11' => 'iso-8859-11', |
||
| 105 | 'iso_8859-12' => 'iso-8859-12', |
||
| 106 | 'iso_8859-13' => 'iso-8859-13', |
||
| 107 | 'iso_8859-14' => 'iso-8859-14', |
||
| 108 | 'iso_8859-15' => 'iso-8859-15', |
||
| 109 | 'iso_8859-16' => 'iso-8859-16', |
||
| 110 | 'iso88591' => 'iso-8859-1', |
||
| 111 | 'iso88592' => 'iso-8859-2', |
||
| 112 | 'iso88593' => 'iso-8859-3', |
||
| 113 | 'iso88594' => 'iso-8859-4', |
||
| 114 | 'iso88595' => 'iso-8859-5', |
||
| 115 | 'iso88596' => 'iso-8859-6', |
||
| 116 | 'iso88597' => 'iso-8859-7', |
||
| 117 | 'iso88598' => 'iso-8859-8', |
||
| 118 | 'iso88599' => 'iso-8859-9', |
||
| 119 | 'iso885910' => 'iso-8859-10', |
||
| 120 | 'iso885911' => 'iso-8859-11', |
||
| 121 | 'iso885912' => 'iso-8859-12', |
||
| 122 | 'iso885913' => 'iso-8859-13', |
||
| 123 | 'iso885914' => 'iso-8859-14', |
||
| 124 | 'iso885915' => 'iso-8859-15', |
||
| 125 | 'iso885916' => 'iso-8859-16', |
||
| 126 | ); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $str |
||
| 130 | * @param int $mode |
||
| 131 | * @param string $charset |
||
| 132 | * |
||
| 133 | * @return array|bool |
||
| 134 | */ |
||
| 135 | 1 | public static function iconv_mime_decode_headers($str, $mode = 0, $charset = INF) |
|
| 136 | { |
||
| 137 | 1 | INF === $charset && $charset = self::$internal_encoding; |
|
| 138 | |||
| 139 | 1 | false !== strpos($str, "\r") && $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); |
|
| 140 | 1 | $str = explode("\n\n", $str, 2); |
|
| 141 | |||
| 142 | 1 | $headers = array(); |
|
| 143 | |||
| 144 | 1 | $str = preg_split('/\n(?![ \t])/', $str[0]); |
|
| 145 | // $str as $str ?? |
||
| 146 | 1 | foreach ($str as $str) { |
|
| 147 | 1 | $str = self::iconv_mime_decode($str, $mode, $charset); |
|
| 148 | |||
| 149 | 1 | if (false === $str) { |
|
| 150 | return false; |
||
| 151 | } |
||
| 152 | 1 | $str = explode(':', $str, 2); |
|
| 153 | |||
| 154 | 1 | if (2 === count($str)) { |
|
| 155 | 1 | if (isset($headers[$str[0]])) { |
|
| 156 | 1 | is_array($headers[$str[0]]) || $headers[$str[0]] = array($headers[$str[0]]); |
|
| 157 | 1 | $headers[$str[0]][] = ltrim($str[1]); |
|
| 158 | } else { |
||
| 159 | 1 | $headers[$str[0]] = ltrim($str[1]); |
|
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | 1 | return $headers; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $str |
||
| 169 | * @param int $mode |
||
| 170 | * @param string $charset |
||
| 171 | * |
||
| 172 | * @return false|string |
||
| 173 | */ |
||
| 174 | 2 | public static function iconv_mime_decode($str, $mode = 0, $charset = INF) |
|
| 175 | { |
||
| 176 | 2 | INF === $charset && $charset = self::$internal_encoding; |
|
| 177 | |||
| 178 | 2 | if (ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) { |
|
| 179 | 2 | $charset .= '//IGNORE'; |
|
| 180 | } |
||
| 181 | |||
| 182 | 2 | false !== strpos($str, "\r") && $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); |
|
| 183 | 2 | $str = preg_split('/\n(?![ \t])/', rtrim($str), 2); |
|
| 184 | 2 | $str = preg_replace('/[ \t]*\n[ \t]+/', ' ', rtrim($str[0])); |
|
| 185 | 2 | $str = preg_split('/=\?([^?]+)\?([bqBQ])\?(.*?)\?=/', $str, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 186 | |||
| 187 | 2 | $result = self::iconv('utf-8', $charset, $str[0]); |
|
| 188 | 2 | if (false === $result) { |
|
| 189 | return false; |
||
| 190 | } |
||
| 191 | |||
| 192 | 2 | $i = 1; |
|
| 193 | 2 | $len = count($str); |
|
| 194 | |||
| 195 | 2 | while ($i < $len) { |
|
| 196 | 2 | $c = strtolower($str[$i]); |
|
| 197 | 2 | if ((ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) |
|
| 198 | 2 | && 'utf-8' !== $c |
|
| 199 | 2 | && !isset(self::$alias[$c]) |
|
| 200 | 2 | && !static::loadMap('from.', $c, $d) |
|
| 201 | ) { |
||
| 202 | 1 | $d = false; |
|
| 203 | 2 | } elseif ('B' === strtoupper($str[$i + 1])) { |
|
| 204 | 2 | $d = base64_decode($str[$i + 2]); |
|
| 205 | } else { |
||
| 206 | 2 | $d = rawurldecode(strtr(str_replace('%', '%25', $str[$i + 2]), '=_', '% ')); |
|
| 207 | } |
||
| 208 | |||
| 209 | 2 | if (false !== $d) { |
|
| 210 | 2 | $result .= self::iconv($c, $charset, $d); |
|
| 211 | 2 | $d = self::iconv('utf-8', $charset, $str[$i + 3]); |
|
| 212 | 2 | if ('' !== trim($d)) { |
|
| 213 | 2 | $result .= $d; |
|
| 214 | } |
||
| 215 | 1 | } elseif (ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) { |
|
| 216 | 1 | $result .= "=?{$str[$i]}?{$str[$i + 1]}?{$str[$i + 2]}?={$str[$i + 3]}"; |
|
| 217 | } else { |
||
| 218 | $result = false; |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | |||
| 222 | 2 | $i += 4; |
|
| 223 | } |
||
| 224 | |||
| 225 | 2 | return $result; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $in_charset |
||
| 230 | * @param string $out_charset |
||
| 231 | * @param string $str |
||
| 232 | * |
||
| 233 | * @return string|false |
||
| 234 | */ |
||
| 235 | 3 | public static function iconv($in_charset, $out_charset, $str) |
|
| 236 | { |
||
| 237 | 3 | if ('' === $str .= '') { |
|
| 238 | 1 | return ''; |
|
| 239 | } |
||
| 240 | |||
| 241 | // Prepare for //IGNORE and //TRANSLIT |
||
| 242 | |||
| 243 | 3 | $TRANSLIT = $IGNORE = ''; |
|
| 244 | |||
| 245 | 3 | $out_charset = strtolower($out_charset); |
|
| 246 | 3 | $in_charset = strtolower($in_charset); |
|
| 247 | |||
| 248 | 3 | '' === $out_charset && $out_charset = 'iso-8859-1'; |
|
| 249 | 3 | '' === $in_charset && $in_charset = 'iso-8859-1'; |
|
| 250 | |||
| 251 | 3 | if ('//translit' === substr($out_charset, -10)) { |
|
| 252 | 1 | $TRANSLIT = '//TRANSLIT'; |
|
| 253 | 1 | $out_charset = substr($out_charset, 0, -10); |
|
| 254 | } |
||
| 255 | |||
| 256 | 3 | if ('//ignore' === substr($out_charset, -8)) { |
|
| 257 | 3 | $IGNORE = '//IGNORE'; |
|
| 258 | 3 | $out_charset = substr($out_charset, 0, -8); |
|
| 259 | } |
||
| 260 | |||
| 261 | 3 | '//translit' === substr($in_charset, -10) && $in_charset = substr($in_charset, 0, -10); |
|
| 262 | 3 | '//ignore' === substr($in_charset, -8) && $in_charset = substr($in_charset, 0, -8); |
|
| 263 | |||
| 264 | 3 | isset(self::$alias[$in_charset]) && $in_charset = self::$alias[$in_charset]; |
|
| 265 | 3 | isset(self::$alias[$out_charset]) && $out_charset = self::$alias[$out_charset]; |
|
| 266 | |||
| 267 | // Load charset maps |
||
| 268 | |||
| 269 | if ( |
||
| 270 | 3 | ('utf-8' !== $in_charset && !static::loadMap('from.', $in_charset, $in_map)) |
|
| 271 | || |
||
| 272 | 3 | ('utf-8' !== $out_charset && !static::loadMap('to.', $out_charset, $out_map)) |
|
| 273 | ) { |
||
| 274 | user_error(sprintf(self::ERROR_WRONG_CHARSET, $in_charset, $out_charset)); |
||
| 275 | |||
| 276 | return false; |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | 3 | if ('utf-8' !== $in_charset) { |
|
| 281 | // Convert input to UTF-8 |
||
| 282 | 2 | $result = ''; |
|
| 283 | /** @noinspection PhpUndefinedVariableInspection */ |
||
| 284 | 2 | if (self::map_to_utf8($result, $in_map, $str, $IGNORE)) { |
|
| 285 | 2 | $str = $result; |
|
| 286 | } else { |
||
| 287 | $str = false; |
||
| 288 | } |
||
| 289 | 2 | self::$is_valid_utf8 = true; |
|
| 290 | } else { |
||
| 291 | 3 | self::$is_valid_utf8 = preg_match('//u', $str); |
|
| 292 | |||
| 293 | 3 | if (!self::$is_valid_utf8 && !$IGNORE) { |
|
| 294 | 1 | user_error(self::ERROR_ILLEGAL_CHARACTER); |
|
| 295 | |||
| 296 | return false; |
||
| 297 | } |
||
| 298 | |||
| 299 | 3 | if ('utf-8' === $out_charset) { |
|
| 300 | // UTF-8 validation |
||
| 301 | 3 | $str = self::utf8_to_utf8($str, $IGNORE); |
|
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | if ( |
||
| 306 | 3 | 'utf-8' !== $out_charset |
|
| 307 | && |
||
| 308 | 3 | false !== $str |
|
| 309 | ) { |
||
| 310 | // Convert output to UTF-8 |
||
| 311 | 1 | $result = ''; |
|
| 312 | /** @noinspection PhpUndefinedVariableInspection */ |
||
| 313 | 1 | if (self::map_from_utf8($result, $out_map, $str, $IGNORE, $TRANSLIT)) { |
|
| 314 | 1 | return $result; |
|
| 315 | } else { |
||
| 316 | 1 | return false; |
|
| 317 | } |
||
| 318 | } else { |
||
| 319 | 3 | return $str; |
|
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $type |
||
| 325 | * @param string $charset |
||
| 326 | * @param array $map |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | 2 | protected static function loadMap($type, $charset, &$map) |
|
| 331 | { |
||
| 332 | 2 | if (!isset(self::$convert_map[$type . $charset])) { |
|
| 333 | 2 | if (false === $map = static::getData($type . $charset)) { |
|
| 334 | 2 | if ('to.' === $type && static::loadMap('from.', $charset, $map)) { |
|
| 335 | 1 | $map = array_flip($map); |
|
| 336 | } else { |
||
| 337 | 1 | return false; |
|
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | 2 | self::$convert_map[$type . $charset] = $map; |
|
| 342 | } else { |
||
| 343 | 2 | $map = self::$convert_map[$type . $charset]; |
|
| 344 | } |
||
| 345 | |||
| 346 | 2 | return true; |
|
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * get data |
||
| 351 | * |
||
| 352 | * @param string $file |
||
| 353 | * |
||
| 354 | * @return bool|mixed |
||
| 355 | */ |
||
| 356 | 2 | View Code Duplication | protected static function getData($file) |
| 365 | |||
| 366 | /** |
||
| 367 | * map to utf8 |
||
| 368 | * |
||
| 369 | * @param string $result |
||
| 370 | * @param array $map |
||
| 371 | * @param string $str |
||
| 372 | * @param string $IGNORE |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 2 | protected static function map_to_utf8(&$result, $map, $str, $IGNORE) |
|
| 377 | { |
||
| 378 | 2 | $len = strlen($str); |
|
| 379 | 2 | for ($i = 0; $i < $len; ++$i) { |
|
| 380 | 2 | if (isset($str[$i + 1], $map[$str[$i] . $str[$i + 1]])) { |
|
| 381 | 1 | $result .= $map[$str[$i] . $str[++$i]]; |
|
| 382 | 2 | } elseif (isset($map[$str[$i]])) { |
|
| 383 | 2 | $result .= $map[$str[$i]]; |
|
| 384 | } elseif (!$IGNORE) { |
||
| 385 | user_error(self::ERROR_ILLEGAL_CHARACTER); |
||
| 386 | |||
| 387 | return false; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | 2 | return true; |
|
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * utf8 to utf8 |
||
| 396 | * |
||
| 397 | * @param string $str |
||
| 398 | * @param string $IGNORE |
||
| 399 | * |
||
| 400 | * @return false|string |
||
| 401 | */ |
||
| 402 | 3 | protected static function utf8_to_utf8($str, $IGNORE) |
|
| 403 | { |
||
| 404 | 3 | $ulen_mask = self::$ulen_mask; |
|
| 405 | 3 | $valid = self::$is_valid_utf8; |
|
| 406 | |||
| 407 | 3 | $u = $str; |
|
| 408 | 3 | $i = $j = 0; |
|
| 409 | 3 | $len = strlen($str); |
|
| 410 | |||
| 411 | 3 | while ($i < $len) { |
|
| 412 | 3 | if ($str[$i] < "\x80") { |
|
| 413 | 3 | $u[$j++] = $str[$i++]; |
|
| 414 | } else { |
||
| 415 | 2 | $ulen = $str[$i] & "\xF0"; |
|
| 416 | 2 | $ulen = isset($ulen_mask[$ulen]) ? $ulen_mask[$ulen] : 1; |
|
| 417 | 2 | $uchr = substr($str, $i, $ulen); |
|
| 418 | |||
| 419 | 2 | if (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr))) { |
|
| 420 | 1 | if ($IGNORE) { |
|
| 421 | 1 | ++$i; |
|
| 422 | 1 | continue; |
|
| 423 | } |
||
| 424 | |||
| 425 | user_error(self::ERROR_ILLEGAL_CHARACTER); |
||
| 426 | |||
| 427 | return false; |
||
| 428 | } else { |
||
| 429 | 1 | $i += $ulen; |
|
| 430 | } |
||
| 431 | |||
| 432 | 1 | $u[$j++] = $uchr[0]; |
|
| 433 | |||
| 434 | 1 | isset($uchr[1]) && 0 !== ($u[$j++] = $uchr[1]) |
|
| 435 | 1 | && isset($uchr[2]) && 0 !== ($u[$j++] = $uchr[2]) |
|
| 436 | 1 | && isset($uchr[3]) && 0 !== ($u[$j++] = $uchr[3]); |
|
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | 3 | return substr($u, 0, $j); |
|
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * map from utf8 |
||
| 445 | * |
||
| 446 | * @param string $result |
||
| 447 | * @param array $map |
||
| 448 | * @param string $str |
||
| 449 | * @param string $IGNORE |
||
| 450 | * @param string $TRANSLIT |
||
| 451 | * |
||
| 452 | * @return bool |
||
| 453 | */ |
||
| 454 | 1 | protected static function map_from_utf8(&$result, $map, $str, $IGNORE, $TRANSLIT) |
|
| 455 | { |
||
| 456 | 1 | $ulen_mask = self::$ulen_mask; |
|
| 457 | 1 | $valid = self::$is_valid_utf8; |
|
| 458 | |||
| 459 | 1 | if ($TRANSLIT) { |
|
| 460 | 1 | self::$translit_map or self::$translit_map = static::getData('translit'); |
|
| 461 | } |
||
| 462 | |||
| 463 | 1 | $i = 0; |
|
| 464 | 1 | $len = strlen($str); |
|
| 465 | |||
| 466 | 1 | while ($i < $len) { |
|
| 467 | 1 | if ($str[$i] < "\x80") { |
|
| 468 | 1 | $uchr = $str[$i++]; |
|
| 469 | } else { |
||
| 470 | 1 | $ulen = $str[$i] & "\xF0"; |
|
| 471 | 1 | $ulen = isset($ulen_mask[$ulen]) ? $ulen_mask[$ulen] : 1; |
|
| 472 | 1 | $uchr = substr($str, $i, $ulen); |
|
| 473 | |||
| 474 | 1 | if ($IGNORE && (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr)))) { |
|
| 475 | ++$i; |
||
| 476 | continue; |
||
| 477 | } else { |
||
| 478 | 1 | $i += $ulen; |
|
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | 1 | if (isset($map[$uchr])) { |
|
| 483 | 1 | $result .= $map[$uchr]; |
|
| 484 | 1 | } elseif ($TRANSLIT) { |
|
| 485 | 1 | if (isset(self::$translit_map[$uchr])) { |
|
| 486 | 1 | $uchr = self::$translit_map[$uchr]; |
|
| 487 | 1 | } elseif ($uchr >= "\xC3\x80") { |
|
| 488 | 1 | $uchr = \Normalizer::normalize($uchr, \Normalizer::NFD); |
|
| 489 | |||
| 490 | 1 | if ($uchr[0] < "\x80") { |
|
| 491 | 1 | $uchr = $uchr[0]; |
|
| 492 | } elseif ($IGNORE) { |
||
| 493 | continue; |
||
| 494 | } else { |
||
| 495 | return false; |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | 1 | $str = $uchr . substr($str, $i); |
|
| 500 | 1 | $len = strlen($str); |
|
| 501 | 1 | $i = 0; |
|
| 502 | 1 | } elseif (!$IGNORE) { |
|
| 503 | 1 | return false; |
|
| 504 | } |
||
| 505 | } |
||
| 506 | |||
| 507 | 1 | return true; |
|
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * iconv: get encoding |
||
| 512 | * |
||
| 513 | * @param string $type |
||
| 514 | * |
||
| 515 | * @return array|string |
||
| 516 | */ |
||
| 517 | 1 | public static function iconv_get_encoding($type = 'all') |
|
| 518 | { |
||
| 519 | switch ($type) { |
||
| 520 | 1 | case 'input_encoding' : |
|
| 521 | 1 | return self::$input_encoding; |
|
| 522 | case 'output_encoding' : |
||
| 523 | 1 | return self::$output_encoding; |
|
| 524 | 1 | case 'internal_encoding': |
|
| 525 | 1 | return self::$internal_encoding; |
|
| 526 | } |
||
| 527 | |||
| 528 | return array( |
||
| 529 | 1 | 'input_encoding' => self::$input_encoding, |
|
| 530 | 1 | 'output_encoding' => self::$output_encoding, |
|
| 531 | 1 | 'internal_encoding' => self::$internal_encoding, |
|
| 532 | ); |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * iconv: set encoding |
||
| 537 | * |
||
| 538 | * @param string $type |
||
| 539 | * @param string $charset |
||
| 540 | * |
||
| 541 | * @return bool |
||
| 542 | */ |
||
| 543 | 1 | public static function iconv_set_encoding($type, $charset) |
|
| 544 | { |
||
| 545 | switch ($type) { |
||
| 546 | 1 | case 'input_encoding' : |
|
| 547 | 1 | self::$input_encoding = $charset; |
|
| 548 | 1 | break; |
|
| 549 | case 'output_encoding' : |
||
| 550 | 1 | self::$output_encoding = $charset; |
|
| 551 | 1 | break; |
|
| 552 | 1 | case 'internal_encoding': |
|
| 553 | 1 | self::$internal_encoding = $charset; |
|
| 554 | 1 | break; |
|
| 555 | |||
| 556 | default: |
||
| 557 | 1 | return false; |
|
| 558 | } |
||
| 559 | |||
| 560 | 1 | return true; |
|
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * iconv: mime encode |
||
| 565 | * |
||
| 566 | * @param string $field_name |
||
| 567 | * @param string $field_value |
||
| 568 | * @param array $pref |
||
| 569 | * |
||
| 570 | * @return false|string |
||
| 571 | */ |
||
| 572 | 1 | public static function iconv_mime_encode($field_name, $field_value, $pref = INF) |
|
| 573 | { |
||
| 574 | 1 | if (is_array($pref) === false) { |
|
| 575 | $pref = array(); |
||
| 576 | } |
||
| 577 | |||
| 578 | 1 | $pref = array_merge( |
|
| 579 | array( |
||
| 580 | 1 | 'scheme' => 'B', |
|
| 581 | 1 | 'input-charset' => self::$internal_encoding, |
|
| 582 | 1 | 'output-charset' => self::$internal_encoding, |
|
| 583 | 1 | 'line-length' => 76, |
|
| 584 | 1 | 'line-break-chars' => "\r\n", |
|
| 585 | ), |
||
| 586 | $pref |
||
| 587 | ); |
||
| 588 | |||
| 589 | 1 | preg_match('/[\x80-\xFF]/', $field_name) && $field_name = ''; |
|
| 590 | |||
| 591 | 1 | $scheme = strtoupper(substr($pref['scheme'], 0, 1)); |
|
| 592 | 1 | $in = strtolower($pref['input-charset']); |
|
| 593 | 1 | $out = strtolower($pref['output-charset']); |
|
| 594 | |||
| 595 | 1 | if ('utf-8' !== $in && false === $field_value = self::iconv($in, 'utf-8', $field_value)) { |
|
| 596 | return false; |
||
| 597 | } |
||
| 598 | |||
| 599 | 1 | preg_match_all('/./us', $field_value, $chars); |
|
| 600 | |||
| 601 | 1 | $chars = isset($chars[0]) ? $chars[0] : array(); |
|
| 602 | |||
| 603 | 1 | $line_break = (int)$pref['line-length']; |
|
| 604 | 1 | $line_start = "=?{$pref['output-charset']}?{$scheme}?"; |
|
| 605 | 1 | $line_length = strlen($field_name) + 2 + strlen($line_start) + 2; |
|
| 606 | 1 | $line_offset = strlen($line_start) + 3; |
|
| 607 | 1 | $line_data = ''; |
|
| 608 | |||
| 609 | 1 | $field_value = array(); |
|
| 610 | |||
| 611 | 1 | $Q = 'Q' === $scheme; |
|
| 612 | |||
| 613 | 1 | foreach ($chars as $c) { |
|
| 614 | 1 | if ('utf-8' !== $out && false === $c = self::iconv('utf-8', $out, $c)) { |
|
| 615 | return false; |
||
| 616 | } |
||
| 617 | |||
| 618 | 1 | $o = $Q |
|
| 619 | 1 | ? $c = preg_replace_callback( |
|
| 620 | 1 | '/[=_\?\x00-\x1F\x80-\xFF]/', |
|
| 621 | array( |
||
| 622 | 1 | __CLASS__, |
|
| 623 | 'qp_byte_callback', |
||
| 624 | ), |
||
| 625 | $c |
||
| 626 | ) |
||
| 627 | 1 | : base64_encode($line_data . $c); |
|
| 628 | |||
| 629 | 1 | if (isset($o[$line_break - $line_length])) { |
|
| 630 | 1 | $Q || $line_data = base64_encode($line_data); |
|
| 631 | 1 | $field_value[] = $line_start . $line_data . '?='; |
|
| 632 | 1 | $line_length = $line_offset; |
|
| 633 | 1 | $line_data = ''; |
|
| 634 | } |
||
| 635 | |||
| 636 | 1 | $line_data .= $c; |
|
| 637 | 1 | $Q && $line_length += strlen($c); |
|
| 638 | } |
||
| 639 | |||
| 640 | 1 | if ('' !== $line_data) { |
|
| 641 | 1 | $Q || $line_data = base64_encode($line_data); |
|
| 642 | 1 | $field_value[] = $line_start . $line_data . '?='; |
|
| 643 | } |
||
| 644 | |||
| 645 | 1 | return $field_name . ': ' . implode($pref['line-break-chars'] . ' ', $field_value); |
|
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @param string $buffer |
||
| 650 | * @param mixed $mode |
||
| 651 | * |
||
| 652 | * @return string|false |
||
| 653 | */ |
||
| 654 | public static function ob_iconv_handler($buffer, /** @noinspection PhpUnusedParameterInspection */ $mode) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * iconv: strpos |
||
| 661 | * |
||
| 662 | * @param string $haystack |
||
| 663 | * @param string $needle |
||
| 664 | * @param int $offset |
||
| 665 | * @param string $encoding |
||
| 666 | * |
||
| 667 | * @return bool|int |
||
| 668 | */ |
||
| 669 | 1 | public static function iconv_strpos($haystack, $needle, $offset = 0, $encoding = INF) |
|
| 670 | { |
||
| 671 | 1 | INF === $encoding && $encoding = self::$internal_encoding; |
|
| 672 | |||
| 673 | 1 | View Code Duplication | if (0 !== strncasecmp($encoding, 'utf-8', 5)) { |
| 674 | if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) { |
||
| 675 | return false; |
||
| 676 | } |
||
| 677 | |||
| 678 | if (false === $needle = self::iconv($encoding, 'utf-8', $needle)) { |
||
| 679 | return false; |
||
| 680 | } |
||
| 681 | } |
||
| 682 | |||
| 683 | 1 | $offset = (int)$offset; |
|
| 684 | |||
| 685 | 1 | if ($offset) { |
|
| 686 | $haystack = self::iconv_substr($haystack, $offset, 2147483647, 'utf-8'); |
||
| 687 | } |
||
| 688 | 1 | $pos = strpos($haystack, $needle); |
|
| 689 | |||
| 690 | 1 | return false === $pos ? false : ($offset + ($pos ? self::iconv_strlen(substr($haystack, 0, $pos), 'utf-8') : 0)); |
|
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * iconv: substr |
||
| 695 | * |
||
| 696 | * @param string $s |
||
| 697 | * @param int $start |
||
| 698 | * @param int $length |
||
| 699 | * @param string $encoding |
||
| 700 | * |
||
| 701 | * @return false|string |
||
| 702 | */ |
||
| 703 | 1 | public static function iconv_substr($s, $start, $length = 2147483647, $encoding = INF) |
|
| 704 | { |
||
| 705 | 1 | INF === $encoding && $encoding = self::$internal_encoding; |
|
| 706 | 1 | View Code Duplication | if (0 === strncasecmp($encoding, 'utf-8', 5)) { |
| 707 | 1 | $encoding = INF; |
|
| 708 | } elseif (false === $s = self::iconv($encoding, 'utf-8', $s)) { |
||
| 709 | return false; |
||
| 710 | } |
||
| 711 | |||
| 712 | 1 | $s .= ''; |
|
| 713 | 1 | $slen = self::iconv_strlen($s, 'utf-8'); |
|
| 714 | 1 | $start = (int)$start; |
|
| 715 | |||
| 716 | 1 | if (0 > $start) { |
|
| 717 | $start += $slen; |
||
| 718 | } |
||
| 719 | |||
| 720 | 1 | if (0 > $start) { |
|
| 721 | return false; |
||
| 722 | } |
||
| 723 | |||
| 724 | 1 | if ($start >= $slen) { |
|
| 725 | return false; |
||
| 726 | } |
||
| 727 | |||
| 728 | 1 | $rx = $slen - $start; |
|
| 729 | |||
| 730 | 1 | if (0 > $length) { |
|
| 731 | $length += $rx; |
||
| 732 | } |
||
| 733 | |||
| 734 | 1 | if (0 === $length) { |
|
| 735 | return ''; |
||
| 736 | } |
||
| 737 | |||
| 738 | 1 | if (0 > $length) { |
|
| 739 | return false; |
||
| 740 | } |
||
| 741 | |||
| 742 | 1 | if ($length > $rx) { |
|
| 743 | $length = $rx; |
||
| 744 | } |
||
| 745 | |||
| 746 | 1 | $rx = '/^' . ($start ? self::preg_offset($start) : '') . '(' . self::preg_offset($length) . ')/u'; |
|
| 747 | |||
| 748 | 1 | $s = preg_match($rx, $s, $s) ? $s[1] : ''; |
|
| 749 | |||
| 750 | 1 | if (INF === $encoding) { |
|
| 751 | 1 | return $s; |
|
| 752 | } else { |
||
| 753 | return self::iconv('utf-8', $encoding, $s); |
||
| 754 | } |
||
| 755 | } |
||
| 756 | |||
| 757 | /** |
||
| 758 | * iconv: strlen |
||
| 759 | * |
||
| 760 | * @param string $s |
||
| 761 | * @param string $encoding |
||
| 762 | * |
||
| 763 | * @return bool|int |
||
| 764 | */ |
||
| 765 | 3 | public static function iconv_strlen($s, $encoding = INF) |
|
| 773 | |||
| 774 | /** |
||
| 775 | * @param string $s |
||
| 776 | * @param string $encoding |
||
| 777 | * |
||
| 778 | * @return bool|int |
||
| 779 | */ |
||
| 780 | 3 | public static function strlen1($s, $encoding = INF) |
|
| 790 | |||
| 791 | /** |
||
| 792 | * @param string $s |
||
| 793 | * @param string $encoding |
||
| 794 | * |
||
| 795 | * @return bool|int |
||
| 796 | */ |
||
| 797 | 1 | public static function strlen2($s, $encoding = INF) |
|
| 798 | { |
||
| 799 | 1 | INF === $encoding && $encoding = self::$internal_encoding; |
|
| 800 | |||
| 801 | 1 | View Code Duplication | if (0 !== strncasecmp($encoding, 'utf-8', 5) && false === $s = self::iconv($encoding, 'utf-8', $s)) { |
| 802 | return false; |
||
| 803 | } |
||
| 804 | |||
| 805 | 1 | $ulen_mask = self::$ulen_mask; |
|
| 806 | |||
| 807 | 1 | $i = 0; |
|
| 808 | 1 | $j = 0; |
|
| 809 | 1 | $len = strlen($s); |
|
| 810 | |||
| 811 | 1 | while ($i < $len) { |
|
| 812 | 1 | $u = $s[$i] & "\xF0"; |
|
| 813 | 1 | $i += isset($ulen_mask[$u]) ? $ulen_mask[$u] : 1; |
|
| 814 | 1 | ++$j; |
|
| 815 | } |
||
| 816 | |||
| 817 | 1 | return $j; |
|
| 818 | } |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @param int $offset |
||
| 822 | * |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | 1 | protected static function preg_offset($offset) |
|
| 826 | { |
||
| 827 | 1 | $rx = array(); |
|
| 828 | 1 | $offset = (int)$offset; |
|
| 829 | |||
| 830 | 1 | while ($offset > 65535) { |
|
| 831 | $rx[] = '.{65535}'; |
||
| 832 | $offset -= 65535; |
||
| 833 | } |
||
| 834 | |||
| 835 | 1 | return implode('', $rx) . '.{' . $offset . '}'; |
|
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * iconv: strrpos |
||
| 840 | * |
||
| 841 | * @param string $haystack |
||
| 842 | * @param string $needle |
||
| 843 | * @param string $encoding |
||
| 844 | * |
||
| 845 | * @return bool|int |
||
| 846 | */ |
||
| 847 | 1 | public static function iconv_strrpos($haystack, $needle, $encoding = INF) |
|
| 848 | { |
||
| 849 | 1 | INF === $encoding && $encoding = self::$internal_encoding; |
|
| 850 | |||
| 851 | 1 | View Code Duplication | if (0 !== strncasecmp($encoding, 'utf-8', 5)) { |
| 852 | if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) { |
||
| 853 | return false; |
||
| 854 | } |
||
| 855 | if (false === $needle = self::iconv($encoding, 'utf-8', $needle)) { |
||
| 856 | return false; |
||
| 857 | } |
||
| 858 | } |
||
| 859 | |||
| 860 | 1 | $pos = isset($needle[0]) ? strrpos($haystack, $needle) : false; |
|
| 861 | |||
| 862 | 1 | return false === $pos ? false : self::iconv_strlen($pos ? substr($haystack, 0, $pos) : $haystack, 'utf-8'); |
|
| 863 | } |
||
| 864 | |||
| 865 | /** |
||
| 866 | * @param array $m |
||
| 867 | * |
||
| 868 | * @return string |
||
| 869 | */ |
||
| 870 | 1 | protected static function qp_byte_callback($m) |
|
| 874 | } |
||
| 875 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.