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