示例#1
0
 /**
  * @return bool
  */
 public function analyze()
 {
     $info =& $this->getid3->info;
     $info['fileformat'] = 'gzip';
     $start_length = 10;
     $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
     //+---+---+---+---+---+---+---+---+---+---+
     //|ID1|ID2|CM |FLG|     MTIME     |XFL|OS |
     //+---+---+---+---+---+---+---+---+---+---+
     if ($info['filesize'] > $info['php_memory_limit']) {
         $info['error'][] = 'File is too large (' . number_format($info['filesize']) . ' bytes) to read into memory (limit: ' . number_format($info['php_memory_limit'] / 1048576) . 'MB)';
         return false;
     }
     fseek($this->getid3->fp, 0);
     $buffer = fread($this->getid3->fp, $info['filesize']);
     $arr_members = explode("‹", $buffer);
     while (true) {
         $is_wrong_members = false;
         $num_members = intval(count($arr_members));
         for ($i = 0; $i < $num_members; ++$i) {
             if (strlen($arr_members[$i]) == 0) {
                 continue;
             }
             $buf = "‹" . $arr_members[$i];
             $attr = unpack($unpack_header, substr($buf, 0, $start_length));
             if (!$this->get_os_type(ord($attr['os']))) {
                 // Merge member with previous if wrong OS type
                 $arr_members[$i - 1] .= $buf;
                 $arr_members[$i] = '';
                 $is_wrong_members = true;
                 continue;
             }
         }
         if (!$is_wrong_members) {
             break;
         }
     }
     $info['gzip']['files'] = array();
     $fpointer = 0;
     $idx = 0;
     for ($i = 0; $i < $num_members; ++$i) {
         if (strlen($arr_members[$i]) == 0) {
             continue;
         }
         $thisInfo =& $info['gzip']['member_header'][++$idx];
         $buff = "‹" . $arr_members[$i];
         $attr = unpack($unpack_header, substr($buff, 0, $start_length));
         $thisInfo['filemtime'] = Helper::LittleEndian2Int($attr['mtime']);
         $thisInfo['raw']['id1'] = ord($attr['cmethod']);
         $thisInfo['raw']['id2'] = ord($attr['cmethod']);
         $thisInfo['raw']['cmethod'] = ord($attr['cmethod']);
         $thisInfo['raw']['os'] = ord($attr['os']);
         $thisInfo['raw']['xflags'] = ord($attr['xflags']);
         $thisInfo['raw']['flags'] = ord($attr['flags']);
         $thisInfo['flags']['crc16'] = (bool) ($thisInfo['raw']['flags'] & 0x2);
         $thisInfo['flags']['extra'] = (bool) ($thisInfo['raw']['flags'] & 0x4);
         $thisInfo['flags']['filename'] = (bool) ($thisInfo['raw']['flags'] & 0x8);
         $thisInfo['flags']['comment'] = (bool) ($thisInfo['raw']['flags'] & 0x10);
         $thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']);
         $thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']);
         if (!$thisInfo['os']) {
             $info['error'][] = 'Read error on gzip file';
             return false;
         }
         $fpointer = 10;
         $arr_xsubfield = array();
         // bit 2 - FLG.FEXTRA
         //+---+---+=================================+
         //| XLEN  |...XLEN bytes of "extra field"...|
         //+---+---+=================================+
         if ($thisInfo['flags']['extra']) {
             $w_xlen = substr($buff, $fpointer, 2);
             $xlen = Helper::LittleEndian2Int($w_xlen);
             $fpointer += 2;
             $thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
             // Extra SubFields
             //+---+---+---+---+==================================+
             //|SI1|SI2|  LEN  |... LEN bytes of subfield data ...|
             //+---+---+---+---+==================================+
             $idx = 0;
             while (true) {
                 if ($idx >= $xlen) {
                     break;
                 }
                 $si1 = ord(substr($buff, $fpointer + $idx++, 1));
                 $si2 = ord(substr($buff, $fpointer + $idx++, 1));
                 if ($si1 == 0x41 && $si2 == 0x70) {
                     $w_xsublen = substr($buff, $fpointer + $idx, 2);
                     $xsublen = Helper::LittleEndian2Int($w_xsublen);
                     $idx += 2;
                     $arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen);
                     $idx += $xsublen;
                 } else {
                     break;
                 }
             }
             $fpointer += $xlen;
         }
         // bit 3 - FLG.FNAME
         //+=========================================+
         //|...original file name, zero-terminated...|
         //+=========================================+
         // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
         $thisInfo['filename'] = preg_replace('#\\.gz$#i', '', $info['filename']);
         if ($thisInfo['flags']['filename']) {
             while (true) {
                 if (ord($buff[$fpointer]) == 0) {
                     ++$fpointer;
                     break;
                 }
                 $thisInfo['filename'] .= $buff[$fpointer];
                 ++$fpointer;
             }
         }
         // bit 4 - FLG.FCOMMENT
         //+===================================+
         //|...file comment, zero-terminated...|
         //+===================================+
         if ($thisInfo['flags']['comment']) {
             while (true) {
                 if (ord($buff[$fpointer]) == 0) {
                     ++$fpointer;
                     break;
                 }
                 $thisInfo['comment'] .= $buff[$fpointer];
                 ++$fpointer;
             }
         }
         // bit 1 - FLG.FHCRC
         //+---+---+
         //| CRC16 |
         //+---+---+
         if ($thisInfo['flags']['crc16']) {
             $w_crc = substr($buff, $fpointer, 2);
             $thisInfo['crc16'] = Helper::LittleEndian2Int($w_crc);
             $fpointer += 2;
         }
         // bit 0 - FLG.FTEXT
         //if ($thisInfo['raw']['flags'] & 0x01) {
         //	Ignored...
         //}
         // bits 5, 6, 7 - reserved
         $thisInfo['crc32'] = Helper::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
         $thisInfo['filesize'] = Helper::LittleEndian2Int(substr($buff, strlen($buff) - 4));
         $info['gzip']['files'] = Helper::array_merge_clobber($info['gzip']['files'], Helper::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize']));
         if ($this->option_gzip_parse_contents) {
             // Try to inflate GZip
             $csize = 0;
             $inflated = '';
             $chkcrc32 = '';
             if (function_exists('gzinflate')) {
                 $cdata = substr($buff, $fpointer);
                 $cdata = substr($cdata, 0, strlen($cdata) - 8);
                 $csize = strlen($cdata);
                 $inflated = gzinflate($cdata);
                 // Calculate CRC32 for inflated content
                 $thisInfo['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $thisInfo['crc32']);
                 // determine format
                 $formattest = substr($inflated, 0, 32774);
                 $getid3_temp = new GetId3Core();
                 $determined_format = $getid3_temp->GetFileFormat($formattest);
                 unset($getid3_temp);
                 // file format is determined
                 $determined_format['module'] = isset($determined_format['module']) ? $determined_format['module'] : '';
                 switch ($determined_format['module']) {
                     case 'tar':
                         // view TAR-file info
                         if (class_exists($determined_format['class'])) {
                             if (($temp_tar_filename = tempnam(GetId3Core::getTempDir(), 'getID3')) === false) {
                                 // can't find anywhere to create a temp file, abort
                                 $info['error'][] = 'Unable to create temp file to parse TAR inside GZIP file';
                                 break;
                             }
                             if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) {
                                 fwrite($fp_temp_tar, $inflated);
                                 fclose($fp_temp_tar);
                                 $getid3_temp = new GetId3Core();
                                 $getid3_temp->openfile($temp_tar_filename);
                                 $getid3_tar = new Tar($getid3_temp);
                                 $getid3_tar->analyze();
                                 $info['gzip']['member_header'][$idx]['tar'] = $getid3_temp->info['tar'];
                                 unset($getid3_temp, $getid3_tar);
                                 unlink($temp_tar_filename);
                             } else {
                                 $info['error'][] = 'Unable to fopen() temp file to parse TAR inside GZIP file';
                                 break;
                             }
                         }
                         break;
                     case '':
                     default:
                         // unknown or unhandled format
                         break;
                 }
             }
         }
     }
     return true;
 }