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.

829 lines
24 KiB

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