[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.archive.gzip.php
1 <?php
2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
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 // public: Optional file list - disable for speed.
26 public $option_gzip_parse_contents = false; // decode gzipped files, if possible, and parse recursively (.tar.gz for example)
27
28 public function Analyze() {
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 $this->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 $this->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 $this->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 $this->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 }
248
249 // Converts the OS type
250 public function get_os_type($key) {
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 }
270
271 // Converts the eXtra FLags
272 public function get_xflag_type($key) {
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 }
280 }
281