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