2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2015, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
20 #include "ext/standard/info.h"
21 #include "ext/json/php_json.h"
22 #include "php_json_post.h"
25 ZEND_DECLARE_MODULE_GLOBALS(json_post
);
28 STD_PHP_INI_ENTRY("json_post.flags", "1", PHP_INI_PERDIR
, OnUpdateLong
, flags
, zend_json_post_globals
, json_post_globals
)
29 STD_PHP_INI_ENTRY("json_post.onerror.response", "0", PHP_INI_PERDIR
, OnUpdateLong
, onerror
.response
, zend_json_post_globals
, json_post_globals
)
30 STD_PHP_INI_ENTRY("json_post.onerror.exit", "0", PHP_INI_PERDIR
, OnUpdateBool
, onerror
.exit
, zend_json_post_globals
, json_post_globals
)
31 STD_PHP_INI_ENTRY("json_post.onerror.warning", "0", PHP_INI_PERDIR
, OnUpdateBool
, onerror
.warning
, zend_json_post_globals
, json_post_globals
)
34 static void php_json_post_init_globals(zend_json_post_globals
*json_post_globals
)
36 memset(json_post_globals
, 0, sizeof(*json_post_globals
));
37 #if PHP_VERSION_ID >= 50400
38 json_post_globals
->flags
= PHP_JSON_OBJECT_AS_ARRAY
;
40 json_post_globals
->flags
= 1;
50 PHP_MINFO_FUNCTION(json_post
)
52 php_info_print_table_start();
53 php_info_print_table_header(2, "json_post support", "enabled");
54 php_info_print_table_end();
56 DISPLAY_INI_ENTRIES();
59 #if PHP_VERSION_ID < 70000
62 # define JSON_G(v) TSRMG(JSON_POST_G(json_module)->globals_id_ptr, zend_json_globals *, v)
64 # define JSON_G(v) ((zend_json_globals *) JSON_POST_G(json_module)->globals_ptr)->v
68 static SAPI_POST_HANDLER_FUNC(php_json_post_handler
)
70 int module_number
= 0;
72 #if PHP_VERSION_ID >= 70000
73 zend_string
*json
= NULL
;
75 if (SG(request_info
).request_body
) {
76 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
77 php_stream_rewind(SG(request_info
).request_body
);
78 json
= php_stream_copy_to_mem(SG(request_info
).request_body
, PHP_STREAM_COPY_ALL
, 0);
84 long flags
= JSON_POST_G(flags
);
86 #ifdef PHP_JSON_THROW_ON_ERROR
87 /* there's no execute data, so we must ensure json_decode() is not throwing */
88 flags
&= ~PHP_JSON_THROW_ON_ERROR
;
92 php_json_decode_ex(&tmp
, json
->val
, json
->len
, flags
, PG(max_input_nesting_level
));
94 switch (Z_TYPE(tmp
)) {
97 if (zend_hash_num_elements(HASH_OF(&tmp
))) {
99 ZVAL_COPY_VALUE(&PG(http_globals
)[TRACK_VARS_POST
], &tmp
);
101 /* PHP-7.4 optimizes empty array */
109 zend_string_release(json
);
115 char *json_str
= NULL
;
118 # if PHP_VERSION_ID >= 50600
119 if (!SG(request_info
).request_body
) {
123 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
124 php_stream_rewind(SG(request_info
).request_body
);
125 json_len
= php_stream_copy_to_mem(SG(request_info
).request_body
, &json_str
, PHP_STREAM_COPY_ALL
, 0);
127 json_str
= SG(request_info
).raw_post_data
;
128 json_len
= SG(request_info
).raw_post_data_length
;
136 # if PHP_VERSION_ID >= 50400
137 php_json_decode_ex(&zjson
, json_str
, json_len
, JSON_POST_G(flags
), PG(max_input_nesting_level
) TSRMLS_CC
);
138 if (Z_TYPE(zjson
) != IS_NULL
) {
140 ZVAL_COPY_VALUE(zarg
, (&zjson
));
142 php_json_decode(&zjson
, json_str
, json_len
, (zend_bool
)(JSON_POST_G(flags
)&1), PG(max_input_nesting_level
) TSRMLS_CC
);
143 if (Z_TYPE(zjson
) != IS_NULL
) {
145 zarg
->value
= zjson
.value
;
146 Z_TYPE_P(zarg
) = Z_TYPE(zjson
);
150 # if PHP_VERSION_ID >= 50600
157 REGISTER_LONG_CONSTANT("JSON_POST_ERROR", JSON_G(error_code
), CONST_CS
);
159 if (JSON_G(error_code
)) {
160 if (JSON_POST_G(onerror
.response
)) {
161 sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) (zend_long
) JSON_POST_G(onerror
.response
) TSRMLS_CC
);
163 if (JSON_POST_G(onerror
.warning
)) {
164 zend_error(E_WARNING
, "json_post: json_decode failed with error code: %d", JSON_G(error_code
));
166 if (JSON_POST_G(onerror
.exit
)) {
167 sapi_send_headers(TSRMLS_C
);
170 #if PHP_VERSION_ID >= 70000 && PHP_VERSION_ID < 80000
171 /* ext/json in PHP-7 fails to reset error_code in RINIT */
172 JSON_G(error_code
) = 0;
177 PHP_MINIT_FUNCTION(json_post
)
179 sapi_post_entry json_post_entries
[] = {
180 { "text/json", sizeof("text/json")-1, sapi_read_standard_form_data
, php_json_post_handler
},
181 { "application/json", sizeof("application/json")-1, sapi_read_standard_form_data
, php_json_post_handler
},
182 { NULL
, 0, NULL
, NULL
}
185 sapi_register_post_entries(json_post_entries TSRMLS_CC
);
187 ZEND_INIT_MODULE_GLOBALS(json_post
, php_json_post_init_globals
, NULL
);
189 #if PHP_VERSION_ID < 70000
190 zend_hash_find(&module_registry
, ZEND_STRS("json"), (void **) &JSON_POST_G(json_module
));
193 REGISTER_INI_ENTRIES();
197 PHP_MSHUTDOWN_FUNCTION(json_post
)
199 UNREGISTER_INI_ENTRIES();
204 zend_function_entry json_post_functions
[] = {
208 static zend_module_dep json_post_module_deps
[] = {
209 ZEND_MOD_REQUIRED("json")
210 {NULL
, NULL
, NULL
, 0}
213 zend_module_entry json_post_module_entry
= {
214 STANDARD_MODULE_HEADER_EX
,
216 json_post_module_deps
,
219 PHP_MINIT(json_post
),
220 PHP_MSHUTDOWN(json_post
),
223 PHP_MINFO(json_post
),
224 PHP_JSON_POST_VERSION
,
225 STANDARD_MODULE_PROPERTIES
229 #ifdef COMPILE_DL_JSON_POST
230 ZEND_GET_MODULE(json_post
)
238 * vim600: noet sw=4 ts=4 fdm=marker
239 * vim<600: noet sw=4 ts=4