1<?php 2 3///////////////////////////////////////////////////////////////// 4/// getID3() by James Heinrich <info@getid3.org> // 5// available at https://github.com/JamesHeinrich/getID3 // 6// or https://www.getid3.org // 7// or http://getid3.sourceforge.net // 8// see readme.txt for more details // 9///////////////////////////////////////////////////////////////// 10// // 11// module.archive.gzip.php // 12// module for analyzing GZIP files // 13// dependencies: NONE // 14// /// 15///////////////////////////////////////////////////////////////// 16// // 17// Module originally written by // 18// Mike Mozolin <teddybearØmail*ru> // 19// // 20///////////////////////////////////////////////////////////////// 21 22if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers 23 exit; 24} 25 26class getid3_gzip extends getid3_handler 27{ 28 /** 29 * Optional file list - disable for speed. 30 * 31 * Decode gzipped files, if possible, and parse recursively (.tar.gz for example). 32 * 33 * @var bool 34 */ 35 public $option_gzip_parse_contents = false; 36 37 /** 38 * @return bool 39 */ 40 public function Analyze() { 41 $info = &$this->getid3->info; 42 43 $info['fileformat'] = 'gzip'; 44 45 $start_length = 10; 46 $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os'; 47 //+---+---+---+---+---+---+---+---+---+---+ 48 //|ID1|ID2|CM |FLG| MTIME |XFL|OS | 49 //+---+---+---+---+---+---+---+---+---+---+ 50 51 if ($info['php_memory_limit'] && ($info['filesize'] > $info['php_memory_limit'])) { 52 $this->error('File is too large ('.number_format($info['filesize']).' bytes) to read into memory (limit: '.number_format($info['php_memory_limit'] / 1048576).'MB)'); 53 return false; 54 } 55 $this->fseek(0); 56 $buffer = $this->fread($info['filesize']); 57 58 $arr_members = explode("\x1F\x8B\x08", $buffer); 59 $num_members = 0; 60 while (true) { 61 $is_wrong_members = false; 62 $num_members = intval(count($arr_members)); 63 for ($i = 0; $i < $num_members; $i++) { 64 if (strlen($arr_members[$i]) == 0) { 65 continue; 66 } 67 $buf = "\x1F\x8B\x08".$arr_members[$i]; 68 69 $attr = unpack($unpack_header, substr($buf, 0, $start_length)); 70 if (!$this->get_os_type(ord($attr['os']))) { 71 // Merge member with previous if wrong OS type 72 $arr_members[($i - 1)] .= $buf; 73 $arr_members[$i] = ''; 74 $is_wrong_members = true; 75 continue; 76 } 77 } 78 if (!$is_wrong_members) { 79 break; 80 } 81 } 82 83 $info['gzip']['files'] = array(); 84 85 $fpointer = 0; 86 $idx = 0; 87 for ($i = 0; $i < $num_members; $i++) { 88 if (strlen($arr_members[$i]) == 0) { 89 continue; 90 } 91 $thisInfo = &$info['gzip']['member_header'][++$idx]; 92 93 $buff = "\x1F\x8B\x08".$arr_members[$i]; 94 95 $attr = unpack($unpack_header, substr($buff, 0, $start_length)); 96 $thisInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']); 97 $thisInfo['raw']['id1'] = ord($attr['cmethod']); 98 $thisInfo['raw']['id2'] = ord($attr['cmethod']); 99 $thisInfo['raw']['cmethod'] = ord($attr['cmethod']); 100 $thisInfo['raw']['os'] = ord($attr['os']); 101 $thisInfo['raw']['xflags'] = ord($attr['xflags']); 102 $thisInfo['raw']['flags'] = ord($attr['flags']); 103 104 $thisInfo['flags']['crc16'] = (bool) ($thisInfo['raw']['flags'] & 0x02); 105 $thisInfo['flags']['extra'] = (bool) ($thisInfo['raw']['flags'] & 0x04); 106 $thisInfo['flags']['filename'] = (bool) ($thisInfo['raw']['flags'] & 0x08); 107 $thisInfo['flags']['comment'] = (bool) ($thisInfo['raw']['flags'] & 0x10); 108 109 $thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']); 110 111 $thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']); 112 if (!$thisInfo['os']) { 113 $this->error('Read error on gzip file'); 114 return false; 115 } 116 117 $fpointer = 10; 118 $arr_xsubfield = array(); 119 // bit 2 - FLG.FEXTRA 120 //+---+---+=================================+ 121 //| XLEN |...XLEN bytes of "extra field"...| 122 //+---+---+=================================+ 123 if ($thisInfo['flags']['extra']) { 124 $w_xlen = substr($buff, $fpointer, 2); 125 $xlen = getid3_lib::LittleEndian2Int($w_xlen); 126 $fpointer += 2; 127 128 $thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen); 129 // Extra SubFields 130 //+---+---+---+---+==================================+ 131 //|SI1|SI2| LEN |... LEN bytes of subfield data ...| 132 //+---+---+---+---+==================================+ 133 $idx = 0; 134 while (true) { 135 if ($idx >= $xlen) { 136 break; 137 } 138 $si1 = ord(substr($buff, $fpointer + $idx++, 1)); 139 $si2 = ord(substr($buff, $fpointer + $idx++, 1)); 140 if (($si1 == 0x41) && ($si2 == 0x70)) { 141 $w_xsublen = substr($buff, $fpointer + $idx, 2); 142 $xsublen = getid3_lib::LittleEndian2Int($w_xsublen); 143 $idx += 2; 144 $arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen); 145 $idx += $xsublen; 146 } else { 147 break; 148 } 149 } 150 $fpointer += $xlen; 151 } 152 // bit 3 - FLG.FNAME 153 //+=========================================+ 154 //|...original file name, zero-terminated...| 155 //+=========================================+ 156 // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz 157 $thisInfo['filename'] = preg_replace('#\\.gz$#i', '', $info['filename']); 158 if ($thisInfo['flags']['filename']) { 159 $thisInfo['filename'] = ''; 160 while (true) { 161 if (ord($buff[$fpointer]) == 0) { 162 $fpointer++; 163 break; 164 } 165 $thisInfo['filename'] .= $buff[$fpointer]; 166 $fpointer++; 167 } 168 } 169 // bit 4 - FLG.FCOMMENT 170 //+===================================+ 171 //|...file comment, zero-terminated...| 172 //+===================================+ 173 if ($thisInfo['flags']['comment']) { 174 while (true) { 175 if (ord($buff[$fpointer]) == 0) { 176 $fpointer++; 177 break; 178 } 179 $thisInfo['comment'] .= $buff[$fpointer]; 180 $fpointer++; 181 } 182 } 183 // bit 1 - FLG.FHCRC 184 //+---+---+ 185 //| CRC16 | 186 //+---+---+ 187 if ($thisInfo['flags']['crc16']) { 188 $w_crc = substr($buff, $fpointer, 2); 189 $thisInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc); 190 $fpointer += 2; 191 } 192 // bit 0 - FLG.FTEXT 193 //if ($thisInfo['raw']['flags'] & 0x01) { 194 // Ignored... 195 //} 196 // bits 5, 6, 7 - reserved 197 198 $thisInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4)); 199 $thisInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4)); 200 201 $info['gzip']['files'] = getid3_lib::array_merge_clobber($info['gzip']['files'], getid3_lib::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize'])); 202 203 if ($this->option_gzip_parse_contents) { 204 // Try to inflate GZip 205 $csize = 0; 206 $inflated = ''; 207 $chkcrc32 = ''; 208 if (function_exists('gzinflate')) { 209 $cdata = substr($buff, $fpointer); 210 $cdata = substr($cdata, 0, strlen($cdata) - 8); 211 $csize = strlen($cdata); 212 $inflated = gzinflate($cdata); 213 214 // Calculate CRC32 for inflated content 215 $thisInfo['crc32_valid'] = sprintf('%u', crc32($inflated)) == $thisInfo['crc32']; 216 217 // determine format 218 $formattest = substr($inflated, 0, 32774); 219 $getid3_temp = new getID3(); 220 $determined_format = $getid3_temp->GetFileFormat($formattest); 221 unset($getid3_temp); 222 223 // file format is determined 224 $determined_format['module'] = (isset($determined_format['module']) ? $determined_format['module'] : ''); 225 switch ($determined_format['module']) { 226 case 'tar': 227 // view TAR-file info 228 if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && include_once(GETID3_INCLUDEPATH.$determined_format['include'])) { 229 if (($temp_tar_filename = tempnam(GETID3_TEMP_DIR, 'getID3')) === false) { 230 // can't find anywhere to create a temp file, abort 231 $this->error('Unable to create temp file to parse TAR inside GZIP file'); 232 break; 233 } 234 if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) { 235 fwrite($fp_temp_tar, $inflated); 236 fclose($fp_temp_tar); 237 $getid3_temp = new getID3(); 238 $getid3_temp->openfile($temp_tar_filename); 239 $getid3_tar = new getid3_tar($getid3_temp); 240 $getid3_tar->Analyze(); 241 $info['gzip']['member_header'][$idx]['tar'] = $getid3_temp->info['tar']; 242 unset($getid3_temp, $getid3_tar); 243 unlink($temp_tar_filename); 244 } else { 245 $this->error('Unable to fopen() temp file to parse TAR inside GZIP file'); 246 break; 247 } 248 } 249 break; 250 251 case '': 252 default: 253 // unknown or unhandled format 254 break; 255 } 256 } else { 257 $this->warning('PHP is not compiled with gzinflate() support. Please enable PHP Zlib extension or recompile with the --with-zlib switch'); 258 } 259 } 260 } 261 return true; 262 } 263 264 /** 265 * Converts the OS type. 266 * 267 * @param string $key 268 * 269 * @return string 270 */ 271 public function get_os_type($key) { 272 static $os_type = array( 273 '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)', 274 '1' => 'Amiga', 275 '2' => 'VMS (or OpenVMS)', 276 '3' => 'Unix', 277 '4' => 'VM/CMS', 278 '5' => 'Atari TOS', 279 '6' => 'HPFS filesystem (OS/2, NT)', 280 '7' => 'Macintosh', 281 '8' => 'Z-System', 282 '9' => 'CP/M', 283 '10' => 'TOPS-20', 284 '11' => 'NTFS filesystem (NT)', 285 '12' => 'QDOS', 286 '13' => 'Acorn RISCOS', 287 '255' => 'unknown' 288 ); 289 return (isset($os_type[$key]) ? $os_type[$key] : ''); 290 } 291 292 /** 293 * Converts the eXtra FLags. 294 * 295 * @param string $key 296 * 297 * @return string 298 */ 299 public function get_xflag_type($key) { 300 static $xflag_type = array( 301 '0' => 'unknown', 302 '2' => 'maximum compression', 303 '4' => 'fastest algorithm' 304 ); 305 return (isset($xflag_type[$key]) ? $xflag_type[$key] : ''); 306 } 307} 308 309