This repository was archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathversion_match.php
More file actions
1385 lines (1256 loc) · 38.5 KB
/
version_match.php
File metadata and controls
1385 lines (1256 loc) · 38.5 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* WoWRoster.net WoWRoster
*
* Roster Diag File Checker Interface
* DO NOT SHIP WITH THE RELEASE ROSTER!!!!
*
* @copyright 2002-2011 WoWRoster.net
* @license http://www.gnu.org/licenses/gpl.html Licensed under the GNU General Public License v3.
* @package WoWRoster
* @subpackage RosterDiag
*/
error_reporting(E_ALL);
// Needed so files think we are in Roster =P
define('IN_ROSTER', true);
/**
* OS specific Directory Seperator
*/
define('DIR_SEP', DIRECTORY_SEPARATOR);
/**
* Base, absolute roster directory
*/
define('ROSTER_BASE', dirname(__FILE__) . DIR_SEP);
define('ROSTER_LIB', ROSTER_BASE . 'lib' . DIR_SEP);
// This file is for on the SVN only, so this should NOT be shipped to the clients!!!
require_once (ROSTER_BASE . 'lib/constants.php');
require_once (ROSTER_LIB . 'functions.lib.php');
require_once (ROSTER_LIB . 'roster.php');
$roster = new roster();
define('ROSTER_PAGE_NAME', '');
$roster->config['seo_url'] = false;
require_once (ROSTER_LIB . 'cmslink.lib.php');
$roster->config['img_url'] = ROSTER_PATH . 'img/';
require_once (ROSTER_LIB . 'rosterdiag.lib.php');
if( isset($_GET['getfile']) && $_GET['getfile'] != '' )
{
$getfile = $_GET['getfile'];
$pathparts_getfile = pathinfo($getfile);
$realpath_getfile = realpath($pathparts_getfile['dirname']);
$realpathparts_getfile = pathinfo($realpath_getfile);
$thisfile = $_SERVER['SCRIPT_FILENAME'];
$realpath_thisfile = realpath($thisfile);
$pathparts_thisfile = pathinfo($thisfile);
$realpathparts_thisfile = pathinfo($realpath_thisfile);
if( substr($_GET['getfile'], 0, 1) == '.' )
{
$subpath_getfile = substr($pathparts_getfile['dirname'], 2);
}
else
{
$subpath_getfile = $pathparts_getfile['dirname'];
}
if( !checkfile($pathparts_getfile, $realpath_getfile, $realpathparts_thisfile['dirname'] . '/' . $subpath_getfile) )
{
print("<pre>[ERROR] INVALID FILE: " . $_GET['getfile'] . ", Operation NOT Allowed!!!</pre>\n");
}
else
{
// Safety Checks have been completed, the file requested in this part or deeper, and is not conf.php or version_match.php
$filename = $getfile;
if( is_file($filename) && is_readable($filename) )
{
// File is OK and readable, so lets get it, either header only or the full file
if( isset($_GET['mode']) && $_GET['mode'] == 'diff' )
{
$handle = fopen($filename, 'rb');
$contents = fread($handle, filesize($filename));
fclose($handle);
print($contents);
}
elseif( isset($_GET['mode']) && $_GET['mode'] == 'md5' )
{
print(md5_file($filename));
}
else
{
print("<pre>[ERROR] FILE MODE ERROR: No Get-Mode specified</pre>\n");
}
}
else
{
print("<pre>[ERROR] FILE NOT READABLE: " . $filename . " is not readable!</pre>\n");
}
}
exit();
}
elseif( isset($_POST['remotediag']) && $_POST['remotediag'] == 'true' )
{
$roster->config['theme'] = 'default';
$roster->config['default_name'] = $_POST['guildname'];
$roster->output['title'] = 'Remote Diagnostics';
$roster->config['website_address'] = $_SERVER['HTTP_REFERER'];
$roster->config['logo'] = $roster->config['img_url'] . 'wowroster_logo.jpg';
$roster->config['roster_bg'] = $roster->config['img_url'] . 'wowroster_bg.jpg';
require_once (ROSTER_LIB . 'template.php');
$roster->tpl = new RosterTemplate();
/**
* Assign initial template vars
*/
$roster->tpl->assign_vars(array(
'S_SEO_URL' => false,
'S_HEADER_LOGO' => (!empty($roster->config['logo']) ? true : false),
'U_MAKELINK' => makelink(),
'U_LINKFORM' => linkform(),
'ROSTER_URL' => ROSTER_URL,
'ROSTER_PATH' => ROSTER_PATH,
'WEBSITE_ADDRESS' => $roster->config['website_address'],
'HEADER_LOGO' => $roster->config['logo'],
'IMG_URL' => $roster->config['img_url'],
'INTERFACE_URL' => '',
'ROSTER_VERSION' => ROSTER_VERSION,
'ROSTER_CREDITS' => '',
'XML_LANG' => 'en',
'PAGE_TITLE' => '',
'ROSTER_HEAD' => '',
'ROSTER_BODY' => '',
'ROSTER_ONLOAD' => ''
));
include_once (ROSTER_BASE . 'header.php');
$temp_array = split('&', $_SERVER['QUERY_STRING']);
foreach( $temp_array as $key => $value )
{
if( substr($value, 0, 15) == 'files' )
{
$_POST['files'][] = substr($value, 15, strlen($value));
}
}
foreach( $files as $directory => $filedata )
{
foreach( $filedata as $filename => $file )
{
$files[$directory][$filename]['remote']['versionFile'] = $filename;
unset($files[$directory][$filename]['local']['versionFile']);
$files[$directory][$filename]['remote']['versionDesc'] = $file['local']['versionDesc'];
unset($files[$directory][$filename]['local']['versionDesc']);
$files[$directory][$filename]['remote']['versionRev'] = $file['local']['versionRev'];
unset($files[$directory][$filename]['local']['versionRev']);
$files[$directory][$filename]['remote']['versionDate'] = $file['local']['versionDate'];
unset($files[$directory][$filename]['local']['versionDate']);
$files[$directory][$filename]['remote']['versionAuthor'] = $file['local']['versionAuthor'];
unset($files[$directory][$filename]['local']['versionAuthor']);
$files[$directory][$filename]['remote']['versionMD5'] = $file['local']['versionMD5'];
unset($files[$directory][$filename]['local']['versionMD5']);
unset($files[$directory][$filename]['local']);
}
}
foreach( $_POST['files'] as $directory => $filedata )
{
foreach( $filedata as $file => $fdata )
{
$files[$directory][$file]['local']['versionFile'] = $file;
$files[$directory][$file]['local']['versionDesc'] = $fdata['versionDesc'];
$files[$directory][$file]['local']['versionRev'] = $fdata['versionRev'];
$files[$directory][$file]['local']['versionDate'] = $fdata['versionDate'];
$files[$directory][$file]['local']['versionAuthor'] = $fdata['versionAuthor'];
$files[$directory][$file]['local']['versionMD5'] = $fdata['versionMD5'];
}
}
VerifyVersions();
// Make a post form for the download of a Zip Package
$zippackage_files = '';
foreach( $directories as $directory => $filecount )
{
if( isset($files[$directory]) )
{
foreach( $files[$directory] as $file => $filedata )
{
if( $filedata['update'] )
{
if( isset($file) && $file != 'newer' && $file != 'severity' && $file != 'tooltip' && $file != 'rollup' && $file != 'rev' && $file != 'date' && $file != 'author' && $file != 'md5' && $file != 'update' && $file != 'missing' )
{
if( $zippackage_files != '' )
{
$zippackage_files .= ';';
}
$zippackage_files .= $directory . '/' . $file;
}
}
}
}
}
if( $zippackage_files != '' )
{
echo border('spurple', 'start', 'Download Update Package From: <small style="font-weight:bold;"><i>SVN @ ' . str_replace('version_match.php', '', ROSTER_SVNREMOTE) . '</i></small>');
echo '<div align="center"><form method="post" action="' . ROSTER_SVNREMOTE . '">';
echo '<input type="hidden" name="filestoget" value="' . $zippackage_files . '">';
echo '<input type="hidden" name="guildname" value="' . $roster->config['default_name'] . '">';
echo '<input type="hidden" name="website" value="' . $roster->config['website_address'] . '">';
echo '<input type="radio" name="ziptype" value="zip" checked="checked">.zip Archive<br />';
echo '<input type="radio" name="ziptype" value="targz">.tar.gz Archive<br /><br />';
echo '<input style="decoration:bold;" type="submit" value="[GET UPDATE PACKAGE]">';
echo '</form></div>';
echo border('spurple', 'end') . '<br />';
}
// Open the main FileVersion table in total color
echo border('sgray', 'start', '<span class="blue">File Versions:</span> <small style="color:#6ABED7;font-weight:bold;"><i>SVN @ ' . str_replace('version_match.php', '', ROSTER_SVNREMOTE) . '</i></small>');
// Get all the gathered information and display it in a table
foreach( $directories as $directory => $filecount )
{
if( isset($files[$directory]) )
{
// echo $directory . ', '.$files[$directory]['tooltip'] . '<br />';
$dirtooltip = str_replace("'", "\\'", $files[$directory]['tooltip']);
$dirtooltip = str_replace('"', '"', $dirtooltip);
$directory_id = str_replace(array('.', '/', '\\'), '', $directory);
$dirshow = substr_replace($directory, ROSTER_PATH, 0, 1);
$headertext_max = '<div style="cursor:pointer;width:800px;text-align:left;" onclick="swapShow(\'' . $directory_id . 'TableShow\',\'' . $directory_id . 'TableHide\')" '
. 'onmouseover="overlib(\'' . $dirtooltip . '\',CAPTION,\'' . $directory . '/ - ' . $severity[$files[$directory]['rollup']]['severityname'] . '\',WRAP);" onmouseout="return nd();">'
. '<div style="float:right;"><span style="color:' . $severity[$files[$directory]['rollup']]['color'] . ';">' . $severity[$files[$directory]['rollup']]['severityname'] . '</span> <img class="middle" src="' . $roster->config['theme_path'] . '/images/plus.gif" alt="+" /></div>' . $dirshow . '/</div>';
$headertext_min = '<div style="cursor:pointer;width:800px;text-align:left;" onclick="swapShow(\'' . $directory_id . 'TableShow\',\'' . $directory_id . 'TableHide\')" '
. 'onmouseover="overlib(\'' . $dirtooltip . '\',CAPTION,\'' . $directory . '/ - ' . $severity[$files[$directory]['rollup']]['severityname'] . '\',WRAP);" onmouseout="return nd();">'
. '<div style="float:right;"><span style="color:' . $severity[$files[$directory]['rollup']]['color'] . ';">' . $severity[$files[$directory]['rollup']]['severityname'] . '</span> <img class="middle" src="' . $roster->config['theme_path'] . '/images/minus.gif" alt="-" /></div>' . $dirshow . '/</div>';
echo '<div style="display:none;" id="' . $directory_id . 'TableShow">';
echo border($severity[$files[$directory]['rollup']]['style'], 'start', $headertext_min);
echo '<table width="100%" cellpadding="0" cellspacing="0">';
echo '<tr><th class="membersHeader">Filename</th><th class="membersHeader">Revision</th><th class="membersHeader">Date</th><th class="membersHeader">Author</th><th class="membersHeader">MD5 Match</th><th class="membersHeaderRight">SVN</th>';
echo '</tr>';
$row = 0;
foreach( $files[$directory] as $file => $filedata )
{
if( $row == 1 )
{
$row = 2;
}
else
{
$row = 1;
}
if( isset($filedata['tooltip']) )
{
$filetooltip = str_replace("'", "\\'", $filedata['tooltip']);
$filetooltip = str_replace('"', '"', $filetooltip);
}
else
{
$filetooltip = 'Unknown';
}
if( isset($file) && $file != 'newer' && $file != 'severity' && $file != 'tooltip' && $file != 'rollup' && $file != 'rev' && $file != 'date' && $file != 'author' && $file != 'md5' && $file != 'update' && $file != 'diff' && $file != 'missing' )
{
echo '<tr style="cursor:help;" onmouseover="overlib(\'<span style="color:blue;">' . $filetooltip . '</span>\',CAPTION,\'' . $file . '/ - ' . $severity[$filedata['rollup']]['severityname'] . '\',WRAP);" onmouseout="return nd();">';
echo '<td class="membersRow' . $row . '"><span style="color:' . $severity[$filedata['rollup']]['color'] . '">' . $file . '</span></td>';
echo '<td class="membersRow' . $row . '">' . "\n";
if( isset($filedata['rev']) )
{
echo $filedata['rev'];
}
else
{
echo 'Unknown Rev';
}
echo "</td>\n";
echo '<td class="membersRow' . $row . '">';
if( isset($filedata['date']) )
{
echo $filedata['date'];
}
else
{
echo 'Unknown Date';
}
echo "</td>\n";
echo '<td class="membersRow' . $row . '">';
if( isset($filedata['author']) )
{
echo $filedata['author'];
}
else
{
echo 'Unknown Author';
}
echo "</td>\n";
echo '<td class="membersRow' . $row . '">';
if( isset($filedata['md5']) )
{
echo $filedata['md5'];
}
else
{
echo 'Unknown';
}
echo "</td>\n";
echo '<td class="membersRowRight' . $row . '">' . "\n";
if( $filedata['diff'] || $filedata['missing'] )
{
echo '<form method="post" action="' . makelink('rosterdiag') . '">' . "\n";
echo '<input type="hidden" name="filename" value="' . $directory . '/' . $file . "\">\n";
echo '<input type="hidden" name="downloadsvn" value="confirmation">' . "\n";
if( isset($filedata['diff']) && $filedata['diff'] )
{
echo '<input type="hidden" name="downmode" value="update">' . "\n";
echo '<input type="submit" value="Diff Check">' . "\n";
}
elseif( isset($filedata['missing']) && $filedata['missing'] )
{
echo '<input type="hidden" name="downmode" value="install">' . "\n";
echo '<input type="submit" value="Show File">' . "\n";
}
echo '</form>';
}
else
{
echo ' ';
}
echo "</td>\n";
echo "</tr>\n";
}
}
echo '</table>';
echo border($severity[$files[$directory]['rollup']]['style'], 'end') . '</div>';
echo '<div id="' . $directory_id . 'TableHide">';
echo border($severity[$files[$directory]['rollup']]['style'], 'start', $headertext_max);
echo border($severity[$files[$directory]['rollup']]['style'], 'end') . '</div>';
}
}
echo border('sgray', 'end');
echo '<!-- Begin Roster Footer -->';
echo '</div>';
echo '</body>';
echo '</html>';
exit();
}
elseif( isset($_POST['filestoget']) && isset($_POST['ziptype']) )
{
$filesarray = explode(';', $_POST['filestoget']);
$ziptype = $_POST['ziptype']; // .tar.gz or .zip
$errors = '';
if( $ziptype == 'targz' )
{
$downloadpackage = new gzip_file('WoWRoster_UpdatePackage_' . date('Ymd_Hi') . '.tar.gz');
}
else
{
$downloadpackage = new zip_file('WoWRoster_UpdatePackage_' . date('Ymd_Hi') . '.zip');
}
$downloadpackage->set_options(array(
'inmemory' => 1,
'recurse' => 0,
'storepaths' => 1
));
foreach( $filesarray as $file )
{
$getfile = $file;
$pathparts_getfile = pathinfo($getfile);
$realpath_getfile = realpath($pathparts_getfile['dirname']);
$realpathparts_getfile = pathinfo($realpath_getfile);
$thisfile = $_SERVER['SCRIPT_FILENAME'];
$realpath_thisfile = realpath($thisfile);
$pathparts_thisfile = pathinfo($thisfile);
$realpathparts_thisfile = pathinfo($realpath_thisfile);
// echo $file . ', ' . $thisfile . ', ' . $realpath_thisfile . ', ' . $realpathparts_getfile['dirname'] . ', ' . $realpathparts_thisfile . '<br />';
if( substr($getfile, 0, 1) == '.' )
{
$subpath_getfile = substr($pathparts_getfile['dirname'], 2);
}
else
{
$subpath_getfile = $pathparts_getfile['dirname'];
}
if( !checkfile($pathparts_getfile, $realpath_getfile, $realpathparts_thisfile['dirname'] . '/' . $subpath_getfile) )
{
$errors .= '[ERROR] INVALID FILE: ' . $getfile . ', Operation NOT Allowed!!!<br />';
}
else
{
// Add file to Archive
// echo $getfile . '<br />';
$downloadpackage->add_files($getfile);
}
}
if( $errors == '' )
{
$downloadpackage->create_archive();
// Send archive to user for download
if( count($downloadpackage->error) == 0 )
{
$downloadpackage->download_file();
}
else
{
foreach( $downloadpackage->error as $error )
{
echo $error . '<br />';
}
}
/**
foreach( $filesarray as $file )
{
echo $file . '<br />';
}
echo 'DOWNLOAD STARTING of the following files';
*/
}
else
{
echo $errors;
}
}
else
{
foreach( $files as $directory => $filedata )
{
foreach( $filedata as $filename => $file )
{
print($directory . $explode . $filename . $explode . $file['local']['versionDesc'] . $explode . $file['local']['versionRev'] . $explode . $file['local']['versionDate'] . $explode . $file['local']['versionAuthor'] . $explode . $file['local']['versionMD5'] . $break);
}
}
}
// Check the file requested function
function checkfile( )
{
global $extensions, $pathparts_getfile, $realpath_getfile, $realpath_thisfile, $subpath_getfile;
$returnvalue = 0;
$unwanted = 0;
if( !strcmp($realpath_getfile, $realpath_thisfile . '/' . $subpath_getfile) )
{
$unwanted = 1;
}
if( !strcmp('version_match.php', $pathparts_getfile['basename']) )
{
$unwanted = 1;
}
if( !strcmp('conf.php', $pathparts_getfile['basename']) )
{
$unwanted = 1;
}
if( $unwanted )
{
$returnvalue = 0;
}
else
{
foreach( $extensions as $wantedextension )
{
if( !strcmp($wantedextension, $pathparts_getfile['extension']) )
{
$returnvalue = 1;
}
}
}
return $returnvalue;
}
/**
* TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES Examples
*
* Examples of Compression:
*
* The following example creates a gzipped tar file:
*
* Assume the following script is executing in /var/www/htdocs/test
* Create a new gzip file test.tgz in htdocs/test
* $test = new gzip_file("htdocs/test/test.tgz");
*
* Set basedir to "../..", which translates to /var/www
* Overwrite /var/www/htdocs/test/test.tgz if it already exists
* Set compression level to 1 (lowest)
* $test->set_options(array('basedir' => "../..", 'overwrite' => 1, 'level' => 1));
*
* Add entire htdocs directory and all subdirectories
* Add all php files in htsdocs and its subdirectories
* *.php"));
*
* Exclude all jpg files in htdocs and its subdirectories
* *.jpg");
*
* Create /var/www/htdocs/test/test.tgz
* $test->create_archive();
* Check for errors (you can check for errors at any point)
* if (count($test->errors) > 0)
* print ("Errors occurred."); // Process errors here
*
* The following example creates a zip file:
* Create new zip file in the directory below the current one
* $test = new zip_file("../example.zip");
*
* All files added will be relative to the directory in which the script is executing since no basedir is set.
* Create archive in memory
* Do not recurse through subdirectories
* Do not store file paths in archive
* $test->set_options(array('inmemory' => 1, 'recurse' => 0, 'storepaths' => 0));
*
* Add lib/archive.php to archive
* $test->add_files("src/archive.php");
*
* Add all jpegs and gifs in the images directory to archive
* *.jp*g", "images/*.gif"));
*
* Store all exe files in bin without compression
* *.exe");
*
* Create archive in memory
* $test->create_archive();
*
* Send archive to user for download
* $test->download_file();
*
*
* Examples of Decompression:
*
* The following example extracts a bzipped tar file:
* Open test.tbz2
* $test = new bzip_file("test.tbz2");
*
* Overwrite existing files
* $test->set_options(array('overwrite' => 1));
*
* Extract contents of archive to disk
* $test->extract_files();
*
* The following example extracts a tar file:
* Open archives/test.tar
* $test = new tar_file("archives/test.tar");
*
* Extract in memory
* $test->set_options(array('inmemory' => 0));
*
* Extract archive to memory
* $test->extract_files();
*
* Write out the name and size of each file extracted
* foreach ($test->files as $file)
* print ("File " + $file['name'] + " is " + $file['stat'][7] + " bytes\n");
*/
/*--------------------------------------------------
| TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1
| By Devin Doucette
| Copyright (c) 2005 Devin Doucette
| Email: darksnoopy@shaw.ca
+--------------------------------------------------
| Email bugs/suggestions to darksnoopy@shaw.ca
+--------------------------------------------------
| This script has been created and released under
| the GNU GPL and is free to use and redistribute
| only if this copyright statement is not removed
+--------------------------------------------------*/
class archive
{
var $options;
var $files;
var $exclude;
var $storeonly;
var $error;
function archive( $name )
{
$this->options = array(
'basedir' => '.',
'name' => $name,
'prepend' => '',
'inmemory' => 0,
'overwrite' => 0,
'recurse' => 1,
'storepaths' => 1,
'followlinks' => 0,
'level' => 3,
'method' => 1,
'sfx' => '',
'type' => '',
'comment' => ''
);
$this->files = array();
$this->exclude = array();
$this->storeonly = array();
$this->error = array();
}
function set_options( $options )
{
foreach( $options as $key => $value )
{
$this->options[$key] = $value;
}
if( !empty($this->options['basedir']) )
{
$this->options['basedir'] = str_replace("\\", "/", $this->options['basedir']);
$this->options['basedir'] = preg_replace("/\/+/", "/", $this->options['basedir']);
$this->options['basedir'] = preg_replace("/\/$/", "", $this->options['basedir']);
}
if( !empty($this->options['name']) )
{
$this->options['name'] = str_replace("\\", "/", $this->options['name']);
$this->options['name'] = preg_replace("/\/+/", "/", $this->options['name']);
}
if( !empty($this->options['prepend']) )
{
$this->options['prepend'] = str_replace("\\", "/", $this->options['prepend']);
$this->options['prepend'] = preg_replace("/^(\.*\/+)+/", "", $this->options['prepend']);
$this->options['prepend'] = preg_replace("/\/+/", "/", $this->options['prepend']);
$this->options['prepend'] = preg_replace("/\/$/", "", $this->options['prepend']) . "/";
}
}
function create_archive( )
{
$this->make_list();
if( $this->options['inmemory'] == 0 )
{
$pwd = getcwd();
chdir($this->options['basedir']);
if( $this->options['overwrite'] == 0 && file_exists($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : "")) )
{
$this->error[] = "File {$this->options['name']} already exists.";
chdir($pwd);
return 0;
}
elseif( $this->archive = @fopen($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""), "wb+") )
{
chdir($pwd);
}
else
{
$this->error[] = "Could not open {$this->options['name']} for writing.";
chdir($pwd);
return 0;
}
}
else
{
$this->archive = "";
}
switch( $this->options['type'] )
{
case "zip":
if( !$this->create_zip() )
{
$this->error[] = "Could not create zip file.";
return 0;
}
break;
case "bzip":
if( !$this->create_tar() )
{
$this->error[] = "Could not create tar file.";
return 0;
}
if( !$this->create_bzip() )
{
$this->error[] = "Could not create bzip2 file.";
return 0;
}
break;
case "gzip":
if( !$this->create_tar() )
{
$this->error[] = "Could not create tar file.";
return 0;
}
if( !$this->create_gzip() )
{
$this->error[] = "Could not create gzip file.";
return 0;
}
break;
case "tar":
if( !$this->create_tar() )
{
$this->error[] = "Could not create tar file.";
return 0;
}
}
if( $this->options['inmemory'] == 0 )
{
fclose($this->archive);
if( $this->options['type'] == "gzip" || $this->options['type'] == "bzip" )
{
unlink($this->options['basedir'] . "/" . $this->options['name'] . ".tmp");
}
}
}
function add_data( $data )
{
if( $this->options['inmemory'] == 0 )
{
fwrite($this->archive, $data);
}
else
{
$this->archive .= $data;
}
}
function make_list( )
{
if( !empty($this->exclude) )
{
foreach( $this->files as $key => $value )
{
foreach( $this->exclude as $current )
{
if( $value['name'] == $current['name'] )
{
unset($this->files[$key]);
}
}
}
}
if( !empty($this->storeonly) )
{
foreach( $this->files as $key => $value )
{
foreach( $this->storeonly as $current )
{
if( $value['name'] == $current['name'] )
{
$this->files[$key]['method'] = 0;
}
}
}
}
unset($this->exclude, $this->storeonly);
}
function add_files( $list )
{
$temp = $this->list_files($list);
foreach( $temp as $current )
{
$this->files[] = $current;
}
}
function exclude_files( $list )
{
$temp = $this->list_files($list);
foreach( $temp as $current )
{
$this->exclude[] = $current;
}
}
function store_files( $list )
{
$temp = $this->list_files($list);
foreach( $temp as $current )
{
$this->storeonly[] = $current;
}
}
function list_files( $list )
{
if( !is_array($list) )
{
$temp = $list;
$list = array($temp);
unset($temp);
}
$files = array();
$pwd = getcwd();
chdir($this->options['basedir']);
foreach( $list as $current )
{
$current = str_replace("\\", "/", $current);
$current = preg_replace("/\/+/", "/", $current);
$current = preg_replace("/\/$/", "", $current);
if( strstr($current, "*") )
{
$regex = preg_replace("/([\\\^\$\.\[\]\|\(\)\?\+\{\}\/])/", "\\\\\\1", $current);
$regex = str_replace("*", ".*", $regex);
$dir = strstr($current, "/") ? substr($current, 0, strrpos($current, "/")) : ".";
$temp = $this->parse_dir($dir);
foreach( $temp as $current2 )
{
if( preg_match("/^{$regex}$/i", $current2['name']) )
{
$files[] = $current2;
}
}
unset($regex, $dir, $temp, $current);
}
elseif( @is_dir($current) )
{
$temp = $this->parse_dir($current);
foreach( $temp as $file )
{
$files[] = $file;
}
unset($temp, $file);
}
elseif( @file_exists($current) )
{
$files[] = array(
'name' => $current,
'name2' => $this->options['prepend'] . preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($current, "/")) ? substr($current, strrpos($current, "/") + 1) : $current),
'type' => @is_link($current) && $this->options['followlinks'] == 0 ? 2 : 0,
'ext' => substr($current, strrpos($current, ".")),
'stat' => stat($current)
);
}
}
chdir($pwd);
unset($current, $pwd);
usort($files, array("archive", "sort_files"));
return $files;
}
function parse_dir( $dirname )
{
if( $this->options['storepaths'] == 1 && !preg_match("/^(\.+\/*)+$/", $dirname) )
{
$files = array(
array(
'name' => $dirname,
'name2' => $this->options['prepend'] . preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($dirname, "/")) ? substr($dirname, strrpos($dirname, "/") + 1) : $dirname),
'type' => 5,
'stat' => stat($dirname)
)
);
}
else
{
$files = array();
}
$dir = @opendir($dirname);
while( $file = @readdir($dir) )
{
$fullname = $dirname . "/" . $file;
if( $file == "." || $file == ".." )
{
continue;
}
elseif( @is_dir($fullname) )
{
if( empty($this->options['recurse']) )
{
continue;
}
$temp = $this->parse_dir($fullname);
foreach( $temp as $file2 )
{
$files[] = $file2;
}
}
elseif( @file_exists($fullname) )
{
$files[] = array(
'name' => $fullname,
'name2' => $this->options['prepend'] . preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($fullname, "/")) ? substr($fullname, strrpos($fullname, "/") + 1) : $fullname),
'type' => @is_link($fullname) && $this->options['followlinks'] == 0 ? 2 : 0,
'ext' => substr($file, strrpos($file, ".")),
'stat' => stat($fullname)
);
}
}
@closedir($dir);
return $files;
}
function sort_files( $a , $b )
{
if( $a['type'] != $b['type'] )
{
if( $a['type'] == 5 || $b['type'] == 2 )
{
return -1;
}
elseif( $a['type'] == 2 || $b['type'] == 5 )
{
return 1;
}
}
elseif( $a['type'] == 5 )
{
return strcmp(strtolower($a['name']), strtolower($b['name']));
}
elseif( $a['ext'] != $b['ext'] )
{
return strcmp($a['ext'], $b['ext']);
}
elseif( $a['stat'][7] != $b['stat'][7] )
{
return $a['stat'][7] > $b['stat'][7] ? -1 : 1;
}
else
{
return strcmp(strtolower($a['name']), strtolower($b['name']));
}
return 0;
}
function download_file( )
{
if( $this->options['inmemory'] == 0 )
{
$this->error[] = "Can only use download_file() if archive is in memory. Redirect to file otherwise, it is faster.";
return;
}
switch( $this->options['type'] )
{
case "zip":
header("Content-Type: application/zip");
break;
case "bzip":
header("Content-Type: application/x-bzip2");
break;
case "gzip":
header("Content-Type: application/x-gzip");
break;
case "tar":
header("Content-Type: application/x-tar");
}
$header = "Content-Disposition: attachment; filename=\"";
$header .= strstr($this->options['name'], "/") ? substr($this->options['name'], strrpos($this->options['name'], "/") + 1) : $this->options['name'];
$header .= "\"";
header($header);
header("Content-Length: " . strlen($this->archive));
header("Content-Transfer-Encoding: binary");
header("Cache-Control: no-cache, must-revalidate, max-age=60");
header("Expires: Sat, 01 Jan 2000 12:00:00 GMT");
print($this->archive);
}
}
class tar_file extends archive
{
function tar_file( $name )
{
$this->archive($name);
$this->options['type'] = "tar";
}