forked from junamai2000/mysql_unserialize_php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pfc.cc
206 lines (160 loc) · 4.59 KB
/
pfc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "pfc.h"
static void pfc_sapi_error(int type, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
void
pfc_set_string(const char *name, const char *value,
size_t name_len, size_t value_len)
{
if (name_len == 0) {
name_len = strlen(name) + 1;
}
if (value_len == 0) {
value_len = strlen(value);
}
zval *var;
MAKE_STD_ZVAL(var);
ZVAL_STRINGL(var, (char *)value, value_len, 1);
/* Optimized ZEND_SET_GLOBAL_VAR(name, var), removed name lookup */
(void)zend_hash_update(&EG(symbol_table), (char *)name, name_len,
&var, sizeof(zval *), NULL);
}
bool
pfc_get_string(const char *name, std::string *value, size_t name_len)
{
if (name_len == 0) {
name_len = strlen(name) + 1;
}
zval **data = NULL;
if (zend_hash_find(&EG(symbol_table), (char *)name, name_len,
(void **)&data) == FAILURE) {
pfc_sapi_error(0, "%s(): Name not found in $GLOBALS", __func__);
return false;
}
if (data == NULL) {
pfc_sapi_error(0, "%s(): Value is NULL", __func__);
return false;
}
convert_to_string(*data);
value->assign(Z_STRVAL(**data), Z_STRLEN(**data));
return true;
}
bool
pfc_eval(const char *source, const char *code)
{
bool result = false;
zend_first_try {
result = zend_eval_string((char *)code, NULL, (char *)source) == SUCCESS;
} zend_catch {
} zend_end_try();
return result;
}
bool
pfc_evalf(const char *source, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char *data = NULL;
vspprintf(&data, 0, fmt, ap);
bool result = pfc_eval(source, data);
if (data != NULL) {
efree(data);
}
return result;
}
static int
pfc_startup(sapi_module_struct *mod)
{
return php_module_startup(mod, NULL, 0);
}
/* echo */
static int
pfc_ub_write(const char *str, uint len TSRMLS_DC)
{
(void)fputs(str, stdout);
return 0;
}
static void
pfc_log_message(char *str)
{
(void)fprintf(stderr, "%s\n", str);
}
static void
pfc_sapi_error(int type, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
(void)fputs("\n", stderr);
va_end(ap);
}
#define pfc_shutdown php_module_shutdown_wrapper
sapi_module_struct g_pfc_module = {
"foobar", /* name */
"FOOBAR-PHP Interface", /* pretty name */
pfc_startup, /* startup */
pfc_shutdown, /* shutdown */
NULL, /* activate */
NULL, /* deactivate */
pfc_ub_write, /* unbuffered write */
NULL, /* flush */
NULL, /* get uid */
NULL, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
NULL, /* send headers handler */
NULL, /* send header handler */
NULL, /* read POST data */
NULL, /* read Cookies */
NULL, /* register server variables */
pfc_log_message, /* Log message */
STANDARD_SAPI_MODULE_PROPERTIES
};
bool
pfc_initialize()
{
g_pfc_module.sapi_error = pfc_sapi_error;
zend_llist global_vars;
#ifdef ZTS /* zend threat safety */
zend_compiler_globals *compiler_globals;
zend_executor_globals *executor_globals;
php_core_globals *core_globals;
sapi_globals_struct *sapi_globals;
void ***tsrm_ls;
tsrm_startup(1, 1, 0, NULL);
compiler_globals = ts_resource(compiler_globals_id);
executor_globals = ts_resource(executor_globals_id);
core_globals = ts_resource(core_globals_id);
sapi_globals = ts_resource(sapi_globals_id);
tsrm_ls = ts_resource(0);
*ptsrm_ls = tsrm_ls;
#endif
signal(SIGPIPE, SIG_IGN);
sapi_startup(&g_pfc_module);
if (g_pfc_module.startup(&g_pfc_module) == FAILURE) {
return false;
}
g_pfc_module.executable_location = (char *)__func__;
zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
/* Set some Embedded PHP defaults */
SG(options) |= SAPI_OPTION_NO_CHDIR;
if (php_request_startup(TSRMLS_C) == FAILURE) {
php_module_shutdown(TSRMLS_C);
return false;
}
SG(headers_sent) = 1;
SG(request_info).no_headers = 1;
php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
return true;
}
void
pfc_finalize()
{
php_request_shutdown((void *) 0);
php_module_shutdown(TSRMLS_C);
sapi_shutdown();
#ifdef ZTS
tsrm_shutdown();
#endif
}
/*
* $Log$
*/