Fix PHP 5.3 build
[m6w6/ext-json_post] / php_json_post.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: json_post |
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 +--------------------------------------------------------------------+
11 */
12
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 #include "php.h"
19 #include "php_ini.h"
20 #include "ext/standard/info.h"
21 #include "ext/json/php_json.h"
22 #include "php_json_post.h"
23 #include "SAPI.h"
24
25 ZEND_DECLARE_MODULE_GLOBALS(json_post);
26
27 PHP_INI_BEGIN()
28 STD_PHP_INI_ENTRY("json_post.flags", "1", PHP_INI_PERDIR, OnUpdateLong, flags, zend_json_post_globals, json_post_globals)
29 PHP_INI_END()
30
31 static void php_json_post_init_globals(zend_json_post_globals *json_post_globals)
32 {
33 #if PHP_VERSION_ID >= 50400
34 json_post_globals->flags = PHP_JSON_OBJECT_AS_ARRAY;
35 #else
36 json_post_globals->flags = 1;
37 #endif
38 }
39
40 PHP_MINFO_FUNCTION(json_post)
41 {
42 php_info_print_table_start();
43 php_info_print_table_header(2, "json_post support", "enabled");
44 php_info_print_table_end();
45
46 DISPLAY_INI_ENTRIES();
47 }
48
49 static SAPI_POST_HANDLER_FUNC(php_json_post_handler)
50 {
51 zval *zarg = arg;
52 char *json_str = NULL;
53 size_t json_len = 0;
54
55 #if PHP_VERSION_ID >= 50600
56 if (SG(request_info).request_body) {
57 /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
58 php_stream_rewind(SG(request_info).request_body);
59 json_len = php_stream_copy_to_mem(SG(request_info).request_body, &json_str, PHP_STREAM_COPY_ALL, 0);
60 }
61 #else
62 json_str = SG(request_info).raw_post_data;
63 json_len = SG(request_info).raw_post_data_length;
64 #endif
65
66 if (json_len) {
67 zval zjson;
68
69 INIT_ZVAL(zjson);
70 #if PHP_VERSION_ID >= 50400
71 php_json_decode_ex(&zjson, json_str, json_len, JSON_POST_G(flags), PG(max_input_nesting_level) TSRMLS_CC);
72 if (Z_TYPE(zjson) != IS_NULL) {
73 zval_dtor(zarg);
74 ZVAL_COPY_VALUE(zarg, (&zjson));
75 #else
76 php_json_decode(&zjson, json_str, json_len, (zend_bool)(JSON_POST_G(flags)&1), PG(max_input_nesting_level) TSRMLS_CC);
77 if (Z_TYPE(zjson) != IS_NULL) {
78 zval_dtor(zarg);
79 zarg->value = zjson.value;
80 Z_TYPE_P(zarg) = Z_TYPE(zjson);
81 #endif
82 }
83 }
84 #if PHP_VERSION_ID >= 50600
85 if (json_str) {
86 efree(json_str);
87 }
88 #endif
89 }
90
91 PHP_MINIT_FUNCTION(json_post)
92 {
93 sapi_post_entry entry = {NULL, 0, NULL, NULL};
94
95 entry.post_reader = sapi_read_standard_form_data;
96 entry.post_handler = php_json_post_handler;
97
98 entry.content_type = "text/json";
99 entry.content_type_len = sizeof("text/json")-1;
100 sapi_register_post_entry(&entry TSRMLS_CC);
101
102 ZEND_INIT_MODULE_GLOBALS(json_post, php_json_post_init_globals, NULL);
103 REGISTER_INI_ENTRIES();
104 return SUCCESS;
105 }
106
107 PHP_MSHUTDOWN_FUNCTION(json_post)
108 {
109 UNREGISTER_INI_ENTRIES();
110
111 return SUCCESS;
112 }
113
114 zend_function_entry json_post_functions[] = {
115 {0}
116 };
117
118 static zend_module_dep json_post_module_deps[] = {
119 ZEND_MOD_REQUIRED("json")
120 {NULL, NULL, NULL, 0}
121 };
122
123 zend_module_entry json_post_module_entry = {
124 STANDARD_MODULE_HEADER_EX,
125 NULL,
126 json_post_module_deps,
127 "json_post",
128 json_post_functions,
129 PHP_MINIT(json_post),
130 PHP_MSHUTDOWN(json_post),
131 NULL,
132 NULL,
133 PHP_MINFO(json_post),
134 PHP_JSON_POST_VERSION,
135 STANDARD_MODULE_PROPERTIES
136 };
137 /* }}} */
138
139 #ifdef COMPILE_DL_JSON_POST
140 ZEND_GET_MODULE(json_post)
141 #endif
142
143 /*
144 * Local variables:
145 * tab-width: 4
146 * c-basic-offset: 4
147 * End:
148 * vim600: noet sw=4 ts=4 fdm=marker
149 * vim<600: noet sw=4 ts=4
150 */