Back to index

php5  5.3.10
Defines | Functions | Variables
php_variables.h File Reference
#include "php.h"
#include "SAPI.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define PARSE_POST   0
#define PARSE_GET   1
#define PARSE_COOKIE   2
#define PARSE_STRING   3
#define PARSE_ENV   4
#define PARSE_SERVER   5
#define PARSE_SESSION   6
#define NUM_TRACK_VARS   6

Functions

void php_startup_auto_globals (TSRMLS_D)
PHPAPI void php_register_variable (char *var, char *val, zval *track_vars_array TSRMLS_DC)
PHPAPI void php_register_variable_safe (char *var, char *val, int val_len, zval *track_vars_array TSRMLS_DC)
PHPAPI void php_register_variable_ex (char *var, zval *val, zval *track_vars_array TSRMLS_DC)
int php_hash_environment (TSRMLS_D)

Variables

PHPAPI void(* php_import_environment_variables )(zval *array_ptr TSRMLS_DC)

Define Documentation

#define NUM_TRACK_VARS   6

Definition at line 47 of file php_variables.h.

#define PARSE_COOKIE   2

Definition at line 30 of file php_variables.h.

#define PARSE_ENV   4

Definition at line 32 of file php_variables.h.

#define PARSE_GET   1

Definition at line 29 of file php_variables.h.

#define PARSE_POST   0

Definition at line 28 of file php_variables.h.

#define PARSE_SERVER   5

Definition at line 33 of file php_variables.h.

#define PARSE_SESSION   6

Definition at line 34 of file php_variables.h.

#define PARSE_STRING   3

Definition at line 31 of file php_variables.h.


Function Documentation

Definition at line 678 of file php_variables.c.

{
       char *p;
       unsigned char _gpc_flags[5] = {0, 0, 0, 0, 0};
       zend_bool jit_initialization = (PG(auto_globals_jit) && !PG(register_globals) && !PG(register_long_arrays));
       struct auto_global_record {
              char *name;
              uint name_len;
              char *long_name;
              uint long_name_len;
              zend_bool jit_initialization;
       } auto_global_records[] = {
              { "_POST", sizeof("_POST"), "HTTP_POST_VARS", sizeof("HTTP_POST_VARS"), 0 },
              { "_GET", sizeof("_GET"), "HTTP_GET_VARS", sizeof("HTTP_GET_VARS"), 0 },
              { "_COOKIE", sizeof("_COOKIE"), "HTTP_COOKIE_VARS", sizeof("HTTP_COOKIE_VARS"), 0 },
              { "_SERVER", sizeof("_SERVER"), "HTTP_SERVER_VARS", sizeof("HTTP_SERVER_VARS"), 1 },
              { "_ENV", sizeof("_ENV"), "HTTP_ENV_VARS", sizeof("HTTP_ENV_VARS"), 1 },
              { "_FILES", sizeof("_FILES"), "HTTP_POST_FILES", sizeof("HTTP_POST_FILES"), 0 },
       };
       size_t num_track_vars = sizeof(auto_global_records)/sizeof(struct auto_global_record);
       size_t i;

       /* jit_initialization = 0; */
       for (i=0; i<num_track_vars; i++) {
              PG(http_globals)[i] = NULL;
       }

       for (p=PG(variables_order); p && *p; p++) {
              switch(*p) {
                     case 'p':
                     case 'P':
                            if (!_gpc_flags[0] && !SG(headers_sent) && SG(request_info).request_method && !strcasecmp(SG(request_info).request_method, "POST")) {
                                   sapi_module.treat_data(PARSE_POST, NULL, NULL TSRMLS_CC);      /* POST Data */
                                   _gpc_flags[0] = 1;
                                   if (PG(register_globals)) {
                                          php_autoglobal_merge(&EG(symbol_table), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_POST]) TSRMLS_CC);
                                   }
                            }
                            break;
                     case 'c':
                     case 'C':
                            if (!_gpc_flags[1]) {
                                   sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);    /* Cookie Data */
                                   _gpc_flags[1] = 1;
                                   if (PG(register_globals)) {
                                          php_autoglobal_merge(&EG(symbol_table), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]) TSRMLS_CC);
                                   }
                            }
                            break;
                     case 'g':
                     case 'G':
                            if (!_gpc_flags[2]) {
                                   sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);       /* GET Data */
                                   _gpc_flags[2] = 1;
                                   if (PG(register_globals)) {
                                          php_autoglobal_merge(&EG(symbol_table), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]) TSRMLS_CC);
                                   }
                            }
                            break;
                     case 'e':
                     case 'E':
                            if (!jit_initialization && !_gpc_flags[3]) {
                                   zend_auto_global_disable_jit("_ENV", sizeof("_ENV")-1 TSRMLS_CC);
                                   php_auto_globals_create_env("_ENV", sizeof("_ENV")-1 TSRMLS_CC);
                                   _gpc_flags[3] = 1;
                                   if (PG(register_globals)) {
                                          php_autoglobal_merge(&EG(symbol_table), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV]) TSRMLS_CC);
                                   }
                            }
                            break;
                     case 's':
                     case 'S':
                            if (!jit_initialization && !_gpc_flags[4]) {
                                   zend_auto_global_disable_jit("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
                                   php_register_server_variables(TSRMLS_C);
                                   _gpc_flags[4] = 1;
                                   if (PG(register_globals)) {
                                          php_autoglobal_merge(&EG(symbol_table), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]) TSRMLS_CC);
                                   }
                            }
                            break;
              }
       }

       /* argv/argc support */
       if (PG(register_argc_argv)) {
              php_build_argv(SG(request_info).query_string, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
       }

       for (i=0; i<num_track_vars; i++) {
              if (jit_initialization && auto_global_records[i].jit_initialization) {
                     continue;
              }
              if (!PG(http_globals)[i]) {
                     ALLOC_ZVAL(PG(http_globals)[i]);
                     array_init(PG(http_globals)[i]);
                     INIT_PZVAL(PG(http_globals)[i]);
              }

              Z_ADDREF_P(PG(http_globals)[i]);
              zend_hash_update(&EG(symbol_table), auto_global_records[i].name, auto_global_records[i].name_len, &PG(http_globals)[i], sizeof(zval *), NULL);
              if (PG(register_long_arrays)) {
                     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);
                     Z_ADDREF_P(PG(http_globals)[i]);
              }
       }

       /* Create _REQUEST */
       if (!jit_initialization) {
              zend_auto_global_disable_jit("_REQUEST", sizeof("_REQUEST")-1 TSRMLS_CC);
              php_auto_globals_create_request("_REQUEST", sizeof("_REQUEST")-1 TSRMLS_CC);
       }

       return SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI void php_register_variable ( char *  var,
char *  val,
zval *track_vars_array  TSRMLS_DC 
)

Definition at line 37 of file php_variables.c.

{
       php_register_variable_safe(var, strval, strlen(strval), track_vars_array TSRMLS_CC);
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI void php_register_variable_ex ( char *  var,
zval *  val,
zval *track_vars_array  TSRMLS_DC 
)

Definition at line 60 of file php_variables.c.

{
       char *p = NULL;
       char *ip;            /* index pointer */
       char *index, *escaped_index = NULL;
       char *var, *var_orig;
       int var_len, index_len;
       zval *gpc_element, **gpc_element_p;
       zend_bool is_array = 0;
       HashTable *symtable1 = NULL;

       assert(var_name != NULL);

       if (track_vars_array) {
              symtable1 = Z_ARRVAL_P(track_vars_array);
       } else if (PG(register_globals)) {
              if (!EG(active_symbol_table)) {
                     zend_rebuild_symbol_table(TSRMLS_C);
              }
              symtable1 = EG(active_symbol_table);
       }
       if (!symtable1) {
              /* Nothing to do */
              zval_dtor(val);
              return;
       }

       /*
        * Prepare variable name
        */

       var_orig = estrdup(var_name);
       var = var_orig;
       /* ignore leading spaces in the variable name */
       while (*var && *var==' ') {
              var++;
       }

       /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
       for (p = var; *p; p++) {
              if (*p == ' ' || *p == '.') {
                     *p='_';
              } else if (*p == '[') {
                     is_array = 1;
                     ip = p;
                     *p = 0;
                     break;
              }
       }
       var_len = p - var;

       if (var_len==0) { /* empty variable name, or variable name with a space in it */
              zval_dtor(val);
              efree(var_orig);
              return;
       }

       /* GLOBALS hijack attempt, reject parameter */
       if (symtable1 == EG(active_symbol_table) &&
              var_len == sizeof("GLOBALS")-1 &&
              !memcmp(var, "GLOBALS", sizeof("GLOBALS")-1)) {
              zval_dtor(val);
              efree(var_orig);
              return;
       }

       index = var;
       index_len = var_len;

       if (is_array) {
              int nest_level = 0;
              while (1) {
                     char *index_s;
                     int new_idx_len = 0;

                     if(++nest_level > PG(max_input_nesting_level)) {
                            HashTable *ht;
                            /* too many levels of nesting */

                            if (track_vars_array) {
                                   ht = Z_ARRVAL_P(track_vars_array);
                                   zend_hash_del(ht, var, var_len + 1);
                            } else if (PG(register_globals)) {
                                   ht = EG(active_symbol_table);
                                   zend_hash_del(ht, var, var_len + 1);
                            }

                            zval_dtor(val);

                            /* do not output the error message to the screen,
                             this helps us to to avoid "information disclosure" */
                            if (!PG(display_errors)) {
                                   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));
                            }
                            efree(var_orig);
                            return;
                     }

                     ip++;
                     index_s = ip;
                     if (isspace(*ip)) {
                            ip++;
                     }
                     if (*ip==']') {
                            index_s = NULL;
                     } else {
                            ip = strchr(ip, ']');
                            if (!ip) {
                                   /* PHP variables cannot contain '[' in their names, so we replace the character with a '_' */
                                   *(index_s - 1) = '_';

                                   index_len = 0;
                                   if (index) {
                                          index_len = strlen(index);
                                   }
                                   goto plain_var;
                                   return;
                            }
                            *ip = 0;
                            new_idx_len = strlen(index_s);     
                     }

                     if (!index) {
                            MAKE_STD_ZVAL(gpc_element);
                            array_init(gpc_element);
                            if (zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p) == FAILURE) {
                                   zval_ptr_dtor(&gpc_element);
                                   zval_dtor(val);
                                   efree(var_orig);
                                   return;
                            }
                     } else {
                            if (PG(magic_quotes_gpc)) {
                                   escaped_index = php_addslashes(index, index_len, &index_len, 0 TSRMLS_CC);
                            } else {
                                   escaped_index = index;
                            }
                            if (zend_symtable_find(symtable1, escaped_index, index_len + 1, (void **) &gpc_element_p) == FAILURE
                                   || Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
                                   if (zend_hash_num_elements(symtable1) <= PG(max_input_vars)) {
                                          if (zend_hash_num_elements(symtable1) == PG(max_input_vars)) {
                                                 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));
                                          }
                                          MAKE_STD_ZVAL(gpc_element);
                                          array_init(gpc_element);
                                          zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
                                   } else {
                                          if (index != escaped_index) {
                                                 efree(escaped_index);
                                          }
                                          zval_dtor(val);
                                          efree(var_orig);
                                          return;
                                   }
                            }
                            if (index != escaped_index) {
                                   efree(escaped_index);
                            }
                     }
                     symtable1 = Z_ARRVAL_PP(gpc_element_p);
                     /* ip pointed to the '[' character, now obtain the key */
                     index = index_s;
                     index_len = new_idx_len;

                     ip++;
                     if (*ip == '[') {
                            is_array = 1;
                            *ip = 0;
                     } else {
                            goto plain_var;
                     }
              }
       } else {
plain_var:
              MAKE_STD_ZVAL(gpc_element);
              gpc_element->value = val->value;
              Z_TYPE_P(gpc_element) = Z_TYPE_P(val);
              if (!index) {
                     if (zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p) == FAILURE) {
                            zval_ptr_dtor(&gpc_element);
                     }
              } else {
                     if (PG(magic_quotes_gpc)) { 
                            escaped_index = php_addslashes(index, index_len, &index_len, 0 TSRMLS_CC);
                     } else {
                            escaped_index = index;
                     }
                     /* 
                      * According to rfc2965, more specific paths are listed above the less specific ones.
                      * If we encounter a duplicate cookie name, we should skip it, since it is not possible
                      * to have the same (plain text) cookie name for the same path and we should not overwrite
                      * more specific cookies with the less specific ones.
                      */
                     if (PG(http_globals)[TRACK_VARS_COOKIE] &&
                            symtable1 == Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]) &&
                            zend_symtable_exists(symtable1, escaped_index, index_len + 1)) {
                            zval_ptr_dtor(&gpc_element);
                     } else {
                            if (zend_hash_num_elements(symtable1) <= PG(max_input_vars)) {
                                   if (zend_hash_num_elements(symtable1) == PG(max_input_vars)) {
                                          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));
                                   }
                                   zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
                            } else {
                                   zval_ptr_dtor(&gpc_element);
                            }
                     }
                     if (escaped_index != index) {
                            efree(escaped_index);
                     }
              }
       }
       efree(var_orig);
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI void php_register_variable_safe ( char *  var,
char *  val,
int  val_len,
zval *track_vars_array  TSRMLS_DC 
)

Definition at line 43 of file php_variables.c.

{
       zval new_entry;
       assert(strval != NULL);
       
       /* Prepare value */
       Z_STRLEN(new_entry) = str_len;
       if (PG(magic_quotes_gpc)) {
              Z_STRVAL(new_entry) = php_addslashes(strval, Z_STRLEN(new_entry), &Z_STRLEN(new_entry), 0 TSRMLS_CC);
       } else {
              Z_STRVAL(new_entry) = estrndup(strval, Z_STRLEN(new_entry));
       }
       Z_TYPE(new_entry) = IS_STRING;

       php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 910 of file php_variables.c.

{
       zend_register_auto_global("_GET", sizeof("_GET")-1, NULL TSRMLS_CC);
       zend_register_auto_global("_POST", sizeof("_POST")-1, NULL TSRMLS_CC);
       zend_register_auto_global("_COOKIE", sizeof("_COOKIE")-1, NULL TSRMLS_CC);
       zend_register_auto_global("_SERVER", sizeof("_SERVER")-1, php_auto_globals_create_server TSRMLS_CC);
       zend_register_auto_global("_ENV", sizeof("_ENV")-1, php_auto_globals_create_env TSRMLS_CC);
       zend_register_auto_global("_REQUEST", sizeof("_REQUEST")-1, php_auto_globals_create_request TSRMLS_CC);
       zend_register_auto_global("_FILES", sizeof("_FILES")-1, NULL TSRMLS_CC);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 35 of file php_variables.c.