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