2
/////////////////////////////////////////////////////////////////
3
/// getID3() by James Heinrich <info@getid3.org> //
4
// available at http://getid3.sourceforge.net //
5
// or http://www.getid3.org //
6
/////////////////////////////////////////////////////////////////
8
// getid3.lib.php - part of getID3() //
9
// See readme.txt for more details //
11
/////////////////////////////////////////////////////////////////
17
static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') {
19
for ($i = 0; $i < strlen($string); $i++) {
21
$returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
23
$returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string{$i}) ? $string{$i} : '�');
29
if (!empty($htmlencoding)) {
30
if ($htmlencoding === true) {
31
$htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean
33
$returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding);
38
static function trunc($floatnumber) {
39
// truncates a floating-point number at the decimal point
40
// returns int (if possible, otherwise float)
41
if ($floatnumber >= 1) {
42
$truncatednumber = floor($floatnumber);
43
} elseif ($floatnumber <= -1) {
44
$truncatednumber = ceil($floatnumber);
48
if (getid3_lib::intValueSupported($truncatednumber)) {
49
$truncatednumber = (int) $truncatednumber;
51
return $truncatednumber;
55
static function safe_inc(&$variable, $increment=1) {
56
if (isset($variable)) {
57
$variable += $increment;
59
$variable = $increment;
64
static function CastAsInt($floatnum) {
65
// convert to float if not already
66
$floatnum = (float) $floatnum;
68
// convert a float to type int, only if possible
69
if (getid3_lib::trunc($floatnum) == $floatnum) {
70
// it's not floating point
71
if (getid3_lib::intValueSupported($floatnum)) {
72
// it's within int range
73
$floatnum = (int) $floatnum;
79
public static function intValueSupported($num) {
80
// check if integers are 64-bit
81
static $hasINT64 = null;
82
if ($hasINT64 === null) { // 10x faster than is_null()
83
$hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1
84
if (!$hasINT64 && !defined('PHP_INT_MIN')) {
85
define('PHP_INT_MIN', ~PHP_INT_MAX);
88
// if integers are 64-bit - no other check required
89
if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) {
95
static function DecimalizeFraction($fraction) {
96
list($numerator, $denominator) = explode('/', $fraction);
97
return $numerator / ($denominator ? $denominator : 1);
101
static function DecimalBinary2Float($binarynumerator) {
102
$numerator = getid3_lib::Bin2Dec($binarynumerator);
103
$denominator = getid3_lib::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
104
return ($numerator / $denominator);
108
static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
109
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
110
if (strpos($binarypointnumber, '.') === false) {
111
$binarypointnumber = '0.'.$binarypointnumber;
112
} elseif ($binarypointnumber{0} == '.') {
113
$binarypointnumber = '0'.$binarypointnumber;
116
while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
117
if (substr($binarypointnumber, 1, 1) == '.') {
119
$binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
121
$pointpos = strpos($binarypointnumber, '.');
122
$exponent += ($pointpos - 1);
123
$binarypointnumber = str_replace('.', '', $binarypointnumber);
124
$binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
127
$binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
128
return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
132
static function Float2BinaryDecimal($floatvalue) {
133
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
134
$maxbits = 128; // to how many bits of precision should the calculations be taken?
135
$intpart = getid3_lib::trunc($floatvalue);
136
$floatpart = abs($floatvalue - $intpart);
137
$pointbitstring = '';
138
while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
140
$pointbitstring .= (string) getid3_lib::trunc($floatpart);
141
$floatpart -= getid3_lib::trunc($floatpart);
143
$binarypointnumber = decbin($intpart).'.'.$pointbitstring;
144
return $binarypointnumber;
148
static function Float2String($floatvalue, $bits) {
149
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
165
if ($floatvalue >= 0) {
170
$normalizedbinary = getid3_lib::NormalizeBinaryPoint(getid3_lib::Float2BinaryDecimal($floatvalue), $fractionbits);
171
$biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
172
$exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
173
$fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
175
return getid3_lib::BigEndian2String(getid3_lib::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
179
static function LittleEndian2Float($byteword) {
180
return getid3_lib::BigEndian2Float(strrev($byteword));
184
static function BigEndian2Float($byteword) {
185
// ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
186
// http://www.psc.edu/general/software/packages/ieee/ieee.html
187
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
189
$bitword = getid3_lib::BigEndian2Bin($byteword);
193
$signbit = $bitword{0};
195
switch (strlen($byteword) * 8) {
207
// 80-bit Apple SANE format
208
// http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
209
$exponentstring = substr($bitword, 1, 15);
210
$isnormalized = intval($bitword{16});
211
$fractionstring = substr($bitword, 17, 63);
212
$exponent = pow(2, getid3_lib::Bin2Dec($exponentstring) - 16383);
213
$fraction = $isnormalized + getid3_lib::DecimalBinary2Float($fractionstring);
214
$floatvalue = $exponent * $fraction;
215
if ($signbit == '1') {
225
$exponentstring = substr($bitword, 1, $exponentbits);
226
$fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
227
$exponent = getid3_lib::Bin2Dec($exponentstring);
228
$fraction = getid3_lib::Bin2Dec($fractionstring);
230
if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
233
} elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
234
if ($signbit == '1') {
235
$floatvalue = '-infinity';
237
$floatvalue = '+infinity';
239
} elseif (($exponent == 0) && ($fraction == 0)) {
240
if ($signbit == '1') {
245
$floatvalue = ($signbit ? 0 : -0);
246
} elseif (($exponent == 0) && ($fraction != 0)) {
247
// These are 'unnormalized' values
248
$floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fractionstring);
249
if ($signbit == '1') {
252
} elseif ($exponent != 0) {
253
$floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fractionstring));
254
if ($signbit == '1') {
258
return (float) $floatvalue;
262
static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
264
$bytewordlen = strlen($byteword);
265
if ($bytewordlen == 0) {
268
for ($i = 0; $i < $bytewordlen; $i++) {
269
if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
270
//$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems
271
$intvalue += (ord($byteword{$i}) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7);
273
$intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
276
if ($signed && !$synchsafe) {
277
// synchsafe ints are not allowed to be signed
278
if ($bytewordlen <= PHP_INT_SIZE) {
279
$signMaskBit = 0x80 << (8 * ($bytewordlen - 1));
280
if ($intvalue & $signMaskBit) {
281
$intvalue = 0 - ($intvalue & ($signMaskBit - 1));
284
throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in getid3_lib::BigEndian2Int()');
288
return getid3_lib::CastAsInt($intvalue);
292
static function LittleEndian2Int($byteword, $signed=false) {
293
return getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
297
static function BigEndian2Bin($byteword) {
299
$bytewordlen = strlen($byteword);
300
for ($i = 0; $i < $bytewordlen; $i++) {
301
$binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
307
static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
309
throw new Exception('ERROR: getid3_lib::BigEndian2String() does not support negative numbers');
311
$maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
314
if ($minbytes > PHP_INT_SIZE) {
315
throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in getid3_lib::BigEndian2String()');
317
$number = $number & (0x80 << (8 * ($minbytes - 1)));
319
while ($number != 0) {
320
$quotient = ($number / ($maskbyte + 1));
321
$intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
322
$number = floor($quotient);
324
return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
328
static function Dec2Bin($number) {
329
while ($number >= 256) {
330
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
331
$number = floor($number / 256);
335
for ($i = 0; $i < count($bytes); $i++) {
336
$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
342
static function Bin2Dec($binstring, $signed=false) {
345
if ($binstring{0} == '1') {
348
$binstring = substr($binstring, 1);
351
for ($i = 0; $i < strlen($binstring); $i++) {
352
$decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
354
return getid3_lib::CastAsInt($decvalue * $signmult);
358
static function Bin2String($binstring) {
359
// return 'hi' for input of '0110100001101001'
361
$binstringreversed = strrev($binstring);
362
for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
363
$string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
369
static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
371
while ($number > 0) {
373
$intstring = $intstring.chr($number & 127);
376
$intstring = $intstring.chr($number & 255);
380
return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
384
static function array_merge_clobber($array1, $array2) {
385
// written by kc�hireability*com
386
// taken from http://www.php.net/manual/en/function.array-merge-recursive.php
387
if (!is_array($array1) || !is_array($array2)) {
391
foreach ($array2 as $key => $val) {
392
if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
393
$newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
395
$newarray[$key] = $val;
402
static function array_merge_noclobber($array1, $array2) {
403
if (!is_array($array1) || !is_array($array2)) {
407
foreach ($array2 as $key => $val) {
408
if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
409
$newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
410
} elseif (!isset($newarray[$key])) {
411
$newarray[$key] = $val;
418
static function ksort_recursive(&$theArray) {
420
foreach ($theArray as $key => $value) {
421
if (is_array($value)) {
422
self::ksort_recursive($theArray[$key]);
428
static function fileextension($filename, $numextensions=1) {
429
if (strstr($filename, '.')) {
430
$reversedfilename = strrev($filename);
432
for ($i = 0; $i < $numextensions; $i++) {
433
$offset = strpos($reversedfilename, '.', $offset + 1);
434
if ($offset === false) {
438
return strrev(substr($reversedfilename, 0, $offset));
444
static function PlaytimeString($seconds) {
445
$sign = (($seconds < 0) ? '-' : '');
446
$seconds = abs($seconds);
447
$H = floor( $seconds / 3600);
448
$M = floor(($seconds - (3600 * $H) ) / 60);
449
$S = round( $seconds - (3600 * $H) - (60 * $M) );
450
return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT);
454
static function DateMac2Unix($macdate) {
455
// Macintosh timestamp: seconds since 00:00h January 1, 1904
456
// UNIX timestamp: seconds since 00:00h January 1, 1970
457
return getid3_lib::CastAsInt($macdate - 2082844800);
461
static function FixedPoint8_8($rawdata) {
462
return getid3_lib::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
466
static function FixedPoint16_16($rawdata) {
467
return getid3_lib::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
471
static function FixedPoint2_30($rawdata) {
472
$binarystring = getid3_lib::BigEndian2Bin($rawdata);
473
return getid3_lib::Bin2Dec(substr($binarystring, 0, 2)) + (float) (getid3_lib::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
477
static function CreateDeepArray($ArrayPath, $Separator, $Value) {
478
// assigns $Value to a nested array path:
479
// $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
481
// $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
483
// $foo['path']['to']['my'] = 'file.txt';
484
while ($ArrayPath && ($ArrayPath{0} == $Separator)) {
485
$ArrayPath = substr($ArrayPath, 1);
487
if (($pos = strpos($ArrayPath, $Separator)) !== false) {
488
$ReturnedArray[substr($ArrayPath, 0, $pos)] = getid3_lib::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
490
$ReturnedArray[$ArrayPath] = $Value;
492
return $ReturnedArray;
495
static function array_max($arraydata, $returnkey=false) {
498
foreach ($arraydata as $key => $value) {
499
if (!is_array($value)) {
500
if ($value > $maxvalue) {
506
return ($returnkey ? $maxkey : $maxvalue);
509
static function array_min($arraydata, $returnkey=false) {
512
foreach ($arraydata as $key => $value) {
513
if (!is_array($value)) {
514
if ($value > $minvalue) {
520
return ($returnkey ? $minkey : $minvalue);
523
static function XML2array($XMLstring) {
524
if (function_exists('simplexml_load_string')) {
525
if (function_exists('get_object_vars')) {
526
$XMLobject = simplexml_load_string($XMLstring);
527
return self::SimpleXMLelement2array($XMLobject);
533
static function SimpleXMLelement2array($XMLobject) {
534
if (!is_object($XMLobject) && !is_array($XMLobject)) {
537
$XMLarray = (is_object($XMLobject) ? get_object_vars($XMLobject) : $XMLobject);
538
foreach ($XMLarray as $key => $value) {
539
$XMLarray[$key] = self::SimpleXMLelement2array($value);
545
// Allan Hansen <ah�artemis*dk>
546
// getid3_lib::md5_data() - returns md5sum for a file from startuing position to absolute end position
547
static function hash_data($file, $offset, $end, $algorithm) {
548
static $tempdir = '';
549
if (!getid3_lib::intValueSupported($end)) {
552
switch ($algorithm) {
554
$hash_function = 'md5_file';
555
$unix_call = 'md5sum';
556
$windows_call = 'md5sum.exe';
561
$hash_function = 'sha1_file';
562
$unix_call = 'sha1sum';
563
$windows_call = 'sha1sum.exe';
568
throw new Exception('Invalid algorithm ('.$algorithm.') in getid3_lib::hash_data()');
571
$size = $end - $offset;
573
if (GETID3_OS_ISWINDOWS) {
575
// It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
576
// Fall back to create-temp-file method:
577
if ($algorithm == 'sha1') {
581
$RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
582
foreach ($RequiredFiles as $required_file) {
583
if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
584
// helper apps not available - fall back to old method
588
$commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' "'.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).'" | ';
589
$commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
590
$commandline .= GETID3_HELPERAPPSDIR.$windows_call;
594
$commandline = 'head -c'.$end.' '.escapeshellarg($file).' | ';
595
$commandline .= 'tail -c'.$size.' | ';
596
$commandline .= $unix_call;
599
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
600
//throw new Exception('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm');
603
return substr(`$commandline`, 0, $hash_length);
606
if (empty($tempdir)) {
607
// yes this is ugly, feel free to suggest a better way
608
require_once(dirname(__FILE__).'/getid3.php');
609
$getid3_temp = new getID3();
610
$tempdir = $getid3_temp->tempdir;
613
// try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
614
if (($data_filename = tempnam($tempdir, 'gI3')) === false) {
615
// can't find anywhere to create a temp file, just fail
622
// copy parts of file
624
getid3_lib::CopyFileParts($file, $data_filename, $offset, $end - $offset);
625
$result = $hash_function($data_filename);
626
} catch (Exception $e) {
627
throw new Exception('getid3_lib::CopyFileParts() failed in getid_lib::hash_data(): '.$e->getMessage());
629
unlink($data_filename);
633
static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
634
if (!getid3_lib::intValueSupported($offset + $length)) {
635
throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit');
637
if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) {
638
if (($fp_dest = fopen($filename_dest, 'wb'))) {
639
if (fseek($fp_src, $offset, SEEK_SET) == 0) {
640
$byteslefttowrite = $length;
641
while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) {
642
$byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite);
643
$byteslefttowrite -= $byteswritten;
647
throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
651
throw new Exception('failed to create file for writing '.$filename_dest);
655
throw new Exception('failed to open file for reading '.$filename_source);
660
static function iconv_fallback_int_utf8($charval) {
661
if ($charval < 128) {
663
$newcharstring = chr($charval);
664
} elseif ($charval < 2048) {
666
$newcharstring = chr(($charval >> 6) | 0xC0);
667
$newcharstring .= chr(($charval & 0x3F) | 0x80);
668
} elseif ($charval < 65536) {
669
// 1110bbbb 10bbbbbb 10bbbbbb
670
$newcharstring = chr(($charval >> 12) | 0xE0);
671
$newcharstring .= chr(($charval >> 6) | 0xC0);
672
$newcharstring .= chr(($charval & 0x3F) | 0x80);
674
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
675
$newcharstring = chr(($charval >> 18) | 0xF0);
676
$newcharstring .= chr(($charval >> 12) | 0xC0);
677
$newcharstring .= chr(($charval >> 6) | 0xC0);
678
$newcharstring .= chr(($charval & 0x3F) | 0x80);
680
return $newcharstring;
683
// ISO-8859-1 => UTF-8
684
static function iconv_fallback_iso88591_utf8($string, $bom=false) {
685
if (function_exists('utf8_encode')) {
686
return utf8_encode($string);
688
// utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
691
$newcharstring .= "\xEF\xBB\xBF";
693
for ($i = 0; $i < strlen($string); $i++) {
694
$charval = ord($string{$i});
695
$newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
697
return $newcharstring;
700
// ISO-8859-1 => UTF-16BE
701
static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
704
$newcharstring .= "\xFE\xFF";
706
for ($i = 0; $i < strlen($string); $i++) {
707
$newcharstring .= "\x00".$string{$i};
709
return $newcharstring;
712
// ISO-8859-1 => UTF-16LE
713
static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
716
$newcharstring .= "\xFF\xFE";
718
for ($i = 0; $i < strlen($string); $i++) {
719
$newcharstring .= $string{$i}."\x00";
721
return $newcharstring;
724
// ISO-8859-1 => UTF-16LE (BOM)
725
static function iconv_fallback_iso88591_utf16($string) {
726
return getid3_lib::iconv_fallback_iso88591_utf16le($string, true);
729
// UTF-8 => ISO-8859-1
730
static function iconv_fallback_utf8_iso88591($string) {
731
if (function_exists('utf8_decode')) {
732
return utf8_decode($string);
734
// utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
737
$stringlength = strlen($string);
738
while ($offset < $stringlength) {
739
if ((ord($string{$offset}) | 0x07) == 0xF7) {
740
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
741
$charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
742
((ord($string{($offset + 1)}) & 0x3F) << 12) &
743
((ord($string{($offset + 2)}) & 0x3F) << 6) &
744
(ord($string{($offset + 3)}) & 0x3F);
746
} elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
747
// 1110bbbb 10bbbbbb 10bbbbbb
748
$charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
749
((ord($string{($offset + 1)}) & 0x3F) << 6) &
750
(ord($string{($offset + 2)}) & 0x3F);
752
} elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
754
$charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
755
(ord($string{($offset + 1)}) & 0x3F);
757
} elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
759
$charval = ord($string{$offset});
762
// error? throw some kind of warning here?
766
if ($charval !== false) {
767
$newcharstring .= (($charval < 256) ? chr($charval) : '?');
770
return $newcharstring;
774
static function iconv_fallback_utf8_utf16be($string, $bom=false) {
777
$newcharstring .= "\xFE\xFF";
780
$stringlength = strlen($string);
781
while ($offset < $stringlength) {
782
if ((ord($string{$offset}) | 0x07) == 0xF7) {
783
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
784
$charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
785
((ord($string{($offset + 1)}) & 0x3F) << 12) &
786
((ord($string{($offset + 2)}) & 0x3F) << 6) &
787
(ord($string{($offset + 3)}) & 0x3F);
789
} elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
790
// 1110bbbb 10bbbbbb 10bbbbbb
791
$charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
792
((ord($string{($offset + 1)}) & 0x3F) << 6) &
793
(ord($string{($offset + 2)}) & 0x3F);
795
} elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
797
$charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
798
(ord($string{($offset + 1)}) & 0x3F);
800
} elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
802
$charval = ord($string{$offset});
805
// error? throw some kind of warning here?
809
if ($charval !== false) {
810
$newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
813
return $newcharstring;
817
static function iconv_fallback_utf8_utf16le($string, $bom=false) {
820
$newcharstring .= "\xFF\xFE";
823
$stringlength = strlen($string);
824
while ($offset < $stringlength) {
825
if ((ord($string{$offset}) | 0x07) == 0xF7) {
826
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
827
$charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
828
((ord($string{($offset + 1)}) & 0x3F) << 12) &
829
((ord($string{($offset + 2)}) & 0x3F) << 6) &
830
(ord($string{($offset + 3)}) & 0x3F);
832
} elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
833
// 1110bbbb 10bbbbbb 10bbbbbb
834
$charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
835
((ord($string{($offset + 1)}) & 0x3F) << 6) &
836
(ord($string{($offset + 2)}) & 0x3F);
838
} elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
840
$charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
841
(ord($string{($offset + 1)}) & 0x3F);
843
} elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
845
$charval = ord($string{$offset});
848
// error? maybe throw some warning here?
852
if ($charval !== false) {
853
$newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
856
return $newcharstring;
859
// UTF-8 => UTF-16LE (BOM)
860
static function iconv_fallback_utf8_utf16($string) {
861
return getid3_lib::iconv_fallback_utf8_utf16le($string, true);
865
static function iconv_fallback_utf16be_utf8($string) {
866
if (substr($string, 0, 2) == "\xFE\xFF") {
868
$string = substr($string, 2);
871
for ($i = 0; $i < strlen($string); $i += 2) {
872
$charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
873
$newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
875
return $newcharstring;
879
static function iconv_fallback_utf16le_utf8($string) {
880
if (substr($string, 0, 2) == "\xFF\xFE") {
882
$string = substr($string, 2);
885
for ($i = 0; $i < strlen($string); $i += 2) {
886
$charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
887
$newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
889
return $newcharstring;
892
// UTF-16BE => ISO-8859-1
893
static function iconv_fallback_utf16be_iso88591($string) {
894
if (substr($string, 0, 2) == "\xFE\xFF") {
896
$string = substr($string, 2);
899
for ($i = 0; $i < strlen($string); $i += 2) {
900
$charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
901
$newcharstring .= (($charval < 256) ? chr($charval) : '?');
903
return $newcharstring;
906
// UTF-16LE => ISO-8859-1
907
static function iconv_fallback_utf16le_iso88591($string) {
908
if (substr($string, 0, 2) == "\xFF\xFE") {
910
$string = substr($string, 2);
913
for ($i = 0; $i < strlen($string); $i += 2) {
914
$charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
915
$newcharstring .= (($charval < 256) ? chr($charval) : '?');
917
return $newcharstring;
920
// UTF-16 (BOM) => ISO-8859-1
921
static function iconv_fallback_utf16_iso88591($string) {
922
$bom = substr($string, 0, 2);
923
if ($bom == "\xFE\xFF") {
924
return getid3_lib::iconv_fallback_utf16be_iso88591(substr($string, 2));
925
} elseif ($bom == "\xFF\xFE") {
926
return getid3_lib::iconv_fallback_utf16le_iso88591(substr($string, 2));
931
// UTF-16 (BOM) => UTF-8
932
static function iconv_fallback_utf16_utf8($string) {
933
$bom = substr($string, 0, 2);
934
if ($bom == "\xFE\xFF") {
935
return getid3_lib::iconv_fallback_utf16be_utf8(substr($string, 2));
936
} elseif ($bom == "\xFF\xFE") {
937
return getid3_lib::iconv_fallback_utf16le_utf8(substr($string, 2));
942
static function iconv_fallback($in_charset, $out_charset, $string) {
944
if ($in_charset == $out_charset) {
949
if (function_exists('iconv')) {
950
if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
951
switch ($out_charset) {
953
$converted_string = rtrim($converted_string, "\x00");
956
return $converted_string;
959
// iconv() may sometimes fail with "illegal character in input string" error message
960
// and return an empty string, but returning the unconverted string is more useful
965
// iconv() not available
966
static $ConversionFunctionList = array();
967
if (empty($ConversionFunctionList)) {
968
$ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
969
$ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
970
$ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
971
$ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
972
$ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
973
$ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
974
$ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
975
$ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
976
$ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
977
$ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
978
$ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
979
$ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
980
$ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
981
$ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
983
if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
984
$ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
985
return getid3_lib::$ConversionFunction($string);
987
throw new Exception('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
991
static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
992
$string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
1021
case 'Windows-1251':
1022
case 'Windows-1252':
1023
$HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
1027
$strlen = strlen($string);
1028
for ($i = 0; $i < $strlen; $i++) {
1029
$char_ord_val = ord($string{$i});
1031
if ($char_ord_val < 0x80) {
1032
$charval = $char_ord_val;
1033
} elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) {
1034
$charval = (($char_ord_val & 0x07) << 18);
1035
$charval += ((ord($string{++$i}) & 0x3F) << 12);
1036
$charval += ((ord($string{++$i}) & 0x3F) << 6);
1037
$charval += (ord($string{++$i}) & 0x3F);
1038
} elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) {
1039
$charval = (($char_ord_val & 0x0F) << 12);
1040
$charval += ((ord($string{++$i}) & 0x3F) << 6);
1041
$charval += (ord($string{++$i}) & 0x3F);
1042
} elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) {
1043
$charval = (($char_ord_val & 0x1F) << 6);
1044
$charval += (ord($string{++$i}) & 0x3F);
1046
if (($charval >= 32) && ($charval <= 127)) {
1047
$HTMLstring .= htmlentities(chr($charval));
1049
$HTMLstring .= '&#'.$charval.';';
1055
for ($i = 0; $i < strlen($string); $i += 2) {
1056
$charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
1057
if (($charval >= 32) && ($charval <= 127)) {
1058
$HTMLstring .= chr($charval);
1060
$HTMLstring .= '&#'.$charval.';';
1066
for ($i = 0; $i < strlen($string); $i += 2) {
1067
$charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
1068
if (($charval >= 32) && ($charval <= 127)) {
1069
$HTMLstring .= chr($charval);
1071
$HTMLstring .= '&#'.$charval.';';
1077
$HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
1085
static function RGADnameLookup($namecode) {
1086
static $RGADname = array();
1087
if (empty($RGADname)) {
1088
$RGADname[0] = 'not set';
1089
$RGADname[1] = 'Track Gain Adjustment';
1090
$RGADname[2] = 'Album Gain Adjustment';
1093
return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
1097
static function RGADoriginatorLookup($originatorcode) {
1098
static $RGADoriginator = array();
1099
if (empty($RGADoriginator)) {
1100
$RGADoriginator[0] = 'unspecified';
1101
$RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
1102
$RGADoriginator[2] = 'set by user';
1103
$RGADoriginator[3] = 'determined automatically';
1106
return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
1110
static function RGADadjustmentLookup($rawadjustment, $signbit) {
1111
$adjustment = $rawadjustment / 10;
1112
if ($signbit == 1) {
1115
return (float) $adjustment;
1119
static function RGADgainString($namecode, $originatorcode, $replaygain) {
1120
if ($replaygain < 0) {
1125
$storedreplaygain = intval(round($replaygain * 10));
1126
$gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
1127
$gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
1128
$gainstring .= $signbit;
1129
$gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
1134
static function RGADamplitude2dB($amplitude) {
1135
return 20 * log10($amplitude);
1139
static function GetDataImageSize($imgData, &$imageinfo) {
1140
static $tempdir = '';
1141
if (empty($tempdir)) {
1142
// yes this is ugly, feel free to suggest a better way
1143
require_once(dirname(__FILE__).'/getid3.php');
1144
$getid3_temp = new getID3();
1145
$tempdir = $getid3_temp->tempdir;
1146
unset($getid3_temp);
1148
$GetDataImageSize = false;
1149
if ($tempfilename = tempnam($tempdir, 'gI3')) {
1150
if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) {
1151
fwrite($tmp, $imgData);
1153
$GetDataImageSize = @GetImageSize($tempfilename, $imageinfo);
1155
unlink($tempfilename);
1157
return $GetDataImageSize;
1160
static function ImageTypesLookup($imagetypeid) {
1161
static $ImageTypesLookup = array();
1162
if (empty($ImageTypesLookup)) {
1163
$ImageTypesLookup[1] = 'gif';
1164
$ImageTypesLookup[2] = 'jpeg';
1165
$ImageTypesLookup[3] = 'png';
1166
$ImageTypesLookup[4] = 'swf';
1167
$ImageTypesLookup[5] = 'psd';
1168
$ImageTypesLookup[6] = 'bmp';
1169
$ImageTypesLookup[7] = 'tiff (little-endian)';
1170
$ImageTypesLookup[8] = 'tiff (big-endian)';
1171
$ImageTypesLookup[9] = 'jpc';
1172
$ImageTypesLookup[10] = 'jp2';
1173
$ImageTypesLookup[11] = 'jpx';
1174
$ImageTypesLookup[12] = 'jb2';
1175
$ImageTypesLookup[13] = 'swc';
1176
$ImageTypesLookup[14] = 'iff';
1178
return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
1181
static function CopyTagsToComments(&$ThisFileInfo) {
1183
// Copy all entries from ['tags'] into common ['comments']
1184
if (!empty($ThisFileInfo['tags'])) {
1185
foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
1186
foreach ($tagarray as $tagname => $tagdata) {
1187
foreach ($tagdata as $key => $value) {
1188
if (!empty($value)) {
1189
if (empty($ThisFileInfo['comments'][$tagname])) {
1191
// fall through and append value
1193
} elseif ($tagtype == 'id3v1') {
1195
$newvaluelength = strlen(trim($value));
1196
foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
1197
$oldvaluelength = strlen(trim($existingvalue));
1198
if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
1199
// new value is identical but shorter-than (or equal-length to) one already in comments - skip
1204
} elseif (!is_array($value)) {
1206
$newvaluelength = strlen(trim($value));
1207
foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
1208
$oldvaluelength = strlen(trim($existingvalue));
1209
if (($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
1210
$ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
1216
if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
1217
$value = (is_string($value) ? trim($value) : $value);
1218
$ThisFileInfo['comments'][$tagname][] = $value;
1225
// Copy to ['comments_html']
1226
foreach ($ThisFileInfo['comments'] as $field => $values) {
1227
if ($field == 'picture') {
1228
// pictures can take up a lot of space, and we don't need multiple copies of them
1229
// let there be a single copy in [comments][picture], and not elsewhere
1232
foreach ($values as $index => $value) {
1233
if (is_array($value)) {
1234
$ThisFileInfo['comments_html'][$field][$index] = $value;
1236
$ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', getid3_lib::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
1245
static function EmbeddedLookup($key, $begin, $end, $file, $name) {
1249
if (isset($cache[$file][$name])) {
1250
return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
1254
$keylength = strlen($key);
1255
$line_count = $end - $begin - 7;
1258
$fp = fopen($file, 'r');
1260
// Discard $begin lines
1261
for ($i = 0; $i < ($begin + 3); $i++) {
1266
while (0 < $line_count--) {
1269
$line = ltrim(fgets($fp, 1024), "\t ");
1271
// METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
1272
//$keycheck = substr($line, 0, $keylength);
1273
//if ($key == $keycheck) {
1274
// $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
1278
// METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
1279
//$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
1280
$explodedLine = explode("\t", $line, 2);
1281
$ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : '');
1282
$ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : '');
1283
$cache[$file][$name][$ThisKey] = trim($ThisValue);
1288
return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
1291
static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
1292
global $GETID3_ERRORARRAY;
1294
if (file_exists($filename)) {
1295
if (include_once($filename)) {
1298
$diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
1301
$diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
1303
if ($DieOnFailure) {
1304
throw new Exception($diemessage);
1306
$GETID3_ERRORARRAY[] = $diemessage;
1311
public static function trimNullByte($string) {
1312
return trim($string, "\x00");
b'\\ No newline at end of file'