[ 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 https://web.archive.org/web/20120325162206/http://www.psc.edu/general/software/packages/ieee/ieee.php 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 = NAN; 298 } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) { 299 if ($signbit == '1') { 300 $floatvalue = -INF; 301 } else { 302 $floatvalue = INF; 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 if (!is_numeric($number)) { 431 // https://github.com/JamesHeinrich/getID3/issues/299 432 trigger_error('TypeError: Dec2Bin(): Argument #1 ($number) must be numeric, '.gettype($number).' given', E_USER_WARNING); 433 return ''; 434 } 435 $bytes = array(); 436 while ($number >= 256) { 437 $bytes[] = (int) (($number / 256) - (floor($number / 256))) * 256; 438 $number = floor($number / 256); 439 } 440 $bytes[] = (int) $number; 441 $binstring = ''; 442 foreach ($bytes as $i => $byte) { 443 $binstring = (($i == count($bytes) - 1) ? decbin($byte) : str_pad(decbin($byte), 8, '0', STR_PAD_LEFT)).$binstring; 444 } 445 return $binstring; 446 } 447 448 /** 449 * @param string $binstring 450 * @param bool $signed 451 * 452 * @return int|float 453 */ 454 public static function Bin2Dec($binstring, $signed=false) { 455 $signmult = 1; 456 if ($signed) { 457 if ($binstring[0] == '1') { 458 $signmult = -1; 459 } 460 $binstring = substr($binstring, 1); 461 } 462 $decvalue = 0; 463 for ($i = 0; $i < strlen($binstring); $i++) { 464 $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i); 465 } 466 return self::CastAsInt($decvalue * $signmult); 467 } 468 469 /** 470 * @param string $binstring 471 * 472 * @return string 473 */ 474 public static function Bin2String($binstring) { 475 // return 'hi' for input of '0110100001101001' 476 $string = ''; 477 $binstringreversed = strrev($binstring); 478 for ($i = 0; $i < strlen($binstringreversed); $i += 8) { 479 $string = chr(self::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string; 480 } 481 return $string; 482 } 483 484 /** 485 * @param int $number 486 * @param int $minbytes 487 * @param bool $synchsafe 488 * 489 * @return string 490 */ 491 public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) { 492 $intstring = ''; 493 while ($number > 0) { 494 if ($synchsafe) { 495 $intstring = $intstring.chr($number & 127); 496 $number >>= 7; 497 } else { 498 $intstring = $intstring.chr($number & 255); 499 $number >>= 8; 500 } 501 } 502 return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); 503 } 504 505 /** 506 * @param mixed $array1 507 * @param mixed $array2 508 * 509 * @return array|false 510 */ 511 public static function array_merge_clobber($array1, $array2) { 512 // written by kcรhireability*com 513 // taken from http://www.php.net/manual/en/function.array-merge-recursive.php 514 if (!is_array($array1) || !is_array($array2)) { 515 return false; 516 } 517 $newarray = $array1; 518 foreach ($array2 as $key => $val) { 519 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) { 520 $newarray[$key] = self::array_merge_clobber($newarray[$key], $val); 521 } else { 522 $newarray[$key] = $val; 523 } 524 } 525 return $newarray; 526 } 527 528 /** 529 * @param mixed $array1 530 * @param mixed $array2 531 * 532 * @return array|false 533 */ 534 public static function array_merge_noclobber($array1, $array2) { 535 if (!is_array($array1) || !is_array($array2)) { 536 return false; 537 } 538 $newarray = $array1; 539 foreach ($array2 as $key => $val) { 540 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) { 541 $newarray[$key] = self::array_merge_noclobber($newarray[$key], $val); 542 } elseif (!isset($newarray[$key])) { 543 $newarray[$key] = $val; 544 } 545 } 546 return $newarray; 547 } 548 549 /** 550 * @param mixed $array1 551 * @param mixed $array2 552 * 553 * @return array|false|null 554 */ 555 public static function flipped_array_merge_noclobber($array1, $array2) { 556 if (!is_array($array1) || !is_array($array2)) { 557 return false; 558 } 559 # naturally, this only works non-recursively 560 $newarray = array_flip($array1); 561 foreach (array_flip($array2) as $key => $val) { 562 if (!isset($newarray[$key])) { 563 $newarray[$key] = count($newarray); 564 } 565 } 566 return array_flip($newarray); 567 } 568 569 /** 570 * @param array $theArray 571 * 572 * @return bool 573 */ 574 public static function ksort_recursive(&$theArray) { 575 ksort($theArray); 576 foreach ($theArray as $key => $value) { 577 if (is_array($value)) { 578 self::ksort_recursive($theArray[$key]); 579 } 580 } 581 return true; 582 } 583 584 /** 585 * @param string $filename 586 * @param int $numextensions 587 * 588 * @return string 589 */ 590 public static function fileextension($filename, $numextensions=1) { 591 if (strstr($filename, '.')) { 592 $reversedfilename = strrev($filename); 593 $offset = 0; 594 for ($i = 0; $i < $numextensions; $i++) { 595 $offset = strpos($reversedfilename, '.', $offset + 1); 596 if ($offset === false) { 597 return ''; 598 } 599 } 600 return strrev(substr($reversedfilename, 0, $offset)); 601 } 602 return ''; 603 } 604 605 /** 606 * @param int $seconds 607 * 608 * @return string 609 */ 610 public static function PlaytimeString($seconds) { 611 $sign = (($seconds < 0) ? '-' : ''); 612 $seconds = round(abs($seconds)); 613 $H = (int) floor( $seconds / 3600); 614 $M = (int) floor(($seconds - (3600 * $H) ) / 60); 615 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) ); 616 return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT); 617 } 618 619 /** 620 * @param int $macdate 621 * 622 * @return int|float 623 */ 624 public static function DateMac2Unix($macdate) { 625 // Macintosh timestamp: seconds since 00:00h January 1, 1904 626 // UNIX timestamp: seconds since 00:00h January 1, 1970 627 return self::CastAsInt($macdate - 2082844800); 628 } 629 630 /** 631 * @param string $rawdata 632 * 633 * @return float 634 */ 635 public static function FixedPoint8_8($rawdata) { 636 return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8)); 637 } 638 639 /** 640 * @param string $rawdata 641 * 642 * @return float 643 */ 644 public static function FixedPoint16_16($rawdata) { 645 return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16)); 646 } 647 648 /** 649 * @param string $rawdata 650 * 651 * @return float 652 */ 653 public static function FixedPoint2_30($rawdata) { 654 $binarystring = self::BigEndian2Bin($rawdata); 655 return self::Bin2Dec(substr($binarystring, 0, 2)) + (float) (self::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30)); 656 } 657 658 659 /** 660 * @param string $ArrayPath 661 * @param string $Separator 662 * @param mixed $Value 663 * 664 * @return array 665 */ 666 public static function CreateDeepArray($ArrayPath, $Separator, $Value) { 667 // assigns $Value to a nested array path: 668 // $foo = self::CreateDeepArray('/path/to/my', '/', 'file.txt') 669 // is the same as: 670 // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt')))); 671 // or 672 // $foo['path']['to']['my'] = 'file.txt'; 673 $ArrayPath = ltrim($ArrayPath, $Separator); 674 $ReturnedArray = array(); 675 if (($pos = strpos($ArrayPath, $Separator)) !== false) { 676 $ReturnedArray[substr($ArrayPath, 0, $pos)] = self::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value); 677 } else { 678 $ReturnedArray[$ArrayPath] = $Value; 679 } 680 return $ReturnedArray; 681 } 682 683 /** 684 * @param array $arraydata 685 * @param bool $returnkey 686 * 687 * @return int|false 688 */ 689 public static function array_max($arraydata, $returnkey=false) { 690 $maxvalue = false; 691 $maxkey = false; 692 foreach ($arraydata as $key => $value) { 693 if (!is_array($value)) { 694 if (($maxvalue === false) || ($value > $maxvalue)) { 695 $maxvalue = $value; 696 $maxkey = $key; 697 } 698 } 699 } 700 return ($returnkey ? $maxkey : $maxvalue); 701 } 702 703 /** 704 * @param array $arraydata 705 * @param bool $returnkey 706 * 707 * @return int|false 708 */ 709 public static function array_min($arraydata, $returnkey=false) { 710 $minvalue = false; 711 $minkey = false; 712 foreach ($arraydata as $key => $value) { 713 if (!is_array($value)) { 714 if (($minvalue === false) || ($value < $minvalue)) { 715 $minvalue = $value; 716 $minkey = $key; 717 } 718 } 719 } 720 return ($returnkey ? $minkey : $minvalue); 721 } 722 723 /** 724 * @param string $XMLstring 725 * 726 * @return array|false 727 */ 728 public static function XML2array($XMLstring) { 729 if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) { 730 // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html 731 // https://core.trac.wordpress.org/changeset/29378 732 // This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading is 733 // disabled by default, but is still needed when LIBXML_NOENT is used. 734 $loader = @libxml_disable_entity_loader(true); 735 $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT); 736 $return = self::SimpleXMLelement2array($XMLobject); 737 @libxml_disable_entity_loader($loader); 738 return $return; 739 } 740 return false; 741 } 742 743 /** 744 * @param SimpleXMLElement|array|mixed $XMLobject 745 * 746 * @return mixed 747 */ 748 public static function SimpleXMLelement2array($XMLobject) { 749 if (!is_object($XMLobject) && !is_array($XMLobject)) { 750 return $XMLobject; 751 } 752 $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject; 753 foreach ($XMLarray as $key => $value) { 754 $XMLarray[$key] = self::SimpleXMLelement2array($value); 755 } 756 return $XMLarray; 757 } 758 759 /** 760 * Returns checksum for a file from starting position to absolute end position. 761 * 762 * @param string $file 763 * @param int $offset 764 * @param int $end 765 * @param string $algorithm 766 * 767 * @return string|false 768 * @throws getid3_exception 769 */ 770 public static function hash_data($file, $offset, $end, $algorithm) { 771 if (!self::intValueSupported($end)) { 772 return false; 773 } 774 if (!in_array($algorithm, array('md5', 'sha1'))) { 775 throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()'); 776 } 777 778 $size = $end - $offset; 779 780 $fp = fopen($file, 'rb'); 781 fseek($fp, $offset); 782 $ctx = hash_init($algorithm); 783 while ($size > 0) { 784 $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE)); 785 hash_update($ctx, $buffer); 786 $size -= getID3::FREAD_BUFFER_SIZE; 787 } 788 $hash = hash_final($ctx); 789 fclose($fp); 790 791 return $hash; 792 } 793 794 /** 795 * @param string $filename_source 796 * @param string $filename_dest 797 * @param int $offset 798 * @param int $length 799 * 800 * @return bool 801 * @throws Exception 802 * 803 * @deprecated Unused, may be removed in future versions of getID3 804 */ 805 public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) { 806 if (!self::intValueSupported($offset + $length)) { 807 throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit'); 808 } 809 if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) { 810 if (($fp_dest = fopen($filename_dest, 'wb'))) { 811 if (fseek($fp_src, $offset) == 0) { 812 $byteslefttowrite = $length; 813 while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) { 814 $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite); 815 $byteslefttowrite -= $byteswritten; 816 } 817 fclose($fp_dest); 818 return true; 819 } else { 820 fclose($fp_src); 821 throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source); 822 } 823 } else { 824 throw new Exception('failed to create file for writing '.$filename_dest); 825 } 826 } else { 827 throw new Exception('failed to open file for reading '.$filename_source); 828 } 829 } 830 831 /** 832 * @param int $charval 833 * 834 * @return string 835 */ 836 public static function iconv_fallback_int_utf8($charval) { 837 if ($charval < 128) { 838 // 0bbbbbbb 839 $newcharstring = chr($charval); 840 } elseif ($charval < 2048) { 841 // 110bbbbb 10bbbbbb 842 $newcharstring = chr(($charval >> 6) | 0xC0); 843 $newcharstring .= chr(($charval & 0x3F) | 0x80); 844 } elseif ($charval < 65536) { 845 // 1110bbbb 10bbbbbb 10bbbbbb 846 $newcharstring = chr(($charval >> 12) | 0xE0); 847 $newcharstring .= chr(($charval >> 6) | 0xC0); 848 $newcharstring .= chr(($charval & 0x3F) | 0x80); 849 } else { 850 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 851 $newcharstring = chr(($charval >> 18) | 0xF0); 852 $newcharstring .= chr(($charval >> 12) | 0xC0); 853 $newcharstring .= chr(($charval >> 6) | 0xC0); 854 $newcharstring .= chr(($charval & 0x3F) | 0x80); 855 } 856 return $newcharstring; 857 } 858 859 /** 860 * ISO-8859-1 => UTF-8 861 * 862 * @param string $string 863 * @param bool $bom 864 * 865 * @return string 866 */ 867 public static function iconv_fallback_iso88591_utf8($string, $bom=false) { 868 if (function_exists('utf8_encode')) { 869 return utf8_encode($string); 870 } 871 // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) 872 $newcharstring = ''; 873 if ($bom) { 874 $newcharstring .= "\xEF\xBB\xBF"; 875 } 876 for ($i = 0; $i < strlen($string); $i++) { 877 $charval = ord($string[$i]); 878 $newcharstring .= self::iconv_fallback_int_utf8($charval); 879 } 880 return $newcharstring; 881 } 882 883 /** 884 * ISO-8859-1 => UTF-16BE 885 * 886 * @param string $string 887 * @param bool $bom 888 * 889 * @return string 890 */ 891 public static function iconv_fallback_iso88591_utf16be($string, $bom=false) { 892 $newcharstring = ''; 893 if ($bom) { 894 $newcharstring .= "\xFE\xFF"; 895 } 896 for ($i = 0; $i < strlen($string); $i++) { 897 $newcharstring .= "\x00".$string[$i]; 898 } 899 return $newcharstring; 900 } 901 902 /** 903 * ISO-8859-1 => UTF-16LE 904 * 905 * @param string $string 906 * @param bool $bom 907 * 908 * @return string 909 */ 910 public static function iconv_fallback_iso88591_utf16le($string, $bom=false) { 911 $newcharstring = ''; 912 if ($bom) { 913 $newcharstring .= "\xFF\xFE"; 914 } 915 for ($i = 0; $i < strlen($string); $i++) { 916 $newcharstring .= $string[$i]."\x00"; 917 } 918 return $newcharstring; 919 } 920 921 /** 922 * ISO-8859-1 => UTF-16LE (BOM) 923 * 924 * @param string $string 925 * 926 * @return string 927 */ 928 public static function iconv_fallback_iso88591_utf16($string) { 929 return self::iconv_fallback_iso88591_utf16le($string, true); 930 } 931 932 /** 933 * UTF-8 => ISO-8859-1 934 * 935 * @param string $string 936 * 937 * @return string 938 */ 939 public static function iconv_fallback_utf8_iso88591($string) { 940 if (function_exists('utf8_decode')) { 941 return utf8_decode($string); 942 } 943 // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) 944 $newcharstring = ''; 945 $offset = 0; 946 $stringlength = strlen($string); 947 while ($offset < $stringlength) { 948 if ((ord($string[$offset]) | 0x07) == 0xF7) { 949 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 950 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 951 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 952 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 953 (ord($string[($offset + 3)]) & 0x3F); 954 $offset += 4; 955 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 956 // 1110bbbb 10bbbbbb 10bbbbbb 957 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 958 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 959 (ord($string[($offset + 2)]) & 0x3F); 960 $offset += 3; 961 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 962 // 110bbbbb 10bbbbbb 963 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 964 (ord($string[($offset + 1)]) & 0x3F); 965 $offset += 2; 966 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 967 // 0bbbbbbb 968 $charval = ord($string[$offset]); 969 $offset += 1; 970 } else { 971 // error? throw some kind of warning here? 972 $charval = false; 973 $offset += 1; 974 } 975 if ($charval !== false) { 976 $newcharstring .= (($charval < 256) ? chr($charval) : '?'); 977 } 978 } 979 return $newcharstring; 980 } 981 982 /** 983 * UTF-8 => UTF-16BE 984 * 985 * @param string $string 986 * @param bool $bom 987 * 988 * @return string 989 */ 990 public static function iconv_fallback_utf8_utf16be($string, $bom=false) { 991 $newcharstring = ''; 992 if ($bom) { 993 $newcharstring .= "\xFE\xFF"; 994 } 995 $offset = 0; 996 $stringlength = strlen($string); 997 while ($offset < $stringlength) { 998 if ((ord($string[$offset]) | 0x07) == 0xF7) { 999 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 1000 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 1001 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 1002 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 1003 (ord($string[($offset + 3)]) & 0x3F); 1004 $offset += 4; 1005 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 1006 // 1110bbbb 10bbbbbb 10bbbbbb 1007 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 1008 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 1009 (ord($string[($offset + 2)]) & 0x3F); 1010 $offset += 3; 1011 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 1012 // 110bbbbb 10bbbbbb 1013 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 1014 (ord($string[($offset + 1)]) & 0x3F); 1015 $offset += 2; 1016 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 1017 // 0bbbbbbb 1018 $charval = ord($string[$offset]); 1019 $offset += 1; 1020 } else { 1021 // error? throw some kind of warning here? 1022 $charval = false; 1023 $offset += 1; 1024 } 1025 if ($charval !== false) { 1026 $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?'); 1027 } 1028 } 1029 return $newcharstring; 1030 } 1031 1032 /** 1033 * UTF-8 => UTF-16LE 1034 * 1035 * @param string $string 1036 * @param bool $bom 1037 * 1038 * @return string 1039 */ 1040 public static function iconv_fallback_utf8_utf16le($string, $bom=false) { 1041 $newcharstring = ''; 1042 if ($bom) { 1043 $newcharstring .= "\xFF\xFE"; 1044 } 1045 $offset = 0; 1046 $stringlength = strlen($string); 1047 while ($offset < $stringlength) { 1048 if ((ord($string[$offset]) | 0x07) == 0xF7) { 1049 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 1050 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 1051 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 1052 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 1053 (ord($string[($offset + 3)]) & 0x3F); 1054 $offset += 4; 1055 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 1056 // 1110bbbb 10bbbbbb 10bbbbbb 1057 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 1058 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 1059 (ord($string[($offset + 2)]) & 0x3F); 1060 $offset += 3; 1061 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 1062 // 110bbbbb 10bbbbbb 1063 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 1064 (ord($string[($offset + 1)]) & 0x3F); 1065 $offset += 2; 1066 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 1067 // 0bbbbbbb 1068 $charval = ord($string[$offset]); 1069 $offset += 1; 1070 } else { 1071 // error? maybe throw some warning here? 1072 $charval = false; 1073 $offset += 1; 1074 } 1075 if ($charval !== false) { 1076 $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00"); 1077 } 1078 } 1079 return $newcharstring; 1080 } 1081 1082 /** 1083 * UTF-8 => UTF-16LE (BOM) 1084 * 1085 * @param string $string 1086 * 1087 * @return string 1088 */ 1089 public static function iconv_fallback_utf8_utf16($string) { 1090 return self::iconv_fallback_utf8_utf16le($string, true); 1091 } 1092 1093 /** 1094 * UTF-16BE => UTF-8 1095 * 1096 * @param string $string 1097 * 1098 * @return string 1099 */ 1100 public static function iconv_fallback_utf16be_utf8($string) { 1101 if (substr($string, 0, 2) == "\xFE\xFF") { 1102 // strip BOM 1103 $string = substr($string, 2); 1104 } 1105 $newcharstring = ''; 1106 for ($i = 0; $i < strlen($string); $i += 2) { 1107 $charval = self::BigEndian2Int(substr($string, $i, 2)); 1108 $newcharstring .= self::iconv_fallback_int_utf8($charval); 1109 } 1110 return $newcharstring; 1111 } 1112 1113 /** 1114 * UTF-16LE => UTF-8 1115 * 1116 * @param string $string 1117 * 1118 * @return string 1119 */ 1120 public static function iconv_fallback_utf16le_utf8($string) { 1121 if (substr($string, 0, 2) == "\xFF\xFE") { 1122 // strip BOM 1123 $string = substr($string, 2); 1124 } 1125 $newcharstring = ''; 1126 for ($i = 0; $i < strlen($string); $i += 2) { 1127 $charval = self::LittleEndian2Int(substr($string, $i, 2)); 1128 $newcharstring .= self::iconv_fallback_int_utf8($charval); 1129 } 1130 return $newcharstring; 1131 } 1132 1133 /** 1134 * UTF-16BE => ISO-8859-1 1135 * 1136 * @param string $string 1137 * 1138 * @return string 1139 */ 1140 public static function iconv_fallback_utf16be_iso88591($string) { 1141 if (substr($string, 0, 2) == "\xFE\xFF") { 1142 // strip BOM 1143 $string = substr($string, 2); 1144 } 1145 $newcharstring = ''; 1146 for ($i = 0; $i < strlen($string); $i += 2) { 1147 $charval = self::BigEndian2Int(substr($string, $i, 2)); 1148 $newcharstring .= (($charval < 256) ? chr($charval) : '?'); 1149 } 1150 return $newcharstring; 1151 } 1152 1153 /** 1154 * UTF-16LE => ISO-8859-1 1155 * 1156 * @param string $string 1157 * 1158 * @return string 1159 */ 1160 public static function iconv_fallback_utf16le_iso88591($string) { 1161 if (substr($string, 0, 2) == "\xFF\xFE") { 1162 // strip BOM 1163 $string = substr($string, 2); 1164 } 1165 $newcharstring = ''; 1166 for ($i = 0; $i < strlen($string); $i += 2) { 1167 $charval = self::LittleEndian2Int(substr($string, $i, 2)); 1168 $newcharstring .= (($charval < 256) ? chr($charval) : '?'); 1169 } 1170 return $newcharstring; 1171 } 1172 1173 /** 1174 * UTF-16 (BOM) => ISO-8859-1 1175 * 1176 * @param string $string 1177 * 1178 * @return string 1179 */ 1180 public static function iconv_fallback_utf16_iso88591($string) { 1181 $bom = substr($string, 0, 2); 1182 if ($bom == "\xFE\xFF") { 1183 return self::iconv_fallback_utf16be_iso88591(substr($string, 2)); 1184 } elseif ($bom == "\xFF\xFE") { 1185 return self::iconv_fallback_utf16le_iso88591(substr($string, 2)); 1186 } 1187 return $string; 1188 } 1189 1190 /** 1191 * UTF-16 (BOM) => UTF-8 1192 * 1193 * @param string $string 1194 * 1195 * @return string 1196 */ 1197 public static function iconv_fallback_utf16_utf8($string) { 1198 $bom = substr($string, 0, 2); 1199 if ($bom == "\xFE\xFF") { 1200 return self::iconv_fallback_utf16be_utf8(substr($string, 2)); 1201 } elseif ($bom == "\xFF\xFE") { 1202 return self::iconv_fallback_utf16le_utf8(substr($string, 2)); 1203 } 1204 return $string; 1205 } 1206 1207 /** 1208 * @param string $in_charset 1209 * @param string $out_charset 1210 * @param string $string 1211 * 1212 * @return string 1213 * @throws Exception 1214 */ 1215 public static function iconv_fallback($in_charset, $out_charset, $string) { 1216 1217 if ($in_charset == $out_charset) { 1218 return $string; 1219 } 1220 1221 // mb_convert_encoding() available 1222 if (function_exists('mb_convert_encoding')) { 1223 if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) { 1224 // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM 1225 $string = "\xFF\xFE".$string; 1226 } 1227 if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) { 1228 if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) { 1229 // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified 1230 return ''; 1231 } 1232 } 1233 if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) { 1234 switch ($out_charset) { 1235 case 'ISO-8859-1': 1236 $converted_string = rtrim($converted_string, "\x00"); 1237 break; 1238 } 1239 return $converted_string; 1240 } 1241 return $string; 1242 1243 // iconv() available 1244 } elseif (function_exists('iconv')) { 1245 if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) { 1246 switch ($out_charset) { 1247 case 'ISO-8859-1': 1248 $converted_string = rtrim($converted_string, "\x00"); 1249 break; 1250 } 1251 return $converted_string; 1252 } 1253 1254 // iconv() may sometimes fail with "illegal character in input string" error message 1255 // and return an empty string, but returning the unconverted string is more useful 1256 return $string; 1257 } 1258 1259 1260 // neither mb_convert_encoding or iconv() is available 1261 static $ConversionFunctionList = array(); 1262 if (empty($ConversionFunctionList)) { 1263 $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8'; 1264 $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16'; 1265 $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be'; 1266 $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le'; 1267 $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591'; 1268 $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16'; 1269 $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be'; 1270 $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le'; 1271 $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591'; 1272 $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8'; 1273 $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591'; 1274 $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8'; 1275 $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591'; 1276 $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8'; 1277 } 1278 if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) { 1279 $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)]; 1280 return self::$ConversionFunction($string); 1281 } 1282 throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset); 1283 } 1284 1285 /** 1286 * @param mixed $data 1287 * @param string $charset 1288 * 1289 * @return mixed 1290 */ 1291 public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') { 1292 if (is_string($data)) { 1293 return self::MultiByteCharString2HTML($data, $charset); 1294 } elseif (is_array($data)) { 1295 $return_data = array(); 1296 foreach ($data as $key => $value) { 1297 $return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset); 1298 } 1299 return $return_data; 1300 } 1301 // integer, float, objects, resources, etc 1302 return $data; 1303 } 1304 1305 /** 1306 * @param string|int|float $string 1307 * @param string $charset 1308 * 1309 * @return string 1310 */ 1311 public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') { 1312 $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string 1313 $HTMLstring = ''; 1314 1315 switch (strtolower($charset)) { 1316 case '1251': 1317 case '1252': 1318 case '866': 1319 case '932': 1320 case '936': 1321 case '950': 1322 case 'big5': 1323 case 'big5-hkscs': 1324 case 'cp1251': 1325 case 'cp1252': 1326 case 'cp866': 1327 case 'euc-jp': 1328 case 'eucjp': 1329 case 'gb2312': 1330 case 'ibm866': 1331 case 'iso-8859-1': 1332 case 'iso-8859-15': 1333 case 'iso8859-1': 1334 case 'iso8859-15': 1335 case 'koi8-r': 1336 case 'koi8-ru': 1337 case 'koi8r': 1338 case 'shift_jis': 1339 case 'sjis': 1340 case 'win-1251': 1341 case 'windows-1251': 1342 case 'windows-1252': 1343 $HTMLstring = htmlentities($string, ENT_COMPAT, $charset); 1344 break; 1345 1346 case 'utf-8': 1347 $strlen = strlen($string); 1348 for ($i = 0; $i < $strlen; $i++) { 1349 $char_ord_val = ord($string[$i]); 1350 $charval = 0; 1351 if ($char_ord_val < 0x80) { 1352 $charval = $char_ord_val; 1353 } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) { 1354 $charval = (($char_ord_val & 0x07) << 18); 1355 $charval += ((ord($string[++$i]) & 0x3F) << 12); 1356 $charval += ((ord($string[++$i]) & 0x3F) << 6); 1357 $charval += (ord($string[++$i]) & 0x3F); 1358 } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) { 1359 $charval = (($char_ord_val & 0x0F) << 12); 1360 $charval += ((ord($string[++$i]) & 0x3F) << 6); 1361 $charval += (ord($string[++$i]) & 0x3F); 1362 } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) { 1363 $charval = (($char_ord_val & 0x1F) << 6); 1364 $charval += (ord($string[++$i]) & 0x3F); 1365 } 1366 if (($charval >= 32) && ($charval <= 127)) { 1367 $HTMLstring .= htmlentities(chr($charval)); 1368 } else { 1369 $HTMLstring .= '&#'.$charval.';'; 1370 } 1371 } 1372 break; 1373 1374 case 'utf-16le': 1375 for ($i = 0; $i < strlen($string); $i += 2) { 1376 $charval = self::LittleEndian2Int(substr($string, $i, 2)); 1377 if (($charval >= 32) && ($charval <= 127)) { 1378 $HTMLstring .= chr($charval); 1379 } else { 1380 $HTMLstring .= '&#'.$charval.';'; 1381 } 1382 } 1383 break; 1384 1385 case 'utf-16be': 1386 for ($i = 0; $i < strlen($string); $i += 2) { 1387 $charval = self::BigEndian2Int(substr($string, $i, 2)); 1388 if (($charval >= 32) && ($charval <= 127)) { 1389 $HTMLstring .= chr($charval); 1390 } else { 1391 $HTMLstring .= '&#'.$charval.';'; 1392 } 1393 } 1394 break; 1395 1396 default: 1397 $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()'; 1398 break; 1399 } 1400 return $HTMLstring; 1401 } 1402 1403 /** 1404 * @param int $namecode 1405 * 1406 * @return string 1407 */ 1408 public static function RGADnameLookup($namecode) { 1409 static $RGADname = array(); 1410 if (empty($RGADname)) { 1411 $RGADname[0] = 'not set'; 1412 $RGADname[1] = 'Track Gain Adjustment'; 1413 $RGADname[2] = 'Album Gain Adjustment'; 1414 } 1415 1416 return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : ''); 1417 } 1418 1419 /** 1420 * @param int $originatorcode 1421 * 1422 * @return string 1423 */ 1424 public static function RGADoriginatorLookup($originatorcode) { 1425 static $RGADoriginator = array(); 1426 if (empty($RGADoriginator)) { 1427 $RGADoriginator[0] = 'unspecified'; 1428 $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer'; 1429 $RGADoriginator[2] = 'set by user'; 1430 $RGADoriginator[3] = 'determined automatically'; 1431 } 1432 1433 return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : ''); 1434 } 1435 1436 /** 1437 * @param int $rawadjustment 1438 * @param int $signbit 1439 * 1440 * @return float 1441 */ 1442 public static function RGADadjustmentLookup($rawadjustment, $signbit) { 1443 $adjustment = (float) $rawadjustment / 10; 1444 if ($signbit == 1) { 1445 $adjustment *= -1; 1446 } 1447 return $adjustment; 1448 } 1449 1450 /** 1451 * @param int $namecode 1452 * @param int $originatorcode 1453 * @param int $replaygain 1454 * 1455 * @return string 1456 */ 1457 public static function RGADgainString($namecode, $originatorcode, $replaygain) { 1458 if ($replaygain < 0) { 1459 $signbit = '1'; 1460 } else { 1461 $signbit = '0'; 1462 } 1463 $storedreplaygain = intval(round($replaygain * 10)); 1464 $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT); 1465 $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT); 1466 $gainstring .= $signbit; 1467 $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT); 1468 1469 return $gainstring; 1470 } 1471 1472 /** 1473 * @param float $amplitude 1474 * 1475 * @return float 1476 */ 1477 public static function RGADamplitude2dB($amplitude) { 1478 return 20 * log10($amplitude); 1479 } 1480 1481 /** 1482 * @param string $imgData 1483 * @param array $imageinfo 1484 * 1485 * @return array|false 1486 */ 1487 public static function GetDataImageSize($imgData, &$imageinfo=array()) { 1488 if (PHP_VERSION_ID >= 50400) { 1489 $GetDataImageSize = @getimagesizefromstring($imgData, $imageinfo); 1490 if ($GetDataImageSize === false || !isset($GetDataImageSize[0], $GetDataImageSize[1])) { 1491 return false; 1492 } 1493 $GetDataImageSize['height'] = $GetDataImageSize[0]; 1494 $GetDataImageSize['width'] = $GetDataImageSize[1]; 1495 return $GetDataImageSize; 1496 } 1497 static $tempdir = ''; 1498 if (empty($tempdir)) { 1499 if (function_exists('sys_get_temp_dir')) { 1500 $tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52 1501 } 1502 1503 // yes this is ugly, feel free to suggest a better way 1504 if (include_once(dirname(__FILE__).'/getid3.php')) { 1505 $getid3_temp = new getID3(); 1506 if ($getid3_temp_tempdir = $getid3_temp->tempdir) { 1507 $tempdir = $getid3_temp_tempdir; 1508 } 1509 unset($getid3_temp, $getid3_temp_tempdir); 1510 } 1511 } 1512 $GetDataImageSize = false; 1513 if ($tempfilename = tempnam($tempdir, 'gI3')) { 1514 if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) { 1515 fwrite($tmp, $imgData); 1516 fclose($tmp); 1517 $GetDataImageSize = @getimagesize($tempfilename, $imageinfo); 1518 if (($GetDataImageSize === false) || !isset($GetDataImageSize[0]) || !isset($GetDataImageSize[1])) { 1519 return false; 1520 } 1521 $GetDataImageSize['height'] = $GetDataImageSize[0]; 1522 $GetDataImageSize['width'] = $GetDataImageSize[1]; 1523 } 1524 unlink($tempfilename); 1525 } 1526 return $GetDataImageSize; 1527 } 1528 1529 /** 1530 * @param string $mime_type 1531 * 1532 * @return string 1533 */ 1534 public static function ImageExtFromMime($mime_type) { 1535 // temporary way, works OK for now, but should be reworked in the future 1536 return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type); 1537 } 1538 1539 /** 1540 * @param array $ThisFileInfo 1541 * @param bool $option_tags_html default true (just as in the main getID3 class) 1542 * 1543 * @return bool 1544 */ 1545 public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) { 1546 // Copy all entries from ['tags'] into common ['comments'] 1547 if (!empty($ThisFileInfo['tags'])) { 1548 1549 // Some tag types can only support limited character sets and may contain data in non-standard encoding (usually ID3v1) 1550 // and/or poorly-transliterated tag values that are also in tag formats that do support full-range character sets 1551 // To make the output more user-friendly, process the potentially-problematic tag formats last to enhance the chance that 1552 // the first entries in [comments] are the most correct and the "bad" ones (if any) come later. 1553 // https://github.com/JamesHeinrich/getID3/issues/338 1554 $processLastTagTypes = array('id3v1','riff'); 1555 foreach ($processLastTagTypes as $processLastTagType) { 1556 if (isset($ThisFileInfo['tags'][$processLastTagType])) { 1557 // bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings 1558 $temp = $ThisFileInfo['tags'][$processLastTagType]; 1559 unset($ThisFileInfo['tags'][$processLastTagType]); 1560 $ThisFileInfo['tags'][$processLastTagType] = $temp; 1561 unset($temp); 1562 } 1563 } 1564 foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) { 1565 foreach ($tagarray as $tagname => $tagdata) { 1566 foreach ($tagdata as $key => $value) { 1567 if (!empty($value)) { 1568 if (empty($ThisFileInfo['comments'][$tagname])) { 1569 1570 // fall through and append value 1571 1572 } elseif ($tagtype == 'id3v1') { 1573 1574 $newvaluelength = strlen(trim($value)); 1575 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { 1576 $oldvaluelength = strlen(trim($existingvalue)); 1577 if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) { 1578 // new value is identical but shorter-than (or equal-length to) one already in comments - skip 1579 break 2; 1580 } 1581 1582 if (function_exists('mb_convert_encoding')) { 1583 if (trim($value) == trim(substr(mb_convert_encoding($existingvalue, $ThisFileInfo['id3v1']['encoding'], $ThisFileInfo['encoding']), 0, 30))) { 1584 // value stored in ID3v1 appears to be probably the multibyte value transliterated (badly) into ISO-8859-1 in ID3v1. 1585 // 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 1586 break 2; 1587 } 1588 } 1589 } 1590 1591 } elseif (!is_array($value)) { 1592 1593 $newvaluelength = strlen(trim($value)); 1594 $newvaluelengthMB = mb_strlen(trim($value)); 1595 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { 1596 $oldvaluelength = strlen(trim($existingvalue)); 1597 $oldvaluelengthMB = mb_strlen(trim($existingvalue)); 1598 if (($newvaluelengthMB == $oldvaluelengthMB) && ($existingvalue == getid3_lib::iconv_fallback('UTF-8', 'ASCII', $value))) { 1599 // https://github.com/JamesHeinrich/getID3/issues/338 1600 // check for tags containing extended characters that may have been forced into limited-character storage (e.g. UTF8 values into ASCII) 1601 // which will usually display unrepresentable characters as "?" 1602 $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value); 1603 break; 1604 } 1605 if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) { 1606 $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value); 1607 break; 1608 } 1609 } 1610 1611 } 1612 if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) { 1613 $value = (is_string($value) ? trim($value) : $value); 1614 if (!is_int($key) && !ctype_digit($key)) { 1615 $ThisFileInfo['comments'][$tagname][$key] = $value; 1616 } else { 1617 if (!isset($ThisFileInfo['comments'][$tagname])) { 1618 $ThisFileInfo['comments'][$tagname] = array($value); 1619 } else { 1620 $ThisFileInfo['comments'][$tagname][] = $value; 1621 } 1622 } 1623 } 1624 } 1625 } 1626 } 1627 } 1628 1629 // attempt to standardize spelling of returned keys 1630 if (!empty($ThisFileInfo['comments'])) { 1631 $StandardizeFieldNames = array( 1632 'tracknumber' => 'track_number', 1633 'track' => 'track_number', 1634 ); 1635 foreach ($StandardizeFieldNames as $badkey => $goodkey) { 1636 if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) { 1637 $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey]; 1638 unset($ThisFileInfo['comments'][$badkey]); 1639 } 1640 } 1641 } 1642 1643 if ($option_tags_html) { 1644 // Copy ['comments'] to ['comments_html'] 1645 if (!empty($ThisFileInfo['comments'])) { 1646 foreach ($ThisFileInfo['comments'] as $field => $values) { 1647 if ($field == 'picture') { 1648 // pictures can take up a lot of space, and we don't need multiple copies of them 1649 // let there be a single copy in [comments][picture], and not elsewhere 1650 continue; 1651 } 1652 foreach ($values as $index => $value) { 1653 if (is_array($value)) { 1654 $ThisFileInfo['comments_html'][$field][$index] = $value; 1655 } else { 1656 $ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding'])); 1657 } 1658 } 1659 } 1660 } 1661 } 1662 1663 } 1664 return true; 1665 } 1666 1667 /** 1668 * @param string $key 1669 * @param int $begin 1670 * @param int $end 1671 * @param string $file 1672 * @param string $name 1673 * 1674 * @return string 1675 */ 1676 public static function EmbeddedLookup($key, $begin, $end, $file, $name) { 1677 1678 // Cached 1679 static $cache; 1680 if (isset($cache[$file][$name])) { 1681 return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); 1682 } 1683 1684 // Init 1685 $keylength = strlen($key); 1686 $line_count = $end - $begin - 7; 1687 1688 // Open php file 1689 $fp = fopen($file, 'r'); 1690 1691 // Discard $begin lines 1692 for ($i = 0; $i < ($begin + 3); $i++) { 1693 fgets($fp, 1024); 1694 } 1695 1696 // Loop thru line 1697 while (0 < $line_count--) { 1698 1699 // Read line 1700 $line = ltrim(fgets($fp, 1024), "\t "); 1701 1702 // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key 1703 //$keycheck = substr($line, 0, $keylength); 1704 //if ($key == $keycheck) { 1705 // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1); 1706 // break; 1707 //} 1708 1709 // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key 1710 //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1)); 1711 $explodedLine = explode("\t", $line, 2); 1712 $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : ''); 1713 $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : ''); 1714 $cache[$file][$name][$ThisKey] = trim($ThisValue); 1715 } 1716 1717 // Close and return 1718 fclose($fp); 1719 return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); 1720 } 1721 1722 /** 1723 * @param string $filename 1724 * @param string $sourcefile 1725 * @param bool $DieOnFailure 1726 * 1727 * @return bool 1728 * @throws Exception 1729 */ 1730 public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) { 1731 global $GETID3_ERRORARRAY; 1732 1733 if (file_exists($filename)) { 1734 if (include_once($filename)) { 1735 return true; 1736 } else { 1737 $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors'; 1738 } 1739 } else { 1740 $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing'; 1741 } 1742 if ($DieOnFailure) { 1743 throw new Exception($diemessage); 1744 } else { 1745 $GETID3_ERRORARRAY[] = $diemessage; 1746 } 1747 return false; 1748 } 1749 1750 /** 1751 * @param string $string 1752 * 1753 * @return string 1754 */ 1755 public static function trimNullByte($string) { 1756 return trim($string, "\x00"); 1757 } 1758 1759 /** 1760 * @param string $path 1761 * 1762 * @return float|bool 1763 */ 1764 public static function getFileSizeSyscall($path) { 1765 $commandline = null; 1766 $filesize = false; 1767 1768 if (GETID3_OS_ISWINDOWS) { 1769 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: 1770 $filesystem = new COM('Scripting.FileSystemObject'); 1771 $file = $filesystem->GetFile($path); 1772 $filesize = $file->Size(); 1773 unset($filesystem, $file); 1774 } else { 1775 $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI'; 1776 } 1777 } else { 1778 $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\''; 1779 } 1780 if (isset($commandline)) { 1781 $output = trim(`$commandline`); 1782 if (ctype_digit($output)) { 1783 $filesize = (float) $output; 1784 } 1785 } 1786 return $filesize; 1787 } 1788 1789 /** 1790 * @param string $filename 1791 * 1792 * @return string|false 1793 */ 1794 public static function truepath($filename) { 1795 // 2017-11-08: this could use some improvement, patches welcome 1796 if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) { 1797 // PHP's built-in realpath function does not work on UNC Windows shares 1798 $goodpath = array(); 1799 foreach (explode('/', str_replace('\\', '/', $filename)) as $part) { 1800 if ($part == '.') { 1801 continue; 1802 } 1803 if ($part == '..') { 1804 if (count($goodpath)) { 1805 array_pop($goodpath); 1806 } else { 1807 // cannot step above this level, already at top level 1808 return false; 1809 } 1810 } else { 1811 $goodpath[] = $part; 1812 } 1813 } 1814 return implode(DIRECTORY_SEPARATOR, $goodpath); 1815 } 1816 return realpath($filename); 1817 } 1818 1819 /** 1820 * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268) 1821 * 1822 * @param string $path A path. 1823 * @param string $suffix If the name component ends in suffix this will also be cut off. 1824 * 1825 * @return string 1826 */ 1827 public static function mb_basename($path, $suffix = '') { 1828 $splited = preg_split('#/#', rtrim($path, '/ ')); 1829 return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1); 1830 } 1831 1832 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |