[ PHPXref.com ] [ Generated: Sun Jul 20 19:52:21 2008 ] [ phPOP3clean 0.9.10 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> phPOP3clean.getid3.lib.php (source)

   1  <?php
   2  /////////////////////////////////////////////////////////////////
   3  /// getID3() by James Heinrich <info@getid3.org>               //
   4  //  available at http://getid3.sourceforge.net                 //
   5  //            or http://www.getid3.org                         //
   6  /////////////////////////////////////////////////////////////////
   7  //                                                             //
   8  // getid3.lib.php - part of getID3()                           //
   9  // See readme.txt for more details                             //
  10  //                                                             //
  11  /////////////////////////////////////////////////////////////////
  12  // getid3_lib::GetURLImageSize( $urlpic ) determines the       //
  13  // dimensions of local/remote URL pictures.                    //
  14  // returns array with ($width, $height, $type)                 //
  15  //                                                             //
  16  // Thanks to: Oyvind Hallsteinsen aka Gosub / ELq -            //
  17  // gosubØelq*org  for the original size determining code       //
  18  //                                                             //
  19  // PHP Hack by Filipe Laborde-Basto Oct 21/2000                //
  20  // FREELY DISTRIBUTABLE -- use at your sole discretion! :)     //
  21  // Enjoy. (Not to be sold in commercial packages though,       //
  22  // keep it free!) Feel free to contact me at filØrezox*com     //
  23  // (http://www.rezox.com)                                      //
  24  //                                                             //
  25  // Modified by James Heinrich <getid3Øusers*sourceforge*net>   //
  26  // June 1, 2001 - created GetDataImageSize($imgData) by        //
  27  // seperating the fopen() stuff to GetURLImageSize($urlpic)    //
  28  // which then calls GetDataImageSize($imgData). The idea being //
  29  // you can call GetDataImageSize($imgData) with image data     //
  30  // from a database etc.                                        //
  31  //                                                            ///
  32  /////////////////////////////////////////////////////////////////
  33  // bundled as part of phPOP3clean - http://phpop3clean.sf.net  //
  34  /////////////////////////////////////////////////////////////////
  35  
  36  
  37  define('GETID3_GIF_SIG',     "\x47\x49\x46");  // 'GIF'
  38  define('GETID3_PNG_SIG',     "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A");
  39  define('GETID3_JPG_SIG',     "\xFF\xD8\xFF");
  40  define('GETID3_JPG_SOS',     "\xDA"); // Start Of Scan - image data start
  41  define('GETID3_JPG_SOF0',    "\xC0"); // Start Of Frame N
  42  define('GETID3_JPG_SOF1',    "\xC1"); // N indicates which compression process
  43  define('GETID3_JPG_SOF2',    "\xC2"); // Only SOF0-SOF2 are now in common use
  44  define('GETID3_JPG_SOF3',    "\xC3");
  45  // NB: codes C4 and CC are *not* SOF markers
  46  define('GETID3_JPG_SOF5',    "\xC5");
  47  define('GETID3_JPG_SOF6',    "\xC6");
  48  define('GETID3_JPG_SOF7',    "\xC7");
  49  define('GETID3_JPG_SOF9',    "\xC9");
  50  define('GETID3_JPG_SOF10',   "\xCA");
  51  define('GETID3_JPG_SOF11',   "\xCB");
  52  // NB: codes C4 and CC are *not* SOF markers
  53  define('GETID3_JPG_SOF13',   "\xCD");
  54  define('GETID3_JPG_SOF14',   "\xCE");
  55  define('GETID3_JPG_SOF15',   "\xCF");
  56  define('GETID3_JPG_EOI',     "\xD9"); // End Of Image (end of datastream)
  57  
  58  
  59  
  60  class getid3_lib
  61  {
  62  
  63  	function PrintHexBytes($string, $hex=true, $spaces=true, $htmlsafe=true) {
  64          $returnstring = '';
  65          for ($i = 0; $i < strlen($string); $i++) {
  66              if ($hex) {
  67                  $returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
  68              } else {
  69                  $returnstring .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '¤');
  70              }
  71              if ($spaces) {
  72                  $returnstring .= ' ';
  73              }
  74          }
  75          if ($htmlsafe) {
  76              $returnstring = htmlentities($returnstring);
  77          }
  78          return $returnstring;
  79      }
  80  
  81  	function SafeStripSlashes($text) {
  82          if (get_magic_quotes_gpc()) {
  83              return stripslashes($text);
  84          }
  85          return $text;
  86      }
  87  
  88  
  89  	function trunc($floatnumber) {
  90          // truncates a floating-point number at the decimal point
  91          // returns int (if possible, otherwise float)
  92          if ($floatnumber >= 1) {
  93              $truncatednumber = floor($floatnumber);
  94          } elseif ($floatnumber <= -1) {
  95              $truncatednumber = ceil($floatnumber);
  96          } else {
  97              $truncatednumber = 0;
  98          }
  99          if ($truncatednumber <= pow(2, 30)) {
 100              $truncatednumber = (int) $truncatednumber;
 101          }
 102          return $truncatednumber;
 103      }
 104  
 105  
 106  	function CastAsInt($floatnum) {
 107          // convert to float if not already
 108          $floatnum = (float) $floatnum;
 109  
 110          // convert a float to type int, only if possible
 111          if (getid3_lib::trunc($floatnum) == $floatnum) {
 112              // it's not floating point
 113              if ($floatnum <= pow(2, 30)) {
 114                  // it's within int range
 115                  $floatnum = (int) $floatnum;
 116              }
 117          }
 118          return $floatnum;
 119      }
 120  
 121  
 122  	function DecimalBinary2Float($binarynumerator) {
 123          $numerator   = getid3_lib::Bin2Dec($binarynumerator);
 124          $denominator = getid3_lib::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
 125          return ($numerator / $denominator);
 126      }
 127  
 128  
 129  	function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
 130          // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
 131          if (strpos($binarypointnumber, '.') === false) {
 132              $binarypointnumber = '0.'.$binarypointnumber;
 133          } elseif ($binarypointnumber{0} == '.') {
 134              $binarypointnumber = '0'.$binarypointnumber;
 135          }
 136          $exponent = 0;
 137          while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
 138              if (substr($binarypointnumber, 1, 1) == '.') {
 139                  $exponent--;
 140                  $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
 141              } else {
 142                  $pointpos = strpos($binarypointnumber, '.');
 143                  $exponent += ($pointpos - 1);
 144                  $binarypointnumber = str_replace('.', '', $binarypointnumber);
 145                  $binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
 146              }
 147          }
 148          $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
 149          return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
 150      }
 151  
 152  
 153  	function Float2BinaryDecimal($floatvalue) {
 154          // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
 155          $maxbits = 128; // to how many bits of precision should the calculations be taken?
 156          $intpart   = getid3_lib::trunc($floatvalue);
 157          $floatpart = abs($floatvalue - $intpart);
 158          $pointbitstring = '';
 159          while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
 160              $floatpart *= 2;
 161              $pointbitstring .= (string) getid3_lib::trunc($floatpart);
 162              $floatpart -= getid3_lib::trunc($floatpart);
 163          }
 164          $binarypointnumber = decbin($intpart).'.'.$pointbitstring;
 165          return $binarypointnumber;
 166      }
 167  
 168  
 169  	function Float2String($floatvalue, $bits) {
 170          // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
 171          switch ($bits) {
 172              case 32:
 173                  $exponentbits = 8;
 174                  $fractionbits = 23;
 175                  break;
 176  
 177              case 64:
 178                  $exponentbits = 11;
 179                  $fractionbits = 52;
 180                  break;
 181  
 182              default:
 183                  return false;
 184                  break;
 185          }
 186          if ($floatvalue >= 0) {
 187              $signbit = '0';
 188          } else {
 189              $signbit = '1';
 190          }
 191          $normalizedbinary  = getid3_lib::NormalizeBinaryPoint(getid3_lib::Float2BinaryDecimal($floatvalue), $fractionbits);
 192          $biasedexponent    = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
 193          $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
 194          $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
 195  
 196          return getid3_lib::BigEndian2String(getid3_lib::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
 197      }
 198  
 199  
 200  	function LittleEndian2Float($byteword) {
 201          return getid3_lib::BigEndian2Float(strrev($byteword));
 202      }
 203  
 204  
 205  	function BigEndian2Float($byteword) {
 206          // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
 207          // http://www.psc.edu/general/software/packages/ieee/ieee.html
 208          // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
 209  
 210          $bitword = getid3_lib::BigEndian2Bin($byteword);
 211          $signbit = $bitword{0};
 212  
 213          switch (strlen($byteword) * 8) {
 214              case 32:
 215                  $exponentbits = 8;
 216                  $fractionbits = 23;
 217                  break;
 218  
 219              case 64:
 220                  $exponentbits = 11;
 221                  $fractionbits = 52;
 222                  break;
 223  
 224              case 80:
 225                  // 80-bit Apple SANE format
 226                  // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
 227                  $exponentstring = substr($bitword, 1, 15);
 228                  $isnormalized = intval($bitword{16});
 229                  $fractionstring = substr($bitword, 17, 63);
 230                  $exponent = pow(2, getid3_lib::Bin2Dec($exponentstring) - 16383);
 231                  $fraction = $isnormalized + getid3_lib::DecimalBinary2Float($fractionstring);
 232                  $floatvalue = $exponent * $fraction;
 233                  if ($signbit == '1') {
 234                      $floatvalue *= -1;
 235                  }
 236                  return $floatvalue;
 237                  break;
 238  
 239              default:
 240                  return false;
 241                  break;
 242          }
 243          $exponentstring = substr($bitword, 1, $exponentbits);
 244          $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
 245          $exponent = getid3_lib::Bin2Dec($exponentstring);
 246          $fraction = getid3_lib::Bin2Dec($fractionstring);
 247  
 248          if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
 249              // Not a Number
 250              $floatvalue = false;
 251          } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
 252              if ($signbit == '1') {
 253                  $floatvalue = '-infinity';
 254              } else {
 255                  $floatvalue = '+infinity';
 256              }
 257          } elseif (($exponent == 0) && ($fraction == 0)) {
 258              if ($signbit == '1') {
 259                  $floatvalue = -0;
 260              } else {
 261                  $floatvalue = 0;
 262              }
 263              $floatvalue = ($signbit ? 0 : -0);
 264          } elseif (($exponent == 0) && ($fraction != 0)) {
 265              // These are 'unnormalized' values
 266              $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fractionstring);
 267              if ($signbit == '1') {
 268                  $floatvalue *= -1;
 269              }
 270          } elseif ($exponent != 0) {
 271              $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fractionstring));
 272              if ($signbit == '1') {
 273                  $floatvalue *= -1;
 274              }
 275          }
 276          return (float) $floatvalue;
 277      }
 278  
 279  
 280  	function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
 281          $intvalue = 0;
 282          $bytewordlen = strlen($byteword);
 283          for ($i = 0; $i < $bytewordlen; $i++) {
 284              if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
 285                  $intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
 286              } else {
 287                  $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
 288              }
 289          }
 290          if ($signed && !$synchsafe) {
 291              // synchsafe ints are not allowed to be signed
 292              switch ($bytewordlen) {
 293                  case 1:
 294                  case 2:
 295                  case 3:
 296                  case 4:
 297                      $signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
 298                      if ($intvalue & $signmaskbit) {
 299                          $intvalue = 0 - ($intvalue & ($signmaskbit - 1));
 300                      }
 301                      break;
 302  
 303                  default:
 304                      die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2Int()');
 305                      break;
 306              }
 307          }
 308          return getid3_lib::CastAsInt($intvalue);
 309      }
 310  
 311  
 312  	function LittleEndian2Int($byteword, $signed=false) {
 313          return getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
 314      }
 315  
 316  
 317  	function BigEndian2Bin($byteword) {
 318          $binvalue = '';
 319          $bytewordlen = strlen($byteword);
 320          for ($i = 0; $i < $bytewordlen; $i++) {
 321              $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
 322          }
 323          return $binvalue;
 324      }
 325  
 326  
 327  	function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
 328          if ($number < 0) {
 329              return false;
 330          }
 331          $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
 332          $intstring = '';
 333          if ($signed) {
 334              if ($minbytes > 4) {
 335                  die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2String()');
 336              }
 337              $number = $number & (0x80 << (8 * ($minbytes - 1)));
 338          }
 339          while ($number != 0) {
 340              $quotient = ($number / ($maskbyte + 1));
 341              $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
 342              $number = floor($quotient);
 343          }
 344          return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
 345      }
 346  
 347  
 348  	function Dec2Bin($number) {
 349          while ($number >= 256) {
 350              $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
 351              $number = floor($number / 256);
 352          }
 353          $bytes[] = $number;
 354          $binstring = '';
 355          for ($i = 0; $i < count($bytes); $i++) {
 356              $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
 357          }
 358          return $binstring;
 359      }
 360  
 361  
 362  	function Bin2Dec($binstring, $signed=false) {
 363          $signmult = 1;
 364          if ($signed) {
 365              if ($binstring{0} == '1') {
 366                  $signmult = -1;
 367              }
 368              $binstring = substr($binstring, 1);
 369          }
 370          $decvalue = 0;
 371          for ($i = 0; $i < strlen($binstring); $i++) {
 372              $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
 373          }
 374          return getid3_lib::CastAsInt($decvalue * $signmult);
 375      }
 376  
 377  
 378  	function Bin2String($binstring) {
 379          // return 'hi' for input of '0110100001101001'
 380          $string = '';
 381          $binstringreversed = strrev($binstring);
 382          for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
 383              $string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
 384          }
 385          return $string;
 386      }
 387  
 388  
 389  	function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
 390          $intstring = '';
 391          while ($number > 0) {
 392              if ($synchsafe) {
 393                  $intstring = $intstring.chr($number & 127);
 394                  $number >>= 7;
 395              } else {
 396                  $intstring = $intstring.chr($number & 255);
 397                  $number >>= 8;
 398              }
 399          }
 400          return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
 401      }
 402  
 403  
 404  	function array_merge_clobber($array1, $array2) {
 405          // written by kcØhireability*com
 406          // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
 407          if (!is_array($array1) || !is_array($array2)) {
 408              return false;
 409          }
 410          $newarray = $array1;
 411          foreach ($array2 as $key => $val) {
 412              if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
 413                  $newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
 414              } else {
 415                  $newarray[$key] = $val;
 416              }
 417          }
 418          return $newarray;
 419      }
 420  
 421  
 422  	function array_merge_noclobber($array1, $array2) {
 423          if (!is_array($array1) || !is_array($array2)) {
 424              return false;
 425          }
 426          $newarray = $array1;
 427          foreach ($array2 as $key => $val) {
 428              if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
 429                  $newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
 430              } elseif (!isset($newarray[$key])) {
 431                  $newarray[$key] = $val;
 432              }
 433          }
 434          return $newarray;
 435      }
 436  
 437  
 438  	function fileextension($filename, $numextensions=1) {
 439          if (strstr($filename, '.')) {
 440              $reversedfilename = strrev($filename);
 441              $offset = 0;