-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathphp_zip.c
3245 lines (2719 loc) · 76.1 KB
/
php_zip.c
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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Piere-Alain Joye <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "ext/standard/php_string.h" /* For php_basename() */
#include "ext/pcre/php_pcre.h"
#include "ext/standard/php_filestat.h"
#include "zend_attributes.h"
#include "zend_interfaces.h"
#include "php_zip.h"
#include "php_zip_arginfo.h"
#ifdef HAVE_GLOB
#ifndef PHP_WIN32
#include <glob.h>
#else
#include "win32/glob.h"
#endif
#endif
#if PHP_VERSION_ID < 80300
#define zend_zval_value_name(opt) zend_zval_type_name(opt)
#endif
#if PHP_VERSION_ID < 80400
#define zend_argument_must_not_be_empty_error(n) zend_argument_value_error(n, "must not be empty");
#endif
/* {{{ Resource le */
static int le_zip_dir;
#define le_zip_dir_name "Zip Directory"
static int le_zip_entry;
#define le_zip_entry_name "Zip Entry"
/* }}} */
/* {{{ PHP_ZIP_STAT_INDEX(za, index, flags, sb) */
#define PHP_ZIP_STAT_INDEX(za, index, flags, sb) \
if (zip_stat_index(za, index, flags, &sb) != 0) { \
RETURN_FALSE; \
}
/* }}} */
/* {{{ PHP_ZIP_STAT_PATH(za, path, path_len, flags, sb)
This is always used for the first argument*/
#define PHP_ZIP_STAT_PATH(za, path, path_len, flags, sb) \
if (path_len == 0) { \
zend_argument_must_not_be_empty_error(1); \
RETURN_THROWS(); \
} \
if (zip_stat(za, path, flags, &sb) != 0) { \
RETURN_FALSE; \
}
/* }}} */
/* {{{ PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) */
#define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) \
if (comment_len == 0) { \
/* Passing NULL remove the existing comment */ \
if (zip_file_set_comment(za, index, NULL, 0, 0) < 0) { \
RETURN_FALSE; \
} \
} else if (zip_file_set_comment(za, index, comment, comment_len, 0) < 0) { \
RETURN_FALSE; \
} \
RETURN_TRUE;
/* }}} */
# define add_ascii_assoc_string add_assoc_string
# define add_ascii_assoc_long add_assoc_long
/* Flatten a path by making a relative path (to .)*/
static char * php_zip_make_relative_path(char *path, size_t path_len) /* {{{ */
{
char *path_begin = path;
size_t i;
if (path_len < 1 || path == NULL) {
return NULL;
}
if (IS_ABSOLUTE_PATH(path, path_len)) {
return path + COPY_WHEN_ABSOLUTE(path) + 1;
}
i = path_len;
while (1) {
while (i > 0 && !IS_SLASH(path[i])) {
i--;
}
if (!i) {
return path;
}
if (i >= 2 && path[i -1] == '.') {
/* i is the position of ., add 1 for / */
path_begin = path + i + 1;
break;
}
i--;
}
return path_begin;
}
/* }}} */
# define CWD_STATE_ALLOC(l) emalloc(l)
# define CWD_STATE_FREE(s) efree(s)
/* {{{ php_zip_extract_file */
static int php_zip_extract_file(struct zip * za, char *dest, const char *file, size_t file_len, zip_int64_t idx)
{
php_stream_statbuf ssb;
struct zip_file *zf;
struct zip_stat sb;
char b[8192];
int n, ret;
php_stream *stream;
char *fullpath;
char *file_dirname_fullpath;
char file_dirname[MAXPATHLEN];
size_t dir_len, len;
int is_dir_only = 0;
char *path_cleaned;
size_t path_cleaned_len;
cwd_state new_state;
zend_string *file_basename;
if (idx < 0) {
idx = zip_name_locate(za, file, 0);
if (idx < 0) {
return 0;
}
}
new_state.cwd = CWD_STATE_ALLOC(1);
new_state.cwd[0] = '\0';
new_state.cwd_length = 0;
/* Clean/normlize the path and then transform any path (absolute or relative)
to a path relative to cwd (../../mydir/foo.txt > mydir/foo.txt)
*/
virtual_file_ex(&new_state, file, NULL, CWD_EXPAND);
path_cleaned = php_zip_make_relative_path(new_state.cwd, new_state.cwd_length);
if(!path_cleaned) {
CWD_STATE_FREE(new_state.cwd);
return 0;
}
path_cleaned_len = strlen(path_cleaned);
if (path_cleaned_len >= MAXPATHLEN || zip_stat_index(za, idx, 0, &sb) != 0) {
CWD_STATE_FREE(new_state.cwd);
return 0;
}
/* it is a directory only, see #40228 */
if (path_cleaned_len > 1 && IS_SLASH(path_cleaned[path_cleaned_len - 1])) {
len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, path_cleaned);
is_dir_only = 1;
} else {
memcpy(file_dirname, path_cleaned, path_cleaned_len);
dir_len = zend_dirname(file_dirname, path_cleaned_len);
if (!dir_len || (dir_len == 1 && file_dirname[0] == '.')) {
len = spprintf(&file_dirname_fullpath, 0, "%s", dest);
} else {
len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname);
}
file_basename = php_basename(path_cleaned, path_cleaned_len, NULL, 0);
if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname_fullpath)) {
efree(file_dirname_fullpath);
zend_string_release_ex(file_basename, 0);
CWD_STATE_FREE(new_state.cwd);
return 0;
}
}
/* let see if the path already exists */
if (php_stream_stat_path_ex(file_dirname_fullpath, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL) < 0) {
ret = php_stream_mkdir(file_dirname_fullpath, 0777, PHP_STREAM_MKDIR_RECURSIVE|REPORT_ERRORS, NULL);
if (!ret) {
efree(file_dirname_fullpath);
if (!is_dir_only) {
zend_string_release_ex(file_basename, 0);
}
CWD_STATE_FREE(new_state.cwd);
return 0;
}
}
/* it is a standalone directory, job done */
if (is_dir_only) {
efree(file_dirname_fullpath);
CWD_STATE_FREE(new_state.cwd);
return 1;
}
len = spprintf(&fullpath, 0, "%s/%s", file_dirname_fullpath, ZSTR_VAL(file_basename));
if (!len) {
efree(file_dirname_fullpath);
zend_string_release_ex(file_basename, 0);
CWD_STATE_FREE(new_state.cwd);
return 0;
} else if (len > MAXPATHLEN) {
php_error_docref(NULL, E_WARNING, "Full extraction path exceed MAXPATHLEN (%i)", MAXPATHLEN);
efree(file_dirname_fullpath);
zend_string_release_ex(file_basename, 0);
CWD_STATE_FREE(new_state.cwd);
return 0;
}
/* check again the full path, not sure if it
* is required, does a file can have a different
* safemode status as its parent folder?
*/
if (ZIP_OPENBASEDIR_CHECKPATH(fullpath)) {
efree(fullpath);
efree(file_dirname_fullpath);
zend_string_release_ex(file_basename, 0);
CWD_STATE_FREE(new_state.cwd);
return 0;
}
zf = zip_fopen_index(za, idx, 0);
if (zf == NULL) {
n = -1;
goto done;
}
stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
if (stream == NULL) {
n = -1;
zip_fclose(zf);
goto done;
}
n = 0;
while ((n=zip_fread(zf, b, sizeof(b))) > 0) {
php_stream_write(stream, b, n);
}
if (stream->wrapper->wops->stream_metadata) {
struct utimbuf ut;
ut.modtime = ut.actime = sb.mtime;
stream->wrapper->wops->stream_metadata(stream->wrapper, fullpath, PHP_STREAM_META_TOUCH, &ut, NULL);
}
php_stream_close(stream);
n = zip_fclose(zf);
done:
efree(fullpath);
zend_string_release_ex(file_basename, 0);
efree(file_dirname_fullpath);
CWD_STATE_FREE(new_state.cwd);
if (n<0) {
return 0;
} else {
return 1;
}
}
/* }}} */
static int php_zip_add_file(ze_zip_object *obj, const char *filename, size_t filename_len,
char *entry_name, size_t entry_name_len, /* unused if replace >= 0 */
zip_uint64_t offset_start, zip_uint64_t offset_len,
zend_long replace, /* index to replace, add new file if < 0 */
zip_flags_t flags
) /* {{{ */
{
struct zip_source *zs;
char resolved_path[MAXPATHLEN];
php_stream_statbuf ssb;
if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
return -1;
}
if (!expand_filepath(filename, resolved_path)) {
php_error_docref(NULL, E_WARNING, "No such file or directory");
return -1;
}
if (php_stream_stat_path_ex(resolved_path, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL)) {
php_error_docref(NULL, E_WARNING, "No such file or directory");
return -1;
}
if (flags & ZIP_FL_OPEN_FILE_NOW) {
FILE *fd;
fd = fopen(resolved_path, "rb");
if (!fd) {
return -1;
}
flags ^= ZIP_FL_OPEN_FILE_NOW;
zs = zip_source_filep(obj->za, fd, offset_start, offset_len);
} else {
zs = zip_source_file(obj->za, resolved_path, offset_start, offset_len);
}
if (!zs) {
return -1;
}
/* Replace */
if (replace >= 0) {
if (zip_file_replace(obj->za, replace, zs, flags) < 0) {
zip_source_free(zs);
return -1;
}
zip_error_clear(obj->za);
return 1;
}
/* Add */
obj->last_id = zip_file_add(obj->za, entry_name, zs, flags);
if (obj->last_id < 0) {
zip_source_free(zs);
return -1;
}
zip_error_clear(obj->za);
return 1;
}
/* }}} */
typedef struct {
zend_long remove_all_path;
char *remove_path;
size_t remove_path_len;
char *add_path;
size_t add_path_len;
zip_flags_t flags;
zip_int32_t comp_method;
zip_uint32_t comp_flags;
#ifdef HAVE_ENCRYPTION
zip_int16_t enc_method;
char *enc_password;
#endif
} zip_options;
static int php_zip_parse_options(HashTable *options, zip_options *opts)
/* {{{ */
{
zval *option;
/* default values */
memset(opts, 0, sizeof(zip_options));
opts->flags = ZIP_FL_OVERWRITE;
opts->comp_method = -1; /* -1 to not change default */
#ifdef HAVE_ENCRYPTION
opts->enc_method = -1; /* -1 to not change default */
#endif
if ((option = zend_hash_str_find(options, "remove_all_path", sizeof("remove_all_path") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_FALSE && Z_TYPE_P(option) != IS_TRUE) {
php_error_docref(NULL, E_WARNING, "Option \"remove_all_path\" must be of type bool, %s given",
zend_zval_value_name(option));
}
opts->remove_all_path = zval_get_long(option);
}
if ((option = zend_hash_str_find(options, "comp_method", sizeof("comp_method") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_LONG) {
php_error_docref(NULL, E_WARNING, "Option \"comp_method\" must be of type int, %s given",
zend_zval_value_name(option));
}
opts->comp_method = zval_get_long(option);
if ((option = zend_hash_str_find(options, "comp_flags", sizeof("comp_flags") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_LONG) {
php_error_docref(NULL, E_WARNING, "Option \"comp_flags\" must be of type int, %s given",
zend_zval_value_name(option));
}
opts->comp_flags = zval_get_long(option);
}
}
#ifdef HAVE_ENCRYPTION
if ((option = zend_hash_str_find(options, "enc_method", sizeof("enc_method") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_LONG) {
php_error_docref(NULL, E_WARNING, "Option \"enc_method\" must be of type int, %s given",
zend_zval_value_name(option));
}
opts->enc_method = zval_get_long(option);
if ((option = zend_hash_str_find(options, "enc_password", sizeof("enc_password") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_STRING) {
zend_type_error("Option \"enc_password\" must be of type string, %s given",
zend_zval_value_name(option));
return -1;
}
opts->enc_password = Z_STRVAL_P(option);
}
}
#endif
if ((option = zend_hash_str_find(options, "remove_path", sizeof("remove_path") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_STRING) {
zend_type_error("Option \"remove_path\" must be of type string, %s given",
zend_zval_value_name(option));
return -1;
}
if (Z_STRLEN_P(option) == 0) {
zend_value_error("Option \"remove_path\" must not be empty");
return -1;
}
if (Z_STRLEN_P(option) >= MAXPATHLEN) {
zend_value_error("Option \"remove_path\" must be less than %d bytes", MAXPATHLEN - 1);
return -1;
}
opts->remove_path_len = Z_STRLEN_P(option);
opts->remove_path = Z_STRVAL_P(option);
}
if ((option = zend_hash_str_find(options, "add_path", sizeof("add_path") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_STRING) {
zend_type_error("Option \"add_path\" must be of type string, %s given",
zend_zval_value_name(option));
return -1;
}
if (Z_STRLEN_P(option) == 0) {
zend_value_error("Option \"add_path\" must not be empty");
return -1;
}
if (Z_STRLEN_P(option) >= MAXPATHLEN) {
zend_value_error("Option \"add_path\" must be less than %d bytes", MAXPATHLEN - 1);
return -1;
}
opts->add_path_len = Z_STRLEN_P(option);
opts->add_path = Z_STRVAL_P(option);
}
if ((option = zend_hash_str_find(options, "flags", sizeof("flags") - 1)) != NULL) {
if (Z_TYPE_P(option) != IS_LONG) {
zend_type_error("Option \"flags\" must be of type int, %s given",
zend_zval_value_name(option));
return -1;
}
opts->flags = Z_LVAL_P(option);
}
return 1;
}
/* }}} */
/* {{{ ZIP_FROM_OBJECT */
#define ZIP_FROM_OBJECT(intern, object) \
{ \
ze_zip_object *obj = Z_ZIP_P(object); \
intern = obj->za; \
if (!intern) { \
zend_value_error("Invalid or uninitialized Zip object"); \
RETURN_THROWS(); \
} \
}
/* }}} */
/* {{{ RETURN_SB(sb) */
#ifdef HAVE_ENCRYPTION
#define RETURN_SB(sb) \
{ \
array_init(return_value); \
add_ascii_assoc_string(return_value, "name", (char *)(sb)->name); \
add_ascii_assoc_long(return_value, "index", (zend_long) (sb)->index); \
add_ascii_assoc_long(return_value, "crc", (zend_long) (sb)->crc); \
add_ascii_assoc_long(return_value, "size", (zend_long) (sb)->size); \
add_ascii_assoc_long(return_value, "mtime", (zend_long) (sb)->mtime); \
add_ascii_assoc_long(return_value, "comp_size", (zend_long) (sb)->comp_size); \
add_ascii_assoc_long(return_value, "comp_method", (zend_long) (sb)->comp_method); \
add_ascii_assoc_long(return_value, "encryption_method", (zend_long) (sb)->encryption_method); \
}
#else
#define RETURN_SB(sb) \
{ \
array_init(return_value); \
add_ascii_assoc_string(return_value, "name", (char *)(sb)->name); \
add_ascii_assoc_long(return_value, "index", (zend_long) (sb)->index); \
add_ascii_assoc_long(return_value, "crc", (zend_long) (sb)->crc); \
add_ascii_assoc_long(return_value, "size", (zend_long) (sb)->size); \
add_ascii_assoc_long(return_value, "mtime", (zend_long) (sb)->mtime); \
add_ascii_assoc_long(return_value, "comp_size", (zend_long) (sb)->comp_size); \
add_ascii_assoc_long(return_value, "comp_method", (zend_long) (sb)->comp_method); \
}
#endif
/* }}} */
static zend_long php_zip_status(ze_zip_object *obj) /* {{{ */
{
int zep = obj->err_zip; /* saved err if closed */
if (obj->za) {
#if LIBZIP_VERSION_MAJOR < 1
int syp;
zip_error_get(obj->za, &zep, &syp);
#else
zip_error_t *err;
err = zip_get_error(obj->za);
zep = zip_error_code_zip(err);
zip_error_fini(err);
#endif
}
return zep;
}
/* }}} */
static zend_long php_zip_last_id(ze_zip_object *obj) /* {{{ */
{
return obj->last_id;
}
/* }}} */
static zend_long php_zip_status_sys(ze_zip_object *obj) /* {{{ */
{
int syp = obj->err_sys; /* saved err if closed */
if (obj->za) {
#if LIBZIP_VERSION_MAJOR < 1
int zep;
zip_error_get(obj->za, &zep, &syp);
#else
zip_error_t *err;
err = zip_get_error(obj->za);
syp = zip_error_code_system(err);
zip_error_fini(err);
#endif
}
return syp;
}
/* }}} */
static zend_long php_zip_get_num_files(ze_zip_object *obj) /* {{{ */
{
if (obj->za) {
zip_int64_t num = zip_get_num_entries(obj->za, 0);
return MIN(num, ZEND_LONG_MAX);
}
return 0;
}
/* }}} */
static char * php_zipobj_get_filename(ze_zip_object *obj, int *len) /* {{{ */
{
if (obj && obj->filename) {
*len = strlen(obj->filename);
return obj->filename;
}
return NULL;
}
/* }}} */
static char * php_zipobj_get_zip_comment(ze_zip_object *obj, int *len) /* {{{ */
{
if (obj->za) {
return (char *)zip_get_archive_comment(obj->za, len, 0);
}
return NULL;
}
/* }}} */
#ifdef HAVE_GLOB /* {{{ */
#ifndef GLOB_ONLYDIR
#define GLOB_ONLYDIR (1<<30)
#define GLOB_EMULATE_ONLYDIR
#define GLOB_FLAGMASK (~GLOB_ONLYDIR)
#else
#define GLOB_FLAGMASK (~0)
#endif
#ifndef GLOB_BRACE
# define GLOB_BRACE 0
#endif
#ifndef GLOB_MARK
# define GLOB_MARK 0
#endif
#ifndef GLOB_NOSORT
# define GLOB_NOSORT 0
#endif
#ifndef GLOB_NOCHECK
# define GLOB_NOCHECK 0
#endif
#ifndef GLOB_NOESCAPE
# define GLOB_NOESCAPE 0
#endif
#ifndef GLOB_ERR
# define GLOB_ERR 0
#endif
/* This is used for checking validity of passed flags (passing invalid flags causes segfault in glob()!! */
#define GLOB_AVAILABLE_FLAGS (0 | GLOB_BRACE | GLOB_MARK | GLOB_NOSORT | GLOB_NOCHECK | GLOB_NOESCAPE | GLOB_ERR | GLOB_ONLYDIR)
#endif /* }}} */
int php_zip_glob(char *pattern, int pattern_len, zend_long flags, zval *return_value) /* {{{ */
{
#ifdef HAVE_GLOB
int cwd_skip = 0;
#ifdef ZTS
char cwd[MAXPATHLEN];
char work_pattern[MAXPATHLEN];
char *result;
#endif
glob_t globbuf;
size_t n;
int ret;
if (pattern_len >= MAXPATHLEN) {
php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
return -1;
}
if ((GLOB_AVAILABLE_FLAGS & flags) != flags) {
php_error_docref(NULL, E_WARNING, "At least one of the passed flags is invalid or not supported on this platform");
return -1;
}
#ifdef ZTS
if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
result = VCWD_GETCWD(cwd, MAXPATHLEN);
if (!result) {
cwd[0] = '\0';
}
#ifdef PHP_WIN32
if (IS_SLASH(*pattern)) {
cwd[2] = '\0';
}
#endif
cwd_skip = strlen(cwd)+1;
snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
pattern = work_pattern;
}
#endif
globbuf.gl_offs = 0;
if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
#ifdef GLOB_NOMATCH
if (GLOB_NOMATCH == ret) {
/* Some glob implementation simply return no data if no matches
were found, others return the GLOB_NOMATCH error code.
We don't want to treat GLOB_NOMATCH as an error condition
so that PHP glob() behaves the same on both types of
implementations and so that 'foreach (glob() as ...'
can be used for simple glob() calls without further error
checking.
*/
array_init(return_value);
return 0;
}
#endif
return 0;
}
/* now catch the FreeBSD style of "no matches" */
if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
array_init(return_value);
return 0;
}
/* we assume that any glob pattern will match files from one directory only
so checking the dirname of the first match should be sufficient */
if (ZIP_OPENBASEDIR_CHECKPATH(globbuf.gl_pathv[0])) {
return -1;
}
array_init(return_value);
for (n = 0; n < globbuf.gl_pathc; n++) {
/* we need to do this every time since GLOB_ONLYDIR does not guarantee that
* all directories will be filtered. GNU libc documentation states the
* following:
* If the information about the type of the file is easily available
* non-directories will be rejected but no extra work will be done to
* determine the information for each file. I.e., the caller must still be
* able to filter directories out.
*/
if (flags & GLOB_ONLYDIR) {
zend_stat_t s = {0};
if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
continue;
}
if (S_IFDIR != (s.st_mode & S_IFMT)) {
continue;
}
}
add_next_index_string(return_value, globbuf.gl_pathv[n]+cwd_skip);
}
ret = globbuf.gl_pathc;
globfree(&globbuf);
return ret;
#else
zend_throw_error(NULL, "Glob support is not available");
return 0;
#endif /* HAVE_GLOB */
}
/* }}} */
int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *return_value) /* {{{ */
{
#ifdef ZTS
char cwd[MAXPATHLEN];
char work_path[MAXPATHLEN];
char *result;
#endif
int files_cnt;
zend_string **namelist;
pcre2_match_context *mctx = php_pcre_mctx();
#ifdef ZTS
if (!IS_ABSOLUTE_PATH(path, path_len)) {
result = VCWD_GETCWD(cwd, MAXPATHLEN);
if (!result) {
cwd[0] = '\0';
}
#ifdef PHP_WIN32
if (IS_SLASH(*path)) {
cwd[2] = '\0';
}
#endif
snprintf(work_path, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, path);
path = work_path;
}
#endif
if (ZIP_OPENBASEDIR_CHECKPATH(path)) {
return -1;
}
files_cnt = php_stream_scandir(path, &namelist, NULL, (void *) php_stream_dirent_alphasort);
if (files_cnt > 0) {
pcre2_code *re = NULL;
pcre2_match_data *match_data = NULL;
uint32_t i, capture_count;
int rc;
re = pcre_get_compiled_regex(regexp, &capture_count);
if (!re) {
php_error_docref(NULL, E_WARNING, "Invalid expression");
return -1;
}
array_init(return_value);
/* only the files, directories are ignored */
for (i = 0; i < files_cnt; i++) {
zend_stat_t s = {0};
char fullpath[MAXPATHLEN];
size_t namelist_len = ZSTR_LEN(namelist[i]);
if ((namelist_len == 1 && ZSTR_VAL(namelist[i])[0] == '.') ||
(namelist_len == 2 && ZSTR_VAL(namelist[i])[0] == '.' && ZSTR_VAL(namelist[i])[1] == '.')) {
zend_string_release_ex(namelist[i], 0);
continue;
}
if ((path_len + namelist_len + 1) >= MAXPATHLEN) {
php_error_docref(NULL, E_WARNING, "add_path string too long (max: %u, %zu given)",
MAXPATHLEN - 1, (path_len + namelist_len + 1));
zend_string_release_ex(namelist[i], 0);
break;
}
match_data = php_pcre_create_match_data(capture_count, re);
if (!match_data) {
/* Allocation failed, but can proceed to the next pattern. */
zend_string_release_ex(namelist[i], 0);
continue;
}
rc = pcre2_match(re, (PCRE2_SPTR)ZSTR_VAL(namelist[i]), ZSTR_LEN(namelist[i]), 0, 0, match_data, mctx);
php_pcre_free_match_data(match_data);
/* 0 means that the vector is too small to hold all the captured substring offsets */
if (rc < 0) {
zend_string_release_ex(namelist[i], 0);
continue;
}
snprintf(fullpath, MAXPATHLEN, "%s%c%s", path, DEFAULT_SLASH, ZSTR_VAL(namelist[i]));
if (0 != VCWD_STAT(fullpath, &s)) {
php_error_docref(NULL, E_WARNING, "Cannot read <%s>", fullpath);
zend_string_release_ex(namelist[i], 0);
continue;
}
if (S_IFDIR == (s.st_mode & S_IFMT)) {
zend_string_release_ex(namelist[i], 0);
continue;
}
add_next_index_string(return_value, fullpath);
zend_string_release_ex(namelist[i], 0);
}
efree(namelist);
}
return files_cnt;
}
/* }}} */
/* {{{ ZE2 OO definitions */
static zend_class_entry *zip_class_entry;
static zend_object_handlers zip_object_handlers;
static HashTable zip_prop_handlers;
typedef zend_long (*zip_read_int_t)(ze_zip_object *obj);
typedef char *(*zip_read_const_char_t)(ze_zip_object *obj, int *len);
typedef struct _zip_prop_handler {
zip_read_int_t read_int_func;
zip_read_const_char_t read_const_char_func;
int type;
} zip_prop_handler;
/* }}} */
static void php_zip_register_prop_handler(HashTable *prop_handler, char *name, zip_read_int_t read_int_func, zip_read_const_char_t read_char_func, int rettype) /* {{{ */
{
zip_prop_handler hnd;
zend_string *str;
hnd.read_const_char_func = read_char_func;
hnd.read_int_func = read_int_func;
hnd.type = rettype;
str = zend_string_init_interned(name, strlen(name), 1);
zend_hash_add_mem(prop_handler, str, &hnd, sizeof(zip_prop_handler));
zend_string_release_ex(str, 1);
}
/* }}} */
static zval *php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd, zval *rv) /* {{{ */
{
const char *retchar = NULL;
zend_long retint = 0;
int len = 0;
if (hnd->read_const_char_func) {
retchar = hnd->read_const_char_func(obj, &len);
} else if (hnd->read_int_func) {
retint = hnd->read_int_func(obj);
}
switch (hnd->type) {
case IS_STRING:
if (retchar) {
ZVAL_STRINGL(rv, (char *) retchar, len);
} else {
ZVAL_EMPTY_STRING(rv);
}
break;
case IS_LONG:
ZVAL_LONG(rv, retint);
break;
default:
ZVAL_NULL(rv);
}
return rv;
}
/* }}} */
static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
{
ze_zip_object *obj;
zval *retval = NULL;
zip_prop_handler *hnd = NULL;
obj = php_zip_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}
if (hnd == NULL) {
retval = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
}
return retval;
}
/* }}} */
static zval *php_zip_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
{
ze_zip_object *obj;
zip_prop_handler *hnd = NULL;
obj = php_zip_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}
if (hnd != NULL) {
zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
return &EG(error_zval);
}
return zend_std_write_property(object, name, value, cache_slot);
}
static zval *php_zip_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
{
ze_zip_object *obj;
zval *retval = NULL;
zip_prop_handler *hnd = NULL;
obj = php_zip_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}
if (hnd != NULL) {
retval = php_zip_property_reader(obj, hnd, rv);
if (retval == NULL) {
retval = &EG(uninitialized_zval);
}
} else {
retval = zend_std_read_property(object, name, type, cache_slot, rv);
}
return retval;
}
/* }}} */
// todo: make php_zip_has_property return bool as well
static int php_zip_has_property(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
{
ze_zip_object *obj;
zip_prop_handler *hnd = NULL;
bool retval = false;
obj = php_zip_fetch_object(object);
if (obj->prop_handler != NULL) {
hnd = zend_hash_find_ptr(obj->prop_handler, name);
}
if (hnd != NULL) {
zval tmp, *prop;
if (type == 2) {
retval = true;
} else if ((prop = php_zip_property_reader(obj, hnd, &tmp)) != NULL) {
if (type == 1) {
retval = zend_is_true(&tmp);
} else if (type == 0) {
retval = (Z_TYPE(tmp) != IS_NULL);
}
}
zval_ptr_dtor(&tmp);
} else {
retval = zend_std_has_property(object, name, type, cache_slot);
}
return retval;
}
/* }}} */
static HashTable *php_zip_get_gc(zend_object *object, zval **gc_data, int *gc_data_count) /* {{{ */
{
*gc_data = NULL;
*gc_data_count = 0;
return zend_std_get_properties(object);
}