Annotation of embedaddon/php/ext/mbstring/mb_gpc.c, revision 1.1.1.1
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 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: /* $Id: mb_gpc.c 321634 2012-01-01 13:15:04Z felipe $ */
21:
22: /* {{{ includes */
23: #ifdef HAVE_CONFIG_H
24: #include "config.h"
25: #endif
26:
27: #include "php.h"
28: #include "php_ini.h"
29: #include "php_variables.h"
30: #include "mbstring.h"
31: #include "ext/standard/php_string.h"
32: #include "ext/standard/php_mail.h"
33: #include "ext/standard/url.h"
34: #include "main/php_output.h"
35: #include "ext/standard/info.h"
36:
37: #include "php_variables.h"
38: #include "php_globals.h"
39: #include "rfc1867.h"
40: #include "php_content_types.h"
41: #include "SAPI.h"
42: #include "TSRM.h"
43:
44: #include "mb_gpc.h"
45: /* }}} */
46:
47: #if HAVE_MBSTRING
48:
49: ZEND_EXTERN_MODULE_GLOBALS(mbstring)
50:
51: /* {{{ MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
52: * http input processing */
53: MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
54: {
55: char *res = NULL, *separator=NULL;
56: const char *c_var;
57: zval *array_ptr;
58: int free_buffer=0;
59: enum mbfl_no_encoding detected;
60: php_mb_encoding_handler_info_t info;
61:
62: if (arg != PARSE_STRING) {
63: char *value = zend_ini_string("mbstring.internal_encoding", sizeof("mbstring.internal_encoding"), 0);
64: _php_mb_ini_mbstring_internal_encoding_set(value, value ? strlen(value): 0 TSRMLS_CC);
65: }
66:
67: if (!MBSTRG(encoding_translation)) {
68: php_default_treat_data(arg, str, destArray TSRMLS_CC);
69: return;
70: }
71:
72: switch (arg) {
73: case PARSE_POST:
74: case PARSE_GET:
75: case PARSE_COOKIE:
76: ALLOC_ZVAL(array_ptr);
77: array_init(array_ptr);
78: INIT_PZVAL(array_ptr);
79: switch (arg) {
80: case PARSE_POST:
81: PG(http_globals)[TRACK_VARS_POST] = array_ptr;
82: break;
83: case PARSE_GET:
84: PG(http_globals)[TRACK_VARS_GET] = array_ptr;
85: break;
86: case PARSE_COOKIE:
87: PG(http_globals)[TRACK_VARS_COOKIE] = array_ptr;
88: break;
89: }
90: break;
91: default:
92: array_ptr=destArray;
93: break;
94: }
95:
96: if (arg==PARSE_POST) {
97: sapi_handle_post(array_ptr TSRMLS_CC);
98: return;
99: }
100:
101: if (arg == PARSE_GET) { /* GET data */
102: c_var = SG(request_info).query_string;
103: if (c_var && *c_var) {
104: res = (char *) estrdup(c_var);
105: free_buffer = 1;
106: } else {
107: free_buffer = 0;
108: }
109: } else if (arg == PARSE_COOKIE) { /* Cookie data */
110: c_var = SG(request_info).cookie_data;
111: if (c_var && *c_var) {
112: res = (char *) estrdup(c_var);
113: free_buffer = 1;
114: } else {
115: free_buffer = 0;
116: }
117: } else if (arg == PARSE_STRING) { /* String data */
118: res = str;
119: free_buffer = 1;
120: }
121:
122: if (!res) {
123: return;
124: }
125:
126: switch (arg) {
127: case PARSE_POST:
128: case PARSE_GET:
129: case PARSE_STRING:
130: separator = (char *) estrdup(PG(arg_separator).input);
131: break;
132: case PARSE_COOKIE:
133: separator = ";\0";
134: break;
135: }
136:
137: switch(arg) {
138: case PARSE_POST:
139: MBSTRG(http_input_identify_post) = mbfl_no_encoding_invalid;
140: break;
141: case PARSE_GET:
142: MBSTRG(http_input_identify_get) = mbfl_no_encoding_invalid;
143: break;
144: case PARSE_COOKIE:
145: MBSTRG(http_input_identify_cookie) = mbfl_no_encoding_invalid;
146: break;
147: case PARSE_STRING:
148: MBSTRG(http_input_identify_string) = mbfl_no_encoding_invalid;
149: break;
150: }
151:
152: info.data_type = arg;
153: info.separator = separator;
154: info.force_register_globals = 0;
155: info.report_errors = 0;
156: info.to_encoding = MBSTRG(internal_encoding);
157: info.to_language = MBSTRG(language);
158: info.from_encodings = MBSTRG(http_input_list);
159: info.num_from_encodings = MBSTRG(http_input_list_size);
160: info.from_language = MBSTRG(language);
161:
162: MBSTRG(illegalchars) = 0;
163:
164: detected = _php_mb_encoding_handler_ex(&info, array_ptr, res TSRMLS_CC);
165: MBSTRG(http_input_identify) = detected;
166:
167: if (detected != mbfl_no_encoding_invalid) {
168: switch(arg){
169: case PARSE_POST:
170: MBSTRG(http_input_identify_post) = detected;
171: break;
172: case PARSE_GET:
173: MBSTRG(http_input_identify_get) = detected;
174: break;
175: case PARSE_COOKIE:
176: MBSTRG(http_input_identify_cookie) = detected;
177: break;
178: case PARSE_STRING:
179: MBSTRG(http_input_identify_string) = detected;
180: break;
181: }
182: }
183:
184: if (arg != PARSE_COOKIE) {
185: efree(separator);
186: }
187:
188: if (free_buffer) {
189: efree(res);
190: }
191: }
192: /* }}} */
193:
194: /* {{{ mbfl_no_encoding _php_mb_encoding_handler_ex() */
195: enum mbfl_no_encoding _php_mb_encoding_handler_ex(const php_mb_encoding_handler_info_t *info, zval *arg, char *res TSRMLS_DC)
196: {
197: char *var, *val;
198: const char *s1, *s2;
199: char *strtok_buf = NULL, **val_list = NULL;
200: zval *array_ptr = (zval *) arg;
201: int n, num, *len_list = NULL;
202: unsigned int val_len, new_val_len;
203: mbfl_string string, resvar, resval;
204: enum mbfl_no_encoding from_encoding = mbfl_no_encoding_invalid;
205: mbfl_encoding_detector *identd = NULL;
206: mbfl_buffer_converter *convd = NULL;
207: int prev_rg_state = 0;
208:
209: mbfl_string_init_set(&string, info->to_language, info->to_encoding);
210: mbfl_string_init_set(&resvar, info->to_language, info->to_encoding);
211: mbfl_string_init_set(&resval, info->to_language, info->to_encoding);
212:
213: /* register_globals stuff
214: * XXX: this feature is going to be deprecated? */
215:
216: if (info->force_register_globals && !(prev_rg_state = PG(register_globals))) {
217: zend_alter_ini_entry("register_globals", sizeof("register_globals"), "1", sizeof("1")-1, PHP_INI_PERDIR, PHP_INI_STAGE_RUNTIME);
218: }
219:
220: if (!res || *res == '\0') {
221: goto out;
222: }
223:
224: /* count the variables(separators) contained in the "res".
225: * separator may contain multiple separator chars.
226: */
227: num = 1;
228: for (s1=res; *s1 != '\0'; s1++) {
229: for (s2=info->separator; *s2 != '\0'; s2++) {
230: if (*s1 == *s2) {
231: num++;
232: }
233: }
234: }
235: num *= 2; /* need space for variable name and value */
236:
237: val_list = (char **)ecalloc(num, sizeof(char *));
238: len_list = (int *)ecalloc(num, sizeof(int));
239:
240: /* split and decode the query */
241: n = 0;
242: strtok_buf = NULL;
243: var = php_strtok_r(res, info->separator, &strtok_buf);
244: while (var) {
245: val = strchr(var, '=');
246: if (val) { /* have a value */
247: len_list[n] = php_url_decode(var, val-var);
248: val_list[n] = var;
249: n++;
250:
251: *val++ = '\0';
252: val_list[n] = val;
253: len_list[n] = php_url_decode(val, strlen(val));
254: } else {
255: len_list[n] = php_url_decode(var, strlen(var));
256: val_list[n] = var;
257: n++;
258:
259: val_list[n] = "";
260: len_list[n] = 0;
261: }
262: n++;
263: var = php_strtok_r(NULL, info->separator, &strtok_buf);
264: }
265: num = n; /* make sure to process initilized vars only */
266:
267: /* initialize converter */
268: if (info->num_from_encodings <= 0) {
269: from_encoding = mbfl_no_encoding_pass;
270: } else if (info->num_from_encodings == 1) {
271: from_encoding = info->from_encodings[0];
272: } else {
273: /* auto detect */
274: from_encoding = mbfl_no_encoding_invalid;
275: identd = mbfl_encoding_detector_new((enum mbfl_no_encoding *)info->from_encodings, info->num_from_encodings, MBSTRG(strict_detection));
276: if (identd) {
277: n = 0;
278: while (n < num) {
279: string.val = (unsigned char *)val_list[n];
280: string.len = len_list[n];
281: if (mbfl_encoding_detector_feed(identd, &string)) {
282: break;
283: }
284: n++;
285: }
286: from_encoding = mbfl_encoding_detector_judge(identd);
287: mbfl_encoding_detector_delete(identd);
288: }
289: if (from_encoding == mbfl_no_encoding_invalid) {
290: if (info->report_errors) {
291: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to detect encoding");
292: }
293: from_encoding = mbfl_no_encoding_pass;
294: }
295: }
296:
297: convd = NULL;
298: if (from_encoding != mbfl_no_encoding_pass) {
299: convd = mbfl_buffer_converter_new(from_encoding, info->to_encoding, 0);
300: if (convd != NULL) {
301: mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
302: mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));
303: } else {
304: if (info->report_errors) {
305: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create converter");
306: }
307: goto out;
308: }
309: }
310:
311: /* convert encoding */
312: string.no_encoding = from_encoding;
313:
314: n = 0;
315: while (n < num) {
316: string.val = (unsigned char *)val_list[n];
317: string.len = len_list[n];
318: if (convd != NULL && mbfl_buffer_converter_feed_result(convd, &string, &resvar) != NULL) {
319: var = (char *)resvar.val;
320: } else {
321: var = val_list[n];
322: }
323: n++;
324: string.val = val_list[n];
325: string.len = len_list[n];
326: if (convd != NULL && mbfl_buffer_converter_feed_result(convd, &string, &resval) != NULL) {
327: val = resval.val;
328: val_len = resval.len;
329: } else {
330: val = val_list[n];
331: val_len = len_list[n];
332: }
333: n++;
334: /* we need val to be emalloc()ed */
335: val = estrndup(val, val_len);
336: if (sapi_module.input_filter(info->data_type, var, &val, val_len, &new_val_len TSRMLS_CC)) {
337: /* add variable to symbol table */
338: php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
339: }
340: efree(val);
341:
342: if (convd != NULL){
343: mbfl_string_clear(&resvar);
344: mbfl_string_clear(&resval);
345: }
346: }
347:
348: out:
349: /* register_global stuff */
350: if (info->force_register_globals && !prev_rg_state) {
351: zend_alter_ini_entry("register_globals", sizeof("register_globals"), "0", sizeof("0")-1, PHP_INI_PERDIR, PHP_INI_STAGE_RUNTIME);
352: }
353:
354: if (convd != NULL) {
355: MBSTRG(illegalchars) += mbfl_buffer_illegalchars(convd);
356: mbfl_buffer_converter_delete(convd);
357: }
358: if (val_list != NULL) {
359: efree((void *)val_list);
360: }
361: if (len_list != NULL) {
362: efree((void *)len_list);
363: }
364:
365: return from_encoding;
366: }
367: /* }}} */
368:
369: /* {{{ SAPI_POST_HANDLER_FUNC(php_mb_post_handler) */
370: SAPI_POST_HANDLER_FUNC(php_mb_post_handler)
371: {
372: enum mbfl_no_encoding detected;
373: php_mb_encoding_handler_info_t info;
374:
375: MBSTRG(http_input_identify_post) = mbfl_no_encoding_invalid;
376:
377: info.data_type = PARSE_POST;
378: info.separator = "&";
379: info.force_register_globals = 0;
380: info.report_errors = 0;
381: info.to_encoding = MBSTRG(internal_encoding);
382: info.to_language = MBSTRG(language);
383: info.from_encodings = MBSTRG(http_input_list);
384: info.num_from_encodings = MBSTRG(http_input_list_size);
385: info.from_language = MBSTRG(language);
386:
387: detected = _php_mb_encoding_handler_ex(&info, arg, SG(request_info).post_data TSRMLS_CC);
388:
389: MBSTRG(http_input_identify) = detected;
390: if (detected != mbfl_no_encoding_invalid) {
391: MBSTRG(http_input_identify_post) = detected;
392: }
393: }
394: /* }}} */
395:
396: #endif /* HAVE_MBSTRING */
397:
398: /*
399: * Local variables:
400: * tab-width: 4
401: * c-basic-offset: 4
402: * End:
403: * vim600: fdm=marker
404: * vim: noet sw=4 ts=4
405: */
406:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>