This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
dumplist.php
465 lines (357 loc) · 12.3 KB
/
dumplist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* Usage: dumplist.php [check|testmd5|testsha1|updatesha1|generate|update|touchdir]
*
* From the shadows. We shall rise...
*
* @Author Jorge Oliveira (NewEraCracker)
* @License Public Domain
* @Version 2.9.3
*/
/** Increase memory limit to handle large amounts of data */
ini_set('memory_limit', '512M');
/** Run */
new NewEra_DumpList('./filelist.md5', array('./_incoming/', './.htaccess', './.htpasswd', './index.php'));
/** Utility static methods for dump listing */
class NewEra_DumpListUtil {
/**
* This will parse a listfile
* Note: With strict option enabled, it will not be compatible with files being mixed version 1 and 2.
*/
public static function parse_listfile($filename, $strict = false) {
$fileproperties = $comment = $content = array();
if (false === file_exists($filename)) {
trigger_error('Error parsing listfile: File does not exist', E_USER_WARNING);
return false;
}
$filecontents = file_get_contents($filename);
$marker = $strict ? '[*]' : '';
if (0 >= preg_match_all('@^; (?<mtime>[0-9]+) (?<name>' . $marker . '[^\r\n]+)$@m', $filecontents, $comment)) {
$comment = array();
// Compatibility with files generated by version 2
if (0 >= preg_match_all('@^; (?<mtime>[0-9]+) (?<sha1>[0-9a-f]{40}) (?<name>[*][^\r\n]+)$@m', $filecontents, $comment))
{
trigger_error('Error parsing listfile: Unable to parse comments', E_USER_WARNING);
return false;
}
}
if (0 >= preg_match_all('@^(?<md5>[0-9a-f]{32}) (?<name>[*][^\r\n]+)$@m', $filecontents, $content)) {
trigger_error('Error parsing listfile: Unable to parse contents', E_USER_WARNING);
return false;
}
if (count($comment['name']) == count($content['name'])) {
// Compatibility with files generated by version 1
if (!isset($comment['sha1'])) { $comment['sha1'] = array(); }
for ($i=0, $sz=count($comment['name']); $i<$sz; $i++) {
// Compatibility with files generated by version 1 and/or version 2
if ($comment['name'][$i][0] != '*') {
$comment['sha1'][$i] = substr($comment['name'][$i], 0, 40);
$comment['name'][$i] = strstr($comment['name'][$i], '*');
}
if ($comment['name'][$i] == $content['name'][$i]) {
// Not an hack: We have to remove the asterisk in begining and restore ./ in path for PHP to be able to work it out
$file = './'.substr($comment['name'][$i], 1);
$fileproperties["{$file}"] = array(
'mtime' => $comment['mtime'][$i],
'sha1' => (empty($comment['sha1'][$i]) ? '' : $comment['sha1'][$i]),
'md5' => $content['md5'][$i]
);
} else {
trigger_error('Error parsing listfile: Invalid entry order', E_USER_WARNING);
return false;
}
}
} else {
trigger_error('Error parsing listfile: Invalid entry count', E_USER_WARNING);
return false;
}
return $fileproperties;
}
/** This will generate a listfile */
public static function generate_listfile($fileproperties) {
// Sort file properties array
uksort($fileproperties, 'NewEra_Compare::sort_files_by_name');
// Init contents of list file
$comment = $content = '';
foreach ($fileproperties as $file => $properties) {
// Not an hack: We have to replace ./ in path by an asterisk for other applications (QuickSFV, TeraCopy...) to be able to work it out
$filename = '*'.substr($file, 2);
$comment .= "; {$properties['mtime']} " . ($properties['sha1'] ? "{$properties['sha1']} " : ''). "{$filename}\n";
$content .= "{$properties['md5']} {$filename}\n";
}
return $comment.$content;
}
/** Array with the paths a dir contains */
public static function readdir_recursive($dir='.', $show_dirs=false, $ignored=array()) {
// Set types for stack and return value
$stack = $result = array();
// Initialize stack
$stack[] = $dir;
// Pop the first element of stack and evaluate it (do this until stack is fully empty)
while ($dir = array_shift($stack)) {
$dh = opendir($dir);
while ($dh && (false !== ($path = readdir($dh)))) {
if ($path != '.' && $path != '..') {
// Prepend dir to current path
$path = $dir.'/'.$path;
if (is_dir($path)) {
// Check ignored dirs
if (is_array($ignored) && count($ignored) && in_array($path.'/', $ignored)) { continue; }
// Add dir to stack for reading
$stack[] = $path;
// If $show_dirs is true, add dir path to result
if ($show_dirs) { $result[] = $path; }
} elseif (is_file($path)) {
// Check ignored files
if (is_array($ignored) && count($ignored) && in_array($path, $ignored)) { continue; }
// Add file path to result
$result[] = $path;
}
}
}
closedir($dh);
}
// Sort the array using simple ordering
sort($result);
// Now we can return it
return $result;
}
}
/* Useful comparators */
class NewEra_Compare {
/* Ascending directory sorting by names */
public static function sort_files_by_name($a, $b) {
/* Equal */
if ($a == $b) { return 0; }
/* Let strcmp decide */
return strcmp($a, $b);
}
/* Ascending directory sorting by levels and names */
public static function sort_files_by_level_asc($a, $b) {
/* Equal */
if ($a == $b) { return 0; }
/* Check dir levels */
$la = substr_count($a, '/');
$lb = substr_count($b, '/');
/* Prioritize levels, in case of equality let sorting by names decide */
return (($la < $lb) ? -1 : (($la == $lb) ? self::sort_files_by_name($a, $b) : 1));
}
/* Reverse directory sorting by levels and names */
public static function sort_files_by_level_dsc($a, $b) {
return self::sort_files_by_level_asc($b, $a);
}
}
/** Methods used in dump listing */
class NewEra_DumpList {
/** Ignored paths */
private $ignored;
/** The file that holds the file list */
private $listfile;
/** Simple file list array */
private $filelist;
/** Detailed file list array */
private $fileproperties;
/** Construct the object and perform actions */
public function __construct($listfile='./filelist.md5', $ignored=array()) {
$this->listfile = $listfile;
// Build ignored paths
$this->ignored = array_merge(
array( $listfile, /* List file */
// './'.basename(__FILE__), /* This file */
), $ignored); /* Original ignored array */
// Check CLI
if (isset($_SERVER['REQUEST_METHOD'])) { die('This script must be ran from CLI.'); }
// Check arguments count
if ($_SERVER['argc'] != 2) { die('Usage: '.basename(__FILE__)." [check|testmd5|testsha1|updatesha1|generate|update|touchdir]\n"); }
// Change dir
chdir(dirname(__FILE__));
// Process arguments
switch($_SERVER['argv'][1]) {
case 'testmd5':
$this->dumplist_check(true);
break;
case 'testsha1':
$this->dumplist_check(false, true);
break;
case 'updatesha1':
$this->dumplist_check(false, false, true);
break;
case 'check':
$this->dumplist_check(false);
break;
case 'generate':
$this->dumplist_generate();
break;
case 'update':
$this->dumplist_update();
break;
case 'touchdir':
$this->dumplist_touchdir();
break;
default:
die('Usage: '.basename(__FILE__)." [check|testmd5|testsha1|updatesha1|generate|update|touchdir]\n");
}
}
/** Run the check on each file */
private function dumplist_check($testmd5 = false, $testsha1 = false, $updatesha1 = false) {
$this->filelist = NewEra_DumpListUtil::readdir_recursive('.', false, $this->ignored);
$this->fileproperties = NewEra_DumpListUtil::parse_listfile($this->listfile);
if (!$this->fileproperties) { return; }
foreach ($this->filelist as $file) {
// Handle creation case
if (!isset($this->fileproperties["{$file}"])) {
echo "{$file} is a new file.\n";
continue;
}
}
foreach ($this->fileproperties as $file => $properties) {
// Handle deletion
if (!file_exists($file)) {
echo "{$file} does not exist.\n";
continue;
}
// Handle file modification
if (filemtime($file) != $properties['mtime']) {
echo "{$file} was modified.\n";
continue;
}
// Test file MD5 if required
if ($testmd5) {
$md5 = md5_file($file);
if ($md5 != $properties['md5']) {
echo "{$file} Expected MD5: {$properties['md5']} Got: {$md5}.\n";
continue;
}
}
// Test file SHA1 if required
if ($testsha1 && $properties['sha1']) {
$sha1 = sha1_file($file);
if ($sha1 != $properties['sha1']) {
echo "{$file} Expected SHA1: {$properties['sha1']} Got: {$sha1}.\n";
continue;
}
}
// Migrate SHA1 if required
if ($updatesha1 && !$properties['sha1']) {
$md5 = md5_file($file);
if ($md5 == $properties['md5']) {
$properties['sha1'] = sha1_file($file);
$this->fileproperties["{$file}"] = $properties;
} else {
echo "{$file} Expected MD5: {$properties['md5']} Got: {$md5}.\n";
continue;
}
}
}
// Write new file if migrating
if ($updatesha1) {
$contents = NewEra_DumpListUtil::generate_listfile($this->fileproperties);
file_put_contents($this->listfile, $contents);
}
}
/** Generate dump file listing */
private function dumplist_generate() {
$this->filelist = NewEra_DumpListUtil::readdir_recursive('.', false, $this->ignored);
$this->fileproperties = array();
foreach ($this->filelist as $file) {
$this->fileproperties["{$file}"] = array(
'mtime' => filemtime($file),
'sha1' => sha1_file($file),
'md5' => md5_file($file)
);
}
$contents = NewEra_DumpListUtil::generate_listfile($this->fileproperties);
file_put_contents($this->listfile, $contents);
}
/** Update dump file listing */
private function dumplist_update() {
$this->filelist = NewEra_DumpListUtil::readdir_recursive('.', false, $this->ignored);
$this->fileproperties = NewEra_DumpListUtil::parse_listfile($this->listfile);
if (!$this->fileproperties) { return; }
foreach ($this->filelist as $file) {
// Handle creation case
if (!isset($this->fileproperties["{$file}"]))
{
$this->fileproperties["{$file}"] = array(
'mtime' => filemtime($file),
'sha1' => sha1_file($file),
'md5' => md5_file($file)
);
continue;
}
}
// Save the keys to remove in case there is file deletion
$keys_to_remove = array();
// Handle each file in the properties list
foreach ($this->fileproperties as $file => $properties) {
// Handle deletion (Save it, will delete the keys later)
if (!file_exists($file)) {
$keys_to_remove[] = $file;
continue;
}
// Handle file modification
if (filemtime($file) != $properties['mtime']) {
$this->fileproperties["{$file}"] = array(
'mtime' => filemtime($file),
'sha1' => sha1_file($file),
'md5' => md5_file($file)
);
continue;
}
}
// Handle deletion (Delete the keys now)
if (count($keys_to_remove) > 0) {
foreach ($keys_to_remove as $key) {
unset($this->fileproperties[$key]);
}
}
$contents = NewEra_DumpListUtil::generate_listfile($this->fileproperties);
file_put_contents($this->listfile, $contents);
}
private function dumplist_touchdir() {
// Filelist including directories
$list = NewEra_DumpListUtil::readdir_recursive('.', true, $this->ignored);
// http://php.net/manual/en/function.touch.php#refsect1-function.touch-changelog
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && version_compare(PHP_VERSION, '5.3') < 0) {
// This method will not work on Windows with PHP versions lower than 5.3
return false;
}
// Easier with a bottom to top approach
usort($list, 'NewEra_Compare::sort_files_by_level_dsc');
// Handle list including directories. Then run
// another pass with list without directories
for ($i = 0; $i < 2; $i++) {
// Reset internal variables state
$dir = $time = null;
// Handle list
foreach ($list as $file) {
// Ignore dir dates on pass two
if ($i == 1 && is_dir($file)) {
continue;
}
// Blacklist certain names
if (false !== stripos($file, '/desktop.ini') || false !== stripos($file, '/.')) {
continue;
}
// Reset internal variables state when moving to another dir
if ($dir !== dirname($file)) {
$dir = dirname($file);
$time = 0;
}
// Save current time
$mtime = @filemtime($file);
// Only update when mtime is correctly set and higher than time
// Also check for writability to prevent errors
if ($mtime > 0 && $mtime > $time && is_writable($dir)) {
// Save new timestamp
$time = $mtime;
// Update timestamp
touch($dir, $time);
}
}
}
// I think we should be OK
return true;
}
}
?>