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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Pi3Web version 2.0 |
+----------------------------------------------------------------------+
| This file is committed by the Pi3 development group. |
| (pi3web.sourceforge.net) |
| |
| Author: Holger Zimmermann (zimpel@users.sourceforge.net) |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
#include "php.h"
#include "php_main.h"
#include "php_variables.h"
#include "SAPI.h"
#include "php_globals.h"
#include "ext/standard/info.h"
#include "zend_highlight.h"
#include "zend_indent.h"
#include "zend_alloc.h"
#include "ext/standard/basic_functions.h"
#include "TSRM/TSRM.h"
#include "PiAPI.h"
#include "Pi3API.h"
#include "pi3web_sapi.h"
#define PI3WEB_SERVER_VAR_BUF_SIZE 1024
int IWasLoaded=0;
static void php_info_pi3web(ZEND_MODULE_INFO_FUNC_ARGS)
{
char variable_buf[PI3WEB_SERVER_VAR_BUF_SIZE];
DWORD variable_len;
LPCONTROL_BLOCK lpCB = (LPCONTROL_BLOCK) SG(server_context);
PIDB *pDB = (PIDB *)lpCB->GetVariableNames(lpCB->ConnID);
PIDBIterator *pIter = PIDB_getIterator( pDB, PIDBTYPE_STRING, 0, 0 );
PUTS("<table border=0 cellpadding=3 cellspacing=1 width=600 align=center>\n");
PUTS("<tr><th colspan=2 bgcolor=\"" PHP_HEADER_COLOR "\">Pi3Web Server Information</th></tr>\n");
php_info_print_table_header(2, "Information Field", "Value");
php_info_print_table_row(2, "Pi3Web SAPI module version", "$Id$");
php_info_print_table_row(2, "Server Name Stamp", HTTPCore_getServerStamp());
snprintf(variable_buf, 511, "%d", HTTPCore_debugEnabled());
php_info_print_table_row(2, "Debug Enabled", variable_buf);
PIPlatform_getCurrentDirectory( variable_buf, PI3WEB_SERVER_VAR_BUF_SIZE);
php_info_print_table_row(2, "Current Path", variable_buf);
if (lpCB->GetServerVariable(lpCB->ConnID, "SERVER_NAME", variable_buf, &variable_len)
&& variable_buf[0]) {
php_info_print_table_row(2, "Main Virtual Hostname", variable_buf);
};
snprintf(variable_buf, 511, "%d", PIPlatform_getProcessId());
php_info_print_table_row(2, "Server PID", variable_buf);
php_info_print_table_row(2, "Server Platform", PIPlatform_getDescription());
PUTS("</table><br />");
PUTS("<table border=0 cellpadding=3 cellspacing=1 width=600 align=center>\n");
PUTS("<tr><th colspan=2 bgcolor=\"" PHP_HEADER_COLOR "\">HTTP Request Information</th></tr>\n");
php_info_print_table_row(2, "HTTP Request Line", lpCB->lpszReq);
PUTS("<tr><th colspan=2 bgcolor=\"" PHP_HEADER_COLOR "\">HTTP Headers</th></tr>\n");
php_info_print_table_header(2, "Server Variable", "Value");
/* --- loop over all registered server variables --- */
for(; pIter && PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter ) )
{
PCHAR pKey;
PIDBIterator_current( pIter, &pKey );
if ( !pKey ) { /* sanity */ continue; };
variable_len = PI3WEB_SERVER_VAR_BUF_SIZE;
if (lpCB->GetServerVariable(lpCB->ConnID, pKey, variable_buf, &variable_len)
&& variable_buf[0]) {
php_info_print_table_row(2, pKey, variable_buf);
} else if (PIPlatform_getLastError() == PIAPI_EINVAL) {
char *tmp_variable_buf;
tmp_variable_buf = (char *) emalloc(variable_len);
if (lpCB->GetServerVariable(lpCB->ConnID, pKey, tmp_variable_buf, &variable_len)
&& variable_buf[0]) {
php_info_print_table_row(2, pKey, tmp_variable_buf);
}
efree(tmp_variable_buf);
}
}
PUTS("</table>");
}
static zend_module_entry php_pi3web_module = {
STANDARD_MODULE_HEADER,
"PI3WEB",
NULL,
NULL,
NULL,
NULL,
NULL,
php_info_pi3web,
NULL,
STANDARD_MODULE_PROPERTIES
};
static int zend_pi3web_ub_write(const char *str, uint str_length TSRMLS_DC)
{
DWORD num_bytes = str_length;
LPCONTROL_BLOCK cb;
cb = (LPCONTROL_BLOCK) SG(server_context);
if ( !IWasLoaded ) return 0;
cb->WriteClient(cb->ConnID, (char *) str, &num_bytes, 0 );
if (num_bytes != str_length)
php_handle_aborted_connection();
return num_bytes;
}
static int sapi_pi3web_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC)
{
return SAPI_HEADER_ADD;
}
static void accumulate_header_length(sapi_header_struct *sapi_header, uint *total_length TSRMLS_DC)
{
*total_length += sapi_header->header_len+2;
}
static void concat_header(sapi_header_struct *sapi_header, char **combined_headers_ptr TSRMLS_DC)
{
memcpy(*combined_headers_ptr, sapi_header->header, sapi_header->header_len);
*combined_headers_ptr += sapi_header->header_len;
**combined_headers_ptr = '\r';
(*combined_headers_ptr)++;
**combined_headers_ptr = '\n';
(*combined_headers_ptr)++;
}
static int sapi_pi3web_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
{
uint total_length = 2; /* account for the trailing \r\n */
char *combined_headers, *combined_headers_ptr;
LPCONTROL_BLOCK lpCB = (LPCONTROL_BLOCK) SG(server_context);
sapi_header_struct default_content_type;
if ( !IWasLoaded ) return SAPI_HEADER_SENT_SUCCESSFULLY;
if (SG(sapi_headers).send_default_content_type) {
sapi_get_default_content_type_header(&default_content_type TSRMLS_CC);
accumulate_header_length(&default_content_type, (void *) &total_length TSRMLS_CC);
}
zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) accumulate_header_length, (void *) &total_length TSRMLS_CC);
/* Generate headers */
combined_headers = (char *) emalloc(total_length+1);
combined_headers_ptr = combined_headers;
if (SG(sapi_headers).send_default_content_type) {
concat_header(&default_content_type, (void *) &combined_headers_ptr TSRMLS_CC);
sapi_free_header(&default_content_type); /* we no longer need it */
}
zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t) concat_header, (void *) &combined_headers_ptr TSRMLS_CC);
*combined_headers_ptr++ = '\r';
*combined_headers_ptr++ = '\n';
*combined_headers_ptr = 0;
lpCB->dwHttpStatusCode = SG(sapi_headers).http_response_code;
lpCB->SendHeaderFunction(lpCB->ConnID, &total_length, (LPDWORD) combined_headers);
efree(combined_headers);
if (SG(sapi_headers).http_status_line) {
efree(SG(sapi_headers).http_status_line);
SG(sapi_headers).http_status_line = 0;
}
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
static int php_pi3web_startup(sapi_module_struct *sapi_module)
{
if (php_module_startup(sapi_module, &php_pi3web_module, 1)==FAILURE) {
return FAILURE;
} else {
return SUCCESS;
}
}
static int sapi_pi3web_read_post(char *buffer, uint count_bytes TSRMLS_DC)
{
LPCONTROL_BLOCK lpCB = (LPCONTROL_BLOCK) SG(server_context);
DWORD read_from_buf=0;
DWORD read_from_input=0;
DWORD total_read=0;
if ((DWORD)SG(read_post_bytes) < lpCB->cbAvailable) {
read_from_buf = MIN(lpCB->cbAvailable-SG(read_post_bytes), count_bytes);
memcpy(buffer, lpCB->lpbData+SG(read_post_bytes), read_from_buf);
total_read += read_from_buf;
}
if (read_from_buf<count_bytes
&& (SG(read_post_bytes)+read_from_buf) < lpCB->cbTotalBytes) {
DWORD cbRead=0, cbSize;
read_from_input = MIN(count_bytes-read_from_buf, lpCB->cbTotalBytes-SG(read_post_bytes)-read_from_buf);
while (cbRead < read_from_input) {
cbSize = read_from_input - cbRead;
if (!lpCB->ReadClient(lpCB->ConnID, buffer+read_from_buf+cbRead, &cbSize) || cbSize==0) {
break;
}
cbRead += cbSize;
}
total_read += cbRead;
}
/* removed after re-testing POST with Pi3Web 2.0.2 */
/* SG(read_post_bytes) += total_read; */
return total_read;
}
static char *sapi_pi3web_read_cookies(TSRMLS_D)
{
LPCONTROL_BLOCK lpCB = (LPCONTROL_BLOCK) SG(server_context);
char variable_buf[PI3WEB_SERVER_VAR_BUF_SIZE];
DWORD variable_len = PI3WEB_SERVER_VAR_BUF_SIZE;
if (lpCB->GetServerVariable(lpCB->ConnID, "HTTP_COOKIE", variable_buf, &variable_len)) {
return estrndup(variable_buf, variable_len);
} else if (PIPlatform_getLastError()==PIAPI_EINVAL) {
char *tmp_variable_buf = (char *) emalloc(variable_len+1);
if (lpCB->GetServerVariable(lpCB->ConnID, "HTTP_COOKIE", tmp_variable_buf, &variable_len)) {
tmp_variable_buf[variable_len] = 0;
return tmp_variable_buf;
} else {
efree(tmp_variable_buf);
}
}
return NULL;
}
static void init_request_info(LPCONTROL_BLOCK lpCB TSRMLS_DC)
{
SG(server_context) = lpCB;
SG(request_info).request_method = lpCB->lpszMethod;
SG(request_info).query_string = lpCB->lpszQueryString;
SG(request_info).path_translated = lpCB->lpszPathTranslated;
SG(request_info).request_uri = lpCB->lpszUri;
SG(request_info).content_type = lpCB->lpszContentType;
SG(request_info).content_length = lpCB->cbTotalBytes;
SG(request_info).auth_user = (lpCB->lpszUser) ? (char *)estrdup((const char *)(lpCB->lpszUser)) : 0;
SG(request_info).auth_password = (lpCB->lpszPassword) ? (char *)estrdup((const char *)(lpCB->lpszPassword)) : 0;
SG(sapi_headers).http_response_code = 200;
}
static void sapi_pi3web_register_variables(zval *track_vars_array TSRMLS_DC)
{
char static_variable_buf[PI3WEB_SERVER_VAR_BUF_SIZE];
char *variable_buf;
DWORD variable_len = PI3WEB_SERVER_VAR_BUF_SIZE;
LPCONTROL_BLOCK lpCB = (LPCONTROL_BLOCK) SG(server_context);
PIDB *pDB = (PIDB *)lpCB->GetVariableNames(lpCB->ConnID);
PIDBIterator *pIter = PIDB_getIterator( pDB, PIDBTYPE_STRING, 0, 0 );
/* --- loop over all registered server variables --- */
for(; pIter && PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter ) )
{
PCHAR pKey;
PIDBIterator_current( pIter, &pKey );
if ( !pKey ) { /* sanity */ continue; };
variable_len = PI3WEB_SERVER_VAR_BUF_SIZE;
if (lpCB->GetServerVariable(lpCB->ConnID, pKey, static_variable_buf, &variable_len)
&& (variable_len > 1)) {
php_register_variable(pKey, static_variable_buf, track_vars_array TSRMLS_CC);
} else if (PIPlatform_getLastError()==PIAPI_EINVAL) {
variable_buf = (char *) emalloc(variable_len);
if (lpCB->GetServerVariable(lpCB->ConnID, pKey, variable_buf, &variable_len)) {
php_register_variable(pKey, variable_buf, track_vars_array TSRMLS_CC);
}
efree(variable_buf);
}
}
/* PHP_SELF support */
variable_len = PI3WEB_SERVER_VAR_BUF_SIZE;
if (lpCB->GetServerVariable(lpCB->ConnID, "SCRIPT_NAME", static_variable_buf, &variable_len)
&& (variable_len > 1)) {
php_register_variable("PHP_SELF", static_variable_buf, track_vars_array TSRMLS_CC);
}
}
static sapi_module_struct pi3web_sapi_module = {
"pi3web", /* name */
"PI3WEB", /* pretty name */
php_pi3web_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
NULL, /* deactivate */
zend_pi3web_ub_write, /* unbuffered write */
NULL, /* flush */
NULL, /* get uid */
NULL, /* getenv */
php_error, /* error handler */
sapi_pi3web_header_handler, /* header handler */
sapi_pi3web_send_headers, /* send headers handler */
NULL, /* send header handler */
sapi_pi3web_read_post, /* read POST data */
sapi_pi3web_read_cookies, /* read Cookies */
sapi_pi3web_register_variables, /* register server variables */
NULL, /* Log message */
NULL, /* Get request time */
STANDARD_SAPI_MODULE_PROPERTIES
};
MODULE_API DWORD PHP5_wrapper(LPCONTROL_BLOCK lpCB)
{
zend_file_handle file_handle = {0};
int iRet = PIAPI_COMPLETED;
TSRMLS_FETCH();
zend_first_try {
file_handle.filename = lpCB->lpszFileName;
file_handle.free_filename = 0;
file_handle.type = ZEND_HANDLE_FILENAME;
file_handle.opened_path = NULL;
init_request_info(lpCB TSRMLS_CC);
php_request_startup(TSRMLS_C);
switch ( lpCB->dwBehavior ) {
case PHP_MODE_STANDARD:
iRet = ( php_execute_script( &file_handle TSRMLS_CC ) ) ?
PIAPI_COMPLETED : PIAPI_ERROR;
break;
case PHP_MODE_HIGHLIGHT: {
zend_syntax_highlighter_ini syntax_highlighter_ini;
if ( open_file_for_scanning( &file_handle TSRMLS_CC ) == SUCCESS )
{
php_get_highlight_struct( &syntax_highlighter_ini );
zend_highlight( &syntax_highlighter_ini TSRMLS_CC );
}
else
{
iRet = PIAPI_ERROR;
};
};
break;
case PHP_MODE_INDENT: {
sapi_header_line ctr = {0};
ctr.line = "Content-Type: text/plain";
ctr.line_len = strlen(ctr.line);
sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
}
if ( open_file_for_scanning( &file_handle TSRMLS_CC ) == SUCCESS )
{
zend_indent();
}
else
{
iRet = PIAPI_ERROR;
};
break;
case PHP_MODE_LINT:
iRet = (php_lint_script(&file_handle TSRMLS_CC) == SUCCESS) ?
PIAPI_COMPLETED : PIAPI_ERROR;
break;
default:
iRet = PIAPI_ERROR;;
}
if (SG(request_info).cookie_data) {
efree(SG(request_info).cookie_data);
};
php_request_shutdown(NULL);
} zend_catch {
iRet = PIAPI_ERROR;
} zend_end_try();
return iRet;
}
MODULE_API BOOL PHP5_startup() {
tsrm_startup(1, 1, 0, NULL);
sapi_startup(&pi3web_sapi_module);
if (pi3web_sapi_module.startup) {
pi3web_sapi_module.startup(&pi3web_sapi_module);
};
IWasLoaded = 1;
return IWasLoaded;
};
MODULE_API BOOL PHP5_shutdown() {
if (pi3web_sapi_module.shutdown) {
pi3web_sapi_module.shutdown(&pi3web_sapi_module);
};
sapi_shutdown();
tsrm_shutdown();
IWasLoaded = 0;
return !IWasLoaded;
};
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|