1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999 The PHP Group |
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id: */
#include <stdio.h>
#include "php.h"
#include "php3_standard.h"
#include "php_globals.h"
#include "SAPI.h"
#include "zend_globals.h"
/*
* parse Get/Post/Cookie string and create appropriate variable
*
* This is a tad ugly because it was yanked out of the middle of
* the old TreatData function. This is a temporary measure filling
* the gap until a more flexible parser can be built to do this.
*/
void php_parse_gpc_data(char *val, char *var, pval *track_vars_array ELS_DC PLS_DC)
{
int var_type;
char *ind, *tmp = NULL, *array_index = NULL;
int var_len, val_len;
pval *gpc_element;
zend_bool do_insert;
if (!PG(gpc_globals) && !track_vars_array) {
/* we don't need track_vars, and we're not setting GPC globals either. */
return;
}
var_type = php3_check_ident_type(var);
if (var_type == GPC_INDEXED_ARRAY) {
ind = php3_get_ident_index(var);
if (PG(magic_quotes_gpc)) {
array_index = php_addslashes(ind, 0, NULL, 1);
} else {
array_index = ind;
}
}
if (var_type & GPC_ARRAY) { /* array (indexed or not) */
tmp = strchr(var, '[');
if (tmp) {
*tmp = '\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 (tmp=var; *tmp; tmp++) {
switch(*tmp) {
case ' ':
case '.':
*tmp='_';
break;
}
}
val_len = strlen(val);
if (PG(magic_quotes_gpc)) {
val = php_addslashes(val, val_len, &val_len, 0);
} else {
val = estrndup(val, val_len);
}
if (var_type & GPC_ARRAY) {
pval *gpc_element;
pval **arr_ptr_ptr;
pval *array_element;
if (zend_hash_find(EG(active_symbol_table), var, var_len+1, (void **) &arr_ptr_ptr) == FAILURE) {
/* If the array doesn't exist, create it */
MAKE_STD_ZVAL(gpc_element);
array_init(gpc_element);
do_insert=1;
} else {
if ((*arr_ptr_ptr)->type!=IS_ARRAY) {
if (--(*arr_ptr_ptr)->refcount > 0) {
MAKE_STD_ZVAL(*arr_ptr_ptr);
} else {
zval_dtor(*arr_ptr_ptr);
}
array_init(*arr_ptr_ptr);
}
gpc_element = *arr_ptr_ptr;
do_insert=0;
}
/* Create the element */
array_element = (pval *) emalloc(sizeof(pval));
INIT_PZVAL(array_element);
array_element->value.str.val = val;
array_element->value.str.len = val_len;
array_element->type = IS_STRING;
/* Insert it */
if (array_index) {
/* indexed array */
if (php3_check_type(array_index) == IS_LONG) {
/* numeric index */
zend_hash_index_update(gpc_element->value.ht, atol(array_index), &array_element, sizeof(pval *), NULL); /* s[array_index]=tmp */
} else {
/* associative index */
zend_hash_update(gpc_element->value.ht, array_index, strlen(array_index)+1, &array_element, sizeof(pval *), NULL); /* s["ret"]=tmp */
}
efree(array_index);
} else {
/* non-indexed array */
zend_hash_next_index_insert(gpc_element->value.ht, &array_element, sizeof(pval *), NULL);
}
} else { /* we have a normal variable */
MAKE_STD_ZVAL(gpc_element);
gpc_element->type = IS_STRING;
gpc_element->refcount = 0;
gpc_element->value.str.val = val;
gpc_element->value.str.len = val_len;
do_insert=1;
}
if (do_insert) {
gpc_element->refcount = 0;
if (PG(gpc_globals)) {
zend_hash_update(EG(active_symbol_table), var, var_len+1, &gpc_element, sizeof(pval *), NULL);
gpc_element->refcount++;
}
if (track_vars_array) {
zend_hash_update(track_vars_array->value.ht, var, var_len+1, &gpc_element, sizeof(pval *), NULL);
gpc_element->refcount++;
}
}
}
void php_treat_data(int arg, char *str ELS_DC PLS_DC SLS_DC)
{
char *res = NULL, *var, *val;
pval *array_ptr;
int free_buffer=0;
switch (arg) {
case PARSE_POST:
case PARSE_GET:
case PARSE_COOKIE:
if (PG(track_vars)) {
array_ptr = (pval *) emalloc(sizeof(pval));
array_init(array_ptr);
INIT_PZVAL(array_ptr);
switch (arg) {
case PARSE_POST:
zend_hash_add(&EG(symbol_table), "HTTP_POST_VARS", sizeof("HTTP_POST_VARS"), &array_ptr, sizeof(pval *),NULL);
break;
case PARSE_GET:
zend_hash_add(&EG(symbol_table), "HTTP_GET_VARS", sizeof("HTTP_GET_VARS"), &array_ptr, sizeof(pval *),NULL);
break;
case PARSE_COOKIE:
zend_hash_add(&EG(symbol_table), "HTTP_COOKIE_VARS", sizeof("HTTP_COOKIE_VARS"), &array_ptr, sizeof(pval *),NULL);
break;
}
} else {
array_ptr=NULL;
}
break;
default:
array_ptr=NULL;
break;
}
if (arg == PARSE_POST) { /* POST data */
res = SG(request_info).post_data;
free_buffer = 0;
} else if (arg == PARSE_GET) { /* GET data */
var = SG(request_info).query_string;
if (var && *var) {
res = (char *) estrdup(var);
free_buffer = 1;
} else {
free_buffer = 0;
}
} else if (arg == PARSE_COOKIE) { /* Cookie data */
var = SG(request_info).cookie_data;
if (var && *var) {
res = (char *) estrdup(var);
free_buffer = 1;
} else {
free_buffer = 0;
}
} else if (arg == PARSE_STRING) { /* String data */
res = str;
free_buffer = 1;
}
if (!res) {
return;
}
if (arg == PARSE_COOKIE) {
var = strtok(res, ";");
} else if (arg == PARSE_POST) {
var = strtok(res, "&");
} else {
var = strtok(res, PG(arg_separator));
}
while (var) {
val = strchr(var, '=');
if (val) { /* have a value */
*val++ = '\0';
/* FIXME: XXX: not binary safe, discards returned length */
_php3_urldecode(var, strlen(var));
_php3_urldecode(val, strlen(val));
php_parse_gpc_data(val,var,array_ptr ELS_CC PLS_CC);
}
if (arg == PARSE_COOKIE) {
var = strtok(NULL, ";");
} else if (arg == PARSE_POST) {
var = strtok(NULL, "&");
} else {
var = strtok(NULL, PG(arg_separator));
}
}
if (free_buffer) {
efree(res);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|