1999-04-22 10:48:28 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
1999-07-16 21:13:16 +08:00
|
|
|
| PHP version 4.0 |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-01-01 09:32:05 +08:00
|
|
|
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
1999-07-16 21:13:16 +08:00
|
|
|
| This source file is subject to version 2.0 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
|
|
|
| http://www.php.net/license/2_0.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
|
1999-09-11 23:04:45 +08:00
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
1999-12-02 05:30:45 +08:00
|
|
|
/* $Id: */
|
1999-04-24 04:06:01 +08:00
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "php.h"
|
2000-01-28 21:31:12 +08:00
|
|
|
#include "ext/standard/php_standard.h"
|
2000-01-29 01:24:53 +08:00
|
|
|
#include "php_variables.h"
|
1999-04-22 10:48:28 +08:00
|
|
|
#include "php_globals.h"
|
1999-04-27 01:26:37 +08:00
|
|
|
#include "SAPI.h"
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
#include "zend_globals.h"
|
|
|
|
|
|
|
|
|
2000-01-29 19:55:44 +08:00
|
|
|
PHPAPI void php_register_variable(char *var, char *val, pval *track_vars_array ELS_DC PLS_DC)
|
1999-09-13 07:51:12 +08:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
char *ip; /* index pointer */
|
|
|
|
char *index;
|
|
|
|
int var_len, val_len, index_len;
|
|
|
|
zval *gpc_element, **gpc_element_p, **top_gpc_p=NULL;
|
|
|
|
zend_bool is_array;
|
|
|
|
zend_bool free_index;
|
|
|
|
HashTable *symtable1=NULL;
|
|
|
|
HashTable *symtable2=NULL;
|
|
|
|
|
2000-01-29 02:29:37 +08:00
|
|
|
if (PG(register_globals)) {
|
1999-09-13 07:51:12 +08:00
|
|
|
symtable1 = EG(active_symbol_table);
|
|
|
|
}
|
|
|
|
if (track_vars_array) {
|
|
|
|
if (symtable1) {
|
|
|
|
symtable2 = track_vars_array->value.ht;
|
|
|
|
} else {
|
|
|
|
symtable1 = track_vars_array->value.ht;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symtable1) {
|
|
|
|
/* we don't need track_vars, and we're not setting GPC globals either. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare variable name
|
|
|
|
*/
|
|
|
|
ip = strchr(var, '[');
|
|
|
|
if (ip) {
|
|
|
|
is_array = 1;
|
|
|
|
*ip = 0;
|
|
|
|
} else {
|
|
|
|
is_array = 0;
|
|
|
|
}
|
|
|
|
/* ignore leading spaces in the variable name */
|
|
|
|
while (*var && *var==' ') {
|
|
|
|
var++;
|
|
|
|
}
|
|
|
|
var_len = strlen(var);
|
|
|
|
if (var_len==0) { /* empty variable name, or variable name with a space in it */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* ensure that we don't have spaces or dots in the variable name (not binary safe) */
|
|
|
|
for (p=var; *p; p++) {
|
|
|
|
switch(*p) {
|
|
|
|
case ' ':
|
|
|
|
case '.':
|
|
|
|
*p='_';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare value */
|
|
|
|
val_len = strlen(val);
|
|
|
|
if (PG(magic_quotes_gpc)) {
|
|
|
|
val = php_addslashes(val, val_len, &val_len, 0);
|
|
|
|
} else {
|
|
|
|
val = estrndup(val, val_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = var;
|
|
|
|
index_len = var_len;
|
|
|
|
free_index = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (is_array) {
|
|
|
|
char *escaped_index;
|
|
|
|
|
|
|
|
if (!index) {
|
|
|
|
MAKE_STD_ZVAL(gpc_element);
|
|
|
|
array_init(gpc_element);
|
|
|
|
zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
|
|
|
|
} else {
|
|
|
|
if (PG(magic_quotes_gpc) && (index!=var)) {
|
|
|
|
/* no need to addslashes() the index if it's the main variable name */
|
|
|
|
escaped_index = php_addslashes(index, index_len, &index_len, 0);
|
|
|
|
} else {
|
|
|
|
escaped_index = index;
|
|
|
|
}
|
|
|
|
if (zend_hash_find(symtable1, escaped_index, index_len+1, (void **) &gpc_element_p)==FAILURE
|
|
|
|
|| (*gpc_element_p)->type != IS_ARRAY) {
|
|
|
|
MAKE_STD_ZVAL(gpc_element);
|
|
|
|
array_init(gpc_element);
|
|
|
|
zend_hash_update(symtable1, escaped_index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
|
|
|
|
}
|
|
|
|
if (index!=escaped_index) {
|
|
|
|
efree(escaped_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!top_gpc_p) {
|
|
|
|
top_gpc_p = gpc_element_p;
|
|
|
|
}
|
|
|
|
symtable1 = (*gpc_element_p)->value.ht;
|
|
|
|
/* ip pointed to the '[' character, now obtain the key */
|
|
|
|
index = ++ip;
|
|
|
|
index_len = 0;
|
|
|
|
if (*ip=='\n' || *ip=='\r' || *ip=='\t' || *ip==' ') {
|
|
|
|
ip++;
|
|
|
|
}
|
|
|
|
if (*ip==']') {
|
|
|
|
index = NULL;
|
|
|
|
} else {
|
|
|
|
ip = strchr(ip, ']');
|
|
|
|
if (!ip) {
|
|
|
|
php_error(E_WARNING, "Missing ] in %s variable", var);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*ip = 0;
|
|
|
|
index_len = strlen(index);
|
|
|
|
}
|
|
|
|
ip++;
|
|
|
|
if (*ip=='[') {
|
|
|
|
is_array = 1;
|
|
|
|
*ip = 0;
|
|
|
|
} else {
|
|
|
|
is_array = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MAKE_STD_ZVAL(gpc_element);
|
|
|
|
gpc_element->value.str.val = val;
|
|
|
|
gpc_element->value.str.len = val_len;
|
|
|
|
gpc_element->type = IS_STRING;
|
|
|
|
if (!index) {
|
|
|
|
zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
|
|
|
|
} else {
|
|
|
|
zend_hash_update(symtable1, index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
|
|
|
|
}
|
|
|
|
if (!top_gpc_p) {
|
|
|
|
top_gpc_p = gpc_element_p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-02 04:42:56 +08:00
|
|
|
if (top_gpc_p) {
|
|
|
|
(*top_gpc_p)->is_ref = 1;
|
|
|
|
if (symtable2) {
|
|
|
|
zend_hash_update(symtable2, var, var_len+1, top_gpc_p, sizeof(zval *), NULL);
|
|
|
|
(*top_gpc_p)->refcount++;
|
|
|
|
}
|
|
|
|
}
|
1999-09-13 07:51:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-11 22:09:29 +08:00
|
|
|
void php_treat_data(int arg, char *str ELS_DC PLS_DC SLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
|
|
|
char *res = NULL, *var, *val;
|
|
|
|
pval *array_ptr;
|
1999-05-11 08:01:47 +08:00
|
|
|
int free_buffer=0;
|
1999-11-26 21:34:31 +08:00
|
|
|
char *strtok_buf = NULL;
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
switch (arg) {
|
|
|
|
case PARSE_POST:
|
|
|
|
case PARSE_GET:
|
|
|
|
case PARSE_COOKIE:
|
|
|
|
if (PG(track_vars)) {
|
1999-12-27 05:21:33 +08:00
|
|
|
ALLOC_ZVAL(array_ptr);
|
1999-04-22 10:48:28 +08:00
|
|
|
array_init(array_ptr);
|
1999-07-10 04:45:55 +08:00
|
|
|
INIT_PZVAL(array_ptr);
|
1999-04-22 10:48:28 +08:00
|
|
|
switch (arg) {
|
|
|
|
case PARSE_POST:
|
1999-08-03 03:17:14 +08:00
|
|
|
zend_hash_add(&EG(symbol_table), "HTTP_POST_VARS", sizeof("HTTP_POST_VARS"), &array_ptr, sizeof(pval *),NULL);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case PARSE_GET:
|
1999-08-03 03:17:14 +08:00
|
|
|
zend_hash_add(&EG(symbol_table), "HTTP_GET_VARS", sizeof("HTTP_GET_VARS"), &array_ptr, sizeof(pval *),NULL);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
case PARSE_COOKIE:
|
1999-08-03 03:17:14 +08:00
|
|
|
zend_hash_add(&EG(symbol_table), "HTTP_COOKIE_VARS", sizeof("HTTP_COOKIE_VARS"), &array_ptr, sizeof(pval *),NULL);
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
array_ptr=NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
array_ptr=NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-05-10 02:40:59 +08:00
|
|
|
if (arg == PARSE_POST) { /* POST data */
|
1999-09-11 21:44:23 +08:00
|
|
|
res = SG(request_info).post_data;
|
1999-05-10 02:40:59 +08:00
|
|
|
free_buffer = 0;
|
|
|
|
} else if (arg == PARSE_GET) { /* GET data */
|
1999-05-03 02:07:41 +08:00
|
|
|
var = SG(request_info).query_string;
|
1999-04-22 10:48:28 +08:00
|
|
|
if (var && *var) {
|
|
|
|
res = (char *) estrdup(var);
|
1999-05-10 02:40:59 +08:00
|
|
|
free_buffer = 1;
|
|
|
|
} else {
|
|
|
|
free_buffer = 0;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
} else if (arg == PARSE_COOKIE) { /* Cookie data */
|
1999-05-09 16:48:05 +08:00
|
|
|
var = SG(request_info).cookie_data;
|
1999-04-22 10:48:28 +08:00
|
|
|
if (var && *var) {
|
|
|
|
res = (char *) estrdup(var);
|
1999-05-10 02:40:59 +08:00
|
|
|
free_buffer = 1;
|
|
|
|
} else {
|
|
|
|
free_buffer = 0;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
} else if (arg == PARSE_STRING) { /* String data */
|
|
|
|
res = str;
|
1999-05-10 02:40:59 +08:00
|
|
|
free_buffer = 1;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
if (!res) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-10-06 13:26:25 +08:00
|
|
|
#if HAVE_FDFLIB
|
|
|
|
if((NULL != SG(request_info).content_type) && (0 == strcmp(SG(request_info).content_type, "application/vnd.fdf"))) {
|
|
|
|
pval *tmp;
|
|
|
|
|
1999-12-27 05:21:33 +08:00
|
|
|
ALLOC_ZVAL(tmp);
|
1999-10-06 13:26:25 +08:00
|
|
|
tmp->value.str.len = SG(request_info).post_data_length;
|
|
|
|
tmp->value.str.val = estrndup(SG(request_info).post_data, SG(request_info).post_data_length);
|
|
|
|
tmp->type = IS_STRING;
|
|
|
|
INIT_PZVAL(tmp);
|
|
|
|
zend_hash_add(&EG(symbol_table), "HTTP_FDF_DATA", sizeof("HTTP_FDF_DATA"), &tmp, sizeof(pval *),NULL);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
#endif
|
1999-04-22 10:48:28 +08:00
|
|
|
if (arg == PARSE_COOKIE) {
|
1999-11-26 21:34:31 +08:00
|
|
|
var = strtok_r(res, ";", &strtok_buf);
|
1999-04-22 10:48:28 +08:00
|
|
|
} else if (arg == PARSE_POST) {
|
1999-11-26 21:34:31 +08:00
|
|
|
var = strtok_r(res, "&", &strtok_buf);
|
1999-04-22 10:48:28 +08:00
|
|
|
} else {
|
1999-11-26 21:34:31 +08:00
|
|
|
var = strtok_r(res, PG(arg_separator), &strtok_buf);
|
1999-10-06 13:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (var) {
|
|
|
|
val = strchr(var, '=');
|
|
|
|
if (val) { /* have a value */
|
|
|
|
*val++ = '\0';
|
|
|
|
/* FIXME: XXX: not binary safe, discards returned length */
|
1999-12-18 12:01:20 +08:00
|
|
|
php_url_decode(var, strlen(var));
|
|
|
|
php_url_decode(val, strlen(val));
|
2000-01-29 19:55:44 +08:00
|
|
|
php_register_variable(var, val, array_ptr ELS_CC PLS_CC);
|
1999-10-06 13:26:25 +08:00
|
|
|
}
|
|
|
|
if (arg == PARSE_COOKIE) {
|
1999-11-26 21:34:31 +08:00
|
|
|
var = strtok_r(NULL, ";", &strtok_buf);
|
1999-10-06 13:26:25 +08:00
|
|
|
} else if (arg == PARSE_POST) {
|
1999-11-26 21:34:31 +08:00
|
|
|
var = strtok_r(NULL, "&", &strtok_buf);
|
1999-10-06 13:26:25 +08:00
|
|
|
} else {
|
1999-11-26 21:34:31 +08:00
|
|
|
var = strtok_r(NULL, PG(arg_separator), &strtok_buf);
|
1999-10-06 13:26:25 +08:00
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
1999-10-06 13:26:25 +08:00
|
|
|
#if HAVE_FDFLIB
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
1999-10-06 13:26:25 +08:00
|
|
|
#endif
|
1999-05-10 02:40:59 +08:00
|
|
|
if (free_buffer) {
|
|
|
|
efree(res);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-29 01:24:53 +08:00
|
|
|
|
|
|
|
void php_import_environment_variables(ELS_D PLS_DC)
|
|
|
|
{
|
|
|
|
char **env, *p, *t;
|
|
|
|
zval *array_ptr=NULL;
|
|
|
|
|
|
|
|
if (PG(track_vars)) {
|
|
|
|
ALLOC_ZVAL(array_ptr);
|
|
|
|
array_init(array_ptr);
|
|
|
|
INIT_PZVAL(array_ptr);
|
|
|
|
zend_hash_add(&EG(symbol_table), "HTTP_ENV_VARS", sizeof("HTTP_ENV_VARS"), &array_ptr, sizeof(pval *),NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (env = environ; env != NULL && *env != NULL; env++) {
|
|
|
|
p = strchr(*env, '=');
|
|
|
|
if (!p) { /* malformed entry? */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
t = estrndup(*env, p - *env);
|
2000-01-29 19:55:44 +08:00
|
|
|
php_register_variable(t, p+1, array_ptr ELS_CC PLS_CC);
|
2000-01-29 01:24:53 +08:00
|
|
|
efree(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|