]> granicus.if.org Git - php/commitdiff
- add ZipArchive::EM_UNKNOWN constant - add ZipArchive::EM_TRAD_PKWARE constant ...
authorRemi Collet <remi@remirepo.net>
Thu, 2 Apr 2020 12:40:05 +0000 (14:40 +0200)
committerRemi Collet <remi@php.net>
Thu, 2 Apr 2020 12:52:05 +0000 (14:52 +0200)
ext/zip/config.m4
ext/zip/php_zip.c
ext/zip/php_zip.stub.php
ext/zip/php_zip_arginfo.h
ext/zip/tests/oo_supported.phpt [new file with mode: 0644]

index abd84ff51d6acbf9cce2a1c574bfcdf7aab43352..308020af631c8db2f2e62db61da473a2e8e37680 100644 (file)
@@ -51,6 +51,14 @@ if test "$PHP_ZIP" != "no"; then
     $LIBZIP_LIBS
   ])
 
+  PHP_CHECK_LIBRARY(zip, zip_compression_method_supported,
+  [
+    AC_DEFINE(HAVE_METHOD_SUPPORTED, 1, [Libzip >= 1.7.0 with zip_*_method_supported functions])
+  ], [
+  ], [
+    $LIBZIP_LIBS
+  ])
+
   AC_DEFINE(HAVE_ZIP,1,[ ])
 
   PHP_ZIP_SOURCES="php_zip.c zip_stream.c"
index 02e64f64ae24546dedf3979413975f18a7289b9a..3b9b07097e50b8e41234bd50cd189b360724dfb8 100644 (file)
@@ -1039,12 +1039,8 @@ static void php_zip_object_free_storage(zend_object *object) /* {{{ */
        }
        if (intern->za) {
                if (zip_close(intern->za) != 0) {
-#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
-                       php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: zip_close have failed");
-#else
                        php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", zip_strerror(intern->za));
                        zip_discard(intern->za);
-#endif
                }
        }
 
@@ -1561,9 +1557,6 @@ static ZIPARCHIVE_METHOD(close)
 
        err = zip_close(intern);
        if (err) {
-#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
-               php_error_docref(NULL, E_WARNING, "zip_close have failed");
-#else
                php_error_docref(NULL, E_WARNING, "%s", zip_strerror(intern));
                /* Save error for property reader */
                #if LIBZIP_VERSION_MAJOR < 1
@@ -1579,7 +1572,6 @@ static ZIPARCHIVE_METHOD(close)
                        }
                #endif
                zip_discard(intern);
-#endif
        } else {
                ze_obj->err_zip = 0;
                ze_obj->err_sys = 0;
@@ -3064,6 +3056,36 @@ static ZIPARCHIVE_METHOD(registerCancelCallback)
 /* }}} */
 #endif
 
+#ifdef HAVE_METHOD_SUPPORTED
+/* {{{ proto bool ZipArchive::isCompressionMethodSupported(int method, bool enc)
+check if a compression method is available in used libzip */
+static ZIPARCHIVE_METHOD(isCompressionMethodSupported)
+{
+       zend_long method;
+       zend_bool enc = 1;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
+               return;
+       }
+       RETVAL_BOOL(zip_compression_method_supported((zip_int32_t)method, enc));
+}
+/* }}} */
+
+/* {{{ proto bool ZipArchive::isEncryptionMethodSupported(int method, bool enc)
+check if a encryption method is available in used libzip */
+static ZIPARCHIVE_METHOD(isEncryptionMethodSupported)
+{
+       zend_long method;
+       zend_bool enc = 1;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
+               return;
+       }
+       RETVAL_BOOL(zip_encryption_method_supported((zip_uint16_t)method, enc));
+}
+/* }}} */
+#endif
+
 /* {{{ ze_zip_object_class_functions */
 static const zend_function_entry zip_class_functions[] = {
        ZIPARCHIVE_ME(open,                                     arginfo_class_ZipArchive_open, ZEND_ACC_PUBLIC)
@@ -3121,6 +3143,10 @@ static const zend_function_entry zip_class_functions[] = {
 #ifdef HAVE_CANCEL_CALLBACK
        ZIPARCHIVE_ME(registerCancelCallback,   arginfo_class_ZipArchive_registerCancelCallback, ZEND_ACC_PUBLIC)
 #endif
+#ifdef HAVE_METHOD_SUPPORTED
+       ZIPARCHIVE_ME(isCompressionMethodSupported, arginfo_class_ZipArchive_isCompressionMethodSupported, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
+       ZIPARCHIVE_ME(isEncryptionMethodSupported,  arginfo_class_ZipArchive_isEncryptionMethodSupported,  ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
+#endif
 
        PHP_FE_END
 };
@@ -3280,12 +3306,14 @@ static PHP_MINIT_FUNCTION(zip)
        REGISTER_ZIP_CLASS_CONST_LONG("OPSYS_DEFAULT", ZIP_OPSYS_DEFAULT);
 #endif /* ifdef ZIP_OPSYS_DEFAULT */
 
-#ifdef HAVE_ENCRYPTION
        REGISTER_ZIP_CLASS_CONST_LONG("EM_NONE",                                ZIP_EM_NONE);
+       REGISTER_ZIP_CLASS_CONST_LONG("EM_TRAD_PKWARE",                 ZIP_EM_TRAD_PKWARE);
+#ifdef HAVE_ENCRYPTION
        REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_128",                             ZIP_EM_AES_128);
        REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_192",                             ZIP_EM_AES_192);
        REGISTER_ZIP_CLASS_CONST_LONG("EM_AES_256",                             ZIP_EM_AES_256);
 #endif
+       REGISTER_ZIP_CLASS_CONST_LONG("EM_UNKNOWN",                             ZIP_EM_UNKNOWN);
 
 #if HAVE_LIBZIP_VERSION
        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)
 #else
        php_info_print_table_row(2, "Libzip version", LIBZIP_VERSION);
 #endif
+#ifdef HAVE_METHOD_SUPPORTED
+       php_info_print_table_row(2, "BZIP2 compression",
+               zip_compression_method_supported(ZIP_CM_BZIP2, 1) ? "Yes" : "No");
+       php_info_print_table_row(2, "XZ compression",
+               zip_compression_method_supported(ZIP_CM_XZ, 1) ? "Yes" : "No");
+       php_info_print_table_row(2, "AES-128 encryption",
+               zip_encryption_method_supported(ZIP_EM_AES_128, 1) ? "Yes" : "No");
+       php_info_print_table_row(2, "AES-192 encryption",
+               zip_encryption_method_supported(ZIP_EM_AES_128, 1) ? "Yes" : "No");
+       php_info_print_table_row(2, "AES-256 encryption",
+               zip_encryption_method_supported(ZIP_EM_AES_128, 1) ? "Yes" : "No");
+#endif
 
        php_info_print_table_end();
 }
index 1e3ceb6d073b089dd291d4cb5f897a9260821577..101afcbe8ee490d033d4915cdaab447d02b50234 100644 (file)
@@ -186,4 +186,12 @@ class ZipArchive
     /** @return bool */
     public function registerCancelCallback(callable $callback) {}
 #endif
+
+#ifdef HAVE_METHOD_SUPPORTED
+    /** @return bool */
+    public static function isCompressionMethodSupported(int $method, bool $enc): bool {}
+
+    /** @return bool */
+    public static function isEncryptionMethodSupported(int $method, bool $enc): bool {}
+#endif
 }
index a90b7681719ea2a10f6d28f106f3586ed6f7fee5..ffb3983527cb3071908d7579142efbacb186b5b3 100644 (file)
@@ -269,3 +269,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ZipArchive_registerCancelCallback, 0, 0, 1)
        ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
 ZEND_END_ARG_INFO()
 #endif
+
+#if defined(HAVE_METHOD_SUPPORTED)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ZipArchive_isCompressionMethodSupported, 0, 2, _IS_BOOL, 0)
+       ZEND_ARG_TYPE_INFO(0, method, IS_LONG, 0)
+       ZEND_ARG_TYPE_INFO(0, enc, _IS_BOOL, 0)
+ZEND_END_ARG_INFO()
+#endif
+
+#if defined(HAVE_METHOD_SUPPORTED)
+#define arginfo_class_ZipArchive_isEncryptionMethodSupported arginfo_class_ZipArchive_isCompressionMethodSupported
+#endif
diff --git a/ext/zip/tests/oo_supported.phpt b/ext/zip/tests/oo_supported.phpt
new file mode 100644 (file)
index 0000000..02b169b
--- /dev/null
@@ -0,0 +1,65 @@
+--TEST--
+ziparchive::properties isset()/empty() checks
+--SKIPIF--
+<?php
+/* $Id$ */
+if(!extension_loaded('zip')) die('skip');
+if (!method_exists('ZipArchive', 'isCompressionMethodSupported')) die('skip needs libzip >= 1.7');
+?>
+--FILE--
+<?php
+$methods = [
+       ZipArchive::CM_STORE      => "STORE",
+       ZipArchive::CM_DEFLATE    => "DEFLATE",
+       ZipArchive::CM_BZIP2      => "BZIP2",
+       ZipArchive::CM_XZ         => "XZ",
+];
+foreach($methods as $method => $name) {
+       echo "Compression $name\n";
+       var_dump(ZipArchive::isCompressionMethodSupported($method));
+       var_dump(ZipArchive::isCompressionMethodSupported($method, false));
+}
+
+$methods = [
+       ZipArchive::EM_NONE        => "NONE",
+       ZipArchive::EM_TRAD_PKWARE => "TRAD_PKWARE",
+       ZipArchive::EM_AES_128     => "AES-128",
+       ZipArchive::EM_AES_192     => "AES-192",
+       ZipArchive::EM_AES_256     => "AES-256",
+];
+foreach($methods as $method => $name) {
+       echo "Encryption $name\n";
+       var_dump(ZipArchive::isEncryptionMethodSupported($method));
+       var_dump(ZipArchive::isEncryptionMethodSupported($method, false));
+}
+?>
+Done
+--EXPECTF--
+Compression STORE
+bool(true)
+bool(true)
+Compression DEFLATE
+bool(true)
+bool(true)
+Compression BZIP2
+bool(%s)
+bool(%s)
+Compression XZ
+bool(%s)
+bool(%s)
+Encryption NONE
+bool(true)
+bool(true)
+Encryption TRAD_PKWARE
+bool(true)
+bool(true)
+Encryption AES-128
+bool(%s)
+bool(%s)
+Encryption AES-192
+bool(%s)
+bool(%s)
+Encryption AES-256
+bool(%s)
+bool(%s)
+Done