You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
8.6 KiB

24 years ago
24 years ago
27 years ago
27 years ago
27 years ago
24 years ago
25 years ago
24 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 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_02.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. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include <stdio.h>
  21. #include "php.h"
  22. #include "ext/standard/php_standard.h"
  23. #include "php_variables.h"
  24. #include "php_globals.h"
  25. #include "php_content_types.h"
  26. #include "SAPI.h"
  27. #include "zend_globals.h"
  28. /* for systems that need to override reading of environment variables */
  29. void _php_import_environment_variables(zval *array_ptr TSRMLS_DC);
  30. PHPAPI void (*php_import_environment_variables)(zval *array_ptr TSRMLS_DC) = _php_import_environment_variables;
  31. PHPAPI void php_register_variable(char *var, char *strval, zval *track_vars_array TSRMLS_DC)
  32. {
  33. php_register_variable_safe(var, strval, strlen(strval), track_vars_array TSRMLS_CC);
  34. }
  35. /* binary-safe version */
  36. PHPAPI void php_register_variable_safe(char *var, char *strval, int str_len, zval *track_vars_array TSRMLS_DC)
  37. {
  38. zval new_entry;
  39. assert(strval != NULL);
  40. /* Prepare value */
  41. Z_STRLEN(new_entry) = str_len;
  42. if (PG(magic_quotes_gpc)) {
  43. Z_STRVAL(new_entry) = php_addslashes(strval, Z_STRLEN(new_entry), &Z_STRLEN(new_entry), 0 TSRMLS_CC);
  44. } else {
  45. Z_STRVAL(new_entry) = estrndup(strval, Z_STRLEN(new_entry));
  46. }
  47. Z_TYPE(new_entry) = IS_STRING;
  48. php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC);
  49. }
  50. PHPAPI void php_register_variable_ex(char *var, zval *val, pval *track_vars_array TSRMLS_DC)
  51. {
  52. char *p = NULL;
  53. char *ip; /* index pointer */
  54. char *index;
  55. int var_len, index_len;
  56. zval *gpc_element, **gpc_element_p, **top_gpc_p=NULL;
  57. zend_bool is_array;
  58. HashTable *symtable1=NULL;
  59. HashTable *symtable2=NULL;
  60. assert(var != NULL);
  61. if (PG(register_globals)) {
  62. symtable1 = EG(active_symbol_table);
  63. }
  64. if (track_vars_array) {
  65. if (symtable1) {
  66. symtable2 = Z_ARRVAL_P(track_vars_array);
  67. } else {
  68. symtable1 = Z_ARRVAL_P(track_vars_array);
  69. }
  70. }
  71. if (!symtable1) {
  72. /* Nothing to do */
  73. zval_dtor(val);
  74. return;
  75. }
  76. /*
  77. * Prepare variable name
  78. */
  79. ip = strchr(var, '[');
  80. if (ip) {
  81. is_array = 1;
  82. *ip = 0;
  83. } else {
  84. is_array = 0;
  85. }
  86. /* ignore leading spaces in the variable name */
  87. while (*var && *var==' ') {
  88. var++;
  89. }
  90. var_len = strlen(var);
  91. if (var_len==0) { /* empty variable name, or variable name with a space in it */
  92. zval_dtor(val);
  93. return;
  94. }
  95. /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
  96. for (p=var; *p; p++) {
  97. switch(*p) {
  98. case ' ':
  99. case '.':
  100. *p='_';
  101. break;
  102. }
  103. }
  104. index = var;
  105. index_len = var_len;
  106. while (1) {
  107. if (is_array) {
  108. char *escaped_index;
  109. if (!index) {
  110. MAKE_STD_ZVAL(gpc_element);
  111. array_init(gpc_element);
  112. zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  113. } else {
  114. if (PG(magic_quotes_gpc) && (index!=var)) {
  115. /* no need to addslashes() the index if it's the main variable name */
  116. escaped_index = php_addslashes(index, index_len, &index_len, 0 TSRMLS_CC);
  117. } else {
  118. escaped_index = index;
  119. }
  120. if (zend_hash_find(symtable1, escaped_index, index_len+1, (void **) &gpc_element_p)==FAILURE
  121. || Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
  122. MAKE_STD_ZVAL(gpc_element);
  123. array_init(gpc_element);
  124. zend_hash_update(symtable1, escaped_index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  125. }
  126. if (index!=escaped_index) {
  127. efree(escaped_index);
  128. }
  129. }
  130. if (!top_gpc_p) {
  131. top_gpc_p = gpc_element_p;
  132. }
  133. symtable1 = Z_ARRVAL_PP(gpc_element_p);
  134. /* ip pointed to the '[' character, now obtain the key */
  135. index = ++ip;
  136. index_len = 0;
  137. if (*ip=='\n' || *ip=='\r' || *ip=='\t' || *ip==' ') {
  138. ip++;
  139. }
  140. if (*ip==']') {
  141. index = NULL;
  142. } else {
  143. ip = strchr(ip, ']');
  144. if (!ip) {
  145. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing ] in %s variable", var);
  146. return;
  147. }
  148. *ip = 0;
  149. index_len = strlen(index);
  150. }
  151. ip++;
  152. if (*ip=='[') {
  153. is_array = 1;
  154. *ip = 0;
  155. } else {
  156. is_array = 0;
  157. }
  158. } else {
  159. MAKE_STD_ZVAL(gpc_element);
  160. gpc_element->value = val->value;
  161. Z_TYPE_P(gpc_element) = Z_TYPE_P(val);
  162. if (!index) {
  163. zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  164. } else {
  165. zend_hash_update(symtable1, index, index_len+1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  166. }
  167. if (!top_gpc_p) {
  168. top_gpc_p = gpc_element_p;
  169. }
  170. break;
  171. }
  172. }
  173. if (top_gpc_p) {
  174. if (symtable2) {
  175. zend_hash_update(symtable2, var, var_len+1, top_gpc_p, sizeof(zval *), NULL);
  176. (*top_gpc_p)->refcount++;
  177. }
  178. }
  179. }
  180. SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
  181. {
  182. char *var, *val;
  183. char *strtok_buf = NULL;
  184. zval *array_ptr = (zval *) arg;
  185. if (SG(request_info).post_data==NULL) {
  186. return;
  187. }
  188. var = php_strtok_r(SG(request_info).post_data, "&", &strtok_buf);
  189. while (var) {
  190. val = strchr(var, '=');
  191. if (val) { /* have a value */
  192. int val_len;
  193. *val++ = '\0';
  194. php_url_decode(var, strlen(var));
  195. val_len = php_url_decode(val, strlen(val));
  196. php_register_variable_safe(var, val, val_len, array_ptr TSRMLS_CC);
  197. }
  198. var = php_strtok_r(NULL, "&", &strtok_buf);
  199. }
  200. }
  201. SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
  202. {
  203. char *res = NULL, *var, *val, *separator=NULL;
  204. const char *c_var;
  205. pval *array_ptr;
  206. int free_buffer=0;
  207. char *strtok_buf = NULL;
  208. switch (arg) {
  209. case PARSE_POST:
  210. case PARSE_GET:
  211. case PARSE_COOKIE:
  212. ALLOC_ZVAL(array_ptr);
  213. array_init(array_ptr);
  214. INIT_PZVAL(array_ptr);
  215. switch (arg) {
  216. case PARSE_POST:
  217. PG(http_globals)[TRACK_VARS_POST] = array_ptr;
  218. break;
  219. case PARSE_GET:
  220. PG(http_globals)[TRACK_VARS_GET] = array_ptr;
  221. break;
  222. case PARSE_COOKIE:
  223. PG(http_globals)[TRACK_VARS_COOKIE] = array_ptr;
  224. break;
  225. }
  226. break;
  227. default:
  228. array_ptr=destArray;
  229. break;
  230. }
  231. if (arg==PARSE_POST) {
  232. sapi_handle_post(array_ptr TSRMLS_CC);
  233. return;
  234. }
  235. if (arg == PARSE_GET) { /* GET data */
  236. c_var = SG(request_info).query_string;
  237. if (c_var && *c_var) {
  238. res = (char *) estrdup(c_var);
  239. free_buffer = 1;
  240. } else {
  241. free_buffer = 0;
  242. }
  243. } else if (arg == PARSE_COOKIE) { /* Cookie data */
  244. c_var = SG(request_info).cookie_data;
  245. if (c_var && *c_var) {
  246. res = (char *) estrdup(c_var);
  247. free_buffer = 1;
  248. } else {
  249. free_buffer = 0;
  250. }
  251. } else if (arg == PARSE_STRING) { /* String data */
  252. res = str;
  253. free_buffer = 1;
  254. }
  255. if (!res) {
  256. return;
  257. }
  258. switch (arg) {
  259. case PARSE_GET:
  260. case PARSE_STRING:
  261. separator = (char *) estrdup(PG(arg_separator).input);
  262. break;
  263. case PARSE_COOKIE:
  264. separator = ";\0";
  265. break;
  266. }
  267. var = php_strtok_r(res, separator, &strtok_buf);
  268. while (var) {
  269. val = strchr(var, '=');
  270. if (val) { /* have a value */
  271. int val_len;
  272. *val++ = '\0';
  273. php_url_decode(var, strlen(var));
  274. val_len = php_url_decode(val, strlen(val));
  275. php_register_variable_safe(var, val, val_len, array_ptr TSRMLS_CC);
  276. } else {
  277. php_url_decode(var, strlen(var));
  278. php_register_variable_safe(var, "", 0, array_ptr TSRMLS_CC);
  279. }
  280. var = php_strtok_r(NULL, separator, &strtok_buf);
  281. }
  282. if(arg != PARSE_COOKIE) {
  283. efree(separator);
  284. }
  285. if (free_buffer) {
  286. efree(res);
  287. }
  288. }
  289. void _php_import_environment_variables(zval *array_ptr TSRMLS_DC)
  290. {
  291. char **env, *p, *t;
  292. for (env = environ; env != NULL && *env != NULL; env++) {
  293. p = strchr(*env, '=');
  294. if (!p) { /* malformed entry? */
  295. continue;
  296. }
  297. t = estrndup(*env, p - *env);
  298. php_register_variable(t, p+1, array_ptr TSRMLS_CC);
  299. efree(t);
  300. }
  301. }
  302. /*
  303. * Local variables:
  304. * tab-width: 4
  305. * c-basic-offset: 4
  306. * End:
  307. * vim600: sw=4 ts=4 fdm=marker
  308. * vim<600: sw=4 ts=4
  309. */