[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 ///////////////////////////////////////////////////////////////// 4 /// getID3() by James Heinrich <info@getid3.org> // 5 // available at https://github.com/JamesHeinrich/getID3 // 6 // or https://www.getid3.org // 7 // or http://getid3.sourceforge.net // 8 // // 9 // getid3.lib.php - part of getID3() // 10 // see readme.txt for more details // 11 // /// 12 ///////////////////////////////////////////////////////////////// 13 14 15 class getid3_lib 16 { 17 /** 18 * @param string $string 19 * @param bool $hex 20 * @param bool $spaces 21 * @param string|bool $htmlencoding 22 * 23 * @return string 24 */ 25 public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') { 26 $returnstring = ''; 27 for ($i = 0; $i < strlen($string); $i++) { 28 if ($hex) { 29 $returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT); 30 } else { 31 $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : 'ยค'); 32 } 33 if ($spaces) { 34 $returnstring .= ' '; 35 } 36 } 37 if (!empty($htmlencoding)) { 38 if ($htmlencoding === true) { 39 $htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean 40 } 41 $returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding); 42 } 43 return $returnstring; 44 } 45 46 /** 47 * Truncates a floating-point number at the decimal point. 48 * 49 * @param float $floatnumber 50 * 51 * @return float|int returns int (if possible, otherwise float) 52 */ 53 public static function trunc($floatnumber) { 54 if ($floatnumber >= 1) { 55 $truncatednumber = floor($floatnumber); 56 } elseif ($floatnumber <= -1) { 57 $truncatednumber = ceil($floatnumber); 58 } else { 59 $truncatednumber = 0; 60 } 61 if (self::intValueSupported($truncatednumber)) { 62 $truncatednumber = (int) $truncatednumber; 63 } 64 return $truncatednumber; 65 } 66 67 /** 68 * @param int|null $variable 69 * @param int $increment 70 * 71 * @return bool 72 */ 73 public static function safe_inc(&$variable, $increment=1) { 74 if (isset($variable)) { 75 $variable += $increment; 76 } else { 77 $variable = $increment; 78 } 79 return true; 80 } 81 82 /** 83 * @param int|float $floatnum 84 * 85 * @return int|float 86 */ 87 public static function CastAsInt($floatnum) { 88 // convert to float if not already 89 $floatnum = (float) $floatnum; 90 91 // convert a float to type int, only if possible 92 if (self::trunc($floatnum) == $floatnum) { 93 // it's not floating point 94 if (self::intValueSupported($floatnum)) { 95 // it's within int range 96 $floatnum = (int) $floatnum; 97 } 98 } 99 return $floatnum; 100 } 101 102 /** 103 * @param int $num 104 * 105 * @return bool 106 */ 107 public static function intValueSupported($num) { 108 // check if integers are 64-bit 109 static $hasINT64 = null; 110 if ($hasINT64 === null) { // 10x faster than is_null() 111 $hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1 112 if (!$hasINT64 && !defined('PHP_INT_MIN')) { 113 define('PHP_INT_MIN', ~PHP_INT_MAX); 114 } 115 } 116 // if integers are 64-bit - no other check required 117 if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) { // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound 118 return true; 119 } 120 return false; 121 } 122 123 /** 124 * @param string $fraction 125 * 126 * @return float 127 */ 128 public static function DecimalizeFraction($fraction) { 129 list($numerator, $denominator) = explode('/', $fraction); 130 return $numerator / ($denominator ? $denominator : 1); 131 } 132 133 /** 134 * @param string $binarynumerator 135 * 136 * @return float 137 */ 138 public static function DecimalBinary2Float($binarynumerator) { 139 $numerator = self::Bin2Dec($binarynumerator); 140 $denominator = self::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator))); 141 return ($numerator / $denominator); 142 } 143 144 /** 145 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html 146 * 147 * @param string $binarypointnumber 148 * @param int $maxbits 149 * 150 * @return array 151 */ 152 public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) { 153 if (strpos($binarypointnumber, '.') === false) { 154 $binarypointnumber = '0.'.$binarypointnumber; 155 } elseif ($binarypointnumber[0] == '.') { 156 $binarypointnumber = '0'.$binarypointnumber; 157 } 158 $exponent = 0; 159 while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) { 160 if (substr($binarypointnumber, 1, 1) == '.') { 161 $exponent--; 162 $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3); 163 } else { 164 $pointpos = strpos($binarypointnumber, '.'); 165 $exponent += ($pointpos - 1); 166 $binarypointnumber = str_replace('.', '', $binarypointnumber); 167 $binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1); 168 } 169 } 170 $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT); 171 return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent); 172 } 173 174 /** 175 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html 176 * 177 * @param float $floatvalue 178 * 179 * @return string 180 */ 181 public static function Float2BinaryDecimal($floatvalue) { 182 $maxbits = 128; // to how many bits of precision should the calculations be taken? 183 $intpart = self::trunc($floatvalue); 184 $floatpart = abs($floatvalue - $intpart); 185 $pointbitstring = ''; 186 while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) { 187 $floatpart *= 2; 188 $pointbitstring .= (string) self::trunc($floatpart); 189 $floatpart -= self::trunc($floatpart); 190 } 191 $binarypointnumber = decbin($intpart).'.'.$pointbitstring; 192 return $binarypointnumber; 193 } 194 195 /** 196 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html 197 * 198 * @param float $floatvalue 199 * @param int $bits 200 * 201 * @return string|false 202 */ 203 public static function Float2String($floatvalue, $bits) { 204 $exponentbits = 0; 205 $fractionbits = 0; 206 switch ($bits) { 207 case 32: 208 $exponentbits = 8; 209 $fractionbits = 23; 210 break; 211 212 case 64: 213 $exponentbits = 11; 214 $fractionbits = 52; 215 break; 216 217 default: 218 return false; 219 } 220 if ($floatvalue >= 0) { 221 $signbit = '0'; 222 } else { 223 $signbit = '1'; 224 } 225 $normalizedbinary = self::NormalizeBinaryPoint(self::Float2BinaryDecimal($floatvalue), $fractionbits); 226 $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent 227 $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT); 228 $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT); 229 230 return self::BigEndian2String(self::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false); 231 } 232 233 /** 234 * @param string $byteword 235 * 236 * @return float|false 237 */ 238 public static function LittleEndian2Float($byteword) { 239 return self::BigEndian2Float(strrev($byteword)); 240 } 241 242 /** 243 * ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic 244 * 245 * @link http://www.psc.edu/general/software/packages/ieee/ieee.html 246 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html 247 * 248 * @param string $byteword 249 * 250 * @return float|false 251 */ 252 public static function BigEndian2Float($byteword) { 253 $bitword = self::BigEndian2Bin($byteword); 254 if (!$bitword) { 255 return 0; 256 } 257 $signbit = $bitword[0]; 258 $floatvalue = 0; 259 $exponentbits = 0; 260 $fractionbits = 0; 261 262 switch (strlen($byteword) * 8) { 263 case 32: 264 $exponentbits = 8; 265 $fractionbits = 23; 266 break; 267 268 case 64: 269 $exponentbits = 11; 270 $fractionbits = 52; 271 break; 272 273 case 80: 274 // 80-bit Apple SANE format 275 // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/ 276 $exponentstring = substr($bitword, 1, 15); 277 $isnormalized = intval($bitword[16]); 278 $fractionstring = substr($bitword, 17, 63); 279 $exponent = pow(2, self::Bin2Dec($exponentstring) - 16383); 280 $fraction = $isnormalized + self::DecimalBinary2Float($fractionstring); 281 $floatvalue = $exponent * $fraction; 282 if ($signbit == '1') { 283 $floatvalue *= -1; 284 } 285 return $floatvalue; 286 287 default: 288 return false; 289 } 290 $exponentstring = substr($bitword, 1, $exponentbits); 291 $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits); 292 $exponent = self::Bin2Dec($exponentstring); 293 $fraction = self::Bin2Dec($fractionstring); 294 295 if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) { 296 // Not a Number 297 $floatvalue = false; 298 } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) { 299 if ($signbit == '1') { 300 $floatvalue = '-infinity'; 301 } else { 302 $floatvalue = '+infinity'; 303 } 304 } elseif (($exponent == 0) && ($fraction == 0)) { 305 if ($signbit == '1') { 306 $floatvalue = -0; 307 } else { 308 $floatvalue = 0; 309 } 310 $floatvalue = ($signbit ? 0 : -0); 311 } elseif (($exponent == 0) && ($fraction != 0)) { 312 // These are 'unnormalized' values 313 $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * self::DecimalBinary2Float($fractionstring); 314 if ($signbit == '1') { 315 $floatvalue *= -1; 316 } 317 } elseif ($exponent != 0) { 318 $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self::DecimalBinary2Float($fractionstring)); 319 if ($signbit == '1') { 320 $floatvalue *= -1; 321 } 322 } 323 return (float) $floatvalue; 324 } 325 326 /** 327 * @param string $byteword 328 * @param bool $synchsafe 329 * @param bool $signed 330 * 331 * @return int|float|false 332 * @throws Exception 333 */ 334 public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) { 335 $intvalue = 0; 336 $bytewordlen = strlen($byteword); 337 if ($bytewordlen == 0) { 338 return false; 339 } 340 for ($i = 0; $i < $bytewordlen; $i++) { 341 if ($synchsafe) { // disregard MSB, effectively 7-bit bytes 342 //$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems 343 $intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7); 344 } else { 345 $intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i)); 346 } 347 } 348 if ($signed && !$synchsafe) { 349 // synchsafe ints are not allowed to be signed 350 if ($bytewordlen <= PHP_INT_SIZE) { 351 $signMaskBit = 0x80 << (8 * ($bytewordlen - 1)); 352 if ($intvalue & $signMaskBit) { 353 $intvalue = 0 - ($intvalue & ($signMaskBit - 1)); 354 } 355 } else { 356 throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in self::BigEndian2Int()'); 357 } 358 } 359 return self::CastAsInt($intvalue); 360 } 361 362 /** 363 * @param string $byteword 364 * @param bool $signed 365 * 366 * @return int|float|false 367 */ 368 public static function LittleEndian2Int($byteword, $signed=false) { 369 return self::BigEndian2Int(strrev($byteword), false, $signed); 370 } 371 372 /** 373 * @param string $byteword 374 * 375 * @return string 376 */ 377 public static function LittleEndian2Bin($byteword) { 378 return self::BigEndian2Bin(strrev($byteword)); 379 } 380 381 /** 382 * @param string $byteword 383 * 384 * @return string 385 */ 386 public static function BigEndian2Bin($byteword) { 387 $binvalue = ''; 388 $bytewordlen = strlen($byteword); 389 for ($i = 0; $i < $bytewordlen; $i++) { 390 $binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT); 391 } 392 return $binvalue; 393 } 394 395 /** 396 * @param int $number 397 * @param int $minbytes 398 * @param bool $synchsafe 399 * @param bool $signed 400 * 401 * @return string 402 * @throws Exception 403 */ 404 public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) { 405 if ($number < 0) { 406 throw new Exception('ERROR: self::BigEndian2String() does not support negative numbers'); 407 } 408 $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF); 409 $intstring = ''; 410 if ($signed) { 411 if ($minbytes > PHP_INT_SIZE) { 412 throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in self::BigEndian2String()'); 413 } 414 $number = $number & (0x80 << (8 * ($minbytes - 1))); 415 } 416 while ($number != 0) { 417 $quotient = ($number / ($maskbyte + 1)); 418 $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring; 419 $number = floor($quotient); 420 } 421 return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT); 422 } 423 424 /** 425 * @param int $number 426 * 427 * @return string 428 */ 429 public static function Dec2Bin($number) { 430 while ($number >= 256) { 431 $bytes[] = (($number / 256) - (floor($number / 256))) * 256; 432 $number = floor($number / 256); 433 } 434 $bytes[] = $number; 435 $binstring = ''; 436 for ($i = 0; $i < count($bytes); $i++) { 437 $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring; 438 } 439 return $binstring; 440 } 441 442 /** 443 * @param string $binstring 444 * @param bool $signed 445 * 446 * @return int|float 447 */ 448 public static function Bin2Dec($binstring, $signed=false) { 449 $signmult = 1; 450 if ($signed) { 451 if ($binstring[0] == '1') { 452 $signmult = -1; 453 } 454 $binstring = substr($binstring, 1); 455 } 456 $decvalue = 0; 457 for ($i = 0; $i < strlen($binstring); $i++) { 458 $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i); 459 } 460 return self::CastAsInt($decvalue * $signmult); 461 } 462 463 /** 464 * @param string $binstring 465 * 466 * @return string 467 */ 468 public static function Bin2String($binstring) { 469 // return 'hi' for input of '0110100001101001' 470 $string = ''; 471 $binstringreversed = strrev($binstring); 472 for ($i = 0; $i < strlen($binstringreversed); $i += 8) { 473 $string = chr(self::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string; 474 } 475 return $string; 476 } 477 478 /** 479 * @param int $number 480 * @param int $minbytes 481 * @param bool $synchsafe 482 * 483 * @return string 484 */ 485 public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) { 486 $intstring = ''; 487 while ($number > 0) { 488 if ($synchsafe) { 489 $intstring = $intstring.chr($number & 127); 490 $number >>= 7; 491 } else { 492 $intstring = $intstring.chr($number & 255); 493 $number >>= 8; 494 } 495 } 496 return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); 497 } 498 499 /** 500 * @param mixed $array1 501 * @param mixed $array2 502 * 503 * @return array|false 504 */ 505 public static function array_merge_clobber($array1, $array2) { 506 // written by kcรhireability*com 507 // taken from http://www.php.net/manual/en/function.array-merge-recursive.php 508 if (!is_array($array1) || !is_array($array2)) { 509 return false; 510 } 511 $newarray = $array1; 512 foreach ($array2 as $key => $val) { 513 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) { 514 $newarray[$key] = self::array_merge_clobber($newarray[$key], $val); 515 } else { 516 $newarray[$key] = $val; 517 } 518 } 519 return $newarray; 520 } 521 522 /** 523 * @param mixed $array1 524 * @param mixed $array2 525 * 526 * @return array|false 527 */ 528 public static function array_merge_noclobber($array1, $array2) { 529 if (!is_array($array1) || !is_array($array2)) { 530 return false; 531 } 532 $newarray = $array1; 533 foreach ($array2 as $key => $val) { 534 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) { 535 $newarray[$key] = self::array_merge_noclobber($newarray[$key], $val); 536 } elseif (!isset($newarray[$key])) { 537 $newarray[$key] = $val; 538 } 539 } 540 return $newarray; 541 } 542 543 /** 544 * @param mixed $array1 545 * @param mixed $array2 546 * 547 * @return array|false|null 548 */ 549 public static function flipped_array_merge_noclobber($array1, $array2) { 550 if (!is_array($array1) || !is_array($array2)) { 551 return false; 552 } 553 # naturally, this only works non-recursively 554 $newarray = array_flip($array1); 555 foreach (array_flip($array2) as $key => $val) { 556 if (!isset($newarray[$key])) { 557 $newarray[$key] = count($newarray); 558 } 559 } 560 return array_flip($newarray); 561 } 562 563 /** 564 * @param array $theArray 565 * 566 * @return bool 567 */ 568 public static function ksort_recursive(&$theArray) { 569 ksort($theArray); 570 foreach ($theArray as $key => $value) { 571 if (is_array($value)) { 572 self::ksort_recursive($theArray[$key]); 573 } 574 } 575 return true; 576 } 577 578 /** 579 * @param string $filename 580 * @param int $numextensions 581 * 582 * @return string 583 */ 584 public static function fileextension($filename, $numextensions=1) { 585 if (strstr($filename, '.')) { 586 $reversedfilename = strrev($filename); 587 $offset = 0; 588 for ($i = 0; $i < $numextensions; $i++) { 589 $offset = strpos($reversedfilename, '.', $offset + 1); 590 if ($offset === false) { 591 return ''; 592 } 593 } 594 return strrev(substr($reversedfilename, 0, $offset)); 595 } 596 return ''; 597 } 598 599 /** 600 * @param int $seconds 601 * 602 * @return string 603 */ 604 public static function PlaytimeString($seconds) { 605 $sign = (($seconds < 0) ? '-' : ''); 606 $seconds = round(abs($seconds)); 607 $H = (int) floor( $seconds / 3600); 608 $M = (int) floor(($seconds - (3600 * $H) ) / 60); 609 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) ); 610 return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT); 611 } 612 613 /** 614 * @param int $macdate 615 * 616 * @return int|float 617 */ 618 public static function DateMac2Unix($macdate) { 619 // Macintosh timestamp: seconds since 00:00h January 1, 1904 620 // UNIX timestamp: seconds since 00:00h January 1, 1970 621 return self::CastAsInt($macdate - 2082844800); 622 } 623 624 /** 625 * @param string $rawdata 626 * 627 * @return float 628 */ 629 public static function FixedPoint8_8($rawdata) { 630 return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8)); 631 } 632 633 /** 634 * @param string $rawdata 635 * 636 * @return float 637 */ 638 public static function FixedPoint16_16($rawdata) { 639 return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16)); 640 } 641 642 /** 643 * @param string $rawdata 644 * 645 * @return float 646 */ 647 public static function FixedPoint2_30($rawdata) { 648 $binarystring = self::BigEndian2Bin($rawdata); 649 return self::Bin2Dec(substr($binarystring, 0, 2)) + (float) (self::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30)); 650 } 651 652 653 /** 654 * @param string $ArrayPath 655 * @param string $Separator 656 * @param mixed $Value 657 * 658 * @return array 659 */ 660 public static function CreateDeepArray($ArrayPath, $Separator, $Value) { 661 // assigns $Value to a nested array path: 662 // $foo = self::CreateDeepArray('/path/to/my', '/', 'file.txt') 663 // is the same as: 664 // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt')))); 665 // or 666 // $foo['path']['to']['my'] = 'file.txt'; 667 $ArrayPath = ltrim($ArrayPath, $Separator); 668 if (($pos = strpos($ArrayPath, $Separator)) !== false) { 669 $ReturnedArray[substr($ArrayPath, 0, $pos)] = self::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value); 670 } else { 671 $ReturnedArray[$ArrayPath] = $Value; 672 } 673 return $ReturnedArray; 674 } 675 676 /** 677 * @param array $arraydata 678 * @param bool $returnkey 679 * 680 * @return int|false 681 */ 682 public static function array_max($arraydata, $returnkey=false) { 683 $maxvalue = false; 684 $maxkey = false; 685 foreach ($arraydata as $key => $value) { 686 if (!is_array($value)) { 687 if (($maxvalue === false) || ($value > $maxvalue)) { 688 $maxvalue = $value; 689 $maxkey = $key; 690 } 691 } 692 } 693 return ($returnkey ? $maxkey : $maxvalue); 694 } 695 696 /** 697 * @param array $arraydata 698 * @param bool $returnkey 699 * 700 * @return int|false 701 */ 702 public static function array_min($arraydata, $returnkey=false) { 703 $minvalue = false; 704 $minkey = false; 705 foreach ($arraydata as $key => $value) { 706 if (!is_array($value)) { 707 if (($minvalue === false) || ($value < $minvalue)) { 708 $minvalue = $value; 709 $minkey = $key; 710 } 711 } 712 } 713 return ($returnkey ? $minkey : $minvalue); 714 } 715 716 /** 717 * @param string $XMLstring 718 * 719 * @return array|false 720 */ 721 public static function XML2array($XMLstring) { 722 if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) { 723 if (PHP_VERSION_ID < 80000) { 724 // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html 725 // https://core.trac.wordpress.org/changeset/29378 726 // This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading is 727 // disabled by default, so this function is no longer needed to protect against XXE attacks. 728 $loader = libxml_disable_entity_loader(true); 729 } 730 $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT); 731 $return = self::SimpleXMLelement2array($XMLobject); 732 if (PHP_VERSION_ID < 80000 && isset($loader)) { 733 libxml_disable_entity_loader($loader); 734 } 735 return $return; 736 } 737 return false; 738 } 739 740 /** 741 * @param SimpleXMLElement|array|mixed $XMLobject 742 * 743 * @return mixed 744 */ 745 public static function SimpleXMLelement2array($XMLobject) { 746 if (!is_object($XMLobject) && !is_array($XMLobject)) { 747 return $XMLobject; 748 } 749 $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject; 750 foreach ($XMLarray as $key => $value) { 751 $XMLarray[$key] = self::SimpleXMLelement2array($value); 752 } 753 return $XMLarray; 754 } 755 756 /** 757 * Returns checksum for a file from starting position to absolute end position. 758 * 759 * @param string $file 760 * @param int $offset 761 * @param int $end 762 * @param string $algorithm 763 * 764 * @return string|false 765 * @throws getid3_exception 766 */ 767 public static function hash_data($file, $offset, $end, $algorithm) { 768 if (!self::intValueSupported($end)) { 769 return false; 770 } 771 if (!in_array($algorithm, array('md5', 'sha1'))) { 772 throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()'); 773 } 774 775 $size = $end - $offset; 776 777 $fp = fopen($file, 'rb'); 778 fseek($fp, $offset); 779 $ctx = hash_init($algorithm); 780 while ($size > 0) { 781 $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE)); 782 hash_update($ctx, $buffer); 783 $size -= getID3::FREAD_BUFFER_SIZE; 784 } 785 $hash = hash_final($ctx); 786 fclose($fp); 787 788 return $hash; 789 } 790 791 /** 792 * @param string $filename_source 793 * @param string $filename_dest 794 * @param int $offset 795 * @param int $length 796 * 797 * @return bool 798 * @throws Exception 799 * 800 * @deprecated Unused, may be removed in future versions of getID3 801 */ 802 public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) { 803 if (!self::intValueSupported($offset + $length)) { 804 throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit'); 805 } 806 if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) { 807 if (($fp_dest = fopen($filename_dest, 'wb'))) { 808 if (fseek($fp_src, $offset) == 0) { 809 $byteslefttowrite = $length; 810 while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) { 811 $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite); 812 $byteslefttowrite -= $byteswritten; 813 } 814 fclose($fp_dest); 815 return true; 816 } else { 817 fclose($fp_src); 818 throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source); 819 } 820 } else { 821 throw new Exception('failed to create file for writing '.$filename_dest); 822 } 823 } else { 824 throw new Exception('failed to open file for reading '.$filename_source); 825 } 826 } 827 828 /** 829 * @param int $charval 830 * 831 * @return string 832 */ 833 public static function iconv_fallback_int_utf8($charval) { 834 if ($charval < 128) { 835 // 0bbbbbbb 836 $newcharstring = chr($charval); 837 } elseif ($charval < 2048) { 838 // 110bbbbb 10bbbbbb 839 $newcharstring = chr(($charval >> 6) | 0xC0); 840 $newcharstring .= chr(($charval & 0x3F) | 0x80); 841 } elseif ($charval < 65536) { 842 // 1110bbbb 10bbbbbb 10bbbbbb 843 $newcharstring = chr(($charval >> 12) | 0xE0); 844 $newcharstring .= chr(($charval >> 6) | 0xC0); 845 $newcharstring .= chr(($charval & 0x3F) | 0x80); 846 } else { 847 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 848 $newcharstring = chr(($charval >> 18) | 0xF0); 849 $newcharstring .= chr(($charval >> 12) | 0xC0); 850 $newcharstring .= chr(($charval >> 6) | 0xC0); 851 $newcharstring .= chr(($charval & 0x3F) | 0x80); 852 } 853 return $newcharstring; 854 } 855 856 /** 857 * ISO-8859-1 => UTF-8 858 * 859 * @param string $string 860 * @param bool $bom 861 * 862 * @return string 863 */ 864 public static function iconv_fallback_iso88591_utf8($string, $bom=false) { 865 if (function_exists('utf8_encode')) { 866 return utf8_encode($string); 867 } 868 // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) 869 $newcharstring = ''; 870 if ($bom) { 871 $newcharstring .= "\xEF\xBB\xBF"; 872 } 873 for ($i = 0; $i < strlen($string); $i++) { 874 $charval = ord($string[$i]); 875 $newcharstring .= self::iconv_fallback_int_utf8($charval); 876 } 877 return $newcharstring; 878 } 879 880 /** 881 * ISO-8859-1 => UTF-16BE 882 * 883 * @param string $string 884 * @param bool $bom 885 * 886 * @return string 887 */ 888 public static function iconv_fallback_iso88591_utf16be($string, $bom=false) { 889 $newcharstring = ''; 890 if ($bom) { 891 $newcharstring .= "\xFE\xFF"; 892 } 893 for ($i = 0; $i < strlen($string); $i++) { 894 $newcharstring .= "\x00".$string[$i]; 895 } 896 return $newcharstring; 897 } 898 899 /** 900 * ISO-8859-1 => UTF-16LE 901 * 902 * @param string $string 903 * @param bool $bom 904 * 905 * @return string 906 */ 907 public static function iconv_fallback_iso88591_utf16le($string, $bom=false) { 908 $newcharstring = ''; 909 if ($bom) { 910 $newcharstring .= "\xFF\xFE"; 911 } 912 for ($i = 0; $i < strlen($string); $i++) { 913 $newcharstring .= $string[$i]."\x00"; 914 } 915 return $newcharstring; 916 } 917 918 /** 919 * ISO-8859-1 => UTF-16LE (BOM) 920 * 921 * @param string $string 922 * 923 * @return string 924 */ 925 public static function iconv_fallback_iso88591_utf16($string) { 926 return self::iconv_fallback_iso88591_utf16le($string, true); 927 } 928 929 /** 930 * UTF-8 => ISO-8859-1 931 * 932 * @param string $string 933 * 934 * @return string 935 */ 936 public static function iconv_fallback_utf8_iso88591($string) { 937 if (function_exists('utf8_decode')) { 938 return utf8_decode($string); 939 } 940 // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) 941 $newcharstring = ''; 942 $offset = 0; 943 $stringlength = strlen($string); 944 while ($offset < $stringlength) { 945 if ((ord($string[$offset]) | 0x07) == 0xF7) { 946 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 947 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 948 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 949 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 950 (ord($string[($offset + 3)]) & 0x3F); 951 $offset += 4; 952 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 953 // 1110bbbb 10bbbbbb 10bbbbbb 954 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 955 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 956 (ord($string[($offset + 2)]) & 0x3F); 957 $offset += 3; 958 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 959 // 110bbbbb 10bbbbbb 960 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 961 (ord($string[($offset + 1)]) & 0x3F); 962 $offset += 2; 963 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 964 // 0bbbbbbb 965 $charval = ord($string[$offset]); 966 $offset += 1; 967 } else { 968 // error? throw some kind of warning here? 969 $charval = false; 970 $offset += 1; 971 } 972 if ($charval !== false) { 973 $newcharstring .= (($charval < 256) ? chr($charval) : '?'); 974 } 975 } 976 return $newcharstring; 977 } 978 979 /** 980 * UTF-8 => UTF-16BE 981 * 982 * @param string $string 983 * @param bool $bom 984 * 985 * @return string 986 */ 987 public static function iconv_fallback_utf8_utf16be($string, $bom=false) { 988 $newcharstring = ''; 989 if ($bom) { 990 $newcharstring .= "\xFE\xFF"; 991 } 992 $offset = 0; 993 $stringlength = strlen($string); 994 while ($offset < $stringlength) { 995 if ((ord($string[$offset]) | 0x07) == 0xF7) { 996 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 997 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 998 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 999 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 1000 (ord($string[($offset + 3)]) & 0x3F); 1001 $offset += 4; 1002 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 1003 // 1110bbbb 10bbbbbb 10bbbbbb 1004 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 1005 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 1006 (ord($string[($offset + 2)]) & 0x3F); 1007 $offset += 3; 1008 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 1009 // 110bbbbb 10bbbbbb 1010 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 1011 (ord($string[($offset + 1)]) & 0x3F); 1012 $offset += 2; 1013 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 1014 // 0bbbbbbb 1015 $charval = ord($string[$offset]); 1016 $offset += 1; 1017 } else { 1018 // error? throw some kind of warning here? 1019 $charval = false; 1020 $offset += 1; 1021 } 1022 if ($charval !== false) { 1023 $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?'); 1024 } 1025 } 1026 return $newcharstring; 1027 } 1028 1029 /** 1030 * UTF-8 => UTF-16LE 1031 * 1032 * @param string $string 1033 * @param bool $bom 1034 * 1035 * @return string 1036 */ 1037 public static function iconv_fallback_utf8_utf16le($string, $bom=false) { 1038 $newcharstring = ''; 1039 if ($bom) { 1040 $newcharstring .= "\xFF\xFE"; 1041 } 1042 $offset = 0; 1043 $stringlength = strlen($string); 1044 while ($offset < $stringlength) { 1045 if ((ord($string[$offset]) | 0x07) == 0xF7) { 1046 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 1047 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 1048 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 1049 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 1050 (ord($string[($offset + 3)]) & 0x3F); 1051 $offset += 4; 1052 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 1053 // 1110bbbb 10bbbbbb 10bbbbbb 1054 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 1055 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 1056 (ord($string[($offset + 2)]) & 0x3F); 1057 $offset += 3; 1058 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 1059 // 110bbbbb 10bbbbbb 1060 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 1061 (ord($string[($offset + 1)]) & 0x3F); 1062 $offset += 2; 1063 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 1064 // 0bbbbbbb 1065 $charval = ord($string[$offset]); 1066 $offset += 1; 1067 } else { 1068 // error? maybe throw some warning here? 1069 $charval = false; 1070 $offset += 1; 1071 } 1072 if ($charval !== false) { 1073 $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00"); 1074 } 1075 } 1076 return $newcharstring; 1077 } 1078 1079 /** 1080 * UTF-8 => UTF-16LE (BOM) 1081 * 1082 * @param string $string 1083 * 1084 * @return string 1085 */ 1086 public static function iconv_fallback_utf8_utf16($string) { 1087 return self::iconv_fallback_utf8_utf16le($string, true); 1088 } 1089 1090 /** 1091 * UTF-16BE => UTF-8 1092 * 1093 * @param string $string 1094 * 1095 * @return string 1096 */ 1097 public static function iconv_fallback_utf16be_utf8($string) { 1098 if (substr($string, 0, 2) == "\xFE\xFF") { 1099 // strip BOM 1100 $string = substr($string, 2); 1101 } 1102 $newcharstring = ''; 1103 for ($i = 0; $i < strlen($string); $i += 2) { 1104 $charval = self::BigEndian2Int(substr($string, $i, 2)); 1105 $newcharstring .= self::iconv_fallback_int_utf8($charval); 1106 } 1107 return $newcharstring; 1108 } 1109 1110 /** 1111 * UTF-16LE => UTF-8 1112 * 1113 * @param string $string 1114 * 1115 * @return string 1116 */ 1117 public static function iconv_fallback_utf16le_utf8($string) { 1118 if (substr($string, 0, 2) == "\xFF\xFE") { 1119 // strip BOM 1120 $string = substr($string, 2); 1121 } 1122 $newcharstring = ''; 1123 for ($i = 0; $i < strlen($string); $i += 2) { 1124 $charval = self::LittleEndian2Int(substr($string, $i, 2)); 1125 $newcharstring .= self::iconv_fallback_int_utf8($charval); 1126 } 1127 return $newcharstring; 1128 } 1129 1130 /** 1131 * UTF-16BE => ISO-8859-1 1132 * 1133 * @param string $string 1134 * 1135 * @return string 1136 */ 1137 public static function iconv_fallback_utf16be_iso88591($string) { 1138 if (substr($string, 0, 2) == "\xFE\xFF") { 1139 // strip BOM 1140 $string = substr($string, 2); 1141 } 1142 $newcharstring = ''; 1143 for ($i = 0; $i < strlen($string); $i += 2) { 1144 $charval = self::BigEndian2Int(substr($string, $i, 2)); 1145 $newcharstring .= (($charval < 256) ? chr($charval) : '?'); 1146 } 1147 return $newcharstring; 1148 } 1149 1150 /** 1151 * UTF-16LE => ISO-8859-1 1152 * 1153 * @param string $string 1154 * 1155 * @return string 1156 */ 1157 public static function iconv_fallback_utf16le_iso88591($string) { 1158 if (substr($string, 0, 2) == "\xFF\xFE") { 1159 // strip BOM 1160 $string = substr($string, 2); 1161 } 1162 $newcharstring = ''; 1163 for ($i = 0; $i < strlen($string); $i += 2) { 1164 $charval = self::LittleEndian2Int(substr($string, $i, 2)); 1165 $newcharstring .= (($charval < 256) ? chr($charval) : '?'); 1166 } 1167 return $newcharstring; 1168 } 1169 1170 /** 1171 * UTF-16 (BOM) => ISO-8859-1 1172 * 1173 * @param string $string 1174 * 1175 * @return string 1176 */ 1177 public static function iconv_fallback_utf16_iso88591($string) { 1178 $bom = substr($string, 0, 2); 1179 if ($bom == "\xFE\xFF") { 1180 return self::iconv_fallback_utf16be_iso88591(substr($string, 2)); 1181 } elseif ($bom == "\xFF\xFE") { 1182 return self::iconv_fallback_utf16le_iso88591(substr($string, 2)); 1183 } 1184 return $string; 1185 } 1186 1187 /** 1188 * UTF-16 (BOM) => UTF-8 1189 * 1190 * @param string $string 1191 * 1192 * @return string 1193 */ 1194 public static function iconv_fallback_utf16_utf8($string) { 1195 $bom = substr($string, 0, 2); 1196 if ($bom == "\xFE\xFF") { 1197 return self::iconv_fallback_utf16be_utf8(substr($string, 2)); 1198 } elseif ($bom == "\xFF\xFE") { 1199 return self::iconv_fallback_utf16le_utf8(substr($string, 2)); 1200 } 1201 return $string; 1202 } 1203 1204 /** 1205 * @param string $in_charset 1206 * @param string $out_charset 1207 * @param string $string 1208 * 1209 * @return string 1210 * @throws Exception 1211 */ 1212 public static function iconv_fallback($in_charset, $out_charset, $string) { 1213 1214 if ($in_charset == $out_charset) { 1215 return $string; 1216 } 1217 1218 // mb_convert_encoding() available 1219 if (function_exists('mb_convert_encoding')) { 1220 if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) { 1221 // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM 1222 $string = "\xFF\xFE".$string; 1223 } 1224 if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) { 1225 if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) { 1226 // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified 1227 return ''; 1228 } 1229 } 1230 if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) { 1231 switch ($out_charset) { 1232 case 'ISO-8859-1': 1233 $converted_string = rtrim($converted_string, "\x00"); 1234 break; 1235 } 1236 return $converted_string; 1237 } 1238 return $string; 1239 1240 // iconv() available 1241 } elseif (function_exists('iconv')) { 1242 if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) { 1243 switch ($out_charset) { 1244 case 'ISO-8859-1': 1245 $converted_string = rtrim($converted_string, "\x00"); 1246 break; 1247 } 1248 return $converted_string; 1249 } 1250 1251 // iconv() may sometimes fail with "illegal character in input string" error message 1252 // and return an empty string, but returning the unconverted string is more useful 1253 return $string; 1254 } 1255 1256 1257 // neither mb_convert_encoding or iconv() is available 1258 static $ConversionFunctionList = array(); 1259 if (empty($ConversionFunctionList)) { 1260 $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8'; 1261 $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16'; 1262 $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be'; 1263 $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le'; 1264 $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591'; 1265 $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16'; 1266 $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be'; 1267 $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le'; 1268 $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591'; 1269 $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8'; 1270 $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591'; 1271 $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8'; 1272 $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591'; 1273 $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8'; 1274 } 1275 if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) { 1276 $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)]; 1277 return self::$ConversionFunction($string); 1278 } 1279 throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset); 1280 } 1281 1282 /** 1283 * @param mixed $data 1284 * @param string $charset 1285 * 1286 * @return mixed 1287 */ 1288 public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') { 1289 if (is_string($data)) { 1290 return self::MultiByteCharString2HTML($data, $charset); 1291 } elseif (is_array($data)) { 1292 $return_data = array(); 1293 foreach ($data as $key => $value) { 1294 $return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset); 1295 } 1296 return $return_data; 1297 } 1298 // integer, float, objects, resources, etc 1299 return $data; 1300 } 1301 1302 /** 1303 * @param string|int|float $string 1304 * @param string $charset 1305 * 1306 * @return string 1307 */ 1308 public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') { 1309 $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string 1310 $HTMLstring = ''; 1311 1312 switch (strtolower($charset)) { 1313 case '1251': 1314 case '1252': 1315 case '866': 1316 case '932': 1317 case '936': 1318 case '950': 1319 case 'big5': 1320 case 'big5-hkscs': 1321 case 'cp1251': 1322 case 'cp1252': 1323 case 'cp866': 1324 case 'euc-jp': 1325 case 'eucjp': 1326 case 'gb2312': 1327 case 'ibm866': 1328 case 'iso-8859-1': 1329 case 'iso-8859-15': 1330 case 'iso8859-1': 1331 case 'iso8859-15': 1332 case 'koi8-r': 1333 case 'koi8-ru': 1334 case 'koi8r': 1335 case 'shift_jis': 1336 case 'sjis': 1337 case 'win-1251': 1338 case 'windows-1251': 1339 case 'windows-1252': 1340 $HTMLstring = htmlentities($string, ENT_COMPAT, $charset); 1341 break; 1342 1343 case 'utf-8': 1344 $strlen = strlen($string); 1345 for ($i = 0; $i < $strlen; $i++) { 1346 $char_ord_val = ord($string[$i]); 1347 $charval = 0; 1348 if ($char_ord_val < 0x80) { 1349 $charval = $char_ord_val; 1350 } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) { 1351 $charval = (($char_ord_val & 0x07) << 18); 1352 $charval += ((ord($string[++$i]) & 0x3F) << 12); 1353 $charval += ((ord($string[++$i]) & 0x3F) << 6); 1354 $charval += (ord($string[++$i]) & 0x3F); 1355 } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) { 1356 $charval = (($char_ord_val & 0x0F) << 12); 1357 $charval += ((ord($string[++$i]) & 0x3F) << 6); 1358 $charval += (ord($string[++$i]) & 0x3F); 1359 } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) { 1360 $charval = (($char_ord_val & 0x1F) << 6); 1361 $charval += (ord($string[++$i]) & 0x3F); 1362 } 1363 if (($charval >= 32) && ($charval <= 127)) { 1364 $HTMLstring .= htmlentities(chr($charval)); 1365 } else { 1366 $HTMLstring .= '&#'.$charval.';'; 1367 } 1368 } 1369 break; 1370 1371 case 'utf-16le': 1372 for ($i = 0; $i < strlen($string); $i += 2) { 1373 $charval = self::LittleEndian2Int(substr($string, $i, 2)); 1374 if (($charval >= 32) && ($charval <= 127)) { 1375 $HTMLstring .= chr($charval); 1376 } else { 1377 $HTMLstring .= '&#'.$charval.';'; 1378 } 1379 } 1380 break; 1381 1382 case 'utf-16be': 1383 for ($i = 0; $i < strlen($string); $i += 2) { 1384 $charval = self::BigEndian2Int(substr($string, $i, 2)); 1385 if (($charval >= 32) && ($charval <= 127)) { 1386 $HTMLstring .= chr($charval); 1387 } else { 1388 $HTMLstring .= '&#'.$charval.';'; 1389 } 1390 } 1391 break; 1392 1393 default: 1394 $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()'; 1395 break; 1396 } 1397 return $HTMLstring; 1398 } 1399 1400 /** 1401 * @param int $namecode 1402 * 1403 * @return string 1404 */ 1405 public static function RGADnameLookup($namecode) { 1406 static $RGADname = array(); 1407 if (empty($RGADname)) { 1408 $RGADname[0] = 'not set'; 1409 $RGADname[1] = 'Track Gain Adjustment'; 1410 $RGADname[2] = 'Album Gain Adjustment'; 1411 } 1412 1413 return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : ''); 1414 } 1415 1416 /** 1417 * @param int $originatorcode 1418 * 1419 * @return string 1420 */ 1421 public static function RGADoriginatorLookup($originatorcode) { 1422 static $RGADoriginator = array(); 1423 if (empty($RGADoriginator)) { 1424 $RGADoriginator[0] = 'unspecified'; 1425 $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer'; 1426 $RGADoriginator[2] = 'set by user'; 1427 $RGADoriginator[3] = 'determined automatically'; 1428 } 1429 1430 return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : ''); 1431 } 1432 1433 /** 1434 * @param int $rawadjustment 1435 * @param int $signbit 1436 * 1437 * @return float 1438 */ 1439 public static function RGADadjustmentLookup($rawadjustment, $signbit) { 1440 $adjustment = (float) $rawadjustment / 10; 1441 if ($signbit == 1) { 1442 $adjustment *= -1; 1443 } 1444 return $adjustment; 1445 } 1446 1447 /** 1448 * @param int $namecode 1449 * @param int $originatorcode 1450 * @param int $replaygain 1451 * 1452 * @return string 1453 */ 1454 public static function RGADgainString($namecode, $originatorcode, $replaygain) { 1455 if ($replaygain < 0) { 1456 $signbit = '1'; 1457 } else { 1458 $signbit = '0'; 1459 } 1460 $storedreplaygain = intval(round($replaygain * 10)); 1461 $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT); 1462 $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT); 1463 $gainstring .= $signbit; 1464 $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT); 1465 1466 return $gainstring; 1467 } 1468 1469 /** 1470 * @param float $amplitude 1471 * 1472 * @return float 1473 */ 1474 public static function RGADamplitude2dB($amplitude) { 1475 return 20 * log10($amplitude); 1476 } 1477 1478 /** 1479 * @param string $imgData 1480 * @param array $imageinfo 1481 * 1482 * @return array|false 1483 */ 1484 public static function GetDataImageSize($imgData, &$imageinfo=array()) { 1485 if (PHP_VERSION_ID >= 50400) { 1486 $GetDataImageSize = @getimagesizefromstring($imgData, $imageinfo); 1487 if ($GetDataImageSize === false || !isset($GetDataImageSize[0], $GetDataImageSize[1])) { 1488 return false; 1489 } 1490 $GetDataImageSize['height'] = $GetDataImageSize[0]; 1491 $GetDataImageSize['width'] = $GetDataImageSize[1]; 1492 return $GetDataImageSize; 1493 } 1494 static $tempdir = ''; 1495 if (empty($tempdir)) { 1496 if (function_exists('sys_get_temp_dir')) { 1497 $tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52 1498 } 1499 1500 // yes this is ugly, feel free to suggest a better way 1501 if (include_once(dirname(__FILE__).'/getid3.php')) { 1502 $getid3_temp = new getID3(); 1503 if ($getid3_temp_tempdir = $getid3_temp->tempdir) { 1504 $tempdir = $getid3_temp_tempdir; 1505 } 1506 unset($getid3_temp, $getid3_temp_tempdir); 1507 } 1508 } 1509 $GetDataImageSize = false; 1510 if ($tempfilename = tempnam($tempdir, 'gI3')) { 1511 if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) { 1512 fwrite($tmp, $imgData); 1513 fclose($tmp); 1514 $GetDataImageSize = @getimagesize($tempfilename, $imageinfo); 1515 if (($GetDataImageSize === false) || !isset($GetDataImageSize[0]) || !isset($GetDataImageSize[1])) { 1516 return false; 1517 } 1518 $GetDataImageSize['height'] = $GetDataImageSize[0]; 1519 $GetDataImageSize['width'] = $GetDataImageSize[1]; 1520 } 1521 unlink($tempfilename); 1522 } 1523 return $GetDataImageSize; 1524 } 1525 1526 /** 1527 * @param string $mime_type 1528 * 1529 * @return string 1530 */ 1531 public static function ImageExtFromMime($mime_type) { 1532 // temporary way, works OK for now, but should be reworked in the future 1533 return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type); 1534 } 1535 1536 /** 1537 * @param array $ThisFileInfo 1538 * @param bool $option_tags_html default true (just as in the main getID3 class) 1539 * 1540 * @return bool 1541 */ 1542 public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) { 1543 // Copy all entries from ['tags'] into common ['comments'] 1544 if (!empty($ThisFileInfo['tags'])) { 1545 if (isset($ThisFileInfo['tags']['id3v1'])) { 1546 // bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings 1547 $ID3v1 = $ThisFileInfo['tags']['id3v1']; 1548 unset($ThisFileInfo['tags']['id3v1']); 1549 $ThisFileInfo['tags']['id3v1'] = $ID3v1; 1550 unset($ID3v1); 1551 } 1552 foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) { 1553 foreach ($tagarray as $tagname => $tagdata) { 1554 foreach ($tagdata as $key => $value) { 1555 if (!empty($value)) { 1556 if (empty($ThisFileInfo['comments'][$tagname])) { 1557 1558 // fall through and append value 1559 1560 } elseif ($tagtype == 'id3v1') { 1561 1562 $newvaluelength = strlen(trim($value)); 1563 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { 1564 $oldvaluelength = strlen(trim($existingvalue)); 1565 if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) { 1566 // new value is identical but shorter-than (or equal-length to) one already in comments - skip 1567 break 2; 1568 } 1569 } 1570 if (function_exists('mb_convert_encoding')) { 1571 if (trim($value) == trim(substr(mb_convert_encoding($existingvalue, $ThisFileInfo['id3v1']['encoding'], $ThisFileInfo['encoding']), 0, 30))) { 1572 // value stored in ID3v1 appears to be probably the multibyte value transliterated (badly) into ISO-8859-1 in ID3v1. 1573 // As an example, Foobar2000 will do this if you tag a file with Chinese or Arabic or Cyrillic or something that doesn't fit into ISO-8859-1 the ID3v1 will consist of mostly "?" characters, one per multibyte unrepresentable character 1574 break 2; 1575 } 1576 } 1577 1578 } elseif (!is_array($value)) { 1579 1580 $newvaluelength = strlen(trim($value)); 1581 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { 1582 $oldvaluelength = strlen(trim($existingvalue)); 1583 if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) { 1584 $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value); 1585 break; 1586 } 1587 } 1588 1589 } 1590 if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) { 1591 $value = (is_string($value) ? trim($value) : $value); 1592 if (!is_int($key) && !ctype_digit($key)) { 1593 $ThisFileInfo['comments'][$tagname][$key] = $value; 1594 } else { 1595 if (!isset($ThisFileInfo['comments'][$tagname])) { 1596 $ThisFileInfo['comments'][$tagname] = array($value); 1597 } else { 1598 $ThisFileInfo['comments'][$tagname][] = $value; 1599 } 1600 } 1601 } 1602 } 1603 } 1604 } 1605 } 1606 1607 // attempt to standardize spelling of returned keys 1608 $StandardizeFieldNames = array( 1609 'tracknumber' => 'track_number', 1610 'track' => 'track_number', 1611 ); 1612 foreach ($StandardizeFieldNames as $badkey => $goodkey) { 1613 if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) { 1614 $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey]; 1615 unset($ThisFileInfo['comments'][$badkey]); 1616 } 1617 } 1618 1619 if ($option_tags_html) { 1620 // Copy ['comments'] to ['comments_html'] 1621 if (!empty($ThisFileInfo['comments'])) { 1622 foreach ($ThisFileInfo['comments'] as $field => $values) { 1623 if ($field == 'picture') { 1624 // pictures can take up a lot of space, and we don't need multiple copies of them 1625 // let there be a single copy in [comments][picture], and not elsewhere 1626 continue; 1627 } 1628 foreach ($values as $index => $value) { 1629 if (is_array($value)) { 1630 $ThisFileInfo['comments_html'][$field][$index] = $value; 1631 } else { 1632 $ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding'])); 1633 } 1634 } 1635 } 1636 } 1637 } 1638 1639 } 1640 return true; 1641 } 1642 1643 /** 1644 * @param string $key 1645 * @param int $begin 1646 * @param int $end 1647 * @param string $file 1648 * @param string $name 1649 * 1650 * @return string 1651 */ 1652 public static function EmbeddedLookup($key, $begin, $end, $file, $name) { 1653 1654 // Cached 1655 static $cache; 1656 if (isset($cache[$file][$name])) { 1657 return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); 1658 } 1659 1660 // Init 1661 $keylength = strlen($key); 1662 $line_count = $end - $begin - 7; 1663 1664 // Open php file 1665 $fp = fopen($file, 'r'); 1666 1667 // Discard $begin lines 1668 for ($i = 0; $i < ($begin + 3); $i++) { 1669 fgets($fp, 1024); 1670 } 1671 1672 // Loop thru line 1673 while (0 < $line_count--) { 1674 1675 // Read line 1676 $line = ltrim(fgets($fp, 1024), "\t "); 1677 1678 // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key 1679 //$keycheck = substr($line, 0, $keylength); 1680 //if ($key == $keycheck) { 1681 // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1); 1682 // break; 1683 //} 1684 1685 // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key 1686 //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1)); 1687 $explodedLine = explode("\t", $line, 2); 1688 $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : ''); 1689 $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : ''); 1690 $cache[$file][$name][$ThisKey] = trim($ThisValue); 1691 } 1692 1693 // Close and return 1694 fclose($fp); 1695 return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); 1696 } 1697 1698 /** 1699 * @param string $filename 1700 * @param string $sourcefile 1701 * @param bool $DieOnFailure 1702 * 1703 * @return bool 1704 * @throws Exception 1705 */ 1706 public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) { 1707 global $GETID3_ERRORARRAY; 1708 1709 if (file_exists($filename)) { 1710 if (include_once($filename)) { 1711 return true; 1712 } else { 1713 $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors'; 1714 } 1715 } else { 1716 $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing'; 1717 } 1718 if ($DieOnFailure) { 1719 throw new Exception($diemessage); 1720 } else { 1721 $GETID3_ERRORARRAY[] = $diemessage; 1722 } 1723 return false; 1724 } 1725 1726 /** 1727 * @param string $string 1728 * 1729 * @return string 1730 */ 1731 public static function trimNullByte($string) { 1732 return trim($string, "\x00"); 1733 } 1734 1735 /** 1736 * @param string $path 1737 * 1738 * @return float|bool 1739 */ 1740 public static function getFileSizeSyscall($path) { 1741 $filesize = false; 1742 1743 if (GETID3_OS_ISWINDOWS) { 1744 if (class_exists('COM')) { // From PHP 5.3.15 and 5.4.5, COM and DOTNET is no longer built into the php core.you have to add COM support in php.ini: 1745 $filesystem = new COM('Scripting.FileSystemObject'); 1746 $file = $filesystem->GetFile($path); 1747 $filesize = $file->Size(); 1748 unset($filesystem, $file); 1749 } else { 1750 $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI'; 1751 } 1752 } else { 1753 $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\''; 1754 } 1755 if (isset($commandline)) { 1756 $output = trim(`$commandline`); 1757 if (ctype_digit($output)) { 1758 $filesize = (float) $output; 1759 } 1760 } 1761 return $filesize; 1762 } 1763 1764 /** 1765 * @param string $filename 1766 * 1767 * @return string|false 1768 */ 1769 public static function truepath($filename) { 1770 // 2017-11-08: this could use some improvement, patches welcome 1771 if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) { 1772 // PHP's built-in realpath function does not work on UNC Windows shares 1773 $goodpath = array(); 1774 foreach (explode('/', str_replace('\\', '/', $filename)) as $part) { 1775 if ($part == '.') { 1776 continue; 1777 } 1778 if ($part == '..') { 1779 if (count($goodpath)) { 1780 array_pop($goodpath); 1781 } else { 1782 // cannot step above this level, already at top level 1783 return false; 1784 } 1785 } else { 1786 $goodpath[] = $part; 1787 } 1788 } 1789 return implode(DIRECTORY_SEPARATOR, $goodpath); 1790 } 1791 return realpath($filename); 1792 } 1793 1794 /** 1795 * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268) 1796 * 1797 * @param string $path A path. 1798 * @param string $suffix If the name component ends in suffix this will also be cut off. 1799 * 1800 * @return string 1801 */ 1802 public static function mb_basename($path, $suffix = null) { 1803 $splited = preg_split('#/#', rtrim($path, '/ ')); 1804 return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1); 1805 } 1806 1807 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Jan 27 01:00:08 2021 | Cross-referenced by PHPXref 0.7.1 |