forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_zip.c
2884 lines (2435 loc) · 72.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 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: |
| http://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]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#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"
#include "ext/pcre/php_pcre.h"
#include "php_zip.h"
#include "lib/zip.h"
#include "lib/zipint.h"
/* zip_open is a macro for renaming libzip zipopen, so we need to use PHP_NAMED_FUNCTION */
static PHP_NAMED_FUNCTION(zif_zip_open);
static PHP_NAMED_FUNCTION(zif_zip_read);
static PHP_NAMED_FUNCTION(zif_zip_close);
static PHP_NAMED_FUNCTION(zif_zip_entry_read);
static PHP_NAMED_FUNCTION(zif_zip_entry_filesize);
static PHP_NAMED_FUNCTION(zif_zip_entry_name);
static PHP_NAMED_FUNCTION(zif_zip_entry_compressedsize);
static PHP_NAMED_FUNCTION(zif_zip_entry_compressionmethod);
static PHP_NAMED_FUNCTION(zif_zip_entry_open);
static PHP_NAMED_FUNCTION(zif_zip_entry_close);
#ifdef HAVE_GLOB
#ifndef PHP_WIN32
#include <glob.h>
#else
#include "win32/glob.h"
#endif
#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) */
#define PHP_ZIP_STAT_PATH(za, path, path_len, flags, sb) \
if (path_len < 1) { \
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name"); \
RETURN_FALSE; \
} \
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_set_file_comment(intern, index, NULL, 0) < 0) { \
RETURN_FALSE; \
} \
} else if (zip_set_file_comment(intern, index, comment, comment_len) < 0) { \
RETURN_FALSE; \
} \
RETURN_TRUE;
/* }}} */
#if (PHP_MAJOR_VERSION < 6)
# define add_ascii_assoc_string add_assoc_string
# define add_ascii_assoc_long add_assoc_long
#endif
/* Flatten a path by making a relative path (to .)*/
static char * php_zip_make_relative_path(char *path, int path_len) /* {{{ */
{
char *path_begin = path;
size_t i;
if (IS_SLASH(path[0])) {
return path + 1;
}
if (path_len < 1 || path == NULL) {
return NULL;
}
i = path_len;
while (1) {
while (i > 0 && !IS_SLASH(path[i])) {
i--;
}
if (!i) {
return path;
}
if (i >= 2 && (path[i -1] == '.' || path[i -1] == ':')) {
/* i is the position of . or :, add 1 for / */
path_begin = path + i + 1;
break;
}
i--;
}
return path_begin;
}
/* }}} */
#ifdef PHP_ZIP_USE_OO
/* {{{ php_zip_extract_file */
static int php_zip_extract_file(struct zip * za, char *dest, char *file, int file_len TSRMLS_DC)
{
php_stream_statbuf ssb;
struct zip_file *zf;
struct zip_stat sb;
char b[8192];
int n, len, ret;
php_stream *stream;
char *fullpath;
char *file_dirname_fullpath;
char file_dirname[MAXPATHLEN];
size_t dir_len;
char *file_basename;
size_t file_basename_len;
int is_dir_only = 0;
char *path_cleaned;
size_t path_cleaned_len;
cwd_state new_state;
new_state.cwd = (char*)malloc(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 TSRMLS_CC);
path_cleaned = php_zip_make_relative_path(new_state.cwd, new_state.cwd_length);
if(!path_cleaned) {
return 0;
}
path_cleaned_len = strlen(path_cleaned);
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
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, file);
is_dir_only = 1;
} else {
memcpy(file_dirname, path_cleaned, path_cleaned_len);
dir_len = php_dirname(file_dirname, path_cleaned_len);
if (dir_len <= 0 || (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);
}
php_basename(path_cleaned, path_cleaned_len, NULL, 0, &file_basename, (size_t *)&file_basename_len TSRMLS_CC);
if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname_fullpath)) {
efree(file_dirname_fullpath);
efree(file_basename);
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) {
#if defined(PHP_WIN32) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1)
char *e;
e = file_dirname_fullpath;
while (*e) {
if (*e == '/') {
*e = DEFAULT_SLASH;
}
e++;
}
#endif
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) {
efree(file_basename);
free(new_state.cwd);
}
return 0;
}
}
/* it is a standalone directory, job done */
if (is_dir_only) {
efree(file_dirname_fullpath);
free(new_state.cwd);
return 1;
}
len = spprintf(&fullpath, 0, "%s/%s", file_dirname_fullpath, file_basename);
if (!len) {
efree(file_dirname_fullpath);
efree(file_basename);
free(new_state.cwd);
return 0;
} else if (len > MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Full extraction path exceed MAXPATHLEN (%i)", MAXPATHLEN);
efree(file_dirname_fullpath);
efree(file_basename);
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);
efree(file_basename);
free(new_state.cwd);
return 0;
}
#if PHP_API_VERSION < 20100412
stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
#else
stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
#endif
if (stream == NULL) {
n = -1;
goto done;
}
zf = zip_fopen(za, file, 0);
if (zf == NULL) {
n = -1;
php_stream_close(stream);
goto done;
}
n = 0;
while ((n=zip_fread(zf, b, sizeof(b))) > 0) {
php_stream_write(stream, b, n);
}
php_stream_close(stream);
n = zip_fclose(zf);
done:
efree(fullpath);
efree(file_basename);
efree(file_dirname_fullpath);
free(new_state.cwd);
if (n<0) {
return 0;
} else {
return 1;
}
}
/* }}} */
static int php_zip_add_file(struct zip *za, const char *filename, size_t filename_len,
char *entry_name, size_t entry_name_len, long offset_start, long offset_len TSRMLS_DC) /* {{{ */
{
struct zip_source *zs;
int cur_idx;
char resolved_path[MAXPATHLEN];
if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
return -1;
}
if (!expand_filepath(filename, resolved_path TSRMLS_CC)) {
return -1;
}
zs = zip_source_file(za, resolved_path, offset_start, offset_len);
if (!zs) {
return -1;
}
cur_idx = zip_name_locate(za, (const char *)entry_name, 0);
/* TODO: fix _zip_replace */
if (cur_idx<0) {
/* reset the error */
if (za->error.str) {
_zip_error_fini(&za->error);
}
_zip_error_init(&za->error);
} else {
if (zip_delete(za, cur_idx) == -1) {
zip_source_free(zs);
return -1;
}
}
if (zip_add(za, entry_name, zs) == -1) {
return -1;
} else {
return 1;
}
}
/* }}} */
static int php_zip_parse_options(zval *options, long *remove_all_path,
char **remove_path, int *remove_path_len, char **add_path, int *add_path_len TSRMLS_DC) /* {{{ */
{
zval **option;
if (zend_hash_find(HASH_OF(options), "remove_all_path", sizeof("remove_all_path"), (void **)&option) == SUCCESS) {
long opt;
if (Z_TYPE_PP(option) != IS_LONG) {
zval tmp = **option;
zval_copy_ctor(&tmp);
convert_to_long(&tmp);
opt = Z_LVAL(tmp);
} else {
opt = Z_LVAL_PP(option);
}
*remove_all_path = opt;
}
/* If I add more options, it would make sense to create a nice static struct and loop over it. */
if (zend_hash_find(HASH_OF(options), "remove_path", sizeof("remove_path"), (void **)&option) == SUCCESS) {
if (Z_TYPE_PP(option) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "remove_path option expected to be a string");
return -1;
}
if (Z_STRLEN_PP(option) < 1) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string given as remove_path option");
return -1;
}
if (Z_STRLEN_PP(option) >= MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "remove_path string is too long (max: %i, %i given)",
MAXPATHLEN - 1, Z_STRLEN_PP(option));
return -1;
}
*remove_path_len = Z_STRLEN_PP(option);
*remove_path = Z_STRVAL_PP(option);
}
if (zend_hash_find(HASH_OF(options), "add_path", sizeof("add_path"), (void **)&option) == SUCCESS) {
if (Z_TYPE_PP(option) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_path option expected to be a string");
return -1;
}
if (Z_STRLEN_PP(option) < 1) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string given as the add_path option");
return -1;
}
if (Z_STRLEN_PP(option) >= MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_path string too long (max: %i, %i given)",
MAXPATHLEN - 1, Z_STRLEN_PP(option));
return -1;
}
*add_path_len = Z_STRLEN_PP(option);
*add_path = Z_STRVAL_PP(option);
}
return 1;
}
/* }}} */
/* {{{ REGISTER_ZIP_CLASS_CONST_LONG */
#define REGISTER_ZIP_CLASS_CONST_LONG(const_name, value) \
zend_declare_class_constant_long(zip_class_entry, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
/* }}} */
/* {{{ ZIP_FROM_OBJECT */
#define ZIP_FROM_OBJECT(intern, object) \
{ \
ze_zip_object *obj = (ze_zip_object*) zend_object_store_get_object(object TSRMLS_CC); \
intern = obj->za; \
if (!intern) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or uninitialized Zip object"); \
RETURN_FALSE; \
} \
}
/* }}} */
/* {{{ RETURN_SB(sb) */
#define RETURN_SB(sb) \
{ \
array_init(return_value); \
add_ascii_assoc_string(return_value, "name", (char *)(sb)->name, 1); \
add_ascii_assoc_long(return_value, "index", (long) (sb)->index); \
add_ascii_assoc_long(return_value, "crc", (long) (sb)->crc); \
add_ascii_assoc_long(return_value, "size", (long) (sb)->size); \
add_ascii_assoc_long(return_value, "mtime", (long) (sb)->mtime); \
add_ascii_assoc_long(return_value, "comp_size", (long) (sb)->comp_size); \
add_ascii_assoc_long(return_value, "comp_method", (long) (sb)->comp_method); \
}
/* }}} */
static int php_zip_status(struct zip *za TSRMLS_DC) /* {{{ */
{
int zep, syp;
zip_error_get(za, &zep, &syp);
return zep;
}
/* }}} */
static int php_zip_status_sys(struct zip *za TSRMLS_DC) /* {{{ */
{
int zep, syp;
zip_error_get(za, &zep, &syp);
return syp;
}
/* }}} */
static int php_zip_get_num_files(struct zip *za TSRMLS_DC) /* {{{ */
{
return zip_get_num_files(za);
}
/* }}} */
static char * php_zipobj_get_filename(ze_zip_object *obj TSRMLS_DC) /* {{{ */
{
if (!obj) {
return NULL;
}
if (obj->filename) {
return obj->filename;
}
return NULL;
}
/* }}} */
static char * php_zipobj_get_zip_comment(struct zip *za, int *len TSRMLS_DC) /* {{{ */
{
if (za) {
return (char *)zip_get_archive_comment(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, long flags, zval *return_value TSRMLS_DC) /* {{{ */
{
#ifdef HAVE_GLOB
char cwd[MAXPATHLEN];
int cwd_skip = 0;
#ifdef ZTS
char work_pattern[MAXPATHLEN];
char *result;
#endif
glob_t globbuf;
int n;
int ret;
if (pattern_len >= MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
return -1;
}
if ((GLOB_AVAILABLE_FLAGS & flags) != flags) {
php_error_docref(NULL TSRMLS_CC, 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 */
strncpy(cwd, globbuf.gl_pathv[0], MAXPATHLEN);
if (ZIP_OPENBASEDIR_CHECKPATH(cwd)) {
return -1;
}
array_init(return_value);
for (n = 0; n < globbuf.gl_pathc; n++) {
/* we need to do this everytime 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) {
struct stat s;
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, 1);
}
globfree(&globbuf);
return globbuf.gl_pathc;
#else
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Glob support is not available");
return 0;
#endif /* HAVE_GLOB */
}
/* }}} */
int php_zip_pcre(char *regexp, int regexp_len, char *path, int path_len, zval *return_value TSRMLS_DC) /* {{{ */
{
#ifdef ZTS
char cwd[MAXPATHLEN];
int cwd_skip = 0;
char work_path[MAXPATHLEN];
char *result;
#endif
int files_cnt;
char **namelist;
#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
cwd_skip = strlen(cwd)+1;
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) {
pcre *re = NULL;
pcre_extra *pcre_extra = NULL;
int preg_options = 0, i;
re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC);
if (!re) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid expression");
return -1;
}
array_init(return_value);
/* only the files, directories are ignored */
for (i = 0; i < files_cnt; i++) {
struct stat s;
char fullpath[MAXPATHLEN];
int ovector[3];
int matches;
int namelist_len = strlen(namelist[i]);
if ((namelist_len == 1 && namelist[i][0] == '.') ||
(namelist_len == 2 && namelist[i][0] == '.' && namelist[i][1] == '.')) {
efree(namelist[i]);
continue;
}
if ((path_len + namelist_len + 1) >= MAXPATHLEN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_path string too long (max: %i, %i given)",
MAXPATHLEN - 1, (path_len + namelist_len + 1));
efree(namelist[i]);
break;
}
snprintf(fullpath, MAXPATHLEN, "%s%c%s", path, DEFAULT_SLASH, namelist[i]);
if (0 != VCWD_STAT(fullpath, &s)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot read <%s>", fullpath);
efree(namelist[i]);
continue;
}
if (S_IFDIR == (s.st_mode & S_IFMT)) {
efree(namelist[i]);
continue;
}
matches = pcre_exec(re, NULL, namelist[i], strlen(namelist[i]), 0, 0, ovector, 3);
/* 0 means that the vector is too small to hold all the captured substring offsets */
if (matches < 0) {
efree(namelist[i]);
continue;
}
add_next_index_string(return_value, fullpath, 1);
efree(namelist[i]);
}
efree(namelist);
}
return files_cnt;
}
/* }}} */
#endif
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_close, 0, 0, 1)
ZEND_ARG_INFO(0, zip)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_read, 0, 0, 1)
ZEND_ARG_INFO(0, zip)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_open, 0, 0, 2)
ZEND_ARG_INFO(0, zip_dp)
ZEND_ARG_INFO(0, zip_entry)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_close, 0, 0, 1)
ZEND_ARG_INFO(0, zip_ent)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_read, 0, 0, 1)
ZEND_ARG_INFO(0, zip_entry)
ZEND_ARG_INFO(0, len)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_name, 0, 0, 1)
ZEND_ARG_INFO(0, zip_entry)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_compressedsize, 0, 0, 1)
ZEND_ARG_INFO(0, zip_entry)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_filesize, 0, 0, 1)
ZEND_ARG_INFO(0, zip_entry)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_compressionmethod, 0, 0, 1)
ZEND_ARG_INFO(0, zip_entry)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ zend_function_entry */
static const zend_function_entry zip_functions[] = {
ZEND_RAW_FENTRY("zip_open", zif_zip_open, arginfo_zip_open, 0)
ZEND_RAW_FENTRY("zip_close", zif_zip_close, arginfo_zip_close, 0)
ZEND_RAW_FENTRY("zip_read", zif_zip_read, arginfo_zip_read, 0)
PHP_FE(zip_entry_open, arginfo_zip_entry_open)
PHP_FE(zip_entry_close, arginfo_zip_entry_close)
PHP_FE(zip_entry_read, arginfo_zip_entry_read)
PHP_FE(zip_entry_filesize, arginfo_zip_entry_filesize)
PHP_FE(zip_entry_name, arginfo_zip_entry_name)
PHP_FE(zip_entry_compressedsize, arginfo_zip_entry_compressedsize)
PHP_FE(zip_entry_compressionmethod, arginfo_zip_entry_compressionmethod)
PHP_FE_END
};
/* }}} */
/* {{{ ZE2 OO definitions */
#ifdef PHP_ZIP_USE_OO
static zend_class_entry *zip_class_entry;
static zend_object_handlers zip_object_handlers;
static HashTable zip_prop_handlers;
typedef int (*zip_read_int_t)(struct zip *za TSRMLS_DC);
typedef char *(*zip_read_const_char_t)(struct zip *za, int *len TSRMLS_DC);
typedef char *(*zip_read_const_char_from_ze_t)(ze_zip_object *obj TSRMLS_DC);
typedef struct _zip_prop_handler {
zip_read_int_t read_int_func;
zip_read_const_char_t read_const_char_func;
zip_read_const_char_from_ze_t read_const_char_from_obj_func;
int type;
} zip_prop_handler;
#endif
/* }}} */
#ifdef PHP_ZIP_USE_OO
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, zip_read_const_char_from_ze_t read_char_from_obj_func, int rettype TSRMLS_DC) /* {{{ */
{
zip_prop_handler hnd;
hnd.read_const_char_func = read_char_func;
hnd.read_int_func = read_int_func;
hnd.read_const_char_from_obj_func = read_char_from_obj_func;
hnd.type = rettype;
zend_hash_add(prop_handler, name, strlen(name)+1, &hnd, sizeof(zip_prop_handler), NULL);
}
/* }}} */
static int php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd, zval **retval, int newzval TSRMLS_DC) /* {{{ */
{
const char *retchar = NULL;
int retint = 0;
int len = 0;
if (obj && obj->za != NULL) {
if (hnd->read_const_char_func) {
retchar = hnd->read_const_char_func(obj->za, &len TSRMLS_CC);
} else {
if (hnd->read_int_func) {
retint = hnd->read_int_func(obj->za TSRMLS_CC);
if (retint == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal zip error returned");
return FAILURE;
}
} else {
if (hnd->read_const_char_from_obj_func) {
retchar = hnd->read_const_char_from_obj_func(obj TSRMLS_CC);
len = strlen(retchar);
}
}
}
}
if (newzval) {
ALLOC_ZVAL(*retval);
}
switch (hnd->type) {
case IS_STRING:
if (retchar) {
ZVAL_STRINGL(*retval, (char *) retchar, len, 1);
} else {
ZVAL_EMPTY_STRING(*retval);
}
break;
case IS_BOOL:
ZVAL_BOOL(*retval, (long)retint);
break;
case IS_LONG:
ZVAL_LONG(*retval, (long)retint);
break;
default:
ZVAL_NULL(*retval);
}
return SUCCESS;
}
/* }}} */
static zval **php_zip_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ze_zip_object *obj;
zval tmp_member;
zval **retval = NULL;
zip_prop_handler *hnd;
zend_object_handlers *std_hnd;
int ret;
if (member->type != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
key = NULL;
}
ret = FAILURE;
obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
if (obj->prop_handler != NULL) {
if (key) {
ret = zend_hash_quick_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, key->hash_value, (void **) &hnd);
} else {
ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
}
}
if (ret == FAILURE) {
std_hnd = zend_get_std_object_handlers();
retval = std_hnd->get_property_ptr_ptr(object, member, type, key TSRMLS_CC);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
/* }}} */
static zval* php_zip_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ze_zip_object *obj;
zval tmp_member;
zval *retval;
zip_prop_handler *hnd;
zend_object_handlers *std_hnd;
int ret;
if (member->type != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
key = NULL;
}
ret = FAILURE;
obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
if (obj->prop_handler != NULL) {
if (key) {
ret = zend_hash_quick_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, key->hash_value, (void **) &hnd);
} else {
ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
}
}
if (ret == SUCCESS) {
ret = php_zip_property_reader(obj, hnd, &retval, 1 TSRMLS_CC);
if (ret == SUCCESS) {
/* ensure we're creating a temporary variable */
Z_SET_REFCOUNT_P(retval, 0);
} else {
retval = EG(uninitialized_zval_ptr);
}
} else {
std_hnd = zend_get_std_object_handlers();
retval = std_hnd->read_property(object, member, type, key TSRMLS_CC);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
/* }}} */
static int php_zip_has_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC) /* {{{ */
{
ze_zip_object *obj;
zval tmp_member;
zip_prop_handler *hnd;
zend_object_handlers *std_hnd;
int ret, retval = 0;
if (member->type != IS_STRING) {
tmp_member = *member;
zval_copy_ctor(&tmp_member);
convert_to_string(&tmp_member);
member = &tmp_member;
key = NULL;
}
ret = FAILURE;
obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
if (obj->prop_handler != NULL) {
if (key) {
ret = zend_hash_quick_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, key->hash_value, (void **) &hnd);
} else {
ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
}
}
if (ret == SUCCESS) {
zval *tmp;
ALLOC_INIT_ZVAL(tmp);
if (type == 2) {
retval = 1;
} else if (php_zip_property_reader(obj, hnd, &tmp, 0 TSRMLS_CC) == SUCCESS) {
Z_SET_REFCOUNT_P(tmp, 1);
Z_UNSET_ISREF_P(tmp);
if (type == 1) {
retval = zend_is_true(tmp);
} else if (type == 0) {
retval = (Z_TYPE_P(tmp) != IS_NULL);
}
}
zval_ptr_dtor(&tmp);
} else {
std_hnd = zend_get_std_object_handlers();