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