• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/MediaObjects/getid3/getid3/getid3.lib.php

Go to the documentation of this file.
00001 <?php
00004 //  available at http://getid3.sourceforge.net                 //
00005 //            or http://www.getid3.org                         //
00007 //                                                             //
00008 // getid3.lib.php - part of getID3()                           //
00009 // See readme.txt for more details                             //
00010 //                                                            ///
00012 
00013 
00014 class getid3_lib
00015 {
00016 
00017         function PrintHexBytes($string, $hex=true, $spaces=true, $htmlsafe=true) {
00018                 $returnstring = '';
00019                 for ($i = 0; $i < strlen($string); $i++) {
00020                         if ($hex) {
00021                                 $returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
00022                         } else {
00023                                 $returnstring .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '');
00024                         }
00025                         if ($spaces) {
00026                                 $returnstring .= ' ';
00027                         }
00028                 }
00029                 if ($htmlsafe) {
00030                         $returnstring = htmlentities($returnstring);
00031                 }
00032                 return $returnstring;
00033         }
00034 
00035         function SafeStripSlashes($text) {
00036                 if (get_magic_quotes_gpc()) {
00037                         return stripslashes($text);
00038                 }
00039                 return $text;
00040         }
00041 
00042 
00043         function trunc($floatnumber) {
00044                 // truncates a floating-point number at the decimal point
00045                 // returns int (if possible, otherwise float)
00046                 if ($floatnumber >= 1) {
00047                         $truncatednumber = floor($floatnumber);
00048                 } elseif ($floatnumber <= -1) {
00049                         $truncatednumber = ceil($floatnumber);
00050                 } else {
00051                         $truncatednumber = 0;
00052                 }
00053                 if ($truncatednumber <= 1073741824) { // 2^30
00054                         $truncatednumber = (int) $truncatednumber;
00055                 }
00056                 return $truncatednumber;
00057         }
00058 
00059 
00060         function CastAsInt($floatnum) {
00061                 // convert to float if not already
00062                 $floatnum = (float) $floatnum;
00063 
00064                 // convert a float to type int, only if possible
00065                 if (getid3_lib::trunc($floatnum) == $floatnum) {
00066                         // it's not floating point
00067                         if ($floatnum <= 1073741824) { // 2^30
00068                                 // it's within int range
00069                                 $floatnum = (int) $floatnum;
00070                         }
00071                 }
00072                 return $floatnum;
00073         }
00074 
00075 
00076         function DecimalBinary2Float($binarynumerator) {
00077                 $numerator   = getid3_lib::Bin2Dec($binarynumerator);
00078                 $denominator = getid3_lib::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
00079                 return ($numerator / $denominator);
00080         }
00081 
00082 
00083         function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
00084                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
00085                 if (strpos($binarypointnumber, '.') === false) {
00086                         $binarypointnumber = '0.'.$binarypointnumber;
00087                 } elseif ($binarypointnumber{0} == '.') {
00088                         $binarypointnumber = '0'.$binarypointnumber;
00089                 }
00090                 $exponent = 0;
00091                 while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
00092                         if (substr($binarypointnumber, 1, 1) == '.') {
00093                                 $exponent--;
00094                                 $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
00095                         } else {
00096                                 $pointpos = strpos($binarypointnumber, '.');
00097                                 $exponent += ($pointpos - 1);
00098                                 $binarypointnumber = str_replace('.', '', $binarypointnumber);
00099                                 $binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
00100                         }
00101                 }
00102                 $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
00103                 return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
00104         }
00105 
00106 
00107         function Float2BinaryDecimal($floatvalue) {
00108                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
00109                 $maxbits = 128; // to how many bits of precision should the calculations be taken?
00110                 $intpart   = getid3_lib::trunc($floatvalue);
00111                 $floatpart = abs($floatvalue - $intpart);
00112                 $pointbitstring = '';
00113                 while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
00114                         $floatpart *= 2;
00115                         $pointbitstring .= (string) getid3_lib::trunc($floatpart);
00116                         $floatpart -= getid3_lib::trunc($floatpart);
00117                 }
00118                 $binarypointnumber = decbin($intpart).'.'.$pointbitstring;
00119                 return $binarypointnumber;
00120         }
00121 
00122 
00123         function Float2String($floatvalue, $bits) {
00124                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
00125                 switch ($bits) {
00126                         case 32:
00127                                 $exponentbits = 8;
00128                                 $fractionbits = 23;
00129                                 break;
00130 
00131                         case 64:
00132                                 $exponentbits = 11;
00133                                 $fractionbits = 52;
00134                                 break;
00135 
00136                         default:
00137                                 return false;
00138                                 break;
00139                 }
00140                 if ($floatvalue >= 0) {
00141                         $signbit = '0';
00142                 } else {
00143                         $signbit = '1';
00144                 }
00145                 $normalizedbinary  = getid3_lib::NormalizeBinaryPoint(getid3_lib::Float2BinaryDecimal($floatvalue), $fractionbits);
00146                 $biasedexponent    = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
00147                 $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
00148                 $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
00149 
00150                 return getid3_lib::BigEndian2String(getid3_lib::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
00151         }
00152 
00153 
00154         function LittleEndian2Float($byteword) {
00155                 return getid3_lib::BigEndian2Float(strrev($byteword));
00156         }
00157 
00158 
00159         function BigEndian2Float($byteword) {
00160                 // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
00161                 // http://www.psc.edu/general/software/packages/ieee/ieee.html
00162                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
00163 
00164                 $bitword = getid3_lib::BigEndian2Bin($byteword);
00165                 $signbit = $bitword{0};
00166 
00167                 switch (strlen($byteword) * 8) {
00168                         case 32:
00169                                 $exponentbits = 8;
00170                                 $fractionbits = 23;
00171                                 break;
00172 
00173                         case 64:
00174                                 $exponentbits = 11;
00175                                 $fractionbits = 52;
00176                                 break;
00177 
00178                         case 80:
00179                                 // 80-bit Apple SANE format
00180                                 // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
00181                                 $exponentstring = substr($bitword, 1, 15);
00182                                 $isnormalized = intval($bitword{16});
00183                                 $fractionstring = substr($bitword, 17, 63);
00184                                 $exponent = pow(2, getid3_lib::Bin2Dec($exponentstring) - 16383);
00185                                 $fraction = $isnormalized + getid3_lib::DecimalBinary2Float($fractionstring);
00186                                 $floatvalue = $exponent * $fraction;
00187                                 if ($signbit == '1') {
00188                                         $floatvalue *= -1;
00189                                 }
00190                                 return $floatvalue;
00191                                 break;
00192 
00193                         default:
00194                                 return false;
00195                                 break;
00196                 }
00197                 $exponentstring = substr($bitword, 1, $exponentbits);
00198                 $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
00199                 $exponent = getid3_lib::Bin2Dec($exponentstring);
00200                 $fraction = getid3_lib::Bin2Dec($fractionstring);
00201 
00202                 if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
00203                         // Not a Number
00204                         $floatvalue = false;
00205                 } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
00206                         if ($signbit == '1') {
00207                                 $floatvalue = '-infinity';
00208                         } else {
00209                                 $floatvalue = '+infinity';
00210                         }
00211                 } elseif (($exponent == 0) && ($fraction == 0)) {
00212                         if ($signbit == '1') {
00213                                 $floatvalue = -0;
00214                         } else {
00215                                 $floatvalue = 0;
00216                         }
00217                         $floatvalue = ($signbit ? 0 : -0);
00218                 } elseif (($exponent == 0) && ($fraction != 0)) {
00219                         // These are 'unnormalized' values
00220                         $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fractionstring);
00221                         if ($signbit == '1') {
00222                                 $floatvalue *= -1;
00223                         }
00224                 } elseif ($exponent != 0) {
00225                         $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fractionstring));
00226                         if ($signbit == '1') {
00227                                 $floatvalue *= -1;
00228                         }
00229                 }
00230                 return (float) $floatvalue;
00231         }
00232 
00233 
00234         function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
00235                 $intvalue = 0;
00236                 $bytewordlen = strlen($byteword);
00237                 for ($i = 0; $i < $bytewordlen; $i++) {
00238                         if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
00239                                 $intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
00240                         } else {
00241                                 $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
00242                         }
00243                 }
00244                 if ($signed && !$synchsafe) {
00245                         // synchsafe ints are not allowed to be signed
00246                         switch ($bytewordlen) {
00247                                 case 1:
00248                                 case 2:
00249                                 case 3:
00250                                 case 4:
00251                                         $signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
00252                                         if ($intvalue & $signmaskbit) {
00253                                                 $intvalue = 0 - ($intvalue & ($signmaskbit - 1));
00254                                         }
00255                                         break;
00256 
00257                                 default:
00258                                         die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2Int()');
00259                                         break;
00260                         }
00261                 }
00262                 return getid3_lib::CastAsInt($intvalue);
00263         }
00264 
00265 
00266         function LittleEndian2Int($byteword, $signed=false) {
00267                 return getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
00268         }
00269 
00270 
00271         function BigEndian2Bin($byteword) {
00272                 $binvalue = '';
00273                 $bytewordlen = strlen($byteword);
00274                 for ($i = 0; $i < $bytewordlen; $i++) {
00275                         $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
00276                 }
00277                 return $binvalue;
00278         }
00279 
00280 
00281         function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
00282                 if ($number < 0) {
00283                         return false;
00284                 }
00285                 $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
00286                 $intstring = '';
00287                 if ($signed) {
00288                         if ($minbytes > 4) {
00289                                 die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2String()');
00290                         }
00291                         $number = $number & (0x80 << (8 * ($minbytes - 1)));
00292                 }
00293                 while ($number != 0) {
00294                         $quotient = ($number / ($maskbyte + 1));
00295                         $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
00296                         $number = floor($quotient);
00297                 }
00298                 return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
00299         }
00300 
00301 
00302         function Dec2Bin($number) {
00303                 while ($number >= 256) {
00304                         $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
00305                         $number = floor($number / 256);
00306                 }
00307                 $bytes[] = $number;
00308                 $binstring = '';
00309                 for ($i = 0; $i < count($bytes); $i++) {
00310                         $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
00311                 }
00312                 return $binstring;
00313         }
00314 
00315 
00316         function Bin2Dec($binstring, $signed=false) {
00317                 $signmult = 1;
00318                 if ($signed) {
00319                         if ($binstring{0} == '1') {
00320                                 $signmult = -1;
00321                         }
00322                         $binstring = substr($binstring, 1);
00323                 }
00324                 $decvalue = 0;
00325                 for ($i = 0; $i < strlen($binstring); $i++) {
00326                         $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
00327                 }
00328                 return getid3_lib::CastAsInt($decvalue * $signmult);
00329         }
00330 
00331 
00332         function Bin2String($binstring) {
00333                 // return 'hi' for input of '0110100001101001'
00334                 $string = '';
00335                 $binstringreversed = strrev($binstring);
00336                 for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
00337                         $string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
00338                 }
00339                 return $string;
00340         }
00341 
00342 
00343         function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
00344                 $intstring = '';
00345                 while ($number > 0) {
00346                         if ($synchsafe) {
00347                                 $intstring = $intstring.chr($number & 127);
00348                                 $number >>= 7;
00349                         } else {
00350                                 $intstring = $intstring.chr($number & 255);
00351                                 $number >>= 8;
00352                         }
00353                 }
00354                 return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
00355         }
00356 
00357 
00358         function array_merge_clobber($array1, $array2) {
00359                 // written by kchireability*com
00360                 // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
00361                 if (!is_array($array1) || !is_array($array2)) {
00362                         return false;
00363                 }
00364                 $newarray = $array1;
00365                 foreach ($array2 as $key => $val) {
00366                         if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
00367                                 $newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
00368                         } else {
00369                                 $newarray[$key] = $val;
00370                         }
00371                 }
00372                 return $newarray;
00373         }
00374 
00375 
00376         function array_merge_noclobber($array1, $array2) {
00377                 if (!is_array($array1) || !is_array($array2)) {
00378                         return false;
00379                 }
00380                 $newarray = $array1;
00381                 foreach ($array2 as $key => $val) {
00382                         if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
00383                                 $newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
00384                         } elseif (!isset($newarray[$key])) {
00385                                 $newarray[$key] = $val;
00386                         }
00387                 }
00388                 return $newarray;
00389         }
00390 
00391 
00392         function fileextension($filename, $numextensions=1) {
00393                 if (strstr($filename, '.')) {
00394                         $reversedfilename = strrev($filename);
00395                         $offset = 0;
00396                         for ($i = 0; $i < $numextensions; $i++) {
00397                                 $offset = strpos($reversedfilename, '.', $offset + 1);
00398                                 if ($offset === false) {
00399                                         return '';
00400                                 }
00401                         }
00402                         return strrev(substr($reversedfilename, 0, $offset));
00403                 }
00404                 return '';
00405         }
00406 
00407 
00408         function PlaytimeString($playtimeseconds) {
00409                 $contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
00410                 $contentminutes = floor($playtimeseconds / 60);
00411                 if ($contentseconds >= 60) {
00412                         $contentseconds -= 60;
00413                         $contentminutes++;
00414                 }
00415                 return intval($contentminutes).':'.str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
00416         }
00417 
00418 
00419         function image_type_to_mime_type($imagetypeid) {
00420                 // only available in PHP v4.3.0+
00421                 static $image_type_to_mime_type = array();
00422                 if (empty($image_type_to_mime_type)) {
00423                         $image_type_to_mime_type[1]  = 'image/gif';                     // GIF
00424                         $image_type_to_mime_type[2]  = 'image/jpeg';                    // JPEG
00425                         $image_type_to_mime_type[3]  = 'image/png';                     // PNG
00426                         $image_type_to_mime_type[4]  = 'application/x-shockwave-flash'; // Flash
00427                         $image_type_to_mime_type[5]  = 'image/psd';                     // PSD
00428                         $image_type_to_mime_type[6]  = 'image/bmp';                     // BMP
00429                         $image_type_to_mime_type[7]  = 'image/tiff';                    // TIFF: little-endian (Intel)
00430                         $image_type_to_mime_type[8]  = 'image/tiff';                    // TIFF: big-endian (Motorola)
00431                         //$image_type_to_mime_type[9]  = 'image/jpc';                   // JPC
00432                         //$image_type_to_mime_type[10] = 'image/jp2';                   // JPC
00433                         //$image_type_to_mime_type[11] = 'image/jpx';                   // JPC
00434                         //$image_type_to_mime_type[12] = 'image/jb2';                   // JPC
00435                         $image_type_to_mime_type[13] = 'application/x-shockwave-flash'; // Shockwave
00436                         $image_type_to_mime_type[14] = 'image/iff';                     // IFF
00437                 }
00438                 return (isset($image_type_to_mime_type[$imagetypeid]) ? $image_type_to_mime_type[$imagetypeid] : 'application/octet-stream');
00439         }
00440 
00441 
00442         function DateMac2Unix($macdate) {
00443                 // Macintosh timestamp: seconds since 00:00h January 1, 1904
00444                 // UNIX timestamp:      seconds since 00:00h January 1, 1970
00445                 return getid3_lib::CastAsInt($macdate - 2082844800);
00446         }
00447 
00448 
00449         function FixedPoint8_8($rawdata) {
00450                 return getid3_lib::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
00451         }
00452 
00453 
00454         function FixedPoint16_16($rawdata) {
00455                 return getid3_lib::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
00456         }
00457 
00458 
00459         function FixedPoint2_30($rawdata) {
00460                 $binarystring = getid3_lib::BigEndian2Bin($rawdata);
00461                 return getid3_lib::Bin2Dec(substr($binarystring, 0, 2)) + (float) (getid3_lib::Bin2Dec(substr($binarystring, 2, 30)) / 1073741824);
00462         }
00463 
00464 
00465         function CreateDeepArray($ArrayPath, $Separator, $Value) {
00466                 // assigns $Value to a nested array path:
00467                 //   $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
00468                 // is the same as:
00469                 //   $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
00470                 // or
00471                 //   $foo['path']['to']['my'] = 'file.txt';
00472                 while ($ArrayPath && ($ArrayPath{0} == $Separator)) {
00473                         $ArrayPath = substr($ArrayPath, 1);
00474                 }
00475                 if (($pos = strpos($ArrayPath, $Separator)) !== false) {
00476                         $ReturnedArray[substr($ArrayPath, 0, $pos)] = getid3_lib::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
00477                 } else {
00478                         $ReturnedArray[$ArrayPath] = $Value;
00479                 }
00480                 return $ReturnedArray;
00481         }
00482 
00483         function array_max($arraydata, $returnkey=false) {
00484                 $maxvalue = false;
00485                 $maxkey = false;
00486                 foreach ($arraydata as $key => $value) {
00487                         if (!is_array($value)) {
00488                                 if ($value > $maxvalue) {
00489                                         $maxvalue = $value;
00490                                         $maxkey = $key;
00491                                 }
00492                         }
00493                 }
00494                 return ($returnkey ? $maxkey : $maxvalue);
00495         }
00496 
00497         function array_min($arraydata, $returnkey=false) {
00498                 $minvalue = false;
00499                 $minkey = false;
00500                 foreach ($arraydata as $key => $value) {
00501                         if (!is_array($value)) {
00502                                 if ($value > $minvalue) {
00503                                         $minvalue = $value;
00504                                         $minkey = $key;
00505                                 }
00506                         }
00507                 }
00508                 return ($returnkey ? $minkey : $minvalue);
00509         }
00510 
00511 
00512         function md5_file($file) {
00513 
00514                 // md5_file() exists in PHP 4.2.0+.
00515                 if (function_exists('md5_file')) {
00516                         return md5_file($file);
00517                 }
00518 
00519                 if (GETID3_OS_ISWINDOWS) {
00520 
00521                         $RequiredFiles = array('cygwin1.dll', 'md5sum.exe');
00522                         foreach ($RequiredFiles as $required_file) {
00523                                 if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
00524                                         die(implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::md5_file() to function under Windows in PHP < v4.2.0');
00525                                 }
00526                         }
00527                         $commandline = GETID3_HELPERAPPSDIR.'md5sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
00528                         if (ereg("^[\\]?([0-9a-f]{32})", strtolower(`$commandline`), $r)) {
00529                                 return $r[1];
00530                         }
00531 
00532                 } else {
00533 
00534                         // The following works under UNIX only
00535                         $file = str_replace('`', '\\`', $file);
00536                         if (ereg("^([0-9a-f]{32})[ \t\n\r]", `md5sum "$file"`, $r)) {
00537                                 return $r[1];
00538                         }
00539 
00540                 }
00541                 return false;
00542         }
00543 
00544 
00545         function sha1_file($file) {
00546 
00547                 // sha1_file() exists in PHP 4.3.0+.
00548                 if (function_exists('sha1_file')) {
00549                         return sha1_file($file);
00550                 }
00551 
00552                 $file = str_replace('`', '\\`', $file);
00553 
00554                 if (GETID3_OS_ISWINDOWS) {
00555 
00556                         $RequiredFiles = array('cygwin1.dll', 'sha1sum.exe');
00557                         foreach ($RequiredFiles as $required_file) {
00558                                 if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
00559                                         die(implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::sha1_file() to function under Windows in PHP < v4.3.0');
00560                                 }
00561                         }
00562                         $commandline = GETID3_HELPERAPPSDIR.'sha1sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
00563                         if (ereg("^sha1=([0-9a-f]{40})", strtolower(`$commandline`), $r)) {
00564                                 return $r[1];
00565                         }
00566 
00567                 } else {
00568 
00569                         $commandline = 'sha1sum '.escapeshellarg($file).'';
00570                         if (ereg("^([0-9a-f]{40})[ \t\n\r]", strtolower(`$commandline`), $r)) {
00571                                 return $r[1];
00572                         }
00573 
00574                 }
00575 
00576                 return false;
00577         }
00578 
00579 
00580         // Allan Hansen <ahartemis*dk>
00581         // getid3_lib::md5_data() - returns md5sum for a file from startuing position to absolute end position
00582         function hash_data($file, $offset, $end, $algorithm) {
00583 
00584                 switch ($algorithm) {
00585                         case 'md5':
00586                                 $hash_function = 'md5_file';
00587                                 $unix_call     = 'md5sum';
00588                                 $windows_call  = 'md5sum.exe';
00589                                 $hash_length   = 32;
00590                                 break;
00591 
00592                         case 'sha1':
00593                                 $hash_function = 'sha1_file';
00594                                 $unix_call     = 'sha1sum';
00595                                 $windows_call  = 'sha1sum.exe';
00596                                 $hash_length   = 40;
00597                                 break;
00598 
00599                         default:
00600                                 die('Invalid algorithm ('.$algorithm.') in getid3_lib::hash_data()');
00601                                 break;
00602                 }
00603                 $size = $end - $offset;
00604                 while (true) {
00605                         if (GETID3_OS_ISWINDOWS) {
00606 
00607                                 // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
00608                                 // Fall back to create-temp-file method:
00609                                 if ($algorithm == 'sha1') {
00610                                         break;
00611                                 }
00612 
00613                                 $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
00614                                 foreach ($RequiredFiles as $required_file) {
00615                                         if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
00616                                                 // helper apps not available - fall back to old method
00617                                                 break;
00618                                         }
00619                                 }
00620                                 $commandline  = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' "'.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).'" | ';
00621                                 $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
00622                                 $commandline .= GETID3_HELPERAPPSDIR.$windows_call;
00623 
00624                         } else {
00625 
00626                                 $commandline  = 'head -c'.$end.' '.escapeshellarg($file).' | ';
00627                                 $commandline .= 'tail -c'.$size.' | ';
00628                                 $commandline .= $unix_call;
00629 
00630                         }
00631                         if ((bool) ini_get('safe_mode')) {
00632                                 $ThisFileInfo['warning'][] = 'PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm';
00633                                 break;
00634                         }
00635                         return substr(`$commandline`, 0, $hash_length);
00636                 }
00637 
00638                 // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
00639                 if (($data_filename = tempnam('*', 'getID3')) === false) {
00640                         // can't find anywhere to create a temp file, just die
00641                         return false;
00642                 }
00643 
00644                 // Init
00645                 $result = false;
00646 
00647                 // copy parts of file
00648                 if ($fp = @fopen($file, 'rb')) {
00649 
00650                         if ($fp_data = @fopen($data_filename, 'wb')) {
00651 
00652                                 fseek($fp, $offset, SEEK_SET);
00653                                 $byteslefttowrite = $end - $offset;
00654                                 while (($byteslefttowrite > 0) && ($buffer = fread($fp, GETID3_FREAD_BUFFER_SIZE))) {
00655                                         $byteswritten = fwrite($fp_data, $buffer, $byteslefttowrite);
00656                                         $byteslefttowrite -= $byteswritten;
00657                                 }
00658                                 fclose($fp_data);
00659                                 $result = getid3_lib::$hash_function($data_filename);
00660 
00661                         }
00662                         fclose($fp);
00663                 }
00664                 unlink($data_filename);
00665                 return $result;
00666         }
00667 
00668 
00669         function iconv_fallback_int_utf8($charval) {
00670                 if ($charval < 128) {
00671                         // 0bbbbbbb
00672                         $newcharstring = chr($charval);
00673                 } elseif ($charval < 2048) {
00674                         // 110bbbbb 10bbbbbb
00675                         $newcharstring  = chr(($charval >> 6) | 0xC0);
00676                         $newcharstring .= chr(($charval & 0x3F) | 0x80);
00677                 } elseif ($charval < 65536) {
00678                         // 1110bbbb 10bbbbbb 10bbbbbb
00679                         $newcharstring  = chr(($charval >> 12) | 0xE0);
00680                         $newcharstring .= chr(($charval >>  6) | 0xC0);
00681                         $newcharstring .= chr(($charval & 0x3F) | 0x80);
00682                 } else {
00683                         // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
00684                         $newcharstring  = chr(($charval >> 18) | 0xF0);
00685                         $newcharstring .= chr(($charval >> 12) | 0xC0);
00686                         $newcharstring .= chr(($charval >>  6) | 0xC0);
00687                         $newcharstring .= chr(($charval & 0x3F) | 0x80);
00688                 }
00689                 return $newcharstring;
00690         }
00691 
00692         // ISO-8859-1 => UTF-8
00693         function iconv_fallback_iso88591_utf8($string, $bom=false) {
00694                 if (function_exists('utf8_encode')) {
00695                         return utf8_encode($string);
00696                 }
00697                 // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
00698                 $newcharstring = '';
00699                 if ($bom) {
00700                         $newcharstring .= "\xEF\xBB\xBF";
00701                 }
00702                 for ($i = 0; $i < strlen($string); $i++) {
00703                         $charval = ord($string{$i});
00704                         $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
00705                 }
00706                 return $newcharstring;
00707         }
00708 
00709         // ISO-8859-1 => UTF-16BE
00710         function iconv_fallback_iso88591_utf16be($string, $bom=false) {
00711                 $newcharstring = '';
00712                 if ($bom) {
00713                         $newcharstring .= "\xFE\xFF";
00714                 }
00715                 for ($i = 0; $i < strlen($string); $i++) {
00716                         $newcharstring .= "\x00".$string{$i};
00717                 }
00718                 return $newcharstring;
00719         }
00720 
00721         // ISO-8859-1 => UTF-16LE
00722         function iconv_fallback_iso88591_utf16le($string, $bom=false) {
00723                 $newcharstring = '';
00724                 if ($bom) {
00725                         $newcharstring .= "\xFF\xFE";
00726                 }
00727                 for ($i = 0; $i < strlen($string); $i++) {
00728                         $newcharstring .= $string{$i}."\x00";
00729                 }
00730                 return $newcharstring;
00731         }
00732 
00733         // ISO-8859-1 => UTF-16LE (BOM)
00734         function iconv_fallback_iso88591_utf16($string) {
00735                 return getid3_lib::iconv_fallback_iso88591_utf16le($string, true);
00736         }
00737 
00738         // UTF-8 => ISO-8859-1
00739         function iconv_fallback_utf8_iso88591($string) {
00740                 if (function_exists('utf8_decode')) {
00741                         return utf8_decode($string);
00742                 }
00743                 // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
00744                 $newcharstring = '';
00745                 $offset = 0;
00746                 $stringlength = strlen($string);
00747                 while ($offset < $stringlength) {
00748                         if ((ord($string{$offset}) | 0x07) == 0xF7) {
00749                                 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
00750                                 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
00751                                            ((ord($string{($offset + 1)}) & 0x3F) << 12) &
00752                                            ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
00753                                             (ord($string{($offset + 3)}) & 0x3F);
00754                                 $offset += 4;
00755                         } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
00756                                 // 1110bbbb 10bbbbbb 10bbbbbb
00757                                 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
00758                                            ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
00759                                             (ord($string{($offset + 2)}) & 0x3F);
00760                                 $offset += 3;
00761                         } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
00762                                 // 110bbbbb 10bbbbbb
00763                                 $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
00764                                             (ord($string{($offset + 1)}) & 0x3F);
00765                                 $offset += 2;
00766                         } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
00767                                 // 0bbbbbbb
00768                                 $charval = ord($string{$offset});
00769                                 $offset += 1;
00770                         } else {
00771                                 // error? throw some kind of warning here?
00772                                 $charval = false;
00773                                 $offset += 1;
00774                         }
00775                         if ($charval !== false) {
00776                                 $newcharstring .= (($charval < 256) ? chr($charval) : '?');
00777                         }
00778                 }
00779                 return $newcharstring;
00780         }
00781 
00782         // UTF-8 => UTF-16BE
00783         function iconv_fallback_utf8_utf16be($string, $bom=false) {
00784                 $newcharstring = '';
00785                 if ($bom) {
00786                         $newcharstring .= "\xFE\xFF";
00787                 }
00788                 $offset = 0;
00789                 $stringlength = strlen($string);
00790                 while ($offset < $stringlength) {
00791                         if ((ord($string{$offset}) | 0x07) == 0xF7) {
00792                                 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
00793                                 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
00794                                            ((ord($string{($offset + 1)}) & 0x3F) << 12) &
00795                                            ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
00796                                             (ord($string{($offset + 3)}) & 0x3F);
00797                                 $offset += 4;
00798                         } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
00799                                 // 1110bbbb 10bbbbbb 10bbbbbb
00800                                 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
00801                                            ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
00802                                             (ord($string{($offset + 2)}) & 0x3F);
00803                                 $offset += 3;
00804                         } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
00805                                 // 110bbbbb 10bbbbbb
00806                                 $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
00807                                             (ord($string{($offset + 1)}) & 0x3F);
00808                                 $offset += 2;
00809                         } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
00810                                 // 0bbbbbbb
00811                                 $charval = ord($string{$offset});
00812                                 $offset += 1;
00813                         } else {
00814                                 // error? throw some kind of warning here?
00815                                 $charval = false;
00816                                 $offset += 1;
00817                         }
00818                         if ($charval !== false) {
00819                                 $newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
00820                         }
00821                 }
00822                 return $newcharstring;
00823         }
00824 
00825         // UTF-8 => UTF-16LE
00826         function iconv_fallback_utf8_utf16le($string, $bom=false) {
00827                 $newcharstring = '';
00828                 if ($bom) {
00829                         $newcharstring .= "\xFF\xFE";
00830                 }
00831                 $offset = 0;
00832                 $stringlength = strlen($string);
00833                 while ($offset < $stringlength) {
00834                         if ((ord($string{$offset}) | 0x07) == 0xF7) {
00835                                 // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
00836                                 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
00837                                            ((ord($string{($offset + 1)}) & 0x3F) << 12) &
00838                                            ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
00839                                             (ord($string{($offset + 3)}) & 0x3F);
00840                                 $offset += 4;
00841                         } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
00842                                 // 1110bbbb 10bbbbbb 10bbbbbb
00843                                 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
00844                                            ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
00845                                             (ord($string{($offset + 2)}) & 0x3F);
00846                                 $offset += 3;
00847                         } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
00848                                 // 110bbbbb 10bbbbbb
00849                                 $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
00850                                             (ord($string{($offset + 1)}) & 0x3F);
00851                                 $offset += 2;
00852                         } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
00853                                 // 0bbbbbbb
00854                                 $charval = ord($string{$offset});
00855                                 $offset += 1;
00856                         } else {
00857                                 // error? maybe throw some warning here?
00858                                 $charval = false;
00859                                 $offset += 1;
00860                         }
00861                         if ($charval !== false) {
00862                                 $newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
00863                         }
00864                 }
00865                 return $newcharstring;
00866         }
00867 
00868         // UTF-8 => UTF-16LE (BOM)
00869         function iconv_fallback_utf8_utf16($string) {
00870                 return getid3_lib::iconv_fallback_utf8_utf16le($string, true);
00871         }
00872 
00873         // UTF-16BE => UTF-8
00874         function iconv_fallback_utf16be_utf8($string) {
00875                 if (substr($string, 0, 2) == "\xFE\xFF") {
00876                         // strip BOM
00877                         $string = substr($string, 2);
00878                 }
00879                 $newcharstring = '';
00880                 for ($i = 0; $i < strlen($string); $i += 2) {
00881                         $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
00882                         $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
00883                 }
00884                 return $newcharstring;
00885         }
00886 
00887         // UTF-16LE => UTF-8
00888         function iconv_fallback_utf16le_utf8($string) {
00889                 if (substr($string, 0, 2) == "\xFF\xFE") {
00890                         // strip BOM
00891                         $string = substr($string, 2);
00892                 }
00893                 $newcharstring = '';
00894                 for ($i = 0; $i < strlen($string); $i += 2) {
00895                         $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
00896                         $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
00897                 }
00898                 return $newcharstring;
00899         }
00900 
00901         // UTF-16BE => ISO-8859-1
00902         function iconv_fallback_utf16be_iso88591($string) {
00903                 if (substr($string, 0, 2) == "\xFE\xFF") {
00904                         // strip BOM
00905                         $string = substr($string, 2);
00906                 }
00907                 $newcharstring = '';
00908                 for ($i = 0; $i < strlen($string); $i += 2) {
00909                         $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
00910                         $newcharstring .= (($charval < 256) ? chr($charval) : '?');
00911                 }
00912                 return $newcharstring;
00913         }
00914 
00915         // UTF-16LE => ISO-8859-1
00916         function iconv_fallback_utf16le_iso88591($string) {
00917                 if (substr($string, 0, 2) == "\xFF\xFE") {
00918                         // strip BOM
00919                         $string = substr($string, 2);
00920                 }
00921                 $newcharstring = '';
00922                 for ($i = 0; $i < strlen($string); $i += 2) {
00923                         $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
00924                         $newcharstring .= (($charval < 256) ? chr($charval) : '?');
00925                 }
00926                 return $newcharstring;
00927         }
00928 
00929         // UTF-16 (BOM) => ISO-8859-1
00930         function iconv_fallback_utf16_iso88591($string) {
00931                 $bom = substr($string, 0, 2);
00932                 if ($bom == "\xFE\xFF") {
00933                         return getid3_lib::iconv_fallback_utf16be_iso88591(substr($string, 2));
00934                 } elseif ($bom == "\xFF\xFE") {
00935                         return getid3_lib::iconv_fallback_utf16le_iso88591(substr($string, 2));
00936                 }
00937                 return $string;
00938         }
00939 
00940         // UTF-16 (BOM) => UTF-8
00941         function iconv_fallback_utf16_utf8($string) {
00942                 $bom = substr($string, 0, 2);
00943                 if ($bom == "\xFE\xFF") {
00944                         return getid3_lib::iconv_fallback_utf16be_utf8(substr($string, 2));
00945                 } elseif ($bom == "\xFF\xFE") {
00946                         return getid3_lib::iconv_fallback_utf16le_utf8(substr($string, 2));
00947                 }
00948                 return $string;
00949         }
00950 
00951         function iconv_fallback($in_charset, $out_charset, $string) {
00952 
00953                 if ($in_charset == $out_charset) {
00954                         return $string;
00955                 }
00956 
00957                 static $iconv_broken_or_unavailable = array();
00958                 if (is_null(@$iconv_broken_or_unavailable[$in_charset.'_'.$out_charset])) {
00959                         $GETID3_ICONV_TEST_STRING = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ';
00960 
00961                         // Check iconv()
00962                         if (function_exists('iconv')) {
00963                                 if (@iconv($in_charset, 'ISO-8859-1', @iconv('ISO-8859-1', $in_charset, $GETID3_ICONV_TEST_STRING)) == $GETID3_ICONV_TEST_STRING) {
00964                                         if (@iconv($out_charset, 'ISO-8859-1', @iconv('ISO-8859-1', $out_charset, $GETID3_ICONV_TEST_STRING)) == $GETID3_ICONV_TEST_STRING) {
00965                                                 // everything works, use iconv()
00966                                                 $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = false;
00967                                         } else {
00968                                                 // iconv() available, but broken. Use getID3()'s iconv_fallback() conversions instead
00969                                                 // known issue in PHP v4.1.x
00970                                                 $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = true;
00971                                         }
00972                                 } else {
00973                                         // iconv() available, but broken. Use getID3()'s iconv_fallback() conversions instead
00974                                         // known issue in PHP v4.1.x
00975                                         $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = true;
00976                                 }
00977                         } else {
00978                                 // iconv() unavailable, use getID3()'s iconv_fallback() conversions
00979                                 $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = true;
00980                         }
00981                 }
00982 
00983                 if ($iconv_broken_or_unavailable[$in_charset.'_'.$out_charset]) {
00984                         static $ConversionFunctionList = array();
00985                         if (empty($ConversionFunctionList)) {
00986                                 $ConversionFunctionList['ISO-8859-1']['UTF-8']    = 'iconv_fallback_iso88591_utf8';
00987                                 $ConversionFunctionList['ISO-8859-1']['UTF-16']   = 'iconv_fallback_iso88591_utf16';
00988                                 $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
00989                                 $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
00990                                 $ConversionFunctionList['UTF-8']['ISO-8859-1']    = 'iconv_fallback_utf8_iso88591';
00991                                 $ConversionFunctionList['UTF-8']['UTF-16']        = 'iconv_fallback_utf8_utf16';
00992                                 $ConversionFunctionList['UTF-8']['UTF-16BE']      = 'iconv_fallback_utf8_utf16be';
00993                                 $ConversionFunctionList['UTF-8']['UTF-16LE']      = 'iconv_fallback_utf8_utf16le';
00994                                 $ConversionFunctionList['UTF-16']['ISO-8859-1']   = 'iconv_fallback_utf16_iso88591';
00995                                 $ConversionFunctionList['UTF-16']['UTF-8']        = 'iconv_fallback_utf16_utf8';
00996                                 $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
00997                                 $ConversionFunctionList['UTF-16LE']['UTF-8']      = 'iconv_fallback_utf16le_utf8';
00998                                 $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
00999                                 $ConversionFunctionList['UTF-16BE']['UTF-8']      = 'iconv_fallback_utf16be_utf8';
01000                         }
01001                         if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
01002                                 $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
01003                                 return getid3_lib::$ConversionFunction($string);
01004                         }
01005                         die('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
01006                 }
01007 
01008                 if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
01009                         switch ($out_charset) {
01010                                 case 'ISO-8859-1':
01011                                         $converted_string = rtrim($converted_string, "\x00");
01012                                         break;
01013                         }
01014                         return $converted_string;
01015                 }
01016 
01017                 // iconv() may sometimes fail with "illegal character in input string" error message
01018                 // and return an empty string, but returning the unconverted string is more useful
01019                 return $string;
01020         }
01021 
01022 
01023         function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
01024                 $HTMLstring = '';
01025 
01026                 switch ($charset) {
01027                         case 'ISO-8859-1':
01028                         case 'ISO8859-1':
01029                         case 'ISO-8859-15':
01030                         case 'ISO8859-15':
01031                         case 'cp866':
01032                         case 'ibm866':
01033                         case '866':
01034                         case 'cp1251':
01035                         case 'Windows-1251':
01036                         case 'win-1251':
01037                         case '1251':
01038                         case 'cp1252':
01039                         case 'Windows-1252':
01040                         case '1252':
01041                         case 'KOI8-R':
01042                         case 'koi8-ru':
01043                         case 'koi8r':
01044                         case 'BIG5':
01045                         case '950':
01046                         case 'GB2312':
01047                         case '936':
01048                         case 'BIG5-HKSCS':
01049                         case 'Shift_JIS':
01050                         case 'SJIS':
01051                         case '932':
01052                         case 'EUC-JP':
01053                         case 'EUCJP':
01054                                 $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
01055                                 break;
01056 
01057                         case 'UTF-8':
01058                                 $strlen = strlen($string);
01059                                 for ($i = 0; $i < $strlen; $i++) {
01060                                         $char_ord_val = ord($string{$i});
01061                                         $charval = 0;
01062                                         if ($char_ord_val < 0x80) {
01063                                                 $charval = $char_ord_val;
01064                                         } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F) {
01065                                                 $charval  = (($char_ord_val & 0x07) << 18);
01066                                                 $charval += ((ord($string{++$i}) & 0x3F) << 12);
01067                                                 $charval += ((ord($string{++$i}) & 0x3F) << 6);
01068                                                 $charval +=  (ord($string{++$i}) & 0x3F);
01069                                         } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07) {
01070                                                 $charval  = (($char_ord_val & 0x0F) << 12);
01071                                                 $charval += ((ord($string{++$i}) & 0x3F) << 6);
01072                                                 $charval +=  (ord($string{++$i}) & 0x3F);
01073                                         } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03) {
01074                                                 $charval  = (($char_ord_val & 0x1F) << 6);
01075                                                 $charval += (ord($string{++$i}) & 0x3F);
01076                                         }
01077                                         if (($charval >= 32) && ($charval <= 127)) {
01078                                                 $HTMLstring .= chr($charval);
01079                                         } else {
01080                                                 $HTMLstring .= '&#'.$charval.';';
01081                                         }
01082                                 }
01083                                 break;
01084 
01085                         case 'UTF-16LE':
01086                                 for ($i = 0; $i < strlen($string); $i += 2) {
01087                                         $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
01088                                         if (($charval >= 32) && ($charval <= 127)) {
01089                                                 $HTMLstring .= chr($charval);
01090                                         } else {
01091                                                 $HTMLstring .= '&#'.$charval.';';
01092                                         }
01093                                 }
01094                                 break;
01095 
01096                         case 'UTF-16BE':
01097                                 for ($i = 0; $i < strlen($string); $i += 2) {
01098                                         $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
01099                                         if (($charval >= 32) && ($charval <= 127)) {
01100                                                 $HTMLstring .= chr($charval);
01101                                         } else {
01102                                                 $HTMLstring .= '&#'.$charval.';';
01103                                         }
01104                                 }
01105                                 break;
01106 
01107                         default:
01108                                 $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
01109                                 break;
01110                 }
01111                 return $HTMLstring;
01112         }
01113 
01114 
01115 
01116         function RGADnameLookup($namecode) {
01117                 static $RGADname = array();
01118                 if (empty($RGADname)) {
01119                         $RGADname[0] = 'not set';
01120                         $RGADname[1] = 'Track Gain Adjustment';
01121                         $RGADname[2] = 'Album Gain Adjustment';
01122                 }
01123 
01124                 return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
01125         }
01126 
01127 
01128         function RGADoriginatorLookup($originatorcode) {
01129                 static $RGADoriginator = array();
01130                 if (empty($RGADoriginator)) {
01131                         $RGADoriginator[0] = 'unspecified';
01132                         $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
01133                         $RGADoriginator[2] = 'set by user';
01134                         $RGADoriginator[3] = 'determined automatically';
01135                 }
01136 
01137                 return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
01138         }
01139 
01140 
01141         function RGADadjustmentLookup($rawadjustment, $signbit) {
01142                 $adjustment = $rawadjustment / 10;
01143                 if ($signbit == 1) {
01144                         $adjustment *= -1;
01145                 }
01146                 return (float) $adjustment;
01147         }
01148 
01149 
01150         function RGADgainString($namecode, $originatorcode, $replaygain) {
01151                 if ($replaygain < 0) {
01152                         $signbit = '1';
01153                 } else {
01154                         $signbit = '0';
01155                 }
01156                 $storedreplaygain = intval(round($replaygain * 10));
01157                 $gainstring  = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
01158                 $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
01159                 $gainstring .= $signbit;
01160                 $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
01161 
01162                 return $gainstring;
01163         }
01164 
01165         function RGADamplitude2dB($amplitude) {
01166                 return 20 * log10($amplitude);
01167         }
01168 
01169 
01170         function GetDataImageSize($imgData) {
01171                 $GetDataImageSize = false;
01172                 if ($tempfilename = tempnam('*', 'getID3')) {
01173                         if ($tmp = @fopen($tempfilename, 'wb')) {
01174                                 fwrite($tmp, $imgData);
01175                                 fclose($tmp);
01176                                 $GetDataImageSize = @GetImageSize($tempfilename);
01177                         }
01178                         unlink($tempfilename);
01179                 }
01180                 return $GetDataImageSize;
01181         }
01182 
01183         function ImageTypesLookup($imagetypeid) {
01184                 static $ImageTypesLookup = array();
01185                 if (empty($ImageTypesLookup)) {
01186                         $ImageTypesLookup[1]  = 'gif';
01187                         $ImageTypesLookup[2]  = 'jpeg';
01188                         $ImageTypesLookup[3]  = 'png';
01189                         $ImageTypesLookup[4]  = 'swf';
01190                         $ImageTypesLookup[5]  = 'psd';
01191                         $ImageTypesLookup[6]  = 'bmp';
01192                         $ImageTypesLookup[7]  = 'tiff (little-endian)';
01193                         $ImageTypesLookup[8]  = 'tiff (big-endian)';
01194                         $ImageTypesLookup[9]  = 'jpc';
01195                         $ImageTypesLookup[10] = 'jp2';
01196                         $ImageTypesLookup[11] = 'jpx';
01197                         $ImageTypesLookup[12] = 'jb2';
01198                         $ImageTypesLookup[13] = 'swc';
01199                         $ImageTypesLookup[14] = 'iff';
01200                 }
01201                 return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
01202         }
01203 
01204         function CopyTagsToComments(&$ThisFileInfo) {
01205 
01206                 // Copy all entries from ['tags'] into common ['comments'] 
01207                 if (!empty($ThisFileInfo['tags'])) {
01208                         foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
01209                                 foreach ($tagarray as $tagname => $tagdata) {
01210                                         foreach ($tagdata as $key => $value) {
01211                                                 if (!empty($value)) {
01212                                                         if (empty($ThisFileInfo['comments'][$tagname])) {
01213 
01214                                                                 // fall through and append value
01215 
01216                                                         } elseif ($tagtype == 'id3v1') {
01217 
01218                                                                 $newvaluelength = strlen(trim($value));
01219                                                                 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
01220                                                                         $oldvaluelength = strlen(trim($existingvalue));
01221                                                                         if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
01222                                                                                 // new value is identical but shorter-than (or equal-length to) one already in comments - skip
01223                                                                                 break 2;
01224                                                                         }
01225                                                                 }
01226 
01227                                                         } else {
01228 
01229                                                                 $newvaluelength = strlen(trim($value));
01230                                                                 foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
01231                                                                         $oldvaluelength = strlen(trim($existingvalue));
01232                                                                         if (($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
01233                                                                                 $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
01234                                                                                 break 2;
01235                                                                         }
01236                                                                 }
01237 
01238                                                         }
01239                                                         if (empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
01240                                                                 $ThisFileInfo['comments'][$tagname][] = trim($value);
01241                                                         }
01242                                                 }
01243                                         }
01244                                 }
01245                         }
01246                         
01247                         // Copy to ['comments_html'] 
01248                 foreach ($ThisFileInfo['comments'] as $field => $values) {
01249                     foreach ($values as $index => $value) {
01250                         $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
01251                     }
01252             }
01253                 }
01254         }
01255 
01256 
01257         function EmbeddedLookup($key, $begin, $end, $file, $name) {
01258 
01259                 // Cached
01260                 static $cache;
01261                 if (isset($cache[$file][$name])) {
01262                         return @$cache[$file][$name][$key];
01263                 }
01264 
01265                 // Init
01266                 $keylength  = strlen($key);
01267                 $line_count = $end - $begin - 7;
01268 
01269                 // Open php file
01270                 $fp = fopen($file, 'r');
01271 
01272                 // Discard $begin lines
01273                 for ($i = 0; $i < ($begin + 3); $i++) {
01274                         fgets($fp, 1024);
01275                 }
01276 
01277                 // Loop thru line
01278                 while (0 < $line_count--) {
01279 
01280                         // Read line
01281                         $line = ltrim(fgets($fp, 1024), "\t ");
01282 
01283                         // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
01284                         //$keycheck = substr($line, 0, $keylength);
01285                         //if ($key == $keycheck)  {
01286                         //      $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
01287                         //      break;
01288                         //}
01289 
01290                         // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
01291                         //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
01292                         @list($ThisKey, $ThisValue) = explode("\t", $line, 2);
01293                         $cache[$file][$name][$ThisKey] = trim($ThisValue);
01294                 }
01295 
01296                 // Close and return
01297                 fclose($fp);
01298                 return @$cache[$file][$name][$key];
01299         }
01300 
01301         function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
01302                 global $GETID3_ERRORARRAY;
01303 
01304                 if (file_exists($filename)) {
01305                         if (@include_once($filename)) {
01306                                 return true;
01307                         } else {
01308                                 $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
01309                         }
01310                 } else {
01311                         $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
01312                 }
01313                 if ($DieOnFailure) {
01314                         die($diemessage);
01315                 } else {
01316                         $GETID3_ERRORARRAY[] = $diemessage;
01317                 }
01318                 return false;
01319         }
01320 
01321 }
01322 
01323 ?>

Generated on Fri Dec 13 2013 17:56:58 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1