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