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.

885 lines
26 KiB

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