attempt to fix PHP-5
authorMichael Wallner <mike@php.net>
Fri, 17 Sep 2021 10:42:03 +0000 (12:42 +0200)
committerMichael Wallner <mike@php.net>
Fri, 17 Sep 2021 10:42:03 +0000 (12:42 +0200)
php_json_post.c
php_json_post.h

index 94e0e36eb9cc996811df4560dcb39d5232861c95..6922b5107045e7750e928f252157ff7cbb59e72f 100644 (file)
@@ -41,16 +41,12 @@ static void php_json_post_init_globals(zend_json_post_globals *json_post_globals
 #endif
 }
 
 #endif
 }
 
-#if PHP_VERSION_ID < 70000
-ZEND_EXTERN_MODULE_GLOBALS(json);
-#endif
-
 #ifndef TSRMLS_CC
 #ifndef TSRMLS_CC
+#      define TSRMLS_D
 #      define TSRMLS_C
 #      define TSRMLS_CC
 #endif
 
 #      define TSRMLS_C
 #      define TSRMLS_CC
 #endif
 
-
 PHP_MINFO_FUNCTION(json_post)
 {
        php_info_print_table_start();
 PHP_MINFO_FUNCTION(json_post)
 {
        php_info_print_table_start();
@@ -60,6 +56,24 @@ PHP_MINFO_FUNCTION(json_post)
        DISPLAY_INI_ENTRIES();
 }
 
        DISPLAY_INI_ENTRIES();
 }
 
+#if PHP_VERSION_ID < 70000
+#      define JSON_POST_LAST_ERROR() json_post_last_error(TSRMLS_C)
+static inline int json_post_last_error(TSRMLS_D)
+{
+       zend_long l;
+       zval *zv, **zv_ptr = &zv;
+
+       ALLOC_ZVAL(zv);
+       JSON_POST_G(json_last_error)->internal_function.handler(0, zv, zv_ptr, NULL, 1);
+       l = Z_LVAL_P(zv);
+       FREE_ZVAL(zv);
+
+       return l;
+}
+#else
+#      define JSON_POST_LAST_ERROR() JSON_G(error_code)
+#endif
+
 static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
 {
        int module_number = 0;
 static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
 {
        int module_number = 0;
@@ -149,21 +163,23 @@ static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
 #      endif
 #endif
 
 #      endif
 #endif
 
-       REGISTER_LONG_CONSTANT("JSON_POST_ERROR", JSON_G(error_code), CONST_CS);
+       REGISTER_LONG_CONSTANT("JSON_POST_ERROR", JSON_POST_LAST_ERROR(), CONST_CS);
 
 
-       if (JSON_G(error_code)) {
+       if (JSON_POST_LAST_ERROR()) {
                if (JSON_POST_G(onerror.response)) {
                        sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (zend_long) JSON_POST_G(onerror.response) TSRMLS_CC);
                }
                if (JSON_POST_G(onerror.warning)) {
                if (JSON_POST_G(onerror.response)) {
                        sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (zend_long) JSON_POST_G(onerror.response) TSRMLS_CC);
                }
                if (JSON_POST_G(onerror.warning)) {
-                       zend_error(E_WARNING, "json_post: json_decode failed with error code: %d", JSON_G(error_code));
+                       zend_error(E_WARNING, "json_post: json_decode failed with error code: %d", JSON_POST_LAST_ERROR());
                }
                if (JSON_POST_G(onerror.exit)) {
                        sapi_send_headers(TSRMLS_C);
                        zend_bailout();
                }
                }
                if (JSON_POST_G(onerror.exit)) {
                        sapi_send_headers(TSRMLS_C);
                        zend_bailout();
                }
+#if PHP_VERSION_ID >= 70000 && PHP_VERSION_ID < 80000
                /* ext/json in PHP-7 fails to reset error_code in RINIT */
                JSON_G(error_code) = 0;
                /* ext/json in PHP-7 fails to reset error_code in RINIT */
                JSON_G(error_code) = 0;
+#endif
        }
 }
 
        }
 }
 
@@ -178,6 +194,11 @@ PHP_MINIT_FUNCTION(json_post)
        sapi_register_post_entries(json_post_entries TSRMLS_CC);
 
        ZEND_INIT_MODULE_GLOBALS(json_post, php_json_post_init_globals, NULL);
        sapi_register_post_entries(json_post_entries TSRMLS_CC);
 
        ZEND_INIT_MODULE_GLOBALS(json_post, php_json_post_init_globals, NULL);
+
+#if PHP_VERSION_ID < 70000
+       zend_hash_find(EG(function_table), ZEND_STRS("json_last_error"), (void **) &JSON_POST_G(json_last_error));
+#endif
+
        REGISTER_INI_ENTRIES();
        return SUCCESS;
 }
        REGISTER_INI_ENTRIES();
        return SUCCESS;
 }
index 2895b70256978f6ce3d1fde52a8578e5a991776b..9623a449a83498bed2b14d5082ab903a683b8fc4 100644 (file)
@@ -41,6 +41,9 @@ ZEND_BEGIN_MODULE_GLOBALS(json_post)
                zend_bool warning;
                zend_bool exit;
        } onerror;
                zend_bool warning;
                zend_bool exit;
        } onerror;
+#if PHP_VERSION_ID < 70000
+       zend_function *json_last_error;
+#endif
 ZEND_END_MODULE_GLOBALS(json_post)
 
 ZEND_EXTERN_MODULE_GLOBALS(json_post);
 ZEND_END_MODULE_GLOBALS(json_post)
 
 ZEND_EXTERN_MODULE_GLOBALS(json_post);