xref: /php-src/ext/mbstring/mb_gpc.c (revision cd66fcc6)
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 	zend_long max_input_vars = REQUEST_PARSE_BODY_OPTION_GET(max_input_vars, PG(max_input_vars));
225 	if (ZEND_SIZE_T_GT_ZEND_LONG(n, max_input_vars * 2)) {
226 		php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", max_input_vars);
227 		goto out;
228 	}
229 
230 	num = n; /* make sure to process initialized vars only */
231 
232 	/* initialize converter */
233 	if (info->num_from_encodings == 0) {
234 		from_encoding = &mbfl_encoding_pass;
235 	} else if (info->num_from_encodings == 1) {
236 		from_encoding = info->from_encodings[0];
237 	} else {
238 		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);
239 		if (!from_encoding) {
240 			if (info->report_errors) {
241 				php_error_docref(NULL, E_WARNING, "Unable to detect encoding");
242 			}
243 			from_encoding = &mbfl_encoding_pass;
244 		}
245 	}
246 
247 	/* convert encoding */
248 	n = 0;
249 	while (n < num) {
250 		if (from_encoding != &mbfl_encoding_pass && info->to_encoding != &mbfl_encoding_pass) {
251 			unsigned int num_errors = 0;
252 			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);
253 			MBSTRG(illegalchars) += num_errors;
254 			n++;
255 
256 			num_errors = 0;
257 			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);
258 			MBSTRG(illegalchars) += num_errors;
259 			n++;
260 
261 			/* `val` must be a pointer returned by `emalloc` */
262 			val = estrndup(ZSTR_VAL(converted_val), ZSTR_LEN(converted_val));
263 			if (sapi_module.input_filter(info->data_type, ZSTR_VAL(converted_var), &val, ZSTR_LEN(converted_val), &new_val_len)) {
264 				/* add variable to symbol table */
265 				php_register_variable_safe(ZSTR_VAL(converted_var), val, new_val_len, array_ptr);
266 			}
267 			zend_string_free(converted_var);
268 			zend_string_free(converted_val);
269 		} else {
270 			var = val_list[n++];
271 			val = estrndup(val_list[n], len_list[n]);
272 			if (sapi_module.input_filter(info->data_type, var, &val, len_list[n], &new_val_len)) {
273 				php_register_variable_safe(var, val, new_val_len, array_ptr);
274 			}
275 			n++;
276 		}
277 		efree(val);
278 	}
279 
280 out:
281 	if (val_list != NULL) {
282 		efree((void *)val_list);
283 	}
284 	if (len_list != NULL) {
285 		efree((void *)len_list);
286 	}
287 
288 	return from_encoding;
289 }
290 /* }}} */
291 
292 /* {{{ SAPI_POST_HANDLER_FUNC(php_mb_post_handler) */
SAPI_POST_HANDLER_FUNC(php_mb_post_handler)293 SAPI_POST_HANDLER_FUNC(php_mb_post_handler)
294 {
295 	const mbfl_encoding *detected;
296 	php_mb_encoding_handler_info_t info;
297 	zend_string *post_data_str = NULL;
298 
299 	MBSTRG(http_input_identify_post) = NULL;
300 
301 	info.data_type              = PARSE_POST;
302 	info.separator              = "&";
303 	info.report_errors          = false;
304 	info.to_encoding            = MBSTRG(internal_encoding);
305 	info.from_encodings         = MBSTRG(http_input_list);
306 	info.num_from_encodings     = MBSTRG(http_input_list_size);
307 
308 	php_stream_rewind(SG(request_info).request_body);
309 	post_data_str = php_stream_copy_to_mem(SG(request_info).request_body, PHP_STREAM_COPY_ALL, 0);
310 	detected = _php_mb_encoding_handler_ex(&info, arg, post_data_str ? ZSTR_VAL(post_data_str) : NULL);
311 	if (post_data_str) {
312 		zend_string_release_ex(post_data_str, 0);
313 	}
314 
315 	MBSTRG(http_input_identify) = detected;
316 	if (detected) {
317 		MBSTRG(http_input_identify_post) = detected;
318 	}
319 }
320 /* }}} */
321