Changeset 46112 for trunk/src/wp-includes/ID3/getid3.lib.php
- Timestamp:
- 09/14/2019 07:06:09 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ID3/getid3.lib.php
r41196 r46112 1 1 <?php 2 2 3 ///////////////////////////////////////////////////////////////// 3 4 /// getID3() by James Heinrich <info@getid3.org> // 4 // available at http://getid3.sourceforge.net // 5 // or http://www.getid3.org // 6 // also https://github.com/JamesHeinrich/getID3 // 7 ///////////////////////////////////////////////////////////////// 5 // available at https://github.com/JamesHeinrich/getID3 // 6 // or https://www.getid3.org // 7 // or http://getid3.sourceforge.net // 8 8 // // 9 9 // getid3.lib.php - part of getID3() // 10 // See readme.txt for more details//10 // see readme.txt for more details // 11 11 // /// 12 12 ///////////////////////////////////////////////////////////////// … … 15 15 class getid3_lib 16 16 { 17 17 /** 18 * @param string $string 19 * @param bool $hex 20 * @param bool $spaces 21 * @param string $htmlencoding 22 * 23 * @return string 24 */ 18 25 public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') { 19 26 $returnstring = ''; 20 27 for ($i = 0; $i < strlen($string); $i++) { 21 28 if ($hex) { 22 $returnstring .= str_pad(dechex(ord($string {$i})), 2, '0', STR_PAD_LEFT);29 $returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT); 23 30 } else { 24 $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string {$i}) ? $string{$i}: '¤');31 $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : '¤'); 25 32 } 26 33 if ($spaces) { … … 37 44 } 38 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 */ 39 53 public static function trunc($floatnumber) { 40 // truncates a floating-point number at the decimal point41 // returns int (if possible, otherwise float)42 54 if ($floatnumber >= 1) { 43 55 $truncatednumber = floor($floatnumber); … … 53 65 } 54 66 55 67 /** 68 * @param int|null $variable 69 * @param int $increment 70 * 71 * @return bool 72 */ 56 73 public static function safe_inc(&$variable, $increment=1) { 57 74 if (isset($variable)) { … … 63 80 } 64 81 82 /** 83 * @param int|float $floatnum 84 * 85 * @return int|float 86 */ 65 87 public static function CastAsInt($floatnum) { 66 88 // convert to float if not already … … 78 100 } 79 101 102 /** 103 * @param int $num 104 * 105 * @return bool 106 */ 80 107 public static function intValueSupported($num) { 81 108 // check if integers are 64-bit … … 94 121 } 95 122 123 /** 124 * @param string $fraction 125 * 126 * @return float 127 */ 96 128 public static function DecimalizeFraction($fraction) { 97 129 list($numerator, $denominator) = explode('/', $fraction); … … 99 131 } 100 132 101 133 /** 134 * @param string $binarynumerator 135 * 136 * @return float 137 */ 102 138 public static function DecimalBinary2Float($binarynumerator) { 103 139 $numerator = self::Bin2Dec($binarynumerator); … … 106 142 } 107 143 108 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 */ 109 152 public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) { 110 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html111 153 if (strpos($binarypointnumber, '.') === false) { 112 154 $binarypointnumber = '0.'.$binarypointnumber; 113 } elseif ($binarypointnumber {0}== '.') {155 } elseif ($binarypointnumber[0] == '.') { 114 156 $binarypointnumber = '0'.$binarypointnumber; 115 157 } 116 158 $exponent = 0; 117 while (($binarypointnumber {0}!= '1') || (substr($binarypointnumber, 1, 1) != '.')) {159 while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) { 118 160 if (substr($binarypointnumber, 1, 1) == '.') { 119 161 $exponent--; … … 123 165 $exponent += ($pointpos - 1); 124 166 $binarypointnumber = str_replace('.', '', $binarypointnumber); 125 $binarypointnumber = $binarypointnumber {0}.'.'.substr($binarypointnumber, 1);167 $binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1); 126 168 } 127 169 } … … 130 172 } 131 173 132 174 /** 175 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html 176 * 177 * @param float $floatvalue 178 * 179 * @return string 180 */ 133 181 public static function Float2BinaryDecimal($floatvalue) { 134 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html135 182 $maxbits = 128; // to how many bits of precision should the calculations be taken? 136 183 $intpart = self::trunc($floatvalue); … … 146 193 } 147 194 148 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 */ 149 203 public static function Float2String($floatvalue, $bits) { 150 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html 204 $exponentbits = 0; 205 $fractionbits = 0; 151 206 switch ($bits) { 152 207 case 32: … … 177 232 } 178 233 179 234 /** 235 * @param string $byteword 236 * 237 * @return float|false 238 */ 180 239 public static function LittleEndian2Float($byteword) { 181 240 return self::BigEndian2Float(strrev($byteword)); 182 241 } 183 242 184 243 /** 244 * ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic 245 * 246 * @link http://www.psc.edu/general/software/packages/ieee/ieee.html 247 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html 248 * 249 * @param string $byteword 250 * 251 * @return float|false 252 */ 185 253 public static function BigEndian2Float($byteword) { 186 // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic187 // http://www.psc.edu/general/software/packages/ieee/ieee.html188 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html189 190 254 $bitword = self::BigEndian2Bin($byteword); 191 255 if (!$bitword) { 192 256 return 0; 193 257 } 194 $signbit = $bitword{0}; 258 $signbit = $bitword[0]; 259 $floatvalue = 0; 260 $exponentbits = 0; 261 $fractionbits = 0; 195 262 196 263 switch (strlen($byteword) * 8) { … … 209 276 // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/ 210 277 $exponentstring = substr($bitword, 1, 15); 211 $isnormalized = intval($bitword {16});278 $isnormalized = intval($bitword[16]); 212 279 $fractionstring = substr($bitword, 17, 63); 213 280 $exponent = pow(2, self::Bin2Dec($exponentstring) - 16383); … … 260 327 } 261 328 262 329 /** 330 * @param string $byteword 331 * @param bool $synchsafe 332 * @param bool $signed 333 * 334 * @return int|float|false 335 * @throws Exception 336 */ 263 337 public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) { 264 338 $intvalue = 0; … … 270 344 if ($synchsafe) { // disregard MSB, effectively 7-bit bytes 271 345 //$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems 272 $intvalue += (ord($byteword {$i}) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7);346 $intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7); 273 347 } else { 274 $intvalue += ord($byteword {$i}) * pow(256, ($bytewordlen - 1 - $i));348 $intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i)); 275 349 } 276 350 } … … 289 363 } 290 364 291 365 /** 366 * @param string $byteword 367 * @param bool $signed 368 * 369 * @return int|float|false 370 */ 292 371 public static function LittleEndian2Int($byteword, $signed=false) { 293 372 return self::BigEndian2Int(strrev($byteword), false, $signed); 294 373 } 295 374 375 /** 376 * @param string $byteword 377 * 378 * @return string 379 */ 296 380 public static function LittleEndian2Bin($byteword) { 297 381 return self::BigEndian2Bin(strrev($byteword)); 298 382 } 299 383 384 /** 385 * @param string $byteword 386 * 387 * @return string 388 */ 300 389 public static function BigEndian2Bin($byteword) { 301 390 $binvalue = ''; 302 391 $bytewordlen = strlen($byteword); 303 392 for ($i = 0; $i < $bytewordlen; $i++) { 304 $binvalue .= str_pad(decbin(ord($byteword {$i})), 8, '0', STR_PAD_LEFT);393 $binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT); 305 394 } 306 395 return $binvalue; 307 396 } 308 397 309 398 /** 399 * @param int $number 400 * @param int $minbytes 401 * @param bool $synchsafe 402 * @param bool $signed 403 * 404 * @return string 405 * @throws Exception 406 */ 310 407 public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) { 311 408 if ($number < 0) { … … 328 425 } 329 426 330 427 /** 428 * @param int $number 429 * 430 * @return string 431 */ 331 432 public static function Dec2Bin($number) { 332 433 while ($number >= 256) { … … 342 443 } 343 444 344 445 /** 446 * @param string $binstring 447 * @param bool $signed 448 * 449 * @return int|float 450 */ 345 451 public static function Bin2Dec($binstring, $signed=false) { 346 452 $signmult = 1; 347 453 if ($signed) { 348 if ($binstring {0}== '1') {454 if ($binstring[0] == '1') { 349 455 $signmult = -1; 350 456 } … … 358 464 } 359 465 360 466 /** 467 * @param string $binstring 468 * 469 * @return string 470 */ 361 471 public static function Bin2String($binstring) { 362 472 // return 'hi' for input of '0110100001101001' … … 369 479 } 370 480 371 481 /** 482 * @param int $number 483 * @param int $minbytes 484 * @param bool $synchsafe 485 * 486 * @return string 487 */ 372 488 public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) { 373 489 $intstring = ''; … … 384 500 } 385 501 386 502 /** 503 * @param array $array1 504 * @param array $array2 505 * 506 * @return array|false 507 */ 387 508 public static function array_merge_clobber($array1, $array2) { 388 509 // written by kcØhireability*com … … 402 523 } 403 524 404 525 /** 526 * @param array $array1 527 * @param array $array2 528 * 529 * @return array|false 530 */ 405 531 public static function array_merge_noclobber($array1, $array2) { 406 532 if (!is_array($array1) || !is_array($array2)) { … … 418 544 } 419 545 546 /** 547 * @param array $array1 548 * @param array $array2 549 * 550 * @return array|false|null 551 */ 420 552 public static function flipped_array_merge_noclobber($array1, $array2) { 421 553 if (!is_array($array1) || !is_array($array2)) { … … 432 564 } 433 565 434 566 /** 567 * @param array $theArray 568 * 569 * @return bool 570 */ 435 571 public static function ksort_recursive(&$theArray) { 436 572 ksort($theArray); … … 443 579 } 444 580 581 /** 582 * @param string $filename 583 * @param int $numextensions 584 * 585 * @return string 586 */ 445 587 public static function fileextension($filename, $numextensions=1) { 446 588 if (strstr($filename, '.')) { … … 458 600 } 459 601 460 602 /** 603 * @param int $seconds 604 * 605 * @return string 606 */ 461 607 public static function PlaytimeString($seconds) { 462 608 $sign = (($seconds < 0) ? '-' : ''); … … 468 614 } 469 615 470 616 /** 617 * @param int $macdate 618 * 619 * @return int|float 620 */ 471 621 public static function DateMac2Unix($macdate) { 472 622 // Macintosh timestamp: seconds since 00:00h January 1, 1904 … … 475 625 } 476 626 477 627 /** 628 * @param string $rawdata 629 * 630 * @return float 631 */ 478 632 public static function FixedPoint8_8($rawdata) { 479 633 return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8)); 480 634 } 481 635 482 636 /** 637 * @param string $rawdata 638 * 639 * @return float 640 */ 483 641 public static function FixedPoint16_16($rawdata) { 484 642 return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16)); 485 643 } 486 644 487 645 /** 646 * @param string $rawdata 647 * 648 * @return float 649 */ 488 650 public static function FixedPoint2_30($rawdata) { 489 651 $binarystring = self::BigEndian2Bin($rawdata); … … 492 654 493 655 656 /** 657 * @param string $ArrayPath 658 * @param string $Separator 659 * @param mixed $Value 660 * 661 * @return array 662 */ 494 663 public static function CreateDeepArray($ArrayPath, $Separator, $Value) { 495 664 // assigns $Value to a nested array path: … … 508 677 } 509 678 679 /** 680 * @param array $arraydata 681 * @param bool $returnkey 682 * 683 * @return int|false 684 */ 510 685 public static function array_max($arraydata, $returnkey=false) { 511 686 $maxvalue = false; … … 522 697 } 523 698 699 /** 700 * @param array $arraydata 701 * @param bool $returnkey 702 * 703 * @return int|false 704 */ 524 705 public static function array_min($arraydata, $returnkey=false) { 525 706 $minvalue = false; … … 536 717 } 537 718 719 /** 720 * @param string $XMLstring 721 * 722 * @return array|false 723 */ 538 724 public static function XML2array($XMLstring) { 539 725 if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) { … … 549 735 } 550 736 737 /** 738 * @param SimpleXMLElement|array $XMLobject 739 * 740 * @return array 741 */ 551 742 public static function SimpleXMLelement2array($XMLobject) { 552 743 if (!is_object($XMLobject) && !is_array($XMLobject)) { 553 744 return $XMLobject; 554 745 } 555 $XMLarray = (is_object($XMLobject) ? get_object_vars($XMLobject) : $XMLobject);746 $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject; 556 747 foreach ($XMLarray as $key => $value) { 557 748 $XMLarray[$key] = self::SimpleXMLelement2array($value); … … 560 751 } 561 752 562 563 // Allan Hansen <ahØartemis*dk> 564 // self::md5_data() - returns md5sum for a file from startuing position to absolute end position 753 /** 754 * Returns checksum for a file from starting position to absolute end position. 755 * 756 * @param string $file 757 * @param int $offset 758 * @param int $end 759 * @param string $algorithm 760 * 761 * @return string|false 762 * @throws getid3_exception 763 */ 565 764 public static function hash_data($file, $offset, $end, $algorithm) { 566 static $tempdir = '';567 765 if (!self::intValueSupported($end)) { 568 766 return false; 569 767 } 570 switch ($algorithm) { 571 case 'md5': 572 $hash_function = 'md5_file'; 573 $unix_call = 'md5sum'; 574 $windows_call = 'md5sum.exe'; 575 $hash_length = 32; 576 break; 577 578 case 'sha1': 579 $hash_function = 'sha1_file'; 580 $unix_call = 'sha1sum'; 581 $windows_call = 'sha1sum.exe'; 582 $hash_length = 40; 583 break; 584 585 default: 586 throw new Exception('Invalid algorithm ('.$algorithm.') in self::hash_data()'); 587 break; 588 } 768 if (!in_array($algorithm, array('md5', 'sha1'))) { 769 throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()'); 770 } 771 589 772 $size = $end - $offset; 590 while (true) { 591 if (GETID3_OS_ISWINDOWS) { 592 593 // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data 594 // Fall back to create-temp-file method: 595 if ($algorithm == 'sha1') { 596 break; 597 } 598 599 $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call); 600 foreach ($RequiredFiles as $required_file) { 601 if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) { 602 // helper apps not available - fall back to old method 603 break 2; 604 } 605 } 606 $commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' '.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).' | '; 607 $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | '; 608 $commandline .= GETID3_HELPERAPPSDIR.$windows_call; 609 610 } else { 611 612 $commandline = 'head -c'.$end.' '.escapeshellarg($file).' | '; 613 $commandline .= 'tail -c'.$size.' | '; 614 $commandline .= $unix_call; 615 616 } 617 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) { 618 //throw new Exception('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm'); 619 break; 620 } 621 return substr(`$commandline`, 0, $hash_length); 622 } 623 624 if (empty($tempdir)) { 625 // yes this is ugly, feel free to suggest a better way 626 require_once(dirname(__FILE__).'/getid3.php'); 627 $getid3_temp = new getID3(); 628 $tempdir = $getid3_temp->tempdir; 629 unset($getid3_temp); 630 } 631 // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir 632 if (($data_filename = tempnam($tempdir, 'gI3')) === false) { 633 // can't find anywhere to create a temp file, just fail 634 return false; 635 } 636 637 // Init 638 $result = false; 639 640 // copy parts of file 641 try { 642 self::CopyFileParts($file, $data_filename, $offset, $end - $offset); 643 $result = $hash_function($data_filename); 644 } catch (Exception $e) { 645 throw new Exception('self::CopyFileParts() failed in getid_lib::hash_data(): '.$e->getMessage()); 646 } 647 unlink($data_filename); 648 return $result; 649 } 650 773 774 $fp = fopen($file, 'rb'); 775 fseek($fp, $offset); 776 $ctx = hash_init($algorithm); 777 while ($size > 0) { 778 $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE)); 779 hash_update($ctx, $buffer); 780 $size -= getID3::FREAD_BUFFER_SIZE; 781 } 782 $hash = hash_final($ctx); 783 fclose($fp); 784 785 return $hash; 786 } 787 788 /** 789 * @param string $filename_source 790 * @param string $filename_dest 791 * @param int $offset 792 * @param int $length 793 * 794 * @return bool 795 * @throws Exception 796 * 797 * @deprecated Unused, may be removed in future versions of getID3 798 */ 651 799 public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) { 652 800 if (!self::intValueSupported($offset + $length)) { … … 661 809 $byteslefttowrite -= $byteswritten; 662 810 } 811 fclose($fp_dest); 663 812 return true; 664 813 } else { 814 fclose($fp_src); 665 815 throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source); 666 816 } 667 fclose($fp_dest);668 817 } else { 669 818 throw new Exception('failed to create file for writing '.$filename_dest); 670 819 } 671 fclose($fp_src);672 820 } else { 673 821 throw new Exception('failed to open file for reading '.$filename_source); 674 822 } 675 return false; 676 } 677 823 } 824 825 /** 826 * @param int $charval 827 * 828 * @return string 829 */ 678 830 public static function iconv_fallback_int_utf8($charval) { 679 831 if ($charval < 128) { … … 699 851 } 700 852 701 // ISO-8859-1 => UTF-8 853 /** 854 * ISO-8859-1 => UTF-8 855 * 856 * @param string $string 857 * @param bool $bom 858 * 859 * @return string 860 */ 702 861 public static function iconv_fallback_iso88591_utf8($string, $bom=false) { 703 862 if (function_exists('utf8_encode')) { … … 710 869 } 711 870 for ($i = 0; $i < strlen($string); $i++) { 712 $charval = ord($string {$i});871 $charval = ord($string[$i]); 713 872 $newcharstring .= self::iconv_fallback_int_utf8($charval); 714 873 } … … 716 875 } 717 876 718 // ISO-8859-1 => UTF-16BE 877 /** 878 * ISO-8859-1 => UTF-16BE 879 * 880 * @param string $string 881 * @param bool $bom 882 * 883 * @return string 884 */ 719 885 public static function iconv_fallback_iso88591_utf16be($string, $bom=false) { 720 886 $newcharstring = ''; … … 723 889 } 724 890 for ($i = 0; $i < strlen($string); $i++) { 725 $newcharstring .= "\x00".$string {$i};891 $newcharstring .= "\x00".$string[$i]; 726 892 } 727 893 return $newcharstring; 728 894 } 729 895 730 // ISO-8859-1 => UTF-16LE 896 /** 897 * ISO-8859-1 => UTF-16LE 898 * 899 * @param string $string 900 * @param bool $bom 901 * 902 * @return string 903 */ 731 904 public static function iconv_fallback_iso88591_utf16le($string, $bom=false) { 732 905 $newcharstring = ''; … … 735 908 } 736 909 for ($i = 0; $i < strlen($string); $i++) { 737 $newcharstring .= $string {$i}."\x00";910 $newcharstring .= $string[$i]."\x00"; 738 911 } 739 912 return $newcharstring; 740 913 } 741 914 742 // ISO-8859-1 => UTF-16LE (BOM) 915 /** 916 * ISO-8859-1 => UTF-16LE (BOM) 917 * 918 * @param string $string 919 * 920 * @return string 921 */ 743 922 public static function iconv_fallback_iso88591_utf16($string) { 744 923 return self::iconv_fallback_iso88591_utf16le($string, true); 745 924 } 746 925 747 // UTF-8 => ISO-8859-1 926 /** 927 * UTF-8 => ISO-8859-1 928 * 929 * @param string $string 930 * 931 * @return string 932 */ 748 933 public static function iconv_fallback_utf8_iso88591($string) { 749 934 if (function_exists('utf8_decode')) { … … 755 940 $stringlength = strlen($string); 756 941 while ($offset < $stringlength) { 757 if ((ord($string {$offset}) | 0x07) == 0xF7) {942 if ((ord($string[$offset]) | 0x07) == 0xF7) { 758 943 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 759 $charval = ((ord($string {($offset + 0)}) & 0x07) << 18) &760 ((ord($string {($offset + 1)}) & 0x3F) << 12) &761 ((ord($string {($offset + 2)}) & 0x3F) << 6) &762 (ord($string {($offset + 3)}) & 0x3F);944 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 945 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 946 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 947 (ord($string[($offset + 3)]) & 0x3F); 763 948 $offset += 4; 764 } elseif ((ord($string {$offset}) | 0x0F) == 0xEF) {949 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 765 950 // 1110bbbb 10bbbbbb 10bbbbbb 766 $charval = ((ord($string {($offset + 0)}) & 0x0F) << 12) &767 ((ord($string {($offset + 1)}) & 0x3F) << 6) &768 (ord($string {($offset + 2)}) & 0x3F);951 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 952 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 953 (ord($string[($offset + 2)]) & 0x3F); 769 954 $offset += 3; 770 } elseif ((ord($string {$offset}) | 0x1F) == 0xDF) {955 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 771 956 // 110bbbbb 10bbbbbb 772 $charval = ((ord($string {($offset + 0)}) & 0x1F) << 6) &773 (ord($string {($offset + 1)}) & 0x3F);957 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 958 (ord($string[($offset + 1)]) & 0x3F); 774 959 $offset += 2; 775 } elseif ((ord($string {$offset}) | 0x7F) == 0x7F) {960 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 776 961 // 0bbbbbbb 777 $charval = ord($string {$offset});962 $charval = ord($string[$offset]); 778 963 $offset += 1; 779 964 } else { … … 789 974 } 790 975 791 // UTF-8 => UTF-16BE 976 /** 977 * UTF-8 => UTF-16BE 978 * 979 * @param string $string 980 * @param bool $bom 981 * 982 * @return string 983 */ 792 984 public static function iconv_fallback_utf8_utf16be($string, $bom=false) { 793 985 $newcharstring = ''; … … 798 990 $stringlength = strlen($string); 799 991 while ($offset < $stringlength) { 800 if ((ord($string {$offset}) | 0x07) == 0xF7) {992 if ((ord($string[$offset]) | 0x07) == 0xF7) { 801 993 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 802 $charval = ((ord($string {($offset + 0)}) & 0x07) << 18) &803 ((ord($string {($offset + 1)}) & 0x3F) << 12) &804 ((ord($string {($offset + 2)}) & 0x3F) << 6) &805 (ord($string {($offset + 3)}) & 0x3F);994 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 995 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 996 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 997 (ord($string[($offset + 3)]) & 0x3F); 806 998 $offset += 4; 807 } elseif ((ord($string {$offset}) | 0x0F) == 0xEF) {999 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 808 1000 // 1110bbbb 10bbbbbb 10bbbbbb 809 $charval = ((ord($string {($offset + 0)}) & 0x0F) << 12) &810 ((ord($string {($offset + 1)}) & 0x3F) << 6) &811 (ord($string {($offset + 2)}) & 0x3F);1001 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 1002 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 1003 (ord($string[($offset + 2)]) & 0x3F); 812 1004 $offset += 3; 813 } elseif ((ord($string {$offset}) | 0x1F) == 0xDF) {1005 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 814 1006 // 110bbbbb 10bbbbbb 815 $charval = ((ord($string {($offset + 0)}) & 0x1F) << 6) &816 (ord($string {($offset + 1)}) & 0x3F);1007 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 1008 (ord($string[($offset + 1)]) & 0x3F); 817 1009 $offset += 2; 818 } elseif ((ord($string {$offset}) | 0x7F) == 0x7F) {1010 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 819 1011 // 0bbbbbbb 820 $charval = ord($string {$offset});1012 $charval = ord($string[$offset]); 821 1013 $offset += 1; 822 1014 } else { … … 832 1024 } 833 1025 834 // UTF-8 => UTF-16LE 1026 /** 1027 * UTF-8 => UTF-16LE 1028 * 1029 * @param string $string 1030 * @param bool $bom 1031 * 1032 * @return string 1033 */ 835 1034 public static function iconv_fallback_utf8_utf16le($string, $bom=false) { 836 1035 $newcharstring = ''; … … 841 1040 $stringlength = strlen($string); 842 1041 while ($offset < $stringlength) { 843 if ((ord($string {$offset}) | 0x07) == 0xF7) {1042 if ((ord($string[$offset]) | 0x07) == 0xF7) { 844 1043 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb 845 $charval = ((ord($string {($offset + 0)}) & 0x07) << 18) &846 ((ord($string {($offset + 1)}) & 0x3F) << 12) &847 ((ord($string {($offset + 2)}) & 0x3F) << 6) &848 (ord($string {($offset + 3)}) & 0x3F);1044 $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) & 1045 ((ord($string[($offset + 1)]) & 0x3F) << 12) & 1046 ((ord($string[($offset + 2)]) & 0x3F) << 6) & 1047 (ord($string[($offset + 3)]) & 0x3F); 849 1048 $offset += 4; 850 } elseif ((ord($string {$offset}) | 0x0F) == 0xEF) {1049 } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) { 851 1050 // 1110bbbb 10bbbbbb 10bbbbbb 852 $charval = ((ord($string {($offset + 0)}) & 0x0F) << 12) &853 ((ord($string {($offset + 1)}) & 0x3F) << 6) &854 (ord($string {($offset + 2)}) & 0x3F);1051 $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) & 1052 ((ord($string[($offset + 1)]) & 0x3F) << 6) & 1053 (ord($string[($offset + 2)]) & 0x3F); 855 1054 $offset += 3; 856 } elseif ((ord($string {$offset}) | 0x1F) == 0xDF) {1055 } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) { 857 1056 // 110bbbbb 10bbbbbb 858 $charval = ((ord($string {($offset + 0)}) & 0x1F) << 6) &859 (ord($string {($offset + 1)}) & 0x3F);1057 $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) & 1058 (ord($string[($offset + 1)]) & 0x3F); 860 1059 $offset += 2; 861 } elseif ((ord($string {$offset}) | 0x7F) == 0x7F) {1060 } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) { 862 1061 // 0bbbbbbb 863 $charval = ord($string {$offset});1062 $charval = ord($string[$offset]); 864 1063 $offset += 1; 865 1064 } else { … … 875 1074 } 876 1075 877 // UTF-8 => UTF-16LE (BOM) 1076 /** 1077 * UTF-8 => UTF-16LE (BOM) 1078 * 1079 * @param string $string 1080 * 1081 * @return string 1082 */ 878 1083 public static function iconv_fallback_utf8_utf16($string) { 879 1084 return self::iconv_fallback_utf8_utf16le($string, true); 880 1085 } 881 1086 882 // UTF-16BE => UTF-8 1087 /** 1088 * UTF-16BE => UTF-8 1089 * 1090 * @param string $string 1091 * 1092 * @return string 1093 */ 883 1094 public static function iconv_fallback_utf16be_utf8($string) { 884 1095 if (substr($string, 0, 2) == "\xFE\xFF") { … … 894 1105 } 895 1106 896 // UTF-16LE => UTF-8 1107 /** 1108 * UTF-16LE => UTF-8 1109 * 1110 * @param string $string 1111 * 1112 * @return string 1113 */ 897 1114 public static function iconv_fallback_utf16le_utf8($string) { 898 1115 if (substr($string, 0, 2) == "\xFF\xFE") { … … 908 1125 } 909 1126 910 // UTF-16BE => ISO-8859-1 1127 /** 1128 * UTF-16BE => ISO-8859-1 1129 * 1130 * @param string $string 1131 * 1132 * @return string 1133 */ 911 1134 public static function iconv_fallback_utf16be_iso88591($string) { 912 1135 if (substr($string, 0, 2) == "\xFE\xFF") { … … 922 1145 } 923 1146 924 // UTF-16LE => ISO-8859-1 1147 /** 1148 * UTF-16LE => ISO-8859-1 1149 * 1150 * @param string $string 1151 * 1152 * @return string 1153 */ 925 1154 public static function iconv_fallback_utf16le_iso88591($string) { 926 1155 if (substr($string, 0, 2) == "\xFF\xFE") { … … 936 1165 } 937 1166 938 // UTF-16 (BOM) => ISO-8859-1 1167 /** 1168 * UTF-16 (BOM) => ISO-8859-1 1169 * 1170 * @param string $string 1171 * 1172 * @return string 1173 */ 939 1174 public static function iconv_fallback_utf16_iso88591($string) { 940 1175 $bom = substr($string, 0, 2); … … 947 1182 } 948 1183 949 // UTF-16 (BOM) => UTF-8 1184 /** 1185 * UTF-16 (BOM) => UTF-8 1186 * 1187 * @param string $string 1188 * 1189 * @return string 1190 */ 950 1191 public static function iconv_fallback_utf16_utf8($string) { 951 1192 $bom = substr($string, 0, 2); … … 958 1199 } 959 1200 1201 /** 1202 * @param string $in_charset 1203 * @param string $out_charset 1204 * @param string $string 1205 * 1206 * @return string 1207 * @throws Exception 1208 */ 960 1209 public static function iconv_fallback($in_charset, $out_charset, $string) { 961 1210 … … 964 1213 } 965 1214 966 // mb_convert_encoding() avail ble1215 // mb_convert_encoding() available 967 1216 if (function_exists('mb_convert_encoding')) { 1217 if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) { 1218 // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM 1219 $string = "\xFF\xFE".$string; 1220 } 1221 if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) { 1222 if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) { 1223 // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified 1224 return ''; 1225 } 1226 } 968 1227 if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) { 969 1228 switch ($out_charset) { … … 975 1234 } 976 1235 return $string; 977 } 978 // iconv() avail ble979 elseif (function_exists('iconv')) {1236 1237 // iconv() available 1238 } elseif (function_exists('iconv')) { 980 1239 if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) { 981 1240 switch ($out_charset) { … … 1018 1277 } 1019 1278 1279 /** 1280 * @param mixed $data 1281 * @param string $charset 1282 * 1283 * @return mixed 1284 */ 1020 1285 public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') { 1021 1286 if (is_string($data)) { … … 1032 1297 } 1033 1298 1299 /** 1300 * @param string|int|float $string 1301 * @param string $charset 1302 * 1303 * @return string 1304 */ 1034 1305 public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') { 1035 1306 $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string … … 1070 1341 $strlen = strlen($string); 1071 1342 for ($i = 0; $i < $strlen; $i++) { 1072 $char_ord_val = ord($string {$i});1343 $char_ord_val = ord($string[$i]); 1073 1344 $charval = 0; 1074 1345 if ($char_ord_val < 0x80) { … … 1076 1347 } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) { 1077 1348 $charval = (($char_ord_val & 0x07) << 18); 1078 $charval += ((ord($string {++$i}) & 0x3F) << 12);1079 $charval += ((ord($string {++$i}) & 0x3F) << 6);1080 $charval += (ord($string {++$i}) & 0x3F);1349 $charval += ((ord($string[++$i]) & 0x3F) << 12); 1350 $charval += ((ord($string[++$i]) & 0x3F) << 6); 1351 $charval += (ord($string[++$i]) & 0x3F); 1081 1352 } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) { 1082 1353 $charval = (($char_ord_val & 0x0F) << 12); 1083 $charval += ((ord($string {++$i}) & 0x3F) << 6);1084 $charval += (ord($string {++$i}) & 0x3F);1354 $charval += ((ord($string[++$i]) & 0x3F) << 6); 1355 $charval += (ord($string[++$i]) & 0x3F); 1085 1356 } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) { 1086 1357 $charval = (($char_ord_val & 0x1F) << 6); 1087 $charval += (ord($string {++$i}) & 0x3F);1358 $charval += (ord($string[++$i]) & 0x3F); 1088 1359 } 1089 1360 if (($charval >= 32) && ($charval <= 127)) { … … 1124 1395 } 1125 1396 1126 1127 1397 /** 1398 * @param int $namecode 1399 * 1400 * @return string 1401 */ 1128 1402 public static function RGADnameLookup($namecode) { 1129 1403 static $RGADname = array(); … … 1137 1411 } 1138 1412 1139 1413 /** 1414 * @param int $originatorcode 1415 * 1416 * @return string 1417 */ 1140 1418 public static function RGADoriginatorLookup($originatorcode) { 1141 1419 static $RGADoriginator = array(); … … 1150 1428 } 1151 1429 1152 1430 /** 1431 * @param int $rawadjustment 1432 * @param int $signbit 1433 * 1434 * @return float 1435 */ 1153 1436 public static function RGADadjustmentLookup($rawadjustment, $signbit) { 1154 $adjustment = $rawadjustment / 10;1437 $adjustment = (float) $rawadjustment / 10; 1155 1438 if ($signbit == 1) { 1156 1439 $adjustment *= -1; 1157 1440 } 1158 return (float) $adjustment; 1159 } 1160 1161 1441 return $adjustment; 1442 } 1443 1444 /** 1445 * @param int $namecode 1446 * @param int $originatorcode 1447 * @param int $replaygain 1448 * 1449 * @return string 1450 */ 1162 1451 public static function RGADgainString($namecode, $originatorcode, $replaygain) { 1163 1452 if ($replaygain < 0) { … … 1175 1464 } 1176 1465 1466 /** 1467 * @param float $amplitude 1468 * 1469 * @return float 1470 */ 1177 1471 public static function RGADamplitude2dB($amplitude) { 1178 1472 return 20 * log10($amplitude); 1179 1473 } 1180 1474 1181 1475 /** 1476 * @param string $imgData 1477 * @param array $imageinfo 1478 * 1479 * @return array|false 1480 */ 1182 1481 public static function GetDataImageSize($imgData, &$imageinfo=array()) { 1183 1482 static $tempdir = ''; … … 1214 1513 } 1215 1514 1515 /** 1516 * @param string $mime_type 1517 * 1518 * @return string 1519 */ 1216 1520 public static function ImageExtFromMime($mime_type) { 1217 1521 // temporary way, works OK for now, but should be reworked in the future … … 1219 1523 } 1220 1524 1221 public static function ImageTypesLookup($imagetypeid) { 1222 static $ImageTypesLookup = array(); 1223 if (empty($ImageTypesLookup)) { 1224 $ImageTypesLookup[1] = 'gif'; 1225 $ImageTypesLookup[2] = 'jpeg'; 1226 $ImageTypesLookup[3] = 'png'; 1227 $ImageTypesLookup[4] = 'swf'; 1228 $ImageTypesLookup[5] = 'psd'; 1229 $ImageTypesLookup[6] = 'bmp'; 1230 $ImageTypesLookup[7] = 'tiff (little-endian)'; 1231 $ImageTypesLookup[8] = 'tiff (big-endian)'; 1232 $ImageTypesLookup[9] = 'jpc'; 1233 $ImageTypesLookup[10] = 'jp2'; 1234 $ImageTypesLookup[11] = 'jpx'; 1235 $ImageTypesLookup[12] = 'jb2'; 1236 $ImageTypesLookup[13] = 'swc'; 1237 $ImageTypesLookup[14] = 'iff'; 1238 } 1239 return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : ''); 1240 } 1241 1525 /** 1526 * @param array $ThisFileInfo 1527 * 1528 * @return bool 1529 */ 1242 1530 public static function CopyTagsToComments(&$ThisFileInfo) { 1243 1531 … … 1327 1615 } 1328 1616 1329 1617 /** 1618 * @param string $key 1619 * @param int $begin 1620 * @param int $end 1621 * @param string $file 1622 * @param string $name 1623 * 1624 * @return string 1625 */ 1330 1626 public static function EmbeddedLookup($key, $begin, $end, $file, $name) { 1331 1627 … … 1374 1670 } 1375 1671 1672 /** 1673 * @param string $filename 1674 * @param string $sourcefile 1675 * @param bool $DieOnFailure 1676 * 1677 * @return bool 1678 * @throws Exception 1679 */ 1376 1680 public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) { 1377 1681 global $GETID3_ERRORARRAY; … … 1394 1698 } 1395 1699 1700 /** 1701 * @param string $string 1702 * 1703 * @return string 1704 */ 1396 1705 public static function trimNullByte($string) { 1397 1706 return trim($string, "\x00"); 1398 1707 } 1399 1708 1709 /** 1710 * @param string $path 1711 * 1712 * @return float|bool 1713 */ 1400 1714 public static function getFileSizeSyscall($path) { 1401 1715 $filesize = false; … … 1422 1736 } 1423 1737 1424 1425 /** 1426 * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268) 1427 * @param string $path A path. 1428 * @param string $suffix If the name component ends in suffix this will also be cut off. 1429 * @return string 1430 */ 1738 /** 1739 * @param string $filename 1740 * 1741 * @return string|false 1742 */ 1743 public static function truepath($filename) { 1744 // 2017-11-08: this could use some improvement, patches welcome 1745 if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) { 1746 // PHP's built-in realpath function does not work on UNC Windows shares 1747 $goodpath = array(); 1748 foreach (explode('/', str_replace('\\', '/', $filename)) as $part) { 1749 if ($part == '.') { 1750 continue; 1751 } 1752 if ($part == '..') { 1753 if (count($goodpath)) { 1754 array_pop($goodpath); 1755 } else { 1756 // cannot step above this level, already at top level 1757 return false; 1758 } 1759 } else { 1760 $goodpath[] = $part; 1761 } 1762 } 1763 return implode(DIRECTORY_SEPARATOR, $goodpath); 1764 } 1765 return realpath($filename); 1766 } 1767 1768 /** 1769 * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268) 1770 * 1771 * @param string $path A path. 1772 * @param string $suffix If the name component ends in suffix this will also be cut off. 1773 * 1774 * @return string 1775 */ 1431 1776 public static function mb_basename($path, $suffix = null) { 1432 1777 $splited = preg_split('#/#', rtrim($path, '/ '));
Note: See TracChangeset
for help on using the changeset viewer.