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
)
31 static void php_json_post_init_globals(zend_json_post_globals
*json_post_globals
)
33 #if PHP_VERSION_ID >= 50400
34 json_post_globals
->flags
= PHP_JSON_OBJECT_AS_ARRAY
;
36 json_post_globals
->flags
= 1;
40 PHP_MINFO_FUNCTION(json_post
)
42 php_info_print_table_start();
43 php_info_print_table_header(2, "json_post support", "enabled");
44 php_info_print_table_end();
46 DISPLAY_INI_ENTRIES();
49 #if PHP_VERSION_ID >= 70000
50 static SAPI_POST_HANDLER_FUNC(php_json_post_handler
)
52 zend_string
*json
= NULL
;
54 if (SG(request_info
).request_body
) {
55 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
56 php_stream_rewind(SG(request_info
).request_body
);
57 json
= php_stream_copy_to_mem(SG(request_info
).request_body
, PHP_STREAM_COPY_ALL
, 0);
66 php_json_decode_ex(&tmp
, json
->val
, json
->len
, JSON_POST_G(flags
), PG(max_input_nesting_level
));
68 switch (Z_TYPE(tmp
)) {
72 ZVAL_COPY_VALUE(&PG(http_globals
)[TRACK_VARS_POST
], &tmp
);
78 zend_string_release(json
);
84 static SAPI_POST_HANDLER_FUNC(php_json_post_handler
)
87 char *json_str
= NULL
;
90 # if PHP_VERSION_ID >= 50600
91 if (!SG(request_info
).request_body
) {
95 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
96 php_stream_rewind(SG(request_info
).request_body
);
97 json_len
= php_stream_copy_to_mem(SG(request_info
).request_body
, &json_str
, PHP_STREAM_COPY_ALL
, 0);
99 json_str
= SG(request_info
).raw_post_data
;
100 json_len
= SG(request_info
).raw_post_data_length
;
108 # if PHP_VERSION_ID >= 50400
109 php_json_decode_ex(&zjson
, json_str
, json_len
, JSON_POST_G(flags
), PG(max_input_nesting_level
) TSRMLS_CC
);
110 if (Z_TYPE(zjson
) != IS_NULL
) {
112 ZVAL_COPY_VALUE(zarg
, (&zjson
));
114 php_json_decode(&zjson
, json_str
, json_len
, (zend_bool
)(JSON_POST_G(flags
)&1), PG(max_input_nesting_level
) TSRMLS_CC
);
115 if (Z_TYPE(zjson
) != IS_NULL
) {
117 zarg
->value
= zjson
.value
;
118 Z_TYPE_P(zarg
) = Z_TYPE(zjson
);
122 # if PHP_VERSION_ID >= 50600
131 PHP_MINIT_FUNCTION(json_post
)
133 sapi_post_entry json_post_entries
[] = {
134 { "text/json", sizeof("text/json")-1, sapi_read_standard_form_data
, php_json_post_handler
},
135 { "application/json", sizeof("application/json")-1, sapi_read_standard_form_data
, php_json_post_handler
},
136 { NULL
, 0, NULL
, NULL
}
139 sapi_register_post_entries(json_post_entries TSRMLS_CC
);
141 ZEND_INIT_MODULE_GLOBALS(json_post
, php_json_post_init_globals
, NULL
);
142 REGISTER_INI_ENTRIES();
146 PHP_MSHUTDOWN_FUNCTION(json_post
)
148 UNREGISTER_INI_ENTRIES();
153 zend_function_entry json_post_functions
[] = {
157 static zend_module_dep json_post_module_deps
[] = {
158 ZEND_MOD_REQUIRED("json")
159 {NULL
, NULL
, NULL
, 0}
162 zend_module_entry json_post_module_entry
= {
163 STANDARD_MODULE_HEADER_EX
,
165 json_post_module_deps
,
168 PHP_MINIT(json_post
),
169 PHP_MSHUTDOWN(json_post
),
172 PHP_MINFO(json_post
),
173 PHP_JSON_POST_VERSION
,
174 STANDARD_MODULE_PROPERTIES
178 #ifdef COMPILE_DL_JSON_POST
179 ZEND_GET_MODULE(json_post
)
187 * vim600: noet sw=4 ts=4 fdm=marker
188 * vim<600: noet sw=4 ts=4