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 //
9 // getid3.lib.php - part of getID3() //
10 // see readme.txt for more details //
12 /////////////////////////////////////////////////////////////////
18 * @param string $string
21 * @param string|bool $htmlencoding
25 public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') {
27 for ($i = 0; $i < strlen($string); $i++
) {
29 $returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT
);
31 $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ?
$string[$i] : 'ยค');
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
41 $returnstring = htmlentities($returnstring, ENT_QUOTES
, $htmlencoding);
47 * Truncates a floating-point number at the decimal point.
49 * @param float $floatnumber
51 * @return float|int returns int (if possible, otherwise float)
53 public static function trunc($floatnumber) {
54 if ($floatnumber >= 1) {
55 $truncatednumber = floor($floatnumber);
56 } elseif ($floatnumber <= -1) {
57 $truncatednumber = ceil($floatnumber);
61 if (self
::intValueSupported($truncatednumber)) {
62 $truncatednumber = (int) $truncatednumber;
64 return $truncatednumber;
68 * @param int|null $variable
69 * @param int $increment
73 public static function safe_inc(&$variable, $increment=1) {
74 if (isset($variable)) {
75 $variable +
= $increment;
77 $variable = $increment;
83 * @param int|float $floatnum
87 public static function CastAsInt($floatnum) {
88 // convert to float if not already
89 $floatnum = (float) $floatnum;
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;
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
);
116 // if integers are 64-bit - no other check required
117 if ($hasINT64 ||
(($num <= PHP_INT_MAX
) && ($num >= PHP_INT_MIN
))) {
124 * @param string $fraction
128 public static function DecimalizeFraction($fraction) {
129 list($numerator, $denominator) = explode('/', $fraction);
130 return $numerator / ($denominator ?
$denominator : 1);
134 * @param string $binarynumerator
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);
145 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
147 * @param string $binarypointnumber
148 * @param int $maxbits
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;
159 while (($binarypointnumber[0] != '1') ||
(substr($binarypointnumber, 1, 1) != '.')) {
160 if (substr($binarypointnumber, 1, 1) == '.') {
162 $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
164 $pointpos = strpos($binarypointnumber, '.');
165 $exponent +
= ($pointpos - 1);
166 $binarypointnumber = str_replace('.', '', $binarypointnumber);
167 $binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1);
170 $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits +
2), $maxbits +
2, '0', STR_PAD_RIGHT
);
171 return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
175 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
177 * @param float $floatvalue
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)) {
188 $pointbitstring .= (string) self
::trunc($floatpart);
189 $floatpart -= self
::trunc($floatpart);
191 $binarypointnumber = decbin($intpart).'.'.$pointbitstring;
192 return $binarypointnumber;
196 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
198 * @param float $floatvalue
201 * @return string|false
203 public static function Float2String($floatvalue, $bits) {
220 if ($floatvalue >= 0) {
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
);
230 return self
::BigEndian2String(self
::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits %
8, false);
234 * @param string $byteword
236 * @return float|false
238 public static function LittleEndian2Float($byteword) {
239 return self
::BigEndian2Float(strrev($byteword));
243 * ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
245 * @link http://www.psc.edu/general/software/packages/ieee/ieee.html
246 * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
248 * @param string $byteword
250 * @return float|false
252 public static function BigEndian2Float($byteword) {
253 $bitword = self
::BigEndian2Bin($byteword);
257 $signbit = $bitword[0];
262 switch (strlen($byteword) * 8) {
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') {
290 $exponentstring = substr($bitword, 1, $exponentbits);
291 $fractionstring = substr($bitword, $exponentbits +
1, $fractionbits);
292 $exponent = self
::Bin2Dec($exponentstring);
293 $fraction = self
::Bin2Dec($fractionstring);
295 if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
298 } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
299 if ($signbit == '1') {
300 $floatvalue = '-infinity';
302 $floatvalue = '+infinity';
304 } elseif (($exponent == 0) && ($fraction == 0)) {
305 if ($signbit == '1') {
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') {
317 } elseif ($exponent != 0) {
318 $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self
::DecimalBinary2Float($fractionstring));
319 if ($signbit == '1') {
323 return (float) $floatvalue;
327 * @param string $byteword
328 * @param bool $synchsafe
329 * @param bool $signed
331 * @return int|float|false
334 public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
336 $bytewordlen = strlen($byteword);
337 if ($bytewordlen == 0) {
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);
345 $intvalue +
= ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i));
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));
356 throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE
).'-bits ('.strlen($byteword).') in self::BigEndian2Int()');
359 return self
::CastAsInt($intvalue);
363 * @param string $byteword
364 * @param bool $signed
366 * @return int|float|false
368 public static function LittleEndian2Int($byteword, $signed=false) {
369 return self
::BigEndian2Int(strrev($byteword), false, $signed);
373 * @param string $byteword
377 public static function LittleEndian2Bin($byteword) {
378 return self
::BigEndian2Bin(strrev($byteword));
382 * @param string $byteword
386 public static function BigEndian2Bin($byteword) {
388 $bytewordlen = strlen($byteword);
389 for ($i = 0; $i < $bytewordlen; $i++
) {
390 $binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT
);
397 * @param int $minbytes
398 * @param bool $synchsafe
399 * @param bool $signed
404 public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
406 throw new Exception('ERROR: self::BigEndian2String() does not support negative numbers');
408 $maskbyte = (($synchsafe ||
$signed) ?
0x7F : 0xFF);
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()');
414 $number = $number & (0x80 << (8 * ($minbytes - 1)));
416 while ($number != 0) {
417 $quotient = ($number / ($maskbyte +
1));
418 $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
419 $number = floor($quotient);
421 return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT
);
429 public static function Dec2Bin($number) {
430 while ($number >= 256) {
431 $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
432 $number = floor($number / 256);
436 for ($i = 0; $i < count($bytes); $i++
) {
437 $binstring = (($i == count($bytes) - 1) ?
decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT
)).$binstring;
443 * @param string $binstring
444 * @param bool $signed
448 public static function Bin2Dec($binstring, $signed=false) {
451 if ($binstring[0] == '1') {
454 $binstring = substr($binstring, 1);
457 for ($i = 0; $i < strlen($binstring); $i++
) {
458 $decvalue +
= ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
460 return self
::CastAsInt($decvalue * $signmult);
464 * @param string $binstring
468 public static function Bin2String($binstring) {
469 // return 'hi' for input of '0110100001101001'
471 $binstringreversed = strrev($binstring);
472 for ($i = 0; $i < strlen($binstringreversed); $i +
= 8) {
473 $string = chr(self
::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
480 * @param int $minbytes
481 * @param bool $synchsafe
485 public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
487 while ($number > 0) {
489 $intstring = $intstring.chr($number & 127);
492 $intstring = $intstring.chr($number & 255);
496 return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT
);
500 * @param mixed $array1
501 * @param mixed $array2
503 * @return array|false
505 public static function array_merge_clobber($array1, $array2) {
506 // written by kcรhireability*com
507 // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
508 if (!is_array($array1) ||
!is_array($array2)) {
512 foreach ($array2 as $key => $val) {
513 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
514 $newarray[$key] = self
::array_merge_clobber($newarray[$key], $val);
516 $newarray[$key] = $val;
523 * @param mixed $array1
524 * @param mixed $array2
526 * @return array|false
528 public static function array_merge_noclobber($array1, $array2) {
529 if (!is_array($array1) ||
!is_array($array2)) {
533 foreach ($array2 as $key => $val) {
534 if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
535 $newarray[$key] = self
::array_merge_noclobber($newarray[$key], $val);
536 } elseif (!isset($newarray[$key])) {
537 $newarray[$key] = $val;
544 * @param mixed $array1
545 * @param mixed $array2
547 * @return array|false|null
549 public static function flipped_array_merge_noclobber($array1, $array2) {
550 if (!is_array($array1) ||
!is_array($array2)) {
553 # naturally, this only works non-recursively
554 $newarray = array_flip($array1);
555 foreach (array_flip($array2) as $key => $val) {
556 if (!isset($newarray[$key])) {
557 $newarray[$key] = count($newarray);
560 return array_flip($newarray);
564 * @param array $theArray
568 public static function ksort_recursive(&$theArray) {
570 foreach ($theArray as $key => $value) {
571 if (is_array($value)) {
572 self
::ksort_recursive($theArray[$key]);
579 * @param string $filename
580 * @param int $numextensions
584 public static function fileextension($filename, $numextensions=1) {
585 if (strstr($filename, '.')) {
586 $reversedfilename = strrev($filename);
588 for ($i = 0; $i < $numextensions; $i++
) {
589 $offset = strpos($reversedfilename, '.', $offset +
1);
590 if ($offset === false) {
594 return strrev(substr($reversedfilename, 0, $offset));
600 * @param int $seconds
604 public static function PlaytimeString($seconds) {
605 $sign = (($seconds < 0) ?
'-' : '');
606 $seconds = round(abs($seconds));
607 $H = (int) floor( $seconds / 3600);
608 $M = (int) floor(($seconds - (3600 * $H) ) / 60);
609 $S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
610 return $sign.($H ?
$H.':' : '').($H ?
str_pad($M, 2, '0', STR_PAD_LEFT
) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT
);
614 * @param int $macdate
618 public static function DateMac2Unix($macdate) {
619 // Macintosh timestamp: seconds since 00:00h January 1, 1904
620 // UNIX timestamp: seconds since 00:00h January 1, 1970
621 return self
::CastAsInt($macdate - 2082844800);
625 * @param string $rawdata
629 public static function FixedPoint8_8($rawdata) {
630 return self
::BigEndian2Int(substr($rawdata, 0, 1)) +
(float) (self
::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
634 * @param string $rawdata
638 public static function FixedPoint16_16($rawdata) {
639 return self
::BigEndian2Int(substr($rawdata, 0, 2)) +
(float) (self
::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
643 * @param string $rawdata
647 public static function FixedPoint2_30($rawdata) {
648 $binarystring = self
::BigEndian2Bin($rawdata);
649 return self
::Bin2Dec(substr($binarystring, 0, 2)) +
(float) (self
::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
654 * @param string $ArrayPath
655 * @param string $Separator
656 * @param mixed $Value
660 public static function CreateDeepArray($ArrayPath, $Separator, $Value) {
661 // assigns $Value to a nested array path:
662 // $foo = self::CreateDeepArray('/path/to/my', '/', 'file.txt')
664 // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
666 // $foo['path']['to']['my'] = 'file.txt';
667 $ArrayPath = ltrim($ArrayPath, $Separator);
668 if (($pos = strpos($ArrayPath, $Separator)) !== false) {
669 $ReturnedArray[substr($ArrayPath, 0, $pos)] = self
::CreateDeepArray(substr($ArrayPath, $pos +
1), $Separator, $Value);
671 $ReturnedArray[$ArrayPath] = $Value;
673 return $ReturnedArray;
677 * @param array $arraydata
678 * @param bool $returnkey
682 public static function array_max($arraydata, $returnkey=false) {
685 foreach ($arraydata as $key => $value) {
686 if (!is_array($value)) {
687 if (($maxvalue === false) ||
($value > $maxvalue)) {
693 return ($returnkey ?
$maxkey : $maxvalue);
697 * @param array $arraydata
698 * @param bool $returnkey
702 public static function array_min($arraydata, $returnkey=false) {
705 foreach ($arraydata as $key => $value) {
706 if (!is_array($value)) {
707 if (($minvalue === false) ||
($value < $minvalue)) {
713 return ($returnkey ?
$minkey : $minvalue);
717 * @param string $XMLstring
719 * @return array|false
721 public static function XML2array($XMLstring) {
722 if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) {
723 // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html
724 // https://core.trac.wordpress.org/changeset/29378
725 $loader = libxml_disable_entity_loader(true);
726 $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT
);
727 $return = self
::SimpleXMLelement2array($XMLobject);
728 libxml_disable_entity_loader($loader);
735 * @param SimpleXMLElement|array|mixed $XMLobject
739 public static function SimpleXMLelement2array($XMLobject) {
740 if (!is_object($XMLobject) && !is_array($XMLobject)) {
743 $XMLarray = $XMLobject instanceof SimpleXMLElement ?
get_object_vars($XMLobject) : $XMLobject;
744 foreach ($XMLarray as $key => $value) {
745 $XMLarray[$key] = self
::SimpleXMLelement2array($value);
751 * Returns checksum for a file from starting position to absolute end position.
753 * @param string $file
756 * @param string $algorithm
758 * @return string|false
759 * @throws getid3_exception
761 public static function hash_data($file, $offset, $end, $algorithm) {
762 if (!self
::intValueSupported($end)) {
765 if (!in_array($algorithm, array('md5', 'sha1'))) {
766 throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
769 $size = $end - $offset;
771 $fp = fopen($file, 'rb');
773 $ctx = hash_init($algorithm);
775 $buffer = fread($fp, min($size, getID3
::FREAD_BUFFER_SIZE
));
776 hash_update($ctx, $buffer);
777 $size -= getID3
::FREAD_BUFFER_SIZE
;
779 $hash = hash_final($ctx);
786 * @param string $filename_source
787 * @param string $filename_dest
794 * @deprecated Unused, may be removed in future versions of getID3
796 public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
797 if (!self
::intValueSupported($offset +
$length)) {
798 throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX
/ 1073741824).'GB limit');
800 if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) {
801 if (($fp_dest = fopen($filename_dest, 'wb'))) {
802 if (fseek($fp_src, $offset) == 0) {
803 $byteslefttowrite = $length;
804 while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3
::FREAD_BUFFER_SIZE
)))) {
805 $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite);
806 $byteslefttowrite -= $byteswritten;
812 throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
815 throw new Exception('failed to create file for writing '.$filename_dest);
818 throw new Exception('failed to open file for reading '.$filename_source);
823 * @param int $charval
827 public static function iconv_fallback_int_utf8($charval) {
828 if ($charval < 128) {
830 $newcharstring = chr($charval);
831 } elseif ($charval < 2048) {
833 $newcharstring = chr(($charval >> 6) |
0xC0);
834 $newcharstring .= chr(($charval & 0x3F) |
0x80);
835 } elseif ($charval < 65536) {
836 // 1110bbbb 10bbbbbb 10bbbbbb
837 $newcharstring = chr(($charval >> 12) |
0xE0);
838 $newcharstring .= chr(($charval >> 6) |
0xC0);
839 $newcharstring .= chr(($charval & 0x3F) |
0x80);
841 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
842 $newcharstring = chr(($charval >> 18) |
0xF0);
843 $newcharstring .= chr(($charval >> 12) |
0xC0);
844 $newcharstring .= chr(($charval >> 6) |
0xC0);
845 $newcharstring .= chr(($charval & 0x3F) |
0x80);
847 return $newcharstring;
851 * ISO-8859-1 => UTF-8
853 * @param string $string
858 public static function iconv_fallback_iso88591_utf8($string, $bom=false) {
859 if (function_exists('utf8_encode')) {
860 return utf8_encode($string);
862 // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
865 $newcharstring .= "\xEF\xBB\xBF";
867 for ($i = 0; $i < strlen($string); $i++
) {
868 $charval = ord($string[$i]);
869 $newcharstring .= self
::iconv_fallback_int_utf8($charval);
871 return $newcharstring;
875 * ISO-8859-1 => UTF-16BE
877 * @param string $string
882 public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
885 $newcharstring .= "\xFE\xFF";
887 for ($i = 0; $i < strlen($string); $i++
) {
888 $newcharstring .= "\x00".$string[$i];
890 return $newcharstring;
894 * ISO-8859-1 => UTF-16LE
896 * @param string $string
901 public static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
904 $newcharstring .= "\xFF\xFE";
906 for ($i = 0; $i < strlen($string); $i++
) {
907 $newcharstring .= $string[$i]."\x00";
909 return $newcharstring;
913 * ISO-8859-1 => UTF-16LE (BOM)
915 * @param string $string
919 public static function iconv_fallback_iso88591_utf16($string) {
920 return self
::iconv_fallback_iso88591_utf16le($string, true);
924 * UTF-8 => ISO-8859-1
926 * @param string $string
930 public static function iconv_fallback_utf8_iso88591($string) {
931 if (function_exists('utf8_decode')) {
932 return utf8_decode($string);
934 // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
937 $stringlength = strlen($string);
938 while ($offset < $stringlength) {
939 if ((ord($string[$offset]) |
0x07) == 0xF7) {
940 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
941 $charval = ((ord($string[($offset +
0)]) & 0x07) << 18) &
942 ((ord($string[($offset +
1)]) & 0x3F) << 12) &
943 ((ord($string[($offset +
2)]) & 0x3F) << 6) &
944 (ord($string[($offset +
3)]) & 0x3F);
946 } elseif ((ord($string[$offset]) |
0x0F) == 0xEF) {
947 // 1110bbbb 10bbbbbb 10bbbbbb
948 $charval = ((ord($string[($offset +
0)]) & 0x0F) << 12) &
949 ((ord($string[($offset +
1)]) & 0x3F) << 6) &
950 (ord($string[($offset +
2)]) & 0x3F);
952 } elseif ((ord($string[$offset]) |
0x1F) == 0xDF) {
954 $charval = ((ord($string[($offset +
0)]) & 0x1F) << 6) &
955 (ord($string[($offset +
1)]) & 0x3F);
957 } elseif ((ord($string[$offset]) |
0x7F) == 0x7F) {
959 $charval = ord($string[$offset]);
962 // error? throw some kind of warning here?
966 if ($charval !== false) {
967 $newcharstring .= (($charval < 256) ?
chr($charval) : '?');
970 return $newcharstring;
976 * @param string $string
981 public static function iconv_fallback_utf8_utf16be($string, $bom=false) {
984 $newcharstring .= "\xFE\xFF";
987 $stringlength = strlen($string);
988 while ($offset < $stringlength) {
989 if ((ord($string[$offset]) |
0x07) == 0xF7) {
990 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
991 $charval = ((ord($string[($offset +
0)]) & 0x07) << 18) &
992 ((ord($string[($offset +
1)]) & 0x3F) << 12) &
993 ((ord($string[($offset +
2)]) & 0x3F) << 6) &
994 (ord($string[($offset +
3)]) & 0x3F);
996 } elseif ((ord($string[$offset]) |
0x0F) == 0xEF) {
997 // 1110bbbb 10bbbbbb 10bbbbbb
998 $charval = ((ord($string[($offset +
0)]) & 0x0F) << 12) &
999 ((ord($string[($offset +
1)]) & 0x3F) << 6) &
1000 (ord($string[($offset +
2)]) & 0x3F);
1002 } elseif ((ord($string[$offset]) |
0x1F) == 0xDF) {
1003 // 110bbbbb 10bbbbbb
1004 $charval = ((ord($string[($offset +
0)]) & 0x1F) << 6) &
1005 (ord($string[($offset +
1)]) & 0x3F);
1007 } elseif ((ord($string[$offset]) |
0x7F) == 0x7F) {
1009 $charval = ord($string[$offset]);
1012 // error? throw some kind of warning here?
1016 if ($charval !== false) {
1017 $newcharstring .= (($charval < 65536) ? self
::BigEndian2String($charval, 2) : "\x00".'?');
1020 return $newcharstring;
1026 * @param string $string
1031 public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
1032 $newcharstring = '';
1034 $newcharstring .= "\xFF\xFE";
1037 $stringlength = strlen($string);
1038 while ($offset < $stringlength) {
1039 if ((ord($string[$offset]) |
0x07) == 0xF7) {
1040 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
1041 $charval = ((ord($string[($offset +
0)]) & 0x07) << 18) &
1042 ((ord($string[($offset +
1)]) & 0x3F) << 12) &
1043 ((ord($string[($offset +
2)]) & 0x3F) << 6) &
1044 (ord($string[($offset +
3)]) & 0x3F);
1046 } elseif ((ord($string[$offset]) |
0x0F) == 0xEF) {
1047 // 1110bbbb 10bbbbbb 10bbbbbb
1048 $charval = ((ord($string[($offset +
0)]) & 0x0F) << 12) &
1049 ((ord($string[($offset +
1)]) & 0x3F) << 6) &
1050 (ord($string[($offset +
2)]) & 0x3F);
1052 } elseif ((ord($string[$offset]) |
0x1F) == 0xDF) {
1053 // 110bbbbb 10bbbbbb
1054 $charval = ((ord($string[($offset +
0)]) & 0x1F) << 6) &
1055 (ord($string[($offset +
1)]) & 0x3F);
1057 } elseif ((ord($string[$offset]) |
0x7F) == 0x7F) {
1059 $charval = ord($string[$offset]);
1062 // error? maybe throw some warning here?
1066 if ($charval !== false) {
1067 $newcharstring .= (($charval < 65536) ? self
::LittleEndian2String($charval, 2) : '?'."\x00");
1070 return $newcharstring;
1074 * UTF-8 => UTF-16LE (BOM)
1076 * @param string $string
1080 public static function iconv_fallback_utf8_utf16($string) {
1081 return self
::iconv_fallback_utf8_utf16le($string, true);
1087 * @param string $string
1091 public static function iconv_fallback_utf16be_utf8($string) {
1092 if (substr($string, 0, 2) == "\xFE\xFF") {
1094 $string = substr($string, 2);
1096 $newcharstring = '';
1097 for ($i = 0; $i < strlen($string); $i +
= 2) {
1098 $charval = self
::BigEndian2Int(substr($string, $i, 2));
1099 $newcharstring .= self
::iconv_fallback_int_utf8($charval);
1101 return $newcharstring;
1107 * @param string $string
1111 public static function iconv_fallback_utf16le_utf8($string) {
1112 if (substr($string, 0, 2) == "\xFF\xFE") {
1114 $string = substr($string, 2);
1116 $newcharstring = '';
1117 for ($i = 0; $i < strlen($string); $i +
= 2) {
1118 $charval = self
::LittleEndian2Int(substr($string, $i, 2));
1119 $newcharstring .= self
::iconv_fallback_int_utf8($charval);
1121 return $newcharstring;
1125 * UTF-16BE => ISO-8859-1
1127 * @param string $string
1131 public static function iconv_fallback_utf16be_iso88591($string) {
1132 if (substr($string, 0, 2) == "\xFE\xFF") {
1134 $string = substr($string, 2);
1136 $newcharstring = '';
1137 for ($i = 0; $i < strlen($string); $i +
= 2) {
1138 $charval = self
::BigEndian2Int(substr($string, $i, 2));
1139 $newcharstring .= (($charval < 256) ?
chr($charval) : '?');
1141 return $newcharstring;
1145 * UTF-16LE => ISO-8859-1
1147 * @param string $string
1151 public static function iconv_fallback_utf16le_iso88591($string) {
1152 if (substr($string, 0, 2) == "\xFF\xFE") {
1154 $string = substr($string, 2);
1156 $newcharstring = '';
1157 for ($i = 0; $i < strlen($string); $i +
= 2) {
1158 $charval = self
::LittleEndian2Int(substr($string, $i, 2));
1159 $newcharstring .= (($charval < 256) ?
chr($charval) : '?');
1161 return $newcharstring;
1165 * UTF-16 (BOM) => ISO-8859-1
1167 * @param string $string
1171 public static function iconv_fallback_utf16_iso88591($string) {
1172 $bom = substr($string, 0, 2);
1173 if ($bom == "\xFE\xFF") {
1174 return self
::iconv_fallback_utf16be_iso88591(substr($string, 2));
1175 } elseif ($bom == "\xFF\xFE") {
1176 return self
::iconv_fallback_utf16le_iso88591(substr($string, 2));
1182 * UTF-16 (BOM) => UTF-8
1184 * @param string $string
1188 public static function iconv_fallback_utf16_utf8($string) {
1189 $bom = substr($string, 0, 2);
1190 if ($bom == "\xFE\xFF") {
1191 return self
::iconv_fallback_utf16be_utf8(substr($string, 2));
1192 } elseif ($bom == "\xFF\xFE") {
1193 return self
::iconv_fallback_utf16le_utf8(substr($string, 2));
1199 * @param string $in_charset
1200 * @param string $out_charset
1201 * @param string $string
1206 public static function iconv_fallback($in_charset, $out_charset, $string) {
1208 if ($in_charset == $out_charset) {
1212 // mb_convert_encoding() available
1213 if (function_exists('mb_convert_encoding')) {
1214 if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) {
1215 // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM
1216 $string = "\xFF\xFE".$string;
1218 if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) {
1219 if (($string == "\xFF\xFE") ||
($string == "\xFE\xFF")) {
1220 // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified
1224 if ($converted_string = @mb_convert_encoding
($string, $out_charset, $in_charset)) {
1225 switch ($out_charset) {
1227 $converted_string = rtrim($converted_string, "\x00");
1230 return $converted_string;
1234 // iconv() available
1235 } elseif (function_exists('iconv')) {
1236 if ($converted_string = @iconv
($in_charset, $out_charset.'//TRANSLIT', $string)) {
1237 switch ($out_charset) {
1239 $converted_string = rtrim($converted_string, "\x00");
1242 return $converted_string;
1245 // iconv() may sometimes fail with "illegal character in input string" error message
1246 // and return an empty string, but returning the unconverted string is more useful
1251 // neither mb_convert_encoding or iconv() is available
1252 static $ConversionFunctionList = array();
1253 if (empty($ConversionFunctionList)) {
1254 $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
1255 $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
1256 $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
1257 $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
1258 $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
1259 $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
1260 $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
1261 $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
1262 $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
1263 $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
1264 $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
1265 $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
1266 $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
1267 $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
1269 if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
1270 $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
1271 return self
::$ConversionFunction($string);
1273 throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
1277 * @param mixed $data
1278 * @param string $charset
1282 public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') {
1283 if (is_string($data)) {
1284 return self
::MultiByteCharString2HTML($data, $charset);
1285 } elseif (is_array($data)) {
1286 $return_data = array();
1287 foreach ($data as $key => $value) {
1288 $return_data[$key] = self
::recursiveMultiByteCharString2HTML($value, $charset);
1290 return $return_data;
1292 // integer, float, objects, resources, etc
1297 * @param string|int|float $string
1298 * @param string $charset
1302 public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
1303 $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
1306 switch (strtolower($charset)) {
1332 case 'windows-1251':
1333 case 'windows-1252':
1334 $HTMLstring = htmlentities($string, ENT_COMPAT
, $charset);
1338 $strlen = strlen($string);
1339 for ($i = 0; $i < $strlen; $i++
) {
1340 $char_ord_val = ord($string[$i]);
1342 if ($char_ord_val < 0x80) {
1343 $charval = $char_ord_val;
1344 } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+
3 < $strlen) {
1345 $charval = (($char_ord_val & 0x07) << 18);
1346 $charval +
= ((ord($string[++
$i]) & 0x3F) << 12);
1347 $charval +
= ((ord($string[++
$i]) & 0x3F) << 6);
1348 $charval +
= (ord($string[++
$i]) & 0x3F);
1349 } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+
2 < $strlen) {
1350 $charval = (($char_ord_val & 0x0F) << 12);
1351 $charval +
= ((ord($string[++
$i]) & 0x3F) << 6);
1352 $charval +
= (ord($string[++
$i]) & 0x3F);
1353 } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+
1 < $strlen) {
1354 $charval = (($char_ord_val & 0x1F) << 6);
1355 $charval +
= (ord($string[++
$i]) & 0x3F);
1357 if (($charval >= 32) && ($charval <= 127)) {
1358 $HTMLstring .= htmlentities(chr($charval));
1360 $HTMLstring .= '&#'.$charval.';';
1366 for ($i = 0; $i < strlen($string); $i +
= 2) {
1367 $charval = self
::LittleEndian2Int(substr($string, $i, 2));
1368 if (($charval >= 32) && ($charval <= 127)) {
1369 $HTMLstring .= chr($charval);
1371 $HTMLstring .= '&#'.$charval.';';
1377 for ($i = 0; $i < strlen($string); $i +
= 2) {
1378 $charval = self
::BigEndian2Int(substr($string, $i, 2));
1379 if (($charval >= 32) && ($charval <= 127)) {
1380 $HTMLstring .= chr($charval);
1382 $HTMLstring .= '&#'.$charval.';';
1388 $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
1395 * @param int $namecode
1399 public static function RGADnameLookup($namecode) {
1400 static $RGADname = array();
1401 if (empty($RGADname)) {
1402 $RGADname[0] = 'not set';
1403 $RGADname[1] = 'Track Gain Adjustment';
1404 $RGADname[2] = 'Album Gain Adjustment';
1407 return (isset($RGADname[$namecode]) ?
$RGADname[$namecode] : '');
1411 * @param int $originatorcode
1415 public static function RGADoriginatorLookup($originatorcode) {
1416 static $RGADoriginator = array();
1417 if (empty($RGADoriginator)) {
1418 $RGADoriginator[0] = 'unspecified';
1419 $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
1420 $RGADoriginator[2] = 'set by user';
1421 $RGADoriginator[3] = 'determined automatically';
1424 return (isset($RGADoriginator[$originatorcode]) ?
$RGADoriginator[$originatorcode] : '');
1428 * @param int $rawadjustment
1429 * @param int $signbit
1433 public static function RGADadjustmentLookup($rawadjustment, $signbit) {
1434 $adjustment = (float) $rawadjustment / 10;
1435 if ($signbit == 1) {
1442 * @param int $namecode
1443 * @param int $originatorcode
1444 * @param int $replaygain
1448 public static function RGADgainString($namecode, $originatorcode, $replaygain) {
1449 if ($replaygain < 0) {
1454 $storedreplaygain = intval(round($replaygain * 10));
1455 $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT
);
1456 $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT
);
1457 $gainstring .= $signbit;
1458 $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT
);
1464 * @param float $amplitude
1468 public static function RGADamplitude2dB($amplitude) {
1469 return 20 * log10($amplitude);
1473 * @param string $imgData
1474 * @param array $imageinfo
1476 * @return array|false
1478 public static function GetDataImageSize($imgData, &$imageinfo=array()) {
1479 if (PHP_VERSION_ID
>= 50400) {
1480 $GetDataImageSize = @getimagesizefromstring
($imgData, $imageinfo);
1481 if ($GetDataImageSize === false ||
!isset($GetDataImageSize[0], $GetDataImageSize[1])) {
1484 $GetDataImageSize['height'] = $GetDataImageSize[0];
1485 $GetDataImageSize['width'] = $GetDataImageSize[1];
1486 return $GetDataImageSize;
1488 static $tempdir = '';
1489 if (empty($tempdir)) {
1490 if (function_exists('sys_get_temp_dir')) {
1491 $tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52
1494 // yes this is ugly, feel free to suggest a better way
1495 if (include_once(dirname(__FILE__
).'/getid3.php')) {
1496 $getid3_temp = new getID3();
1497 if ($getid3_temp_tempdir = $getid3_temp->tempdir
) {
1498 $tempdir = $getid3_temp_tempdir;
1500 unset($getid3_temp, $getid3_temp_tempdir);
1503 $GetDataImageSize = false;
1504 if ($tempfilename = tempnam($tempdir, 'gI3')) {
1505 if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) {
1506 fwrite($tmp, $imgData);
1508 $GetDataImageSize = @getimagesize
($tempfilename, $imageinfo);
1509 if (($GetDataImageSize === false) ||
!isset($GetDataImageSize[0]) ||
!isset($GetDataImageSize[1])) {
1512 $GetDataImageSize['height'] = $GetDataImageSize[0];
1513 $GetDataImageSize['width'] = $GetDataImageSize[1];
1515 unlink($tempfilename);
1517 return $GetDataImageSize;
1521 * @param string $mime_type
1525 public static function ImageExtFromMime($mime_type) {
1526 // temporary way, works OK for now, but should be reworked in the future
1527 return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type);
1531 * @param array $ThisFileInfo
1532 * @param bool $option_tags_html default true (just as in the main getID3 class)
1536 public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) {
1537 // Copy all entries from ['tags'] into common ['comments']
1538 if (!empty($ThisFileInfo['tags'])) {
1539 if (isset($ThisFileInfo['tags']['id3v1'])) {
1540 // bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings
1541 $ID3v1 = $ThisFileInfo['tags']['id3v1'];
1542 unset($ThisFileInfo['tags']['id3v1']);
1543 $ThisFileInfo['tags']['id3v1'] = $ID3v1;
1546 foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
1547 foreach ($tagarray as $tagname => $tagdata) {
1548 foreach ($tagdata as $key => $value) {
1549 if (!empty($value)) {
1550 if (empty($ThisFileInfo['comments'][$tagname])) {
1552 // fall through and append value
1554 } elseif ($tagtype == 'id3v1') {
1556 $newvaluelength = strlen(trim($value));
1557 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
1558 $oldvaluelength = strlen(trim($existingvalue));
1559 if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
1560 // new value is identical but shorter-than (or equal-length to) one already in comments - skip
1564 if (function_exists('mb_convert_encoding')) {
1565 if (trim($value) == trim(substr(mb_convert_encoding($existingvalue, $ThisFileInfo['id3v1']['encoding'], $ThisFileInfo['encoding']), 0, 30))) {
1566 // value stored in ID3v1 appears to be probably the multibyte value transliterated (badly) into ISO-8859-1 in ID3v1.
1567 // 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
1572 } elseif (!is_array($value)) {
1574 $newvaluelength = strlen(trim($value));
1575 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
1576 $oldvaluelength = strlen(trim($existingvalue));
1577 if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
1578 $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
1584 if (is_array($value) ||
empty($ThisFileInfo['comments'][$tagname]) ||
!in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
1585 $value = (is_string($value) ?
trim($value) : $value);
1586 if (!is_int($key) && !ctype_digit($key)) {
1587 $ThisFileInfo['comments'][$tagname][$key] = $value;
1589 if (!isset($ThisFileInfo['comments'][$tagname])) {
1590 $ThisFileInfo['comments'][$tagname] = array($value);
1592 $ThisFileInfo['comments'][$tagname][] = $value;
1601 // attempt to standardize spelling of returned keys
1602 $StandardizeFieldNames = array(
1603 'tracknumber' => 'track_number',
1604 'track' => 'track_number',
1606 foreach ($StandardizeFieldNames as $badkey => $goodkey) {
1607 if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) {
1608 $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey];
1609 unset($ThisFileInfo['comments'][$badkey]);
1613 if ($option_tags_html) {
1614 // Copy ['comments'] to ['comments_html']
1615 if (!empty($ThisFileInfo['comments'])) {
1616 foreach ($ThisFileInfo['comments'] as $field => $values) {
1617 if ($field == 'picture') {
1618 // pictures can take up a lot of space, and we don't need multiple copies of them
1619 // let there be a single copy in [comments][picture], and not elsewhere
1622 foreach ($values as $index => $value) {
1623 if (is_array($value)) {
1624 $ThisFileInfo['comments_html'][$field][$index] = $value;
1626 $ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', self
::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
1638 * @param string $key
1641 * @param string $file
1642 * @param string $name
1646 public static function EmbeddedLookup($key, $begin, $end, $file, $name) {
1650 if (isset($cache[$file][$name])) {
1651 return (isset($cache[$file][$name][$key]) ?
$cache[$file][$name][$key] : '');
1655 $keylength = strlen($key);
1656 $line_count = $end - $begin - 7;
1659 $fp = fopen($file, 'r');
1661 // Discard $begin lines
1662 for ($i = 0; $i < ($begin +
3); $i++
) {
1667 while (0 < $line_count--) {
1670 $line = ltrim(fgets($fp, 1024), "\t ");
1672 // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
1673 //$keycheck = substr($line, 0, $keylength);
1674 //if ($key == $keycheck) {
1675 // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
1679 // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
1680 //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
1681 $explodedLine = explode("\t", $line, 2);
1682 $ThisKey = (isset($explodedLine[0]) ?
$explodedLine[0] : '');
1683 $ThisValue = (isset($explodedLine[1]) ?
$explodedLine[1] : '');
1684 $cache[$file][$name][$ThisKey] = trim($ThisValue);
1689 return (isset($cache[$file][$name][$key]) ?
$cache[$file][$name][$key] : '');
1693 * @param string $filename
1694 * @param string $sourcefile
1695 * @param bool $DieOnFailure
1700 public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
1701 global $GETID3_ERRORARRAY;
1703 if (file_exists($filename)) {
1704 if (include_once($filename)) {
1707 $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
1710 $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
1712 if ($DieOnFailure) {
1713 throw new Exception($diemessage);
1715 $GETID3_ERRORARRAY[] = $diemessage;
1721 * @param string $string
1725 public static function trimNullByte($string) {
1726 return trim($string, "\x00");
1730 * @param string $path
1732 * @return float|bool
1734 public static function getFileSizeSyscall($path) {
1737 if (GETID3_OS_ISWINDOWS
) {
1738 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:
1739 $filesystem = new COM('Scripting.FileSystemObject');
1740 $file = $filesystem->GetFile($path);
1741 $filesize = $file->Size();
1742 unset($filesystem, $file);
1744 $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI';
1747 $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\'';
1749 if (isset($commandline)) {
1750 $output = trim(`
$commandline`
);
1751 if (ctype_digit($output)) {
1752 $filesize = (float) $output;
1759 * @param string $filename
1761 * @return string|false
1763 public static function truepath($filename) {
1764 // 2017-11-08: this could use some improvement, patches welcome
1765 if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) {
1766 // PHP's built-in realpath function does not work on UNC Windows shares
1767 $goodpath = array();
1768 foreach (explode('/', str_replace('\\', '/', $filename)) as $part) {
1772 if ($part == '..') {
1773 if (count($goodpath)) {
1774 array_pop($goodpath);
1776 // cannot step above this level, already at top level
1780 $goodpath[] = $part;
1783 return implode(DIRECTORY_SEPARATOR
, $goodpath);
1785 return realpath($filename);
1789 * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268)
1791 * @param string $path A path.
1792 * @param string $suffix If the name component ends in suffix this will also be cut off.
1796 public static function mb_basename($path, $suffix = null) {
1797 $splited = preg_split('#/#', rtrim($path, '/ '));
1798 return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1);