-
Notifications
You must be signed in to change notification settings - Fork 4
/
tbszip.php
1052 lines (919 loc) · 31.8 KB
/
tbszip.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
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
/*
TbsZip version 2.17
Date : 2023-09-17
Author : Skrol29 (email: http://www.tinybutstrong.com/onlyyou.html)
Licence : LGPL
This class is independent from any other classes and has been originally created for the OpenTbs plug-in
for TinyButStrong Template Engine (TBS). OpenTbs makes TBS able to merge OpenOffice and Ms Office documents.
Visit http://www.tinybutstrong.com
*/
define('TBSZIP_DOWNLOAD',1); // download (default)
define('TBSZIP_NOHEADER',4); // option to use with DOWNLOAD: no header is sent
define('TBSZIP_FILE',8); // output to file , or add from file
define('TBSZIP_STRING',32); // output to string, or add from string
class clsTbsZip {
public $Meth8Ok;
public $DisplayError;
public $ArchFile;
public $Error;
// Compatibility PHP 8.2
public $ArchHnd;
public $ArchIsNew;
public $CdEndPos;
public $CdPos;
public $CdInfo;
public $ArchIsStream;
public $CdFileLst;
public $CdFileNbr;
public $CdFileByName;
public $VisFileLst;
public $LastReadComp;
public $LastReadIdx;
public $ReplInfo;
public $ReplByPos;
public $AddInfo;
public $OutputMode;
public $OutputHandle;
public $OutputSrc;
function __construct() {
$this->Meth8Ok = extension_loaded('zlib'); // check if Zlib extension is available. This is need for compress and uncompress with method 8.
$this->DisplayError = true;
$this->ArchFile = '';
$this->Error = false;
}
/**
* Create a new virtual empty archive, the name will be the default name when the archive is flushed.
*/
function CreateNew($ArchName='new.zip') {
if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility
$this->Close(); // note that $this->ArchHnd is set to false here
$this->Error = false;
$this->ArchFile = $ArchName;
$this->ArchIsNew = true;
$bin = 'PK'.chr(05).chr(06).str_repeat(chr(0), 18);
$this->CdEndPos = strlen($bin) - 4;
$this->CdInfo = array('disk_num_curr'=>0, 'disk_num_cd'=>0, 'file_nbr_curr'=>0, 'file_nbr_tot'=>0, 'l_cd'=>0, 'p_cd'=>0, 'l_comm'=>0, 'v_comm'=>'', 'bin'=>$bin);
$this->CdPos = $this->CdInfo['p_cd'];
}
/**
* Open the zip archive
*/
function Open($ArchFile, $UseIncludePath=false) {
if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility
$this->Close(); // close handle and init info
$this->Error = false;
$this->ArchIsNew = false;
$this->ArchIsStream = (is_resource($ArchFile) && (get_resource_type($ArchFile)=='stream'));
if ($this->ArchIsStream) {
$info = stream_get_meta_data($ArchFile);
if (isset($info['uri'])) {
$this->ArchFile = $info['uri'];
} else {
$this->ArchFile = 'from_stream.zip';
}
$this->ArchHnd = $ArchFile;
} else {
// open the file
$this->ArchFile = $ArchFile;
$this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath);
}
$ok = !($this->ArchHnd===false);
if ($ok) $ok = $this->CentralDirRead();
return $ok;
}
function Close() {
if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) fclose($this->ArchHnd);
$this->ArchFile = '';
$this->ArchHnd = false;
$this->CdInfo = array();
$this->CdFileLst = array();
$this->CdFileNbr = 0;
$this->CdFileByName = array();
$this->VisFileLst = array();
$this->ArchCancelModif();
}
function ArchCancelModif() {
$this->LastReadComp = false; // compression of the last read file (1=compressed, 0=stored not compressed, -1= stored compressed but read uncompressed)
$this->LastReadIdx = false; // index of the last file read
$this->ReplInfo = array();
$this->ReplByPos = array();
$this->AddInfo = array();
}
function FileAdd($Name, $Data, $DataType=TBSZIP_STRING, $Compress=true) {
if ($Data===false) return $this->FileCancelModif($Name, false); // Cancel a previously added file
// Save information for adding a new file into the archive
$Diff = 30 + 46 + 2*strlen($Name); // size of the header + cd info
$Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $Name);
if ($Ref===false) return false;
$Ref['name'] = $Name;
$this->AddInfo[] = $Ref;
return $Ref['res'];
}
function CentralDirRead() {
$cd_info = 'PK'.chr(05).chr(06); // signature of the Central Directory
$cd_pos = -22;
$this->_MoveTo($cd_pos, SEEK_END);
$b = $this->_ReadData(4);
if ($b===$cd_info) {
$this->CdEndPos = ftell($this->ArchHnd) - 4;
} else {
$p = $this->_FindCDEnd($cd_info);
//echo 'p='.var_export($p,true); exit;
if ($p===false) {
return $this->RaiseError('The End of Central Directory Record is not found.');
} else {
$this->CdEndPos = $p;
$this->_MoveTo($p+4);
}
}
$this->CdInfo = $this->CentralDirRead_End($cd_info);
$this->CdFileLst = array();
$this->CdFileNbr = $this->CdInfo['file_nbr_curr'];
$this->CdPos = $this->CdInfo['p_cd'];
if ($this->CdFileNbr<=0) return $this->RaiseError('No header found in the Central Directory.');
if ($this->CdPos<=0) return $this->RaiseError('No position found for the Central Directory.');
$this->_MoveTo($this->CdPos);
for ($i=0;$i<$this->CdFileNbr;$i++) {
$x = $this->CentralDirRead_File($i);
if ($x!==false) {
$this->CdFileLst[$i] = $x;
$this->CdFileByName[$x['v_name']] = $i;
}
}
return true;
}
function CentralDirRead_End($cd_info) {
$b = $cd_info.$this->_ReadData(18);
$x = array();
$x['disk_num_curr'] = $this->_GetDec($b,4,2); // number of this disk
$x['disk_num_cd'] = $this->_GetDec($b,6,2); // number of the disk with the start of the central directory
$x['file_nbr_curr'] = $this->_GetDec($b,8,2); // total number of entries in the central directory on this disk
$x['file_nbr_tot'] = $this->_GetDec($b,10,2); // total number of entries in the central directory
$x['l_cd'] = $this->_GetDec($b,12,4); // size of the central directory
$x['p_cd'] = $this->_GetDec($b,16,4); // position of start of central directory with respect to the starting disk number
$x['l_comm'] = $this->_GetDec($b,20,2); // .ZIP file comment length
$x['v_comm'] = $this->_ReadData($x['l_comm']); // .ZIP file comment
$x['bin'] = $b.$x['v_comm'];
return $x;
}
function CentralDirRead_File($idx) {
$b = $this->_ReadData(46);
$x = $this->_GetHex($b,0,4);
if ($x!=='h:02014b50') return $this->RaiseError("Signature of Central Directory Header #".$idx." (file information) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd) - 46).".");
$x = array();
$x['vers_used'] = $this->_GetDec($b,4,2);
$x['vers_necess'] = $this->_GetDec($b,6,2);
$x['purp'] = $this->_GetBin($b,8,2);
$x['meth'] = $this->_GetDec($b,10,2);
$x['time'] = $this->_GetDec($b,12,2);
$x['date'] = $this->_GetDec($b,14,2);
$x['crc32'] = $this->_GetDec($b,16,4);
$x['l_data_c'] = $this->_GetDec($b,20,4);
$x['l_data_u'] = $this->_GetDec($b,24,4);
$x['l_name'] = $this->_GetDec($b,28,2);
$x['l_fields'] = $this->_GetDec($b,30,2);
$x['l_comm'] = $this->_GetDec($b,32,2);
$x['disk_num'] = $this->_GetDec($b,34,2);
$x['int_file_att'] = $this->_GetDec($b,36,2);
$x['ext_file_att'] = $this->_GetDec($b,38,4);
$x['p_loc'] = $this->_GetDec($b,42,4);
$x['v_name'] = $this->_ReadData($x['l_name']);
$x['v_fields'] = $this->_ReadData($x['l_fields']);
$x['v_comm'] = $this->_ReadData($x['l_comm']);
$x['bin'] = $b.$x['v_name'].$x['v_fields'].$x['v_comm'];
return $x;
}
function RaiseError($Msg) {
if ($this->DisplayError) {
if (PHP_SAPI==='cli') {
echo get_class($this).' ERROR with the zip archive: '.$Msg."\r\n";
} else {
echo '<strong>'.get_class($this).' ERROR with the zip archive:</strong> '.$Msg.'<br>'."\r\n";
}
}
$this->Error = $Msg;
return false;
}
function Debug($FileHeaders=false) {
$this->DisplayError = true;
if ($FileHeaders) {
// Calculations first in order to have error messages before other information
$idx = 0;
$pos = 0;
$pos_stop = $this->CdInfo['p_cd'];
$this->_MoveTo($pos);
while ( ($pos<$pos_stop) && ($ok = $this->_ReadFile($idx,false)) ) {
$this->VisFileLst[$idx]['p_this_header (debug_mode only)'] = $pos;
$pos = ftell($this->ArchHnd);
$idx++;
}
}
$nl = "\r\n";
echo "<pre>";
echo "-------------------------------".$nl;
echo "End of Central Directory record".$nl;
echo "-------------------------------".$nl;
print_r($this->DebugArray($this->CdInfo));
echo $nl;
echo "-------------------------".$nl;
echo "Central Directory headers".$nl;
echo "-------------------------".$nl;
print_r($this->DebugArray($this->CdFileLst));
if ($FileHeaders) {
echo $nl;
echo "------------------".$nl;
echo "Local File headers".$nl;
echo "------------------".$nl;
print_r($this->DebugArray($this->VisFileLst));
}
echo "</pre>";
}
function DebugArray($arr) {
foreach ($arr as $k=>$v) {
if (is_array($v)) {
$arr[$k] = $this->DebugArray($v);
} elseif (substr($k,0,2)=='p_') {
$arr[$k] = $this->_TxtPos($v);
}
}
return $arr;
}
function FileExists($NameOrIdx) {
return ($this->FileGetIdx($NameOrIdx)!==false);
}
/**
* Check if a file name, or a file index exists in the Central Directory, and return its index
*/
function FileGetIdx($NameOrIdx) {
if (is_string($NameOrIdx)) {
if (isset($this->CdFileByName[$NameOrIdx])) {
return $this->CdFileByName[$NameOrIdx];
} else {
return false;
}
} else {
if (isset($this->CdFileLst[$NameOrIdx])) {
return $NameOrIdx;
} else {
return false;
}
}
}
/**
* Check if a file name exists in the list of file to add, and return its index
*/
function FileGetIdxAdd($Name) {
if (!is_string($Name)) return false;
$idx_lst = array_keys($this->AddInfo);
foreach ($idx_lst as $idx) {
if ($this->AddInfo[$idx]['name']===$Name) return $idx;
}
return false;
}
function FileRead($NameOrIdx, $Uncompress=true) {
$this->LastReadComp = false; // means the file is not found
$this->LastReadIdx = false;
$idx = $this->FileGetIdx($NameOrIdx);
if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.');
$pos = $this->CdFileLst[$idx]['p_loc'];
$this->_MoveTo($pos);
$this->LastReadIdx = $idx; // Can be usefull to get the idx
$Data = $this->_ReadFile($idx, true);
// Manage uncompression
$Comp = 1; // means the contents stays compressed
$meth = $this->CdFileLst[$idx]['meth'];
if ($meth==8) {
if ($Uncompress) {
if ($this->Meth8Ok) {
$Data = gzinflate($Data);
$Comp = -1; // means uncompressed
} else {
$this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because extension Zlib is not installed.');
}
}
} elseif($meth==0) {
$Comp = 0; // means stored without compression
} else {
if ($Uncompress) $this->RaiseError('Unable to uncompress file "'.$NameOrIdx.'" because it is compressed with method '.$meth.'.');
}
$this->LastReadComp = $Comp;
return $Data;
}
/**
* Read the file header (and maybe the data ) in the archive, assuming the cursor in at a new file position
*/
function _ReadFile($idx, $ReadData) {
$b = $this->_ReadData(30);
$x = $this->_GetHex($b,0,4);
if ($x!=='h:04034b50') return $this->RaiseError("Signature of Local File Header #".$idx." (data section) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd)-30).".");
$x = array();
$x['vers'] = $this->_GetDec($b,4,2);
$x['purp'] = $this->_GetBin($b,6,2);
$x['meth'] = $this->_GetDec($b,8,2);
$x['time'] = $this->_GetDec($b,10,2);
$x['date'] = $this->_GetDec($b,12,2);
$x['crc32'] = $this->_GetDec($b,14,4);
$x['l_data_c'] = $this->_GetDec($b,18,4);
$x['l_data_u'] = $this->_GetDec($b,22,4);
$x['l_name'] = $this->_GetDec($b,26,2);
$x['l_fields'] = $this->_GetDec($b,28,2);
$x['v_name'] = $this->_ReadData($x['l_name']);
$x['v_fields'] = $this->_ReadData($x['l_fields']);
$x['bin'] = $b.$x['v_name'].$x['v_fields'];
// Read Data
if (isset($this->CdFileLst[$idx])) {
$len_cd = $this->CdFileLst[$idx]['l_data_c'];
if ($x['l_data_c']==0) {
// Sometimes, the size is not specified in the local information.
$len = $len_cd;
} else {
$len = $x['l_data_c'];
if ($len!=$len_cd) {
//echo "TbsZip Warning: Local information for file #".$idx." says len=".$len.", while Central Directory says len=".$len_cd.".";
}
}
} else {
$len = $x['l_data_c'];
if ($len==0) $this->RaiseError("File Data #".$idx." cannt be read because no length is specified in the Local File Header and its Central Directory information has not been found.");
}
if ($ReadData) {
$Data = $this->_ReadData($len);
} else {
$this->_MoveTo($len, SEEK_CUR);
}
// Description information
$desc_ok = ($x['purp'][2+3]=='1');
if ($desc_ok) {
$b = $this->_ReadData(12);
$s = $this->_GetHex($b,0,4);
$d = 0;
// the specification says the signature may or may not be present
if ($s=='h:08074b50') {
$b .= $this->_ReadData(4);
$d = 4;
$x['desc_bin'] = $b;
$x['desc_sign'] = $s;
} else {
$x['desc_bin'] = $b;
}
$x['desc_crc32'] = $this->_GetDec($b,0+$d,4);
$x['desc_l_data_c'] = $this->_GetDec($b,4+$d,4);
$x['desc_l_data_u'] = $this->_GetDec($b,8+$d,4);
}
// Save file info without the data
$this->VisFileLst[$idx] = $x;
// Return the info
if ($ReadData) {
return $Data;
} else {
return true;
}
}
/**
* Store replacement information.
*/
function FileReplace($NameOrIdx, $Data, $DataType=TBSZIP_STRING, $Compress=true) {
$idx = $this->FileGetIdx($NameOrIdx);
if ($idx===false) return $this->RaiseError('File "'.$NameOrIdx.'" is not found in the Central Directory.');
$pos = $this->CdFileLst[$idx]['p_loc'];
if ($Data===false) {
// file to delete
$this->ReplInfo[$idx] = false;
$Result = true;
} else {
// file to replace
$Diff = - $this->CdFileLst[$idx]['l_data_c'];
$Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx);
if ($Ref===false) return false;
$this->ReplInfo[$idx] = $Ref;
$Result = $Ref['res'];
}
$this->ReplByPos[$pos] = $idx;
return $Result;
}
/**
* Return the state of the file.
* @return {string} 'u'=unchanged, 'm'=modified, 'd'=deleted, 'a'=added, false=unknown
*/
function FileGetState($NameOrIdx) {
$idx = $this->FileGetIdx($NameOrIdx);
if ($idx===false) {
$idx = $this->FileGetIdxAdd($NameOrIdx);
if ($idx===false) {
return false;
} else {
return 'a';
}
} elseif (isset($this->ReplInfo[$idx])) {
if ($this->ReplInfo[$idx]===false) {
return 'd';
} else {
return 'm';
}
} else {
return 'u';
}
}
/**
* Cancel added, modified or deleted modifications on a file in the archive.
* @return integer The number of cancellations.
*/
function FileCancelModif($NameOrIdx, $ReplacedAndDeleted=true) {
$nbr = 0;
if ($ReplacedAndDeleted) {
// replaced or deleted files
$idx = $this->FileGetIdx($NameOrIdx);
if ($idx!==false) {
if (isset($this->ReplInfo[$idx])) {
$pos = $this->CdFileLst[$idx]['p_loc'];
unset($this->ReplByPos[$pos]);
unset($this->ReplInfo[$idx]);
$nbr++;
}
}
}
// added files
$idx = $this->FileGetIdxAdd($NameOrIdx);
if ($idx!==false) {
unset($this->AddInfo[$idx]);
$nbr++;
}
return $nbr;
}
function Flush($Render=TBSZIP_DOWNLOAD, $File='', $ContentType='') {
if ( ($File!=='') && ($this->ArchFile===$File) && ($Render==TBSZIP_FILE) ) {
$this->RaiseError('Method Flush() cannot overwrite the current opened archive: \''.$File.'\''); // this makes corrupted zip archives without PHP error.
return false;
}
$ArchPos = 0;
$Delta = 0;
$FicNewPos = array();
$DelLst = array(); // idx of deleted files
$DeltaCdLen = 0; // delta of the CD's size
$now = time();
$date = $this->_MsDos_Date($now);
$time = $this->_MsDos_Time($now);
if (!$this->OutputOpen($Render, $File, $ContentType)) return false;
// output modified zipped files and unmodified zipped files that are beetween them
ksort($this->ReplByPos);
foreach ($this->ReplByPos as $ReplPos => $ReplIdx) {
// output data from the zip archive which is before the data to replace
$this->OutputFromArch($ArchPos, $ReplPos);
// get current file information
if (!isset($this->VisFileLst[$ReplIdx])) $this->_ReadFile($ReplIdx, false);
$FileInfo =& $this->VisFileLst[$ReplIdx];
$b1 = $FileInfo['bin'];
if (isset($FileInfo['desc_bin'])) {
$b2 = $FileInfo['desc_bin'];
} else {
$b2 = '';
}
$info_old_len = strlen($b1) + $this->CdFileLst[$ReplIdx]['l_data_c'] + strlen($b2); // $FileInfo['l_data_c'] may have a 0 value in some archives
// get replacement information
$ReplInfo =& $this->ReplInfo[$ReplIdx];
if ($ReplInfo===false) {
// The file is to be deleted
$Delta = $Delta - $info_old_len; // headers and footers are also deleted
$DelLst[$ReplIdx] = true;
} else {
// prepare the header of the current file
$this->_DataPrepare($ReplInfo); // get data from external file if necessary
$this->_PutDec($b1, $time, 10, 2); // time
$this->_PutDec($b1, $date, 12, 2); // date
$this->_PutDec($b1, $ReplInfo['crc32'], 14, 4); // crc32
$this->_PutDec($b1, $ReplInfo['len_c'], 18, 4); // l_data_c
$this->_PutDec($b1, $ReplInfo['len_u'], 22, 4); // l_data_u
if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 8, 2); // meth
// prepare the bottom description if the zipped file, if any
if ($b2!=='') {
$d = (strlen($b2)==16) ? 4 : 0; // offset because of the signature if any
$this->_PutDec($b2, $ReplInfo['crc32'], $d+0, 4); // crc32
$this->_PutDec($b2, $ReplInfo['len_c'], $d+4, 4); // l_data_c
$this->_PutDec($b2, $ReplInfo['len_u'], $d+8, 4); // l_data_u
}
// output data
$this->OutputFromString($b1.$ReplInfo['data'].$b2);
unset($ReplInfo['data']); // save PHP memory
$Delta = $Delta + $ReplInfo['diff'] + $ReplInfo['len_c'];
}
// Update the delta of positions for zipped files which are physically after the currently replaced one
for ($i=0;$i<$this->CdFileNbr;$i++) {
if ($this->CdFileLst[$i]['p_loc']>$ReplPos) {
$FicNewPos[$i] = $this->CdFileLst[$i]['p_loc'] + $Delta;
}
}
// Update the current pos in the archive
$ArchPos = $ReplPos + $info_old_len;
}
// Ouput all the zipped files that remain before the Central Directory listing
if ($this->ArchHnd!==false) $this->OutputFromArch($ArchPos, $this->CdPos); // ArchHnd is false if CreateNew() has been called
$ArchPos = $this->CdPos;
// Output file to add
$AddNbr = count($this->AddInfo);
$AddDataLen = 0; // total len of added data (inlcuding file headers)
if ($AddNbr>0) {
$AddPos = $ArchPos + $Delta; // position of the start
$AddLst = array_keys($this->AddInfo);
foreach ($AddLst as $idx) {
$n = $this->_DataOuputAddedFile($idx, $AddPos);
$AddPos += $n;
$AddDataLen += $n;
}
}
// Modifiy file information in the Central Directory for replaced files
$b2 = '';
$old_cd_len = 0;
for ($i=0;$i<$this->CdFileNbr;$i++) {
$b1 = $this->CdFileLst[$i]['bin'];
$old_cd_len += strlen($b1);
if (!isset($DelLst[$i])) {
if (isset($FicNewPos[$i])) $this->_PutDec($b1, $FicNewPos[$i], 42, 4); // p_loc
if (isset($this->ReplInfo[$i])) {
$ReplInfo =& $this->ReplInfo[$i];
$this->_PutDec($b1, $time, 12, 2); // time
$this->_PutDec($b1, $date, 14, 2); // date
$this->_PutDec($b1, $ReplInfo['crc32'], 16, 4); // crc32
$this->_PutDec($b1, $ReplInfo['len_c'], 20, 4); // l_data_c
$this->_PutDec($b1, $ReplInfo['len_u'], 24, 4); // l_data_u
if ($ReplInfo['meth']!==false) $this->_PutDec($b1, $ReplInfo['meth'], 10, 2); // meth
}
$b2 .= $b1;
}
}
$this->OutputFromString($b2);
$ArchPos += $old_cd_len;
$DeltaCdLen = $DeltaCdLen + strlen($b2) - $old_cd_len;
// Output until "end of central directory record"
if ($this->ArchHnd!==false) $this->OutputFromArch($ArchPos, $this->CdEndPos); // ArchHnd is false if CreateNew() has been called
// Output file information of the Central Directory for added files
if ($AddNbr>0) {
$b2 = '';
foreach ($AddLst as $idx) {
$b2 .= $this->AddInfo[$idx]['bin'];
}
$this->OutputFromString($b2);
$DeltaCdLen += strlen($b2);
}
// Output "end of central directory record"
$b2 = $this->CdInfo['bin'];
$DelNbr = count($DelLst);
if ( ($AddNbr>0) || ($DelNbr>0) ) {
// total number of entries in the central directory on this disk
$n = $this->_GetDec($b2, 8, 2);
$this->_PutDec($b2, $n + $AddNbr - $DelNbr, 8, 2);
// total number of entries in the central directory
$n = $this->_GetDec($b2, 10, 2);
$this->_PutDec($b2, $n + $AddNbr - $DelNbr, 10, 2);
// size of the central directory
$n = $this->_GetDec($b2, 12, 4);
$this->_PutDec($b2, $n + $DeltaCdLen, 12, 4);
$Delta = $Delta + $AddDataLen;
}
$this->_PutDec($b2, $this->CdPos+$Delta , 16, 4); // p_cd (offset of start of central directory with respect to the starting disk number)
$this->OutputFromString($b2);
$this->OutputClose();
return true;
}
// ----------------
// output functions
// ----------------
function OutputOpen($Render, $File, $ContentType) {
if (($Render & TBSZIP_FILE)==TBSZIP_FILE) {
$this->OutputMode = TBSZIP_FILE;
if (''.$File=='') $File = basename($this->ArchFile).'.zip';
$this->OutputHandle = @fopen($File, 'w');
if ($this->OutputHandle===false) {
return $this->RaiseError('Method Flush() cannot overwrite the target file \''.$File.'\'. This may not be a valid file path or the file may be locked by another process or because of a denied permission.');
}
} elseif (($Render & TBSZIP_STRING)==TBSZIP_STRING) {
$this->OutputMode = TBSZIP_STRING;
$this->OutputSrc = '';
} elseif (($Render & TBSZIP_DOWNLOAD)==TBSZIP_DOWNLOAD) {
$this->OutputMode = TBSZIP_DOWNLOAD;
// Output the file
if (''.$File=='') $File = basename($this->ArchFile);
if (($Render & TBSZIP_NOHEADER)==TBSZIP_NOHEADER) {
} else {
header ('Pragma: no-cache');
if ($ContentType!='') header ('Content-Type: '.$ContentType);
header('Content-Disposition: attachment; filename="'.$File.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
$Len = $this->_EstimateNewArchSize();
if ($Len!==false) header('Content-Length: '.$Len);
}
} else {
return $this->RaiseError('Method Flush is called with a unsupported render option.');
}
return true;
}
function OutputFromArch($pos, $pos_stop) {
$len = $pos_stop - $pos;
if ($len<0) return;
$this->_MoveTo($pos);
$block = 1024;
while ($len>0) {
$l = min($len, $block);
$x = $this->_ReadData($l);
$this->OutputFromString($x);
$len = $len - $l;
}
unset($x);
}
function OutputFromString($data) {
if ($this->OutputMode===TBSZIP_DOWNLOAD) {
echo $data; // donwload
} elseif ($this->OutputMode===TBSZIP_STRING) {
$this->OutputSrc .= $data; // to string
} elseif (TBSZIP_FILE) {
fwrite($this->OutputHandle, $data); // to file
}
}
function OutputClose() {
if ( ($this->OutputMode===TBSZIP_FILE) && ($this->OutputHandle!==false) ) {
fclose($this->OutputHandle);
$this->OutputHandle = false;
}
}
// ----------------
// Reading functions
// ----------------
function _MoveTo($pos, $relative = SEEK_SET) {
fseek($this->ArchHnd, $pos, $relative);
}
function _ReadData($len) {
if ($len>0) {
$x = fread($this->ArchHnd, $len);
return $x;
} else {
return '';
}
}
// ----------------
// Take info from binary data
// ----------------
function _GetDec($txt, $pos, $len) {
$x = substr($txt, $pos, $len);
$z = 0;
for ($i=0;$i<$len;$i++) {
$asc = ord($x[$i]);
if ($asc>0) $z = $z + $asc*pow(256,$i);
}
return $z;
}
function _GetHex($txt, $pos, $len) {
$x = substr($txt, $pos, $len);
return 'h:'.bin2hex(strrev($x));
}
function _GetBin($txt, $pos, $len) {
$x = substr($txt, $pos, $len);
$z = '';
for ($i=0;$i<$len;$i++) {
$asc = ord($x[$i]);
if (isset($x[$i])) {
for ($j=0;$j<8;$j++) {
$z .= ($asc & pow(2,$j)) ? '1' : '0';
}
} else {
$z .= '00000000';
}
}
return 'b:'.$z;
}
// ----------------
// Put info into binary data
// ----------------
function _PutDec(&$txt, $val, $pos, $len) {
$x = '';
for ($i=0;$i<$len;$i++) {
if ($val==0) {
$z = 0;
} else {
$z = intval($val % 256);
if (($val<0) && ($z!=0)) { // ($z!=0) is very important, example: val=-420085702
// special opration for negative value. If the number id too big, PHP stores it into a signed integer. For example: crc32('coucou') => -256185401 instead of 4038781895. NegVal = BigVal - (MaxVal+1) = BigVal - 256^4
$val = ($val - $z)/256 -1;
$z = 256 + $z;
} else {
$val = ($val - $z)/256;
}
}
$x .= chr($z);
}
$txt = substr_replace($txt, $x, $pos, $len);
}
function _MsDos_Date($Timestamp = false) {
// convert a date-time timstamp into the MS-Dos format
$d = ($Timestamp===false) ? getdate() : getdate($Timestamp);
return (($d['year']-1980)*512) + ($d['mon']*32) + $d['mday'];
}
function _MsDos_Time($Timestamp = false) {
// convert a date-time timstamp into the MS-Dos format
$d = ($Timestamp===false) ? getdate() : getdate($Timestamp);
return ($d['hours']*2048) + ($d['minutes']*32) + intval($d['seconds']/2); // seconds are rounded to an even number in order to save 1 bit
}
function _MsDos_Debug($date, $time) {
// Display the formated date and time. Just for debug purpose.
// date end time are encoded on 16 bits (2 bytes) : date = yyyyyyymmmmddddd , time = hhhhhnnnnnssssss
$y = ($date & 65024)/512 + 1980;
$m = ($date & 480)/32;
$d = ($date & 31);
$h = ($time & 63488)/2048;
$i = ($time & 1984)/32;
$s = ($time & 31) * 2; // seconds have been rounded to an even number in order to save 1 bit
return $y.'-'.str_pad($m,2,'0',STR_PAD_LEFT).'-'.str_pad($d,2,'0',STR_PAD_LEFT).' '.str_pad($h,2,'0',STR_PAD_LEFT).':'.str_pad($i,2,'0',STR_PAD_LEFT).':'.str_pad($s,2,'0',STR_PAD_LEFT);
}
function _TxtPos($pos) {
// Return the human readable position in both decimal and hexa
return $pos." (h:".dechex($pos).")";
}
/**
* Search the record of end of the Central Directory.
* Return the position of the record in the file.
* Return false if the record is not found. The comment cannot exceed 65335 bytes (=FFFF).
* The method is read backwards a block of 256 bytes and search the key in this block.
*/
function _FindCDEnd($cd_info) {
$nbr = 1;
$p = false;
$pos = ftell($this->ArchHnd) - 4 - 256;
while ( ($p===false) && ($nbr<256) ) {
if ($pos<=0) {
$pos = 0;
$nbr = 256; // in order to make this a last check
}
$this->_MoveTo($pos);
$x = $this->_ReadData(256);
$p = strpos($x, $cd_info);
if ($p===false) {
$nbr++;
$pos = $pos - 256 - 256;
} else {
return $pos + $p;
}
}
return false;
}
function _DataOuputAddedFile($Idx, $PosLoc) {
$Ref =& $this->AddInfo[$Idx];
$this->_DataPrepare($Ref); // get data from external file if necessary
// Other info
$now = time();
$date = $this->_MsDos_Date($now);
$time = $this->_MsDos_Time($now);
$len_n = strlen($Ref['name']);
$purp = 2048 ; // purpose // +8 to indicates that there is an extended local header
// Header for file in the data section
$b = 'PK'.chr(03).chr(04).str_repeat(' ',26); // signature
$this->_PutDec($b,20,4,2); //vers = 20
$this->_PutDec($b,$purp,6,2); // purp
$this->_PutDec($b,$Ref['meth'],8,2); // meth
$this->_PutDec($b,$time,10,2); // time
$this->_PutDec($b,$date,12,2); // date
$this->_PutDec($b,$Ref['crc32'],14,4); // crc32
$this->_PutDec($b,$Ref['len_c'],18,4); // l_data_c
$this->_PutDec($b,$Ref['len_u'],22,4); // l_data_u
$this->_PutDec($b,$len_n,26,2); // l_name
$this->_PutDec($b,0,28,2); // l_fields
$b .= $Ref['name']; // name
$b .= ''; // fields
// Output the data
$this->OutputFromString($b.$Ref['data']);
$OutputLen = strlen($b) + $Ref['len_c']; // new position of the cursor
unset($Ref['data']); // save PHP memory
// Information for file in the Central Directory
$b = 'PK'.chr(01).chr(02).str_repeat(' ',42); // signature
$this->_PutDec($b,20,4,2); // vers_used = 20
$this->_PutDec($b,20,6,2); // vers_necess = 20
$this->_PutDec($b,$purp,8,2); // purp
$this->_PutDec($b,$Ref['meth'],10,2); // meth
$this->_PutDec($b,$time,12,2); // time
$this->_PutDec($b,$date,14,2); // date
$this->_PutDec($b,$Ref['crc32'],16,4); // crc32
$this->_PutDec($b,$Ref['len_c'],20,4); // l_data_c
$this->_PutDec($b,$Ref['len_u'],24,4); // l_data_u
$this->_PutDec($b,$len_n,28,2); // l_name
$this->_PutDec($b,0,30,2); // l_fields
$this->_PutDec($b,0,32,2); // l_comm
$this->_PutDec($b,0,34,2); // disk_num
$this->_PutDec($b,0,36,2); // int_file_att
$this->_PutDec($b,0,38,4); // ext_file_att
$this->_PutDec($b,$PosLoc,42,4); // p_loc
$b .= $Ref['name']; // v_name
$b .= ''; // v_fields
$b .= ''; // v_comm
$Ref['bin'] = $b;
return $OutputLen;
}
function _DataCreateNewRef($Data, $DataType, $Compress, $Diff, $NameOrIdx) {
if (is_array($Compress)) {
$result = 2;
$meth = $Compress['meth'];
$len_u = $Compress['len_u'];
$crc32 = $Compress['crc32'];
$Compress = false;
} elseif ($Compress and ($this->Meth8Ok)) {
$result = 1;
$meth = 8;
$len_u = false; // means unknown
$crc32 = false;
} else {
$result = ($Compress) ? -1 : 0;
$meth = 0;
$len_u = false;
$crc32 = false;
$Compress = false;
}
if ($DataType==TBSZIP_STRING) {
$path = false;
if ($Compress) {
// we compress now in order to save PHP memory
$len_u = strlen($Data);
$crc32 = crc32($Data);
$Data = gzdeflate($Data);
$len_c = strlen($Data);
} else {
$len_c = strlen($Data);
if ($len_u===false) {
$len_u = $len_c;
$crc32 = crc32($Data);
}
}
} else {
$path = $Data;
$Data = false;
if (file_exists($path)) {
$fz = filesize($path);
if ($len_u===false) $len_u = $fz;
$len_c = ($Compress) ? false : $fz;
} else {
return $this->RaiseError("Cannot add the file '".$path."' because it is not found.");
}
}
// at this step $Data and $crc32 can be false only in case of external file, and $len_c is false only in case of external file to compress
return array('data'=>$Data, 'path'=>$path, 'meth'=>$meth, 'len_u'=>$len_u, 'len_c'=>$len_c, 'crc32'=>$crc32, 'diff'=>$Diff, 'res'=>$result);
}
/**
* Returns the real size of data
*/
function _DataPrepare(&$Ref) {
if ($Ref['path']!==false) {
$Ref['data'] = file_get_contents($Ref['path']);
if ($Ref['crc32']===false) $Ref['crc32'] = crc32($Ref['data']);
if ($Ref['len_c']===false) {
// means the data must be compressed
$Ref['data'] = gzdeflate($Ref['data']);
$Ref['len_c'] = strlen($Ref['data']);
}
}
}