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 /////////////////////////////////////////////////////////////////
11 // module.archive.gzip.php //
12 // module for analyzing GZIP files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 // Module originally written by //
18 // Mike Mozolin <teddybearØmail*ru> //
20 /////////////////////////////////////////////////////////////////
22 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
26 class getid3_gzip
extends getid3_handler
29 * Optional file list - disable for speed.
31 * Decode gzipped files, if possible, and parse recursively (.tar.gz for example).
35 public $option_gzip_parse_contents = false;
40 public function Analyze() {
41 $info = &$this->getid3
->info
;
43 $info['fileformat'] = 'gzip';
46 $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
47 //+---+---+---+---+---+---+---+---+---+---+
48 //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
49 //+---+---+---+---+---+---+---+---+---+---+
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)');
56 $buffer = $this->fread($info['filesize']);
58 $arr_members = explode("\x1F\x8B\x08", $buffer);
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) {
67 $buf = "\x1F\x8B\x08".$arr_members[$i];
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;
78 if (!$is_wrong_members) {
83 $info['gzip']['files'] = array();
87 for ($i = 0; $i < $num_members; $i++
) {
88 if (strlen($arr_members[$i]) == 0) {
91 $thisInfo = &$info['gzip']['member_header'][++
$idx];
93 $buff = "\x1F\x8B\x08".$arr_members[$i];
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']);
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);
109 $thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']);
111 $thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']);
112 if (!$thisInfo['os']) {
113 $this->error('Read error on gzip file');
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);
128 $thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
130 //+---+---+---+---+==================================+
131 //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
132 //+---+---+---+---+==================================+
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);
144 $arr_xsubfield[] = substr($buff, $fpointer +
$idx, $xsublen);
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'] = '';
161 if (ord($buff[$fpointer]) == 0) {
165 $thisInfo['filename'] .= $buff[$fpointer];
169 // bit 4 - FLG.FCOMMENT
170 //+===================================+
171 //|...file comment, zero-terminated...|
172 //+===================================+
173 if ($thisInfo['flags']['comment']) {
175 if (ord($buff[$fpointer]) == 0) {
179 $thisInfo['comment'] .= $buff[$fpointer];
187 if ($thisInfo['flags']['crc16']) {
188 $w_crc = substr($buff, $fpointer, 2);
189 $thisInfo['crc16'] = getid3_lib
::LittleEndian2Int($w_crc);
193 //if ($thisInfo['raw']['flags'] & 0x01) {
196 // bits 5, 6, 7 - reserved
198 $thisInfo['crc32'] = getid3_lib
::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
199 $thisInfo['filesize'] = getid3_lib
::LittleEndian2Int(substr($buff, strlen($buff) - 4));
201 $info['gzip']['files'] = getid3_lib
::array_merge_clobber($info['gzip']['files'], getid3_lib
::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize']));
203 if ($this->option_gzip_parse_contents
) {
204 // Try to inflate GZip
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);
214 // Calculate CRC32 for inflated content
215 $thisInfo['crc32_valid'] = sprintf('%u', crc32($inflated)) == $thisInfo['crc32'];
218 $formattest = substr($inflated, 0, 32774);
219 $getid3_temp = new getID3();
220 $determined_format = $getid3_temp->GetFileFormat($formattest);
223 // file format is determined
224 $determined_format['module'] = (isset($determined_format['module']) ?
$determined_format['module'] : '');
225 switch ($determined_format['module']) {
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');
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);
245 $this->error('Unable to fopen() temp file to parse TAR inside GZIP file');
253 // unknown or unhandled format
257 $this->warning('PHP is not compiled with gzinflate() support. Please enable PHP Zlib extension or recompile with the --with-zlib switch');
265 * Converts the OS type.
271 public function get_os_type($key) {
272 static $os_type = array(
273 '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
275 '2' => 'VMS (or OpenVMS)',
279 '6' => 'HPFS filesystem (OS/2, NT)',
284 '11' => 'NTFS filesystem (NT)',
286 '13' => 'Acorn RISCOS',
289 return (isset($os_type[$key]) ?
$os_type[$key] : '');
293 * Converts the eXtra FLags.
299 public function get_xflag_type($key) {
300 static $xflag_type = array(
302 '2' => 'maximum compression',
303 '4' => 'fastest algorithm'
305 return (isset($xflag_type[$key]) ?
$xflag_type[$key] : '');