PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/main/php_variables.c

https://github.com/php/php-src
C | 913 lines | 731 code | 121 blank | 61 comment | 237 complexity | 8158908aafc59d22ca6a162ac3afb897 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  14. | Zeev Suraski <zeev@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include <stdio.h>
  18. #include "php.h"
  19. #include "ext/standard/php_standard.h"
  20. #include "ext/standard/credits.h"
  21. #include "zend_smart_str.h"
  22. #include "php_variables.h"
  23. #include "php_globals.h"
  24. #include "php_content_types.h"
  25. #include "SAPI.h"
  26. #include "zend_globals.h"
  27. /* for systems that need to override reading of environment variables */
  28. void _php_import_environment_variables(zval *array_ptr);
  29. PHPAPI void (*php_import_environment_variables)(zval *array_ptr) = _php_import_environment_variables;
  30. PHPAPI void php_register_variable(const char *var, const char *strval, zval *track_vars_array)
  31. {
  32. php_register_variable_safe(var, strval, strlen(strval), track_vars_array);
  33. }
  34. /* binary-safe version */
  35. PHPAPI void php_register_variable_safe(const char *var, const char *strval, size_t str_len, zval *track_vars_array)
  36. {
  37. zval new_entry;
  38. assert(strval != NULL);
  39. ZVAL_STRINGL_FAST(&new_entry, strval, str_len);
  40. php_register_variable_ex(var, &new_entry, track_vars_array);
  41. }
  42. static zend_always_inline void php_register_variable_quick(const char *name, size_t name_len, zval *val, HashTable *ht)
  43. {
  44. zend_string *key = zend_string_init_interned(name, name_len, 0);
  45. zend_hash_update_ind(ht, key, val);
  46. zend_string_release_ex(key, 0);
  47. }
  48. PHPAPI void php_register_variable_ex(const char *var_name, zval *val, zval *track_vars_array)
  49. {
  50. char *p = NULL;
  51. char *ip = NULL; /* index pointer */
  52. char *index;
  53. char *var, *var_orig;
  54. size_t var_len, index_len;
  55. zval gpc_element, *gpc_element_p;
  56. bool is_array = 0;
  57. HashTable *symtable1 = NULL;
  58. ALLOCA_FLAG(use_heap)
  59. assert(var_name != NULL);
  60. if (track_vars_array && Z_TYPE_P(track_vars_array) == IS_ARRAY) {
  61. symtable1 = Z_ARRVAL_P(track_vars_array);
  62. }
  63. if (!symtable1) {
  64. /* Nothing to do */
  65. zval_ptr_dtor_nogc(val);
  66. return;
  67. }
  68. /* ignore leading spaces in the variable name */
  69. while (*var_name==' ') {
  70. var_name++;
  71. }
  72. /*
  73. * Prepare variable name
  74. */
  75. var_len = strlen(var_name);
  76. var = var_orig = do_alloca(var_len + 1, use_heap);
  77. memcpy(var_orig, var_name, var_len + 1);
  78. /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
  79. for (p = var; *p; p++) {
  80. if (*p == ' ' || *p == '.') {
  81. *p='_';
  82. } else if (*p == '[') {
  83. is_array = 1;
  84. ip = p;
  85. *p = 0;
  86. break;
  87. }
  88. }
  89. var_len = p - var;
  90. if (var_len==0) { /* empty variable name, or variable name with a space in it */
  91. zval_ptr_dtor_nogc(val);
  92. free_alloca(var_orig, use_heap);
  93. return;
  94. }
  95. if (var_len == sizeof("this")-1 && EG(current_execute_data)) {
  96. zend_execute_data *ex = EG(current_execute_data);
  97. while (ex) {
  98. if (ex->func && ZEND_USER_CODE(ex->func->common.type)) {
  99. if ((ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE)
  100. && ex->symbol_table == symtable1) {
  101. if (memcmp(var, "this", sizeof("this")-1) == 0) {
  102. zend_throw_error(NULL, "Cannot re-assign $this");
  103. zval_ptr_dtor_nogc(val);
  104. free_alloca(var_orig, use_heap);
  105. return;
  106. }
  107. }
  108. break;
  109. }
  110. ex = ex->prev_execute_data;
  111. }
  112. }
  113. /* GLOBALS hijack attempt, reject parameter */
  114. if (symtable1 == &EG(symbol_table) &&
  115. var_len == sizeof("GLOBALS")-1 &&
  116. !memcmp(var, "GLOBALS", sizeof("GLOBALS")-1)) {
  117. zval_ptr_dtor_nogc(val);
  118. free_alloca(var_orig, use_heap);
  119. return;
  120. }
  121. index = var;
  122. index_len = var_len;
  123. if (is_array) {
  124. int nest_level = 0;
  125. while (1) {
  126. char *index_s;
  127. size_t new_idx_len = 0;
  128. if(++nest_level > PG(max_input_nesting_level)) {
  129. HashTable *ht;
  130. /* too many levels of nesting */
  131. if (track_vars_array) {
  132. ht = Z_ARRVAL_P(track_vars_array);
  133. zend_symtable_str_del(ht, var, var_len);
  134. }
  135. zval_ptr_dtor_nogc(val);
  136. /* do not output the error message to the screen,
  137. this helps us to to avoid "information disclosure" */
  138. if (!PG(display_errors)) {
  139. php_error_docref(NULL, E_WARNING, "Input variable nesting level exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_nesting_level in php.ini.", PG(max_input_nesting_level));
  140. }
  141. free_alloca(var_orig, use_heap);
  142. return;
  143. }
  144. ip++;
  145. index_s = ip;
  146. if (isspace(*ip)) {
  147. ip++;
  148. }
  149. if (*ip==']') {
  150. index_s = NULL;
  151. } else {
  152. ip = strchr(ip, ']');
  153. if (!ip) {
  154. /* not an index; un-terminate the var name */
  155. *(index_s - 1) = '_';
  156. /* PHP variables cannot contain ' ', '.', '[' in their names, so we replace the characters with a '_' */
  157. for (p = index_s; *p; p++) {
  158. if (*p == ' ' || *p == '.' || *p == '[') {
  159. *p = '_';
  160. }
  161. }
  162. index_len = 0;
  163. if (index) {
  164. index_len = strlen(index);
  165. }
  166. goto plain_var;
  167. return;
  168. }
  169. *ip = 0;
  170. new_idx_len = strlen(index_s);
  171. }
  172. if (!index) {
  173. array_init(&gpc_element);
  174. if ((gpc_element_p = zend_hash_next_index_insert(symtable1, &gpc_element)) == NULL) {
  175. zend_array_destroy(Z_ARR(gpc_element));
  176. zval_ptr_dtor_nogc(val);
  177. free_alloca(var_orig, use_heap);
  178. return;
  179. }
  180. } else {
  181. gpc_element_p = zend_symtable_str_find(symtable1, index, index_len);
  182. if (!gpc_element_p) {
  183. zval tmp;
  184. array_init(&tmp);
  185. gpc_element_p = zend_symtable_str_update_ind(symtable1, index, index_len, &tmp);
  186. } else {
  187. if (Z_TYPE_P(gpc_element_p) == IS_INDIRECT) {
  188. gpc_element_p = Z_INDIRECT_P(gpc_element_p);
  189. }
  190. if (Z_TYPE_P(gpc_element_p) != IS_ARRAY) {
  191. zval_ptr_dtor_nogc(gpc_element_p);
  192. array_init(gpc_element_p);
  193. } else {
  194. SEPARATE_ARRAY(gpc_element_p);
  195. }
  196. }
  197. }
  198. symtable1 = Z_ARRVAL_P(gpc_element_p);
  199. /* ip pointed to the '[' character, now obtain the key */
  200. index = index_s;
  201. index_len = new_idx_len;
  202. ip++;
  203. if (*ip == '[') {
  204. is_array = 1;
  205. *ip = 0;
  206. } else {
  207. goto plain_var;
  208. }
  209. }
  210. } else {
  211. plain_var:
  212. if (!index) {
  213. if (zend_hash_next_index_insert(symtable1, val) == NULL) {
  214. zval_ptr_dtor_nogc(val);
  215. }
  216. } else {
  217. zend_ulong idx;
  218. /*
  219. * According to rfc2965, more specific paths are listed above the less specific ones.
  220. * If we encounter a duplicate cookie name, we should skip it, since it is not possible
  221. * to have the same (plain text) cookie name for the same path and we should not overwrite
  222. * more specific cookies with the less specific ones.
  223. */
  224. if (Z_TYPE(PG(http_globals)[TRACK_VARS_COOKIE]) != IS_UNDEF &&
  225. symtable1 == Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]) &&
  226. zend_symtable_str_exists(symtable1, index, index_len)) {
  227. zval_ptr_dtor_nogc(val);
  228. } else if (ZEND_HANDLE_NUMERIC_STR(index, index_len, idx)) {
  229. zend_hash_index_update(symtable1, idx, val);
  230. } else {
  231. php_register_variable_quick(index, index_len, val, symtable1);
  232. }
  233. }
  234. }
  235. free_alloca(var_orig, use_heap);
  236. }
  237. typedef struct post_var_data {
  238. smart_str str;
  239. char *ptr;
  240. char *end;
  241. uint64_t cnt;
  242. /* Bytes in ptr that have already been scanned for '&' */
  243. size_t already_scanned;
  244. } post_var_data_t;
  245. static bool add_post_var(zval *arr, post_var_data_t *var, bool eof)
  246. {
  247. char *start, *ksep, *vsep, *val;
  248. size_t klen, vlen;
  249. size_t new_vlen;
  250. if (var->ptr >= var->end) {
  251. return 0;
  252. }
  253. start = var->ptr + var->already_scanned;
  254. vsep = memchr(start, '&', var->end - start);
  255. if (!vsep) {
  256. if (!eof) {
  257. var->already_scanned = var->end - var->ptr;
  258. return 0;
  259. } else {
  260. vsep = var->end;
  261. }
  262. }
  263. ksep = memchr(var->ptr, '=', vsep - var->ptr);
  264. if (ksep) {
  265. *ksep = '\0';
  266. /* "foo=bar&" or "foo=&" */
  267. klen = ksep - var->ptr;
  268. vlen = vsep - ++ksep;
  269. } else {
  270. ksep = "";
  271. /* "foo&" */
  272. klen = vsep - var->ptr;
  273. vlen = 0;
  274. }
  275. php_url_decode(var->ptr, klen);
  276. val = estrndup(ksep, vlen);
  277. if (vlen) {
  278. vlen = php_url_decode(val, vlen);
  279. }
  280. if (sapi_module.input_filter(PARSE_POST, var->ptr, &val, vlen, &new_vlen)) {
  281. php_register_variable_safe(var->ptr, val, new_vlen, arr);
  282. }
  283. efree(val);
  284. var->ptr = vsep + (vsep != var->end);
  285. var->already_scanned = 0;
  286. return 1;
  287. }
  288. static inline int add_post_vars(zval *arr, post_var_data_t *vars, bool eof)
  289. {
  290. uint64_t max_vars = PG(max_input_vars);
  291. vars->ptr = ZSTR_VAL(vars->str.s);
  292. vars->end = ZSTR_VAL(vars->str.s) + ZSTR_LEN(vars->str.s);
  293. while (add_post_var(arr, vars, eof)) {
  294. if (++vars->cnt > max_vars) {
  295. php_error_docref(NULL, E_WARNING,
  296. "Input variables exceeded %" PRIu64 ". "
  297. "To increase the limit change max_input_vars in php.ini.",
  298. max_vars);
  299. return FAILURE;
  300. }
  301. }
  302. if (!eof && ZSTR_VAL(vars->str.s) != vars->ptr) {
  303. memmove(ZSTR_VAL(vars->str.s), vars->ptr, ZSTR_LEN(vars->str.s) = vars->end - vars->ptr);
  304. }
  305. return SUCCESS;
  306. }
  307. #ifdef PHP_WIN32
  308. #define SAPI_POST_HANDLER_BUFSIZ 16384
  309. #else
  310. # define SAPI_POST_HANDLER_BUFSIZ BUFSIZ
  311. #endif
  312. SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
  313. {
  314. zval *arr = (zval *) arg;
  315. php_stream *s = SG(request_info).request_body;
  316. post_var_data_t post_data;
  317. if (s && SUCCESS == php_stream_rewind(s)) {
  318. memset(&post_data, 0, sizeof(post_data));
  319. while (!php_stream_eof(s)) {
  320. char buf[SAPI_POST_HANDLER_BUFSIZ] = {0};
  321. ssize_t len = php_stream_read(s, buf, SAPI_POST_HANDLER_BUFSIZ);
  322. if (len > 0) {
  323. smart_str_appendl(&post_data.str, buf, len);
  324. if (SUCCESS != add_post_vars(arr, &post_data, 0)) {
  325. smart_str_free(&post_data.str);
  326. return;
  327. }
  328. }
  329. if (len != SAPI_POST_HANDLER_BUFSIZ){
  330. break;
  331. }
  332. }
  333. if (post_data.str.s) {
  334. add_post_vars(arr, &post_data, 1);
  335. smart_str_free(&post_data.str);
  336. }
  337. }
  338. }
  339. #undef SAPI_POST_HANDLER_BUFSIZ
  340. SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter)
  341. {
  342. /* TODO: check .ini setting here and apply user-defined input filter */
  343. if(new_val_len) *new_val_len = val_len;
  344. return 1;
  345. }
  346. SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
  347. {
  348. char *res = NULL, *var, *val, *separator = NULL;
  349. const char *c_var;
  350. zval array;
  351. int free_buffer = 0;
  352. char *strtok_buf = NULL;
  353. zend_long count = 0;
  354. ZVAL_UNDEF(&array);
  355. switch (arg) {
  356. case PARSE_POST:
  357. case PARSE_GET:
  358. case PARSE_COOKIE:
  359. array_init(&array);
  360. switch (arg) {
  361. case PARSE_POST:
  362. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
  363. ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_POST], &array);
  364. break;
  365. case PARSE_GET:
  366. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_GET]);
  367. ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_GET], &array);
  368. break;
  369. case PARSE_COOKIE:
  370. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_COOKIE]);
  371. ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_COOKIE], &array);
  372. break;
  373. }
  374. break;
  375. default:
  376. ZVAL_COPY_VALUE(&array, destArray);
  377. break;
  378. }
  379. if (arg == PARSE_POST) {
  380. sapi_handle_post(&array);
  381. return;
  382. }
  383. if (arg == PARSE_GET) { /* GET data */
  384. c_var = SG(request_info).query_string;
  385. if (c_var && *c_var) {
  386. res = (char *) estrdup(c_var);
  387. free_buffer = 1;
  388. } else {
  389. free_buffer = 0;
  390. }
  391. } else if (arg == PARSE_COOKIE) { /* Cookie data */
  392. c_var = SG(request_info).cookie_data;
  393. if (c_var && *c_var) {
  394. res = (char *) estrdup(c_var);
  395. free_buffer = 1;
  396. } else {
  397. free_buffer = 0;
  398. }
  399. } else if (arg == PARSE_STRING) { /* String data */
  400. res = str;
  401. free_buffer = 1;
  402. }
  403. if (!res) {
  404. return;
  405. }
  406. switch (arg) {
  407. case PARSE_GET:
  408. case PARSE_STRING:
  409. separator = PG(arg_separator).input;
  410. break;
  411. case PARSE_COOKIE:
  412. separator = ";\0";
  413. break;
  414. }
  415. var = php_strtok_r(res, separator, &strtok_buf);
  416. while (var) {
  417. size_t val_len;
  418. size_t new_val_len;
  419. val = strchr(var, '=');
  420. if (arg == PARSE_COOKIE) {
  421. /* Remove leading spaces from cookie names, needed for multi-cookie header where ; can be followed by a space */
  422. while (isspace(*var)) {
  423. var++;
  424. }
  425. if (var == val || *var == '\0') {
  426. goto next_cookie;
  427. }
  428. }
  429. if (++count > PG(max_input_vars)) {
  430. php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
  431. break;
  432. }
  433. if (val) { /* have a value */
  434. *val++ = '\0';
  435. if (arg == PARSE_COOKIE) {
  436. val_len = php_raw_url_decode(val, strlen(val));
  437. } else {
  438. val_len = php_url_decode(val, strlen(val));
  439. }
  440. } else {
  441. val = "";
  442. val_len = 0;
  443. }
  444. val = estrndup(val, val_len);
  445. if (arg != PARSE_COOKIE) {
  446. php_url_decode(var, strlen(var));
  447. }
  448. if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len)) {
  449. php_register_variable_safe(var, val, new_val_len, &array);
  450. }
  451. efree(val);
  452. next_cookie:
  453. var = php_strtok_r(NULL, separator, &strtok_buf);
  454. }
  455. if (free_buffer) {
  456. efree(res);
  457. }
  458. }
  459. static zend_always_inline int valid_environment_name(const char *name, const char *end)
  460. {
  461. const char *s;
  462. for (s = name; s < end; s++) {
  463. if (*s == ' ' || *s == '.' || *s == '[') {
  464. return 0;
  465. }
  466. }
  467. return 1;
  468. }
  469. static zend_always_inline void import_environment_variable(HashTable *ht, char *env)
  470. {
  471. char *p;
  472. size_t name_len, len;
  473. zval val;
  474. zend_ulong idx;
  475. p = strchr(env, '=');
  476. if (!p
  477. || p == env
  478. || !valid_environment_name(env, p)) {
  479. /* malformed entry? */
  480. return;
  481. }
  482. name_len = p - env;
  483. p++;
  484. len = strlen(p);
  485. ZVAL_STRINGL_FAST(&val, p, len);
  486. if (ZEND_HANDLE_NUMERIC_STR(env, name_len, idx)) {
  487. zend_hash_index_update(ht, idx, &val);
  488. } else {
  489. php_register_variable_quick(env, name_len, &val, ht);
  490. }
  491. }
  492. void _php_import_environment_variables(zval *array_ptr)
  493. {
  494. tsrm_env_lock();
  495. #ifndef PHP_WIN32
  496. for (char **env = environ; env != NULL && *env != NULL; env++) {
  497. import_environment_variable(Z_ARRVAL_P(array_ptr), *env);
  498. }
  499. #else
  500. char *environment = GetEnvironmentStringsA();
  501. for (char *env = environment; env != NULL && *env; env += strlen(env) + 1) {
  502. import_environment_variable(Z_ARRVAL_P(array_ptr), env);
  503. }
  504. FreeEnvironmentStringsA(environment);
  505. #endif
  506. tsrm_env_unlock();
  507. }
  508. bool php_std_auto_global_callback(char *name, uint32_t name_len)
  509. {
  510. zend_printf("%s\n", name);
  511. return 0; /* don't rearm */
  512. }
  513. /* {{{ php_build_argv */
  514. PHPAPI void php_build_argv(const char *s, zval *track_vars_array)
  515. {
  516. zval arr, argc, tmp;
  517. int count = 0;
  518. if (!(SG(request_info).argc || track_vars_array)) {
  519. return;
  520. }
  521. array_init(&arr);
  522. /* Prepare argv */
  523. if (SG(request_info).argc) { /* are we in cli sapi? */
  524. int i;
  525. for (i = 0; i < SG(request_info).argc; i++) {
  526. ZVAL_STRING(&tmp, SG(request_info).argv[i]);
  527. if (zend_hash_next_index_insert(Z_ARRVAL(arr), &tmp) == NULL) {
  528. zend_string_efree(Z_STR(tmp));
  529. }
  530. }
  531. } else if (s && *s) {
  532. while (1) {
  533. const char *space = strchr(s, '+');
  534. /* auto-type */
  535. ZVAL_STRINGL(&tmp, s, space ? space - s : strlen(s));
  536. count++;
  537. if (zend_hash_next_index_insert(Z_ARRVAL(arr), &tmp) == NULL) {
  538. zend_string_efree(Z_STR(tmp));
  539. }
  540. if (!space) {
  541. break;
  542. }
  543. s = space + 1;
  544. }
  545. }
  546. /* prepare argc */
  547. if (SG(request_info).argc) {
  548. ZVAL_LONG(&argc, SG(request_info).argc);
  549. } else {
  550. ZVAL_LONG(&argc, count);
  551. }
  552. if (SG(request_info).argc) {
  553. Z_ADDREF(arr);
  554. zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), &arr);
  555. zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), &argc);
  556. }
  557. if (track_vars_array && Z_TYPE_P(track_vars_array) == IS_ARRAY) {
  558. Z_ADDREF(arr);
  559. zend_hash_update(Z_ARRVAL_P(track_vars_array), ZSTR_KNOWN(ZEND_STR_ARGV), &arr);
  560. zend_hash_update(Z_ARRVAL_P(track_vars_array), ZSTR_KNOWN(ZEND_STR_ARGC), &argc);
  561. }
  562. zval_ptr_dtor_nogc(&arr);
  563. }
  564. /* }}} */
  565. /* {{{ php_register_server_variables */
  566. static inline void php_register_server_variables(void)
  567. {
  568. zval tmp;
  569. zval *arr = &PG(http_globals)[TRACK_VARS_SERVER];
  570. HashTable *ht;
  571. zval_ptr_dtor_nogc(arr);
  572. array_init(arr);
  573. /* Server variables */
  574. if (sapi_module.register_server_variables) {
  575. sapi_module.register_server_variables(arr);
  576. }
  577. ht = Z_ARRVAL_P(arr);
  578. /* PHP Authentication support */
  579. if (SG(request_info).auth_user) {
  580. ZVAL_STRING(&tmp, SG(request_info).auth_user);
  581. php_register_variable_quick("PHP_AUTH_USER", sizeof("PHP_AUTH_USER")-1, &tmp, ht);
  582. }
  583. if (SG(request_info).auth_password) {
  584. ZVAL_STRING(&tmp, SG(request_info).auth_password);
  585. php_register_variable_quick("PHP_AUTH_PW", sizeof("PHP_AUTH_PW")-1, &tmp, ht);
  586. }
  587. if (SG(request_info).auth_digest) {
  588. ZVAL_STRING(&tmp, SG(request_info).auth_digest);
  589. php_register_variable_quick("PHP_AUTH_DIGEST", sizeof("PHP_AUTH_DIGEST")-1, &tmp, ht);
  590. }
  591. /* store request init time */
  592. ZVAL_DOUBLE(&tmp, sapi_get_request_time());
  593. php_register_variable_quick("REQUEST_TIME_FLOAT", sizeof("REQUEST_TIME_FLOAT")-1, &tmp, ht);
  594. ZVAL_LONG(&tmp, zend_dval_to_lval(Z_DVAL(tmp)));
  595. php_register_variable_quick("REQUEST_TIME", sizeof("REQUEST_TIME")-1, &tmp, ht);
  596. }
  597. /* }}} */
  598. /* {{{ php_autoglobal_merge */
  599. static void php_autoglobal_merge(HashTable *dest, HashTable *src)
  600. {
  601. zval *src_entry, *dest_entry;
  602. zend_string *string_key;
  603. zend_ulong num_key;
  604. int globals_check = (dest == (&EG(symbol_table)));
  605. ZEND_HASH_FOREACH_KEY_VAL(src, num_key, string_key, src_entry) {
  606. if (Z_TYPE_P(src_entry) != IS_ARRAY
  607. || (string_key && (dest_entry = zend_hash_find(dest, string_key)) == NULL)
  608. || (string_key == NULL && (dest_entry = zend_hash_index_find(dest, num_key)) == NULL)
  609. || Z_TYPE_P(dest_entry) != IS_ARRAY) {
  610. Z_TRY_ADDREF_P(src_entry);
  611. if (string_key) {
  612. if (!globals_check || ZSTR_LEN(string_key) != sizeof("GLOBALS") - 1
  613. || memcmp(ZSTR_VAL(string_key), "GLOBALS", sizeof("GLOBALS") - 1)) {
  614. zend_hash_update(dest, string_key, src_entry);
  615. } else {
  616. Z_TRY_DELREF_P(src_entry);
  617. }
  618. } else {
  619. zend_hash_index_update(dest, num_key, src_entry);
  620. }
  621. } else {
  622. SEPARATE_ARRAY(dest_entry);
  623. php_autoglobal_merge(Z_ARRVAL_P(dest_entry), Z_ARRVAL_P(src_entry));
  624. }
  625. } ZEND_HASH_FOREACH_END();
  626. }
  627. /* }}} */
  628. /* {{{ php_hash_environment */
  629. PHPAPI int php_hash_environment(void)
  630. {
  631. memset(PG(http_globals), 0, sizeof(PG(http_globals)));
  632. zend_activate_auto_globals();
  633. if (PG(register_argc_argv)) {
  634. php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
  635. }
  636. return SUCCESS;
  637. }
  638. /* }}} */
  639. static bool php_auto_globals_create_get(zend_string *name)
  640. {
  641. if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) {
  642. sapi_module.treat_data(PARSE_GET, NULL, NULL);
  643. } else {
  644. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_GET]);
  645. array_init(&PG(http_globals)[TRACK_VARS_GET]);
  646. }
  647. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);
  648. Z_ADDREF(PG(http_globals)[TRACK_VARS_GET]);
  649. return 0; /* don't rearm */
  650. }
  651. static bool php_auto_globals_create_post(zend_string *name)
  652. {
  653. if (PG(variables_order) &&
  654. (strchr(PG(variables_order),'P') || strchr(PG(variables_order),'p')) &&
  655. !SG(headers_sent) &&
  656. SG(request_info).request_method &&
  657. !strcasecmp(SG(request_info).request_method, "POST")) {
  658. sapi_module.treat_data(PARSE_POST, NULL, NULL);
  659. } else {
  660. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
  661. array_init(&PG(http_globals)[TRACK_VARS_POST]);
  662. }
  663. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);
  664. Z_ADDREF(PG(http_globals)[TRACK_VARS_POST]);
  665. return 0; /* don't rearm */
  666. }
  667. static bool php_auto_globals_create_cookie(zend_string *name)
  668. {
  669. if (PG(variables_order) && (strchr(PG(variables_order),'C') || strchr(PG(variables_order),'c'))) {
  670. sapi_module.treat_data(PARSE_COOKIE, NULL, NULL);
  671. } else {
  672. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_COOKIE]);
  673. array_init(&PG(http_globals)[TRACK_VARS_COOKIE]);
  674. }
  675. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);
  676. Z_ADDREF(PG(http_globals)[TRACK_VARS_COOKIE]);
  677. return 0; /* don't rearm */
  678. }
  679. static bool php_auto_globals_create_files(zend_string *name)
  680. {
  681. if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) == IS_UNDEF) {
  682. array_init(&PG(http_globals)[TRACK_VARS_FILES]);
  683. }
  684. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);
  685. Z_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
  686. return 0; /* don't rearm */
  687. }
  688. /* Ugly hack to fix HTTP_PROXY issue, see bug #72573 */
  689. static void check_http_proxy(HashTable *var_table)
  690. {
  691. if (zend_hash_str_exists(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1)) {
  692. char *local_proxy = getenv("HTTP_PROXY");
  693. if (!local_proxy) {
  694. zend_hash_str_del(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1);
  695. } else {
  696. zval local_zval;
  697. ZVAL_STRING(&local_zval, local_proxy);
  698. zend_hash_str_update(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1, &local_zval);
  699. }
  700. }
  701. }
  702. static bool php_auto_globals_create_server(zend_string *name)
  703. {
  704. if (PG(variables_order) && (strchr(PG(variables_order),'S') || strchr(PG(variables_order),'s'))) {
  705. php_register_server_variables();
  706. if (PG(register_argc_argv)) {
  707. if (SG(request_info).argc) {
  708. zval *argc, *argv;
  709. if ((argc = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), 1)) != NULL &&
  710. (argv = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL) {
  711. Z_ADDREF_P(argv);
  712. zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), argv);
  713. zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGC), argc);
  714. }
  715. } else {
  716. php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
  717. }
  718. }
  719. } else {
  720. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_SERVER]);
  721. array_init(&PG(http_globals)[TRACK_VARS_SERVER]);
  722. }
  723. check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
  724. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);
  725. Z_ADDREF(PG(http_globals)[TRACK_VARS_SERVER]);
  726. /* TODO: TRACK_VARS_SERVER is modified in a number of places (e.g. phar) past this point,
  727. * where rc>1 due to the $_SERVER global. Ideally this shouldn't happen, but for now we
  728. * ignore this issue, as it would probably require larger changes. */
  729. HT_ALLOW_COW_VIOLATION(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
  730. return 0; /* don't rearm */
  731. }
  732. static bool php_auto_globals_create_env(zend_string *name)
  733. {
  734. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_ENV]);
  735. array_init(&PG(http_globals)[TRACK_VARS_ENV]);
  736. if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
  737. php_import_environment_variables(&PG(http_globals)[TRACK_VARS_ENV]);
  738. }
  739. check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV]));
  740. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);
  741. Z_ADDREF(PG(http_globals)[TRACK_VARS_ENV]);
  742. return 0; /* don't rearm */
  743. }
  744. static bool php_auto_globals_create_request(zend_string *name)
  745. {
  746. zval form_variables;
  747. unsigned char _gpc_flags[3] = {0, 0, 0};
  748. char *p;
  749. array_init(&form_variables);
  750. if (PG(request_order) != NULL) {
  751. p = PG(request_order);
  752. } else {
  753. p = PG(variables_order);
  754. }
  755. for (; p && *p; p++) {
  756. switch (*p) {
  757. case 'g':
  758. case 'G':
  759. if (!_gpc_flags[0]) {
  760. php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_GET]));
  761. _gpc_flags[0] = 1;
  762. }
  763. break;
  764. case 'p':
  765. case 'P':
  766. if (!_gpc_flags[1]) {
  767. php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_POST]));
  768. _gpc_flags[1] = 1;
  769. }
  770. break;
  771. case 'c':
  772. case 'C':
  773. if (!_gpc_flags[2]) {
  774. php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]));
  775. _gpc_flags[2] = 1;
  776. }
  777. break;
  778. }
  779. }
  780. zend_hash_update(&EG(symbol_table), name, &form_variables);
  781. return 0;
  782. }
  783. void php_startup_auto_globals(void)
  784. {
  785. zend_register_auto_global(zend_string_init_interned("_GET", sizeof("_GET")-1, 1), 0, php_auto_globals_create_get);
  786. zend_register_auto_global(zend_string_init_interned("_POST", sizeof("_POST")-1, 1), 0, php_auto_globals_create_post);
  787. zend_register_auto_global(zend_string_init_interned("_COOKIE", sizeof("_COOKIE")-1, 1), 0, php_auto_globals_create_cookie);
  788. zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER), PG(auto_globals_jit), php_auto_globals_create_server);
  789. zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV), PG(auto_globals_jit), php_auto_globals_create_env);
  790. zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST), PG(auto_globals_jit), php_auto_globals_create_request);
  791. zend_register_auto_global(zend_string_init_interned("_FILES", sizeof("_FILES")-1, 1), 0, php_auto_globals_create_files);
  792. }