@@ -331,18 +331,60 @@ static int php_zip_add_file(ze_zip_object *obj, const char *filename, size_t fil
331
331
}
332
332
/* }}} */
333
333
334
- static int php_zip_parse_options (zval * options , zend_long * remove_all_path ,
335
- char * * remove_path , size_t * remove_path_len ,
336
- char * * add_path , size_t * add_path_len ,
337
- zend_long * flags
338
- ) /* {{{ */
334
+ typedef struct {
335
+ zend_long remove_all_path ;
336
+ char * remove_path ;
337
+ size_t remove_path_len ;
338
+ char * add_path ;
339
+ size_t add_path_len ;
340
+ zip_flags_t flags ;
341
+ zip_int32_t comp_method ;
342
+ zip_uint32_t comp_flags ;
343
+ #ifdef HAVE_ENCRYPTION
344
+ zip_int16_t enc_method ;
345
+ char * enc_password ;
346
+ #endif
347
+ } zip_options ;
348
+
349
+ static int php_zip_parse_options (zval * options , zip_options * opts )
350
+ /* {{{ */
339
351
{
340
352
zval * option ;
353
+
354
+ /* default values */
355
+ memset (opts , 0 , sizeof (zip_options ));
356
+ opts -> flags = ZIP_FL_OVERWRITE ;
357
+ opts -> comp_method = -1 ; /* -1 to not change default */
358
+ #ifdef HAVE_ENCRYPTION
359
+ opts -> enc_method = -1 ; /* -1 to not change default */
360
+ #endif
361
+
341
362
if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "remove_all_path" , sizeof ("remove_all_path" ) - 1 )) != NULL ) {
342
- * remove_all_path = zval_get_long (option );
363
+ opts -> remove_all_path = zval_get_long (option );
364
+ }
365
+
366
+ if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "comp_method" , sizeof ("comp_method" ) - 1 )) != NULL ) {
367
+ opts -> comp_method = zval_get_long (option );
368
+
369
+ if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "comp_flags" , sizeof ("comp_flags" ) - 1 )) != NULL ) {
370
+ opts -> comp_flags = zval_get_long (option );
371
+ }
372
+ }
373
+
374
+ #ifdef HAVE_ENCRYPTION
375
+ if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "enc_method" , sizeof ("enc_method" ) - 1 )) != NULL ) {
376
+ opts -> enc_method = zval_get_long (option );
377
+
378
+ if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "enc_password" , sizeof ("enc_password" ) - 1 )) != NULL ) {
379
+ if (Z_TYPE_P (option ) != IS_STRING ) {
380
+ php_error_docref (NULL , E_WARNING , "enc_password option expected to be a string" );
381
+ return -1 ;
382
+ }
383
+ opts -> enc_password = Z_STRVAL_P (option );
384
+ }
343
385
}
386
+ #endif
344
387
345
- /* If I add more options, it would make sense to create a nice static struct and loop over it. */
346
388
if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "remove_path" , sizeof ("remove_path" ) - 1 )) != NULL ) {
347
389
if (Z_TYPE_P (option ) != IS_STRING ) {
348
390
php_error_docref (NULL , E_WARNING , "remove_path option expected to be a string" );
@@ -359,8 +401,8 @@ static int php_zip_parse_options(zval *options, zend_long *remove_all_path,
359
401
MAXPATHLEN - 1 , Z_STRLEN_P (option ));
360
402
return -1 ;
361
403
}
362
- * remove_path_len = Z_STRLEN_P (option );
363
- * remove_path = Z_STRVAL_P (option );
404
+ opts -> remove_path_len = Z_STRLEN_P (option );
405
+ opts -> remove_path = Z_STRVAL_P (option );
364
406
}
365
407
366
408
if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "add_path" , sizeof ("add_path" ) - 1 )) != NULL ) {
@@ -379,16 +421,16 @@ static int php_zip_parse_options(zval *options, zend_long *remove_all_path,
379
421
MAXPATHLEN - 1 , Z_STRLEN_P (option ));
380
422
return -1 ;
381
423
}
382
- * add_path_len = Z_STRLEN_P (option );
383
- * add_path = Z_STRVAL_P (option );
424
+ opts -> add_path_len = Z_STRLEN_P (option );
425
+ opts -> add_path = Z_STRVAL_P (option );
384
426
}
385
427
386
428
if ((option = zend_hash_str_find (Z_ARRVAL_P (options ), "flags" , sizeof ("flags" ) - 1 )) != NULL ) {
387
429
if (Z_TYPE_P (option ) != IS_LONG ) {
388
430
php_error_docref (NULL , E_WARNING , "flags option expected to be a integer" );
389
431
return -1 ;
390
432
}
391
- * flags = Z_LVAL_P (option );
433
+ opts -> flags = Z_LVAL_P (option );
392
434
}
393
435
394
436
return 1 ;
@@ -1652,13 +1694,10 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
1652
1694
{
1653
1695
zval * self = ZEND_THIS ;
1654
1696
char * path = "." ;
1655
- char * remove_path = NULL ;
1656
- char * add_path = NULL ;
1657
- size_t add_path_len , remove_path_len = 0 , path_len = 1 ;
1658
- zend_long remove_all_path = 0 ;
1697
+ size_t path_len = 1 ;
1659
1698
zend_long glob_flags = 0 ;
1660
- zend_long zip_flags = ZIP_FL_OVERWRITE ;
1661
1699
zval * options = NULL ;
1700
+ zip_options opts ;
1662
1701
int found ;
1663
1702
zend_string * pattern ;
1664
1703
@@ -1679,8 +1718,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
1679
1718
php_error_docref (NULL , E_NOTICE , "Empty string as pattern" );
1680
1719
RETURN_FALSE ;
1681
1720
}
1682
- if (options && (php_zip_parse_options (options , & remove_all_path , & remove_path , & remove_path_len ,
1683
- & add_path , & add_path_len , & zip_flags ) < 0 )) {
1721
+ if (options && (php_zip_parse_options (options , & opts ) < 0 )) {
1684
1722
RETURN_FALSE ;
1685
1723
}
1686
1724
@@ -1693,6 +1731,9 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
1693
1731
if (found > 0 ) {
1694
1732
int i ;
1695
1733
zval * zval_file ;
1734
+ ze_zip_object * ze_obj ;
1735
+
1736
+ ze_obj = Z_ZIP_P (self );
1696
1737
1697
1738
for (i = 0 ; i < found ; i ++ ) {
1698
1739
char * file_stripped , * entry_name ;
@@ -1701,31 +1742,31 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
1701
1742
zend_string * basename = NULL ;
1702
1743
1703
1744
if ((zval_file = zend_hash_index_find (Z_ARRVAL_P (return_value ), i )) != NULL ) {
1704
- if (remove_all_path ) {
1745
+ if (opts . remove_all_path ) {
1705
1746
basename = php_basename (Z_STRVAL_P (zval_file ), Z_STRLEN_P (zval_file ), NULL , 0 );
1706
1747
file_stripped = ZSTR_VAL (basename );
1707
1748
file_stripped_len = ZSTR_LEN (basename );
1708
- } else if (remove_path && strstr (Z_STRVAL_P (zval_file ), remove_path ) != NULL ) {
1709
- if (IS_SLASH (Z_STRVAL_P (zval_file )[remove_path_len ])) {
1710
- file_stripped = Z_STRVAL_P (zval_file ) + remove_path_len + 1 ;
1711
- file_stripped_len = Z_STRLEN_P (zval_file ) - remove_path_len - 1 ;
1749
+ } else if (opts . remove_path && strstr (Z_STRVAL_P (zval_file ), opts . remove_path ) != NULL ) {
1750
+ if (IS_SLASH (Z_STRVAL_P (zval_file )[opts . remove_path_len ])) {
1751
+ file_stripped = Z_STRVAL_P (zval_file ) + opts . remove_path_len + 1 ;
1752
+ file_stripped_len = Z_STRLEN_P (zval_file ) - opts . remove_path_len - 1 ;
1712
1753
} else {
1713
- file_stripped = Z_STRVAL_P (zval_file ) + remove_path_len ;
1714
- file_stripped_len = Z_STRLEN_P (zval_file ) - remove_path_len ;
1754
+ file_stripped = Z_STRVAL_P (zval_file ) + opts . remove_path_len ;
1755
+ file_stripped_len = Z_STRLEN_P (zval_file ) - opts . remove_path_len ;
1715
1756
}
1716
1757
} else {
1717
1758
file_stripped = Z_STRVAL_P (zval_file );
1718
1759
file_stripped_len = Z_STRLEN_P (zval_file );
1719
1760
}
1720
1761
1721
- if (add_path ) {
1722
- if ((add_path_len + file_stripped_len ) > MAXPATHLEN ) {
1762
+ if (opts . add_path ) {
1763
+ if ((opts . add_path_len + file_stripped_len ) > MAXPATHLEN ) {
1723
1764
php_error_docref (NULL , E_WARNING , "Entry name too long (max: %d, %zd given)" ,
1724
- MAXPATHLEN - 1 , (add_path_len + file_stripped_len ));
1765
+ MAXPATHLEN - 1 , (opts . add_path_len + file_stripped_len ));
1725
1766
zend_array_destroy (Z_ARR_P (return_value ));
1726
1767
RETURN_FALSE ;
1727
1768
}
1728
- snprintf (entry_name_buf , MAXPATHLEN , "%s%s" , add_path , file_stripped );
1769
+ snprintf (entry_name_buf , MAXPATHLEN , "%s%s" , opts . add_path , file_stripped );
1729
1770
} else {
1730
1771
snprintf (entry_name_buf , MAXPATHLEN , "%s" , file_stripped );
1731
1772
}
@@ -1737,11 +1778,25 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
1737
1778
basename = NULL ;
1738
1779
}
1739
1780
1740
- if (php_zip_add_file (Z_ZIP_P ( self ) , Z_STRVAL_P (zval_file ), Z_STRLEN_P (zval_file ),
1741
- entry_name , entry_name_len , 0 , 0 , -1 , zip_flags ) < 0 ) {
1781
+ if (php_zip_add_file (ze_obj , Z_STRVAL_P (zval_file ), Z_STRLEN_P (zval_file ),
1782
+ entry_name , entry_name_len , 0 , 0 , -1 , opts . flags ) < 0 ) {
1742
1783
zend_array_destroy (Z_ARR_P (return_value ));
1743
1784
RETURN_FALSE ;
1744
1785
}
1786
+ if (opts .comp_method >= 0 ) {
1787
+ if (zip_set_file_compression (ze_obj -> za , ze_obj -> last_id , opts .comp_method , opts .comp_flags )) {
1788
+ zend_array_destroy (Z_ARR_P (return_value ));
1789
+ RETURN_FALSE ;
1790
+ }
1791
+ }
1792
+ #ifdef HAVE_ENCRYPTION
1793
+ if (opts .enc_method >= 0 ) {
1794
+ if (zip_file_set_encryption (ze_obj -> za , ze_obj -> last_id , opts .enc_method , opts .enc_password )) {
1795
+ zend_array_destroy (Z_ARR_P (return_value ));
1796
+ RETURN_FALSE ;
1797
+ }
1798
+ }
1799
+ #endif
1745
1800
}
1746
1801
}
1747
1802
}
0 commit comments