ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
getid3_gzip Class Reference

getID3() by James Heinrich info@.nosp@m.geti.nosp@m.d3.or.nosp@m.g // More...

+ Inheritance diagram for getid3_gzip:
+ Collaboration diagram for getid3_gzip:

Public Member Functions

 getid3_gzip (&$fd, &$ThisFileInfo)
 
 get_os_type ($key)
 
 get_xflag_type ($key)
 
 Analyze ()
 
 get_os_type ($key)
 
 get_xflag_type ($key)
 
- Public Member Functions inherited from getid3_handler
 __construct (getID3 $getid3, $call_module=null)
 
 Analyze ()
 
 AnalyzeString ($string)
 
 setStringMode ($string)
 
 saveAttachment ($name, $offset, $length, $image_mime=null)
 

Data Fields

 $option_gzip_parse_contents = false
 

Additional Inherited Members

- Protected Member Functions inherited from getid3_handler
 ftell ()
 
 fread ($bytes)
 
 fseek ($bytes, $whence=SEEK_SET)
 
 feof ()
 
 isDependencyFor ($module)
 
 error ($text)
 
 warning ($text)
 
 notice ($text)
 
- Protected Attributes inherited from getid3_handler
 $getid3
 
 $data_string_flag = false
 
 $data_string = ''
 
 $data_string_position = 0
 
 $data_string_length = 0
 

Detailed Description

getID3() by James Heinrich info@.nosp@m.geti.nosp@m.d3.or.nosp@m.g //

Definition at line 17 of file module.archive.gzip.php.

Member Function Documentation

◆ Analyze()

getid3_gzip::Analyze ( )

Definition at line 28 of file module.archive.gzip.php.

References $info, getid3_lib\array_merge_clobber(), getid3_lib\CreateDeepArray(), getid3_handler\fread(), getid3_handler\fseek(), get_os_type(), get_xflag_type(), and getid3_lib\LittleEndian2Int().

28  {
29  $info = &$this->getid3->info;
30 
31  $info['fileformat'] = 'gzip';
32 
33  $start_length = 10;
34  $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
35  //+---+---+---+---+---+---+---+---+---+---+
36  //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
37  //+---+---+---+---+---+---+---+---+---+---+
38 
39  if ($info['php_memory_limit'] && ($info['filesize'] > $info['php_memory_limit'])) {
40  $info['error'][] = 'File is too large ('.number_format($info['filesize']).' bytes) to read into memory (limit: '.number_format($info['php_memory_limit'] / 1048576).'MB)';
41  return false;
42  }
43  $this->fseek(0);
44  $buffer = $this->fread($info['filesize']);
45 
46  $arr_members = explode("\x1F\x8B\x08", $buffer);
47  while (true) {
48  $is_wrong_members = false;
49  $num_members = intval(count($arr_members));
50  for ($i = 0; $i < $num_members; $i++) {
51  if (strlen($arr_members[$i]) == 0) {
52  continue;
53  }
54  $buf = "\x1F\x8B\x08".$arr_members[$i];
55 
56  $attr = unpack($unpack_header, substr($buf, 0, $start_length));
57  if (!$this->get_os_type(ord($attr['os']))) {
58  // Merge member with previous if wrong OS type
59  $arr_members[($i - 1)] .= $buf;
60  $arr_members[$i] = '';
61  $is_wrong_members = true;
62  continue;
63  }
64  }
65  if (!$is_wrong_members) {
66  break;
67  }
68  }
69 
70  $info['gzip']['files'] = array();
71 
72  $fpointer = 0;
73  $idx = 0;
74  for ($i = 0; $i < $num_members; $i++) {
75  if (strlen($arr_members[$i]) == 0) {
76  continue;
77  }
78  $thisInfo = &$info['gzip']['member_header'][++$idx];
79 
80  $buff = "\x1F\x8B\x08".$arr_members[$i];
81 
82  $attr = unpack($unpack_header, substr($buff, 0, $start_length));
83  $thisInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
84  $thisInfo['raw']['id1'] = ord($attr['cmethod']);
85  $thisInfo['raw']['id2'] = ord($attr['cmethod']);
86  $thisInfo['raw']['cmethod'] = ord($attr['cmethod']);
87  $thisInfo['raw']['os'] = ord($attr['os']);
88  $thisInfo['raw']['xflags'] = ord($attr['xflags']);
89  $thisInfo['raw']['flags'] = ord($attr['flags']);
90 
91  $thisInfo['flags']['crc16'] = (bool) ($thisInfo['raw']['flags'] & 0x02);
92  $thisInfo['flags']['extra'] = (bool) ($thisInfo['raw']['flags'] & 0x04);
93  $thisInfo['flags']['filename'] = (bool) ($thisInfo['raw']['flags'] & 0x08);
94  $thisInfo['flags']['comment'] = (bool) ($thisInfo['raw']['flags'] & 0x10);
95 
96  $thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']);
97 
98  $thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']);
99  if (!$thisInfo['os']) {
100  $info['error'][] = 'Read error on gzip file';
101  return false;
102  }
103 
104  $fpointer = 10;
105  $arr_xsubfield = array();
106  // bit 2 - FLG.FEXTRA
107  //+---+---+=================================+
108  //| XLEN |...XLEN bytes of "extra field"...|
109  //+---+---+=================================+
110  if ($thisInfo['flags']['extra']) {
111  $w_xlen = substr($buff, $fpointer, 2);
112  $xlen = getid3_lib::LittleEndian2Int($w_xlen);
113  $fpointer += 2;
114 
115  $thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
116  // Extra SubFields
117  //+---+---+---+---+==================================+
118  //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
119  //+---+---+---+---+==================================+
120  $idx = 0;
121  while (true) {
122  if ($idx >= $xlen) {
123  break;
124  }
125  $si1 = ord(substr($buff, $fpointer + $idx++, 1));
126  $si2 = ord(substr($buff, $fpointer + $idx++, 1));
127  if (($si1 == 0x41) && ($si2 == 0x70)) {
128  $w_xsublen = substr($buff, $fpointer + $idx, 2);
129  $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
130  $idx += 2;
131  $arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen);
132  $idx += $xsublen;
133  } else {
134  break;
135  }
136  }
137  $fpointer += $xlen;
138  }
139  // bit 3 - FLG.FNAME
140  //+=========================================+
141  //|...original file name, zero-terminated...|
142  //+=========================================+
143  // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
144  $thisInfo['filename'] = preg_replace('#\\.gz$#i', '', $info['filename']);
145  if ($thisInfo['flags']['filename']) {
146  $thisInfo['filename'] = '';
147  while (true) {
148  if (ord($buff[$fpointer]) == 0) {
149  $fpointer++;
150  break;
151  }
152  $thisInfo['filename'] .= $buff[$fpointer];
153  $fpointer++;
154  }
155  }
156  // bit 4 - FLG.FCOMMENT
157  //+===================================+
158  //|...file comment, zero-terminated...|
159  //+===================================+
160  if ($thisInfo['flags']['comment']) {
161  while (true) {
162  if (ord($buff[$fpointer]) == 0) {
163  $fpointer++;
164  break;
165  }
166  $thisInfo['comment'] .= $buff[$fpointer];
167  $fpointer++;
168  }
169  }
170  // bit 1 - FLG.FHCRC
171  //+---+---+
172  //| CRC16 |
173  //+---+---+
174  if ($thisInfo['flags']['crc16']) {
175  $w_crc = substr($buff, $fpointer, 2);
176  $thisInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
177  $fpointer += 2;
178  }
179  // bit 0 - FLG.FTEXT
180  //if ($thisInfo['raw']['flags'] & 0x01) {
181  // Ignored...
182  //}
183  // bits 5, 6, 7 - reserved
184 
185  $thisInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
186  $thisInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
187 
188  $info['gzip']['files'] = getid3_lib::array_merge_clobber($info['gzip']['files'], getid3_lib::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize']));
189 
190  if ($this->option_gzip_parse_contents) {
191  // Try to inflate GZip
192  $csize = 0;
193  $inflated = '';
194  $chkcrc32 = '';
195  if (function_exists('gzinflate')) {
196  $cdata = substr($buff, $fpointer);
197  $cdata = substr($cdata, 0, strlen($cdata) - 8);
198  $csize = strlen($cdata);
199  $inflated = gzinflate($cdata);
200 
201  // Calculate CRC32 for inflated content
202  $thisInfo['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $thisInfo['crc32']);
203 
204  // determine format
205  $formattest = substr($inflated, 0, 32774);
206  $getid3_temp = new getID3();
207  $determined_format = $getid3_temp->GetFileFormat($formattest);
208  unset($getid3_temp);
209 
210  // file format is determined
211  $determined_format['module'] = (isset($determined_format['module']) ? $determined_format['module'] : '');
212  switch ($determined_format['module']) {
213  case 'tar':
214  // view TAR-file info
215  if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && include_once(GETID3_INCLUDEPATH.$determined_format['include'])) {
216  if (($temp_tar_filename = tempnam(GETID3_TEMP_DIR, 'getID3')) === false) {
217  // can't find anywhere to create a temp file, abort
218  $info['error'][] = 'Unable to create temp file to parse TAR inside GZIP file';
219  break;
220  }
221  if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) {
222  fwrite($fp_temp_tar, $inflated);
223  fclose($fp_temp_tar);
224  $getid3_temp = new getID3();
225  $getid3_temp->openfile($temp_tar_filename);
226  $getid3_tar = new getid3_tar($getid3_temp);
227  $getid3_tar->Analyze();
228  $info['gzip']['member_header'][$idx]['tar'] = $getid3_temp->info['tar'];
229  unset($getid3_temp, $getid3_tar);
230  unlink($temp_tar_filename);
231  } else {
232  $info['error'][] = 'Unable to fopen() temp file to parse TAR inside GZIP file';
233  break;
234  }
235  }
236  break;
237 
238  case '':
239  default:
240  // unknown or unhandled format
241  break;
242  }
243  }
244  }
245  }
246  return true;
247  }
array_merge_clobber($array1, $array2)
Definition: getid3.lib.php:358
LittleEndian2Int($byteword, $signed=false)
Definition: getid3.lib.php:266
$info
Definition: example_052.php:80
getID3() by James Heinrich info@getid3.org //
fread($bytes)
Definition: getid3.php:1685
CreateDeepArray($ArrayPath, $Separator, $Value)
Definition: getid3.lib.php:465
fseek($bytes, $whence=SEEK_SET)
Definition: getid3.php:1697
+ Here is the call graph for this function:

◆ get_os_type() [1/2]

getid3_gzip::get_os_type (   $key)

Definition at line 217 of file module.archive.gzip.php.

Referenced by Analyze(), and getid3_gzip().

217  {
218  static $os_type = array(
219  '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
220  '1' => 'Amiga',
221  '2' => 'VMS (or OpenVMS)',
222  '3' => 'Unix',
223  '4' => 'VM/CMS',
224  '5' => 'Atari TOS',
225  '6' => 'HPFS filesystem (OS/2, NT)',
226  '7' => 'Macintosh',
227  '8' => 'Z-System',
228  '9' => 'CP/M',
229  '10' => 'TOPS-20',
230  '11' => 'NTFS filesystem (NT)',
231  '12' => 'QDOS',
232  '13' => 'Acorn RISCOS',
233  '255' => 'unknown'
234  );
235  return @$os_type[$key];
236  }
+ Here is the caller graph for this function:

◆ get_os_type() [2/2]

getid3_gzip::get_os_type (   $key)

Definition at line 250 of file module.archive.gzip.php.

250  {
251  static $os_type = array(
252  '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
253  '1' => 'Amiga',
254  '2' => 'VMS (or OpenVMS)',
255  '3' => 'Unix',
256  '4' => 'VM/CMS',
257  '5' => 'Atari TOS',
258  '6' => 'HPFS filesystem (OS/2, NT)',
259  '7' => 'Macintosh',
260  '8' => 'Z-System',
261  '9' => 'CP/M',
262  '10' => 'TOPS-20',
263  '11' => 'NTFS filesystem (NT)',
264  '12' => 'QDOS',
265  '13' => 'Acorn RISCOS',
266  '255' => 'unknown'
267  );
268  return (isset($os_type[$key]) ? $os_type[$key] : '');
269  }

◆ get_xflag_type() [1/2]

getid3_gzip::get_xflag_type (   $key)

Definition at line 239 of file module.archive.gzip.php.

Referenced by Analyze(), and getid3_gzip().

239  {
240  static $xflag_type = array(
241  '0' => 'unknown',
242  '2' => 'maximum compression',
243  '4' => 'fastest algorithm'
244  );
245  return @$xflag_type[$key];
246  }
+ Here is the caller graph for this function:

◆ get_xflag_type() [2/2]

getid3_gzip::get_xflag_type (   $key)

Definition at line 272 of file module.archive.gzip.php.

272  {
273  static $xflag_type = array(
274  '0' => 'unknown',
275  '2' => 'maximum compression',
276  '4' => 'fastest algorithm'
277  );
278  return (isset($xflag_type[$key]) ? $xflag_type[$key] : '');
279  }

◆ getid3_gzip()

getid3_gzip::getid3_gzip ( $fd,
$ThisFileInfo 
)

Definition at line 22 of file module.archive.gzip.php.

References getid3_lib\array_merge_clobber(), getid3_lib\CreateDeepArray(), getid3_handler\fread(), getid3_handler\fseek(), get_os_type(), get_xflag_type(), and getid3_lib\LittleEndian2Int().

22  {
23  $ThisFileInfo['fileformat'] = 'gzip';
24 
25  $start_length = 10;
26  $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
27  //+---+---+---+---+---+---+---+---+---+---+
28  //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
29  //+---+---+---+---+---+---+---+---+---+---+
30  @fseek($fd, 0);
31  $buffer = @fread($fd, $ThisFileInfo['filesize']);
32 
33  $arr_members = explode("\x1F\x8B\x08", $buffer);
34  while (true) {
35  $is_wrong_members = false;
36  $num_members = intval(count($arr_members));
37  for ($i = 0; $i < $num_members; $i++) {
38  if (strlen($arr_members[$i]) == 0) {
39  continue;
40  }
41  $buf = "\x1F\x8B\x08".$arr_members[$i];
42 
43  $attr = unpack($unpack_header, substr($buf, 0, $start_length));
44  if (!$this->get_os_type(ord($attr['os']))) {
45  // Merge member with previous if wrong OS type
46  $arr_members[$i - 1] .= $buf;
47  $arr_members[$i] = '';
48  $is_wrong_members = true;
49  continue;
50  }
51  }
52  if (!$is_wrong_members) {
53  break;
54  }
55  }
56 
57  $ThisFileInfo['gzip']['files'] = array();
58 
59  $fpointer = 0;
60  $idx = 0;
61  for ($i = 0; $i < $num_members; $i++) {
62  if (strlen($arr_members[$i]) == 0) {
63  continue;
64  }
65  $thisThisFileInfo = &$ThisFileInfo['gzip']['member_header'][++$idx];
66 
67  $buff = "\x1F\x8B\x08".$arr_members[$i];
68 
69  $attr = unpack($unpack_header, substr($buff, 0, $start_length));
70  $thisThisFileInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
71  $thisThisFileInfo['raw']['id1'] = ord($attr['cmethod']);
72  $thisThisFileInfo['raw']['id2'] = ord($attr['cmethod']);
73  $thisThisFileInfo['raw']['cmethod'] = ord($attr['cmethod']);
74  $thisThisFileInfo['raw']['os'] = ord($attr['os']);
75  $thisThisFileInfo['raw']['xflags'] = ord($attr['xflags']);
76  $thisThisFileInfo['raw']['flags'] = ord($attr['flags']);
77 
78  $thisThisFileInfo['flags']['crc16'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x02);
79  $thisThisFileInfo['flags']['extra'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x04);
80  $thisThisFileInfo['flags']['filename'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x08);
81  $thisThisFileInfo['flags']['comment'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x10);
82 
83  $thisThisFileInfo['compression'] = $this->get_xflag_type($thisThisFileInfo['raw']['xflags']);
84 
85  $thisThisFileInfo['os'] = $this->get_os_type($thisThisFileInfo['raw']['os']);
86  if (!$thisThisFileInfo['os']) {
87  $ThisFileInfo['error'][] = 'Read error on gzip file';
88  return false;
89  }
90 
91  $fpointer = 10;
92  $arr_xsubfield = array();
93  // bit 2 - FLG.FEXTRA
94  //+---+---+=================================+
95  //| XLEN |...XLEN bytes of "extra field"...|
96  //+---+---+=================================+
97  if ($thisThisFileInfo['flags']['extra']) {
98  $w_xlen = substr($buff, $fpointer, 2);
99  $xlen = getid3_lib::LittleEndian2Int($w_xlen);
100  $fpointer += 2;
101 
102  $thisThisFileInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
103  // Extra SubFields
104  //+---+---+---+---+==================================+
105  //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
106  //+---+---+---+---+==================================+
107  $idx = 0;
108  while (true) {
109  if ($idx >= $xlen) {
110  break;
111  }
112  $si1 = ord(substr($buff, $fpointer + $idx++, 1));
113  $si2 = ord(substr($buff, $fpointer + $idx++, 1));
114  if (($si1 == 0x41) && ($si2 == 0x70)) {
115  $w_xsublen = substr($buff, $fpointer+$idx, 2);
116  $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
117  $idx += 2;
118  $arr_xsubfield[] = substr($buff, $fpointer+$idx, $xsublen);
119  $idx += $xsublen;
120  } else {
121  break;
122  }
123  }
124  $fpointer += $xlen;
125  }
126  // bit 3 - FLG.FNAME
127  //+=========================================+
128  //|...original file name, zero-terminated...|
129  //+=========================================+
130  // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
131  $thisThisFileInfo['filename'] = eregi_replace('.gz$', '', $ThisFileInfo['filename']);
132  if ($thisThisFileInfo['flags']['filename']) {
133  while (true) {
134  if (ord($buff[$fpointer]) == 0) {
135  $fpointer++;
136  break;
137  }
138  $thisThisFileInfo['filename'] .= $buff[$fpointer];
139  $fpointer++;
140  }
141  }
142  // bit 4 - FLG.FCOMMENT
143  //+===================================+
144  //|...file comment, zero-terminated...|
145  //+===================================+
146  if ($thisThisFileInfo['flags']['comment']) {
147  while (true) {
148  if (ord($buff[$fpointer]) == 0) {
149  $fpointer++;
150  break;
151  }
152  $thisThisFileInfo['comment'] .= $buff[$fpointer];
153  $fpointer++;
154  }
155  }
156  // bit 1 - FLG.FHCRC
157  //+---+---+
158  //| CRC16 |
159  //+---+---+
160  if ($thisThisFileInfo['flags']['crc16']) {
161  $w_crc = substr($buff, $fpointer, 2);
162  $thisThisFileInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
163  $fpointer += 2;
164  }
165  // bit 0 - FLG.FTEXT
166  //if ($thisThisFileInfo['raw']['flags'] & 0x01) {
167  // Ignored...
168  //}
169  // bits 5, 6, 7 - reserved
170 
171  $thisThisFileInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
172  $thisThisFileInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
173 
174  $ThisFileInfo['gzip']['files'] = getid3_lib::array_merge_clobber($ThisFileInfo['gzip']['files'], getid3_lib::CreateDeepArray($thisThisFileInfo['filename'], '/', $thisThisFileInfo['filesize']));
175 
176  if ($this->option_gzip_parse_contents) {
177  // Try to inflate GZip
178  $csize = 0;
179  $inflated = '';
180  $chkcrc32 = '';
181  if (function_exists('gzinflate')) {
182  $cdata = substr($buff, $fpointer);
183  $cdata = substr($cdata, 0, strlen($cdata) - 8);
184  $csize = strlen($cdata);
185  $inflated = gzinflate($cdata);
186 
187  // Calculate CRC32 for inflated content
188  $thisThisFileInfo['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $thisThisFileInfo['crc32']);
189 
190  // determine format
191  $formattest = substr($inflated, 0, 32774);
192  $newgetID3 = new getID3();
193  $determined_format = $newgetID3->GetFileFormat($formattest);
194  unset($newgetID3);
195 
196  // file format is determined
197  switch (@$determined_format['module']) {
198  case 'tar':
199  // view TAR-file info
200  if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && @include_once(GETID3_INCLUDEPATH.$determined_format['include'])) {
201  getid3_tar::read_tar($inflated, $ThisFileInfo['gzip']['member_header'][$idx]);
202  }
203  break;
204 
205  case '':
206  default:
207  // unknown or unhandled format
208  break;
209  }
210  }
211  }
212  }
213  return true;
214  }
array_merge_clobber($array1, $array2)
Definition: getid3.lib.php:358
LittleEndian2Int($byteword, $signed=false)
Definition: getid3.lib.php:266
fread($bytes)
Definition: getid3.php:1685
CreateDeepArray($ArrayPath, $Separator, $Value)
Definition: getid3.lib.php:465
fseek($bytes, $whence=SEEK_SET)
Definition: getid3.php:1697
+ Here is the call graph for this function:

Field Documentation

◆ $option_gzip_parse_contents

getid3_gzip::$option_gzip_parse_contents = false

Definition at line 20 of file module.archive.gzip.php.


The documentation for this class was generated from the following file: