PageRenderTime 71ms CodeModel.GetById 3ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 1ms

/common/libraries/plugin/getid3/demos/demo.mp3header.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 1957 lines | 1584 code | 242 blank | 131 comment | 306 complexity | 53daf93fbe080b7c0222b46fdd8b90ac MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3if (! function_exists('PrintHexBytes'))
   4{
   5
   6    function PrintHexBytes($string)
   7    {
   8        $returnstring = '';
   9        for($i = 0; $i < strlen($string); $i ++)
  10        {
  11            $returnstring .= str_pad(dechex(ord(substr($string, $i, 1))), 2, '0', STR_PAD_LEFT) . ' ';
  12        }
  13        return $returnstring;
  14    }
  15}
  16
  17if (! function_exists('PrintTextBytes'))
  18{
  19
  20    function PrintTextBytes($string)
  21    {
  22        $returnstring = '';
  23        for($i = 0; $i < strlen($string); $i ++)
  24        {
  25            if (ord(substr($string, $i, 1)) <= 31)
  26            {
  27                $returnstring .= '   ';
  28            }
  29            else
  30            {
  31                $returnstring .= ' ' . substr($string, $i, 1) . ' ';
  32            }
  33        }
  34        return $returnstring;
  35    }
  36}
  37
  38if (! function_exists('FixDBFields'))
  39{
  40
  41    function FixDBFields($text)
  42    {
  43        return mysql_escape_string($text);
  44    }
  45}
  46
  47if (! function_exists('FixTextFields'))
  48{
  49
  50    function FixTextFields($text)
  51    {
  52        $text = SafeStripSlashes($text);
  53        $text = htmlentities($text, ENT_QUOTES);
  54        return $text;
  55    }
  56}
  57
  58if (! function_exists('SafeStripSlashes'))
  59{
  60
  61    function SafeStripSlashes($text)
  62    {
  63        if (get_magic_quotes_gpc())
  64        {
  65            return stripslashes($text);
  66        }
  67        return $text;
  68    }
  69}
  70
  71if (! function_exists('table_var_dump'))
  72{
  73
  74    function table_var_dump($variable)
  75    {
  76        $returnstring = '';
  77        switch (gettype($variable))
  78        {
  79            case 'array' :
  80                $returnstring .= '<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="2">';
  81                foreach ($variable as $key => $value)
  82                {
  83                    $returnstring .= '<TR><TD VALIGN="TOP"><B>' . str_replace(chr(0), ' ', $key) . '</B></TD>';
  84                    $returnstring .= '<TD VALIGN="TOP">' . gettype($value);
  85                    if (is_array($value))
  86                    {
  87                        $returnstring .= '&nbsp;(' . count($value) . ')';
  88                    }
  89                    elseif (is_string($value))
  90                    {
  91                        $returnstring .= '&nbsp;(' . strlen($value) . ')';
  92                    }
  93                    if (($key == 'data') && isset($variable['image_mime']) && isset($variable['dataoffset']))
  94                    {
  95                        require_once (GETID3_INCLUDEPATH . 'getid3.getimagesize.php');
  96                        $imageinfo = array();
  97                        $imagechunkcheck = GetDataImageSize($value, $imageinfo);
  98                        $DumpedImageSRC = (! empty($_REQUEST['filename']) ? $_REQUEST['filename'] : '.getid3') . '.' . $variable['dataoffset'] . '.' . ImageTypesLookup($imagechunkcheck[2]);
  99                        if ($tempimagefile = fopen($DumpedImageSRC, 'wb'))
 100                        {
 101                            fwrite($tempimagefile, $value);
 102                            fclose($tempimagefile);
 103                        }
 104                        $returnstring .= '</TD><TD><IMG SRC="' . $DumpedImageSRC . '" WIDTH="' . $imagechunkcheck[0] . '" HEIGHT="' . $imagechunkcheck[1] . '"></TD></TR>';
 105                    }
 106                    else
 107                    {
 108                        $returnstring .= '</TD><TD>' . table_var_dump($value) . '</TD></TR>';
 109                    }
 110                }
 111                $returnstring .= '</TABLE>';
 112                break;
 113            
 114            case 'boolean' :
 115                $returnstring .= ($variable ? 'TRUE' : 'FALSE');
 116                break;
 117            
 118            case 'integer' :
 119            case 'double' :
 120            case 'float' :
 121                $returnstring .= $variable;
 122                break;
 123            
 124            case 'object' :
 125            case 'null' :
 126                $returnstring .= string_var_dump($variable);
 127                break;
 128            
 129            case 'string' :
 130                $variable = str_replace(chr(0), ' ', $variable);
 131                $varlen = strlen($variable);
 132                for($i = 0; $i < $varlen; $i ++)
 133                {
 134                    if (ereg('[' . chr(0x0A) . chr(0x0D) . ' -;0-9A-Za-z]', $variable{$i}))
 135                    {
 136                        $returnstring .= $variable{$i};
 137                    }
 138                    else
 139                    {
 140                        $returnstring .= '&#' . str_pad(ord($variable{$i}), 3, '0', STR_PAD_LEFT) . ';';
 141                    }
 142                }
 143                $returnstring = nl2br($returnstring);
 144                break;
 145            
 146            default :
 147                require_once (GETID3_INCLUDEPATH . 'getid3.getimagesize.php');
 148                $imageinfo = array();
 149                $imagechunkcheck = GetDataImageSize(substr($variable, 0, FREAD_BUFFER_SIZE), $imageinfo);
 150                
 151                if (($imagechunkcheck[2] >= 1) && ($imagechunkcheck[2] <= 3))
 152                {
 153                    $returnstring .= '<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="2">';
 154                    $returnstring .= '<TR><TD><B>type</B></TD><TD>' . ImageTypesLookup($imagechunkcheck[2]) . '</TD></TR>';
 155                    $returnstring .= '<TR><TD><B>width</B></TD><TD>' . number_format($imagechunkcheck[0]) . ' px</TD></TR>';
 156                    $returnstring .= '<TR><TD><B>height</B></TD><TD>' . number_format($imagechunkcheck[1]) . ' px</TD></TR>';
 157                    $returnstring .= '<TR><TD><B>size</B></TD><TD>' . number_format(strlen($variable)) . ' bytes</TD></TR></TABLE>';
 158                }
 159                else
 160                {
 161                    $returnstring .= nl2br(htmlspecialchars(str_replace(chr(0), ' ', $variable)));
 162                }
 163                break;
 164        }
 165        return $returnstring;
 166    }
 167}
 168
 169if (! function_exists('string_var_dump'))
 170{
 171
 172    function string_var_dump($variable)
 173    {
 174        ob_start();
 175        var_dump($variable);
 176        $dumpedvariable = ob_get_contents();
 177        ob_end_clean();
 178        return $dumpedvariable;
 179    }
 180}
 181
 182if (! function_exists('fileextension'))
 183{
 184
 185    function fileextension($filename, $numextensions = 1)
 186    {
 187        if (strstr($filename, '.'))
 188        {
 189            $reversedfilename = strrev($filename);
 190            $offset = 0;
 191            for($i = 0; $i < $numextensions; $i ++)
 192            {
 193                $offset = strpos($reversedfilename, '.', $offset + 1);
 194                if ($offset === false)
 195                {
 196                    return '';
 197                }
 198            }
 199            return strrev(substr($reversedfilename, 0, $offset));
 200        }
 201        return '';
 202    }
 203}
 204
 205if (! function_exists('RemoveAccents'))
 206{
 207
 208    function RemoveAccents($string)
 209    {
 210        // return strtr($string, '?????????????????????????????????????????????????????????????????????', 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
 211        // Revised version by marksteward@hotmail.com
 212        return strtr(strtr($string, '????????????????????????????????????????????????????????????', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy'), array(
 213                '?' => 'TH', '?' => 'th', '?' => 'DH', '?' => 'dh', '?' => 'ss', '?' => 'OE', '?' => 'oe', '?' => 'AE', 
 214                '?' => 'ae', '?' => 'u'));
 215    }
 216}
 217
 218if (! function_exists('MoreNaturalSort'))
 219{
 220
 221    function MoreNaturalSort($ar1, $ar2)
 222    {
 223        if ($ar1 === $ar2)
 224        {
 225            return 0;
 226        }
 227        $len1 = strlen($ar1);
 228        $len2 = strlen($ar2);
 229        $shortest = min($len1, $len2);
 230        if (substr($ar1, 0, $shortest) === substr($ar2, 0, $shortest))
 231        {
 232            // the shorter argument is the beginning of the longer one, like "str" and "string"
 233            if ($len1 < $len2)
 234            {
 235                return - 1;
 236            }
 237            elseif ($len1 > $len2)
 238            {
 239                return 1;
 240            }
 241            return 0;
 242        }
 243        $ar1 = RemoveAccents(strtolower(trim($ar1)));
 244        $ar2 = RemoveAccents(strtolower(trim($ar2)));
 245        $translatearray = array('\'' => '', '"' => '', '_' => ' ', '(' => '', ')' => '', '-' => ' ', '  ' => ' ', 
 246                '.' => '', ',' => '');
 247        foreach ($translatearray as $key => $val)
 248        {
 249            $ar1 = str_replace($key, $val, $ar1);
 250            $ar2 = str_replace($key, $val, $ar2);
 251        }
 252        
 253        if ($ar1 < $ar2)
 254        {
 255            return - 1;
 256        }
 257        elseif ($ar1 > $ar2)
 258        {
 259            return 1;
 260        }
 261        return 0;
 262    }
 263}
 264
 265if (! function_exists('trunc'))
 266{
 267
 268    function trunc($floatnumber)
 269    {
 270        // truncates a floating-point number at the decimal point
 271        // returns int (if possible, otherwise float)
 272        if ($floatnumber >= 1)
 273        {
 274            $truncatednumber = floor($floatnumber);
 275        }
 276        elseif ($floatnumber <= - 1)
 277        {
 278            $truncatednumber = ceil($floatnumber);
 279        }
 280        else
 281        {
 282            $truncatednumber = 0;
 283        }
 284        if ($truncatednumber <= pow(2, 30))
 285        {
 286            $truncatednumber = (int) $truncatednumber;
 287        }
 288        return $truncatednumber;
 289    }
 290}
 291
 292if (! function_exists('CastAsInt'))
 293{
 294
 295    function CastAsInt($floatnum)
 296    {
 297        // convert to float if not already
 298        $floatnum = (float) $floatnum;
 299        
 300        // convert a float to type int, only if possible
 301        if (trunc($floatnum) == $floatnum)
 302        {
 303            // it's not floating point
 304            if ($floatnum <= pow(2, 30))
 305            {
 306                // it's within int range
 307                $floatnum = (int) $floatnum;
 308            }
 309        }
 310        return $floatnum;
 311    }
 312}
 313
 314if (! function_exists('getmicrotime'))
 315{
 316
 317    function getmicrotime()
 318    {
 319        list($usec, $sec) = explode(' ', microtime());
 320        return ((float) $usec + (float) $sec);
 321    }
 322}
 323
 324if (! function_exists('DecimalBinary2Float'))
 325{
 326
 327    function DecimalBinary2Float($binarynumerator)
 328    {
 329        $numerator = Bin2Dec($binarynumerator);
 330        $denominator = Bin2Dec(str_repeat('1', strlen($binarynumerator)));
 331        return ($numerator / $denominator);
 332    }
 333}
 334
 335if (! function_exists('NormalizeBinaryPoint'))
 336{
 337
 338    function NormalizeBinaryPoint($binarypointnumber, $maxbits = 52)
 339    {
 340        // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
 341        if (strpos($binarypointnumber, '.') === false)
 342        {
 343            $binarypointnumber = '0.' . $binarypointnumber;
 344        }
 345        elseif ($binarypointnumber{0} == '.')
 346        {
 347            $binarypointnumber = '0' . $binarypointnumber;
 348        }
 349        $exponent = 0;
 350        while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.'))
 351        {
 352            if (substr($binarypointnumber, 1, 1) == '.')
 353            {
 354                $exponent --;
 355                $binarypointnumber = substr($binarypointnumber, 2, 1) . '.' . substr($binarypointnumber, 3);
 356            }
 357            else
 358            {
 359                $pointpos = strpos($binarypointnumber, '.');
 360                $exponent += ($pointpos - 1);
 361                $binarypointnumber = str_replace('.', '', $binarypointnumber);
 362                $binarypointnumber = $binarypointnumber{0} . '.' . substr($binarypointnumber, 1);
 363            }
 364        }
 365        $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
 366        return array('normalized' => $binarypointnumber, 'exponent' => (int) $exponent);
 367    }
 368}
 369
 370if (! function_exists('Float2BinaryDecimal'))
 371{
 372
 373    function Float2BinaryDecimal($floatvalue)
 374    {
 375        // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
 376        $maxbits = 128; // to how many bits of precision should the calculations be taken?
 377        $intpart = trunc($floatvalue);
 378        $floatpart = abs($floatvalue - $intpart);
 379        $pointbitstring = '';
 380        while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits))
 381        {
 382            $floatpart *= 2;
 383            $pointbitstring .= (string) trunc($floatpart);
 384            $floatpart -= trunc($floatpart);
 385        }
 386        $binarypointnumber = decbin($intpart) . '.' . $pointbitstring;
 387        return $binarypointnumber;
 388    }
 389}
 390
 391if (! function_exists('Float2String'))
 392{
 393
 394    function Float2String($floatvalue, $bits)
 395    {
 396        // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
 397        switch ($bits)
 398        {
 399            case 32 :
 400                $exponentbits = 8;
 401                $fractionbits = 23;
 402                break;
 403            
 404            case 64 :
 405                $exponentbits = 11;
 406                $fractionbits = 52;
 407                break;
 408            
 409            default :
 410                return false;
 411                break;
 412        }
 413        if ($floatvalue >= 0)
 414        {
 415            $signbit = '0';
 416        }
 417        else
 418        {
 419            $signbit = '1';
 420        }
 421        $normalizedbinary = NormalizeBinaryPoint(Float2BinaryDecimal($floatvalue), $fractionbits);
 422        $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
 423        $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
 424        $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
 425        
 426        return BigEndian2String(Bin2Dec($signbit . $exponentbitstring . $fractionbitstring), $bits % 8, false);
 427    }
 428}
 429
 430if (! function_exists('LittleEndian2Float'))
 431{
 432
 433    function LittleEndian2Float($byteword)
 434    {
 435        return BigEndian2Float(strrev($byteword));
 436    }
 437}
 438
 439if (! function_exists('BigEndian2Float'))
 440{
 441
 442    function BigEndian2Float($byteword)
 443    {
 444        // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
 445        // http://www.psc.edu/general/software/packages/ieee/ieee.html
 446        // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
 447        
 448
 449        $bitword = BigEndian2Bin($byteword);
 450        $signbit = $bitword{0};
 451        
 452        switch (strlen($byteword) * 8)
 453        {
 454            case 32 :
 455                $exponentbits = 8;
 456                $fractionbits = 23;
 457                break;
 458            
 459            case 64 :
 460                $exponentbits = 11;
 461                $fractionbits = 52;
 462                break;
 463            
 464            case 80 :
 465                $exponentbits = 16;
 466                $fractionbits = 64;
 467                break;
 468            
 469            default :
 470                return false;
 471                break;
 472        }
 473        $exponentstring = substr($bitword, 1, $exponentbits - 1);
 474        $fractionstring = substr($bitword, $exponentbits, $fractionbits);
 475        $exponent = Bin2Dec($exponentstring);
 476        $fraction = Bin2Dec($fractionstring);
 477        
 478        if (($exponentbits == 16) && ($fractionbits == 64))
 479        {
 480            // 80-bit
 481            // As used in Apple AIFF for sample_rate
 482            // A bit of a hack, but it works ;)
 483            return pow(2, ($exponent - 16382)) * DecimalBinary2Float($fractionstring);
 484        }
 485        
 486        if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0))
 487        {
 488            // Not a Number
 489            $floatvalue = false;
 490        }
 491        elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0))
 492        {
 493            if ($signbit == '1')
 494            {
 495                $floatvalue = '-infinity';
 496            }
 497            else
 498            {
 499                $floatvalue = '+infinity';
 500            }
 501        }
 502        elseif (($exponent == 0) && ($fraction == 0))
 503        {
 504            if ($signbit == '1')
 505            {
 506                $floatvalue = - 0;
 507            }
 508            else
 509            {
 510                $floatvalue = 0;
 511            }
 512            $floatvalue = ($signbit ? 0 : - 0);
 513        }
 514        elseif (($exponent == 0) && ($fraction != 0))
 515        {
 516            // These are 'unnormalized' values
 517            $floatvalue = pow(2, (- 1 * (pow(2, $exponentbits - 1) - 2))) * DecimalBinary2Float($fractionstring);
 518            if ($signbit == '1')
 519            {
 520                $floatvalue *= - 1;
 521            }
 522        }
 523        elseif ($exponent != 0)
 524        {
 525            $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + DecimalBinary2Float($fractionstring));
 526            if ($signbit == '1')
 527            {
 528                $floatvalue *= - 1;
 529            }
 530        }
 531        return (float) $floatvalue;
 532    }
 533}
 534
 535if (! function_exists('BigEndian2Int'))
 536{
 537
 538    function BigEndian2Int($byteword, $synchsafe = false, $signed = false)
 539    {
 540        $intvalue = 0;
 541        $bytewordlen = strlen($byteword);
 542        for($i = 0; $i < $bytewordlen; $i ++)
 543        {
 544            if ($synchsafe)
 545            { // disregard MSB, effectively 7-bit bytes
 546                $intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
 547            }
 548            else
 549            {
 550                $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
 551            }
 552        }
 553        if ($signed && ! $synchsafe)
 554        {
 555            // synchsafe ints are not allowed to be signed
 556            switch ($bytewordlen)
 557            {
 558                case 1 :
 559                case 2 :
 560                case 3 :
 561                case 4 :
 562                    $signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
 563                    if ($intvalue & $signmaskbit)
 564                    {
 565                        $intvalue = 0 - ($intvalue & ($signmaskbit - 1));
 566                    }
 567                    break;
 568                
 569                default :
 570                    die('ERROR: Cannot have signed integers larger than 32-bits in BigEndian2Int()');
 571                    break;
 572            }
 573        }
 574        return CastAsInt($intvalue);
 575    }
 576}
 577
 578if (! function_exists('LittleEndian2Int'))
 579{
 580
 581    function LittleEndian2Int($byteword, $signed = false)
 582    {
 583        return BigEndian2Int(strrev($byteword), false, $signed);
 584    }
 585}
 586
 587if (! function_exists('BigEndian2Bin'))
 588{
 589
 590    function BigEndian2Bin($byteword)
 591    {
 592        $binvalue = '';
 593        $bytewordlen = strlen($byteword);
 594        for($i = 0; $i < $bytewordlen; $i ++)
 595        {
 596            $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
 597        }
 598        return $binvalue;
 599    }
 600}
 601
 602if (! function_exists('BigEndian2String'))
 603{
 604
 605    function BigEndian2String($number, $minbytes = 1, $synchsafe = false, $signed = false)
 606    {
 607        if ($number < 0)
 608        {
 609            return false;
 610        }
 611        $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
 612        $intstring = '';
 613        if ($signed)
 614        {
 615            if ($minbytes > 4)
 616            {
 617                die('ERROR: Cannot have signed integers larger than 32-bits in BigEndian2String()');
 618            }
 619            $number = $number & (0x80 << (8 * ($minbytes - 1)));
 620        }
 621        while ($number != 0)
 622        {
 623            $quotient = ($number / ($maskbyte + 1));
 624            $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)) . $intstring;
 625            $number = floor($quotient);
 626        }
 627        return str_pad($intstring, $minbytes, chr(0), STR_PAD_LEFT);
 628    }
 629}
 630
 631if (! function_exists('Dec2Bin'))
 632{
 633
 634    function Dec2Bin($number)
 635    {
 636        while ($number >= 256)
 637        {
 638            $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
 639            $number = floor($number / 256);
 640        }
 641        $bytes[] = $number;
 642        $binstring = '';
 643        for($i = 0; $i < count($bytes); $i ++)
 644        {
 645            $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)) . $binstring;
 646        }
 647        return $binstring;
 648    }
 649}
 650
 651if (! function_exists('Bin2Dec'))
 652{
 653
 654    function Bin2Dec($binstring)
 655    {
 656        $decvalue = 0;
 657        for($i = 0; $i < strlen($binstring); $i ++)
 658        {
 659            $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
 660        }
 661        return CastAsInt($decvalue);
 662    }
 663}
 664
 665if (! function_exists('Bin2String'))
 666{
 667
 668    function Bin2String($binstring)
 669    {
 670        // return 'hi' for input of '0110100001101001'
 671        $string = '';
 672        $binstringreversed = strrev($binstring);
 673        for($i = 0; $i < strlen($binstringreversed); $i += 8)
 674        {
 675            $string = chr(Bin2Dec(strrev(substr($binstringreversed, $i, 8)))) . $string;
 676        }
 677        return $string;
 678    }
 679}
 680
 681if (! function_exists('LittleEndian2String'))
 682{
 683
 684    function LittleEndian2String($number, $minbytes = 1, $synchsafe = false)
 685    {
 686        $intstring = '';
 687        while ($number > 0)
 688        {
 689            if ($synchsafe)
 690            {
 691                $intstring = $intstring . chr($number & 127);
 692                $number >>= 7;
 693            }
 694            else
 695            {
 696                $intstring = $intstring . chr($number & 255);
 697                $number >>= 8;
 698            }
 699        }
 700        return str_pad($intstring, $minbytes, chr(0), STR_PAD_RIGHT);
 701    }
 702}
 703
 704if (! function_exists('Bool2IntString'))
 705{
 706
 707    function Bool2IntString($intvalue)
 708    {
 709        return ($intvalue ? '1' : '0');
 710    }
 711}
 712
 713if (! function_exists('IntString2Bool'))
 714{
 715
 716    function IntString2Bool($char)
 717    {
 718        if ($char == '1')
 719        {
 720            return true;
 721        }
 722        elseif ($char == '0')
 723        {
 724            return false;
 725        }
 726        return null;
 727    }
 728}
 729
 730if (! function_exists('InverseBoolean'))
 731{
 732
 733    function InverseBoolean($value)
 734    {
 735        return ($value ? false : true);
 736    }
 737}
 738
 739if (! function_exists('DeUnSynchronise'))
 740{
 741
 742    function DeUnSynchronise($data)
 743    {
 744        return str_replace(chr(0xFF) . chr(0x00), chr(0xFF), $data);
 745    }
 746}
 747
 748if (! function_exists('Unsynchronise'))
 749{
 750
 751    function Unsynchronise($data)
 752    {
 753        // Whenever a false synchronisation is found within the tag, one zeroed
 754        // byte is inserted after the first false synchronisation byte. The
 755        // format of a correct sync that should be altered by ID3 encoders is as
 756        // follows:
 757        //      %11111111 111xxxxx
 758        // And should be replaced with:
 759        //      %11111111 00000000 111xxxxx
 760        // This has the side effect that all $FF 00 combinations have to be
 761        // altered, so they won't be affected by the decoding process. Therefore
 762        // all the $FF 00 combinations have to be replaced with the $FF 00 00
 763        // combination during the unsynchronisation.
 764        
 765
 766        $data = str_replace(chr(0xFF) . chr(0x00), chr(0xFF) . chr(0x00) . chr(0x00), $data);
 767        $unsyncheddata = '';
 768        for($i = 0; $i < strlen($data); $i ++)
 769        {
 770            $thischar = $data{$i};
 771            $unsyncheddata .= $thischar;
 772            if ($thischar == chr(255))
 773            {
 774                $nextchar = ord(substr($data, $i + 1, 1));
 775                if (($nextchar | 0xE0) == 0xE0)
 776                {
 777                    // previous byte = 11111111, this byte = 111?????
 778                    $unsyncheddata .= chr(0);
 779                }
 780            }
 781        }
 782        return $unsyncheddata;
 783    }
 784}
 785
 786if (! function_exists('is_hash'))
 787{
 788
 789    function is_hash($var)
 790    {
 791        // written by dev-null@christophe.vg
 792        // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
 793        if (is_array($var))
 794        {
 795            $keys = array_keys($var);
 796            $all_num = true;
 797            for($i = 0; $i < count($keys); $i ++)
 798            {
 799                if (is_string($keys[$i]))
 800                {
 801                    return true;
 802                }
 803            }
 804        }
 805        return false;
 806    }
 807}
 808
 809if (! function_exists('array_join_merge'))
 810{
 811
 812    function array_join_merge($arr1, $arr2)
 813    {
 814        // written by dev-null@christophe.vg
 815        // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
 816        if (is_array($arr1) && is_array($arr2))
 817        {
 818            // the same -> merge
 819            $new_array = array();
 820            
 821            if (is_hash($arr1) && is_hash($arr2))
 822            {
 823                // hashes -> merge based on keys
 824                $keys = array_merge(array_keys($arr1), array_keys($arr2));
 825                foreach ($keys as $key)
 826                {
 827                    $new_array[$key] = array_join_merge(@$arr1[$key], @$arr2[$key]);
 828                }
 829            }
 830            else
 831            {
 832                // two real arrays -> merge
 833                $new_array = array_reverse(array_unique(array_reverse(array_merge($arr1, $arr2))));
 834            }
 835            return $new_array;
 836        }
 837        else
 838        {
 839            // not the same ... take new one if defined, else the old one stays
 840            return $arr2 ? $arr2 : $arr1;
 841        }
 842    }
 843}
 844
 845if (! function_exists('array_merge_clobber'))
 846{
 847
 848    function array_merge_clobber($array1, $array2)
 849    {
 850        // written by kc@hireability.com
 851        // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
 852        if (! is_array($array1) || ! is_array($array2))
 853        {
 854            return false;
 855        }
 856        $newarray = $array1;
 857        foreach ($array2 as $key => $val)
 858        {
 859            if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key]))
 860            {
 861                $newarray[$key] = array_merge_clobber($newarray[$key], $val);
 862            }
 863            else
 864            {
 865                $newarray[$key] = $val;
 866            }
 867        }
 868        return $newarray;
 869    }
 870}
 871
 872if (! function_exists('array_merge_noclobber'))
 873{
 874
 875    function array_merge_noclobber($array1, $array2)
 876    {
 877        if (! is_array($array1) || ! is_array($array2))
 878        {
 879            return false;
 880        }
 881        $newarray = $array1;
 882        foreach ($array2 as $key => $val)
 883        {
 884            if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key]))
 885            {
 886                $newarray[$key] = array_merge_noclobber($newarray[$key], $val);
 887            }
 888            elseif (! isset($newarray[$key]))
 889            {
 890                $newarray[$key] = $val;
 891            }
 892        }
 893        return $newarray;
 894    }
 895}
 896
 897if (! function_exists('RoughTranslateUnicodeToASCII'))
 898{
 899
 900    function RoughTranslateUnicodeToASCII($rawdata, $frame_textencoding)
 901    {
 902        // rough translation of data for application that can't handle Unicode data
 903        
 904
 905        $tempstring = '';
 906        switch ($frame_textencoding)
 907        {
 908            case 0 : // ISO-8859-1. Terminated with $00.
 909                $asciidata = $rawdata;
 910                break;
 911            
 912            case 1 : // UTF-16 encoded Unicode with BOM. Terminated with $00 00.
 913                $asciidata = $rawdata;
 914                if (substr($asciidata, 0, 2) == chr(0xFF) . chr(0xFE))
 915                {
 916                    // remove BOM, only if present (it should be, but...)
 917                    $asciidata = substr($asciidata, 2);
 918                }
 919                if (substr($asciidata, strlen($asciidata) - 2, 2) == chr(0) . chr(0))
 920                {
 921                    $asciidata = substr($asciidata, 0, strlen($asciidata) - 2); // remove terminator, only if present (it should be, but...)
 922                }
 923                for($i = 0; $i < strlen($asciidata); $i += 2)
 924                {
 925                    if ((ord($asciidata{$i}) <= 0x7F) || (ord($asciidata{$i}) >= 0xA0))
 926                    {
 927                        $tempstring .= $asciidata{$i};
 928                    }
 929                    else
 930                    {
 931                        $tempstring .= '?';
 932                    }
 933                }
 934                $asciidata = $tempstring;
 935                break;
 936            
 937            case 2 : // UTF-16BE encoded Unicode without BOM. Terminated with $00 00.
 938                $asciidata = $rawdata;
 939                if (substr($asciidata, strlen($asciidata) - 2, 2) == chr(0) . chr(0))
 940                {
 941                    $asciidata = substr($asciidata, 0, strlen($asciidata) - 2); // remove terminator, only if present (it should be, but...)
 942                }
 943                for($i = 0; $i < strlen($asciidata); $i += 2)
 944                {
 945                    if ((ord($asciidata{$i}) <= 0x7F) || (ord($asciidata{$i}) >= 0xA0))
 946                    {
 947                        $tempstring .= $asciidata{$i};
 948                    }
 949                    else
 950                    {
 951                        $tempstring .= '?';
 952                    }
 953                }
 954                $asciidata = $tempstring;
 955                break;
 956            
 957            case 3 : // UTF-8 encoded Unicode. Terminated with $00.
 958                $asciidata = utf8_decode($rawdata);
 959                break;
 960            
 961            case 255 : // Unicode, Big-Endian. Terminated with $00 00.
 962                $asciidata = $rawdata;
 963                if (substr($asciidata, strlen($asciidata) - 2, 2) == chr(0) . chr(0))
 964                {
 965                    $asciidata = substr($asciidata, 0, strlen($asciidata) - 2); // remove terminator, only if present (it should be, but...)
 966                }
 967                for($i = 0; ($i + 1) < strlen($asciidata); $i += 2)
 968                {
 969                    if ((ord($asciidata{($i + 1)}) <= 0x7F) || (ord($asciidata{($i + 1)}) >= 0xA0))
 970                    {
 971                        $tempstring .= $asciidata{($i + 1)};
 972                    }
 973                    else
 974                    {
 975                        $tempstring .= '?';
 976                    }
 977                }
 978                $asciidata = $tempstring;
 979                break;
 980            
 981            default :
 982                // shouldn't happen, but in case $frame_textencoding is not 1 <= $frame_textencoding <= 4
 983                // just pass the data through unchanged.
 984                $asciidata = $rawdata;
 985                break;
 986        }
 987        if (substr($asciidata, strlen($asciidata) - 1, 1) == chr(0))
 988        {
 989            // remove null terminator, if present
 990            $asciidata = NoNullString($asciidata);
 991        }
 992        return $asciidata;
 993    
 994     // return str_replace(chr(0), '', $asciidata); // just in case any nulls slipped through
 995    }
 996}
 997
 998if (! function_exists('PlaytimeString'))
 999{
1000
1001    function PlaytimeString($playtimeseconds)
1002    {
1003        $contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
1004        $contentminutes = floor($playtimeseconds / 60);
1005        if ($contentseconds >= 60)
1006        {
1007            $contentseconds -= 60;
1008            $contentminutes ++;
1009        }
1010        return number_format($contentminutes) . ':' . str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
1011    }
1012}
1013
1014if (! function_exists('CloseMatch'))
1015{
1016
1017    function CloseMatch($value1, $value2, $tolerance)
1018    {
1019        return (abs($value1 - $value2) <= $tolerance);
1020    }
1021}
1022
1023if (! function_exists('ID3v1matchesID3v2'))
1024{
1025
1026    function ID3v1matchesID3v2($id3v1, $id3v2)
1027    {
1028        
1029        $requiredindices = array('title', 'artist', 'album', 'year', 'genre', 'comment');
1030        foreach ($requiredindices as $requiredindex)
1031        {
1032            if (! isset($id3v1["$requiredindex"]))
1033            {
1034                $id3v1["$requiredindex"] = '';
1035            }
1036            if (! isset($id3v2["$requiredindex"]))
1037            {
1038                $id3v2["$requiredindex"] = '';
1039            }
1040        }
1041        
1042        if (trim($id3v1['title']) != trim(substr($id3v2['title'], 0, 30)))
1043        {
1044            return false;
1045        }
1046        if (trim($id3v1['artist']) != trim(substr($id3v2['artist'], 0, 30)))
1047        {
1048            return false;
1049        }
1050        if (trim($id3v1['album']) != trim(substr($id3v2['album'], 0, 30)))
1051        {
1052            return false;
1053        }
1054        if (trim($id3v1['year']) != trim(substr($id3v2['year'], 0, 4)))
1055        {
1056            return false;
1057        }
1058        if (trim($id3v1['genre']) != trim($id3v2['genre']))
1059        {
1060            return false;
1061        }
1062        if (isset($id3v1['track']))
1063        {
1064            if (! isset($id3v1['track']) || (trim($id3v1['track']) != trim($id3v2['track'])))
1065            {
1066                return false;
1067            }
1068            if (trim($id3v1['comment']) != trim(substr($id3v2['comment'], 0, 28)))
1069            {
1070                return false;
1071            }
1072        }
1073        else
1074        {
1075            if (trim($id3v1['comment']) != trim(substr($id3v2['comment'], 0, 30)))
1076            {
1077                return false;
1078            }
1079        }
1080        return true;
1081    }
1082}
1083
1084if (! function_exists('FILETIMEtoUNIXtime'))
1085{
1086
1087    function FILETIMEtoUNIXtime($FILETIME, $round = true)
1088    {
1089        // FILETIME is a 64-bit unsigned integer representing
1090        // the number of 100-nanosecond intervals since January 1, 1601
1091        // UNIX timestamp is number of seconds since January 1, 1970
1092        // 116444736000000000 = 10000000 * 60 * 60 * 24 * 365 * 369 + 89 leap days
1093        if ($round)
1094        {
1095            return round(($FILETIME - 116444736000000000) / 10000000);
1096        }
1097        return ($FILETIME - 116444736000000000) / 10000000;
1098    }
1099}
1100
1101if (! function_exists('GUIDtoBytestring'))
1102{
1103
1104    function GUIDtoBytestring($GUIDstring)
1105    {
1106        // Microsoft defines these 16-byte (128-bit) GUIDs in the strangest way:
1107        // first 4 bytes are in little-endian order
1108        // next 2 bytes are appended in little-endian order
1109        // next 2 bytes are appended in little-endian order
1110        // next 2 bytes are appended in big-endian order
1111        // next 6 bytes are appended in big-endian order
1112        
1113
1114        // AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp is stored as this 16-byte string:
1115        // $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
1116        
1117
1118        $hexbytecharstring = chr(hexdec(substr($GUIDstring, 6, 2)));
1119        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 4, 2)));
1120        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 2, 2)));
1121        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 0, 2)));
1122        
1123        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 11, 2)));
1124        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 9, 2)));
1125        
1126        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 16, 2)));
1127        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 14, 2)));
1128        
1129        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 19, 2)));
1130        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 21, 2)));
1131        
1132        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 24, 2)));
1133        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 26, 2)));
1134        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 28, 2)));
1135        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 30, 2)));
1136        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 32, 2)));
1137        $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 34, 2)));
1138        
1139        return $hexbytecharstring;
1140    }
1141}
1142
1143if (! function_exists('BytestringToGUID'))
1144{
1145
1146    function BytestringToGUID($Bytestring)
1147    {
1148        $GUIDstring = str_pad(dechex(ord($Bytestring{3})), 2, '0', STR_PAD_LEFT);
1149        $GUIDstring .= str_pad(dechex(ord($Bytestring{2})), 2, '0', STR_PAD_LEFT);
1150        $GUIDstring .= str_pad(dechex(ord($Bytestring{1})), 2, '0', STR_PAD_LEFT);
1151        $GUIDstring .= str_pad(dechex(ord($Bytestring{0})), 2, '0', STR_PAD_LEFT);
1152        $GUIDstring .= '-';
1153        $GUIDstring .= str_pad(dechex(ord($Bytestring{5})), 2, '0', STR_PAD_LEFT);
1154        $GUIDstring .= str_pad(dechex(ord($Bytestring{4})), 2, '0', STR_PAD_LEFT);
1155        $GUIDstring .= '-';
1156        $GUIDstring .= str_pad(dechex(ord($Bytestring{7})), 2, '0', STR_PAD_LEFT);
1157        $GUIDstring .= str_pad(dechex(ord($Bytestring{6})), 2, '0', STR_PAD_LEFT);
1158        $GUIDstring .= '-';
1159        $GUIDstring .= str_pad(dechex(ord($Bytestring{8})), 2, '0', STR_PAD_LEFT);
1160        $GUIDstring .= str_pad(dechex(ord($Bytestring{9})), 2, '0', STR_PAD_LEFT);
1161        $GUIDstring .= '-';
1162        $GUIDstring .= str_pad(dechex(ord($Bytestring{10})), 2, '0', STR_PAD_LEFT);
1163        $GUIDstring .= str_pad(dechex(ord($Bytestring{11})), 2, '0', STR_PAD_LEFT);
1164        $GUIDstring .= str_pad(dechex(ord($Bytestring{12})), 2, '0', STR_PAD_LEFT);
1165        $GUIDstring .= str_pad(dechex(ord($Bytestring{13})), 2, '0', STR_PAD_LEFT);
1166        $GUIDstring .= str_pad(dechex(ord($Bytestring{14})), 2, '0', STR_PAD_LEFT);
1167        $GUIDstring .= str_pad(dechex(ord($Bytestring{15})), 2, '0', STR_PAD_LEFT);
1168        
1169        return strtoupper($GUIDstring);
1170    }
1171}
1172
1173if (! function_exists('BitrateColor'))
1174{
1175
1176    function BitrateColor($bitrate)
1177    {
1178        $bitrate /= 3; // scale from 1-768kbps to 1-256kbps
1179        $bitrate --; // scale from 1-256kbps to 0-255kbps
1180        $bitrate = max($bitrate, 0);
1181        $bitrate = min($bitrate, 255);
1182        //$bitrate = max($bitrate, 32);
1183        //$bitrate = min($bitrate, 143);
1184        //$bitrate = ($bitrate * 2) - 32;
1185        
1186
1187        $Rcomponent = max(255 - ($bitrate * 2), 0);
1188        $Gcomponent = max(($bitrate * 2) - 255, 0);
1189        if ($bitrate > 127)
1190        {
1191            $Bcomponent = max((255 - $bitrate) * 2, 0);
1192        }
1193        else
1194        {
1195            $Bcomponent = max($bitrate * 2, 0);
1196        }
1197        return str_pad(dechex($Rcomponent), 2, '0', STR_PAD_LEFT) . str_pad(dechex($Gcomponent), 2, '0', STR_PAD_LEFT) . str_pad(dechex($Bcomponent), 2, '0', STR_PAD_LEFT);
1198    }
1199}
1200
1201if (! function_exists('BitrateText'))
1202{
1203
1204    function BitrateText($bitrate)
1205    {
1206        return '<SPAN STYLE="color: #' . BitrateColor($bitrate) . '">' . round($bitrate) . ' kbps</SPAN>';
1207    }
1208}
1209
1210if (! function_exists('image_type_to_mime_type'))
1211{
1212
1213    function image_type_to_mime_type($imagetypeid)
1214    {
1215        // only available in PHP v4.3.0+
1216        static $image_type_to_mime_type = array();
1217        if (empty($image_type_to_mime_type))
1218        {
1219            $image_type_to_mime_type[1] = 'image/gif'; // GIF
1220            $image_type_to_mime_type[2] = 'image/jpeg'; // JPEG
1221            $image_type_to_mime_type[3] = 'image/png'; // PNG
1222            $image_type_to_mime_type[4] = 'application/x-shockwave-flash'; // Flash
1223            $image_type_to_mime_type[5] = 'image/psd'; // PSD
1224            $image_type_to_mime_type[6] = 'image/bmp'; // BMP
1225            $image_type_to_mime_type[7] = 'image/tiff'; // TIFF: little-endian (Intel)
1226            $image_type_to_mime_type[8] = 'image/tiff'; // TIFF: big-endian (Motorola)
1227            //$image_type_to_mime_type[9]  = 'image/jpc';                   // JPC
1228            //$image_type_to_mime_type[10] = 'image/jp2';                   // JPC
1229            //$image_type_to_mime_type[11] = 'image/jpx';                   // JPC
1230            //$image_type_to_mime_type[12] = 'image/jb2';                   // JPC
1231            $image_type_to_mime_type[13] = 'application/x-shockwave-flash'; // Shockwave
1232            $image_type_to_mime_type[14] = 'image/iff'; // IFF
1233        }
1234        return (isset($image_type_to_mime_type[$imagetypeid]) ? $image_type_to_mime_type[$imagetypeid] : 'application/octet-stream');
1235    }
1236}
1237
1238if (! function_exists('utf8_decode'))
1239{
1240
1241    // PHP has this function built-in if it's configured with the --with-xml option
1242    // This version of the function is only provided in case XML isn't installed
1243    function utf8_decode($utf8text)
1244    {
1245        // http://www.php.net/manual/en/function.utf8-encode.php
1246        // bytes  bits  representation
1247        //   1     7    0bbbbbbb
1248        //   2     11   110bbbbb 10bbbbbb
1249        //   3     16   1110bbbb 10bbbbbb 10bbbbbb
1250        //   4     21   11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
1251        
1252
1253        $utf8length = strlen($utf8text);
1254        $decodedtext = '';
1255        for($i = 0; $i < $utf8length; $i ++)
1256        {
1257            if ((ord($utf8text{$i}) & 0x80) == 0)
1258            {
1259                $decodedtext .= $utf8text{$i};
1260            }
1261            elseif ((ord($utf8text{$i}) & 0xF0) == 0xF0)
1262            {
1263                $decodedtext .= '?';
1264                $i += 3;
1265            }
1266            elseif ((ord($utf8text{$i}) & 0xE0) == 0xE0)
1267            {
1268                $decodedtext .= '?';
1269                $i += 2;
1270            }
1271            elseif ((ord($utf8text{$i}) & 0xC0) == 0xC0)
1272            {
1273                //   2     11   110bbbbb 10bbbbbb
1274                $decodedchar = Bin2Dec(substr(Dec2Bin(ord($utf8text{$i})), 3, 5) . substr(Dec2Bin(ord($utf8text{($i + 1)})), 2, 6));
1275                if ($decodedchar <= 255)
1276                {
1277                    $decodedtext .= chr($decodedchar);
1278                }
1279                else
1280                {
1281                    $decodedtext .= '?';
1282                }
1283                $i += 1;
1284            }
1285        }
1286        return $decodedtext;
1287    }
1288}
1289
1290if (! function_exists('DateMac2Unix'))
1291{
1292
1293    function DateMac2Unix($macdate)
1294    {
1295        // Macintosh timestamp: seconds since 00:00h January 1, 1904
1296        // UNIX timestamp:      seconds since 00:00h January 1, 1970
1297        return CastAsInt($macdate - 2082844800);
1298    }
1299}
1300
1301if (! function_exists('FixedPoint8_8'))
1302{
1303
1304    function FixedPoint8_8($rawdata)
1305    {
1306        return BigEndian2Int(substr($rawdata, 0, 1)) + (float) (BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
1307    }
1308}
1309
1310if (! function_exists('FixedPoint16_16'))
1311{
1312
1313    function FixedPoint16_16($rawdata)
1314    {
1315        return BigEndian2Int(substr($rawdata, 0, 2)) + (float) (BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
1316    }
1317}
1318
1319if (! function_exists('FixedPoint2_30'))
1320{
1321
1322    function FixedPoint2_30($rawdata)
1323    {
1324        $binarystring = BigEndian2Bin($rawdata);
1325        return Bin2Dec(substr($binarystring, 0, 2)) + (float) (Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
1326    }
1327}
1328
1329if (! function_exists('Pascal2String'))
1330{
1331
1332    function Pascal2String($pascalstring)
1333    {
1334        // Pascal strings have 1 byte at the beginning saying how many chars are in the string
1335        return substr($pascalstring, 1);
1336    }
1337}
1338
1339if (! function_exists('NoNullString'))
1340{
1341
1342    function NoNullString($nullterminatedstring)
1343    {
1344        // remove the single null terminator on null terminated strings
1345        if (substr($nullterminatedstring, strlen($nullterminatedstring) - 1, 1) === chr(0))
1346        {
1347            return substr($nullterminatedstring, 0, strlen($nullterminatedstring) - 1);
1348        }
1349        return $nullterminatedstring;
1350    }
1351}
1352
1353if (! function_exists('FileSizeNiceDisplay'))
1354{
1355
1356    function FileSizeNiceDisplay($filesize, $precision = 2)
1357    {
1358        if ($filesize < 1000)
1359        {
1360            $sizeunit = 'bytes';
1361            $precision = 0;
1362        }
1363        else
1364        {
1365            $filesize /= 1024;
1366            $sizeunit = 'kB';
1367        }
1368        if ($filesize >= 1000)
1369        {
1370            $filesize /= 1024;
1371            $sizeunit = 'MB';
1372        }
1373        if ($filesize >= 1000)
1374        {
1375            $filesize /= 1024;
1376            $sizeunit = 'GB';
1377        }
1378        return number_format($filesize, $precision) . ' ' . $sizeunit;
1379    }
1380}
1381
1382if (! function_exists('DOStime2UNIXtime'))
1383{
1384
1385    function DOStime2UNIXtime($DOSdate, $DOStime)
1386    {
1387        // wFatDate
1388        // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
1389        // Bits      Contents
1390        // 0-4    Day of the month (1-31)
1391        // 5-8    Month (1 = January, 2 = February, and so on)
1392        // 9-15   Year offset from 1980 (add 1980 to get actual year)
1393        
1394
1395        $UNIXday = ($DOSdate & 0x001F);
1396        $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
1397        $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
1398        
1399        // wFatTime
1400        // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
1401        // Bits   Contents
1402        // 0-4    Second divided by 2
1403        // 5-10   Minute (0-59)
1404        // 11-15  Hour (0-23 on a 24-hour clock)
1405        
1406
1407        $UNIXsecond = ($DOStime & 0x001F) * 2;
1408        $UNIXminute = (($DOStime & 0x07E0) >> 5);
1409        $UNIXhour = (($DOStime & 0xF800) >> 11);
1410        
1411        return mktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
1412    }
1413}
1414
1415if (! function_exists('CreateDeepArray'))
1416{
1417
1418    function CreateDeepArray($ArrayPath, $Separator, $Value)
1419    {
1420        // assigns $Value to a nested array path:
1421        //   $foo = CreateDeepArray('/path/to/my', '/', 'file.txt')
1422        // is the same as:
1423        //   $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
1424        // or
1425        //   $foo['path']['to']['my'] = 'file.txt';
1426        while ($ArrayPath{0} == $Separator)
1427        {
1428            $ArrayPath = substr($ArrayPath, 1);
1429        }
1430        if (($pos = strpos($ArrayPath, $Separator)) !== false)
1431        {
1432            $ReturnedArray[substr($ArrayPath, 0, $pos)] = CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
1433        }
1434        else
1435        {
1436            $ReturnedArray["$ArrayPath"] = $Value;
1437        }
1438        return $ReturnedArray;
1439    }
1440}
1441
1442if (! function_exists('md5_file'))
1443{
1444
1445    // Allan Hansen <ah@artemis.dk>
1446    // md5_file() exists in PHP 4.2.0.
1447    // The following works under UNIX only, but dies on windows
1448    function md5_file($file)
1449    {
1450        if (substr(php_uname(), 0, 7) == 'Windows')
1451        {
1452            die('PHP 4.2.0 or newer required for md5_file()');
1453        }
1454        
1455        $file = str_replace('`', '\\`', $file);
1456        if (ereg("^([0-9a-f]{32})[ \t\n\r]", `md5sum "$file"`, $r))
1457        {
1458            return $r[1];
1459        }
1460        return false;
1461    }
1462}
1463
1464if (! function_exists('md5_data'))
1465{
1466
1467    // Allan Hansen <ah@artemis.dk>
1468    // md5_data() - returns md5sum for a file from startuing position to absolute end position
1469    
1470
1471    function md5_data($file, $offset, $end, $invertsign = false)
1472    {
1473        // first try and create a temporary file in the same directory as the file being scanned
1474        if (($dataMD5filename = tempnam(dirname($file), eregi_replace('[^[:alnum:]]', '', basename($file)))) === false)
1475        {
1476            // if that fails, create a temporary file in the system temp directory
1477            if (($dataMD5filename = tempnam('/tmp', 'getID3')) === false)
1478            {
1479                // if that fails, create a temporary file in the current directory
1480                if (($dataMD5filename = tempnam('.', eregi_replace('[^[:alnum:]]', '', basename($file)))) === false)
1481                {
1482                    // can't find anywhere to create a temp file, just die
1483                    return false;
1484                }
1485            }
1486        }
1487        $md5 = false;
1488        set_time_limit(max(filesize($file) / 1000000, 30));
1489        
1490        // copy parts of file
1491        if ($fp = @fopen($file, 'rb'))
1492        {
1493            
1494            if ($MD5fp = @fopen($dataMD5filename, 'wb'))
1495            {
1496                
1497                if ($invertsign)
1498                {
1499                    // Load conversion lookup strings for 8-bit unsigned->signed conversion below
1500                    $from = '';
1501                    $to = '';
1502                    for($i = 0; $i < 128; $i ++)
1503                    {
1504                        $from .= chr($i);
1505                        $to .= chr($i + 128);
1506                    }
1507                    for($i = 128; $i < 256; $i ++)
1508                    {
1509                        $from .= chr($i);
1510                        $to .= chr($i - 128);
1511                    }
1512                }
1513                
1514                fseek($fp, $offset, SEEK_SET);
1515                $byteslefttowrite = $end - $offset;
1516                while (($byteslefttowrite > 0) && ($buffer = fread($fp, FREAD_BUFFER_SIZE)))
1517                {
1518                    if ($invertsign)
1519                    {
1520                        // Possibly FLAC-specific (?)
1521                        // FLAC calculates the MD5sum of the source data of 8-bit files
1522                        // not on the actual byte values in the source file, but of those
1523                        // values converted from unsigned to signed, or more specifcally,
1524                        // with the MSB inverted. ex: 01 -> 81; F5 -> 75; etc
1525                        
1526
1527                        // Therefore, 8-bit WAV data has to be converted before getting the
1528                        // md5_data value so as to match the FLAC value
1529                        
1530
1531                        // Flip the MSB for each byte in the buffer before copying
1532                        $buffer = strtr($buffer, $from, $to);
1533                    }
1534                    $byteswritten = fwrite($MD5fp, $buffer, $byteslefttowrite);
1535                    $byteslefttowrite -= $byteswritten…

Large files files are truncated, but you can click here to view the full file