1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Rui Hirokawa <hirokawa@php.net> |
14 | Moriyoshi Koizumi <moriyoshi@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 /* {{{ includes */
19 #include "php.h"
20 #include "php_ini.h"
21 #include "php_variables.h"
22 #include "libmbfl/mbfl/mbfilter_pass.h"
23 #include "mbstring.h"
24 #include "ext/standard/php_string.h"
25 #include "ext/standard/php_mail.h"
26 #include "ext/standard/url.h"
27 #include "main/php_output.h"
28 #include "ext/standard/info.h"
29
30 #include "php_globals.h"
31 #include "rfc1867.h"
32 #include "php_content_types.h"
33 #include "SAPI.h"
34 #include "TSRM.h"
35
36 #include "mb_gpc.h"
37 /* }}} */
38
ZEND_EXTERN_MODULE_GLOBALS(mbstring)39 ZEND_EXTERN_MODULE_GLOBALS(mbstring)
40
41 /* {{{ MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
42 * http input processing */
43 MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
44 {
45 char *res = NULL, *separator=NULL;
46 const char *c_var;
47 zval v_array;
48 int free_buffer=0;
49 const mbfl_encoding *detected;
50 php_mb_encoding_handler_info_t info;
51
52 if (!MBSTRG(encoding_translation)) {
53 php_default_treat_data(arg, str, destArray);
54 return;
55 }
56
57 switch (arg) {
58 case PARSE_POST:
59 case PARSE_GET:
60 case PARSE_COOKIE:
61 array_init(&v_array);
62 switch (arg) {
63 case PARSE_POST:
64 ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_POST], &v_array);
65 break;
66 case PARSE_GET:
67 ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_GET], &v_array);
68 break;
69 case PARSE_COOKIE:
70 ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_COOKIE], &v_array);
71 break;
72 }
73 break;
74 default:
75 ZVAL_COPY_VALUE(&v_array, destArray);
76 break;
77 }
78
79 switch (arg) {
80 case PARSE_POST:
81 sapi_handle_post(&v_array);
82 return;
83 case PARSE_GET: /* GET data */
84 c_var = SG(request_info).query_string;
85 if (c_var && *c_var) {
86 res = (char *) estrdup(c_var);
87 free_buffer = 1;
88 }
89 break;
90 case PARSE_COOKIE: /* Cookie data */
91 c_var = SG(request_info).cookie_data;
92 if (c_var && *c_var) {
93 res = (char *) estrdup(c_var);
94 free_buffer = 1;
95 }
96 break;
97 case PARSE_STRING: /* String data */
98 res = str;
99 free_buffer = 1;
100 break;
101 }
102
103 if (!res) {
104 return;
105 }
106
107 switch (arg) {
108 case PARSE_POST:
109 case PARSE_GET:
110 case PARSE_STRING:
111 separator = (char *) estrdup(PG(arg_separator).input);
112 break;
113 case PARSE_COOKIE:
114 separator = ";\0";
115 break;
116 }
117
118 switch (arg) {
119 case PARSE_POST:
120 MBSTRG(http_input_identify_post) = NULL;
121 break;
122 case PARSE_GET:
123 MBSTRG(http_input_identify_get) = NULL;
124 break;
125 case PARSE_COOKIE:
126 MBSTRG(http_input_identify_cookie) = NULL;
127 break;
128 case PARSE_STRING:
129 MBSTRG(http_input_identify_string) = NULL;
130 break;
131 }
132
133 info.data_type = arg;
134 info.separator = separator;
135 info.report_errors = false;
136 info.to_encoding = MBSTRG(internal_encoding);
137 info.from_encodings = MBSTRG(http_input_list);
138 info.num_from_encodings = MBSTRG(http_input_list_size);
139
140 MBSTRG(illegalchars) = 0;
141
142 detected = _php_mb_encoding_handler_ex(&info, &v_array, res);
143 MBSTRG(http_input_identify) = detected;
144
145 if (detected) {
146 switch(arg){
147 case PARSE_POST:
148 MBSTRG(http_input_identify_post) = detected;
149 break;
150 case PARSE_GET:
151 MBSTRG(http_input_identify_get) = detected;
152 break;
153 case PARSE_COOKIE:
154 MBSTRG(http_input_identify_cookie) = detected;
155 break;
156 case PARSE_STRING:
157 MBSTRG(http_input_identify_string) = detected;
158 break;
159 }
160 }
161
162 if (arg != PARSE_COOKIE) {
163 efree(separator);
164 }
165
166 if (free_buffer) {
167 efree(res);
168 }
169 }
170 /* }}} */
171
172 /* {{{ mbfl_no_encoding _php_mb_encoding_handler_ex() */
_php_mb_encoding_handler_ex(const php_mb_encoding_handler_info_t * info,zval * array_ptr,char * res)173 const mbfl_encoding *_php_mb_encoding_handler_ex(const php_mb_encoding_handler_info_t *info, zval *array_ptr, char *res)
174 {
175 char *var, *val;
176 char *strtok_buf = NULL, **val_list = NULL;
177 size_t n, num = 1, *len_list = NULL;
178 size_t new_val_len;
179 const mbfl_encoding *from_encoding = NULL;
180
181 if (!res || *res == '\0') {
182 goto out;
183 }
184
185 /* count variables contained in `res`.
186 * separator may contain multiple separator chars; ANY of them demarcate variables */
187 for (char *s1 = res; *s1; s1++) {
188 for (const char *s2 = info->separator; *s2; s2++) {
189 if (*s1 == *s2) {
190 num++;
191 }
192 }
193 }
194 num *= 2; /* need space for variable name and value */
195
196 val_list = (char **)ecalloc(num, sizeof(char *));
197 len_list = (size_t *)ecalloc(num, sizeof(size_t));
198
199 /* split and decode the query */
200 n = 0;
201 var = php_strtok_r(res, info->separator, &strtok_buf);
202 while (var) {
203 val = strchr(var, '=');
204 if (val) { /* have a value */
205 len_list[n] = php_url_decode(var, val-var);
206 val_list[n] = var;
207 n++;
208
209 *val++ = '\0';
210 val_list[n] = val;
211 len_list[n] = php_url_decode(val, strlen(val));
212 } else {
213 len_list[n] = php_url_decode(var, strlen(var));
214 val_list[n] = var;
215 n++;
216
217 val_list[n] = "";
218 len_list[n] = 0;
219 }
220 n++;
221 var = php_strtok_r(NULL, info->separator, &strtok_buf);
222 }
223
224 if (ZEND_SIZE_T_GT_ZEND_LONG(n, (PG(max_input_vars) * 2))) {
225 php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
226 goto out;
227 }
228
229 num = n; /* make sure to process initialized vars only */
230
231 /* initialize converter */
232 if (info->num_from_encodings == 0) {
233 from_encoding = &mbfl_encoding_pass;
234 } else if (info->num_from_encodings == 1) {
235 from_encoding = info->from_encodings[0];
236 } else {
237 from_encoding = mb_guess_encoding_for_strings((const unsigned char**)val_list, len_list, num, info->from_encodings, info->num_from_encodings, MBSTRG(strict_detection), false);
238 if (!from_encoding) {
239 if (info->report_errors) {
240 php_error_docref(NULL, E_WARNING, "Unable to detect encoding");
241 }
242 from_encoding = &mbfl_encoding_pass;
243 }
244 }
245
246 /* convert encoding */
247 n = 0;
248 while (n < num) {
249 if (from_encoding != &mbfl_encoding_pass && info->to_encoding != &mbfl_encoding_pass) {
250 unsigned int num_errors = 0;
251 zend_string *converted_var = mb_fast_convert((unsigned char*)val_list[n], len_list[n], from_encoding, info->to_encoding, MBSTRG(current_filter_illegal_substchar), MBSTRG(current_filter_illegal_mode), &num_errors);
252 MBSTRG(illegalchars) += num_errors;
253 n++;
254
255 num_errors = 0;
256 zend_string *converted_val = mb_fast_convert((unsigned char*)val_list[n], len_list[n], from_encoding, info->to_encoding, MBSTRG(current_filter_illegal_substchar), MBSTRG(current_filter_illegal_mode), &num_errors);
257 MBSTRG(illegalchars) += num_errors;
258 n++;
259
260 /* `val` must be a pointer returned by `emalloc` */
261 val = estrndup(ZSTR_VAL(converted_val), ZSTR_LEN(converted_val));
262 if (sapi_module.input_filter(info->data_type, ZSTR_VAL(converted_var), &val, ZSTR_LEN(converted_val), &new_val_len)) {
263 /* add variable to symbol table */
264 php_register_variable_safe(ZSTR_VAL(converted_var), val, new_val_len, array_ptr);
265 }
266 zend_string_free(converted_var);
267 zend_string_free(converted_val);
268 } else {
269 var = val_list[n++];
270 val = estrndup(val_list[n], len_list[n]);
271 if (sapi_module.input_filter(info->data_type, var, &val, len_list[n], &new_val_len)) {
272 php_register_variable_safe(var, val, new_val_len, array_ptr);
273 }
274 n++;
275 }
276 efree(val);
277 }
278
279 out:
280 if (val_list != NULL) {
281 efree((void *)val_list);
282 }
283 if (len_list != NULL) {
284 efree((void *)len_list);
285 }
286
287 return from_encoding;
288 }
289 /* }}} */
290
291 /* {{{ SAPI_POST_HANDLER_FUNC(php_mb_post_handler) */
SAPI_POST_HANDLER_FUNC(php_mb_post_handler)292 SAPI_POST_HANDLER_FUNC(php_mb_post_handler)
293 {
294 const mbfl_encoding *detected;
295 php_mb_encoding_handler_info_t info;
296 zend_string *post_data_str = NULL;
297
298 MBSTRG(http_input_identify_post) = NULL;
299
300 info.data_type = PARSE_POST;
301 info.separator = "&";
302 info.report_errors = false;
303 info.to_encoding = MBSTRG(internal_encoding);
304 info.from_encodings = MBSTRG(http_input_list);
305 info.num_from_encodings = MBSTRG(http_input_list_size);
306
307 php_stream_rewind(SG(request_info).request_body);
308 post_data_str = php_stream_copy_to_mem(SG(request_info).request_body, PHP_STREAM_COPY_ALL, 0);
309 detected = _php_mb_encoding_handler_ex(&info, arg, post_data_str ? ZSTR_VAL(post_data_str) : NULL);
310 if (post_data_str) {
311 zend_string_release_ex(post_data_str, 0);
312 }
313
314 MBSTRG(http_input_identify) = detected;
315 if (detected) {
316 MBSTRG(http_input_identify_post) = detected;
317 }
318 }
319 /* }}} */
320