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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
|
<?php /** * pluginbuddy_zbzipziparchive Class * * Extends the zip capability core class with proc specific capability * * Version: 1.0.0 * Author: * Author URI: * * @param $parent object Optional parent object which can provide functions for reporting, etc. * @return null * */ if ( !class_exists( "pluginbuddy_zbzipziparchive" ) ) {
/** * pluginbuddy_ZipArchive Class * * Wrapper for ZipArchive to handle error situations and provide additional functions * * @param none * @return null * */ class pluginbuddy_ZipArchive {
/** * The created ZipArchive object if it can be created * * @var $_za object */ private $_za = NULL; /** * __construct() * * Default constructor. * TODO: Consider having a "suppress warnings" parameter to determine whether methods * should be invoked with warnings suppressed or not. For is_available() usage we would * want to so as not to potentially flood the PHP error log. For other functions that * are not called frequently we might not want to suppress the warnings. * * @param none * @return null * */ public function __construct() { if ( class_exists( 'ZipArchive', false ) ) { // It's available so create the private instance $this->_za = new ZipArchive(); } else { // Not available so throw the exception for the caller to handle throw new Exception( 'ZipArchive class does not exist.' ); } return; } /** * __destruct() * * Default destructor. * * @return null * */ public function __destruct() { if ( NULL != $this->_za ) { unset ( $this->_za ); } return; } /** * __call() * * Magic method intercepting calls to unknown methods. This allows us to intercept * all method calls and add additional processing * * @param string $method The name of the intercepted method * @param array $arguments Array of the arguments associated with the method call * @return mixed $result Whatever the invoked wrapper method call returns * */ public function __call( $method, $arguments ) { $result = false; $result = @call_user_func_array( array( $this->_za, $method ), $arguments ); return $result; } /** * __get() * * Magic method intercepting calls to unknown properties. This means we have to * provide the properties of the wrapped object but that's ok as there aren't many. * Note: Maybe we can get the object properties and automate this? * * @param string $name The name of the property * @return mixed $result Whatever the wrapped object property returns * */ public function __get( $name ) { switch ( $name ) { case "comment": $result = $this->_za->comment; break; case "numFiles": $result = $this->_za->numFiles; break; case "filename": $result = $this->_za->filename; break; case "status": $result = $this->_za->status; break; case "statusSys": $result = $this->_za->statusSys; break; default: // Hmm, not quite sure what we should return here... $result = false; } return $result; } /** * errorInfo() * * Translate a ZipArchive error code into an informative string description * and returns the string. An error number can be passed in, otherwise will * get the status property and use that (if not indicating no error) or the * statusSys property and use that (if not indicating no error). In the case * of the statusSys property will get the error string from the getStatusString() * method, otherwise for status property or passed in value use the mappings * below. * Note: This does mean that a statusSys error no should not be passed in * presently. In future may choose to use an offset mapping to handle that. * * @param integer $error Optional: The error code * @param bool $full Optional: True to provide a name/value/description string * @return string Informative error string * */ public function errorInfo( $error = PHP_INT_MAX, $full = true ) { // Get statusSys property value in case we need it $error_sys = $this->_za->statusSys; // Check whether we have been given or need to get a status value if ( PHP_INT_MAX == $error) { // No error number passed in, lets get one $error = $this->_za->status; } if ( ( ZIPARCHIVE::ER_OK == $error ) && ( 0 < $error_sys ) ) { // No basic error AND we have a system error $error_string = "ZLIB" . "(" . $error_sys . ") : " . $this->_za->getStatusString(); } else { $error_name = ''; $error_description = ''; // It's either a basic error OR no system error // We can check the symbolic values switch( (int) $error ) { case ZIPARCHIVE::ER_OK: $error_name = "ZIPARCHIVE::ERR_OK"; $error_description = "No error"; break; case ZIPARCHIVE::ER_OPEN: $error_name = "ZIPARCHIVE::ER_OPEN"; $error_description = "Can't open file"; break; case ZIPARCHIVE::ER_MEMORY: $error_name = "ZIPARCHIVE::ER_MEMORY"; $error_description = "Memory allocation failure"; break; case ZIPARCHIVE::ER_EXISTS: $error_name = "ZIPARCHIVE::ERR_EXISTS"; $error_description = "File already exists"; break; case ZIPARCHIVE::ER_INCONS: $error_name = "ZIPARCHIVE::ER_INCONS"; $error_description = "Zip archive inconsistent"; break; case ZIPARCHIVE::ER_INVAL: $error_name = "ZIPARCHIVE::ER_INVAL"; $error_description = "Invalid argument"; break; case ZIPARCHIVE::ER_NOENT: $error_name = "ZIPARCHIVE::ER_NOENT"; $error_description = "No such file"; break; case ZIPARCHIVE::ER_NOZIP: $error_name = "ZIPARCHIVE::ER_NOZIP"; $error_description = "Not a zip archive"; break; case ZIPARCHIVE::ER_READ: $error_name = "ZIPARCHIVE::ER_READ"; $error_description = "Read error"; break; case ZIPARCHIVE::ER_SEEK: $error_name = "ZIPARCHIVE::ER_SEEK"; $error_description = "Seek error"; break; case ZIPARCHIVE::ER_MULTIDISK: $error_name = "ZIPARCHIVE::ER_MULTIDISK"; $error_description = "Multi-disk zip archives not supported"; break; case ZIPARCHIVE::ER_RENAME: $error_name = "ZIPARCHIVE::ER_RENAME"; $error_description = "Renaming temporary file failed"; break; case ZIPARCHIVE::ER_CLOSE: $error_name = "ZIPARCHIVE::ER_CLOSE"; $error_description = "Closing zip archive failed"; break; case ZIPARCHIVE::ER_WRITE: $error_name = "ZIPARCHIVE::ER_WRITE"; $error_description = "Write error"; break; case ZIPARCHIVE::ER_CRC: $error_name = "ZIPARCHIVE::ER_CRC"; $error_description = "CRC error"; break; case ZIPARCHIVE::ER_ZIPCLOSED: $error_name = "ZIPARCHIVE::ER_ZIPCLOSED"; $error_description = "Containing zip archive was closed"; break; case ZIPARCHIVE::ER_TMPOPEN: $error_name = "ZIPARCHIVE::ER_TMPOPEN"; $error_description = "Failure to create temporary file"; break; case ZIPARCHIVE::ER_ZLIB: $error_name = "ZIPARCHIVE::ER_ZLIB"; $error_description = "Zlib error"; break; case ZIPARCHIVE::ER_CHANGED: $error_name = "ZIPARCHIVE::ER_CHANGED"; $error_description = "Entry has been changed"; break; case ZIPARCHIVE::ER_COMPNOTSUPP: $error_name = "ZIPARCHIVE::ER_COMPNOTSUPP"; $error_description = "Compression method not supported"; break; case ZIPARCHIVE::ER_EOF: $error_name = "ZIPARCHIVE::ER_EOF"; $error_description = "Premature EOF"; break; case ZIPARCHIVE::ER_INTERNAL: $error_name = "ZIPARCHIVE::ER_INTERNAL"; $error_description = "Internal error"; break; case ZIPARCHIVE::ER_REMOVE: $error_name = "ZIPARCHIVE::ER_REMOVE"; $error_description = "Can't remove file"; break; case ZIPARCHIVE::ER_DELETED: $error_name = "ZIPARCHIVE::ER_DELETED"; $error_description = "Entry has been deleted"; break; default: $error_name = "ZIPARCHIVE::ERR_UNKNOWN"; $error_description = "Unkmown error"; } $error_string = $error_name . "(" . $error . ") : " . $error_description; } // One way or another we have a string to return return $error_string; } }
class pluginbuddy_zbzipziparchive extends pluginbuddy_zbzipcore { /** * method tag used to refer to the method and entities associated with it such as class name * * @var $_method_tag string */ public static $_method_tag = 'ziparchive'; /** * This tells us whether this method is regarded as a "compatibility" method * * @var bool */ public static $_is_compatibility_method = false; /** * This tells us the dependencies of this method so they can be check to see if the method can be supported * * @var array */ public static $_method_dependencies = array( 'classes' => array( 'ZipArchive' ), 'functions' => array(), 'extensions' => array(), 'files' => array() ); /** * * get_method_tag_static() * * Get the static method tag in a static context * * @return string The method tag * */ public static function get_method_tag_static() { return self::$_method_tag; }
/** * * get_is_compatibility_method_static() * * Get the compatibility method indicator in a static context * * @return bool True if is a compatibility method * */ public static function get_is_compatibility_method_static() { return self::$_is_compatibility_method; }
/** * * get_method_dependencies_static() * * Get the method dependencies array in a static context * * @return array The dependencies of the method that is requires to be a supported method * */ public static function get_method_dependencies_static() { return self::$_method_dependencies; }
/** * __construct() * * Default constructor. * * @param reference &$parent [optional] Reference to the object containing the status() function for status updates. * @return null * */ public function __construct( &$parent = NULL ) {
parent::__construct( $parent ); // Override some of parent defaults $this->_method_details[ 'attr' ] = array_merge( $this->_method_details[ 'attr' ], array( 'name' => 'ZipArchive Method', 'compatibility' => pluginbuddy_zbzipziparchive::$_is_compatibility_method ) );
// No relevant parameters for this method $this->_method_details[ 'param' ] = array(); } /** * __destruct() * * Default destructor. * * @return null * */ public function __destruct( ) { parent::__destruct();
} /** * get_method_tag() * * Returns the (static) method tag * * @return string The method tag * */ public function get_method_tag() { return pluginbuddy_zbzipziparchive::$_method_tag; } /** * get_is_compatibility_method() * * Returns the (static) is_compatibility_method boolean * * @return bool * */ public function get_is_compatibility_method() { return pluginbuddy_zbzipziparchive::$_is_compatibility_method; } /** * is_available() * * A function that tests for the availability of the specific method and its available modes. Will test for * multiple modes (zip & unzip) and only return false if neither is available. Actual available modes will * be indicated in the method attributes. * * Note: in this case as the zip and unzip capabilities are all wrapped up in the same class then if we * can zip then we'll assume (for now) that we can unzip as well so attributes are set accordingly. * * @param string $tempdir Temporary directory to use for any test files (must be writeable) * @return bool True if the method is available for at least one mode, false otherwise * */ public function is_available( $tempdir ) { $result = false; $za = NULL; $test_file = $tempdir . 'temp_test_' . uniqid() . '.zip'; // This should give us a new archive object, of not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive test FAILED: %1$s','it-l10n-backupbuddy' ), $error_string ) ); $result = false;
} // Only continue if we have a valid archive object if ( true === $result ) { // This returns true on success, false for some error scenarios or an error code // If it return false we don't really know why - there may have been a Warning generated // but we need to suppress warnings because of the frequency this function is called // so we can only indicate a general failure $res = $za->open( $test_file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE ); if ( true === $res ) { if ( !$za->addFile( __FILE__, 'this_is_a_test.txt') ) { pb_backupbuddy::status( 'details', __('ZipArchive test FAILED: Unable to add file to zip file.','it-l10n-backupbuddy' ) ); $error_string = $za->errorInfo(); pb_backupbuddy::status( 'details', __('ZipArchive Error: ','it-l10n-backupbuddy' ) . $error_string ); } if ( !$za->close() ) { pb_backupbuddy::status( 'details', __('ZipArchive test FAILED: Problem creating/closing zip file.','it-l10n-backupbuddy' ) ); $error_string = $za->errorInfo(); pb_backupbuddy::status( 'details', __('ZipArchive Error: ','it-l10n-backupbuddy' ) . $error_string ); } if ( @file_exists( $test_file ) ) { if ( !@unlink( $test_file ) ) { pb_backupbuddy::status( 'details', 'Error #564634. Unable to delete test file `' . $test_file . '`.' ); } // The zip operation was successful - implies can zip and unzip and hence check and list // Note: we actually don't want to do archiving with this method yet $this->_method_details[ 'attr' ][ 'is_zipper' ] = true; $this->_method_details[ 'attr' ][ 'is_unzipper' ] = true; $this->_method_details[ 'attr' ][ 'is_checker' ] = true; $this->_method_details[ 'attr' ][ 'is_lister' ] = true; $this->_method_details[ 'attr' ][ 'is_commenter' ] = true; $this->_method_details[ 'attr' ][ 'is_unarchiver' ] = true; $this->_method_details[ 'attr' ][ 'is_extractor' ] = true; pb_backupbuddy::status( 'details', __('ZipArchive test PASSED.','it-l10n-backupbuddy' ) ); $result = true; } else { pb_backupbuddy::status( 'details', __('ZipArchive test FAILED: Zip file not found.','it-l10n-backupbuddy' ) ); $result = false; } } else { pb_backupbuddy::status( 'details', __('ZipArchive test FAILED: Unable to create/open zip file.','it-l10n-backupbuddy' ) ); // If we got an error code (rather than simply a false failure indication) then translate it // It seems that in these cases the internal status doesn't indicate anything so we cannot use that if ( false !== $res ) { $error_string = $za->errorInfo( $res ); pb_backupbuddy::status( 'details', __('ZipArchive Error: ','it-l10n-backupbuddy' ) . $error_string ); } $za->close(); $result = false; } } if ( NULL != $za ) { unset( $za ); } return $result; } /** * create() * * A function that creates an archive file * * The $excludes will be a list or relative path excludes if the $listmaker object is NULL otehrwise * will be absolute path excludes and relative path excludes can be had from the $listmaker object * * @param string $zip Full path & filename of ZIP Archive file to create * @param string $dir Full path of directory to add to ZIP Archive file * @parame array $excludes List of either absolute path exclusions or relative exclusions * @param string $tempdir Full path of directory for temporary usage * @param object $listmaker The object from which we can get an inclusions list * @return bool True if the creation was successful, false otherwise * */ public function create( $zip, $dir, $excludes, $tempdir, $listmaker = NULL ) { pb_backupbuddy::status( 'details', __('The ','it-l10n-backupbuddy' ) . $this->get_method_tag() . __(' method is not currently supported for backup.','it-l10n-backupbuddy' ) ); return false; } /** * extract() * * Extracts the contents of a zip file to the specified directory using the best unzip methods possible. * If no specific items given to extract then it's a complete unzip * * @param string $zip_file Full path & filename of ZIP file to extract from. * @param string $destination_directory Full directory path to extract into. * @param array $items Mapping of what to extract and to what * @return bool true on success (all extractions successful), false otherwise */ public function extract( $zip_file, $destination_directory = '', $items = array() ) { $result = false; switch ( $this->get_os_type() ) { case self::OS_TYPE_NIX: if ( empty( $items ) ) { $result = $this->extract_generic_full( $zip_file, $destination_directory ); } else { $result = $this->extract_generic_selected( $zip_file, $destination_directory, $items ); } break; case self::OS_TYPE_WIN: if ( empty( $items ) ) { $result = $this->extract_generic_full( $zip_file, $destination_directory ); } else { $result = $this->extract_generic_selected( $zip_file, $destination_directory, $items ); } break; default: $result = false; } return $result; }
/** * extract_generic_full() * * Extracts the contents of a zip file to the specified directory using the best unzip methods possible. * * @param string $zip_file Full path & filename of ZIP file to extract from. * @param string $destination_directory Full directory path to extract into. * @return bool true on success, false otherwise */ protected function extract_generic_full( $zip_file, $destination_directory = '' ) { $result = false; $za = NULL; $stat = array(); // This should give us a new archive object, if not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { // Something fishy - the methods indicated ziparchive but we couldn't find the class $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive indicated as available method but error reported: %1$s','it-l10n-backupbuddy' ), $error_string ) ); $result = false; } // Only continue if we have a valid archive object if ( true === $result ) { $result = $za->open( $zip_file ); // Make sure we opened the zip ok if ( true === $result ) { // How many files - could be 0 if we had an empty zip file $file_count = $za->numFiles; // Only returns true for success or false for failure - no indication of why failed $result = $za->extractTo( $destination_directory ); // Currently we can only distinguish between success and failure but no finer grain if ( true === $result ) { pb_backupbuddy::status( 'details', sprintf( __('ziparchive extracted file contents (%1$s to %2$s)','it-l10n-backupbuddy' ), $zip_file, $destination_directory ) );
} else { $error_string = $za->errorInfo(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive failed to extract file contents (%1$s to %2$s) - Error Info: %3$s.','it-l10n-backupbuddy' ), $zip_file, $destination_directory, $error_string ) ); // May seem redundant but belt'n'braces $result = false; } $this->log_archive_file_stats( $zip_file ); } else { // Couldn't open archive - will return for maybe another method to try $error_string = $za->errorInfo( $result ); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to open file to extract contents (%1$s to %2$s) - Error Info: %3$s.','it-l10n-backupbuddy' ), $zip_file, $destination_directory, $error_string ) );
// Return an error code and a description - this needs to be handled more generically //$result = array( 1, "Unable to get archive contents" ); // Currently as we are returning an array as a valid result we just return false on failure $result = false;
} $za->close(); } if ( NULL != $za ) { unset( $za ); } return $result; }
/** * extract_generic_selected() * * Extracts the contents of a zip file to the specified directory using the best unzip methods possible. * * @param string $zip_file Full path & filename of ZIP file to extract from. * @param string $destination_directory Full directory path to extract into. * @param array $items Mapping of what to extract and to what * @return bool true on success (all extractions successful), false otherwise */ protected function extract_generic_selected( $zip_file, $destination_directory = '', $items ) { $result = false; $za = NULL; // This should give us a new archive object, if not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { // Something fishy - the methods indicated ziparchive but we couldn't find the class $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive indicated as available method but error reported: %1$s','it-l10n-backupbuddy' ), $error_string ) ); $result = false; } // Only continue if we have a valid archive object if ( true === $result ) { $result = $za->open( $zip_file ); // Make sure we opened the zip ok if ( true === $result ) { // Now we need to take each item and run an unzip for it - unfortunately there is no easy way of combining // arbitrary extractions into a single command if some might be to a foreach ( $items as $what => $where ) { $rename_required = false; $result = false; // Decide how to extract based on where if ( empty( $where) ) { // First we'll extract and then junk the path $result = $za->extractTo( $destination_directory, $what ); // Unlike exec zip we have to effectively junk the path after the extraction // Do this by renaming the file to the destination directory and then getting rid of any directory // structure it was under. If dirname is not . then we know there is a directry path and not // just a simple file name (remember that $what should _not_ have any leading slash whether // it is a filepath or a simple filename) if ( "." != dirname( $what ) ) { rename( $destination_directory . DIRECTORY_SEPARATOR . $what, $destination_directory . DIRECTORY_SEPARATOR . basename( $what) ); // Get the path component of $what - note that dirname() adds a leading slash // even if none was present originally. We must get the first directory component only // so we can do a recursive delete on it. This is a bit klunky but functional. $whatpath = $what; do { $whatpath = dirname( $whatpath ); } while ( 1 < strlen( dirname( $whatpath ) ) );
// Now we can do the recursive delete from that top level component $this->delete_directory_recursive( $destination_directory . $whatpath );
} } elseif ( !empty( $where ) ) { if ( $what === $where ) { // Check for wildcard directory extraction like dir/* => dir/* if ( "*" == substr( trim( $what ), -1 ) ) { // Get our path match string (just clip off the wildcard) $whatroot = substr( trim( $what ), 0, -1 ); $file_count = $za->numFiles; // Crikey, it's a directory tree extraction - don't panic // We need to go through the whole zip and extract each file that matches for ( $i = 0; $i < $file_count; $i++ ) { // Get the filename by index and see if it's in the tree $filename = $za->getNameIndex( $i ); if ( 0 === strpos( $filename, $whatroot ) ) { // $what matched the root of this filename so extract it $result = $za->extractTo( $destination_directory, $filename );
if ( false === $result ) { // An extraction failed so bail out here - this should just // drop us through to the post-processing of $result which on // a false should then drop us out of the foreach loop break; } } } } else { // It's just a single file extraction - breath a sign of relief // Extract to same directory structure - don't junk path, no need to add where to destnation as automatic $result = $za->extractTo( $destination_directory, $what );
} } else { // First we'll extract and then junk the path $result = $za->extractTo( $destination_directory, $what ); // Unlike exec zip we have to effectively junk the path after the extraction // Do this by renaming the file to the destination directory and then getting rid of any directory // structure it was under. If dirname is not . then we know there is a directry path and not // just a simple file name (remember that $what should _not_ have any leading slash whether // it is a filepath or a simple filename) if ( "." != dirname( $what ) ) { rename( $destination_directory . DIRECTORY_SEPARATOR . $what, $destination_directory . DIRECTORY_SEPARATOR . basename( $what) ); // Get the path component of $what - note that dirname() adds a leading slash // even if none was present originally. We must get the first directory component only // so we can do a recursive delete on it. This is a bit klunky but functional. $whatpath = $what; do { $whatpath = dirname( $whatpath ); } while ( 1 < strlen( dirname( $whatpath ) ) );
// Now we can do the recursive delete from that top level component $this->delete_directory_recursive( $destination_directory . $whatpath );
} // Will need to rename if the extract is ok $rename_required = true; } } // Note: we don't open the file and then do stuff but it's all done in one action // so we need to interpret the return code to dedide what to do // Currently we can only distinguish between success and failure but no finer grain if ( true === $result ) { pb_backupbuddy::status( 'details', sprintf( __('ziparchive extracted file contents (%1$s from %2$s to %3$s%4$s)','it-l10n-backupbuddy' ), $what, $zip_file, $destination_directory, $where ) );
// Rename if we have to if ( true === $rename_required) { // Note: we junked the path on the extraction so just the filename of $what is the source but // $where could be a simple file name or a file path $result = $result && rename( $destination_directory . DIRECTORY_SEPARATOR . basename( $what ), $destination_directory . DIRECTORY_SEPARATOR . $where ); }
} else { // For now let's just print the error code and drop through $error_string = $za->errorInfo(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive failed to open/process file to extract file contents (%1$s from %2$s to %3$s%4$s) - Error Info: %5$s.','it-l10n-backupbuddy' ), $what, $zip_file, $destination_directory, $where, $error_string ) ); // May seem redundant but belt'n'braces $result = false; } // If the extraction failed (or rename after extraction) then break out of the foreach and simply return false if ( false === $result ) { break; } } } else { // Couldn't open archive - will return for maybe another method to try $error_string = $za->errorInfo( $result ); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to open file to extract contents (%1$s to %2$s) - Error Info: %3$s.','it-l10n-backupbuddy' ), $zip_file, $destination_directory, $error_string ) );
// Return an error code and a description - this needs to be handled more generically //$result = array( 1, "Unable to get archive contents" ); // Currently as we are returning an array as a valid result we just return false on failure $result = false;
} $za->close(); } if ( NULL != $za ) { unset( $za ); } return $result; } /** * file_exists() * * Tests whether a file (with path) exists in the given zip file * If leave_open is true then the zip object will be left open for faster checking for subsequent files within this zip * * @param string $zip_file The zip file to check * @param string $locate_file The file to test for * @param bool $leave_open Optional: True if the zip file should be left open * @return bool/array True if the file is found in the zip and false if not, array for other problem * */ public function file_exists( $zip_file, $locate_file, $leave_open = false ) { $result = array( 1, "Generic failure indication" ); $za = NULL; // This should give us a new archive object, of not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { // Something fishy - the methods indicated ziparchive but we couldn't find the class $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive indicated as available method but error reported: %1$s','it-l10n-backupbuddy' ), $error_string ) );
// Return an error code and a description - this needs to be handled more generically $result = array( 1, "Class not available to match method" ); } // Only continue if we have a valid archive object if ( true === $result ) { $result = $za->open( $zip_file ); // Make sure we opened the zip ok if ( true === $result ) { // Now try and find the index of the file $index = $za->locateName( $locate_file ); // If we got an index we found it otherwise not found if ( false !== $index ) { pb_backupbuddy::status( 'details', __('File found (ziparchive)','it-l10n-backupbuddy' ) . ': ' . $locate_file ); $result = true; } else { pb_backupbuddy::status( 'details', __('File not found (ziparchive)','it-l10n-backupbuddy' ) . ': ' . $locate_file ); $result = false; } } else { // Couldn't open archive - will return for maybe another method to try $error_string = $za->errorInfo( $result ); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to open file to check if file exists (looking for %1$s in %2$s) - Error Info: %3$s.','it-l10n-backupbuddy' ), $locate_file , $zip_file, $error_string ) );
// Return an error code and a description - this needs to be handled more generically $result = array( 1, "Failed to open/process file" );
} // We have finished with the archive (leave_open ignored for now) $za->close(); } if ( NULL != $za ) { unset( $za ); } return $result; } /* get_file_list() * * Get an array of all files in a zip file with some file properties. * * @param string $zip_file The file to list the content of * @return bool|array false on failure, otherwise array of file properties (may be empty) */ public function get_file_list( $zip_file ) { $file_list = array(); $result = false; $za = NULL; $stat = array(); // This should give us a new archive object, of not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { // Something fishy - the methods indicated ziparchive but we couldn't find the class $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive indicated as available method but error reported: %1$s','it-l10n-backupbuddy' ), $error_string ) ); $result = false; } // Only continue if we have a valid archive object if ( true === $result ) { $result = $za->open( $zip_file ); // Make sure we opened the zip ok if ( true === $result ) { if ( ( $file_count = $za->numFiles ) > 0 ) { // Get each file in sequence by index and get the properties for( $i = 0; $i < $file_count; $i++ ){ $stat = $za->statIndex( $i ); // Assume all these keys do exist (consider testing) $file_list[] = array( $stat['name'], $stat['size'], $stat['comp_size'], $stat['mtime'], ); } } pb_backupbuddy::status( 'details', sprintf( __('ziparchive listed file contents (%1$s)','it-l10n-backupbuddy' ), $zip_file ) );
$this->log_archive_file_stats( $zip_file );
$result = &$file_list; } else { // Couldn't open archive - will return for maybe another method to try $error_string = $za->errorInfo( $result ); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to open file to list contents (%1$s) - Error Info: %2$s.','it-l10n-backupbuddy' ), $zip_file, $error_string ) );
// Return an error code and a description - this needs to be handled more generically //$result = array( 1, "Unable to get archive contents" ); // Currently as we are returning an array as a valid result we just return false on failure $result = false;
} $za->close(); } if ( NULL != $za ) { unset( $za ); } return $result; } /* set_comment() * * Retrieve archive comment. * * @param string $zip_file Filename of archive to set comment on. * @param string $comment Comment to apply to archive. * @return bool true on success, otherwise false. */ public function set_comment( $zip_file, $comment ) { $result = false; $za = NULL; // This should give us a new archive object, of not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { // Something fishy - the methods indicated ziparchive but we couldn't find the class $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive indicated as available method but error reported: %1$s','it-l10n-backupbuddy' ), $error_string ) ); $result = false; } // Only continue if we have a valid archive object if ( true === $result ) { $result = $za->open( $zip_file ); // Make sure at least the zip file opened ok if ( true === $result ) { // Set the comment - true on success, false on failure $result = $za->setArchiveComment( $comment ); // If we got back true then all is well with the world if ( true === $result ) { pb_backupbuddy::status( 'details', sprintf( __('ZipArchive set comment in file %1$s','it-l10n-backupbuddy' ), $zip_file ) ); $result = true; } else { // If we failed to set the commnent then log it (?) and drop through pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to set comment in file %1$s','it-l10n-backupbuddy' ), $zip_file ) ); $result = false; } } else { // If we couldn't open the zip file then log it (?) and drop through $error_string = $za->errorInfo( $result ); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to open file to set comment in file %1$s - Error Info: %2$s','it-l10n-backupbuddy' ), $zip_file, $error_string ) ); $result = false; } $za->close(); } if ( NULL != $za ) { unset( $za ); } return $result; }
/* get_comment() * * Retrieve archive comment. * * @param string $zip_file Filename of archive to retrieve comment from. * @return bool|string false on failure, Zip comment otherwise. */ public function get_comment( $zip_file ) { $result = false; $za = NULL; // This should give us a new archive object, of not catch it and bail out try { $za = new pluginbuddy_ZipArchive(); $result = true; } catch ( Exception $e ) { // Something fishy - the methods indicated ziparchive but we couldn't find the class $error_string = $e->getMessage(); pb_backupbuddy::status( 'details', sprintf( __('ziparchive indicated as available method but error reported: %1$s','it-l10n-backupbuddy' ), $error_string ) ); $result = false; } // Only continue if we have a valid archive object if ( true === $result ) { $result = $za->open( $zip_file ); // Make sure at least the zip file opened ok if ( true === $result ) { // Get the comment or false on failure for some reason // Note: Currently, due to a bug in ZipArchive, getArchiveComment() // returns false for an empty comment whereas it should just return an // empty string. We'll live with this for now as it should only happen // when an archive is fresh and has the Integrity Check run (or when the // check is rerun). Once a comment is added the function behaves. // If any problems are thrown up then there is the option to use the // archive property but that has a downside in that it can only ever // return a string so if there really is an error in reading the comment // it is not possible to know (AFAIK). Perhaps an error status value might // be set somewhere? // The bug will be reported to PHP developers but we will still have to // live with this for a while because it takes hosts ages to catch up to // updated PHP versions. $comment = $za->getArchiveComment( ZIPARCHIVE::FL_UNCHANGED ); //$comment = $za->comment; // If we have a comment (even if empty) then return it if ( false !== $comment ) {
// Note: new archives will return an empty comment if one was not added at creation pb_backupbuddy::status( 'details', sprintf( __('ZipArchive retrieved comment in file %1$s','it-l10n-backupbuddy' ), $zip_file ) ); $result = $comment; } else { // If we failed to get the commnent then log it (?) and drop through pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to retrieve comment in file %1$s','it-l10n-backupbuddy' ), $zip_file ) ); $result = false; }
} else { // If we couldn't open the zip file then log it (?) and drop through $error_string = $za->errorInfo( $result ); pb_backupbuddy::status( 'details', sprintf( __('ZipArchive failed to open file to get comment in file %1$s - Error Info: %2$s','it-l10n-backupbuddy' ), $zip_file, $error_string ) ); $result = false; } $za->close(); } else { // Something fishy - the methods indicated ziparchive but we couldn't find the class pb_backupbuddy::status( 'details', __('ziparchive indicated as available method but ZipArchive class non-existent','it-l10n-backupbuddy' ) ); $result = false; } if ( NULL != $za ) { unset( $za ); } return $result; } } // end pluginbuddy_zbzipziparchive class. } ?>
|