<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org>               //
//  available at http://getid3.sourceforge.net                 //
//            or http://www.getid3.org                         //
/////////////////////////////////////////////////////////////////
//                                                             //
// getid3.lib.php - part of getID3()                           //
// See readme.txt for more details                             //
//                                                            ///
/////////////////////////////////////////////////////////////////


class getid3_lib
{

    function
PrintHexBytes($string, $hex=true, $spaces=true, $htmlsafe=true) {
        
$returnstring = '';
        for (
$i = 0; $i < strlen($string); $i++) {
            if (
$hex) {
                
$returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
            } else {
                
$returnstring .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '¤');
            }
            if (
$spaces) {
                
$returnstring .= ' ';
            }
        }
        if (
$htmlsafe) {
            
$returnstring = htmlentities($returnstring);
        }
        return
$returnstring;
    }

    function
SafeStripSlashes($text) {
        if (
get_magic_quotes_gpc()) {
            return
stripslashes($text);
        }
        return
$text;
    }


    function
trunc($floatnumber) {
        
// truncates a floating-point number at the decimal point
        // returns int (if possible, otherwise float)
        
if ($floatnumber >= 1) {
            
$truncatednumber = floor($floatnumber);
        } elseif (
$floatnumber <= -1) {
            
$truncatednumber = ceil($floatnumber);
        } else {
            
$truncatednumber = 0;
        }
        if (
$truncatednumber <= 1073741824) { // 2^30
            
$truncatednumber = (int) $truncatednumber;
        }
        return
$truncatednumber;
    }


    function
CastAsInt($floatnum) {
        
// convert to float if not already
        
$floatnum = (float) $floatnum;

        
// convert a float to type int, only if possible
        
if (getid3_lib::trunc($floatnum) == $floatnum) {
            
// it's not floating point
            
if ($floatnum <= 1073741824) { // 2^30
                // it's within int range
                
$floatnum = (int) $floatnum;
            }
        }
        return
$floatnum;
    }


    function
DecimalBinary2Float($binarynumerator) {
        
$numerator   = getid3_lib::Bin2Dec($binarynumerator);
        
$denominator = getid3_lib::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
        return (
$numerator / $denominator);
    }


    function
NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
        
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
        
if (strpos($binarypointnumber, '.') === false) {
            
$binarypointnumber = '0.'.$binarypointnumber;
        } elseif (
$binarypointnumber{0} == '.') {
            
$binarypointnumber = '0'.$binarypointnumber;
        }
        
$exponent = 0;
        while ((
$binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
            if (
substr($binarypointnumber, 1, 1) == '.') {
                
$exponent--;
                
$binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
            } else {
                
$pointpos = strpos($binarypointnumber, '.');
                
$exponent += ($pointpos - 1);
                
$binarypointnumber = str_replace('.', '', $binarypointnumber);
                
$binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
            }
        }
        
$binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
        return array(
'normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
    }


    function
Float2BinaryDecimal($floatvalue) {
        
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
        
$maxbits = 128; // to how many bits of precision should the calculations be taken?
        
$intpart   = getid3_lib::trunc($floatvalue);
        
$floatpart = abs($floatvalue - $intpart);
        
$pointbitstring = '';
        while ((
$floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
            
$floatpart *= 2;
            
$pointbitstring .= (string) getid3_lib::trunc($floatpart);
            
$floatpart -= getid3_lib::trunc($floatpart);
        }
        
$binarypointnumber = decbin($intpart).'.'.$pointbitstring;
        return
$binarypointnumber;
    }


    function
Float2String($floatvalue, $bits) {
        
// http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
        
switch ($bits) {
            case
32:
                
$exponentbits = 8;
                
$fractionbits = 23;
                break;

            case
64:
                
$exponentbits = 11;
                
$fractionbits = 52;
                break;

            default:
                return
false;
                break;
        }
        if (
$floatvalue >= 0) {
            
$signbit = '0';
        } else {
            
$signbit = '1';
        }
        
$normalizedbinary  = getid3_lib::NormalizeBinaryPoint(getid3_lib::Float2BinaryDecimal($floatvalue), $fractionbits);
        
$biasedexponent    = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
        
$exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
        
$fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);

        return
getid3_lib::BigEndian2String(getid3_lib::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
    }


    function
LittleEndian2Float($byteword) {
        return
getid3_lib::BigEndian2Float(strrev($byteword));
    }


    function
BigEndian2Float($byteword) {
        
// ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
        // http://www.psc.edu/general/software/packages/ieee/ieee.html
        // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html

        
$bitword = getid3_lib::BigEndian2Bin($byteword);
        if (!
$bitword) {
            return
0;
        }
        
$signbit = $bitword{0};

        switch (
strlen($byteword) * 8) {
            case
32:
                
$exponentbits = 8;
                
$fractionbits = 23;
                break;

            case
64:
                
$exponentbits = 11;
                
$fractionbits = 52;
                break;

            case
80:
                
// 80-bit Apple SANE format
                // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
                
$exponentstring = substr($bitword, 1, 15);
                
$isnormalized = intval($bitword{16});
                
$fractionstring = substr($bitword, 17, 63);
                
$exponent = pow(2, getid3_lib::Bin2Dec($exponentstring) - 16383);
                
$fraction = $isnormalized + getid3_lib::DecimalBinary2Float($fractionstring);
                
$floatvalue = $exponent * $fraction;
                if (
$signbit == '1') {
                    
$floatvalue *= -1;
                }
                return
$floatvalue;
                break;

            default:
                return
false;
                break;
        }
        
$exponentstring = substr($bitword, 1, $exponentbits);
        
$fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
        
$exponent = getid3_lib::Bin2Dec($exponentstring);
        
$fraction = getid3_lib::Bin2Dec($fractionstring);

        if ((
$exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
            
// Not a Number
            
$floatvalue = false;
        } elseif ((
$exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
            if (
$signbit == '1') {
                
$floatvalue = '-infinity';
            } else {
                
$floatvalue = '+infinity';
            }
        } elseif ((
$exponent == 0) && ($fraction == 0)) {
            if (
$signbit == '1') {
                
$floatvalue = -0;
            } else {
                
$floatvalue = 0;
            }
            
$floatvalue = ($signbit ? 0 : -0);
        } elseif ((
$exponent == 0) && ($fraction != 0)) {
            
// These are 'unnormalized' values
            
$floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fractionstring);
            if (
$signbit == '1') {
                
$floatvalue *= -1;
            }
        } elseif (
$exponent != 0) {
            
$floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fractionstring));
            if (
$signbit == '1') {
                
$floatvalue *= -1;
            }
        }
        return (float)
$floatvalue;
    }


    function
BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
        
$intvalue = 0;
        
$bytewordlen = strlen($byteword);
        for (
$i = 0; $i < $bytewordlen; $i++) {
            if (
$synchsafe) { // disregard MSB, effectively 7-bit bytes
                
$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
            } else {
                
$intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
            }
        }
        if (
$signed && !$synchsafe) {
            
// synchsafe ints are not allowed to be signed
            
switch ($bytewordlen) {
                case
1:
                case
2:
                case
3:
                case
4:
                    
$signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
                    if (
$intvalue & $signmaskbit) {
                        
$intvalue = 0 - ($intvalue & ($signmaskbit - 1));
                    }
                    break;

                default:
                    die(
'ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2Int()');
                    break;
            }
        }
        return
getid3_lib::CastAsInt($intvalue);
    }


    function
LittleEndian2Int($byteword, $signed=false) {
        return
getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
    }


    function
BigEndian2Bin($byteword) {
        
$binvalue = '';
        
$bytewordlen = strlen($byteword);
        for (
$i = 0; $i < $bytewordlen; $i++) {
            
$binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
        }
        return
$binvalue;
    }


    function
BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
        if (
$number < 0) {
            return
false;
        }
        
$maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
        
$intstring = '';
        if (
$signed) {
            if (
$minbytes > 4) {
                die(
'ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2String()');
            }
            
$number = $number & (0x80 << (8 * ($minbytes - 1)));
        }
        while (
$number != 0) {
            
$quotient = ($number / ($maskbyte + 1));
            
$intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
            
$number = floor($quotient);
        }
        return
str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
    }


    function
Dec2Bin($number) {
        while (
$number >= 256) {
            
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
            
$number = floor($number / 256);
        }
        
$bytes[] = $number;
        
$binstring = '';
        for (
$i = 0; $i < count($bytes); $i++) {
            
$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
        }
        return
$binstring;
    }


    function
Bin2Dec($binstring, $signed=false) {
        
$signmult = 1;
        if (
$signed) {
            if (
$binstring{0} == '1') {
                
$signmult = -1;
            }
            
$binstring = substr($binstring, 1);
        }
        
$decvalue = 0;
        for (
$i = 0; $i < strlen($binstring); $i++) {
            
$decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
        }
        return
getid3_lib::CastAsInt($decvalue * $signmult);
    }


    function
Bin2String($binstring) {
        
// return 'hi' for input of '0110100001101001'
        
$string = '';
        
$binstringreversed = strrev($binstring);
        for (
$i = 0; $i < strlen($binstringreversed); $i += 8) {
            
$string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
        }
        return
$string;
    }


    function
LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
        
$intstring = '';
        while (
$number > 0) {
            if (
$synchsafe) {
                
$intstring = $intstring.chr($number & 127);
                
$number >>= 7;
            } else {
                
$intstring = $intstring.chr($number & 255);
                
$number >>= 8;
            }
        }
        return
str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
    }


    function
array_merge_clobber($array1, $array2) {
        
// written by kcØhireability*com
        // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
        
if (!is_array($array1) || !is_array($array2)) {
            return
false;
        }
        
$newarray = $array1;
        foreach (
$array2 as $key => $val) {
            if (
is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
                
$newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
            } else {
                
$newarray[$key] = $val;
            }
        }
        return
$newarray;
    }


    function
array_merge_noclobber($array1, $array2) {
        if (!
is_array($array1) || !is_array($array2)) {
            return
false;
        }
        
$newarray = $array1;
        foreach (
$array2 as $key => $val) {
            if (
is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
                
$newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
            } elseif (!isset(
$newarray[$key])) {
                
$newarray[$key] = $val;
            }
        }
        return
$newarray;
    }


    function
fileextension($filename, $numextensions=1) {
        if (
strstr($filename, '.')) {
            
$reversedfilename = strrev($filename);
            
$offset = 0;
            for (
$i = 0; $i < $numextensions; $i++) {
                
$offset = strpos($reversedfilename, '.', $offset + 1);
                if (
$offset === false) {
                    return
'';
                }
            }
            return
strrev(substr($reversedfilename, 0, $offset));
        }
        return
'';
    }


    function
PlaytimeString($playtimeseconds) {
        
$sign = (($playtimeseconds < 0) ? '-' : '');
        
$playtimeseconds = abs($playtimeseconds);
        
$contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
        
$contentminutes = floor($playtimeseconds / 60);
        if (
$contentseconds >= 60) {
            
$contentseconds -= 60;
            
$contentminutes++;
        }
        return
$sign.intval($contentminutes).':'.str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
    }


    function
image_type_to_mime_type($imagetypeid) {
        
// only available in PHP v4.3.0+
        
static $image_type_to_mime_type = array();
        if (empty(
$image_type_to_mime_type)) {
            
$image_type_to_mime_type[1]  = 'image/gif';                     // GIF
            
$image_type_to_mime_type[2]  = 'image/jpeg';                    // JPEG
            
$image_type_to_mime_type[3]  = 'image/png';                     // PNG
            
$image_type_to_mime_type[4]  = 'application/x-shockwave-flash'; // Flash
            
$image_type_to_mime_type[5]  = 'image/psd';                     // PSD
            
$image_type_to_mime_type[6]  = 'image/bmp';                     // BMP
            
$image_type_to_mime_type[7]  = 'image/tiff';                    // TIFF: little-endian (Intel)
            
$image_type_to_mime_type[8]  = 'image/tiff';                    // TIFF: big-endian (Motorola)
            //$image_type_to_mime_type[9]  = 'image/jpc';                   // JPC
            //$image_type_to_mime_type[10] = 'image/jp2';                   // JPC
            //$image_type_to_mime_type[11] = 'image/jpx';                   // JPC
            //$image_type_to_mime_type[12] = 'image/jb2';                   // JPC
            
$image_type_to_mime_type[13] = 'application/x-shockwave-flash'; // Shockwave
            
$image_type_to_mime_type[14] = 'image/iff';                     // IFF
        
}
        return (isset(
$image_type_to_mime_type[$imagetypeid]) ? $image_type_to_mime_type[$imagetypeid] : 'application/octet-stream');
    }


    function
DateMac2Unix($macdate) {
        
// Macintosh timestamp: seconds since 00:00h January 1, 1904
        // UNIX timestamp:      seconds since 00:00h January 1, 1970
        
return getid3_lib::CastAsInt($macdate - 2082844800);
    }


    function
FixedPoint8_8($rawdata) {
        return
getid3_lib::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
    }


    function
FixedPoint16_16($rawdata) {
        return
getid3_lib::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
    }


    function
FixedPoint2_30($rawdata) {
        
$binarystring = getid3_lib::BigEndian2Bin($rawdata);
        return
getid3_lib::Bin2Dec(substr($binarystring, 0, 2)) + (float) (getid3_lib::Bin2Dec(substr($binarystring, 2, 30)) / 1073741824);
    }


    function
CreateDeepArray($ArrayPath, $Separator, $Value) {
        
// assigns $Value to a nested array path:
        //   $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
        // is the same as:
        //   $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
        // or
        //   $foo['path']['to']['my'] = 'file.txt';
        
while ($ArrayPath && ($ArrayPath{0} == $Separator)) {
            
$ArrayPath = substr($ArrayPath, 1);
        }
        if ((
$pos = strpos($ArrayPath, $Separator)) !== false) {
            
$ReturnedArray[substr($ArrayPath, 0, $pos)] = getid3_lib::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
        } else {
            
$ReturnedArray[$ArrayPath] = $Value;
        }
        return
$ReturnedArray;
    }

    function
array_max($arraydata, $returnkey=false) {
        
$maxvalue = false;
        
$maxkey = false;
        foreach (
$arraydata as $key => $value) {
            if (!
is_array($value)) {
                if (
$value > $maxvalue) {
                    
$maxvalue = $value;
                    
$maxkey = $key;
                }
            }
        }
        return (
$returnkey ? $maxkey : $maxvalue);
    }

    function
array_min($arraydata, $returnkey=false) {
        
$minvalue = false;
        
$minkey = false;
        foreach (
$arraydata as $key => $value) {
            if (!
is_array($value)) {
                if (
$value > $minvalue) {
                    
$minvalue = $value;
                    
$minkey = $key;
                }
            }
        }
        return (
$returnkey ? $minkey : $minvalue);
    }


    function
md5_file($file) {

        
// md5_file() exists in PHP 4.2.0+.
        
if (function_exists('md5_file')) {
            return
md5_file($file);
        }

        if (
GETID3_OS_ISWINDOWS) {

            
$RequiredFiles = array('cygwin1.dll', 'md5sum.exe');
            foreach (
$RequiredFiles as $required_file) {
                if (!
is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
                    die(
implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::md5_file() to function under Windows in PHP < v4.2.0');
                }
            }
            
$commandline = GETID3_HELPERAPPSDIR.'md5sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
            if (
ereg("^[\\]?([0-9a-f]{32})", strtolower(`$commandline`), $r)) {
                return
$r[1];
            }

        } else {

            
// The following works under UNIX only
            
$file = str_replace('`', '\\`', $file);
            if (
ereg("^([0-9a-f]{32})[ \t\n\r]", `md5sum "$file"`, $r)) {
                return
$r[1];
            }

        }
        return
false;
    }


    function
sha1_file($file) {

        
// sha1_file() exists in PHP 4.3.0+.
        
if (function_exists('sha1_file')) {
            return
sha1_file($file);
        }

        
$file = str_replace('`', '\\`', $file);

        if (
GETID3_OS_ISWINDOWS) {

            
$RequiredFiles = array('cygwin1.dll', 'sha1sum.exe');
            foreach (
$RequiredFiles as $required_file) {
                if (!
is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
                    die(
implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::sha1_file() to function under Windows in PHP < v4.3.0');
                }
            }
            
$commandline = GETID3_HELPERAPPSDIR.'sha1sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
            if (
ereg("^sha1=([0-9a-f]{40})", strtolower(`$commandline`), $r)) {
                return
$r[1];
            }

        } else {

            
$commandline = 'sha1sum '.escapeshellarg($file).'';
            if (
ereg("^([0-9a-f]{40})[ \t\n\r]", strtolower(`$commandline`), $r)) {
                return
$r[1];
            }

        }

        return
false;
    }


    
// Allan Hansen <ahØartemis*dk>
    // getid3_lib::md5_data() - returns md5sum for a file from startuing position to absolute end position
    
function hash_data($file, $offset, $end, $algorithm) {

        switch (
$algorithm) {
            case
'md5':
                
$hash_function = 'md5_file';
                
$unix_call     = 'md5sum';
                
$windows_call  = 'md5sum.exe';
                
$hash_length   = 32;
                break;

            case
'sha1':
                
$hash_function = 'sha1_file';
                
$unix_call     = 'sha1sum';
                
$windows_call  = 'sha1sum.exe';
                
$hash_length   = 40;
                break;

            default:
                die(
'Invalid algorithm ('.$algorithm.') in getid3_lib::hash_data()');
                break;
        }
        
$size = $end - $offset;
        while (
true) {
            if (
GETID3_OS_ISWINDOWS) {

                
// It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
                // Fall back to create-temp-file method:
                
if ($algorithm == 'sha1') {
                    break;
                }

                
$RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
                foreach (
$RequiredFiles as $required_file) {
                    if (!
is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
                        
// helper apps not available - fall back to old method
                        
break;
                    }
                }
                
$commandline  = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' "'.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).'" | ';
                
$commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
                
$commandline .= GETID3_HELPERAPPSDIR.$windows_call;

            } else {

                
$commandline  = 'head -c'.$end.' '.escapeshellarg($file).' | ';
                
$commandline .= 'tail -c'.$size.' | ';
                
$commandline .= $unix_call;

            }
            if ((bool)
ini_get('safe_mode')) {
                
$ThisFileInfo['warning'][] = 'PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm';
                break;
            }
            return
substr(`$commandline`, 0, $hash_length);
        }

        
// try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
        
if (($data_filename = tempnam('*', 'getID3')) === fal