1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2015 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 | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
16 | Zeev Suraski <zeev@zend.com> |
17 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #include <stdio.h>
23 #include "php.h"
24 #include "ext/standard/php_standard.h"
25 #include "ext/standard/credits.h"
26 #include "php_variables.h"
27 #include "php_globals.h"
28 #include "php_content_types.h"
29 #include "SAPI.h"
30 #include "zend_globals.h"
31
32 /* for systems that need to override reading of environment variables */
33 void _php_import_environment_variables(zval *array_ptr TSRMLS_DC);
34 PHPAPI void (*php_import_environment_variables)(zval *array_ptr TSRMLS_DC) = _php_import_environment_variables;
35
php_register_variable(char * var,char * strval,zval * track_vars_array TSRMLS_DC)36 PHPAPI void php_register_variable(char *var, char *strval, zval *track_vars_array TSRMLS_DC)
37 {
38 php_register_variable_safe(var, strval, strlen(strval), track_vars_array TSRMLS_CC);
39 }
40
41 /* binary-safe version */
php_register_variable_safe(char * var,char * strval,int str_len,zval * track_vars_array TSRMLS_DC)42 PHPAPI void php_register_variable_safe(char *var, char *strval, int str_len, zval *track_vars_array TSRMLS_DC)
43 {
44 zval new_entry;
45 assert(strval != NULL);
46
47 /* Prepare value */
48 Z_STRLEN(new_entry) = str_len;
49 Z_STRVAL(new_entry) = estrndup(strval, Z_STRLEN(new_entry));
50 Z_TYPE(new_entry) = IS_STRING;
51
52 php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC);
53 }
54
php_register_variable_ex(char * var_name,zval * val,zval * track_vars_array TSRMLS_DC)55 PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars_array TSRMLS_DC)
56 {
57 char *p = NULL;
58 char *ip = NULL; /* index pointer */
59 char *index;
60 char *var, *var_orig;
61 int var_len, index_len;
62 zval *gpc_element, **gpc_element_p;
63 zend_bool is_array = 0;
64 HashTable *symtable1 = NULL;
65 ALLOCA_FLAG(use_heap)
66
67 assert(var_name != NULL);
68
69 if (track_vars_array) {
70 symtable1 = Z_ARRVAL_P(track_vars_array);
71 }
72
73 if (!symtable1) {
74 /* Nothing to do */
75 zval_dtor(val);
76 return;
77 }
78
79
80 /* ignore leading spaces in the variable name */
81 while (*var_name && *var_name==' ') {
82 var_name++;
83 }
84
85 /*
86 * Prepare variable name
87 */
88 var_len = strlen(var_name);
89 var = var_orig = do_alloca(var_len + 1, use_heap);
90 memcpy(var_orig, var_name, var_len + 1);
91
92 /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
93 for (p = var; *p; p++) {
94 if (*p == ' ' || *p == '.') {
95 *p='_';
96 } else if (*p == '[') {
97 is_array = 1;
98 ip = p;
99 *p = 0;
100 break;
101 }
102 }
103 var_len = p - var;
104
105 if (var_len==0) { /* empty variable name, or variable name with a space in it */
106 zval_dtor(val);
107 free_alloca(var_orig, use_heap);
108 return;
109 }
110
111 /* GLOBALS hijack attempt, reject parameter */
112 if (symtable1 == EG(active_symbol_table) &&
113 var_len == sizeof("GLOBALS")-1 &&
114 !memcmp(var, "GLOBALS", sizeof("GLOBALS")-1)) {
115 zval_dtor(val);
116 free_alloca(var_orig, use_heap);
117 return;
118 }
119
120 index = var;
121 index_len = var_len;
122
123 if (is_array) {
124 int nest_level = 0;
125 while (1) {
126 char *index_s;
127 int new_idx_len = 0;
128
129 if(++nest_level > PG(max_input_nesting_level)) {
130 HashTable *ht;
131 /* too many levels of nesting */
132
133 if (track_vars_array) {
134 ht = Z_ARRVAL_P(track_vars_array);
135 zend_symtable_del(ht, var, var_len + 1);
136 }
137
138 zval_dtor(val);
139
140 /* do not output the error message to the screen,
141 this helps us to to avoid "information disclosure" */
142 if (!PG(display_errors)) {
143 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variable nesting level exceeded %ld. To increase the limit change max_input_nesting_level in php.ini.", PG(max_input_nesting_level));
144 }
145 free_alloca(var_orig, use_heap);
146 return;
147 }
148
149 ip++;
150 index_s = ip;
151 if (isspace(*ip)) {
152 ip++;
153 }
154 if (*ip==']') {
155 index_s = NULL;
156 } else {
157 ip = strchr(ip, ']');
158 if (!ip) {
159 /* PHP variables cannot contain '[' in their names, so we replace the character with a '_' */
160 *(index_s - 1) = '_';
161
162 index_len = 0;
163 if (index) {
164 index_len = strlen(index);
165 }
166 goto plain_var;
167 return;
168 }
169 *ip = 0;
170 new_idx_len = strlen(index_s);
171 }
172
173 if (!index) {
174 MAKE_STD_ZVAL(gpc_element);
175 array_init(gpc_element);
176 if (zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p) == FAILURE) {
177 zval_ptr_dtor(&gpc_element);
178 zval_dtor(val);
179 free_alloca(var_orig, use_heap);
180 return;
181 }
182 } else {
183 if (zend_symtable_find(symtable1, index, index_len + 1, (void **) &gpc_element_p) == FAILURE
184 || Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
185 MAKE_STD_ZVAL(gpc_element);
186 array_init(gpc_element);
187 zend_symtable_update(symtable1, index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
188 }
189 }
190 symtable1 = Z_ARRVAL_PP(gpc_element_p);
191 /* ip pointed to the '[' character, now obtain the key */
192 index = index_s;
193 index_len = new_idx_len;
194
195 ip++;
196 if (*ip == '[') {
197 is_array = 1;
198 *ip = 0;
199 } else {
200 goto plain_var;
201 }
202 }
203 } else {
204 plain_var:
205 MAKE_STD_ZVAL(gpc_element);
206 gpc_element->value = val->value;
207 Z_TYPE_P(gpc_element) = Z_TYPE_P(val);
208 if (!index) {
209 if (zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p) == FAILURE) {
210 zval_ptr_dtor(&gpc_element);
211 }
212 } else {
213 /*
214 * According to rfc2965, more specific paths are listed above the less specific ones.
215 * If we encounter a duplicate cookie name, we should skip it, since it is not possible
216 * to have the same (plain text) cookie name for the same path and we should not overwrite
217 * more specific cookies with the less specific ones.
218 */
219 if (PG(http_globals)[TRACK_VARS_COOKIE] &&
220 symtable1 == Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]) &&
221 zend_symtable_exists(symtable1, index, index_len + 1)) {
222 zval_ptr_dtor(&gpc_element);
223 } else {
224 zend_symtable_update(symtable1, index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
225 }
226 }
227 }
228 free_alloca(var_orig, use_heap);
229 }
230
SAPI_POST_HANDLER_FUNC(php_std_post_handler)231 SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
232 {
233 char *var, *val, *e, *s, *p;
234 zval *array_ptr = (zval *) arg;
235 long count = 0;
236
237 if (SG(request_info).post_data == NULL) {
238 return;
239 }
240
241 s = SG(request_info).post_data;
242 e = s + SG(request_info).post_data_length;
243
244 while (s < e && (p = memchr(s, '&', (e - s)))) {
245 last_value:
246 if ((val = memchr(s, '=', (p - s)))) { /* have a value */
247 unsigned int val_len, new_val_len;
248
249 if (++count > PG(max_input_vars)) {
250 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
251 return;
252 }
253 var = s;
254
255 php_url_decode(var, (val - s));
256 val++;
257 val_len = php_url_decode(val, (p - val));
258 val = estrndup(val, val_len);
259 if (sapi_module.input_filter(PARSE_POST, var, &val, val_len, &new_val_len TSRMLS_CC)) {
260 php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
261 }
262 efree(val);
263 }
264 s = p + 1;
265 }
266 if (s < e) {
267 p = e;
268 goto last_value;
269 }
270 }
271
SAPI_INPUT_FILTER_FUNC(php_default_input_filter)272 SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter)
273 {
274 /* TODO: check .ini setting here and apply user-defined input filter */
275 if(new_val_len) *new_val_len = val_len;
276 return 1;
277 }
278
SAPI_TREAT_DATA_FUNC(php_default_treat_data)279 SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
280 {
281 char *res = NULL, *var, *val, *separator = NULL;
282 const char *c_var;
283 zval *array_ptr;
284 int free_buffer = 0;
285 char *strtok_buf = NULL;
286 long count = 0;
287
288 switch (arg) {
289 case PARSE_POST:
290 case PARSE_GET:
291 case PARSE_COOKIE:
292 ALLOC_ZVAL(array_ptr);
293 array_init(array_ptr);
294 INIT_PZVAL(array_ptr);
295 switch (arg) {
296 case PARSE_POST:
297 if (PG(http_globals)[TRACK_VARS_POST]) {
298 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_POST]);
299 }
300 PG(http_globals)[TRACK_VARS_POST] = array_ptr;
301 break;
302 case PARSE_GET:
303 if (PG(http_globals)[TRACK_VARS_GET]) {
304 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_GET]);
305 }
306 PG(http_globals)[TRACK_VARS_GET] = array_ptr;
307 break;
308 case PARSE_COOKIE:
309 if (PG(http_globals)[TRACK_VARS_COOKIE]) {
310 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_COOKIE]);
311 }
312 PG(http_globals)[TRACK_VARS_COOKIE] = array_ptr;
313 break;
314 }
315 break;
316 default:
317 array_ptr = destArray;
318 break;
319 }
320
321 if (arg == PARSE_POST) {
322 sapi_handle_post(array_ptr TSRMLS_CC);
323 return;
324 }
325
326 if (arg == PARSE_GET) { /* GET data */
327 c_var = SG(request_info).query_string;
328 if (c_var && *c_var) {
329 res = (char *) estrdup(c_var);
330 free_buffer = 1;
331 } else {
332 free_buffer = 0;
333 }
334 } else if (arg == PARSE_COOKIE) { /* Cookie data */
335 c_var = SG(request_info).cookie_data;
336 if (c_var && *c_var) {
337 res = (char *) estrdup(c_var);
338 free_buffer = 1;
339 } else {
340 free_buffer = 0;
341 }
342 } else if (arg == PARSE_STRING) { /* String data */
343 res = str;
344 free_buffer = 1;
345 }
346
347 if (!res) {
348 return;
349 }
350
351 switch (arg) {
352 case PARSE_GET:
353 case PARSE_STRING:
354 separator = (char *) estrdup(PG(arg_separator).input);
355 break;
356 case PARSE_COOKIE:
357 separator = ";\0";
358 break;
359 }
360
361 var = php_strtok_r(res, separator, &strtok_buf);
362
363 while (var) {
364 val = strchr(var, '=');
365
366 if (arg == PARSE_COOKIE) {
367 /* Remove leading spaces from cookie names, needed for multi-cookie header where ; can be followed by a space */
368 while (isspace(*var)) {
369 var++;
370 }
371 if (var == val || *var == '\0') {
372 goto next_cookie;
373 }
374 }
375
376 if (++count > PG(max_input_vars)) {
377 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
378 break;
379 }
380
381 if (val) { /* have a value */
382 int val_len;
383 unsigned int new_val_len;
384
385 *val++ = '\0';
386 php_url_decode(var, strlen(var));
387 val_len = php_url_decode(val, strlen(val));
388 val = estrndup(val, val_len);
389 if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len TSRMLS_CC)) {
390 php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
391 }
392 efree(val);
393 } else {
394 int val_len;
395 unsigned int new_val_len;
396
397 php_url_decode(var, strlen(var));
398 val_len = 0;
399 val = estrndup("", val_len);
400 if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len TSRMLS_CC)) {
401 php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
402 }
403 efree(val);
404 }
405 next_cookie:
406 var = php_strtok_r(NULL, separator, &strtok_buf);
407 }
408
409 if (arg != PARSE_COOKIE) {
410 efree(separator);
411 }
412
413 if (free_buffer) {
414 efree(res);
415 }
416 }
417
_php_import_environment_variables(zval * array_ptr TSRMLS_DC)418 void _php_import_environment_variables(zval *array_ptr TSRMLS_DC)
419 {
420 char buf[128];
421 char **env, *p, *t = buf;
422 size_t alloc_size = sizeof(buf);
423 unsigned long nlen; /* ptrdiff_t is not portable */
424
425 for (env = environ; env != NULL && *env != NULL; env++) {
426 p = strchr(*env, '=');
427 if (!p) { /* malformed entry? */
428 continue;
429 }
430 nlen = p - *env;
431 if (nlen >= alloc_size) {
432 alloc_size = nlen + 64;
433 t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
434 }
435 memcpy(t, *env, nlen);
436 t[nlen] = '\0';
437 php_register_variable(t, p + 1, array_ptr TSRMLS_CC);
438 }
439 if (t != buf && t != NULL) {
440 efree(t);
441 }
442 }
443
php_std_auto_global_callback(char * name,uint name_len TSRMLS_DC)444 zend_bool php_std_auto_global_callback(char *name, uint name_len TSRMLS_DC)
445 {
446 zend_printf("%s\n", name);
447 return 0; /* don't rearm */
448 }
449
450 /* {{{ php_build_argv
451 */
php_build_argv(char * s,zval * track_vars_array TSRMLS_DC)452 static void php_build_argv(char *s, zval *track_vars_array TSRMLS_DC)
453 {
454 zval *arr, *argc, *tmp;
455 int count = 0;
456 char *ss, *space;
457
458 if (!(SG(request_info).argc || track_vars_array)) {
459 return;
460 }
461
462 ALLOC_INIT_ZVAL(arr);
463 array_init(arr);
464
465 /* Prepare argv */
466 if (SG(request_info).argc) { /* are we in cli sapi? */
467 int i;
468 for (i = 0; i < SG(request_info).argc; i++) {
469 ALLOC_ZVAL(tmp);
470 Z_TYPE_P(tmp) = IS_STRING;
471 Z_STRLEN_P(tmp) = strlen(SG(request_info).argv[i]);
472 Z_STRVAL_P(tmp) = estrndup(SG(request_info).argv[i], Z_STRLEN_P(tmp));
473 INIT_PZVAL(tmp);
474 if (zend_hash_next_index_insert(Z_ARRVAL_P(arr), &tmp, sizeof(zval *), NULL) == FAILURE) {
475 if (Z_TYPE_P(tmp) == IS_STRING) {
476 efree(Z_STRVAL_P(tmp));
477 }
478 }
479 }
480 } else if (s && *s) {
481 ss = s;
482 while (ss) {
483 space = strchr(ss, '+');
484 if (space) {
485 *space = '\0';
486 }
487 /* auto-type */
488 ALLOC_ZVAL(tmp);
489 Z_TYPE_P(tmp) = IS_STRING;
490 Z_STRLEN_P(tmp) = strlen(ss);
491 Z_STRVAL_P(tmp) = estrndup(ss, Z_STRLEN_P(tmp));
492 INIT_PZVAL(tmp);
493 count++;
494 if (zend_hash_next_index_insert(Z_ARRVAL_P(arr), &tmp, sizeof(zval *), NULL) == FAILURE) {
495 if (Z_TYPE_P(tmp) == IS_STRING) {
496 efree(Z_STRVAL_P(tmp));
497 }
498 }
499 if (space) {
500 *space = '+';
501 ss = space + 1;
502 } else {
503 ss = space;
504 }
505 }
506 }
507
508 /* prepare argc */
509 ALLOC_INIT_ZVAL(argc);
510 if (SG(request_info).argc) {
511 Z_LVAL_P(argc) = SG(request_info).argc;
512 } else {
513 Z_LVAL_P(argc) = count;
514 }
515 Z_TYPE_P(argc) = IS_LONG;
516
517 if (SG(request_info).argc) {
518 Z_ADDREF_P(arr);
519 Z_ADDREF_P(argc);
520 zend_hash_update(&EG(symbol_table), "argv", sizeof("argv"), &arr, sizeof(zval *), NULL);
521 zend_hash_update(&EG(symbol_table), "argc", sizeof("argc"), &argc, sizeof(zval *), NULL);
522 }
523 if (track_vars_array) {
524 Z_ADDREF_P(arr);
525 Z_ADDREF_P(argc);
526 zend_hash_update(Z_ARRVAL_P(track_vars_array), "argv", sizeof("argv"), &arr, sizeof(zval *), NULL);
527 zend_hash_update(Z_ARRVAL_P(track_vars_array), "argc", sizeof("argc"), &argc, sizeof(zval *), NULL);
528 }
529 zval_ptr_dtor(&arr);
530 zval_ptr_dtor(&argc);
531 }
532 /* }}} */
533
534 /* {{{ php_register_server_variables
535 */
php_register_server_variables(TSRMLS_D)536 static inline void php_register_server_variables(TSRMLS_D)
537 {
538 zval *array_ptr = NULL;
539
540 ALLOC_ZVAL(array_ptr);
541 array_init(array_ptr);
542 INIT_PZVAL(array_ptr);
543 if (PG(http_globals)[TRACK_VARS_SERVER]) {
544 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_SERVER]);
545 }
546 PG(http_globals)[TRACK_VARS_SERVER] = array_ptr;
547
548 /* Server variables */
549 if (sapi_module.register_server_variables) {
550 sapi_module.register_server_variables(array_ptr TSRMLS_CC);
551 }
552
553 /* PHP Authentication support */
554 if (SG(request_info).auth_user) {
555 php_register_variable("PHP_AUTH_USER", SG(request_info).auth_user, array_ptr TSRMLS_CC);
556 }
557 if (SG(request_info).auth_password) {
558 php_register_variable("PHP_AUTH_PW", SG(request_info).auth_password, array_ptr TSRMLS_CC);
559 }
560 if (SG(request_info).auth_digest) {
561 php_register_variable("PHP_AUTH_DIGEST", SG(request_info).auth_digest, array_ptr TSRMLS_CC);
562 }
563 /* store request init time */
564 {
565 zval request_time_float, request_time_long;
566 Z_TYPE(request_time_float) = IS_DOUBLE;
567 Z_DVAL(request_time_float) = sapi_get_request_time(TSRMLS_C);
568 php_register_variable_ex("REQUEST_TIME_FLOAT", &request_time_float, array_ptr TSRMLS_CC);
569 Z_TYPE(request_time_long) = IS_LONG;
570 Z_LVAL(request_time_long) = zend_dval_to_lval(Z_DVAL(request_time_float));
571 php_register_variable_ex("REQUEST_TIME", &request_time_long, array_ptr TSRMLS_CC);
572 }
573
574 }
575 /* }}} */
576
577 /* {{{ php_autoglobal_merge
578 */
php_autoglobal_merge(HashTable * dest,HashTable * src TSRMLS_DC)579 static void php_autoglobal_merge(HashTable *dest, HashTable *src TSRMLS_DC)
580 {
581 zval **src_entry, **dest_entry;
582 char *string_key;
583 uint string_key_len;
584 ulong num_key;
585 HashPosition pos;
586 int key_type;
587 int globals_check = (dest == (&EG(symbol_table)));
588
589 zend_hash_internal_pointer_reset_ex(src, &pos);
590 while (zend_hash_get_current_data_ex(src, (void **)&src_entry, &pos) == SUCCESS) {
591 key_type = zend_hash_get_current_key_ex(src, &string_key, &string_key_len, &num_key, 0, &pos);
592 if (Z_TYPE_PP(src_entry) != IS_ARRAY
593 || (key_type == HASH_KEY_IS_STRING && zend_hash_find(dest, string_key, string_key_len, (void **) &dest_entry) != SUCCESS)
594 || (key_type == HASH_KEY_IS_LONG && zend_hash_index_find(dest, num_key, (void **)&dest_entry) != SUCCESS)
595 || Z_TYPE_PP(dest_entry) != IS_ARRAY
596 ) {
597 Z_ADDREF_PP(src_entry);
598 if (key_type == HASH_KEY_IS_STRING) {
599 if (!globals_check || string_key_len != sizeof("GLOBALS") || memcmp(string_key, "GLOBALS", sizeof("GLOBALS") - 1)) {
600 zend_hash_update(dest, string_key, string_key_len, src_entry, sizeof(zval *), NULL);
601 } else {
602 Z_DELREF_PP(src_entry);
603 }
604 } else {
605 zend_hash_index_update(dest, num_key, src_entry, sizeof(zval *), NULL);
606 }
607 } else {
608 SEPARATE_ZVAL(dest_entry);
609 php_autoglobal_merge(Z_ARRVAL_PP(dest_entry), Z_ARRVAL_PP(src_entry) TSRMLS_CC);
610 }
611 zend_hash_move_forward_ex(src, &pos);
612 }
613 }
614 /* }}} */
615
616 static zend_bool php_auto_globals_create_server(const char *name, uint name_len TSRMLS_DC);
617 static zend_bool php_auto_globals_create_env(const char *name, uint name_len TSRMLS_DC);
618 static zend_bool php_auto_globals_create_request(const char *name, uint name_len TSRMLS_DC);
619
620 /* {{{ php_hash_environment
621 */
php_hash_environment(TSRMLS_D)622 PHPAPI int php_hash_environment(TSRMLS_D)
623 {
624 memset(PG(http_globals), 0, sizeof(PG(http_globals)));
625 zend_activate_auto_globals(TSRMLS_C);
626 if (PG(register_argc_argv)) {
627 php_build_argv(SG(request_info).query_string, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
628 }
629 return SUCCESS;
630 }
631 /* }}} */
632
php_auto_globals_create_get(const char * name,uint name_len TSRMLS_DC)633 static zend_bool php_auto_globals_create_get(const char *name, uint name_len TSRMLS_DC)
634 {
635 zval *vars;
636
637 if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) {
638 sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
639 vars = PG(http_globals)[TRACK_VARS_GET];
640 } else {
641 ALLOC_ZVAL(vars);
642 array_init(vars);
643 INIT_PZVAL(vars);
644 if (PG(http_globals)[TRACK_VARS_GET]) {
645 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_GET]);
646 }
647 PG(http_globals)[TRACK_VARS_GET] = vars;
648 }
649
650 zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
651 Z_ADDREF_P(vars);
652
653 return 0; /* don't rearm */
654 }
655
php_auto_globals_create_post(const char * name,uint name_len TSRMLS_DC)656 static zend_bool php_auto_globals_create_post(const char *name, uint name_len TSRMLS_DC)
657 {
658 zval *vars;
659
660 if (PG(variables_order) &&
661 (strchr(PG(variables_order),'P') || strchr(PG(variables_order),'p')) &&
662 !SG(headers_sent) &&
663 SG(request_info).request_method &&
664 !strcasecmp(SG(request_info).request_method, "POST")) {
665 sapi_module.treat_data(PARSE_POST, NULL, NULL TSRMLS_CC);
666 vars = PG(http_globals)[TRACK_VARS_POST];
667 } else {
668 ALLOC_ZVAL(vars);
669 array_init(vars);
670 INIT_PZVAL(vars);
671 if (PG(http_globals)[TRACK_VARS_POST]) {
672 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_POST]);
673 }
674 PG(http_globals)[TRACK_VARS_POST] = vars;
675 }
676
677 zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
678 Z_ADDREF_P(vars);
679
680 return 0; /* don't rearm */
681 }
682
php_auto_globals_create_cookie(const char * name,uint name_len TSRMLS_DC)683 static zend_bool php_auto_globals_create_cookie(const char *name, uint name_len TSRMLS_DC)
684 {
685 zval *vars;
686
687 if (PG(variables_order) && (strchr(PG(variables_order),'C') || strchr(PG(variables_order),'c'))) {
688 sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);
689 vars = PG(http_globals)[TRACK_VARS_COOKIE];
690 } else {
691 ALLOC_ZVAL(vars);
692 array_init(vars);
693 INIT_PZVAL(vars);
694 if (PG(http_globals)[TRACK_VARS_COOKIE]) {
695 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_COOKIE]);
696 }
697 PG(http_globals)[TRACK_VARS_COOKIE] = vars;
698 }
699
700 zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
701 Z_ADDREF_P(vars);
702
703 return 0; /* don't rearm */
704 }
705
php_auto_globals_create_files(const char * name,uint name_len TSRMLS_DC)706 static zend_bool php_auto_globals_create_files(const char *name, uint name_len TSRMLS_DC)
707 {
708 zval *vars;
709
710 if (PG(http_globals)[TRACK_VARS_FILES]) {
711 vars = PG(http_globals)[TRACK_VARS_FILES];
712 } else {
713 ALLOC_ZVAL(vars);
714 array_init(vars);
715 INIT_PZVAL(vars);
716 PG(http_globals)[TRACK_VARS_FILES] = vars;
717 }
718
719 zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
720 Z_ADDREF_P(vars);
721
722 return 0; /* don't rearm */
723 }
724
725 /* Upgly hack to fix HTTP_PROXY issue, see bug #72573 */
check_http_proxy(HashTable * var_table)726 static void check_http_proxy(HashTable *var_table)
727 {
728 if (zend_hash_exists(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY"))) {
729 char *local_proxy = getenv("HTTP_PROXY");
730
731 if (!local_proxy) {
732 zend_hash_del(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY"));
733 } else {
734 zval *local_zval;
735 ALLOC_INIT_ZVAL(local_zval);
736 ZVAL_STRING(local_zval, local_proxy, 1);
737 zend_hash_update(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY"), &local_zval, sizeof(zval **), NULL);
738 }
739 }
740 }
741
php_auto_globals_create_server(const char * name,uint name_len TSRMLS_DC)742 static zend_bool php_auto_globals_create_server(const char *name, uint name_len TSRMLS_DC)
743 {
744 if (PG(variables_order) && (strchr(PG(variables_order),'S') || strchr(PG(variables_order),'s'))) {
745 php_register_server_variables(TSRMLS_C);
746
747 if (PG(register_argc_argv)) {
748 if (SG(request_info).argc) {
749 zval **argc, **argv;
750
751 if (zend_hash_find(&EG(symbol_table), "argc", sizeof("argc"), (void**)&argc) == SUCCESS &&
752 zend_hash_find(&EG(symbol_table), "argv", sizeof("argv"), (void**)&argv) == SUCCESS) {
753 Z_ADDREF_PP(argc);
754 Z_ADDREF_PP(argv);
755 zend_hash_update(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "argv", sizeof("argv"), argv, sizeof(zval *), NULL);
756 zend_hash_update(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "argc", sizeof("argc"), argc, sizeof(zval *), NULL);
757 }
758 } else {
759 php_build_argv(SG(request_info).query_string, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
760 }
761 }
762
763 } else {
764 zval *server_vars=NULL;
765 ALLOC_ZVAL(server_vars);
766 array_init(server_vars);
767 INIT_PZVAL(server_vars);
768 if (PG(http_globals)[TRACK_VARS_SERVER]) {
769 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_SERVER]);
770 }
771 PG(http_globals)[TRACK_VARS_SERVER] = server_vars;
772 }
773
774 check_http_proxy(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]));
775 zend_hash_update(&EG(symbol_table), name, name_len + 1, &PG(http_globals)[TRACK_VARS_SERVER], sizeof(zval *), NULL);
776 Z_ADDREF_P(PG(http_globals)[TRACK_VARS_SERVER]);
777
778 return 0; /* don't rearm */
779 }
780
php_auto_globals_create_env(const char * name,uint name_len TSRMLS_DC)781 static zend_bool php_auto_globals_create_env(const char *name, uint name_len TSRMLS_DC)
782 {
783 zval *env_vars = NULL;
784 ALLOC_ZVAL(env_vars);
785 array_init(env_vars);
786 INIT_PZVAL(env_vars);
787 if (PG(http_globals)[TRACK_VARS_ENV]) {
788 zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_ENV]);
789 }
790 PG(http_globals)[TRACK_VARS_ENV] = env_vars;
791
792 if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
793 php_import_environment_variables(PG(http_globals)[TRACK_VARS_ENV] TSRMLS_CC);
794 }
795
796 check_http_proxy(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV]));
797 zend_hash_update(&EG(symbol_table), name, name_len + 1, &PG(http_globals)[TRACK_VARS_ENV], sizeof(zval *), NULL);
798 Z_ADDREF_P(PG(http_globals)[TRACK_VARS_ENV]);
799
800 return 0; /* don't rearm */
801 }
802
php_auto_globals_create_request(const char * name,uint name_len TSRMLS_DC)803 static zend_bool php_auto_globals_create_request(const char *name, uint name_len TSRMLS_DC)
804 {
805 zval *form_variables;
806 unsigned char _gpc_flags[3] = {0, 0, 0};
807 char *p;
808
809 ALLOC_ZVAL(form_variables);
810 array_init(form_variables);
811 INIT_PZVAL(form_variables);
812
813 if (PG(request_order) != NULL) {
814 p = PG(request_order);
815 } else {
816 p = PG(variables_order);
817 }
818
819 for (; p && *p; p++) {
820 switch (*p) {
821 case 'g':
822 case 'G':
823 if (!_gpc_flags[0]) {
824 php_autoglobal_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]) TSRMLS_CC);
825 _gpc_flags[0] = 1;
826 }
827 break;
828 case 'p':
829 case 'P':
830 if (!_gpc_flags[1]) {
831 php_autoglobal_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_POST]) TSRMLS_CC);
832 _gpc_flags[1] = 1;
833 }
834 break;
835 case 'c':
836 case 'C':
837 if (!_gpc_flags[2]) {
838 php_autoglobal_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]) TSRMLS_CC);
839 _gpc_flags[2] = 1;
840 }
841 break;
842 }
843 }
844
845 zend_hash_update(&EG(symbol_table), name, name_len + 1, &form_variables, sizeof(zval *), NULL);
846 return 0;
847 }
848
php_startup_auto_globals(TSRMLS_D)849 void php_startup_auto_globals(TSRMLS_D)
850 {
851 zend_register_auto_global(ZEND_STRL("_GET"), 0, php_auto_globals_create_get TSRMLS_CC);
852 zend_register_auto_global(ZEND_STRL("_POST"), 0, php_auto_globals_create_post TSRMLS_CC);
853 zend_register_auto_global(ZEND_STRL("_COOKIE"), 0, php_auto_globals_create_cookie TSRMLS_CC);
854 zend_register_auto_global(ZEND_STRL("_SERVER"), PG(auto_globals_jit), php_auto_globals_create_server TSRMLS_CC);
855 zend_register_auto_global(ZEND_STRL("_ENV"), PG(auto_globals_jit), php_auto_globals_create_env TSRMLS_CC);
856 zend_register_auto_global(ZEND_STRL("_REQUEST"), PG(auto_globals_jit), php_auto_globals_create_request TSRMLS_CC);
857 zend_register_auto_global(ZEND_STRL("_FILES"), 0, php_auto_globals_create_files TSRMLS_CC);
858 }
859
860 /*
861 * Local variables:
862 * tab-width: 4
863 * c-basic-offset: 4
864 * End:
865 * vim600: sw=4 ts=4 fdm=marker
866 * vim<600: sw=4 ts=4
867 */
868