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