PHP7 support
[m6w6/ext-apfd] / php_apfd.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: apfd |
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 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include "php.h"
18 #include "php_ini.h"
19 #include "ext/standard/info.h"
20 #include "SAPI.h"
21 #include "rfc1867.h"
22 #include "php_apfd.h"
23
24 #if PHP_VERSION_ID >= 70000
25
26 #define APFD_SG(t) &PG(http_globals)[t]
27 static inline sapi_post_entry *apfd_get_post_entry(const char *ct_str, size_t ct_len)
28 {
29 return zend_hash_str_find_ptr(&SG(known_post_content_types), ct_str, ct_len);
30 }
31
32 static inline zval *apfd_backup_files(void)
33 {
34 if (SG(rfc1867_uploaded_files)) {
35 destroy_uploaded_files_hash();
36 }
37 return NULL;
38 }
39
40 static inline void apfd_update_files(zval *files)
41 {
42 Z_TRY_ADDREF_P(APFD_SG(TRACK_VARS_FILES));
43 zend_hash_str_update(&EG(symbol_table), "_FILES", sizeof("_FILES")-1, APFD_SG(TRACK_VARS_FILES));
44 }
45
46 #else
47
48 #define APFD_SG(t) PG(http_globals)[t]
49 static inline sapi_post_entry *apfd_get_post_entry(const char *ct_str, size_t ct_len TSRMLS_DC)
50 {
51 sapi_post_entry *post_entry;
52
53 if (SUCCESS == zend_hash_find(&SG(known_post_content_types), ct_str, ct_len+1, (void *) &post_entry)) {
54 return post_entry;
55 }
56 return NULL;
57 }
58
59 static inline zval *apfd_backup_files(TSRMLS_D)
60 {
61 return APFD_SG(TRACK_VARS_FILES);
62 }
63
64 static inline void apfd_update_files(zval *files TSRMLS_DC)
65 {
66 if (files != APFD_SG(TRACK_VARS_FILES) && APFD_SG(TRACK_VARS_FILES)) {
67 Z_ADDREF_P(APFD_SG(TRACK_VARS_FILES));
68 zend_hash_update(&EG(symbol_table), "_FILES", sizeof("_FILES"), &APFD_SG(TRACK_VARS_FILES), sizeof(zval *), NULL);
69 if (files) {
70 zval_ptr_dtor(&files);
71 }
72 }
73 }
74 #endif
75
76 PHP_RINIT_FUNCTION(apfd)
77 {
78 /* populate form data on non-POST requests */
79 if (SG(request_info).request_method && strcasecmp(SG(request_info).request_method, "POST") && SG(request_info).content_type && *SG(request_info).content_type) {
80 char *ct_str = zend_str_tolower_dup(SG(request_info).content_type, strlen(SG(request_info).content_type));
81 size_t ct_end = strcspn(ct_str, ";, ");
82 sapi_post_entry *post_entry = NULL;
83 char delim;
84
85 SG(request_info).content_type_dup = ct_str;
86
87 delim = ct_str[ct_end];
88 ct_str[ct_end] = '\0';
89 if ((post_entry = apfd_get_post_entry(ct_str, ct_end TSRMLS_CC))) {
90 zval *files = apfd_backup_files(TSRMLS_C);
91
92 ct_str[ct_end] = delim;
93
94 if (post_entry) {
95 SG(request_info).post_entry = post_entry;
96
97 if (post_entry->post_reader) {
98 post_entry->post_reader(TSRMLS_C);
99 }
100 }
101
102 if (sapi_module.default_post_reader) {
103 sapi_module.default_post_reader(TSRMLS_C);
104 }
105
106 sapi_handle_post(APFD_SG(TRACK_VARS_POST) TSRMLS_CC);
107
108 /*
109 * the rfc1867 handler is an awkward buddy
110 */
111 apfd_update_files(files TSRMLS_CC);
112 }
113
114 if (SG(request_info).content_type_dup) {
115 efree(SG(request_info).content_type_dup);
116 SG(request_info).content_type_dup = NULL;
117 }
118 }
119
120 return SUCCESS;
121 }
122
123 PHP_MINFO_FUNCTION(apfd)
124 {
125 php_info_print_table_start();
126 php_info_print_table_header(2, "apfd support", "enabled");
127 php_info_print_table_end();
128 }
129
130 const zend_function_entry apfd_functions[] = {
131 {0}
132 };
133
134 zend_module_entry apfd_module_entry = {
135 STANDARD_MODULE_HEADER,
136 "apfd",
137 apfd_functions,
138 NULL,
139 NULL,
140 PHP_RINIT(apfd),
141 NULL,
142 PHP_MINFO(apfd),
143 PHP_APFD_VERSION,
144 STANDARD_MODULE_PROPERTIES
145 };
146
147 #ifdef COMPILE_DL_APFD
148 ZEND_GET_MODULE(apfd)
149 #endif
150
151 /*
152 * Local variables:
153 * tab-width: 4
154 * c-basic-offset: 4
155 * End:
156 * vim600: noet sw=4 ts=4 fdm=marker
157 * vim<600: noet sw=4 ts=4
158 */