1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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 at 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: George Wang <gwang@litespeedtech.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "php.h"
20 #include "SAPI.h"
21 #include "php_main.h"
22 #include "php_ini.h"
23 #include "php_variables.h"
24 #include "zend_highlight.h"
25 #include "zend.h"
26 #include "ext/standard/basic_functions.h"
27 #include "ext/standard/info.h"
28 #include "lsapilib.h"
29
30 #include <stdio.h>
31
32 #if HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #ifdef PHP_WIN32
41
42 #include <io.h>
43 #include <fcntl.h>
44 #include "win32/php_registry.h"
45
46 #else
47
48 #include <sys/wait.h>
49
50 #endif
51
52 #include <sys/stat.h>
53
54 #if HAVE_SYS_TYPES_H
55
56 #include <sys/types.h>
57
58 #endif
59
60 #if HAVE_SIGNAL_H
61
62 #include <signal.h>
63
64 #endif
65
66 #include <sys/socket.h>
67 #include <arpa/inet.h>
68 #include <netinet/in.h>
69
70 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
71 #include "lscriu.c"
72 #endif
73
74 #define SAPI_LSAPI_MAX_HEADER_LENGTH 2048
75
76 /* Key for each cache entry is dirname(PATH_TRANSLATED).
77 *
78 * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
79 * the path starting from doc_root throught to dirname(PATH_TRANSLATED). There is no point
80 * storing per-file entries as it would not be possible to detect added / deleted entries
81 * between separate files.
82 */
83 typedef struct _user_config_cache_entry {
84 time_t expires;
85 HashTable user_config;
86 } user_config_cache_entry;
87 static HashTable user_config_cache;
88
89 static int lsapi_mode = 0;
90 static char *php_self = "";
91 static char *script_filename = "";
92 static int source_highlight = 0;
93 static int ignore_php_ini = 0;
94 static char * argv0 = NULL;
95 static int engine = 1;
96 static int parse_user_ini = 0;
97
98 #ifdef ZTS
99 zend_compiler_globals *compiler_globals;
100 zend_executor_globals *executor_globals;
101 php_core_globals *core_globals;
102 sapi_globals_struct *sapi_globals;
103 void ***tsrm_ls;
104 #endif
105
106 zend_module_entry litespeed_module_entry;
107
init_sapi_from_env(sapi_module_struct * sapi_module)108 static void init_sapi_from_env(sapi_module_struct *sapi_module)
109 {
110 char *p;
111 p = getenv("LSPHPRC");
112 if (p)
113 sapi_module->php_ini_path_override = p;
114 }
115
116 /* {{{ php_lsapi_startup
117 */
php_lsapi_startup(sapi_module_struct * sapi_module)118 static int php_lsapi_startup(sapi_module_struct *sapi_module)
119 {
120 if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
121 return FAILURE;
122 }
123 argv0 = sapi_module->executable_location;
124 return SUCCESS;
125 }
126 /* }}} */
127
128 /* {{{ sapi_lsapi_ini_defaults */
129
130 /* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */
131 #define INI_DEFAULT(name,value)\
132 ZVAL_STRING(tmp, value, 0);\
133 zend_hash_update(configuration_hash, name, sizeof(name), tmp, sizeof(zval), (void**)&entry);\
134 Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry))
135
sapi_lsapi_ini_defaults(HashTable * configuration_hash)136 static void sapi_lsapi_ini_defaults(HashTable *configuration_hash)
137 {
138 #if PHP_MAJOR_VERSION > 4
139 /*
140 zval *tmp, *entry;
141
142 MAKE_STD_ZVAL(tmp);
143
144 INI_DEFAULT("register_long_arrays", "0");
145
146 FREE_ZVAL(tmp);
147 */
148 #endif
149
150 }
151 /* }}} */
152
153
154 /* {{{ sapi_lsapi_ub_write
155 */
sapi_lsapi_ub_write(const char * str,size_t str_length)156 static size_t sapi_lsapi_ub_write(const char *str, size_t str_length)
157 {
158 int ret;
159 int remain;
160 if ( lsapi_mode ) {
161 ret = LSAPI_Write( str, str_length );
162 if ( ret < str_length ) {
163 php_handle_aborted_connection();
164 return str_length - ret;
165 }
166 } else {
167 remain = str_length;
168 while( remain > 0 ) {
169 ret = write( 1, str, remain );
170 if ( ret <= 0 ) {
171 php_handle_aborted_connection();
172 return str_length - remain;
173 }
174 str += ret;
175 remain -= ret;
176 }
177 }
178 return str_length;
179 }
180 /* }}} */
181
182
183 /* {{{ sapi_lsapi_flush
184 */
sapi_lsapi_flush(void * server_context)185 static void sapi_lsapi_flush(void * server_context)
186 {
187 if ( lsapi_mode ) {
188 if ( LSAPI_Flush() == -1) {
189 php_handle_aborted_connection();
190 }
191 }
192 }
193 /* }}} */
194
195
196 /* {{{ sapi_lsapi_deactivate
197 */
sapi_lsapi_deactivate(void)198 static int sapi_lsapi_deactivate(void)
199 {
200 if ( SG(request_info).path_translated ) {
201 efree( SG(request_info).path_translated );
202 SG(request_info).path_translated = NULL;
203 }
204
205 return SUCCESS;
206 }
207 /* }}} */
208
209
210
211
212 /* {{{ sapi_lsapi_getenv
213 */
sapi_lsapi_getenv(char * name,size_t name_len)214 static char *sapi_lsapi_getenv( char * name, size_t name_len )
215 {
216 if ( lsapi_mode ) {
217 return LSAPI_GetEnv( name );
218 } else {
219 return getenv( name );
220 }
221 }
222 /* }}} */
223
224
add_variable(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)225 static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
226 void * arg )
227 {
228 int filter_arg = (Z_ARR_P((zval *)arg) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))
229 ? PARSE_ENV : PARSE_SERVER;
230 char * new_val = (char *) pValue;
231 size_t new_val_len;
232
233 if (sapi_module.input_filter(filter_arg, (char *)pKey, &new_val, valLen, &new_val_len)) {
234 php_register_variable_safe((char *)pKey, new_val, new_val_len, (zval *)arg );
235 }
236 return 1;
237 }
238
litespeed_php_import_environment_variables(zval * array_ptr)239 static void litespeed_php_import_environment_variables(zval *array_ptr)
240 {
241 char buf[128];
242 char **env, *p, *t = buf;
243 size_t alloc_size = sizeof(buf);
244 unsigned long nlen; /* ptrdiff_t is not portable */
245
246 if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
247 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
248 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
249 ) {
250 zval_dtor(array_ptr);
251 ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
252 return;
253 } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
254 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
255 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
256 ) {
257 zval_dtor(array_ptr);
258 ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
259 return;
260 }
261
262 for (env = environ; env != NULL && *env != NULL; env++) {
263 p = strchr(*env, '=');
264 if (!p) { /* malformed entry? */
265 continue;
266 }
267 nlen = p - *env;
268 if (nlen >= alloc_size) {
269 alloc_size = nlen + 64;
270 t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
271 }
272 memcpy(t, *env, nlen);
273 t[nlen] = '\0';
274 add_variable(t, nlen, p + 1, strlen( p + 1 ), array_ptr);
275 }
276 if (t != buf && t != NULL) {
277 efree(t);
278 }
279 }
280
281 /* {{{ sapi_lsapi_register_variables
282 */
sapi_lsapi_register_variables(zval * track_vars_array)283 static void sapi_lsapi_register_variables(zval *track_vars_array)
284 {
285 char * php_self = "";
286 if ( lsapi_mode ) {
287 if ( (SG(request_info).request_uri ) )
288 php_self = (SG(request_info).request_uri );
289
290 litespeed_php_import_environment_variables(track_vars_array);
291
292 LSAPI_ForeachHeader( add_variable, track_vars_array );
293 LSAPI_ForeachEnv( add_variable, track_vars_array );
294 add_variable("PHP_SELF", 8, php_self, strlen( php_self ), track_vars_array );
295 } else {
296 php_import_environment_variables(track_vars_array);
297
298 php_register_variable("PHP_SELF", php_self, track_vars_array);
299 php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
300 php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
301 php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
302 php_register_variable("DOCUMENT_ROOT", "", track_vars_array);
303
304 }
305 }
306 /* }}} */
307
308
309 /* {{{ sapi_lsapi_read_post
310 */
sapi_lsapi_read_post(char * buffer,size_t count_bytes)311 static size_t sapi_lsapi_read_post(char *buffer, size_t count_bytes)
312 {
313 if ( lsapi_mode ) {
314 return LSAPI_ReadReqBody( buffer, (unsigned long long)count_bytes );
315 } else {
316 return 0;
317 }
318 }
319 /* }}} */
320
321
322
323
324 /* {{{ sapi_lsapi_read_cookies
325 */
sapi_lsapi_read_cookies(void)326 static char *sapi_lsapi_read_cookies(void)
327 {
328 if ( lsapi_mode ) {
329 return LSAPI_GetHeader( H_COOKIE );
330 } else {
331 return NULL;
332 }
333 }
334 /* }}} */
335
336
337 /* {{{ sapi_lsapi_send_headers
338 */
sapi_lsapi_send_headers(sapi_headers_struct * sapi_headers)339 static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers)
340 {
341 sapi_header_struct *h;
342 zend_llist_position pos;
343 if ( lsapi_mode ) {
344 LSAPI_SetRespStatus( SG(sapi_headers).http_response_code );
345
346 h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
347 while (h) {
348 if ( h->header_len > 0 ) {
349 LSAPI_AppendRespHeader(h->header, h->header_len);
350 }
351 h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
352 }
353 if (SG(sapi_headers).send_default_content_type) {
354 char *hd;
355 int len;
356 char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
357
358 hd = sapi_get_default_content_type();
359 len = snprintf( headerBuf, SAPI_LSAPI_MAX_HEADER_LENGTH - 1,
360 "Content-type: %s", hd );
361 efree(hd);
362
363 LSAPI_AppendRespHeader( headerBuf, len );
364 }
365 }
366 LSAPI_FinalizeRespHeaders();
367 return SAPI_HEADER_SENT_SUCCESSFULLY;
368
369
370 }
371 /* }}} */
372
373
374 /* {{{ sapi_lsapi_send_headers
375 */
sapi_lsapi_log_message(char * message,int syslog_type_int)376 static void sapi_lsapi_log_message(char *message, int syslog_type_int)
377 {
378 char buf[8192];
379 int len = strlen( message );
380 if ( *(message + len - 1 ) != '\n' )
381 {
382 snprintf( buf, 8191, "%s\n", message );
383 message = buf;
384 ++len;
385 }
386 LSAPI_Write_Stderr( message, len);
387 }
388 /* }}} */
389
390 /* Set to 1 to turn on log messages useful during development:
391 */
392 #if 0
393 static void log_message (const char *fmt, ...)
394 {
395 va_list ap;
396 va_start(ap, fmt);
397 char buf[0x100];
398 vsnprintf(buf, sizeof(buf), fmt, ap);
399 va_end(ap);
400 sapi_lsapi_log_message(buf
401 #if PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 1)
402 , 0
403 #endif
404 );
405 }
406 #define DEBUG_MESSAGE(fmt, ...) log_message("LS:%d " fmt "\n", __LINE__, ##__VA_ARGS__)
407 #else
408 #define DEBUG_MESSAGE(fmt, ...)
409 #endif
410
411 static int lsapi_activate_user_ini();
412
sapi_lsapi_activate()413 static int sapi_lsapi_activate()
414 {
415 char *path, *doc_root, *server_name;
416 size_t path_len, doc_root_len, server_name_len;
417
418 /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
419 if (!SG(request_info).path_translated) {
420 return FAILURE;
421 }
422
423 if (php_ini_has_per_host_config()) {
424 server_name = sapi_lsapi_getenv("SERVER_NAME", 0);
425 /* SERVER_NAME should also be defined at this stage..but better check it anyway */
426 if (server_name) {
427 server_name_len = strlen(server_name);
428 server_name = estrndup(server_name, server_name_len);
429 zend_str_tolower(server_name, server_name_len);
430 php_ini_activate_per_host_config(server_name, server_name_len);
431 efree(server_name);
432 }
433 }
434
435 if (php_ini_has_per_dir_config()) {
436 /* Prepare search path */
437 path_len = strlen(SG(request_info).path_translated);
438
439 /* Make sure we have trailing slash! */
440 if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
441 path = emalloc(path_len + 2);
442 memcpy(path, SG(request_info).path_translated, path_len + 1);
443 path_len = zend_dirname(path, path_len);
444 path[path_len++] = DEFAULT_SLASH;
445 } else {
446 path = estrndup(SG(request_info).path_translated, path_len);
447 path_len = zend_dirname(path, path_len);
448 }
449 path[path_len] = 0;
450
451 /* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
452 php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
453
454 efree(path);
455 }
456
457 if (parse_user_ini && lsapi_activate_user_ini() == FAILURE) {
458 return FAILURE;
459 }
460 return SUCCESS;
461 }
462 /* {{{ sapi_module_struct cgi_sapi_module
463 */
464 static sapi_module_struct lsapi_sapi_module =
465 {
466 "litespeed",
467 "LiteSpeed V7.1",
468
469 php_lsapi_startup, /* startup */
470 php_module_shutdown_wrapper, /* shutdown */
471
472 sapi_lsapi_activate, /* activate */
473 sapi_lsapi_deactivate, /* deactivate */
474
475 sapi_lsapi_ub_write, /* unbuffered write */
476 sapi_lsapi_flush, /* flush */
477 NULL, /* get uid */
478 sapi_lsapi_getenv, /* getenv */
479
480 php_error, /* error handler */
481
482 NULL, /* header handler */
483 sapi_lsapi_send_headers, /* send headers handler */
484 NULL, /* send header handler */
485
486 sapi_lsapi_read_post, /* read POST data */
487 sapi_lsapi_read_cookies, /* read Cookies */
488
489 sapi_lsapi_register_variables, /* register server variables */
490 sapi_lsapi_log_message, /* Log message */
491 NULL, /* Get request time */
492 NULL, /* Child terminate */
493
494 STANDARD_SAPI_MODULE_PROPERTIES
495
496 };
497 /* }}} */
498
init_request_info(void)499 static void init_request_info( void )
500 {
501 char * pContentType = LSAPI_GetHeader( H_CONTENT_TYPE );
502 char * pAuth;
503
504 SG(request_info).content_type = pContentType ? pContentType : "";
505 SG(request_info).request_method = LSAPI_GetRequestMethod();
506 SG(request_info).query_string = LSAPI_GetQueryString();
507 SG(request_info).request_uri = LSAPI_GetScriptName();
508 SG(request_info).content_length = LSAPI_GetReqBodyLen();
509 SG(request_info).path_translated = estrdup( LSAPI_GetScriptFileName());
510
511 /* It is not reset by zend engine, set it to 200. */
512 SG(sapi_headers).http_response_code = 200;
513
514 pAuth = LSAPI_GetHeader( H_AUTHORIZATION );
515 php_handle_auth_data(pAuth);
516 }
517
lsapi_execute_script(zend_file_handle * file_handle)518 static int lsapi_execute_script( zend_file_handle * file_handle)
519 {
520 char *p;
521 int len;
522 file_handle->type = ZEND_HANDLE_FILENAME;
523 file_handle->handle.fd = 0;
524 file_handle->filename = SG(request_info).path_translated;
525 file_handle->free_filename = 0;
526 file_handle->opened_path = NULL;
527
528 p = argv0;
529 *p++ = ':';
530 len = strlen( SG(request_info).path_translated );
531 if ( len > 45 )
532 len = len - 45;
533 else
534 len = 0;
535 memccpy( p, SG(request_info).path_translated + len, 0, 46 );
536
537 php_execute_script(file_handle);
538 return 0;
539
540 }
541
lsapi_module_main(int show_source)542 static int lsapi_module_main(int show_source)
543 {
544 zend_file_handle file_handle;
545 memset(&file_handle, 0, sizeof(file_handle));
546 if (php_request_startup() == FAILURE ) {
547 return -1;
548 }
549
550 if (show_source) {
551 zend_syntax_highlighter_ini syntax_highlighter_ini;
552
553 php_get_highlight_struct(&syntax_highlighter_ini);
554 highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
555 } else {
556 lsapi_execute_script( &file_handle);
557 }
558 zend_try {
559 php_request_shutdown(NULL);
560 memset( argv0, 0, 46 );
561 } zend_end_try();
562 return 0;
563 }
564
565
alter_ini(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)566 static int alter_ini( const char * pKey, int keyLen, const char * pValue, int valLen,
567 void * arg )
568 {
569 zend_string * psKey;
570
571 int type = ZEND_INI_PERDIR;
572 int stage = PHP_INI_STAGE_RUNTIME;
573 if ( '\001' == *pKey ) {
574 ++pKey;
575 if ( *pKey == 4 ) {
576 type = ZEND_INI_SYSTEM;
577 stage = PHP_INI_STAGE_ACTIVATE;
578 }
579 else
580 {
581 stage = PHP_INI_STAGE_HTACCESS;
582 }
583 ++pKey;
584 --keyLen;
585 if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 ))
586 {
587 if ( *pValue == '0' )
588 engine = 0;
589 }
590 else
591 {
592 --keyLen;
593 psKey = zend_string_init(pKey, keyLen, 1);
594 zend_alter_ini_entry_chars(psKey,
595 (char *)pValue, valLen,
596 type, stage);
597 zend_string_release(psKey);
598 }
599 }
600 return 1;
601 }
602
user_config_cache_entry_dtor(zval * el)603 static void user_config_cache_entry_dtor(zval *el)
604 {
605 user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
606 zend_hash_destroy(&entry->user_config);
607 free(entry);
608 }
609
user_config_cache_init()610 static void user_config_cache_init()
611 {
612 zend_hash_init(&user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
613 }
614
pathlen_without_trailing_slash(char * path)615 static int pathlen_without_trailing_slash(char *path)
616 {
617 int len = (int)strlen(path);
618 while (len > 1 && /* mind "/" as root dir */
619 path[len-1] == DEFAULT_SLASH)
620 {
621 --len;
622 }
623 return len;
624 }
625
skip_slash(char * s)626 static inline char* skip_slash(char *s)
627 {
628 while (*s == DEFAULT_SLASH) {
629 ++s;
630 }
631 return s;
632 }
633
634 /**
635 * Walk down the path_stop starting at path_start.
636 *
637 * If path_start = "/path1" and path_stop = "/path1/path2/path3"
638 * the callback will be called 3 times with the next args:
639 *
640 * 1. "/path1/path2/path3"
641 * ^ end
642 * ^ start
643 * 2. "/path1/path2/path3"
644 * ^ end
645 * ^ start
646 * 3. "/path1/path2/path3"
647 * ^ end
648 * ^ start
649 *
650 * path_stop has to be a subdir of path_start
651 * or to be path_start itself.
652 *
653 * Both path args have to be absolute.
654 * Trailing slashes are allowed.
655 * NULL or empty string args are not allowed.
656 */
walk_down_the_path(char * path_start,char * path_stop,void (* cb)(char * begin,char * end,void * data),void * data)657 static void walk_down_the_path(char* path_start,
658 char* path_stop,
659 void (*cb)(char* begin,
660 char* end,
661 void* data),
662 void* data)
663 {
664 char *pos = path_stop + pathlen_without_trailing_slash(path_start);
665 cb(path_stop, pos, data);
666
667 while ((pos = skip_slash(pos))[0]) {
668 pos = strchr(pos, DEFAULT_SLASH);
669 if (!pos) {
670 /* The last token without trailing slash
671 */
672 cb(path_stop, path_stop + strlen(path_stop), data);
673 return;
674 }
675 cb(path_stop, pos, data);
676 }
677 }
678
679
680 typedef struct {
681 char *path;
682 uint path_len;
683 char *doc_root;
684 user_config_cache_entry *entry;
685 } _lsapi_activate_user_ini_ctx;
686
687 typedef int (*fn_activate_user_ini_chain_t)
688 (_lsapi_activate_user_ini_ctx *ctx, void* next);
689
lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx * ctx,void * next)690 static int lsapi_activate_user_ini_basic_checks(_lsapi_activate_user_ini_ctx *ctx,
691 void* next)
692 {
693 int rc = SUCCESS;
694 fn_activate_user_ini_chain_t *fn_next = next;
695
696 if (!PG(user_ini_filename) || !*PG(user_ini_filename)) {
697 return SUCCESS;
698 }
699
700 /* PATH_TRANSLATED should be defined at this stage */
701 ctx->path = SG(request_info).path_translated;
702 if (!ctx->path || !*ctx->path) {
703 return FAILURE;
704 }
705
706 ctx->doc_root = sapi_lsapi_getenv("DOCUMENT_ROOT", 0);
707 DEBUG_MESSAGE("doc_root: %s", ctx->doc_root);
708
709 if (*fn_next) {
710 rc = (*fn_next)(ctx, fn_next + 1);
711 }
712
713 return rc;
714 }
715
lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx * ctx,void * next)716 static int lsapi_activate_user_ini_mk_path(_lsapi_activate_user_ini_ctx *ctx,
717 void* next)
718 {
719 char *path;
720 int rc = SUCCESS;
721 fn_activate_user_ini_chain_t *fn_next = next;
722
723 /* Extract dir name from path_translated * and store it in 'path' */
724 ctx->path_len = strlen(ctx->path);
725 path = ctx->path = estrndup(SG(request_info).path_translated, ctx->path_len);
726 if (!path)
727 return FAILURE;
728 ctx->path_len = zend_dirname(path, ctx->path_len);
729
730 if (*fn_next) {
731 rc = (*fn_next)(ctx, fn_next + 1);
732 }
733
734 efree(path);
735 return rc;
736 }
737
lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx * ctx,void * next)738 static int lsapi_activate_user_ini_mk_realpath(_lsapi_activate_user_ini_ctx *ctx,
739 void* next)
740 {
741 char *real_path;
742 int rc = SUCCESS;
743 fn_activate_user_ini_chain_t *fn_next = next;
744
745 if (!IS_ABSOLUTE_PATH(ctx->path, ctx->path_len)) {
746 real_path = tsrm_realpath(ctx->path, NULL);
747 if (!real_path) {
748 return SUCCESS;
749 }
750 ctx->path = real_path;
751 ctx->path_len = strlen(ctx->path);
752 } else {
753 real_path = NULL;
754 }
755
756 if (*fn_next) {
757 rc = (*fn_next)(ctx, fn_next + 1);
758 }
759
760 if (real_path)
761 efree(real_path);
762 return rc;
763 }
764
lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx * ctx,void * next)765 static int lsapi_activate_user_ini_mk_user_config(_lsapi_activate_user_ini_ctx *ctx,
766 void* next)
767 {
768 fn_activate_user_ini_chain_t *fn_next = next;
769
770 /* Find cached config entry: If not found, create one */
771 ctx->entry = zend_hash_str_find_ptr(&user_config_cache, ctx->path, ctx->path_len);
772
773 if (!ctx->entry)
774 {
775 ctx->entry = pemalloc(sizeof(user_config_cache_entry), 1);
776 ctx->entry->expires = 0;
777 zend_hash_init(&ctx->entry->user_config, 0, NULL,
778 config_zval_dtor, 1);
779 zend_hash_str_update_ptr(&user_config_cache, ctx->path,
780 ctx->path_len, ctx->entry);
781 }
782
783 if (*fn_next) {
784 return (*fn_next)(ctx, fn_next + 1);
785 } else {
786 return SUCCESS;
787 }
788 }
789
walk_down_the_path_callback(char * begin,char * end,void * data)790 static void walk_down_the_path_callback(char* begin,
791 char* end,
792 void* data)
793 {
794 _lsapi_activate_user_ini_ctx *ctx = data;
795 char tmp = end[0];
796 end[0] = 0;
797 php_parse_user_ini_file(begin, PG(user_ini_filename), &ctx->entry->user_config);
798 end[0] = tmp;
799 }
800
lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx * ctx,void * next)801 static int lsapi_activate_user_ini_walk_down_the_path(_lsapi_activate_user_ini_ctx *ctx,
802 void* next)
803 {
804 time_t request_time = sapi_get_request_time();
805 uint docroot_len;
806 int rc = SUCCESS;
807 fn_activate_user_ini_chain_t *fn_next = next;
808
809 if (!ctx->entry->expires || request_time > ctx->entry->expires)
810 {
811 docroot_len = ctx->doc_root && ctx->doc_root[0]
812 ? pathlen_without_trailing_slash(ctx->doc_root)
813 : 0;
814
815 int is_outside_of_docroot = !docroot_len ||
816 ctx->path_len < docroot_len ||
817 strncmp(ctx->path, ctx->doc_root, docroot_len) != 0;
818
819 if (is_outside_of_docroot) {
820 php_parse_user_ini_file(ctx->path, PG(user_ini_filename),
821 &ctx->entry->user_config);
822 } else {
823 walk_down_the_path(ctx->doc_root, ctx->path,
824 &walk_down_the_path_callback, ctx);
825 }
826
827 ctx->entry->expires = request_time + PG(user_ini_cache_ttl);
828 }
829
830 if (*fn_next) {
831 rc = (*fn_next)(ctx, fn_next + 1);
832 }
833
834 return rc;
835 }
836
lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx * ctx,void * next)837 static int lsapi_activate_user_ini_finally(_lsapi_activate_user_ini_ctx *ctx,
838 void* next)
839 {
840 int rc = SUCCESS;
841 fn_activate_user_ini_chain_t *fn_next = next;
842
843 php_ini_activate_config(&ctx->entry->user_config, PHP_INI_PERDIR,
844 PHP_INI_STAGE_HTACCESS);
845
846 if (*fn_next) {
847 rc = (*fn_next)(ctx, fn_next + 1);
848 }
849
850 return rc;
851 }
852
lsapi_activate_user_ini(void)853 static int lsapi_activate_user_ini( void )
854 {
855 _lsapi_activate_user_ini_ctx ctx;
856 /**
857 * The reason to have this function list stacked
858 * is each function now can define a scoped destructor.
859 *
860 * Passing control via function pointer is a sign of low coupling,
861 * which means dependencies between these functions are to be
862 * controlled from a single place
863 * (here below, by modifying this function list order)
864 */
865 static const fn_activate_user_ini_chain_t fn_chain[] = {
866 &lsapi_activate_user_ini_basic_checks,
867 &lsapi_activate_user_ini_mk_path,
868 &lsapi_activate_user_ini_mk_realpath,
869 &lsapi_activate_user_ini_mk_user_config,
870 &lsapi_activate_user_ini_walk_down_the_path,
871 &lsapi_activate_user_ini_finally,
872 NULL
873 };
874
875 return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
876 }
877
878
override_ini()879 static void override_ini()
880 {
881
882 LSAPI_ForeachSpecialEnv( alter_ini, NULL );
883
884 }
885
886
processReq(void)887 static int processReq(void)
888 {
889 int ret = 0;
890 zend_first_try {
891
892 /* avoid server_context==NULL checks */
893 SG(server_context) = (void *) 1;
894
895 engine = 1;
896 override_ini();
897
898 if ( engine ) {
899 init_request_info();
900
901 if ( lsapi_module_main( source_highlight ) == -1 ) {
902 ret = -1;
903 }
904 } else {
905 LSAPI_AppendRespHeader( "status: 403", 11 );
906 LSAPI_AppendRespHeader( "content-type: text/html", 23 );
907 LSAPI_Write( "Forbidden: PHP engine is disable.\n", 34 );
908 }
909 } zend_end_try();
910 return ret;
911 }
912
cli_usage(void)913 static void cli_usage(void)
914 {
915 static const char * usage =
916 "Usage: php\n"
917 " php -[b|c|n|h|i|q|s|v|?] [<file>] [args...]\n"
918 " Run in LSAPI mode, only '-b', '-s' and '-c' are effective\n"
919 " Run in Command Line Interpreter mode when parameters are specified\n"
920 "\n"
921 " -b <address:port>|<port> Bind Path for external LSAPI Server mode\n"
922 " -c <path>|<file> Look for php.ini file in this directory\n"
923 " -n No php.ini file will be used\n"
924 " -h This help\n"
925 " -i PHP information\n"
926 " -l Syntax check\n"
927 " -q Quiet-mode. Suppress HTTP Header output.\n"
928 " -s Display colour syntax highlighted source.\n"
929 " -v Version number\n"
930 " -? This help\n"
931 "\n"
932 " args... Arguments passed to script.\n";
933 php_output_startup();
934 php_output_activate();
935 php_printf( "%s", usage );
936 #ifdef PHP_OUTPUT_NEWAPI
937 php_output_end_all();
938 #else
939 php_end_ob_buffers(1);
940 #endif
941 }
942
parse_opt(int argc,char * argv[],int * climode,char ** php_ini_path,char ** php_bind)943 static int parse_opt( int argc, char * argv[], int *climode,
944 char **php_ini_path, char ** php_bind )
945 {
946 char ** p = &argv[1];
947 char ** argend= &argv[argc];
948 int c;
949 while (( p < argend )&&(**p == '-' )) {
950 c = *((*p)+1);
951 ++p;
952 switch( c ) {
953 case 'b':
954 if ( p >= argend ) {
955 fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n");
956 return -1;
957 }
958 *php_bind = strdup(*p++);
959 break;
960
961 case 'c':
962 if ( p >= argend ) {
963 fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n");
964
965 return -1;
966 }
967 *php_ini_path = strdup( *p++ );
968 break;
969 case 's':
970 source_highlight = 1;
971 break;
972 case 'n':
973 ignore_php_ini = 1;
974 break;
975 case '?':
976 if ( *((*(p-1))+2) == 's' )
977 exit( 99 );
978 case 'h':
979 case 'i':
980 case 'l':
981 case 'q':
982 case 'v':
983 default:
984 *climode = 1;
985 break;
986 }
987 }
988 if ( p - argv < argc ) {
989 *climode = 1;
990 }
991 return 0;
992 }
993
cli_main(int argc,char * argv[])994 static int cli_main( int argc, char * argv[] )
995 {
996
997 static const char * ini_defaults[] = {
998 "report_zend_debug", "0",
999 "display_errors", "1",
1000 "register_argc_argv", "1",
1001 "html_errors", "0",
1002 "implicit_flush", "1",
1003 "output_buffering", "0",
1004 "max_execution_time", "0",
1005 "max_input_time", "-1",
1006 NULL
1007 };
1008
1009 const char ** ini;
1010 char ** p = &argv[1];
1011 char ** argend= &argv[argc];
1012 int ret = -1;
1013 int c;
1014 zend_string *psKey;
1015 lsapi_mode = 0; /* enter CLI mode */
1016
1017 #ifdef PHP_WIN32
1018 _fmode = _O_BINARY; /*sets default for file streams to binary */
1019 setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
1020 setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
1021 setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
1022 #endif
1023
1024 zend_first_try {
1025 SG(server_context) = (void *) 1;
1026
1027 zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
1028 CG(in_compilation) = 0; /* not initialized but needed for several options */
1029 SG(options) |= SAPI_OPTION_NO_CHDIR;
1030
1031 for( ini = ini_defaults; *ini; ini+=2 ) {
1032 psKey = zend_string_init(*ini, strlen( *ini ), 1);
1033 zend_alter_ini_entry_chars(psKey,
1034 (char *)*(ini+1), strlen( *(ini+1) ),
1035 PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
1036 zend_string_release(psKey);
1037 }
1038
1039 while (( p < argend )&&(**p == '-' )) {
1040 c = *((*p)+1);
1041 ++p;
1042 switch( c ) {
1043 case 'q':
1044 break;
1045 case 'i':
1046 if (php_request_startup() != FAILURE) {
1047 php_print_info(0xFFFFFFFF);
1048 #ifdef PHP_OUTPUT_NEWAPI
1049 php_output_end_all();
1050 #else
1051 php_end_ob_buffers(1);
1052 #endif
1053 php_request_shutdown( NULL );
1054 ret = 0;
1055 }
1056 break;
1057 case 'v':
1058 if (php_request_startup() != FAILURE) {
1059 #if ZEND_DEBUG
1060 php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2018 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1061 #else
1062 php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2018 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
1063 #endif
1064 #ifdef PHP_OUTPUT_NEWAPI
1065 php_output_end_all();
1066 #else
1067 php_end_ob_buffers(1);
1068 #endif
1069 php_request_shutdown( NULL );
1070 ret = 0;
1071 }
1072 break;
1073 case 'c':
1074 ++p;
1075 /* fall through */
1076 case 's':
1077 break;
1078 case 'l':
1079 source_highlight = 2;
1080 break;
1081 case 'h':
1082 case '?':
1083 default:
1084 cli_usage();
1085 ret = 0;
1086 break;
1087
1088 }
1089 }
1090 if ( ret == -1 ) {
1091 if ( *p ) {
1092 zend_file_handle file_handle;
1093 memset(&file_handle, 0, sizeof(file_handle));
1094 file_handle.type = ZEND_HANDLE_FP;
1095 file_handle.handle.fp = VCWD_FOPEN(*p, "rb");
1096
1097 if ( file_handle.handle.fp ) {
1098 script_filename = *p;
1099 php_self = *p;
1100
1101 SG(request_info).path_translated = estrdup(*p);
1102 SG(request_info).argc = argc - (p - argv);
1103 SG(request_info).argv = p;
1104
1105 if (php_request_startup() == FAILURE ) {
1106 fclose( file_handle.handle.fp );
1107 ret = 2;
1108 } else {
1109 if (source_highlight == 1) {
1110 zend_syntax_highlighter_ini syntax_highlighter_ini;
1111
1112 php_get_highlight_struct(&syntax_highlighter_ini);
1113 highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
1114 } else if (source_highlight == 2) {
1115 file_handle.filename = *p;
1116 file_handle.free_filename = 0;
1117 file_handle.opened_path = NULL;
1118 ret = php_lint_script(&file_handle);
1119 if (ret==SUCCESS) {
1120 zend_printf("No syntax errors detected in %s\n", file_handle.filename);
1121 } else {
1122 zend_printf("Errors parsing %s\n", file_handle.filename);
1123 }
1124
1125 } else {
1126 file_handle.filename = *p;
1127 file_handle.free_filename = 0;
1128 file_handle.opened_path = NULL;
1129
1130 php_execute_script(&file_handle);
1131 ret = EG(exit_status);
1132 }
1133
1134 php_request_shutdown( NULL );
1135 }
1136 } else {
1137 php_printf("Could not open input file: %s.\n", *p);
1138 }
1139 } else {
1140 cli_usage();
1141 }
1142 }
1143
1144 }zend_end_try();
1145
1146 php_module_shutdown();
1147
1148 #ifdef ZTS
1149 tsrm_shutdown();
1150 #endif
1151 return ret;
1152 }
1153
1154 static int s_stop;
litespeed_cleanup(int signal)1155 void litespeed_cleanup(int signal)
1156 {
1157 s_stop = signal;
1158 }
1159
1160
start_children(int children)1161 void start_children( int children )
1162 {
1163 struct sigaction act, old_term, old_quit, old_int, old_usr1;
1164 int running = 0;
1165 int status;
1166 pid_t pid;
1167
1168 /* Create a process group */
1169 setsid();
1170
1171 /* Set up handler to kill children upon exit */
1172 act.sa_flags = 0;
1173 act.sa_handler = litespeed_cleanup;
1174 if( sigaction( SIGTERM, &act, &old_term ) ||
1175 sigaction( SIGINT, &act, &old_int ) ||
1176 sigaction( SIGUSR1, &act, &old_usr1 ) ||
1177 sigaction( SIGQUIT, &act, &old_quit )) {
1178 perror( "Can't set signals" );
1179 exit( 1 );
1180 }
1181 s_stop = 0;
1182 while( 1 ) {
1183 while((!s_stop )&&( running < children )) {
1184 pid = fork();
1185 switch( pid ) {
1186 case 0: /* children process */
1187
1188 /* don't catch our signals */
1189 sigaction( SIGTERM, &old_term, 0 );
1190 sigaction( SIGQUIT, &old_quit, 0 );
1191 sigaction( SIGINT, &old_int, 0 );
1192 sigaction( SIGUSR1, &old_usr1, 0 );
1193 return ;
1194 case -1:
1195 perror( "php (pre-forking)" );
1196 exit( 1 );
1197 break;
1198 default: /* parent process */
1199 running++;
1200 break;
1201 }
1202 }
1203 if ( s_stop ) {
1204 break;
1205 }
1206 pid = wait( &status );
1207 running--;
1208 }
1209 kill( -getpgrp(), SIGUSR1 );
1210 exit( 0 );
1211 }
1212
setArgv0(int argc,char * argv[])1213 void setArgv0( int argc, char * argv[] )
1214 {
1215 char * p;
1216 int i;
1217 argv0 = argv[0] + strlen( argv[0] );
1218 p = argv0;
1219 while(( p > argv[0] )&&( p[-1] != '/'))
1220 --p;
1221 if ( p > argv[0] )
1222 {
1223 memmove( argv[0], p, argv0 - p );
1224 memset( argv[0] + ( argv0 - p ), 0, p - argv[0] );
1225 argv0 = argv[0] + (argv0 - p);
1226 }
1227 for( i = 1; i < argc; ++i )
1228 {
1229 memset( argv[i], 0, strlen( argv[i] ) );
1230 }
1231 }
1232
1233 #include <fcntl.h>
main(int argc,char * argv[])1234 int main( int argc, char * argv[] )
1235 {
1236 int ret;
1237 int bindFd;
1238
1239 char * php_ini_path = NULL;
1240 char * php_bind = NULL;
1241 int n;
1242 int climode = 0;
1243 struct timeval tv_req_begin;
1244 struct timeval tv_req_end;
1245 int slow_script_msec = 0;
1246 char time_buf[40];
1247
1248 #ifdef HAVE_SIGNAL_H
1249 #if defined(SIGPIPE) && defined(SIG_IGN)
1250 signal(SIGPIPE, SIG_IGN);
1251 #endif
1252 #endif
1253
1254 #ifdef ZTS
1255 tsrm_startup(1, 1, 0, NULL);
1256 #endif
1257
1258 #if PHP_MAJOR_VERSION >= 7
1259 #if defined(ZEND_SIGNALS) || PHP_MINOR_VERSION > 0
1260 zend_signal_startup();
1261 #endif
1262 #endif
1263
1264 if (argc > 1 ) {
1265 if ( parse_opt( argc, argv, &climode,
1266 &php_ini_path, &php_bind ) == -1 ) {
1267 return 1;
1268 }
1269 }
1270 if ( climode ) {
1271 lsapi_sapi_module.phpinfo_as_text = 1;
1272 } else {
1273 setArgv0(argc, argv );
1274 }
1275
1276 sapi_startup(&lsapi_sapi_module);
1277
1278 #ifdef ZTS
1279 compiler_globals = ts_resource(compiler_globals_id);
1280 executor_globals = ts_resource(executor_globals_id);
1281 core_globals = ts_resource(core_globals_id);
1282 sapi_globals = ts_resource(sapi_globals_id);
1283 tsrm_ls = ts_resource(0);
1284
1285 SG(request_info).path_translated = NULL;
1286 #endif
1287
1288 lsapi_sapi_module.executable_location = argv[0];
1289
1290 /* Initialize from environment variables before processing command-line
1291 * options: the latter override the former.
1292 */
1293 init_sapi_from_env(&lsapi_sapi_module);
1294
1295 if ( ignore_php_ini )
1296 lsapi_sapi_module.php_ini_ignore = 1;
1297
1298 if ( php_ini_path ) {
1299 lsapi_sapi_module.php_ini_path_override = php_ini_path;
1300 }
1301
1302
1303 lsapi_sapi_module.ini_defaults = sapi_lsapi_ini_defaults;
1304
1305 if (php_module_startup(&lsapi_sapi_module, &litespeed_module_entry, 1) == FAILURE) {
1306 #ifdef ZTS
1307 tsrm_shutdown();
1308 #endif
1309 return FAILURE;
1310 }
1311
1312 if ( climode ) {
1313 return cli_main(argc, argv);
1314 }
1315
1316 if ( php_bind ) {
1317 bindFd = LSAPI_CreateListenSock( php_bind, 10 );
1318 if ( bindFd == -1 ) {
1319 fprintf( stderr,
1320 "Failed to bind socket [%s]: %s\n", php_bind, strerror( errno ) );
1321 exit( 2 );
1322 }
1323 if ( bindFd != 0 ) {
1324 dup2( bindFd, 0 );
1325 close( bindFd );
1326 }
1327 }
1328
1329 LSAPI_Init();
1330
1331 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
1332 int is_criu = LSCRIU_Init(); // Must be called before the regular init as it unsets the parameters.
1333 #endif
1334
1335 LSAPI_Init_Env_Parameters( NULL );
1336 lsapi_mode = 1;
1337
1338 slow_script_msec = LSAPI_Get_Slow_Req_Msecs();
1339
1340 if ( php_bind ) {
1341 LSAPI_No_Check_ppid();
1342 free( php_bind );
1343 php_bind = NULL;
1344 }
1345
1346 int iRequestsProcessed = 0;
1347 int result;
1348
1349 while( ( result = LSAPI_Prefork_Accept_r( &g_req )) >= 0 ) {
1350 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__)
1351 if (is_criu && !result) {
1352 LSCRIU_inc_req_procssed();
1353 }
1354 #endif
1355 if ( slow_script_msec ) {
1356 gettimeofday( &tv_req_begin, NULL );
1357 }
1358 ret = processReq();
1359 if ( slow_script_msec ) {
1360 gettimeofday( &tv_req_end, NULL );
1361 n = ((long) tv_req_end.tv_sec - tv_req_begin.tv_sec ) * 1000
1362 + (tv_req_end.tv_usec - tv_req_begin.tv_usec) / 1000;
1363 if ( n > slow_script_msec )
1364 {
1365 strftime( time_buf, 30, "%d/%b/%Y:%H:%M:%S", localtime( &tv_req_end.tv_sec ) );
1366 fprintf( stderr, "[%s] Slow PHP script: %d ms\n URL: %s %s\n Query String: %s\n Script: %s\n",
1367 time_buf, n, LSAPI_GetRequestMethod(),
1368 LSAPI_GetScriptName(), LSAPI_GetQueryString(),
1369 LSAPI_GetScriptFileName() );
1370
1371 }
1372 }
1373 LSAPI_Finish();
1374 if ( ret ) {
1375 break;
1376 }
1377 }
1378 php_module_shutdown();
1379
1380 #ifdef ZTS
1381 tsrm_shutdown();
1382 #endif
1383 return ret;
1384 }
1385
1386
1387 /* LiteSpeed PHP module starts here */
1388
1389 /* {{{ arginfo */
1390 ZEND_BEGIN_ARG_INFO(arginfo_litespeed__void, 0)
1391 ZEND_END_ARG_INFO()
1392 /* }}} */
1393
1394 PHP_FUNCTION(litespeed_request_headers);
1395 PHP_FUNCTION(litespeed_response_headers);
1396 PHP_FUNCTION(apache_get_modules);
1397
1398 PHP_MINFO_FUNCTION(litespeed);
1399
1400 zend_function_entry litespeed_functions[] = {
1401 PHP_FE(litespeed_request_headers, arginfo_litespeed__void)
1402 PHP_FE(litespeed_response_headers, arginfo_litespeed__void)
1403 PHP_FE(apache_get_modules, arginfo_litespeed__void)
1404 PHP_FALIAS(getallheaders, litespeed_request_headers, arginfo_litespeed__void)
1405 PHP_FALIAS(apache_request_headers, litespeed_request_headers, arginfo_litespeed__void)
1406 PHP_FALIAS(apache_response_headers, litespeed_response_headers, arginfo_litespeed__void)
1407 {NULL, NULL, NULL}
1408 };
1409
PHP_MINIT_FUNCTION(litespeed)1410 static PHP_MINIT_FUNCTION(litespeed)
1411 {
1412 user_config_cache_init();
1413
1414 const char *p = getenv("LSPHP_ENABLE_USER_INI");
1415 if (p && 0 == strcasecmp(p, "on"))
1416 parse_user_ini = 1;
1417
1418 /* REGISTER_INI_ENTRIES(); */
1419 return SUCCESS;
1420 }
1421
1422
PHP_MSHUTDOWN_FUNCTION(litespeed)1423 static PHP_MSHUTDOWN_FUNCTION(litespeed)
1424 {
1425 zend_hash_destroy(&user_config_cache);
1426
1427 /* UNREGISTER_INI_ENTRIES(); */
1428 return SUCCESS;
1429 }
1430
1431 zend_module_entry litespeed_module_entry = {
1432 STANDARD_MODULE_HEADER,
1433 "litespeed",
1434 litespeed_functions,
1435 PHP_MINIT(litespeed),
1436 PHP_MSHUTDOWN(litespeed),
1437 NULL,
1438 NULL,
1439 NULL,
1440 NO_VERSION_YET,
1441 STANDARD_MODULE_PROPERTIES
1442 };
1443
add_associate_array(const char * pKey,int keyLen,const char * pValue,int valLen,void * arg)1444 static int add_associate_array( const char * pKey, int keyLen, const char * pValue, int valLen,
1445 void * arg )
1446 {
1447 add_assoc_string_ex((zval *)arg, (char *)pKey, keyLen, (char *)pValue);
1448 return 1;
1449 }
1450
1451
1452 /* {{{ proto array litespeed_request_headers(void)
1453 Fetch all HTTP request headers */
PHP_FUNCTION(litespeed_request_headers)1454 PHP_FUNCTION(litespeed_request_headers)
1455 {
1456 /* TODO: */
1457 if (ZEND_NUM_ARGS() > 0) {
1458 WRONG_PARAM_COUNT;
1459 }
1460 array_init(return_value);
1461
1462 LSAPI_ForeachOrgHeader( add_associate_array, return_value );
1463
1464 }
1465 /* }}} */
1466
1467
1468
1469 /* {{{ proto array litespeed_response_headers(void)
1470 Fetch all HTTP response headers */
PHP_FUNCTION(litespeed_response_headers)1471 PHP_FUNCTION(litespeed_response_headers)
1472 {
1473 sapi_header_struct *h;
1474 zend_llist_position pos;
1475 char * p;
1476 int len;
1477 char headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
1478
1479 if (ZEND_NUM_ARGS() > 0) {
1480 WRONG_PARAM_COUNT;
1481 }
1482
1483 if (!&SG(sapi_headers).headers) {
1484 RETURN_FALSE;
1485 }
1486 array_init(return_value);
1487
1488 h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos);
1489 while (h) {
1490 if ( h->header_len > 0 ) {
1491 p = strchr( h->header, ':' );
1492 len = p - h->header;
1493 if (( p )&&( len > 0 )) {
1494 memmove( headerBuf, h->header, len );
1495 while( len > 0 && (isspace( headerBuf[len-1])) ) {
1496 --len;
1497 }
1498 headerBuf[len] = 0;
1499 if ( len ) {
1500 while( isspace(*++p));
1501 add_assoc_string_ex(return_value, headerBuf, len, p);
1502 }
1503 }
1504 }
1505 h = zend_llist_get_next_ex(&SG(sapi_headers).headers, &pos);
1506 }
1507 }
1508
1509 /* }}} */
1510
1511
1512 /* {{{ proto array apache_get_modules(void)
1513 Fetch all loaded module names */
PHP_FUNCTION(apache_get_modules)1514 PHP_FUNCTION(apache_get_modules)
1515 {
1516 static const char * mod_names[] =
1517 {
1518 "mod_rewrite", "mod_mime", "mod_headers", "mod_expires", "mod_auth_basic", NULL
1519 };
1520 const char **name = mod_names;
1521 /* TODO: */
1522 if (ZEND_NUM_ARGS() > 0) {
1523 WRONG_PARAM_COUNT;
1524 }
1525 array_init(return_value);
1526 while( *name )
1527 {
1528 add_next_index_string(return_value, *name);
1529 ++name;
1530 }
1531 }
1532 /* }}} */
1533
1534
1535 /*
1536 * Local variables:
1537 * tab-width: 4
1538 * c-basic-offset: 4
1539 * End:
1540 * vim600: sw=4 ts=4 fdm=marker
1541 * vim<600: sw=4 ts=4
1542 */
1543