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