-
Notifications
You must be signed in to change notification settings - Fork 42
/
php_embed.c.5
204 lines (180 loc) · 6.32 KB
/
php_embed.c.5
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
// Copyright 2011 Xing Xing <[email protected]> All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#include "sapi/embed/php_embed.h"
#include "zend.h"
#include <unistd.h>
/* This function doesn't return if it uses E_ERROR */
zend_class_entry *default_exception_ce;
char * exception_error(zval *exception, int severity TSRMLS_DC) {
char * result;
zend_class_entry *ce_exception = Z_OBJCE_P(exception);
if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
zval *str, *file, *line;
EG(exception) = NULL;
zend_call_method(&exception, ce_exception, NULL, "__tostring", 10, &str, 0, NULL, NULL TSRMLS_CC);
if (!EG(exception)) {
if (Z_TYPE_P(str) != IS_STRING) {
result = "Exception::__toString() must return a string";
} else {
zend_update_property_string(default_exception_ce, exception, "string", sizeof("string")-1, EG(exception) ? ce_exception->name : Z_STRVAL_P(str) TSRMLS_CC);
}
}
zval_ptr_dtor(&str);
if (EG(exception)) {
/* do the best we can to inform about the inner exception */
if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
file = zend_read_property(default_exception_ce, EG(exception), "file", sizeof("file")-1, 1 TSRMLS_CC);
line = zend_read_property(default_exception_ce, EG(exception), "line", sizeof("line")-1, 1 TSRMLS_CC);
} else {
file = NULL;
line = NULL;
}
result = "Uncaught in exception handling during call to __tostring()";
}
str = zend_read_property(default_exception_ce, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
result = "Uncaught %s\n thrown";
} else {
size_t n = snprintf(NULL, 0, "Uncaught exception '%s'", ce_exception->name);
result = malloc(n + 1);
snprintf(result, n + 1, "Uncaught exception '%s'", ce_exception->name);
}
return result;
}
/* }}} */
/* {{{ goemphp php_set_ini(ini)
*/
void php_set_ini(char *ini) {
if (php_embed_module.php_ini_path_override) {
free(php_embed_module.php_ini_path_override);
}
php_embed_module.php_ini_path_override = strdup(ini);
}
/* }}} */
/* {{{ goemphp php_startup(ini)
*/
void php_startup() {
php_embed_init(0, NULL PTSRMLS_CC);
}
/* }}} */
/* {{{ goemphp php_eval_script(script)
*/
char * php_eval_script(char *script) {
char *result = NULL;
zend_first_try {
zend_eval_string(script, NULL, "GoEmPHP" TSRMLS_CC);
// executed failure, get error message
if (PG(last_error_message)) {
result = strdup(PG(last_error_message));
free(PG(last_error_message));
PG(last_error_message) = NULL;
}
if (PG(last_error_message)) {
result = strdup(PG(last_error_message));
free(PG(last_error_message));
PG(last_error_message) = NULL;
}
if (!result && EG(exception)) {
result = exception_error(EG(exception), E_ERROR TSRMLS_CC);
EG(exception) = NULL;
}
// trigger_error & throw exception need to handle
// if (error) {
//
// } else if (exception) {
//
// }
} zend_end_try();
return result;
}
/* }}} */
/* {{{ goemphp php_exec_file(filename)
*/
char * php_exec_file(char *filename) {
char *result = NULL;
zend_first_try {
zend_file_handle file_handle = {0};
file_handle.type = ZEND_HANDLE_FILENAME;
file_handle.filename = filename;
file_handle.free_filename = 0;
file_handle.opened_path = NULL;
php_execute_script(&file_handle TSRMLS_CC );
// executed failure, get error message
if (PG(last_error_message)) {
result = strdup(PG(last_error_message));
free(PG(last_error_message));
PG(last_error_message) = NULL;
}
if (!result && EG(exception)) {
result = exception_error(EG(exception), E_ERROR TSRMLS_CC);
EG(exception) = NULL;
}
// trigger_error & throw exception need to handle
// if (error) {
//
// } else if (exception) {
//
// }
} zend_end_try();
return result;
}
/* }}} */
/* {{{ goemphp php_shutdown()
*/
void php_shutdown(void) {
php_embed_shutdown(TSRMLS_CC);
}
/* }}} */
/* {{{ goemphp php_add_var_bool(char *varname, int value)
*/
void php_add_var_bool(char *varname, int value) {
zval *newvar;
MAKE_STD_ZVAL(newvar);
ZVAL_BOOL(newvar, value);
zend_hash_update(&EG(symbol_table), varname, strlen(varname) + 1, &newvar, sizeof(zval *), NULL);
}
/* }}} */
/* {{{ goemphp php_add_var_long(char *varname, long value)
*/
void php_add_var_long(char *varname, int value) {
zval *newvar;
MAKE_STD_ZVAL(newvar);
ZVAL_LONG(newvar, value);
zend_hash_update(&EG(symbol_table), varname, strlen(varname) + 1, &newvar, sizeof(zval *), NULL);
}
/* }}} */
/* {{{ goemphp php_add_var_double(char *varname, double value)
*/
void php_add_var_double(char *varname, double value) {
zval *newvar;
MAKE_STD_ZVAL(newvar);
ZVAL_DOUBLE(newvar, value);
zend_hash_update(&EG(symbol_table), varname, strlen(varname) + 1, &newvar, sizeof(zval *), NULL);
}
/* }}} */
/* {{{ goemphp php_add_var_str(char *varname, char *value)
*/
void php_add_var_str(char *varname, char *value) {
zval *newvar;
MAKE_STD_ZVAL(newvar);
ZVAL_STRING(newvar, value, 1);
zend_hash_update(&EG(symbol_table), varname, strlen(varname) + 1, &newvar, sizeof(zval *), NULL);
}
/* }}} */
zval * php_array_init() {
zval *newvar;
MAKE_STD_ZVAL(newvar);
array_init(newvar);
return newvar;
}
void php_array_add(zval *arr, char *key, char *value) {
add_assoc_string(arr, key, value, 1);
}
void php_array_end(zval *arr, char *varname) {
zend_hash_update(&EG(symbol_table), varname, strlen(varname) + 1, &arr, sizeof(zval *), NULL);
}
void php_unset(char *varname) {
zend_hash_del(&EG(symbol_table), varname, strlen(varname) + 1);
}