php_variables.c 25 KB

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