PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/main/php_variables.c

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