1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2016 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Edin Kadribasic <edink@php.net> |
16 +----------------------------------------------------------------------+
17 */
18 /* $Id$ */
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
php_embed_read_cookies(TSRMLS_D)36 static char* php_embed_read_cookies(TSRMLS_D)
37 {
38 return NULL;
39 }
40
php_embed_deactivate(TSRMLS_D)41 static int php_embed_deactivate(TSRMLS_D)
42 {
43 fflush(stdout);
44 return SUCCESS;
45 }
46
php_embed_single_write(const char * str,uint str_length)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
php_embed_ub_write(const char * str,uint str_length TSRMLS_DC)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
php_embed_flush(void * server_context)82 static void php_embed_flush(void *server_context)
83 {
84 if (fflush(stdout)==EOF) {
85 php_handle_aborted_connection();
86 }
87 }
88
php_embed_send_header(sapi_header_struct * sapi_header,void * server_context TSRMLS_DC)89 static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
90 {
91 }
92
php_embed_log_message(char * message TSRMLS_DC)93 static void php_embed_log_message(char *message TSRMLS_DC)
94 {
95 fprintf (stderr, "%s\n", message);
96 }
97
php_embed_register_variables(zval * track_vars_array TSRMLS_DC)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
php_embed_startup(sapi_module_struct * sapi_module)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", /* name */
113 "PHP Embedded Library", /* pretty name */
114
115 php_embed_startup, /* startup */
116 php_module_shutdown_wrapper, /* shutdown */
117
118 NULL, /* activate */
119 php_embed_deactivate, /* deactivate */
120
121 php_embed_ub_write, /* unbuffered write */
122 php_embed_flush, /* flush */
123 NULL, /* get uid */
124 NULL, /* getenv */
125
126 php_error, /* error handler */
127
128 NULL, /* header handler */
129 NULL, /* send headers handler */
130 php_embed_send_header, /* send header handler */
131
132 NULL, /* read POST data */
133 php_embed_read_cookies, /* read Cookies */
134
135 php_embed_register_variables, /* register server variables */
136 php_embed_log_message, /* Log message */
137 NULL, /* Get request time */
138 NULL, /* Child terminate */
139
140 STANDARD_SAPI_MODULE_PROPERTIES
141 };
142 /* }}} */
143
144 /* {{{ arginfo ext/standard/dl.c */
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
php_embed_init(int argc,char ** argv PTSRMLS_DC)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); /* ignore SIGPIPE in standalone mode so
165 that sockets created via fsockopen()
166 don't kill PHP if the remote site
167 closes it. in apache|apxs mode apache
168 does that for us! thies@thieso.net
169 20000419 */
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; /*sets default for file streams to binary */
183 setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
184 setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
185 setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be 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 /* Set some Embedded PHP defaults */
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
php_embed_shutdown(TSRMLS_D)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 * Local variables:
236 * tab-width: 4
237 * c-basic-offset: 4
238 * End:
239 * vim600: sw=4 ts=4 fdm=marker
240 * vim<600: sw=4 ts=4
241 */
242