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