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.

852 lines
24 KiB

15 years ago
27 years ago
27 years ago
27 years ago
20 years ago
21 years ago
18 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
19 years ago
19 years ago
21 years ago
21 years ago
21 years ago
21 years ago
20 years ago
20 years ago
20 years ago
21 years ago
20 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
22 years ago
24 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
15 years ago
15 years ago
15 years ago
21 years ago
21 years ago
21 years ago
15 years ago
15 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.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 "ext/standard/credits.h"
  24. #include "php_variables.h"
  25. #include "php_globals.h"
  26. #include "php_content_types.h"
  27. #include "SAPI.h"
  28. #include "php_logos.h"
  29. #include "zend_globals.h"
  30. /* for systems that need to override reading of environment variables */
  31. void _php_import_environment_variables(zval *array_ptr TSRMLS_DC);
  32. PHPAPI void (*php_import_environment_variables)(zval *array_ptr TSRMLS_DC) = _php_import_environment_variables;
  33. PHPAPI void php_register_variable(char *var, char *strval, zval *track_vars_array TSRMLS_DC)
  34. {
  35. php_register_variable_safe(var, strval, strlen(strval), track_vars_array TSRMLS_CC);
  36. }
  37. /* binary-safe version */
  38. PHPAPI void php_register_variable_safe(char *var, char *strval, int str_len, zval *track_vars_array TSRMLS_DC)
  39. {
  40. zval new_entry;
  41. assert(strval != NULL);
  42. /* Prepare value */
  43. Z_STRLEN(new_entry) = str_len;
  44. Z_STRVAL(new_entry) = estrndup(strval, Z_STRLEN(new_entry));
  45. Z_TYPE(new_entry) = IS_STRING;
  46. php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC);
  47. }
  48. PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars_array TSRMLS_DC)
  49. {
  50. char *p = NULL;
  51. char *ip; /* index pointer */
  52. char *index, *escaped_index = NULL;
  53. char *var, *var_orig;
  54. int var_len, index_len;
  55. zval *gpc_element, **gpc_element_p;
  56. zend_bool is_array = 0;
  57. HashTable *symtable1 = NULL;
  58. ALLOCA_FLAG(use_heap)
  59. assert(var_name != NULL);
  60. if (track_vars_array) {
  61. symtable1 = Z_ARRVAL_P(track_vars_array);
  62. }
  63. if (!symtable1) {
  64. /* Nothing to do */
  65. zval_dtor(val);
  66. return;
  67. }
  68. /* ignore leading spaces in the variable name */
  69. while (*var_name && *var_name==' ') {
  70. var_name++;
  71. }
  72. /*
  73. * Prepare variable name
  74. */
  75. var_len = strlen(var_name);
  76. var = var_orig = do_alloca(var_len + 1, use_heap);
  77. memcpy(var_orig, var_name, var_len + 1);
  78. /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
  79. for (p = var; *p; p++) {
  80. if (*p == ' ' || *p == '.') {
  81. *p='_';
  82. } else if (*p == '[') {
  83. is_array = 1;
  84. ip = p;
  85. *p = 0;
  86. break;
  87. }
  88. }
  89. var_len = p - var;
  90. if (var_len==0) { /* empty variable name, or variable name with a space in it */
  91. zval_dtor(val);
  92. free_alloca(var_orig, use_heap);
  93. return;
  94. }
  95. /* GLOBALS hijack attempt, reject parameter */
  96. if (symtable1 == EG(active_symbol_table) &&
  97. var_len == sizeof("GLOBALS")-1 &&
  98. !memcmp(var, "GLOBALS", sizeof("GLOBALS")-1)) {
  99. zval_dtor(val);
  100. free_alloca(var_orig, use_heap);
  101. return;
  102. }
  103. index = var;
  104. index_len = var_len;
  105. if (is_array) {
  106. int nest_level = 0;
  107. while (1) {
  108. char *index_s;
  109. int new_idx_len = 0;
  110. if(++nest_level > PG(max_input_nesting_level)) {
  111. HashTable *ht;
  112. /* too many levels of nesting */
  113. if (track_vars_array) {
  114. ht = Z_ARRVAL_P(track_vars_array);
  115. zend_hash_del(ht, var, var_len + 1);
  116. }
  117. zval_dtor(val);
  118. /* do not output the error message to the screen,
  119. this helps us to to avoid "information disclosure" */
  120. if (!PG(display_errors)) {
  121. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variable nesting level exceeded %ld. To increase the limit change max_input_nesting_level in php.ini.", PG(max_input_nesting_level));
  122. }
  123. free_alloca(var_orig, use_heap);
  124. return;
  125. }
  126. ip++;
  127. index_s = ip;
  128. if (isspace(*ip)) {
  129. ip++;
  130. }
  131. if (*ip==']') {
  132. index_s = NULL;
  133. } else {
  134. ip = strchr(ip, ']');
  135. if (!ip) {
  136. /* PHP variables cannot contain '[' in their names, so we replace the character with a '_' */
  137. *(index_s - 1) = '_';
  138. index_len = 0;
  139. if (index) {
  140. index_len = strlen(index);
  141. }
  142. goto plain_var;
  143. return;
  144. }
  145. *ip = 0;
  146. new_idx_len = strlen(index_s);
  147. }
  148. if (!index) {
  149. MAKE_STD_ZVAL(gpc_element);
  150. array_init(gpc_element);
  151. zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  152. } else {
  153. escaped_index = index;
  154. if (zend_symtable_find(symtable1, escaped_index, index_len + 1, (void **) &gpc_element_p) == FAILURE
  155. || Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
  156. MAKE_STD_ZVAL(gpc_element);
  157. array_init(gpc_element);
  158. zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  159. }
  160. if (index != escaped_index) {
  161. efree(escaped_index);
  162. }
  163. }
  164. symtable1 = Z_ARRVAL_PP(gpc_element_p);
  165. /* ip pointed to the '[' character, now obtain the key */
  166. index = index_s;
  167. index_len = new_idx_len;
  168. ip++;
  169. if (*ip == '[') {
  170. is_array = 1;
  171. *ip = 0;
  172. } else {
  173. goto plain_var;
  174. }
  175. }
  176. } else {
  177. plain_var:
  178. MAKE_STD_ZVAL(gpc_element);
  179. gpc_element->value = val->value;
  180. Z_TYPE_P(gpc_element) = Z_TYPE_P(val);
  181. if (!index) {
  182. zend_hash_next_index_insert(symtable1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  183. } else {
  184. escaped_index = index;
  185. /*
  186. * According to rfc2965, more specific paths are listed above the less specific ones.
  187. * If we encounter a duplicate cookie name, we should skip it, since it is not possible
  188. * to have the same (plain text) cookie name for the same path and we should not overwrite
  189. * more specific cookies with the less specific ones.
  190. */
  191. if (PG(http_globals)[TRACK_VARS_COOKIE] &&
  192. symtable1 == Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]) &&
  193. zend_symtable_exists(symtable1, escaped_index, index_len + 1)) {
  194. zval_ptr_dtor(&gpc_element);
  195. } else {
  196. zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
  197. }
  198. if (escaped_index != index) {
  199. efree(escaped_index);
  200. }
  201. }
  202. }
  203. free_alloca(var_orig, use_heap);
  204. }
  205. SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
  206. {
  207. char *var, *val, *e, *s, *p;
  208. zval *array_ptr = (zval *) arg;
  209. if (SG(request_info).post_data == NULL) {
  210. return;
  211. }
  212. s = SG(request_info).post_data;
  213. e = s + SG(request_info).post_data_length;
  214. while (s < e && (p = memchr(s, '&', (e - s)))) {
  215. last_value:
  216. if ((val = memchr(s, '=', (p - s)))) { /* have a value */
  217. unsigned int val_len, new_val_len;
  218. var = s;
  219. php_url_decode(var, (val - s));
  220. val++;
  221. val_len = php_url_decode(val, (p - val));
  222. val = estrndup(val, val_len);
  223. if (sapi_module.input_filter(PARSE_POST, var, &val, val_len, &new_val_len TSRMLS_CC)) {
  224. php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
  225. }
  226. efree(val);
  227. }
  228. s = p + 1;
  229. }
  230. if (s < e) {
  231. p = e;
  232. goto last_value;
  233. }
  234. }
  235. SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter)
  236. {
  237. /* TODO: check .ini setting here and apply user-defined input filter */
  238. if(new_val_len) *new_val_len = val_len;
  239. return 1;
  240. }
  241. SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
  242. {
  243. char *res = NULL, *var, *val, *separator = NULL;
  244. const char *c_var;
  245. zval *array_ptr;
  246. int free_buffer = 0;
  247. char *strtok_buf = NULL;
  248. switch (arg) {
  249. case PARSE_POST:
  250. case PARSE_GET:
  251. case PARSE_COOKIE:
  252. ALLOC_ZVAL(array_ptr);
  253. array_init(array_ptr);
  254. INIT_PZVAL(array_ptr);
  255. switch (arg) {
  256. case PARSE_POST:
  257. if (PG(http_globals)[TRACK_VARS_POST]) {
  258. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_POST]);
  259. }
  260. PG(http_globals)[TRACK_VARS_POST] = array_ptr;
  261. break;
  262. case PARSE_GET:
  263. if (PG(http_globals)[TRACK_VARS_GET]) {
  264. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_GET]);
  265. }
  266. PG(http_globals)[TRACK_VARS_GET] = array_ptr;
  267. break;
  268. case PARSE_COOKIE:
  269. if (PG(http_globals)[TRACK_VARS_COOKIE]) {
  270. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_COOKIE]);
  271. }
  272. PG(http_globals)[TRACK_VARS_COOKIE] = array_ptr;
  273. break;
  274. }
  275. break;
  276. default:
  277. array_ptr = destArray;
  278. break;
  279. }
  280. if (arg == PARSE_POST) {
  281. sapi_handle_post(array_ptr TSRMLS_CC);
  282. return;
  283. }
  284. if (arg == PARSE_GET) { /* GET data */
  285. c_var = SG(request_info).query_string;
  286. if (c_var && *c_var) {
  287. res = (char *) estrdup(c_var);
  288. free_buffer = 1;
  289. } else {
  290. free_buffer = 0;
  291. }
  292. } else if (arg == PARSE_COOKIE) { /* Cookie data */
  293. c_var = SG(request_info).cookie_data;
  294. if (c_var && *c_var) {
  295. res = (char *) estrdup(c_var);
  296. free_buffer = 1;
  297. } else {
  298. free_buffer = 0;
  299. }
  300. } else if (arg == PARSE_STRING) { /* String data */
  301. res = str;
  302. free_buffer = 1;
  303. }
  304. if (!res) {
  305. return;
  306. }
  307. switch (arg) {
  308. case PARSE_GET:
  309. case PARSE_STRING:
  310. separator = (char *) estrdup(PG(arg_separator).input);
  311. break;
  312. case PARSE_COOKIE:
  313. separator = ";\0";
  314. break;
  315. }
  316. var = php_strtok_r(res, separator, &strtok_buf);
  317. while (var) {
  318. val = strchr(var, '=');
  319. if (arg == PARSE_COOKIE) {
  320. /* Remove leading spaces from cookie names, needed for multi-cookie header where ; can be followed by a space */
  321. while (isspace(*var)) {
  322. var++;
  323. }
  324. if (var == val || *var == '\0') {
  325. goto next_cookie;
  326. }
  327. }
  328. if (val) { /* have a value */
  329. int val_len;
  330. unsigned int new_val_len;
  331. *val++ = '\0';
  332. php_url_decode(var, strlen(var));
  333. val_len = php_url_decode(val, strlen(val));
  334. val = estrndup(val, val_len);
  335. if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len TSRMLS_CC)) {
  336. php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
  337. }
  338. efree(val);
  339. } else {
  340. int val_len;
  341. unsigned int new_val_len;
  342. php_url_decode(var, strlen(var));
  343. val_len = 0;
  344. val = estrndup("", val_len);
  345. if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len TSRMLS_CC)) {
  346. php_register_variable_safe(var, val, new_val_len, array_ptr TSRMLS_CC);
  347. }
  348. efree(val);
  349. }
  350. next_cookie:
  351. var = php_strtok_r(NULL, separator, &strtok_buf);
  352. }
  353. if (arg != PARSE_COOKIE) {
  354. efree(separator);
  355. }
  356. if (free_buffer) {
  357. efree(res);
  358. }
  359. }
  360. void _php_import_environment_variables(zval *array_ptr TSRMLS_DC)
  361. {
  362. char buf[128];
  363. char **env, *p, *t = buf;
  364. size_t alloc_size = sizeof(buf);
  365. unsigned long nlen; /* ptrdiff_t is not portable */
  366. for (env = environ; env != NULL && *env != NULL; env++) {
  367. p = strchr(*env, '=');
  368. if (!p) { /* malformed entry? */
  369. continue;
  370. }
  371. nlen = p - *env;
  372. if (nlen >= alloc_size) {
  373. alloc_size = nlen + 64;
  374. t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
  375. }
  376. memcpy(t, *env, nlen);
  377. t[nlen] = '\0';
  378. php_register_variable(t, p + 1, array_ptr TSRMLS_CC);
  379. }
  380. if (t != buf && t != NULL) {
  381. efree(t);
  382. }
  383. }
  384. zend_bool php_std_auto_global_callback(char *name, uint name_len TSRMLS_DC)
  385. {
  386. zend_printf("%s\n", name);
  387. return 0; /* don't rearm */
  388. }
  389. /* {{{ php_build_argv
  390. */
  391. static void php_build_argv(char *s, zval *track_vars_array TSRMLS_DC)
  392. {
  393. zval *arr, *argc, *tmp;
  394. int count = 0;
  395. char *ss, *space;
  396. if (!(SG(request_info).argc || track_vars_array)) {
  397. return;
  398. }
  399. ALLOC_INIT_ZVAL(arr);
  400. array_init(arr);
  401. /* Prepare argv */
  402. if (SG(request_info).argc) { /* are we in cli sapi? */
  403. int i;
  404. for (i = 0; i < SG(request_info).argc; i++) {
  405. ALLOC_ZVAL(tmp);
  406. Z_TYPE_P(tmp) = IS_STRING;
  407. Z_STRLEN_P(tmp) = strlen(SG(request_info).argv[i]);
  408. Z_STRVAL_P(tmp) = estrndup(SG(request_info).argv[i], Z_STRLEN_P(tmp));
  409. INIT_PZVAL(tmp);
  410. if (zend_hash_next_index_insert(Z_ARRVAL_P(arr), &tmp, sizeof(zval *), NULL) == FAILURE) {
  411. if (Z_TYPE_P(tmp) == IS_STRING) {
  412. efree(Z_STRVAL_P(tmp));
  413. }
  414. }
  415. }
  416. } else if (s && *s) {
  417. ss = s;
  418. while (ss) {
  419. space = strchr(ss, '+');
  420. if (space) {
  421. *space = '\0';
  422. }
  423. /* auto-type */
  424. ALLOC_ZVAL(tmp);
  425. Z_TYPE_P(tmp) = IS_STRING;
  426. Z_STRLEN_P(tmp) = strlen(ss);
  427. Z_STRVAL_P(tmp) = estrndup(ss, Z_STRLEN_P(tmp));
  428. INIT_PZVAL(tmp);
  429. count++;
  430. if (zend_hash_next_index_insert(Z_ARRVAL_P(arr), &tmp, sizeof(zval *), NULL) == FAILURE) {
  431. if (Z_TYPE_P(tmp) == IS_STRING) {
  432. efree(Z_STRVAL_P(tmp));
  433. }
  434. }
  435. if (space) {
  436. *space = '+';
  437. ss = space + 1;
  438. } else {
  439. ss = space;
  440. }
  441. }
  442. }
  443. /* prepare argc */
  444. ALLOC_INIT_ZVAL(argc);
  445. if (SG(request_info).argc) {
  446. Z_LVAL_P(argc) = SG(request_info).argc;
  447. } else {
  448. Z_LVAL_P(argc) = count;
  449. }
  450. Z_TYPE_P(argc) = IS_LONG;
  451. if (SG(request_info).argc) {
  452. Z_ADDREF_P(arr);
  453. Z_ADDREF_P(argc);
  454. zend_hash_update(&EG(symbol_table), "argv", sizeof("argv"), &arr, sizeof(zval *), NULL);
  455. zend_hash_add(&EG(symbol_table), "argc", sizeof("argc"), &argc, sizeof(zval *), NULL);
  456. }
  457. if (track_vars_array) {
  458. Z_ADDREF_P(arr);
  459. Z_ADDREF_P(argc);
  460. zend_hash_update(Z_ARRVAL_P(track_vars_array), "argv", sizeof("argv"), &arr, sizeof(zval *), NULL);
  461. zend_hash_update(Z_ARRVAL_P(track_vars_array), "argc", sizeof("argc"), &argc, sizeof(zval *), NULL);
  462. }
  463. zval_ptr_dtor(&arr);
  464. zval_ptr_dtor(&argc);
  465. }
  466. /* }}} */
  467. /* {{{ php_handle_special_queries
  468. */
  469. PHPAPI int php_handle_special_queries(TSRMLS_D)
  470. {
  471. if (PG(expose_php) && SG(request_info).query_string && SG(request_info).query_string[0] == '=') {
  472. if (php_info_logos(SG(request_info).query_string + 1 TSRMLS_CC)) {
  473. return 1;
  474. } else if (!strcmp(SG(request_info).query_string + 1, PHP_CREDITS_GUID)) {
  475. php_print_credits(PHP_CREDITS_ALL TSRMLS_CC);
  476. return 1;
  477. }
  478. }
  479. return 0;
  480. }
  481. /* }}} */
  482. /* {{{ php_register_server_variables
  483. */
  484. static inline void php_register_server_variables(TSRMLS_D)
  485. {
  486. zval *array_ptr = NULL;
  487. ALLOC_ZVAL(array_ptr);
  488. array_init(array_ptr);
  489. INIT_PZVAL(array_ptr);
  490. if (PG(http_globals)[TRACK_VARS_SERVER]) {
  491. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_SERVER]);
  492. }
  493. PG(http_globals)[TRACK_VARS_SERVER] = array_ptr;
  494. /* Server variables */
  495. if (sapi_module.register_server_variables) {
  496. sapi_module.register_server_variables(array_ptr TSRMLS_CC);
  497. }
  498. /* PHP Authentication support */
  499. if (SG(request_info).auth_user) {
  500. php_register_variable("PHP_AUTH_USER", SG(request_info).auth_user, array_ptr TSRMLS_CC);
  501. }
  502. if (SG(request_info).auth_password) {
  503. php_register_variable("PHP_AUTH_PW", SG(request_info).auth_password, array_ptr TSRMLS_CC);
  504. }
  505. if (SG(request_info).auth_digest) {
  506. php_register_variable("PHP_AUTH_DIGEST", SG(request_info).auth_digest, array_ptr TSRMLS_CC);
  507. }
  508. /* store request init time */
  509. {
  510. zval new_entry;
  511. Z_TYPE(new_entry) = IS_DOUBLE;
  512. Z_DVAL(new_entry) = sapi_get_request_time(TSRMLS_C);
  513. php_register_variable_ex("REQUEST_TIME", &new_entry, array_ptr TSRMLS_CC);
  514. }
  515. }
  516. /* }}} */
  517. /* {{{ php_autoglobal_merge
  518. */
  519. static void php_autoglobal_merge(HashTable *dest, HashTable *src TSRMLS_DC)
  520. {
  521. zval **src_entry, **dest_entry;
  522. char *string_key;
  523. uint string_key_len;
  524. ulong num_key;
  525. HashPosition pos;
  526. int key_type;
  527. int globals_check = (dest == (&EG(symbol_table)));
  528. zend_hash_internal_pointer_reset_ex(src, &pos);
  529. while (zend_hash_get_current_data_ex(src, (void **)&src_entry, &pos) == SUCCESS) {
  530. key_type = zend_hash_get_current_key_ex(src, &string_key, &string_key_len, &num_key, 0, &pos);
  531. if (Z_TYPE_PP(src_entry) != IS_ARRAY
  532. || (key_type == HASH_KEY_IS_STRING && zend_hash_find(dest, string_key, string_key_len, (void **) &dest_entry) != SUCCESS)
  533. || (key_type == HASH_KEY_IS_LONG && zend_hash_index_find(dest, num_key, (void **)&dest_entry) != SUCCESS)
  534. || Z_TYPE_PP(dest_entry) != IS_ARRAY
  535. ) {
  536. Z_ADDREF_PP(src_entry);
  537. if (key_type == HASH_KEY_IS_STRING) {
  538. if (!globals_check || string_key_len != sizeof("GLOBALS") || memcmp(string_key, "GLOBALS", sizeof("GLOBALS") - 1)) {
  539. zend_hash_update(dest, string_key, string_key_len, src_entry, sizeof(zval *), NULL);
  540. } else {
  541. Z_DELREF_PP(src_entry);
  542. }
  543. } else {
  544. zend_hash_index_update(dest, num_key, src_entry, sizeof(zval *), NULL);
  545. }
  546. } else {
  547. SEPARATE_ZVAL(dest_entry);
  548. php_autoglobal_merge(Z_ARRVAL_PP(dest_entry), Z_ARRVAL_PP(src_entry) TSRMLS_CC);
  549. }
  550. zend_hash_move_forward_ex(src, &pos);
  551. }
  552. }
  553. /* }}} */
  554. static zend_bool php_auto_globals_create_server(const char *name, uint name_len TSRMLS_DC);
  555. static zend_bool php_auto_globals_create_env(const char *name, uint name_len TSRMLS_DC);
  556. static zend_bool php_auto_globals_create_request(const char *name, uint name_len TSRMLS_DC);
  557. /* {{{ php_hash_environment
  558. */
  559. int php_hash_environment(TSRMLS_D)
  560. {
  561. memset(PG(http_globals), 0, sizeof(PG(http_globals)));
  562. zend_activate_auto_globals(TSRMLS_C);
  563. if (PG(register_argc_argv)) {
  564. php_build_argv(SG(request_info).query_string, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
  565. }
  566. return SUCCESS;
  567. }
  568. /* }}} */
  569. static zend_bool php_auto_globals_create_get(const char *name, uint name_len TSRMLS_DC)
  570. {
  571. zval *vars;
  572. if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) {
  573. sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
  574. vars = PG(http_globals)[TRACK_VARS_GET];
  575. } else {
  576. ALLOC_ZVAL(vars);
  577. array_init(vars);
  578. INIT_PZVAL(vars);
  579. if (PG(http_globals)[TRACK_VARS_GET]) {
  580. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_GET]);
  581. }
  582. PG(http_globals)[TRACK_VARS_GET] = vars;
  583. }
  584. zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
  585. Z_ADDREF_P(vars);
  586. return 0; /* don't rearm */
  587. }
  588. static zend_bool php_auto_globals_create_post(const char *name, uint name_len TSRMLS_DC)
  589. {
  590. zval *vars;
  591. if (PG(variables_order) &&
  592. (strchr(PG(variables_order),'P') || strchr(PG(variables_order),'p')) &&
  593. !SG(headers_sent) &&
  594. SG(request_info).request_method &&
  595. !strcasecmp(SG(request_info).request_method, "POST")) {
  596. sapi_module.treat_data(PARSE_POST, NULL, NULL TSRMLS_CC);
  597. vars = PG(http_globals)[TRACK_VARS_POST];
  598. } else {
  599. ALLOC_ZVAL(vars);
  600. array_init(vars);
  601. INIT_PZVAL(vars);
  602. if (PG(http_globals)[TRACK_VARS_POST]) {
  603. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_POST]);
  604. }
  605. PG(http_globals)[TRACK_VARS_POST] = vars;
  606. }
  607. zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
  608. Z_ADDREF_P(vars);
  609. return 0; /* don't rearm */
  610. }
  611. static zend_bool php_auto_globals_create_cookie(const char *name, uint name_len TSRMLS_DC)
  612. {
  613. zval *vars;
  614. if (PG(variables_order) && (strchr(PG(variables_order),'C') || strchr(PG(variables_order),'c'))) {
  615. sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);
  616. vars = PG(http_globals)[TRACK_VARS_COOKIE];
  617. } else {
  618. ALLOC_ZVAL(vars);
  619. array_init(vars);
  620. INIT_PZVAL(vars);
  621. if (PG(http_globals)[TRACK_VARS_COOKIE]) {
  622. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_COOKIE]);
  623. }
  624. PG(http_globals)[TRACK_VARS_COOKIE] = vars;
  625. }
  626. zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
  627. Z_ADDREF_P(vars);
  628. return 0; /* don't rearm */
  629. }
  630. static zend_bool php_auto_globals_create_files(const char *name, uint name_len TSRMLS_DC)
  631. {
  632. zval *vars;
  633. if (PG(http_globals)[TRACK_VARS_FILES]) {
  634. vars = PG(http_globals)[TRACK_VARS_FILES];
  635. } else {
  636. ALLOC_ZVAL(vars);
  637. array_init(vars);
  638. INIT_PZVAL(vars);
  639. PG(http_globals)[TRACK_VARS_FILES] = vars;
  640. }
  641. zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
  642. Z_ADDREF_P(vars);
  643. return 0; /* don't rearm */
  644. }
  645. static zend_bool php_auto_globals_create_server(const char *name, uint name_len TSRMLS_DC)
  646. {
  647. if (PG(variables_order) && (strchr(PG(variables_order),'S') || strchr(PG(variables_order),'s'))) {
  648. php_register_server_variables(TSRMLS_C);
  649. if (PG(register_argc_argv)) {
  650. if (SG(request_info).argc) {
  651. zval **argc, **argv;
  652. if (zend_hash_find(&EG(symbol_table), "argc", sizeof("argc"), (void**)&argc) == SUCCESS &&
  653. zend_hash_find(&EG(symbol_table), "argv", sizeof("argv"), (void**)&argv) == SUCCESS) {
  654. Z_ADDREF_PP(argc);
  655. Z_ADDREF_PP(argv);
  656. zend_hash_update(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "argv", sizeof("argv"), argv, sizeof(zval *), NULL);
  657. zend_hash_update(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "argc", sizeof("argc"), argc, sizeof(zval *), NULL);
  658. }
  659. } else {
  660. php_build_argv(SG(request_info).query_string, PG(http_globals)[TRACK_VARS_SERVER] TSRMLS_CC);
  661. }
  662. }
  663. } else {
  664. zval *server_vars=NULL;
  665. ALLOC_ZVAL(server_vars);
  666. array_init(server_vars);
  667. INIT_PZVAL(server_vars);
  668. if (PG(http_globals)[TRACK_VARS_SERVER]) {
  669. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_SERVER]);
  670. }
  671. PG(http_globals)[TRACK_VARS_SERVER] = server_vars;
  672. }
  673. zend_hash_update(&EG(symbol_table), name, name_len + 1, &PG(http_globals)[TRACK_VARS_SERVER], sizeof(zval *), NULL);
  674. Z_ADDREF_P(PG(http_globals)[TRACK_VARS_SERVER]);
  675. return 0; /* don't rearm */
  676. }
  677. static zend_bool php_auto_globals_create_env(const char *name, uint name_len TSRMLS_DC)
  678. {
  679. zval *env_vars = NULL;
  680. ALLOC_ZVAL(env_vars);
  681. array_init(env_vars);
  682. INIT_PZVAL(env_vars);
  683. if (PG(http_globals)[TRACK_VARS_ENV]) {
  684. zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_ENV]);
  685. }
  686. PG(http_globals)[TRACK_VARS_ENV] = env_vars;
  687. if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
  688. php_import_environment_variables(PG(http_globals)[TRACK_VARS_ENV] TSRMLS_CC);
  689. }
  690. zend_hash_update(&EG(symbol_table), name, name_len + 1, &PG(http_globals)[TRACK_VARS_ENV], sizeof(zval *), NULL);
  691. Z_ADDREF_P(PG(http_globals)[TRACK_VARS_ENV]);
  692. return 0; /* don't rearm */
  693. }
  694. static zend_bool php_auto_globals_create_request(const char *name, uint name_len TSRMLS_DC)
  695. {
  696. zval *form_variables;
  697. unsigned char _gpc_flags[3] = {0, 0, 0};
  698. char *p;
  699. ALLOC_ZVAL(form_variables);
  700. array_init(form_variables);
  701. INIT_PZVAL(form_variables);
  702. if (PG(request_order) != NULL) {
  703. p = PG(request_order);
  704. } else {
  705. p = PG(variables_order);
  706. }
  707. for (; p && *p; p++) {
  708. switch (*p) {
  709. case 'g':
  710. case 'G':
  711. if (!_gpc_flags[0]) {
  712. php_autoglobal_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]) TSRMLS_CC);
  713. _gpc_flags[0] = 1;
  714. }
  715. break;
  716. case 'p':
  717. case 'P':
  718. if (!_gpc_flags[1]) {
  719. php_autoglobal_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_POST]) TSRMLS_CC);
  720. _gpc_flags[1] = 1;
  721. }
  722. break;
  723. case 'c':
  724. case 'C':
  725. if (!_gpc_flags[2]) {
  726. php_autoglobal_merge(Z_ARRVAL_P(form_variables), Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]) TSRMLS_CC);
  727. _gpc_flags[2] = 1;
  728. }
  729. break;
  730. }
  731. }
  732. zend_hash_update(&EG(symbol_table), name, name_len + 1, &form_variables, sizeof(zval *), NULL);
  733. return 0;
  734. }
  735. void php_startup_auto_globals(TSRMLS_D)
  736. {
  737. zend_register_auto_global(ZEND_STRL("_GET"), 0, php_auto_globals_create_get TSRMLS_CC);
  738. zend_register_auto_global(ZEND_STRL("_POST"), 0, php_auto_globals_create_post TSRMLS_CC);
  739. zend_register_auto_global(ZEND_STRL("_COOKIE"), 0, php_auto_globals_create_cookie TSRMLS_CC);
  740. zend_register_auto_global(ZEND_STRL("_SERVER"), PG(auto_globals_jit), php_auto_globals_create_server TSRMLS_CC);
  741. zend_register_auto_global(ZEND_STRL("_ENV"), PG(auto_globals_jit), php_auto_globals_create_env TSRMLS_CC);
  742. zend_register_auto_global(ZEND_STRL("_REQUEST"), PG(auto_globals_jit), php_auto_globals_create_request TSRMLS_CC);
  743. zend_register_auto_global(ZEND_STRL("_FILES"), 0, php_auto_globals_create_files TSRMLS_CC);
  744. }
  745. /*
  746. * Local variables:
  747. * tab-width: 4
  748. * c-basic-offset: 4
  749. * End:
  750. * vim600: sw=4 ts=4 fdm=marker
  751. * vim<600: sw=4 ts=4
  752. */