Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 318 lines (291 sloc) 8.419 kb
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
1 /*
2 +----------------------------------------------------------------------+
c5724cb @zsuraski License update
zsuraski authored
3 | PHP version 4.0 |
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
4 +----------------------------------------------------------------------+
43ae2bf Happy Y2K patch! Happy new year (or the new millennium, depending on whe...
Sascha Schumann authored
5 | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
6 +----------------------------------------------------------------------+
c5724cb @zsuraski License update
zsuraski authored
7 | This source file is subject to version 2.0 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available at through the world-wide-web at |
10 | http://www.php.net/license/2_0.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. |
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
14 +----------------------------------------------------------------------+
15 | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
15fee4d @zsuraski - Seriously optimize and clean php_parse_gpc_data()
zsuraski authored
16 | Zeev Suraski <zeev@zend.com> |
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
17 +----------------------------------------------------------------------+
18 */
15d47e5 @zsuraski Test commit, ignore
zsuraski authored
19 /* $Id: */
6f7c0ed @zsuraski Remove tls.[ch]
zsuraski authored
20
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
21 #include <stdio.h>
22 #include "php.h"
fc67810 @zsuraski post.c really had nothing to do with POST anymore, and it belongs to the...
zsuraski authored
23 #include "ext/standard/php_standard.h"
9ab35ae @zsuraski Tried to centralize global variable registration as much as possible:
zsuraski authored
24 #include "php_variables.h"
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
25 #include "php_globals.h"
3cd0af1 @zsuraski * Get the Apache module to compile again
zsuraski authored
26 #include "SAPI.h"
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
27
28 #include "zend_globals.h"
29
30
9ab35ae @zsuraski Tried to centralize global variable registration as much as possible:
zsuraski authored
31 PHPAPI void php_register_variable(char *val, char *var, pval *track_vars_array ELS_DC PLS_DC)
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
32 {
b2c0acb @zsuraski - Rewrote the GET/POST/Cookie data reader to support multi-dimensional
zsuraski authored
33 char *p = NULL;
34 char *ip; /* index pointer */
35 char *index;
36 int var_len, val_len, index_len;
37 zval *gpc_element, **gpc_element_p, **top_gpc_p=NULL;
38 zend_bool is_array;
39 zend_bool free_index;
40 HashTable *symtable1=NULL;
41 HashTable *symtable2=NULL;
42
43 if (PG(gpc_globals)) {
44 symtable1 = EG(active_symbol_table);
45 }
46 if (track_vars_array) {
47 if (symtable1) {
48 symtable2 = track_vars_array->value.ht;
49 } else {
50 symtable1 = track_vars_array->value.ht;
51 }
52 }
53 if (!symtable1) {
54 /* we don't need track_vars, and we're not setting GPC globals either. */
55 return;
56 }
57
58 /*
59 * Prepare variable name
60 */
61 ip = strchr(var, '[');
62 if (ip) {
63 is_array = 1;
64 *ip = 0;
65 } else {
66 is_array = 0;
67 }
68 /* ignore leading spaces in the variable name */
69 while (*var && *var==' ') {
70 var++;
71 }
72 var_len = strlen(var);
73 if (var_len==0) { /* empty variable name, or variable name with a space in it */
74 return;
75 }
76 /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
77 for (p=var; *p; p++) {
78 switch(*p) {
79 case ' ':
80 case '.':
81 *p='_';
82 break;
83 }
84 }
85
86 /* Prepare value */
87 val_len = strlen(val);
88 if (PG(magic_quotes_gpc)) {
89 val = php_addslashes(val, val_len, &val_len, 0);
90 } else {
91 val = estrndup(val, val_len);
92 }
93
94 index = var;
95 index_len = var_len;
96 free_index = 0;
97
98 while (1) {
99 if (is_array) {
100 char *escaped_index;
101
102 if (!index) {
103 MAKE_STD_ZVAL(gpc_element);
104 array_init(gpc_element);
105 zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
106 } else {
107 if (PG(magic_quotes_gpc) && (index!=var)) {
108 /* no need to addslashes() the index if it's the main variable name */
109 escaped_index = php_addslashes(index, index_len, &index_len, 0);
110 } else {
111 escaped_index = index;
112 }
113 if (zend_hash_find(symtable1, escaped_index, index_len+1, (void **) &gpc_element_p)==FAILURE
114 || (*gpc_element_p)->type != IS_ARRAY) {
115 MAKE_STD_ZVAL(gpc_element);
116 array_init(gpc_element);
117 zend_hash_update(symtable1, escaped_index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
118 }
119 if (index!=escaped_index) {
120 efree(escaped_index);
121 }
122 }
123 if (!top_gpc_p) {
124 top_gpc_p = gpc_element_p;
125 }
126 symtable1 = (*gpc_element_p)->value.ht;
127 /* ip pointed to the '[' character, now obtain the key */
128 index = ++ip;
129 index_len = 0;
130 if (*ip=='\n' || *ip=='\r' || *ip=='\t' || *ip==' ') {
131 ip++;
132 }
133 if (*ip==']') {
134 index = NULL;
135 } else {
136 ip = strchr(ip, ']');
137 if (!ip) {
138 php_error(E_WARNING, "Missing ] in %s variable", var);
139 return;
140 }
141 *ip = 0;
142 index_len = strlen(index);
143 }
144 ip++;
145 if (*ip=='[') {
146 is_array = 1;
147 *ip = 0;
148 } else {
149 is_array = 0;
150 }
151 } else {
152 MAKE_STD_ZVAL(gpc_element);
153 gpc_element->value.str.val = val;
154 gpc_element->value.str.len = val_len;
155 gpc_element->type = IS_STRING;
156 if (!index) {
157 zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
158 } else {
159 zend_hash_update(symtable1, index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
160 }
161 if (!top_gpc_p) {
162 top_gpc_p = gpc_element_p;
163 }
164 break;
165 }
166 }
167
d6fd485 @zsuraski @- Make the global GET/POST/Cookie variables and their $HTTP_*_VARS[] co...
zsuraski authored
168 if (top_gpc_p) {
169 (*top_gpc_p)->is_ref = 1;
170 if (symtable2) {
171 zend_hash_update(symtable2, var, var_len+1, top_gpc_p, sizeof(zval *), NULL);
172 (*top_gpc_p)->refcount++;
173 }
174 }
b2c0acb @zsuraski - Rewrote the GET/POST/Cookie data reader to support multi-dimensional
zsuraski authored
175 }
176
177
1b6ccfe @zsuraski Cleanups & optimizations
zsuraski authored
178 void php_treat_data(int arg, char *str ELS_DC PLS_DC SLS_DC)
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
179 {
180 char *res = NULL, *var, *val;
181 pval *array_ptr;
0b77132 @zsuraski * Get Apache to work. POST doesn't work yet.
zsuraski authored
182 int free_buffer=0;
3881904 Replace strtok with strtok_r
Sascha Schumann authored
183 char *strtok_buf = NULL;
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
184
185 switch (arg) {
186 case PARSE_POST:
187 case PARSE_GET:
188 case PARSE_COOKIE:
189 if (PG(track_vars)) {
235386b @zsuraski Change ALLOC_ZVAL() semantics
zsuraski authored
190 ALLOC_ZVAL(array_ptr);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
191 array_init(array_ptr);
c43c235 @zsuraski Don't touch refcount and EA directly
zsuraski authored
192 INIT_PZVAL(array_ptr);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
193 switch (arg) {
194 case PARSE_POST:
3cb1eb0 @zsuraski Removed '3' from key functions in PHP (maintained compatibility through
zsuraski authored
195 zend_hash_add(&EG(symbol_table), "HTTP_POST_VARS", sizeof("HTTP_POST_VARS"), &array_ptr, sizeof(pval *),NULL);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
196 break;
197 case PARSE_GET:
3cb1eb0 @zsuraski Removed '3' from key functions in PHP (maintained compatibility through
zsuraski authored
198 zend_hash_add(&EG(symbol_table), "HTTP_GET_VARS", sizeof("HTTP_GET_VARS"), &array_ptr, sizeof(pval *),NULL);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
199 break;
200 case PARSE_COOKIE:
3cb1eb0 @zsuraski Removed '3' from key functions in PHP (maintained compatibility through
zsuraski authored
201 zend_hash_add(&EG(symbol_table), "HTTP_COOKIE_VARS", sizeof("HTTP_COOKIE_VARS"), &array_ptr, sizeof(pval *),NULL);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
202 break;
203 }
204 } else {
205 array_ptr=NULL;
206 }
207 break;
208 default:
209 array_ptr=NULL;
210 break;
211 }
212
4079f91 @andigutmans * Make read_post() read input by chunks instead of returning a single st...
andigutmans authored
213 if (arg == PARSE_POST) { /* POST data */
fb910c6 @zsuraski Cleanups
zsuraski authored
214 res = SG(request_info).post_data;
4079f91 @andigutmans * Make read_post() read input by chunks instead of returning a single st...
andigutmans authored
215 free_buffer = 0;
216 } else if (arg == PARSE_GET) { /* GET data */
5dbe924 @andigutmans - Make ISAPI hook in as an extension too.
andigutmans authored
217 var = SG(request_info).query_string;
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
218 if (var && *var) {
219 res = (char *) estrdup(var);
4079f91 @andigutmans * Make read_post() read input by chunks instead of returning a single st...
andigutmans authored
220 free_buffer = 1;
221 } else {
222 free_buffer = 0;
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
223 }
224 } else if (arg == PARSE_COOKIE) { /* Cookie data */
bc415d5 @zsuraski * Finalizing the PHP version of SAPI. Support POST and cookies among ot...
zsuraski authored
225 var = SG(request_info).cookie_data;
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
226 if (var && *var) {
227 res = (char *) estrdup(var);
4079f91 @andigutmans * Make read_post() read input by chunks instead of returning a single st...
andigutmans authored
228 free_buffer = 1;
229 } else {
230 free_buffer = 0;
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
231 }
232 } else if (arg == PARSE_STRING) { /* String data */
233 res = str;
4079f91 @andigutmans * Make read_post() read input by chunks instead of returning a single st...
andigutmans authored
234 free_buffer = 1;
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
235 }
236 if (!res) {
237 return;
238 }
239
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
240 #if HAVE_FDFLIB
241 if((NULL != SG(request_info).content_type) && (0 == strcmp(SG(request_info).content_type, "application/vnd.fdf"))) {
242 pval *tmp;
243
235386b @zsuraski Change ALLOC_ZVAL() semantics
zsuraski authored
244 ALLOC_ZVAL(tmp);
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
245 tmp->value.str.len = SG(request_info).post_data_length;
246 tmp->value.str.val = estrndup(SG(request_info).post_data, SG(request_info).post_data_length);
247 tmp->type = IS_STRING;
248 INIT_PZVAL(tmp);
249 zend_hash_add(&EG(symbol_table), "HTTP_FDF_DATA", sizeof("HTTP_FDF_DATA"), &tmp, sizeof(pval *),NULL);
250
251 } else {
252 #endif
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
253 if (arg == PARSE_COOKIE) {
3881904 Replace strtok with strtok_r
Sascha Schumann authored
254 var = strtok_r(res, ";", &strtok_buf);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
255 } else if (arg == PARSE_POST) {
3881904 Replace strtok with strtok_r
Sascha Schumann authored
256 var = strtok_r(res, "&", &strtok_buf);
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
257 } else {
3881904 Replace strtok with strtok_r
Sascha Schumann authored
258 var = strtok_r(res, PG(arg_separator), &strtok_buf);
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
259 }
260
261 while (var) {
262 val = strchr(var, '=');
263 if (val) { /* have a value */
264 *val++ = '\0';
265 /* FIXME: XXX: not binary safe, discards returned length */
3ee4e65 @zsuraski More php3_ annihilation
zsuraski authored
266 php_url_decode(var, strlen(var));
267 php_url_decode(val, strlen(val));
9ab35ae @zsuraski Tried to centralize global variable registration as much as possible:
zsuraski authored
268 php_register_variable(val, var, array_ptr ELS_CC PLS_CC);
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
269 }
270 if (arg == PARSE_COOKIE) {
3881904 Replace strtok with strtok_r
Sascha Schumann authored
271 var = strtok_r(NULL, ";", &strtok_buf);
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
272 } else if (arg == PARSE_POST) {
3881904 Replace strtok with strtok_r
Sascha Schumann authored
273 var = strtok_r(NULL, "&", &strtok_buf);
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
274 } else {
3881904 Replace strtok with strtok_r
Sascha Schumann authored
275 var = strtok_r(NULL, PG(arg_separator), &strtok_buf);
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
276 }
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
277 }
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
278 #if HAVE_FDFLIB
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
279 }
8c473e1 - support for mimetype application/vnd.fdf needed by fdf module
Uwe Steinmann authored
280 #endif
4079f91 @andigutmans * Make read_post() read input by chunks instead of returning a single st...
andigutmans authored
281 if (free_buffer) {
282 efree(res);
283 }
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
284 }
285
286
9ab35ae @zsuraski Tried to centralize global variable registration as much as possible:
zsuraski authored
287
288 void php_import_environment_variables(ELS_D PLS_DC)
289 {
290 char **env, *p, *t;
291 zval *array_ptr=NULL;
292
293 if (PG(track_vars)) {
294 ALLOC_ZVAL(array_ptr);
295 array_init(array_ptr);
296 INIT_PZVAL(array_ptr);
297 zend_hash_add(&EG(symbol_table), "HTTP_ENV_VARS", sizeof("HTTP_ENV_VARS"), &array_ptr, sizeof(pval *),NULL);
298 }
299
300 for (env = environ; env != NULL && *env != NULL; env++) {
301 p = strchr(*env, '=');
302 if (!p) { /* malformed entry? */
303 continue;
304 }
305 t = estrndup(*env, p - *env);
306 php_register_variable(p+1, t, array_ptr ELS_CC PLS_CC);
307 efree(t);
308 }
309 }
310
311
2c0ad3e last bunch of extensions moving to ext/
Stig Bakken authored
312 /*
313 * Local variables:
314 * tab-width: 4
315 * c-basic-offset: 4
316 * End:
317 */
Something went wrong with that request. Please try again.