php_variables.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. wchar_t *environmentw = GetEnvironmentStringsW();
  501. for (wchar_t *envw = environmentw; envw != NULL && *envw; envw += wcslen(envw) + 1) {
  502. char *env = php_win32_cp_w_to_any(envw);
  503. if (env != NULL) {
  504. import_environment_variable(Z_ARRVAL_P(array_ptr), env);
  505. free(env);
  506. }
  507. }
  508. FreeEnvironmentStringsW(environmentw);
  509. #endif
  510. tsrm_env_unlock();
  511. }
  512. bool php_std_auto_global_callback(char *name, uint32_t name_len)
  513. {
  514. zend_printf("%s\n", name);
  515. return 0; /* don't rearm */
  516. }
  517. /* {{{ php_build_argv */
  518. PHPAPI void php_build_argv(const char *s, zval *track_vars_array)
  519. {
  520. zval arr, argc, tmp;
  521. int count = 0;
  522. if (!(SG(request_info).argc || track_vars_array)) {
  523. return;
  524. }
  525. array_init(&arr);
  526. /* Prepare argv */
  527. if (SG(request_info).argc) { /* are we in cli sapi? */
  528. int i;
  529. for (i = 0; i < SG(request_info).argc; i++) {
  530. ZVAL_STRING(&tmp, SG(request_info).argv[i]);
  531. if (zend_hash_next_index_insert(Z_ARRVAL(arr), &tmp) == NULL) {
  532. zend_string_efree(Z_STR(tmp));
  533. }
  534. }
  535. } else if (s && *s) {
  536. while (1) {
  537. const char *space = strchr(s, '+');
  538. /* auto-type */
  539. ZVAL_STRINGL(&tmp, s, space ? space - s : strlen(s));
  540. count++;
  541. if (zend_hash_next_index_insert(Z_ARRVAL(arr), &tmp) == NULL) {
  542. zend_string_efree(Z_STR(tmp));
  543. }
  544. if (!space) {
  545. break;
  546. }
  547. s = space + 1;
  548. }
  549. }
  550. /* prepare argc */
  551. if (SG(request_info).argc) {
  552. ZVAL_LONG(&argc, SG(request_info).argc);
  553. } else {
  554. ZVAL_LONG(&argc, count);
  555. }
  556. if (SG(request_info).argc) {
  557. Z_ADDREF(arr);
  558. zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), &arr);
  559. zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), &argc);
  560. }
  561. if (track_vars_array && Z_TYPE_P(track_vars_array) == IS_ARRAY) {
  562. Z_ADDREF(arr);
  563. zend_hash_update(Z_ARRVAL_P(track_vars_array), ZSTR_KNOWN(ZEND_STR_ARGV), &arr);
  564. zend_hash_update(Z_ARRVAL_P(track_vars_array), ZSTR_KNOWN(ZEND_STR_ARGC), &argc);
  565. }
  566. zval_ptr_dtor_nogc(&arr);
  567. }
  568. /* }}} */
  569. /* {{{ php_register_server_variables */
  570. static inline void php_register_server_variables(void)
  571. {
  572. zval tmp;
  573. zval *arr = &PG(http_globals)[TRACK_VARS_SERVER];
  574. HashTable *ht;
  575. zval_ptr_dtor_nogc(arr);
  576. array_init(arr);
  577. /* Server variables */
  578. if (sapi_module.register_server_variables) {
  579. sapi_module.register_server_variables(arr);
  580. }
  581. ht = Z_ARRVAL_P(arr);
  582. /* PHP Authentication support */
  583. if (SG(request_info).auth_user) {
  584. ZVAL_STRING(&tmp, SG(request_info).auth_user);
  585. php_register_variable_quick("PHP_AUTH_USER", sizeof("PHP_AUTH_USER")-1, &tmp, ht);
  586. }
  587. if (SG(request_info).auth_password) {
  588. ZVAL_STRING(&tmp, SG(request_info).auth_password);
  589. php_register_variable_quick("PHP_AUTH_PW", sizeof("PHP_AUTH_PW")-1, &tmp, ht);
  590. }
  591. if (SG(request_info).auth_digest) {
  592. ZVAL_STRING(&tmp, SG(request_info).auth_digest);
  593. php_register_variable_quick("PHP_AUTH_DIGEST", sizeof("PHP_AUTH_DIGEST")-1, &tmp, ht);
  594. }
  595. /* store request init time */
  596. ZVAL_DOUBLE(&tmp, sapi_get_request_time());
  597. php_register_variable_quick("REQUEST_TIME_FLOAT", sizeof("REQUEST_TIME_FLOAT")-1, &tmp, ht);
  598. ZVAL_LONG(&tmp, zend_dval_to_lval(Z_DVAL(tmp)));
  599. php_register_variable_quick("REQUEST_TIME", sizeof("REQUEST_TIME")-1, &tmp, ht);
  600. }
  601. /* }}} */
  602. /* {{{ php_autoglobal_merge */
  603. static void php_autoglobal_merge(HashTable *dest, HashTable *src)
  604. {
  605. zval *src_entry, *dest_entry;
  606. zend_string *string_key;
  607. zend_ulong num_key;
  608. int globals_check = (dest == (&EG(symbol_table)));
  609. ZEND_HASH_FOREACH_KEY_VAL(src, num_key, string_key, src_entry) {
  610. if (Z_TYPE_P(src_entry) != IS_ARRAY
  611. || (string_key && (dest_entry = zend_hash_find(dest, string_key)) == NULL)
  612. || (string_key == NULL && (dest_entry = zend_hash_index_find(dest, num_key)) == NULL)
  613. || Z_TYPE_P(dest_entry) != IS_ARRAY) {
  614. Z_TRY_ADDREF_P(src_entry);
  615. if (string_key) {
  616. if (!globals_check || ZSTR_LEN(string_key) != sizeof("GLOBALS") - 1
  617. || memcmp(ZSTR_VAL(string_key), "GLOBALS", sizeof("GLOBALS") - 1)) {
  618. zend_hash_update(dest, string_key, src_entry);
  619. } else {
  620. Z_TRY_DELREF_P(src_entry);
  621. }
  622. } else {
  623. zend_hash_index_update(dest, num_key, src_entry);
  624. }
  625. } else {
  626. SEPARATE_ARRAY(dest_entry);
  627. php_autoglobal_merge(Z_ARRVAL_P(dest_entry), Z_ARRVAL_P(src_entry));
  628. }
  629. } ZEND_HASH_FOREACH_END();
  630. }
  631. /* }}} */
  632. /* {{{ php_hash_environment */
  633. PHPAPI int php_hash_environment(void)
  634. {
  635. memset(PG(http_globals), 0, sizeof(PG(http_globals)));
  636. zend_activate_auto_globals();
  637. if (PG(register_argc_argv)) {
  638. php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
  639. }
  640. return SUCCESS;
  641. }
  642. /* }}} */
  643. static bool php_auto_globals_create_get(zend_string *name)
  644. {
  645. if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) {
  646. sapi_module.treat_data(PARSE_GET, NULL, NULL);
  647. } else {
  648. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_GET]);
  649. array_init(&PG(http_globals)[TRACK_VARS_GET]);
  650. }
  651. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);
  652. Z_ADDREF(PG(http_globals)[TRACK_VARS_GET]);
  653. return 0; /* don't rearm */
  654. }
  655. static bool php_auto_globals_create_post(zend_string *name)
  656. {
  657. if (PG(variables_order) &&
  658. (strchr(PG(variables_order),'P') || strchr(PG(variables_order),'p')) &&
  659. !SG(headers_sent) &&
  660. SG(request_info).request_method &&
  661. !strcasecmp(SG(request_info).request_method, "POST")) {
  662. sapi_module.treat_data(PARSE_POST, NULL, NULL);
  663. } else {
  664. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
  665. array_init(&PG(http_globals)[TRACK_VARS_POST]);
  666. }
  667. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);
  668. Z_ADDREF(PG(http_globals)[TRACK_VARS_POST]);
  669. return 0; /* don't rearm */
  670. }
  671. static bool php_auto_globals_create_cookie(zend_string *name)
  672. {
  673. if (PG(variables_order) && (strchr(PG(variables_order),'C') || strchr(PG(variables_order),'c'))) {
  674. sapi_module.treat_data(PARSE_COOKIE, NULL, NULL);
  675. } else {
  676. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_COOKIE]);
  677. array_init(&PG(http_globals)[TRACK_VARS_COOKIE]);
  678. }
  679. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);
  680. Z_ADDREF(PG(http_globals)[TRACK_VARS_COOKIE]);
  681. return 0; /* don't rearm */
  682. }
  683. static bool php_auto_globals_create_files(zend_string *name)
  684. {
  685. if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) == IS_UNDEF) {
  686. array_init(&PG(http_globals)[TRACK_VARS_FILES]);
  687. }
  688. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);
  689. Z_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
  690. return 0; /* don't rearm */
  691. }
  692. /* Ugly hack to fix HTTP_PROXY issue, see bug #72573 */
  693. static void check_http_proxy(HashTable *var_table)
  694. {
  695. if (zend_hash_str_exists(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1)) {
  696. char *local_proxy = getenv("HTTP_PROXY");
  697. if (!local_proxy) {
  698. zend_hash_str_del(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1);
  699. } else {
  700. zval local_zval;
  701. ZVAL_STRING(&local_zval, local_proxy);
  702. zend_hash_str_update(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1, &local_zval);
  703. }
  704. }
  705. }
  706. static bool php_auto_globals_create_server(zend_string *name)
  707. {
  708. if (PG(variables_order) && (strchr(PG(variables_order),'S') || strchr(PG(variables_order),'s'))) {
  709. php_register_server_variables();
  710. if (PG(register_argc_argv)) {
  711. if (SG(request_info).argc) {
  712. zval *argc, *argv;
  713. if ((argc = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), 1)) != NULL &&
  714. (argv = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL) {
  715. Z_ADDREF_P(argv);
  716. zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), argv);
  717. zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGC), argc);
  718. }
  719. } else {
  720. php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
  721. }
  722. }
  723. } else {
  724. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_SERVER]);
  725. array_init(&PG(http_globals)[TRACK_VARS_SERVER]);
  726. }
  727. check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
  728. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);
  729. Z_ADDREF(PG(http_globals)[TRACK_VARS_SERVER]);
  730. /* TODO: TRACK_VARS_SERVER is modified in a number of places (e.g. phar) past this point,
  731. * where rc>1 due to the $_SERVER global. Ideally this shouldn't happen, but for now we
  732. * ignore this issue, as it would probably require larger changes. */
  733. HT_ALLOW_COW_VIOLATION(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
  734. return 0; /* don't rearm */
  735. }
  736. static bool php_auto_globals_create_env(zend_string *name)
  737. {
  738. zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_ENV]);
  739. array_init(&PG(http_globals)[TRACK_VARS_ENV]);
  740. if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
  741. php_import_environment_variables(&PG(http_globals)[TRACK_VARS_ENV]);
  742. }
  743. check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV]));
  744. zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);
  745. Z_ADDREF(PG(http_globals)[TRACK_VARS_ENV]);
  746. return 0; /* don't rearm */
  747. }
  748. static bool php_auto_globals_create_request(zend_string *name)
  749. {
  750. zval form_variables;
  751. unsigned char _gpc_flags[3] = {0, 0, 0};
  752. char *p;
  753. array_init(&form_variables);
  754. if (PG(request_order) != NULL) {
  755. p = PG(request_order);
  756. } else {
  757. p = PG(variables_order);
  758. }
  759. for (; p && *p; p++) {
  760. switch (*p) {
  761. case 'g':
  762. case 'G':
  763. if (!_gpc_flags[0]) {
  764. php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_GET]));
  765. _gpc_flags[0] = 1;
  766. }
  767. break;
  768. case 'p':
  769. case 'P':
  770. if (!_gpc_flags[1]) {
  771. php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_POST]));
  772. _gpc_flags[1] = 1;
  773. }
  774. break;
  775. case 'c':
  776. case 'C':
  777. if (!_gpc_flags[2]) {
  778. php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]));
  779. _gpc_flags[2] = 1;
  780. }
  781. break;
  782. }
  783. }
  784. zend_hash_update(&EG(symbol_table), name, &form_variables);
  785. return 0;
  786. }
  787. void php_startup_auto_globals(void)
  788. {
  789. zend_register_auto_global(zend_string_init_interned("_GET", sizeof("_GET")-1, 1), 0, php_auto_globals_create_get);
  790. zend_register_auto_global(zend_string_init_interned("_POST", sizeof("_POST")-1, 1), 0, php_auto_globals_create_post);
  791. zend_register_auto_global(zend_string_init_interned("_COOKIE", sizeof("_COOKIE")-1, 1), 0, php_auto_globals_create_cookie);
  792. zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER), PG(auto_globals_jit), php_auto_globals_create_server);
  793. zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV), PG(auto_globals_jit), php_auto_globals_create_env);
  794. zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST), PG(auto_globals_jit), php_auto_globals_create_request);
  795. zend_register_auto_global(zend_string_init_interned("_FILES", sizeof("_FILES")-1, 1), 0, php_auto_globals_create_files);
  796. }