Skip to content

Commit 2dc8d39

Browse files
committed
- add ZipArchive::EM_UNKNOWN constant - add ZipArchive::EM_TRAD_PKWARE constant - cleanup hack for libzip 1.3.1 (have only exist for a few days) - add ZipArchive::isCompressionMethodSupported() method (libzip 1.7.0) - add ZipArchive::isEncryptionMethodSupported() method (libzip 1.7.0) - bump version to 1.19.0-dev
1 parent 6983ae7 commit 2dc8d39

File tree

5 files changed

+141
-9
lines changed

5 files changed

+141
-9
lines changed

ext/zip/config.m4

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ if test "$PHP_ZIP" != "no"; then
5151
$LIBZIP_LIBS
5252
])
5353

54+
PHP_CHECK_LIBRARY(zip, zip_compression_method_supported,
55+
[
56+
AC_DEFINE(HAVE_METHOD_SUPPORTED, 1, [Libzip >= 1.7.0 with zip_*_method_supported functions])
57+
], [
58+
], [
59+
$LIBZIP_LIBS
60+
])
61+
5462
AC_DEFINE(HAVE_ZIP,1,[ ])
5563

5664
PHP_ZIP_SOURCES="php_zip.c zip_stream.c"

ext/zip/php_zip.c

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,12 +1039,8 @@ static void php_zip_object_free_storage(zend_object *object) /* {{{ */
10391039
}
10401040
if (intern->za) {
10411041
if (zip_close(intern->za) != 0) {
1042-
#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
1043-
php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: zip_close have failed");
1044-
#else
10451042
php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", zip_strerror(intern->za));
10461043
zip_discard(intern->za);
1047-
#endif
10481044
}
10491045
}
10501046

@@ -1561,9 +1557,6 @@ static ZIPARCHIVE_METHOD(close)
15611557

15621558
err = zip_close(intern);
15631559
if (err) {
1564-
#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
1565-
php_error_docref(NULL, E_WARNING, "zip_close have failed");
1566-
#else
15671560
php_error_docref(NULL, E_WARNING, "%s", zip_strerror(intern));
15681561
/* Save error for property reader */
15691562
#if LIBZIP_VERSION_MAJOR < 1
@@ -1579,7 +1572,6 @@ static ZIPARCHIVE_METHOD(close)
15791572
}
15801573
#endif
15811574
zip_discard(intern);
1582-
#endif
15831575
} else {
15841576
ze_obj->err_zip = 0;
15851577
ze_obj->err_sys = 0;
@@ -3064,6 +3056,36 @@ static ZIPARCHIVE_METHOD(registerCancelCallback)
30643056
/* }}} */
30653057
#endif
30663058

3059+
#ifdef HAVE_METHOD_SUPPORTED
3060+
/* {{{ proto bool ZipArchive::isCompressionMethodSupported(int method, bool enc)
3061+
check if a compression method is available in used libzip */
3062+
static ZIPARCHIVE_METHOD(isCompressionMethodSupported)
3063+
{
3064+
zend_long method;
3065+
zend_bool enc = 1;
3066+
3067+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
3068+
return;
3069+
}
3070+
RETVAL_BOOL(zip_compression_method_supported((zip_int32_t)method, enc));
3071+
}
3072+
/* }}} */
3073+
3074+
/* {{{ proto bool ZipArchive::isEncryptionMethodSupported(int method, bool enc)
3075+
check if a encryption method is available in used libzip */
3076+
static ZIPARCHIVE_METHOD(isEncryptionMethodSupported)
3077+
{
3078+
zend_long method;
3079+
zend_bool enc = 1;
3080+
3081+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
3082+
return;
3083+
}
3084+
RETVAL_BOOL(zip_encryption_method_supported((zip_uint16_t)method, enc));
3085+
}
3086+
/* }}} */
3087+
#endif
3088+
30673089
/* {{{ ze_zip_object_class_functions */
30683090
static const zend_function_entry zip_class_functions[] = {
30693091
ZIPARCHIVE_ME(open, arginfo_class_ZipArchive_open, ZEND_ACC_PUBLIC)
@@ -3121,6 +3143,10 @@ static const zend_function_entry zip_class_functions[] = {
31213143
#ifdef HAVE_CANCEL_CALLBACK
31223144
ZIPARCHIVE_ME(registerCancelCallback, arginfo_class_ZipArchive_registerCancelCallback, ZEND_ACC_PUBLIC)
31233145
#endif
3146+
#ifdef HAVE_METHOD_SUPPORTED
3147+
ZIPARCHIVE_ME(isCompressionMethodSupported, arginfo_class_ZipArchive_isCompressionMethodSupported, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
3148+
ZIPARCHIVE_ME(isEncryptionMethodSupported, arginfo_class_ZipArchive_isEncryptionMethodSupported, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
3149+
#endif
31243150

31253151
PHP_FE_END
31263152
};
@@ -3280,12 +3306,14 @@ static PHP_MINIT_FUNCTION(zip)
32803306
REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_DEFAULT", ZIP_OPSYS_DEFAULT);
32813307
#endif /* ifdef ZIP_OPSYS_DEFAULT */
32823308

3283-
#ifdef HAVE_ENCRYPTION
32843309
REGISTER_ZIP_CLASS_CONST_LONG("EM_NONE", ZIP_EM_NONE);
3310+
REGISTER_ZIP_CLASS_CONST_LONG("EM_TRAD_PKWARE", ZIP_EM_TRAD_PKWARE);
3311+
#ifdef HAVE_ENCRYPTION
32853312
REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_128", ZIP_EM_AES_128);
32863313
REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_192", ZIP_EM_AES_192);
32873314
REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_256", ZIP_EM_AES_256);
32883315
#endif
3316+
REGISTER_ZIP_CLASS_CONST_LONG("EM_UNKNOWN", ZIP_EM_UNKNOWN);
32893317

32903318
#if HAVE_LIBZIP_VERSION
32913319
zend_declare_class_constant_string(zip_class_entry, "LIBZIP_VERSION", sizeof("LIBZIP_VERSION")-1, zip_libzip_version());
@@ -3326,6 +3354,18 @@ static PHP_MINFO_FUNCTION(zip)
33263354
#else
33273355
php_info_print_table_row(2, "Libzip version", LIBZIP_VERSION);
33283356
#endif
3357+
#ifdef HAVE_METHOD_SUPPORTED
3358+
php_info_print_table_row(2, "BZIP2 compression",
3359+
zip_compression_method_supported(ZIP_CM_BZIP2, 1) ? "Yes" : "No");
3360+
php_info_print_table_row(2, "XZ compression",
3361+
zip_compression_method_supported(ZIP_CM_XZ, 1) ? "Yes" : "No");
3362+
php_info_print_table_row(2, "AES-128 encryption",
3363+
zip_encryption_method_supported(ZIP_EM_AES_128, 1) ? "Yes" : "No");
3364+
php_info_print_table_row(2, "AES-192 encryption",
3365+
zip_encryption_method_supported(ZIP_EM_AES_128, 1) ? "Yes" : "No");
3366+
php_info_print_table_row(2, "AES-256 encryption",
3367+
zip_encryption_method_supported(ZIP_EM_AES_128, 1) ? "Yes" : "No");
3368+
#endif
33293369

33303370
php_info_print_table_end();
33313371
}

ext/zip/php_zip.stub.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,12 @@ public function registerProgressCallback(float $rate, callable $callback) {}
186186
/** @return bool */
187187
public function registerCancelCallback(callable $callback) {}
188188
#endif
189+
190+
#ifdef HAVE_METHOD_SUPPORTED
191+
/** @return bool */
192+
public static function isCompressionMethodSupported(int $method, bool $enc): bool {}
193+
194+
/** @return bool */
195+
public static function isEncryptionMethodSupported(int $method, bool $enc): bool {}
196+
#endif
189197
}

ext/zip/php_zip_arginfo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ZipArchive_registerCancelCallback, 0, 0, 1)
269269
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
270270
ZEND_END_ARG_INFO()
271271
#endif
272+
273+
#if defined(HAVE_METHOD_SUPPORTED)
274+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ZipArchive_isCompressionMethodSupported, 0, 2, _IS_BOOL, 0)
275+
ZEND_ARG_TYPE_INFO(0, method, IS_LONG, 0)
276+
ZEND_ARG_TYPE_INFO(0, enc, _IS_BOOL, 0)
277+
ZEND_END_ARG_INFO()
278+
#endif
279+
280+
#if defined(HAVE_METHOD_SUPPORTED)
281+
#define arginfo_class_ZipArchive_isEncryptionMethodSupported arginfo_class_ZipArchive_isCompressionMethodSupported
282+
#endif

ext/zip/tests/oo_supported.phpt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
ziparchive::properties isset()/empty() checks
3+
--SKIPIF--
4+
<?php
5+
/* $Id$ */
6+
if(!extension_loaded('zip')) die('skip');
7+
if (!method_exists('ZipArchive', 'isCompressionMethodSupported')) die('skip needs libzip >= 1.7');
8+
?>
9+
--FILE--
10+
<?php
11+
$methods = [
12+
ZipArchive::CM_STORE => "STORE",
13+
ZipArchive::CM_DEFLATE => "DEFLATE",
14+
ZipArchive::CM_BZIP2 => "BZIP2",
15+
ZipArchive::CM_XZ => "XZ",
16+
];
17+
foreach($methods as $method => $name) {
18+
echo "Compression $name\n";
19+
var_dump(ZipArchive::isCompressionMethodSupported($method));
20+
var_dump(ZipArchive::isCompressionMethodSupported($method, false));
21+
}
22+
23+
$methods = [
24+
ZipArchive::EM_NONE => "NONE",
25+
ZipArchive::EM_TRAD_PKWARE => "TRAD_PKWARE",
26+
ZipArchive::EM_AES_128 => "AES-128",
27+
ZipArchive::EM_AES_192 => "AES-192",
28+
ZipArchive::EM_AES_256 => "AES-256",
29+
];
30+
foreach($methods as $method => $name) {
31+
echo "Encryption $name\n";
32+
var_dump(ZipArchive::isEncryptionMethodSupported($method));
33+
var_dump(ZipArchive::isEncryptionMethodSupported($method, false));
34+
}
35+
?>
36+
Done
37+
--EXPECTF--
38+
Compression STORE
39+
bool(true)
40+
bool(true)
41+
Compression DEFLATE
42+
bool(true)
43+
bool(true)
44+
Compression BZIP2
45+
bool(%s)
46+
bool(%s)
47+
Compression XZ
48+
bool(%s)
49+
bool(%s)
50+
Encryption NONE
51+
bool(true)
52+
bool(true)
53+
Encryption TRAD_PKWARE
54+
bool(true)
55+
bool(true)
56+
Encryption AES-128
57+
bool(%s)
58+
bool(%s)
59+
Encryption AES-192
60+
bool(%s)
61+
bool(%s)
62+
Encryption AES-256
63+
bool(%s)
64+
bool(%s)
65+
Done

0 commit comments

Comments
 (0)