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.

803 lines
23 KiB

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