1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This debug script is used during zip support development ONLY.
|
|
|
19 |
*
|
|
|
20 |
* @package core_files
|
|
|
21 |
* @copyright 2012 Petr Skoda
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
define('CLI_SCRIPT', true);
|
|
|
26 |
|
|
|
27 |
require(__DIR__.'/../../../../config.php');
|
|
|
28 |
require_once("$CFG->libdir/clilib.php");
|
|
|
29 |
require_once("$CFG->libdir/filestorage/zip_packer.php");
|
|
|
30 |
|
|
|
31 |
if (count($_SERVER['argv']) != 2 or !file_exists($_SERVER['argv'][1])) {
|
|
|
32 |
cli_error("This script expects zip file name as the only parameter");
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
$archive = $_SERVER['argv'][1];
|
|
|
36 |
|
|
|
37 |
// Note: the ZIP structure is described at http://www.pkware.com/documents/casestudies/APPNOTE.TXT
|
|
|
38 |
if (!$filesize = filesize($archive) or !$fp = fopen($archive, 'rb+')) {
|
|
|
39 |
cli_error("Can not open ZIP archive: $archive");
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
if ($filesize == 22) {
|
|
|
43 |
$info = unpack('Vsig', fread($fp, 4));
|
|
|
44 |
fclose($fp);
|
|
|
45 |
if ($info['sig'] == 0x6054b50) {
|
|
|
46 |
cli_error("This ZIP archive is empty: $archive");
|
|
|
47 |
} else {
|
|
|
48 |
cli_error("This is not a ZIP archive: $archive");
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
fseek($fp, 0);
|
|
|
53 |
$info = unpack('Vsig', fread($fp, 4));
|
|
|
54 |
if ($info['sig'] !== 0x04034b50) {
|
|
|
55 |
fclose($fp);
|
|
|
56 |
cli_error("This is not a ZIP archive: $archive");
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// Find end of central directory record.
|
|
|
60 |
$centralend = zip_archive::zip_get_central_end($fp, $filesize);
|
|
|
61 |
if ($centralend === false) {
|
|
|
62 |
cli_error("This is not a ZIP archive: $archive");
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
if ($centralend['disk'] !== 0 or $centralend['disk_start'] !== 0) {
|
|
|
66 |
cli_error("Multi-disk archives are not supported: $archive");
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
if ($centralend['offset'] === 0xFFFFFFFF) {
|
|
|
70 |
cli_error("ZIP64 archives are not supported: $archive");
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
fseek($fp, $centralend['offset']);
|
|
|
74 |
$data = fread($fp, $centralend['size']);
|
|
|
75 |
$pos = 0;
|
|
|
76 |
$files = array();
|
|
|
77 |
for($i=0; $i<$centralend['entries']; $i++) {
|
|
|
78 |
$file = zip_archive::zip_parse_file_header($data, $centralend, $pos);
|
|
|
79 |
if ($file === false) {
|
|
|
80 |
cli_error('Invalid Zip file header structure: '.$archive);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Read local file header.
|
|
|
84 |
fseek($fp, $file['local_offset']);
|
|
|
85 |
$localfile = unpack('Vsig/vversion_req/vgeneral/vmethod/Vmodified/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length', fread($fp, 30));
|
|
|
86 |
if ($localfile['sig'] !== 0x04034b50) {
|
|
|
87 |
// Borked file!
|
|
|
88 |
$file['error'] = 'Invalid local file signature';
|
|
|
89 |
$files[] = $file;
|
|
|
90 |
continue;
|
|
|
91 |
}
|
|
|
92 |
if ($localfile['name_length']) {
|
|
|
93 |
$localfile['name'] = fread($fp, $localfile['name_length']);
|
|
|
94 |
} else {
|
|
|
95 |
$localfile['name'] = '';
|
|
|
96 |
}
|
|
|
97 |
$localfile['extra'] = array();
|
|
|
98 |
$localfile['extra_data'] = '';
|
|
|
99 |
if ($localfile['extra_length']) {
|
|
|
100 |
$extradata = fread($fp, $localfile['extra_length']);
|
|
|
101 |
$localfile['extra_data'] = $extradata;
|
|
|
102 |
while (strlen($extradata) > 4) {
|
|
|
103 |
$extra = unpack('vid/vsize', substr($extradata, 0, 4));
|
|
|
104 |
$extra['data'] = substr($extradata, 4, $extra['size']);
|
|
|
105 |
$extradata = substr($extradata, 4+$extra['size']);
|
|
|
106 |
$localfile['extra'][] = $extra;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$file['local'] = $localfile;
|
|
|
111 |
$files[] = $file;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
echo "Archive: $archive\n";
|
|
|
115 |
echo "Number of files: {$centralend['entries']}\n";
|
|
|
116 |
echo "Archive comment: \"{$centralend['comment']}\" ({$centralend['comment_length']} bytes)\n";
|
|
|
117 |
foreach ($files as $i=>$file) {
|
|
|
118 |
echo "======== File ".($i+1)." ==============================================\n";
|
|
|
119 |
echo " Name: ".zip_print_name($file['name'])."\n";
|
|
|
120 |
if ($file['comment'] !== '') {
|
|
|
121 |
echo " Comment: \"{$file['comment']}\" ({$file['comment_length']} bytes)\n";
|
|
|
122 |
}
|
|
|
123 |
echo " Version: 0x".str_pad(dechex($file['version']), 4, '0', STR_PAD_LEFT)."\n";
|
|
|
124 |
echo " Required: 0x".str_pad(dechex($file['version_req']), 4, '0', STR_PAD_LEFT)."\n";
|
|
|
125 |
echo " Method: ".zip_print_method($file['method'])."\n";
|
|
|
126 |
echo " General: ".zip_print_general($file['general'])."\n";
|
|
|
127 |
echo " Modified: ".userdate(zip_dos2unixtime($file['modified']))."\n";
|
|
|
128 |
echo " Size: ".zip_print_sizes($file['size'], $file['size_compressed'])."\n";
|
|
|
129 |
echo " CRC-32: {$file['crc']}\n";
|
|
|
130 |
foreach($file['extra'] as $j=>$extra) {
|
|
|
131 |
echo " Extra ".($j+1).": ".zip_print_extra($extra)."\n";
|
|
|
132 |
}
|
|
|
133 |
if (!empty($file['local']['error'])) {
|
|
|
134 |
echo " Local ERROR: {$file['local']['error']}\n";
|
|
|
135 |
continue;
|
|
|
136 |
}
|
|
|
137 |
$localfile = $file['local'];
|
|
|
138 |
if ($localfile['name'] !== $file['name']) {
|
|
|
139 |
echo " Local name: ".zip_print_name($localfile['name'])."\n";
|
|
|
140 |
}
|
|
|
141 |
if ($localfile['version_req'] !== $file['version_req']) {
|
|
|
142 |
echo " Local required: 0x".str_pad(dechex($localfile['version_req']), 4, '0', STR_PAD_LEFT)."\n";
|
|
|
143 |
}
|
|
|
144 |
if ($localfile['method'] !== $file['method']) {
|
|
|
145 |
echo " Local method: ".zip_print_method($localfile['method'])."\n";
|
|
|
146 |
}
|
|
|
147 |
if ($localfile['general'] !== $file['general']) {
|
|
|
148 |
echo " Local general: ".zip_print_general($localfile['general'])."\n";
|
|
|
149 |
}
|
|
|
150 |
if ($localfile['modified'] !== $file['modified']) {
|
|
|
151 |
echo " Local modified: ".userdate(zip_dos2unixtime($localfile['modified']))."\n";
|
|
|
152 |
}
|
|
|
153 |
if ($localfile['size'] !== $file['size']) {
|
|
|
154 |
echo " Local size: ".zip_print_sizes($localfile['size'], $localfile['size_compressed'])."\n";
|
|
|
155 |
}
|
|
|
156 |
if ($localfile['crc'] !== $file['crc']) {
|
|
|
157 |
echo " Local CRC-32: {$localfile['crc']}\n";
|
|
|
158 |
}
|
|
|
159 |
foreach($localfile['extra'] as $j=>$extra) {
|
|
|
160 |
echo " Local extra ".($j+1).": ".zip_print_extra($extra)."\n";
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
fclose($fp);
|
|
|
165 |
exit(0);
|
|
|
166 |
|
|
|
167 |
// === Some useful functions ======================================
|
|
|
168 |
|
|
|
169 |
function zip_print_name($name) {
|
|
|
170 |
$size = strlen($name);
|
|
|
171 |
$crc = crc32($name);
|
|
|
172 |
return "\"$name\" ($size bytes) - CRC $crc";
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
function zip_print_method($method) {
|
|
|
176 |
$desc = '';
|
|
|
177 |
switch($method) {
|
|
|
178 |
case 0: $desc = 'Stored'; break;
|
|
|
179 |
case 1: $desc = 'Shrunk'; break;
|
|
|
180 |
case 2: $desc = 'Reduced factor 1'; break;
|
|
|
181 |
case 3: $desc = 'Reduced factor 2'; break;
|
|
|
182 |
case 4: $desc = 'Reduced factor 3'; break;
|
|
|
183 |
case 5: $desc = 'Reduced factor 4'; break;
|
|
|
184 |
case 6: $desc = 'Imploded'; break;
|
|
|
185 |
case 8: $desc = 'Deflated'; break;
|
|
|
186 |
case 9: $desc = 'Deflate64'; break;
|
|
|
187 |
case 10: $desc = 'old IBM TERSE'; break;
|
|
|
188 |
case 12: $desc = 'BZIP2'; break;
|
|
|
189 |
case 14: $desc = 'LZMA'; break;
|
|
|
190 |
case 18: $desc = 'IBM TERSE'; break;
|
|
|
191 |
case 19: $desc = 'IBM LZ77'; break;
|
|
|
192 |
case 97: $desc = 'WavPack'; break;
|
|
|
193 |
case 98: $desc = 'PPMd v1'; break;
|
|
|
194 |
}
|
|
|
195 |
if ($desc) {
|
|
|
196 |
$desc = " ($desc)";
|
|
|
197 |
}
|
|
|
198 |
return "0x".str_pad(dechex($method), 4, '0', STR_PAD_LEFT).$desc;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
function zip_print_general($general) {
|
|
|
202 |
$desc = array();
|
|
|
203 |
if ($general & pow(2, 0)) {
|
|
|
204 |
$desc[] = 'Encrypted';
|
|
|
205 |
}
|
|
|
206 |
if ($general & pow(2, 11)) {
|
|
|
207 |
$desc[] = 'Unicode name';
|
|
|
208 |
}
|
|
|
209 |
if ($desc) {
|
|
|
210 |
$desc = " (".implode(', ', $desc).")";
|
|
|
211 |
} else {
|
|
|
212 |
$desc = '';
|
|
|
213 |
}
|
|
|
214 |
return str_pad(decbin($general), 16, '0', STR_PAD_LEFT).$desc;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Convert MS date+time format to unix timestamp:
|
|
|
219 |
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms724274(v=vs.85).aspx
|
|
|
220 |
*
|
|
|
221 |
* Copied from: http://plugins.svn.wordpress.org/wp2epub/trunk/zipcreate/functions.lib.php
|
|
|
222 |
* author: redmonkey
|
|
|
223 |
* license: GPL
|
|
|
224 |
*/
|
|
|
225 |
function zip_dos2unixtime($dostime) {
|
|
|
226 |
$sec = 2 * ($dostime & 0x1f);
|
|
|
227 |
$min = ($dostime >> 5) & 0x3f;
|
|
|
228 |
$hrs = ($dostime >> 11) & 0x1f;
|
|
|
229 |
$day = ($dostime >> 16) & 0x1f;
|
|
|
230 |
$mon = ($dostime >> 21) & 0x0f;
|
|
|
231 |
$year = (($dostime >> 25) & 0x7f) + 1980;
|
|
|
232 |
|
|
|
233 |
return mktime($hrs, $min, $sec, $mon, $day, $year);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
function zip_print_sizes($size, $compressed) {
|
|
|
237 |
return "$size ==> $compressed bytes";
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
function zip_print_extra($extra) {
|
|
|
241 |
$desc = '';
|
|
|
242 |
$info = "- ".bin2hex($extra['data'])." ({$extra['size']} bytes)";
|
|
|
243 |
switch($extra['id']) {
|
|
|
244 |
case 0x0009: $desc = 'OS/2'; break;
|
|
|
245 |
case 0x000a: $desc = 'NTFS'; break;
|
|
|
246 |
case 0x000d: $desc = 'UNIX'; break;
|
|
|
247 |
case 0x5455: $desc = 'Extended timestamp'; break;
|
|
|
248 |
case 0x5855: $desc = 'Infor-ZIP (original)'; break;
|
|
|
249 |
case 0x7075:
|
|
|
250 |
$desc = 'Info-ZIP Unicode path';
|
|
|
251 |
$data = unpack('cversion/Vcrc', substr($extra['data'], 0, 5));
|
|
|
252 |
$name = substr($extra['data'], 5);
|
|
|
253 |
$size = strlen($name);
|
|
|
254 |
if ($data['version'] === 1) {
|
|
|
255 |
$info = "- \"$name\" ($size bytes) - CRC {$data['crc']}";
|
|
|
256 |
}
|
|
|
257 |
break;
|
|
|
258 |
case 0x7865: $desc = 'Info-ZIP UNIX (new)'; break;
|
|
|
259 |
case 0x7875: $desc = 'Info-ZIP UNIX (3rd generation)'; break;
|
|
|
260 |
}
|
|
|
261 |
if ($desc) {
|
|
|
262 |
$desc = " ($desc)";
|
|
|
263 |
}
|
|
|
264 |
return "0x".str_pad(dechex($extra['id']), 4, '0', STR_PAD_LEFT)."$desc $info";
|
|
|
265 |
}
|