phpdbg_webdata_transfer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Bob Weinand <bwoebi@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "phpdbg_webdata_transfer.h"
  19. #include "ext/standard/php_var.h"
  20. static int phpdbg_is_auto_global(char *name, int len) {
  21. int ret;
  22. zend_string *str = zend_string_init(name, len, 0);
  23. ret = zend_is_auto_global(str);
  24. zend_string_free(str);
  25. return ret;
  26. }
  27. PHPDBG_API void phpdbg_webdata_compress(char **msg, size_t *len) {
  28. zval array;
  29. HashTable *ht;
  30. zval zv[9] = {{{0}}};
  31. array_init(&array);
  32. ht = Z_ARRVAL(array);
  33. /* fetch superglobals */
  34. {
  35. phpdbg_is_auto_global(ZEND_STRL("GLOBALS"));
  36. /* might be JIT */
  37. phpdbg_is_auto_global(ZEND_STRL("_ENV"));
  38. phpdbg_is_auto_global(ZEND_STRL("_SERVER"));
  39. phpdbg_is_auto_global(ZEND_STRL("_REQUEST"));
  40. array_init(&zv[1]);
  41. zend_hash_copy(Z_ARRVAL(zv[1]), &EG(symbol_table), NULL);
  42. Z_ARRVAL(zv[1])->pDestructor = NULL; /* we're operating on a copy! Don't double free zvals */
  43. zend_hash_str_del(Z_ARRVAL(zv[1]), ZEND_STRL("GLOBALS")); /* do not use the reference to itself in json */
  44. zend_hash_str_add(ht, ZEND_STRL("GLOBALS"), &zv[1]);
  45. }
  46. /* save php://input */
  47. {
  48. php_stream *stream;
  49. zend_string *str;
  50. stream = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir));
  51. if ((str = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0))) {
  52. ZVAL_STR(&zv[2], str);
  53. } else {
  54. ZVAL_EMPTY_STRING(&zv[2]);
  55. }
  56. Z_SET_REFCOUNT(zv[2], 1);
  57. zend_hash_str_add(ht, ZEND_STRL("input"), &zv[2]);
  58. }
  59. /* change sapi name */
  60. {
  61. if (sapi_module.name) {
  62. ZVAL_STRING(&zv[6], sapi_module.name);
  63. } else {
  64. Z_TYPE_INFO(zv[6]) = IS_NULL;
  65. }
  66. zend_hash_str_add(ht, ZEND_STRL("sapi_name"), &zv[6]);
  67. Z_SET_REFCOUNT(zv[6], 1);
  68. }
  69. /* handle modules / extensions */
  70. {
  71. zend_module_entry *module;
  72. zend_extension *extension;
  73. zend_llist_position pos;
  74. array_init(&zv[7]);
  75. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  76. zval *value = ecalloc(sizeof(zval), 1);
  77. ZVAL_STRING(value, module->name);
  78. zend_hash_next_index_insert(Z_ARRVAL(zv[7]), value);
  79. } ZEND_HASH_FOREACH_END();
  80. zend_hash_str_add(ht, ZEND_STRL("modules"), &zv[7]);
  81. array_init(&zv[8]);
  82. extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos);
  83. while (extension) {
  84. zval *value = ecalloc(sizeof(zval), 1);
  85. ZVAL_STRING(value, extension->name);
  86. zend_hash_next_index_insert(Z_ARRVAL(zv[8]), value);
  87. extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos);
  88. }
  89. zend_hash_str_add(ht, ZEND_STRL("extensions"), &zv[8]);
  90. }
  91. /* switch cwd */
  92. if (SG(options) & SAPI_OPTION_NO_CHDIR) {
  93. char *ret = NULL;
  94. char path[MAXPATHLEN];
  95. #if HAVE_GETCWD
  96. ret = VCWD_GETCWD(path, MAXPATHLEN);
  97. #elif HAVE_GETWD
  98. ret = VCWD_GETWD(path);
  99. #endif
  100. if (ret) {
  101. ZVAL_STRING(&zv[5], path);
  102. Z_SET_REFCOUNT(zv[5], 1);
  103. zend_hash_str_add(ht, ZEND_STRL("cwd"), &zv[5]);
  104. }
  105. }
  106. /* get system ini entries */
  107. {
  108. zend_ini_entry *ini_entry;
  109. array_init(&zv[3]);
  110. ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
  111. zval *value = ecalloc(sizeof(zval), 1);
  112. if (ini_entry->modified) {
  113. if (!ini_entry->orig_value) {
  114. efree(value);
  115. continue;
  116. }
  117. ZVAL_STR(value, ini_entry->orig_value);
  118. } else {
  119. if (!ini_entry->value) {
  120. efree(value);
  121. continue;
  122. }
  123. ZVAL_STR(value, ini_entry->value);
  124. }
  125. zend_hash_add(Z_ARRVAL(zv[3]), ini_entry->name, value);
  126. } ZEND_HASH_FOREACH_END();
  127. zend_hash_str_add(ht, ZEND_STRL("systemini"), &zv[3]);
  128. }
  129. /* get perdir ini entries */
  130. if (EG(modified_ini_directives)) {
  131. zend_ini_entry *ini_entry;
  132. array_init(&zv[4]);
  133. ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
  134. zval *value = ecalloc(sizeof(zval), 1);
  135. if (!ini_entry->value) {
  136. efree(value);
  137. continue;
  138. }
  139. ZVAL_STR(value, ini_entry->value);
  140. zend_hash_add(Z_ARRVAL(zv[4]), ini_entry->name, value);
  141. } ZEND_HASH_FOREACH_END();
  142. zend_hash_str_add(ht, ZEND_STRL("userini"), &zv[4]);
  143. }
  144. /* encode data */
  145. {
  146. php_serialize_data_t var_hash;
  147. smart_str buf = {0};
  148. PHP_VAR_SERIALIZE_INIT(var_hash);
  149. php_var_serialize(&buf, &array, &var_hash);
  150. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  151. *msg = ZSTR_VAL(buf.s);
  152. *len = ZSTR_LEN(buf.s);
  153. }
  154. zend_array_destroy(Z_ARR(array));
  155. }