This source file includes following definitions.
- ZEND_TSRMLS_CACHE_DEFINE
- php_embed_deactivate
- php_embed_single_write
- php_embed_ub_write
- php_embed_flush
- php_embed_send_header
- php_embed_log_message
- php_embed_register_variables
- php_embed_startup
- php_embed_init
- php_embed_shutdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include "php_embed.h"
21 #include "ext/standard/php_standard.h"
22
23 #ifdef PHP_WIN32
24 #include <io.h>
25 #include <fcntl.h>
26 #endif
27
28 const char HARDCODED_INI[] =
29 "html_errors=0\n"
30 "register_argc_argv=1\n"
31 "implicit_flush=1\n"
32 "output_buffering=0\n"
33 "max_execution_time=0\n"
34 "max_input_time=-1\n\0";
35
36 #if defined(PHP_WIN32) && defined(ZTS)
37 ZEND_TSRMLS_CACHE_DEFINE()
38 #endif
39
40 static char* php_embed_read_cookies(void)
41 {
42 return NULL;
43 }
44
45 static int php_embed_deactivate(void)
46 {
47 fflush(stdout);
48 return SUCCESS;
49 }
50
51 static inline size_t php_embed_single_write(const char *str, size_t str_length)
52 {
53 #ifdef PHP_WRITE_STDOUT
54 zend_long ret;
55
56 ret = write(STDOUT_FILENO, str, str_length);
57 if (ret <= 0) return 0;
58 return ret;
59 #else
60 size_t ret;
61
62 ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
63 return ret;
64 #endif
65 }
66
67
68 static size_t php_embed_ub_write(const char *str, size_t str_length)
69 {
70 const char *ptr = str;
71 size_t remaining = str_length;
72 size_t ret;
73
74 while (remaining > 0) {
75 ret = php_embed_single_write(ptr, remaining);
76 if (!ret) {
77 php_handle_aborted_connection();
78 }
79 ptr += ret;
80 remaining -= ret;
81 }
82
83 return str_length;
84 }
85
86 static void php_embed_flush(void *server_context)
87 {
88 if (fflush(stdout)==EOF) {
89 php_handle_aborted_connection();
90 }
91 }
92
93 static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context)
94 {
95 }
96
97 static void php_embed_log_message(char *message)
98 {
99 fprintf (stderr, "%s\n", message);
100 }
101
102 static void php_embed_register_variables(zval *track_vars_array)
103 {
104 php_import_environment_variables(track_vars_array);
105 }
106
107 static int php_embed_startup(sapi_module_struct *sapi_module)
108 {
109 if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
110 return FAILURE;
111 }
112 return SUCCESS;
113 }
114
115 EMBED_SAPI_API sapi_module_struct php_embed_module = {
116 "embed",
117 "PHP Embedded Library",
118
119 php_embed_startup,
120 php_module_shutdown_wrapper,
121
122 NULL,
123 php_embed_deactivate,
124
125 php_embed_ub_write,
126 php_embed_flush,
127 NULL,
128 NULL,
129
130 php_error,
131
132 NULL,
133 NULL,
134 php_embed_send_header,
135
136 NULL,
137 php_embed_read_cookies,
138
139 php_embed_register_variables,
140 php_embed_log_message,
141 NULL,
142 NULL,
143
144 STANDARD_SAPI_MODULE_PROPERTIES
145 };
146
147
148
149 ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
150 ZEND_ARG_INFO(0, extension_filename)
151 ZEND_END_ARG_INFO()
152
153
154 static const zend_function_entry additional_functions[] = {
155 ZEND_FE(dl, arginfo_dl)
156 {NULL, NULL, NULL}
157 };
158
159 EMBED_SAPI_API int php_embed_init(int argc, char **argv)
160 {
161 zend_llist global_vars;
162
163 #ifdef HAVE_SIGNAL_H
164 #if defined(SIGPIPE) && defined(SIG_IGN)
165 signal(SIGPIPE, SIG_IGN);
166
167
168
169
170
171 #endif
172 #endif
173
174 #ifdef ZTS
175 tsrm_startup(1, 1, 0, NULL);
176 (void)ts_resource(0);
177 ZEND_TSRMLS_CACHE_UPDATE();
178 #endif
179
180 #ifdef ZEND_SIGNALS
181 zend_signal_startup();
182 #endif
183
184 sapi_startup(&php_embed_module);
185
186 #ifdef PHP_WIN32
187 _fmode = _O_BINARY;
188 setmode(_fileno(stdin), O_BINARY);
189 setmode(_fileno(stdout), O_BINARY);
190 setmode(_fileno(stderr), O_BINARY);
191 #endif
192
193 php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
194 memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
195
196 php_embed_module.additional_functions = additional_functions;
197
198 if (argv) {
199 php_embed_module.executable_location = argv[0];
200 }
201
202 if (php_embed_module.startup(&php_embed_module)==FAILURE) {
203 return FAILURE;
204 }
205
206 zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
207
208
209 SG(options) |= SAPI_OPTION_NO_CHDIR;
210 SG(request_info).argc=argc;
211 SG(request_info).argv=argv;
212
213 if (php_request_startup()==FAILURE) {
214 php_module_shutdown();
215 return FAILURE;
216 }
217
218 SG(headers_sent) = 1;
219 SG(request_info).no_headers = 1;
220 php_register_variable("PHP_SELF", "-", NULL);
221
222 return SUCCESS;
223 }
224
225 EMBED_SAPI_API void php_embed_shutdown(void)
226 {
227 php_request_shutdown((void *) 0);
228 php_module_shutdown();
229 sapi_shutdown();
230 #ifdef ZTS
231 tsrm_shutdown();
232 #endif
233 if (php_embed_module.ini_entries) {
234 free(php_embed_module.ini_entries);
235 php_embed_module.ini_entries = NULL;
236 }
237 }
238
239
240
241
242
243
244
245
246