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    | http://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: Bob Weinand <bwoebi@php.net>                                |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "phpdbg_webdata_transfer.h"
18 #include "ext/standard/php_var.h"
19 
phpdbg_is_auto_global(char * name,int len)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_webdata_compress(char ** msg,size_t * len)28 PHPDBG_API void phpdbg_webdata_compress(char **msg, size_t *len) {
29 	zval array;
30 	HashTable *ht;
31 	zval zv[9];
32 
33 	array_init(&array);
34 	ht = Z_ARRVAL(array);
35 
36 	/* fetch superglobals */
37 	{
38 		phpdbg_is_auto_global(ZEND_STRL("GLOBALS"));
39 		/* might be JIT */
40 		phpdbg_is_auto_global(ZEND_STRL("_ENV"));
41 		phpdbg_is_auto_global(ZEND_STRL("_SERVER"));
42 		phpdbg_is_auto_global(ZEND_STRL("_REQUEST"));
43 		array_init(&zv[1]);
44 		zend_hash_copy(Z_ARRVAL(zv[1]), &EG(symbol_table), NULL);
45 		Z_ARRVAL(zv[1])->pDestructor = NULL; /* we're operating on a copy! Don't double free zvals */
46 		zend_hash_str_del(Z_ARRVAL(zv[1]), ZEND_STRL("GLOBALS")); /* do not use the reference to itself in json */
47 		zend_hash_str_add(ht, ZEND_STRL("GLOBALS"), &zv[1]);
48 	}
49 
50 	/* save php://input */
51 	{
52 		php_stream *stream;
53 		zend_string *str;
54 
55 		stream = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir));
56 		if ((str = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0))) {
57 			ZVAL_STR(&zv[2], str);
58 		} else {
59 			ZVAL_EMPTY_STRING(&zv[2]);
60 		}
61 		Z_SET_REFCOUNT(zv[2], 1);
62 		zend_hash_str_add(ht, ZEND_STRL("input"), &zv[2]);
63 	}
64 
65 	/* change sapi name */
66 	{
67 		if (sapi_module.name) {
68 			ZVAL_STRING(&zv[6], sapi_module.name);
69 		} else {
70 			Z_TYPE_INFO(zv[6]) = IS_NULL;
71 		}
72 		zend_hash_str_add(ht, ZEND_STRL("sapi_name"), &zv[6]);
73 		Z_SET_REFCOUNT(zv[6], 1);
74 	}
75 
76 	/* handle modules / extensions */
77 	{
78 		zend_module_entry *module;
79 		zend_extension *extension;
80 		zend_llist_position pos;
81 
82 		array_init(&zv[7]);
83 		ZEND_HASH_FOREACH_PTR(&module_registry, module) {
84 			zval *value = ecalloc(sizeof(zval), 1);
85 			ZVAL_STRING(value, module->name);
86 			zend_hash_next_index_insert(Z_ARRVAL(zv[7]), value);
87 		} ZEND_HASH_FOREACH_END();
88 		zend_hash_str_add(ht, ZEND_STRL("modules"), &zv[7]);
89 
90 		array_init(&zv[8]);
91 		extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos);
92 		while (extension) {
93 			zval *value = ecalloc(sizeof(zval), 1);
94 			ZVAL_STRING(value, extension->name);
95 			zend_hash_next_index_insert(Z_ARRVAL(zv[8]), value);
96 			extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos);
97 		}
98 		zend_hash_str_add(ht, ZEND_STRL("extensions"), &zv[8]);
99 	}
100 
101 	/* switch cwd */
102 	if (SG(options) & SAPI_OPTION_NO_CHDIR) {
103 		char *ret = NULL;
104 		char path[MAXPATHLEN];
105 
106 #if HAVE_GETCWD
107 		ret = VCWD_GETCWD(path, MAXPATHLEN);
108 #elif HAVE_GETWD
109 		ret = VCWD_GETWD(path);
110 #endif
111 		if (ret) {
112 			ZVAL_STRING(&zv[5], path);
113 			Z_SET_REFCOUNT(zv[5], 1);
114 			zend_hash_str_add(ht, ZEND_STRL("cwd"), &zv[5]);
115 		}
116 	}
117 
118 	/* get system ini entries */
119 	{
120 		zend_ini_entry *ini_entry;
121 
122 		array_init(&zv[3]);
123 		ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
124 			zval *value = ecalloc(sizeof(zval), 1);
125 			if (ini_entry->modified) {
126 				if (!ini_entry->orig_value) {
127 					efree(value);
128 					continue;
129 				}
130 				ZVAL_STR(value, ini_entry->orig_value);
131 			} else {
132 				if (!ini_entry->value) {
133 					efree(value);
134 					continue;
135 				}
136 				ZVAL_STR(value, ini_entry->value);
137 			}
138 			zend_hash_add(Z_ARRVAL(zv[3]), ini_entry->name, value);
139 		} ZEND_HASH_FOREACH_END();
140 		zend_hash_str_add(ht, ZEND_STRL("systemini"), &zv[3]);
141 	}
142 
143 	/* get perdir ini entries */
144 	if (EG(modified_ini_directives)) {
145 		zend_ini_entry *ini_entry;
146 
147 		array_init(&zv[4]);
148 		ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
149 			zval *value = ecalloc(sizeof(zval), 1);
150 			if (!ini_entry->value) {
151 				efree(value);
152 				continue;
153 			}
154 			ZVAL_STR(value, ini_entry->value);
155 			zend_hash_add(Z_ARRVAL(zv[4]), ini_entry->name, value);
156 		} ZEND_HASH_FOREACH_END();
157 		zend_hash_str_add(ht, ZEND_STRL("userini"), &zv[4]);
158 	}
159 
160 	/* encode data */
161 	{
162 		php_serialize_data_t var_hash;
163 		smart_str buf = {0};
164 
165 		PHP_VAR_SERIALIZE_INIT(var_hash);
166 		php_var_serialize(&buf, &array, &var_hash);
167 		PHP_VAR_SERIALIZE_DESTROY(var_hash);
168 		*msg = ZSTR_VAL(buf.s);
169 		*len = ZSTR_LEN(buf.s);
170 	}
171 
172 	zend_array_destroy(Z_ARR(array));
173 }
174