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