1999-04-22 10:48:28 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
1999-04-22 10:48:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-07-16 21:13:16 +08:00
|
|
|
| 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
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
2001-04-06 09:50:40 +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"
|
2000-08-21 17:50:53 +08:00
|
|
|
#include "php_content_types.h"
|
1999-04-27 01:26:37 +08:00
|
|
|
#include "SAPI.h"
|
1999-04-22 10:48:28 +08:00
|
|
|
|
|
|
|
#include "zend_globals.h"
|
|
|
|
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
PHPAPI void php_register_variable(char *var, char *strval, zval *track_vars_array TSRMLS_DC)
|
2001-07-27 18:16:41 +08:00
|
|
|
{
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable_safe(var, strval, strlen(strval), track_vars_array TSRMLS_CC);
|
2000-12-12 18:47:47 +08:00
|
|
|
}
|
|
|
|
|
2001-11-25 00:05:22 +08:00
|
|
|
|
2000-12-12 18:47:47 +08:00
|
|
|
/* binary-safe version */
|
2001-07-28 19:36:37 +08:00
|
|
|
PHPAPI void php_register_variable_safe(char *var, char *strval, int str_len, zval *track_vars_array TSRMLS_DC)
|
2000-02-20 04:12:26 +08:00
|
|
|
{
|
|
|
|
zval new_entry;
|
|
|
|
|
|
|
|
/* Prepare value */
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRLEN(new_entry) = str_len;
|
2000-02-20 04:12:26 +08:00
|
|
|
if (PG(magic_quotes_gpc)) {
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL(new_entry) = php_addslashes(strval, Z_STRLEN(new_entry), &Z_STRLEN(new_entry), 0 TSRMLS_CC);
|
2000-02-20 04:12:26 +08:00
|
|
|
} else {
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL(new_entry) = estrndup(strval, Z_STRLEN(new_entry));
|
2000-02-20 04:12:26 +08:00
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE(new_entry) = IS_STRING;
|
2000-02-20 04:12:26 +08:00
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC);
|
2000-02-20 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
PHPAPI void php_register_variable_ex(char *var, zval *val, pval *track_vars_array TSRMLS_DC)
|
1999-09-13 07:51:12 +08:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
char *ip; /* index pointer */
|
|
|
|
char *index;
|
2000-02-20 04:12:26 +08:00
|
|
|
int var_len, index_len;
|
1999-09-13 07:51:12 +08:00
|
|
|
zval *gpc_element, **gpc_element_p, **top_gpc_p=NULL;
|
|
|
|
zend_bool is_array;
|
|
|
|
HashTable *symtable1=NULL;
|
|
|
|
HashTable *symtable2=NULL;
|
2000-09-06 03:06:29 +08:00
|
|
|
|
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) {
|
2001-09-26 05:58:48 +08:00
|
|
|
symtable2 = Z_ARRVAL_P(track_vars_array);
|
1999-09-13 07:51:12 +08:00
|
|
|
} else {
|
2001-09-26 05:58:48 +08:00
|
|
|
symtable1 = Z_ARRVAL_P(track_vars_array);
|
1999-09-13 07:51:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!symtable1) {
|
2000-09-06 03:06:29 +08:00
|
|
|
/* Nothing to do */
|
2000-02-20 04:12:26 +08:00
|
|
|
zval_dtor(val);
|
1999-09-13 07:51:12 +08:00
|
|
|
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 */
|
2000-02-20 04:12:26 +08:00
|
|
|
zval_dtor(val);
|
1999-09-13 07:51:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index = var;
|
|
|
|
index_len = var_len;
|
|
|
|
|
|
|
|
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 */
|
2001-08-06 11:50:52 +08:00
|
|
|
escaped_index = php_addslashes(index, index_len, &index_len, 0 TSRMLS_CC);
|
1999-09-13 07:51:12 +08:00
|
|
|
} else {
|
|
|
|
escaped_index = index;
|
|
|
|
}
|
|
|
|
if (zend_hash_find(symtable1, escaped_index, index_len+1, (void **) &gpc_element_p)==FAILURE
|
2001-09-26 05:58:48 +08:00
|
|
|
|| Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
|
1999-09-13 07:51:12 +08:00
|
|
|
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;
|
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
symtable1 = Z_ARRVAL_PP(gpc_element_p);
|
1999-09-13 07:51:12 +08:00
|
|
|
/* 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);
|
2000-02-20 04:12:26 +08:00
|
|
|
gpc_element->value = val->value;
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE_P(gpc_element) = Z_TYPE_P(val);
|
1999-09-13 07:51:12 +08:00
|
|
|
if (!index) {
|
|
|
|
zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
|
|
|
|
} else {
|
2000-03-24 19:16:24 +08:00
|
|
|
zend_hash_update(symtable1, index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
|
1999-09-13 07:51:12 +08:00
|
|
|
}
|
|
|
|
if (!top_gpc_p) {
|
|
|
|
top_gpc_p = gpc_element_p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-02 04:42:56 +08:00
|
|
|
if (top_gpc_p) {
|
|
|
|
if (symtable2) {
|
2000-03-24 19:16:24 +08:00
|
|
|
zend_hash_update(symtable2, var, var_len+1, top_gpc_p, sizeof(zval *), NULL);
|
1999-12-02 04:42:56 +08:00
|
|
|
(*top_gpc_p)->refcount++;
|
|
|
|
}
|
|
|
|
}
|
1999-09-13 07:51:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-16 02:01:48 +08:00
|
|
|
SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
|
2000-02-18 04:23:59 +08:00
|
|
|
{
|
|
|
|
char *var, *val;
|
|
|
|
char *strtok_buf = NULL;
|
|
|
|
zval *array_ptr = (zval *) arg;
|
|
|
|
|
2002-07-24 19:25:36 +08:00
|
|
|
if (SG(request_info).post_data==NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-05-04 18:38:17 +08:00
|
|
|
var = php_strtok_r(SG(request_info).post_data, "&", &strtok_buf);
|
2000-02-18 04:23:59 +08:00
|
|
|
|
|
|
|
while (var) {
|
|
|
|
val = strchr(var, '=');
|
|
|
|
if (val) { /* have a value */
|
2000-12-12 18:47:47 +08:00
|
|
|
int val_len;
|
|
|
|
|
2000-02-18 04:23:59 +08:00
|
|
|
*val++ = '\0';
|
|
|
|
php_url_decode(var, strlen(var));
|
2000-12-12 18:47:47 +08:00
|
|
|
val_len = php_url_decode(val, strlen(val));
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable_safe(var, val, val_len, array_ptr TSRMLS_CC);
|
2000-02-18 04:23:59 +08:00
|
|
|
}
|
2000-05-04 18:38:17 +08:00
|
|
|
var = php_strtok_r(NULL, "&", &strtok_buf);
|
2000-02-18 04:23:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
void php_treat_data(int arg, char *str, zval* destArray TSRMLS_DC)
|
1999-04-22 10:48:28 +08:00
|
|
|
{
|
2001-04-05 04:46:26 +08:00
|
|
|
char *res = NULL, *var, *val, *separator=NULL;
|
2000-11-02 23:47:02 +08:00
|
|
|
const char *c_var;
|
1999-04-22 10:48:28 +08:00
|
|
|
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:
|
2000-09-06 03:06:29 +08:00
|
|
|
ALLOC_ZVAL(array_ptr);
|
|
|
|
array_init(array_ptr);
|
|
|
|
INIT_PZVAL(array_ptr);
|
|
|
|
switch (arg) {
|
|
|
|
case PARSE_POST:
|
|
|
|
PG(http_globals)[TRACK_VARS_POST] = array_ptr;
|
|
|
|
break;
|
|
|
|
case PARSE_GET:
|
|
|
|
PG(http_globals)[TRACK_VARS_GET] = array_ptr;
|
|
|
|
break;
|
|
|
|
case PARSE_COOKIE:
|
|
|
|
PG(http_globals)[TRACK_VARS_COOKIE] = array_ptr;
|
|
|
|
break;
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2000-09-11 22:50:26 +08:00
|
|
|
array_ptr=destArray;
|
1999-04-22 10:48:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-02-18 04:23:59 +08:00
|
|
|
if (arg==PARSE_POST) {
|
2001-07-28 19:36:37 +08:00
|
|
|
sapi_handle_post(array_ptr TSRMLS_CC);
|
2000-02-18 04:23:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg == PARSE_GET) { /* GET data */
|
2000-11-02 23:47:02 +08:00
|
|
|
c_var = SG(request_info).query_string;
|
|
|
|
if (c_var && *c_var) {
|
|
|
|
res = (char *) estrdup(c_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 */
|
2000-11-02 23:47:02 +08:00
|
|
|
c_var = SG(request_info).cookie_data;
|
|
|
|
if (c_var && *c_var) {
|
|
|
|
res = (char *) estrdup(c_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
|
|
|
}
|
2000-02-18 04:23:59 +08:00
|
|
|
|
1999-04-22 10:48:28 +08:00
|
|
|
if (!res) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-04-05 04:46:26 +08:00
|
|
|
switch (arg) {
|
|
|
|
case PARSE_GET:
|
|
|
|
case PARSE_STRING:
|
|
|
|
separator = (char *) estrdup(PG(arg_separator).input);
|
|
|
|
break;
|
|
|
|
case PARSE_COOKIE:
|
|
|
|
separator = ";\0";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-04-04 08:37:06 +08:00
|
|
|
var = php_strtok_r(res, separator, &strtok_buf);
|
|
|
|
|
2000-02-18 04:23:59 +08:00
|
|
|
while (var) {
|
|
|
|
val = strchr(var, '=');
|
|
|
|
if (val) { /* have a value */
|
2000-12-12 18:47:47 +08:00
|
|
|
int val_len;
|
|
|
|
|
2000-02-18 04:23:59 +08:00
|
|
|
*val++ = '\0';
|
|
|
|
php_url_decode(var, strlen(var));
|
2000-12-12 18:47:47 +08:00
|
|
|
val_len = php_url_decode(val, strlen(val));
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable_safe(var, val, val_len, array_ptr TSRMLS_CC);
|
2000-02-18 04:23:59 +08:00
|
|
|
}
|
2001-04-04 08:37:06 +08:00
|
|
|
var = php_strtok_r(NULL, separator, &strtok_buf);
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
2001-04-05 04:46:26 +08:00
|
|
|
|
|
|
|
if(arg != PARSE_COOKIE) {
|
|
|
|
efree(separator);
|
|
|
|
}
|
|
|
|
|
1999-05-10 02:40:59 +08:00
|
|
|
if (free_buffer) {
|
|
|
|
efree(res);
|
|
|
|
}
|
1999-04-22 10:48:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-28 19:36:37 +08:00
|
|
|
void php_import_environment_variables(zval *array_ptr TSRMLS_DC)
|
2000-01-29 01:24:53 +08:00
|
|
|
{
|
|
|
|
char **env, *p, *t;
|
|
|
|
|
|
|
|
for (env = environ; env != NULL && *env != NULL; env++) {
|
|
|
|
p = strchr(*env, '=');
|
|
|
|
if (!p) { /* malformed entry? */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
t = estrndup(*env, p - *env);
|
2001-07-28 19:36:37 +08:00
|
|
|
php_register_variable(t, p+1, array_ptr TSRMLS_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:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
1999-04-22 10:48:28 +08:00
|
|
|
*/
|