upgrade.php
631 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * api: php |
| 6 | * title: upgrade.php |
| 7 | * description: Emulates functions from new PHP versions on older interpreters. |
| 8 | * version: 17 |
| 9 | * license: Public Domain |
| 10 | * url: http://freshmeat.net/projects/upgradephp |
| 11 | * type: functions |
| 12 | * category: library |
| 13 | * priority: auto |
| 14 | * load_if: (PHP_VERSION<5.2) |
| 15 | * sort: -255 |
| 16 | * provides: upgrade-php, api:php5, json |
| 17 | * |
| 18 | * |
| 19 | * By loading this library you get PHP version independence. It provides |
| 20 | * downwards compatibility to older PHP interpreters by emulating missing |
| 21 | * functions or constants using IDENTICAL NAMES. So this doesn't slow down |
| 22 | * script execution on setups where the native functions already exist. It |
| 23 | * is meant as quick drop-in solution. It spares you from rewriting code or |
| 24 | * using cumbersome workarounds instead of the more powerful v5 functions. |
| 25 | * |
| 26 | * It cannot mirror PHP5s extended OO-semantics and functionality into PHP4 |
| 27 | * however. A few features are added here that weren't part of PHP yet. And |
| 28 | * some other function collections are separated out into the ext/ directory. |
| 29 | * It doesn't produce many custom error messages (YAGNI), and instead leaves |
| 30 | * reporting to invoked functions or for native PHP execution. |
| 31 | * |
| 32 | * And further this is PUBLIC DOMAIN (no copyright, no license, no warranty) |
| 33 | * so therefore compatible to ALL open source licenses. You could rip this |
| 34 | * paragraph out to republish this instead only under more restrictive terms |
| 35 | * or your favorite license (GNU LGPL/GPL, BSDL, MPL/CDDL, Artistic/PHPL, ..) |
| 36 | * |
| 37 | * Any contribution is appreciated. <milky*users#sf#net> |
| 38 | * |
| 39 | */ |
| 40 | use Piwik\SettingsServer; |
| 41 | /** |
| 42 | * ------------------------------ 5.2 --- |
| 43 | * @group 5_2 |
| 44 | * @since 5.2 |
| 45 | * |
| 46 | * Additions of PHP 5.2.0 |
| 47 | * - some listed here might have appeared earlier or in release candidates |
| 48 | * |
| 49 | * @emulated |
| 50 | * error_get_last |
| 51 | * preg_last_error |
| 52 | * lchown |
| 53 | * lchgrp |
| 54 | * E_RECOVERABLE_ERROR |
| 55 | * M_SQRTPI |
| 56 | * M_LNPI |
| 57 | * M_EULER |
| 58 | * M_SQRT3 |
| 59 | * |
| 60 | * @missing |
| 61 | * sys_getloadavg |
| 62 | * inet_ntop |
| 63 | * inet_pton |
| 64 | * array_fill_keys |
| 65 | * array_intersect_key |
| 66 | * array_intersect_ukey |
| 67 | * array_diff_key |
| 68 | * array_diff_ukey |
| 69 | * array_product |
| 70 | * pdo_drivers |
| 71 | * ftp_ssl_connect |
| 72 | * XmlReader |
| 73 | * XmlWriter |
| 74 | * PDO* |
| 75 | * |
| 76 | * @unimplementable |
| 77 | * stream_* |
| 78 | * |
| 79 | */ |
| 80 | /** |
| 81 | * Constants for future 64-bit integer support. |
| 82 | * |
| 83 | */ |
| 84 | if (!\defined("PHP_INT_SIZE")) { |
| 85 | \define("PHP_INT_SIZE", 4); |
| 86 | } |
| 87 | if (!\defined("PHP_INT_MAX")) { |
| 88 | \define("PHP_INT_MAX", 2147483647); |
| 89 | } |
| 90 | /* |
| 91 | These functions emulate the "character type" extension, which is |
| 92 | present in PHP first since version 4.3 per default. In this variant |
| 93 | only ASCII and Latin-1 characters are being handled. The first part |
| 94 | is eventually faster. |
| 95 | */ |
| 96 | /** |
| 97 | * Sets the default client character set. |
| 98 | * |
| 99 | * @compat |
| 100 | * Procedural style |
| 101 | * @bugs |
| 102 | * PHP documentation says this function exists in PHP 5 >= 5.0.5, |
| 103 | * but it also depends on the versions of external libraries, e.g., |
| 104 | * php_mysqli.dll and libmysql.dll. |
| 105 | * |
| 106 | * @param $link mysqli MySQLi connection resource |
| 107 | * @param $charset string Character set |
| 108 | * @return bool TRUE on success, FALSE on failure |
| 109 | */ |
| 110 | if (\in_array('mysqli', @\get_loaded_extensions()) && !\function_exists('mysqli_set_charset')) { |
| 111 | function mysqli_set_charset($link, $charset) |
| 112 | { |
| 113 | return \mysqli_query($link, "SET NAMES '{$charset}'"); |
| 114 | } |
| 115 | } |
| 116 | /** |
| 117 | * parse_ini_file() replacement. |
| 118 | * Behaves like parse_ini_file($filename, $process_sections); |
| 119 | * |
| 120 | * @author Andrew Sohn <asohn (at) aircanopy (dot) net> |
| 121 | * @author anthon (dot) pang (at) gmail (dot) com |
| 122 | * |
| 123 | * @param string $filename |
| 124 | * @param bool $process_sections (defaults to false) |
| 125 | * @return array |
| 126 | */ |
| 127 | if (\function_exists('parse_ini_file')) { |
| 128 | // provide a wrapper |
| 129 | function _parse_ini_file($filename, $process_sections = \false) |
| 130 | { |
| 131 | if (!\file_exists($filename)) { |
| 132 | return \false; |
| 133 | } |
| 134 | return \parse_ini_file($filename, $process_sections); |
| 135 | } |
| 136 | } else { |
| 137 | // we can't redefine parse_ini_file() if it has been disabled |
| 138 | function _parse_ini_file($filename, $process_sections = \false) |
| 139 | { |
| 140 | if (!\file_exists($filename)) { |
| 141 | return \false; |
| 142 | } |
| 143 | if (\function_exists('file_get_contents')) { |
| 144 | $ini = \file_get_contents($filename); |
| 145 | } else { |
| 146 | if (\function_exists('file')) { |
| 147 | if ($ini = \file($filename)) { |
| 148 | $ini = \implode("\n", $ini); |
| 149 | } |
| 150 | } else { |
| 151 | if (\function_exists('fopen') && \function_exists('fread')) { |
| 152 | $handle = \fopen($filename, 'r'); |
| 153 | if (!$handle) { |
| 154 | return \false; |
| 155 | } |
| 156 | $ini = \fread($handle, \filesize($filename)); |
| 157 | \fclose($handle); |
| 158 | } else { |
| 159 | return \false; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if ($ini === \false) { |
| 164 | return \false; |
| 165 | } |
| 166 | if (\is_string($ini)) { |
| 167 | $ini = \explode("\n", \str_replace("\r", "\n", $ini)); |
| 168 | } |
| 169 | if (\count($ini) == 0) { |
| 170 | return array(); |
| 171 | } |
| 172 | $sections = array(); |
| 173 | $values = array(); |
| 174 | $result = array(); |
| 175 | $globals = array(); |
| 176 | $i = 0; |
| 177 | foreach ($ini as $line) { |
| 178 | $line = \trim($line); |
| 179 | $line = \str_replace("\t", " ", $line); |
| 180 | // Comments |
| 181 | if (!\preg_match('/^[a-zA-Z0-9[]/', $line)) { |
| 182 | continue; |
| 183 | } |
| 184 | // Sections |
| 185 | if ($line[0] == '[') { |
| 186 | $tmp = \explode(']', $line); |
| 187 | $sections[] = \trim(\substr($tmp[0], 1)); |
| 188 | $i++; |
| 189 | continue; |
| 190 | } |
| 191 | // Key-value pair |
| 192 | list($key, $value) = \explode('=', $line, 2); |
| 193 | $key = \trim($key); |
| 194 | $value = \trim($value); |
| 195 | if (\strstr($value, ";")) { |
| 196 | $tmp = \explode(';', $value); |
| 197 | if (\count($tmp) == 2) { |
| 198 | if ($value[0] != '"' && $value[0] != "'" || \preg_match('/^".*"\\s*;/', $value) || \preg_match('/^".*;[^"]*$/', $value) || \preg_match("/^'.*'\\s*;/", $value) || \preg_match("/^'.*;[^']*\$/", $value)) { |
| 199 | $value = $tmp[0]; |
| 200 | } |
| 201 | } else { |
| 202 | if ($value[0] == '"') { |
| 203 | $value = \preg_replace('/^"(.*)".*/', '$1', $value); |
| 204 | } elseif ($value[0] == "'") { |
| 205 | $value = \preg_replace("/^'(.*)'.*/", '$1', $value); |
| 206 | } else { |
| 207 | $value = $tmp[0]; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | $value = \trim($value); |
| 212 | $value = \trim($value, "'\""); |
| 213 | if ($i == 0) { |
| 214 | if (\substr($key, -2) == '[]') { |
| 215 | $globals[\substr($key, 0, -2)][] = $value; |
| 216 | } else { |
| 217 | $globals[$key] = $value; |
| 218 | } |
| 219 | } else { |
| 220 | if (\substr($key, -2) == '[]') { |
| 221 | $values[$i - 1][\substr($key, 0, -2)][] = $value; |
| 222 | } else { |
| 223 | $values[$i - 1][$key] = $value; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | for ($j = 0; $j < $i; $j++) { |
| 228 | if (isset($values[$j])) { |
| 229 | if ($process_sections === \true) { |
| 230 | $result[$sections[$j]] = $values[$j]; |
| 231 | } else { |
| 232 | $result[] = $values[$j]; |
| 233 | } |
| 234 | } else { |
| 235 | if ($process_sections === \true) { |
| 236 | $result[$sections[$j]] = array(); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | return $result + $globals; |
| 241 | } |
| 242 | } |
| 243 | /** |
| 244 | * glob() replacement. |
| 245 | * Behaves like glob($pattern, $flags) |
| 246 | * |
| 247 | * @author BigueNique AT yahoo DOT ca |
| 248 | * @author anthon (dot) pang (at) gmail (dot) com |
| 249 | * |
| 250 | * @param string $pattern |
| 251 | * @param int $flags GLOBL_ONLYDIR, GLOB_MARK, GLOB_NOSORT (other flags not supported; defaults to 0) |
| 252 | * @return array |
| 253 | */ |
| 254 | if (\function_exists('glob')) { |
| 255 | // provide a wrapper |
| 256 | function _glob($pattern, $flags = 0) |
| 257 | { |
| 258 | return \glob($pattern, $flags); |
| 259 | } |
| 260 | } else { |
| 261 | if (\function_exists('opendir') && \function_exists('readdir')) { |
| 262 | // we can't redefine glob() if it has been disabled |
| 263 | function _glob($pattern, $flags = 0) |
| 264 | { |
| 265 | $path = \dirname($pattern); |
| 266 | $filePattern = \basename($pattern); |
| 267 | if (\is_dir($path) && ($handle = \opendir($path)) !== \false) { |
| 268 | $matches = array(); |
| 269 | while (($file = \readdir($handle)) !== \false) { |
| 270 | if ($file[0] != '.' && \fnmatch($filePattern, $file) && (!($flags & \GLOB_ONLYDIR) || \is_dir("{$path}/{$file}"))) { |
| 271 | $matches[] = "{$path}/{$file}" . ($flags & \GLOB_MARK ? '/' : ''); |
| 272 | } |
| 273 | } |
| 274 | \closedir($handle); |
| 275 | if (!($flags & \GLOB_NOSORT)) { |
| 276 | \sort($matches); |
| 277 | } |
| 278 | return $matches; |
| 279 | } |
| 280 | return \false; |
| 281 | } |
| 282 | } else { |
| 283 | function _glob($pattern, $flags = 0) |
| 284 | { |
| 285 | return \false; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | /** |
| 290 | * Reads entire file into a string. |
| 291 | * This function is not 100% compatible with the native function. |
| 292 | * |
| 293 | * @see https://php.net/file_get_contents |
| 294 | * @since PHP 4.3.0 |
| 295 | * |
| 296 | * @param string $filename Name of the file to read. |
| 297 | * @return string The read data or false on failure. |
| 298 | */ |
| 299 | if (!\function_exists('file_get_contents')) { |
| 300 | function file_get_contents($filename) |
| 301 | { |
| 302 | $fhandle = \fopen($filename, "r"); |
| 303 | $fcontents = \fread($fhandle, \filesize($filename)); |
| 304 | \fclose($fhandle); |
| 305 | return $fcontents; |
| 306 | } |
| 307 | } |
| 308 | /** |
| 309 | * Safe serialize() and unserialize() replacements |
| 310 | * |
| 311 | * @license Public Domain |
| 312 | * |
| 313 | * @author anthon (dot) pang (at) gmail (dot) com |
| 314 | */ |
| 315 | /* |
| 316 | * Arbitrary limits for safe_unserialize() |
| 317 | */ |
| 318 | \define('MAX_SERIALIZED_INPUT_LENGTH', 4096); |
| 319 | \define('MAX_SERIALIZED_ARRAY_LENGTH', 256); |
| 320 | \define('MAX_SERIALIZED_ARRAY_DEPTH', 3); |
| 321 | /** |
| 322 | * Safe serialize() replacement |
| 323 | * - output a strict subset of PHP's native serialized representation |
| 324 | * - does not serialize objects |
| 325 | * |
| 326 | * @param mixed $value |
| 327 | * @return string |
| 328 | * @throw Exception if $value is malformed or contains unsupported types (e.g., resources, objects) |
| 329 | */ |
| 330 | function _safe_serialize($value) |
| 331 | { |
| 332 | if (\is_null($value)) { |
| 333 | return 'N;'; |
| 334 | } |
| 335 | if (\is_bool($value)) { |
| 336 | return 'b:' . (int) $value . ';'; |
| 337 | } |
| 338 | if (\is_int($value)) { |
| 339 | return 'i:' . $value . ';'; |
| 340 | } |
| 341 | if (\is_float($value)) { |
| 342 | return 'd:' . \str_replace(',', '.', $value) . ';'; |
| 343 | } |
| 344 | if (\is_string($value)) { |
| 345 | return 's:' . \strlen($value) . ':"' . $value . '";'; |
| 346 | } |
| 347 | if (\is_array($value)) { |
| 348 | $out = ''; |
| 349 | foreach ($value as $k => $v) { |
| 350 | $out .= \_safe_serialize($k) . \_safe_serialize($v); |
| 351 | } |
| 352 | return 'a:' . \count($value) . ':{' . $out . '}'; |
| 353 | } |
| 354 | // safe_serialize cannot serialize resources or objects |
| 355 | return \false; |
| 356 | } |
| 357 | /** |
| 358 | * Wrapper for _safe_serialize() that handles exceptions and multibyte encoding issue |
| 359 | * |
| 360 | * @param mixed $value |
| 361 | * @return string |
| 362 | */ |
| 363 | function safe_serialize($value) |
| 364 | { |
| 365 | // ensure we use the byte count for strings even when strlen() is overloaded by mb_strlen() |
| 366 | if (\function_exists('mb_internal_encoding') && (int) \ini_get('mbstring.func_overload') & 2) { |
| 367 | $mbIntEnc = \mb_internal_encoding(); |
| 368 | \mb_internal_encoding('ASCII'); |
| 369 | } |
| 370 | $out = \_safe_serialize($value); |
| 371 | if (isset($mbIntEnc)) { |
| 372 | \mb_internal_encoding($mbIntEnc); |
| 373 | } |
| 374 | return $out; |
| 375 | } |
| 376 | /** |
| 377 | * Safe unserialize() replacement |
| 378 | * - accepts a strict subset of PHP's native serialized representation |
| 379 | * - does not unserialize objects |
| 380 | * |
| 381 | * @param string $str |
| 382 | * @return mixed |
| 383 | * @throw Exception if $str is malformed or contains unsupported types (e.g., resources, objects) |
| 384 | */ |
| 385 | function _safe_unserialize($str) |
| 386 | { |
| 387 | if (\strlen($str) > \MAX_SERIALIZED_INPUT_LENGTH) { |
| 388 | // input exceeds MAX_SERIALIZED_INPUT_LENGTH |
| 389 | return \false; |
| 390 | } |
| 391 | if (empty($str) || !\is_string($str)) { |
| 392 | return \false; |
| 393 | } |
| 394 | $stack = array(); |
| 395 | $expected = array(); |
| 396 | /* |
| 397 | * states: |
| 398 | * 0 - initial state, expecting a single value or array |
| 399 | * 1 - terminal state |
| 400 | * 2 - in array, expecting end of array or a key |
| 401 | * 3 - in array, expecting value or another array |
| 402 | */ |
| 403 | $state = 0; |
| 404 | while ($state != 1) { |
| 405 | $type = isset($str[0]) ? $str[0] : ''; |
| 406 | if ($type == '}') { |
| 407 | $str = \substr($str, 1); |
| 408 | } else { |
| 409 | if ($type == 'N' && $str[1] == ';') { |
| 410 | $value = null; |
| 411 | $str = \substr($str, 2); |
| 412 | } else { |
| 413 | if ($type == 'b' && \preg_match('/^b:([01]);/', $str, $matches)) { |
| 414 | $value = $matches[1] == '1' ? \true : \false; |
| 415 | $str = \substr($str, 4); |
| 416 | } else { |
| 417 | if ($type == 'i' && \preg_match('/^i:(-?[0-9]+);(.*)/s', $str, $matches)) { |
| 418 | $value = (int) $matches[1]; |
| 419 | $str = $matches[2]; |
| 420 | } else { |
| 421 | if ($type == 'd' && \preg_match('/^d:(-?[0-9]+\\.?[0-9]*(E[+-][0-9]+)?);(.*)/s', $str, $matches)) { |
| 422 | $value = (float) $matches[1]; |
| 423 | $str = $matches[3]; |
| 424 | } else { |
| 425 | if ($type == 's' && \preg_match('/^s:([0-9]+):"(.*)/s', $str, $matches) && \substr($matches[2], (int) $matches[1], 2) == '";') { |
| 426 | $value = \substr($matches[2], 0, (int) $matches[1]); |
| 427 | $str = \substr($matches[2], (int) $matches[1] + 2); |
| 428 | } else { |
| 429 | if ($type == 'a' && \preg_match('/^a:([0-9]+):{(.*)/s', $str, $matches) && $matches[1] < \MAX_SERIALIZED_ARRAY_LENGTH) { |
| 430 | $expectedLength = (int) $matches[1]; |
| 431 | $str = $matches[2]; |
| 432 | } else { |
| 433 | // object or unknown/malformed type |
| 434 | return \false; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | switch ($state) { |
| 443 | case 3: |
| 444 | // in array, expecting value or another array |
| 445 | if ($type == 'a') { |
| 446 | if (\count($stack) >= \MAX_SERIALIZED_ARRAY_DEPTH) { |
| 447 | // array nesting exceeds MAX_SERIALIZED_ARRAY_DEPTH |
| 448 | return \false; |
| 449 | } |
| 450 | $stack[] =& $list; |
| 451 | $list[$key] = array(); |
| 452 | $list =& $list[$key]; |
| 453 | $expected[] = $expectedLength; |
| 454 | $state = 2; |
| 455 | break; |
| 456 | } |
| 457 | if ($type != '}') { |
| 458 | $list[$key] = $value; |
| 459 | $state = 2; |
| 460 | break; |
| 461 | } |
| 462 | // missing array value |
| 463 | return \false; |
| 464 | case 2: |
| 465 | // in array, expecting end of array or a key |
| 466 | if ($type == '}') { |
| 467 | if (\count($list) < \end($expected)) { |
| 468 | // array size less than expected |
| 469 | return \false; |
| 470 | } |
| 471 | unset($list); |
| 472 | $list =& $stack[\count($stack) - 1]; |
| 473 | \array_pop($stack); |
| 474 | // go to terminal state if we're at the end of the root array |
| 475 | \array_pop($expected); |
| 476 | if (\count($expected) == 0) { |
| 477 | $state = 1; |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | if ($type == 'i' || $type == 's') { |
| 482 | if (\count($list) >= \MAX_SERIALIZED_ARRAY_LENGTH) { |
| 483 | // array size exceeds MAX_SERIALIZED_ARRAY_LENGTH |
| 484 | return \false; |
| 485 | } |
| 486 | if (\count($list) >= \end($expected)) { |
| 487 | // array size exceeds expected length |
| 488 | return \false; |
| 489 | } |
| 490 | $key = $value; |
| 491 | $state = 3; |
| 492 | break; |
| 493 | } |
| 494 | // illegal array index type |
| 495 | return \false; |
| 496 | case 0: |
| 497 | // expecting array or value |
| 498 | if ($type == 'a') { |
| 499 | if (\count($stack) >= \MAX_SERIALIZED_ARRAY_DEPTH) { |
| 500 | // array nesting exceeds MAX_SERIALIZED_ARRAY_DEPTH |
| 501 | return \false; |
| 502 | } |
| 503 | $data = array(); |
| 504 | $list =& $data; |
| 505 | $expected[] = $expectedLength; |
| 506 | $state = 2; |
| 507 | break; |
| 508 | } |
| 509 | if ($type != '}') { |
| 510 | $data = $value; |
| 511 | $state = 1; |
| 512 | break; |
| 513 | } |
| 514 | // not in array |
| 515 | return \false; |
| 516 | } |
| 517 | } |
| 518 | if (!empty($str)) { |
| 519 | // trailing data in input |
| 520 | return \false; |
| 521 | } |
| 522 | return $data; |
| 523 | } |
| 524 | /** |
| 525 | * Wrapper for _safe_unserialize() that handles exceptions and multibyte encoding issue |
| 526 | * |
| 527 | * @param string $str |
| 528 | * @return mixed |
| 529 | */ |
| 530 | function safe_unserialize($str) |
| 531 | { |
| 532 | // ensure we use the byte count for strings even when strlen() is overloaded by mb_strlen() |
| 533 | if (\function_exists('mb_internal_encoding') && (int) \ini_get('mbstring.func_overload') & 2) { |
| 534 | $mbIntEnc = \mb_internal_encoding(); |
| 535 | \mb_internal_encoding('ASCII'); |
| 536 | } |
| 537 | $out = \_safe_unserialize($str); |
| 538 | if (isset($mbIntEnc)) { |
| 539 | \mb_internal_encoding($mbIntEnc); |
| 540 | } |
| 541 | return $out; |
| 542 | } |
| 543 | /** |
| 544 | * readfile() replacement. |
| 545 | * Behaves similar to readfile($filename); |
| 546 | * |
| 547 | * @author anthon (dot) pang (at) gmail (dot) com |
| 548 | * |
| 549 | * @param string $filename |
| 550 | * @param bool $useIncludePath |
| 551 | * @param resource $context |
| 552 | * @return int the number of bytes read from the file, or false if an error occurs |
| 553 | */ |
| 554 | function _readfile($filename, $byteStart, $byteEnd, $useIncludePath = \false, $context = null) |
| 555 | { |
| 556 | $count = @\filesize($filename); |
| 557 | // built-in function has a 2 MB limit when using mmap |
| 558 | if (\function_exists('readfile') && $count <= 2 * 1024 * 1024 && $byteStart == 0 && $byteEnd == $count) { |
| 559 | return @\readfile($filename, $useIncludePath, $context); |
| 560 | } |
| 561 | // when in doubt (or when readfile() function is disabled) |
| 562 | $handle = @\fopen($filename, SettingsServer::isWindows() ? "rb" : "r"); |
| 563 | if ($handle) { |
| 564 | \fseek($handle, $byteStart); |
| 565 | for ($pos = $byteStart; $pos < $byteEnd && !\feof($handle); $pos = \ftell($handle)) { |
| 566 | echo \fread($handle, \min(8192, $byteEnd - $pos)); |
| 567 | @\ob_flush(); |
| 568 | @\flush(); |
| 569 | } |
| 570 | \fclose($handle); |
| 571 | return $byteEnd - $byteStart; |
| 572 | } |
| 573 | return \false; |
| 574 | } |
| 575 | /** |
| 576 | * utf8_encode replacement |
| 577 | * |
| 578 | * @param string $data |
| 579 | * @return string |
| 580 | */ |
| 581 | if (!\function_exists('utf8_encode')) { |
| 582 | function utf8_encode($data) |
| 583 | { |
| 584 | if (\function_exists('iconv')) { |
| 585 | return @\iconv('ISO-8859-1', 'UTF-8', $data); |
| 586 | } |
| 587 | return $data; |
| 588 | } |
| 589 | } |
| 590 | /** |
| 591 | * utf8_decode replacement |
| 592 | * |
| 593 | * @param string $data |
| 594 | * @return string |
| 595 | */ |
| 596 | if (!\function_exists('utf8_decode')) { |
| 597 | function utf8_decode($data) |
| 598 | { |
| 599 | if (\function_exists('iconv')) { |
| 600 | return @\iconv('UTF-8', 'ISO-8859-1', $data); |
| 601 | } |
| 602 | return $data; |
| 603 | } |
| 604 | } |
| 605 | /** |
| 606 | * On ubuntu in some cases, there is a bug that gzopen does not exist and one must use gzopen64 instead |
| 607 | */ |
| 608 | if (!\function_exists('gzopen') && \function_exists('gzopen64')) { |
| 609 | function gzopen($filename, $mode = 'r', $use_include_path = 0) |
| 610 | { |
| 611 | return \gzopen64($filename, $mode, $use_include_path); |
| 612 | } |
| 613 | } |
| 614 | /** |
| 615 | * Need to catch that PHP7 error object on php5 |
| 616 | */ |
| 617 | if (!\class_exists('\\Error')) { |
| 618 | class Error |
| 619 | { |
| 620 | } |
| 621 | } |
| 622 | if (!\function_exists('fnmatch')) { |
| 623 | function fnmatch($pattern, $string) |
| 624 | { |
| 625 | return \preg_match("#^" . \strtr(\preg_quote($pattern, '#'), array('\\*' => '.*', '\\?' => '.')) . "\$#i", $string); |
| 626 | } |
| 627 | // end |
| 628 | } |
| 629 | // end if |
| 630 | } |
| 631 |