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;
44 #if PHP_VERSION_ID < 70000
45 ZEND_EXTERN_MODULE_GLOBALS(json
);
54 PHP_MINFO_FUNCTION(json_post
)
56 php_info_print_table_start();
57 php_info_print_table_header(2, "json_post support", "enabled");
58 php_info_print_table_end();
60 DISPLAY_INI_ENTRIES();
63 static SAPI_POST_HANDLER_FUNC(php_json_post_handler
)
65 int module_number
= 0;
67 #if PHP_VERSION_ID >= 70000
68 zend_string
*json
= NULL
;
70 if (SG(request_info
).request_body
) {
71 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
72 php_stream_rewind(SG(request_info
).request_body
);
73 json
= php_stream_copy_to_mem(SG(request_info
).request_body
, PHP_STREAM_COPY_ALL
, 0);
79 long flags
= JSON_POST_G(flags
);
81 #ifdef PHP_JSON_THROW_ON_ERROR
82 /* there's no execute data, so we must ensure json_decode() is not throwing */
83 flags
&= ~PHP_JSON_THROW_ON_ERROR
;
87 php_json_decode_ex(&tmp
, json
->val
, json
->len
, flags
, PG(max_input_nesting_level
));
89 switch (Z_TYPE(tmp
)) {
92 if (zend_hash_num_elements(HASH_OF(&tmp
))) {
94 ZVAL_COPY_VALUE(&PG(http_globals
)[TRACK_VARS_POST
], &tmp
);
96 /* PHP-7.4 optimizes empty array */
104 zend_string_release(json
);
110 char *json_str
= NULL
;
113 # if PHP_VERSION_ID >= 50600
114 if (!SG(request_info
).request_body
) {
118 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
119 php_stream_rewind(SG(request_info
).request_body
);
120 json_len
= php_stream_copy_to_mem(SG(request_info
).request_body
, &json_str
, PHP_STREAM_COPY_ALL
, 0);
122 json_str
= SG(request_info
).raw_post_data
;
123 json_len
= SG(request_info
).raw_post_data_length
;
131 # if PHP_VERSION_ID >= 50400
132 php_json_decode_ex(&zjson
, json_str
, json_len
, JSON_POST_G(flags
), PG(max_input_nesting_level
) TSRMLS_CC
);
133 if (Z_TYPE(zjson
) != IS_NULL
) {
135 ZVAL_COPY_VALUE(zarg
, (&zjson
));
137 php_json_decode(&zjson
, json_str
, json_len
, (zend_bool
)(JSON_POST_G(flags
)&1), PG(max_input_nesting_level
) TSRMLS_CC
);
138 if (Z_TYPE(zjson
) != IS_NULL
) {
140 zarg
->value
= zjson
.value
;
141 Z_TYPE_P(zarg
) = Z_TYPE(zjson
);
145 # if PHP_VERSION_ID >= 50600
152 REGISTER_LONG_CONSTANT("JSON_POST_ERROR", JSON_G(error_code
), CONST_CS
);
154 if (JSON_G(error_code
)) {
155 if (JSON_POST_G(onerror
.response
)) {
156 sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) (zend_long
) JSON_POST_G(onerror
.response
) TSRMLS_CC
);
158 if (JSON_POST_G(onerror
.warning
)) {
159 zend_error(E_WARNING
, "json_post: json_decode failed with error code: %d", JSON_G(error_code
));
161 if (JSON_POST_G(onerror
.exit
)) {
162 sapi_send_headers(TSRMLS_C
);
165 /* ext/json in PHP-7 fails to reset error_code in RINIT */
166 JSON_G(error_code
) = 0;
170 PHP_MINIT_FUNCTION(json_post
)
172 sapi_post_entry json_post_entries
[] = {
173 { "text/json", sizeof("text/json")-1, sapi_read_standard_form_data
, php_json_post_handler
},
174 { "application/json", sizeof("application/json")-1, sapi_read_standard_form_data
, php_json_post_handler
},
175 { NULL
, 0, NULL
, NULL
}
178 sapi_register_post_entries(json_post_entries TSRMLS_CC
);
180 ZEND_INIT_MODULE_GLOBALS(json_post
, php_json_post_init_globals
, NULL
);
181 REGISTER_INI_ENTRIES();
185 PHP_MSHUTDOWN_FUNCTION(json_post
)
187 UNREGISTER_INI_ENTRIES();
192 zend_function_entry json_post_functions
[] = {
196 static zend_module_dep json_post_module_deps
[] = {
197 ZEND_MOD_REQUIRED("json")
198 {NULL
, NULL
, NULL
, 0}
201 zend_module_entry json_post_module_entry
= {
202 STANDARD_MODULE_HEADER_EX
,
204 json_post_module_deps
,
207 PHP_MINIT(json_post
),
208 PHP_MSHUTDOWN(json_post
),
211 PHP_MINFO(json_post
),
212 PHP_JSON_POST_VERSION
,
213 STANDARD_MODULE_PROPERTIES
217 #ifdef COMPILE_DL_JSON_POST
218 ZEND_GET_MODULE(json_post
)
226 * vim600: noet sw=4 ts=4 fdm=marker
227 * vim<600: noet sw=4 ts=4