|
| 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 <[email protected]> | |
| 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 | + json_post_globals->flags = PHP_JSON_OBJECT_AS_ARRAY; |
| 34 | +} |
| 35 | + |
| 36 | +PHP_MINFO_FUNCTION(json_post) |
| 37 | +{ |
| 38 | + php_info_print_table_start(); |
| 39 | + php_info_print_table_header(2, "json_post support", "enabled"); |
| 40 | + php_info_print_table_end(); |
| 41 | + |
| 42 | + DISPLAY_INI_ENTRIES(); |
| 43 | +} |
| 44 | + |
| 45 | +static SAPI_POST_HANDLER_FUNC(php_json_post_handler) |
| 46 | +{ |
| 47 | + zval *zarg = arg; |
| 48 | + char *json_str = NULL; |
| 49 | + size_t json_len = 0; |
| 50 | + |
| 51 | +#if PHP_VERSION_ID >= 50600 |
| 52 | + if (SG(request_info).request_body) { |
| 53 | + /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */ |
| 54 | + php_stream_rewind(SG(request_info).request_body); |
| 55 | + json_len = php_stream_copy_to_mem(SG(request_info).request_body, &json_str, PHP_STREAM_COPY_ALL, 0); |
| 56 | + } |
| 57 | +#else |
| 58 | + json_str = SG(request_info).raw_post_data; |
| 59 | + json_len = SG(request_info).raw_post_data_length; |
| 60 | +#endif |
| 61 | + |
| 62 | + if (json_len) { |
| 63 | + zval zjson; |
| 64 | + |
| 65 | + INIT_ZVAL(zjson); |
| 66 | + php_json_decode_ex(&zjson, json_str, json_len, JSON_POST_G(flags), PG(max_input_nesting_level) TSRMLS_CC); |
| 67 | + if (Z_TYPE(zjson) != IS_NULL) { |
| 68 | + zval_dtor(zarg); |
| 69 | + ZVAL_COPY_VALUE(zarg, (&zjson)); |
| 70 | + } |
| 71 | + } |
| 72 | +#if PHP_VERSION_ID >= 50600 |
| 73 | + if (json_str) { |
| 74 | + efree(json_str); |
| 75 | + } |
| 76 | +#endif |
| 77 | +} |
| 78 | + |
| 79 | +PHP_MINIT_FUNCTION(json_post) |
| 80 | +{ |
| 81 | + sapi_post_entry entry = {NULL, 0, NULL, NULL}; |
| 82 | + |
| 83 | + entry.post_reader = sapi_read_standard_form_data; |
| 84 | + entry.post_handler = php_json_post_handler; |
| 85 | + |
| 86 | + entry.content_type = "text/json"; |
| 87 | + entry.content_type_len = sizeof("text/json")-1; |
| 88 | + sapi_register_post_entry(&entry TSRMLS_CC); |
| 89 | + |
| 90 | + ZEND_INIT_MODULE_GLOBALS(json_post, php_json_post_init_globals, NULL); |
| 91 | + REGISTER_INI_ENTRIES(); |
| 92 | + return SUCCESS; |
| 93 | +} |
| 94 | + |
| 95 | +PHP_MSHUTDOWN_FUNCTION(json_post) |
| 96 | +{ |
| 97 | + UNREGISTER_INI_ENTRIES(); |
| 98 | + |
| 99 | + return SUCCESS; |
| 100 | +} |
| 101 | + |
| 102 | +zend_function_entry json_post_functions[] = { |
| 103 | + {0} |
| 104 | +}; |
| 105 | + |
| 106 | +static zend_module_dep json_post_module_deps[] = { |
| 107 | + ZEND_MOD_REQUIRED("json") |
| 108 | + {NULL, NULL, NULL, 0} |
| 109 | +}; |
| 110 | + |
| 111 | +zend_module_entry json_post_module_entry = { |
| 112 | + STANDARD_MODULE_HEADER_EX, |
| 113 | + NULL, |
| 114 | + json_post_module_deps, |
| 115 | + "json_post", |
| 116 | + json_post_functions, |
| 117 | + PHP_MINIT(json_post), |
| 118 | + PHP_MSHUTDOWN(json_post), |
| 119 | + NULL, |
| 120 | + NULL, |
| 121 | + PHP_MINFO(json_post), |
| 122 | + PHP_JSON_POST_VERSION, |
| 123 | + STANDARD_MODULE_PROPERTIES |
| 124 | +}; |
| 125 | +/* }}} */ |
| 126 | + |
| 127 | +#ifdef COMPILE_DL_JSON_POST |
| 128 | +ZEND_GET_MODULE(json_post) |
| 129 | +#endif |
| 130 | + |
| 131 | +/* |
| 132 | + * Local variables: |
| 133 | + * tab-width: 4 |
| 134 | + * c-basic-offset: 4 |
| 135 | + * End: |
| 136 | + * vim600: noet sw=4 ts=4 fdm=marker |
| 137 | + * vim<600: noet sw=4 ts=4 |
| 138 | + */ |
0 commit comments