1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2016 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available 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
27#include "lsapilib.h"
28
29#include <stdio.h>
30
31#if HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
34
35#if HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38
39#ifdef PHP_WIN32
40
41#include <io.h>
42#include <fcntl.h>
43#include "win32/php_registry.h"
44
45#else
46
47#include <sys/wait.h>
48
49#endif
50
51#include <sys/stat.h>
52
53#if HAVE_SYS_TYPES_H
54
55#include <sys/types.h>
56
57#endif
58
59#if HAVE_SIGNAL_H
60
61#include <signal.h>
62
63#endif
64
65#include <sys/socket.h>
66#include <arpa/inet.h>
67#include <netinet/in.h>
68
69
70#define SAPI_LSAPI_MAX_HEADER_LENGTH 2048
71
72static int  lsapi_mode       = 0;
73static char *php_self        = "";
74static char *script_filename = "";
75static int  source_highlight = 0;
76static int  ignore_php_ini   = 0;
77static char * argv0 = NULL;
78static int  engine = 1;
79#ifdef ZTS
80zend_compiler_globals    *compiler_globals;
81zend_executor_globals    *executor_globals;
82php_core_globals         *core_globals;
83sapi_globals_struct      *sapi_globals;
84void ***tsrm_ls;
85#endif
86
87zend_module_entry litespeed_module_entry;
88
89/* {{{ php_lsapi_startup
90 */
91static int php_lsapi_startup(sapi_module_struct *sapi_module)
92{
93    if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
94        return FAILURE;
95    }
96    argv0 = sapi_module->executable_location;
97    return SUCCESS;
98}
99/* }}} */
100
101/* {{{ sapi_lsapi_ini_defaults */
102
103/* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */
104#define INI_DEFAULT(name,value)\
105    ZVAL_STRING(tmp, value, 0);\
106    zend_hash_update(configuration_hash, name, sizeof(name), tmp, sizeof(zval), (void**)&entry);\
107    Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry))
108
109static void sapi_lsapi_ini_defaults(HashTable *configuration_hash)
110{
111    zval *tmp, *entry;
112
113#if PHP_MAJOR_VERSION > 4
114/*
115    MAKE_STD_ZVAL(tmp);
116
117    INI_DEFAULT("register_long_arrays", "0");
118
119    FREE_ZVAL(tmp);
120*/
121#endif
122
123}
124/* }}} */
125
126
127/* {{{ sapi_lsapi_ub_write
128 */
129static int sapi_lsapi_ub_write(const char *str, uint str_length TSRMLS_DC)
130{
131    int ret;
132    int remain;
133    if ( lsapi_mode ) {
134        ret  = LSAPI_Write( str, str_length );
135        if ( ret < str_length ) {
136            php_handle_aborted_connection();
137            return str_length - ret;
138        }
139    } else {
140        remain = str_length;
141        while( remain > 0 ) {
142            ret = write( 1, str, remain );
143            if ( ret <= 0 ) {
144                php_handle_aborted_connection();
145                return str_length - remain;
146            }
147            str += ret;
148            remain -= ret;
149        }
150    }
151    return str_length;
152}
153/* }}} */
154
155
156/* {{{ sapi_lsapi_flush
157 */
158static void sapi_lsapi_flush( void * server_context TSRMLS_DC )
159{
160    if ( lsapi_mode ) {
161        if ( LSAPI_Flush() == -1) {
162            php_handle_aborted_connection();
163        }
164    }
165}
166/* }}} */
167
168
169/* {{{ sapi_lsapi_deactivate
170 */
171static int sapi_lsapi_deactivate(TSRMLS_D)
172{
173    if ( SG(request_info).path_translated )
174    {
175        efree( SG(request_info).path_translated );
176    }
177
178    return SUCCESS;
179}
180/* }}} */
181
182
183
184
185/* {{{ sapi_lsapi_getenv
186 */
187static char *sapi_lsapi_getenv( char * name, size_t name_len TSRMLS_DC )
188{
189    if ( lsapi_mode ) {
190        return LSAPI_GetEnv( name );
191    } else {
192        return getenv( name );
193    }
194}
195/* }}} */
196
197
198
199
200static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
201                         void * arg )
202{
203#if PHP_MAJOR_VERSION >= 7
204    int filter_arg = (Z_ARR_P((zval *)arg) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))
205        ? PARSE_ENV : PARSE_SERVER;
206#else
207    int filter_arg = (arg == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
208#endif
209    char * new_val = (char *) pValue;
210    unsigned int new_val_len;
211
212    if (sapi_module.input_filter(filter_arg, (char *)pKey, &new_val, valLen, &new_val_len TSRMLS_CC)) {
213        php_register_variable_safe((char *)pKey, new_val, new_val_len, (zval *)arg );
214    }
215    return 1;
216}
217
218/*
219static int add_variable( const char * pKey, int keyLen, const char * pValue, int valLen,
220                         void * arg )
221{
222    zval * gpc_element, **gpc_element_p;
223    HashTable * symtable1 = Z_ARRVAL_P((zval * )arg);
224    register char * pKey1 = (char *)pKey;
225
226    MAKE_STD_ZVAL(gpc_element);
227    Z_STRLEN_P( gpc_element ) = valLen;
228    Z_STRVAL_P( gpc_element ) = estrndup(pValue, valLen);
229    Z_TYPE_P( gpc_element ) = IS_STRING;
230#if PHP_MAJOR_VERSION > 4
231    zend_symtable_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
232#else
233    zend_hash_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
234#endif
235    return 1;
236}
237*/
238
239static void litespeed_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
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 PHP_MAJOR_VERSION >= 7
247    if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
248        Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
249        zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
250    ) {
251        zval_dtor(array_ptr);
252        ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
253        return;
254    } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
255        Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
256        zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
257    ) {
258        zval_dtor(array_ptr);
259        ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
260        return;
261    }
262#else
263    if (PG(http_globals)[TRACK_VARS_ENV] &&
264        array_ptr != PG(http_globals)[TRACK_VARS_ENV] &&
265        Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
266        zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV])) > 0
267    ) {
268        zval_dtor(array_ptr);
269        *array_ptr = *PG(http_globals)[TRACK_VARS_ENV];
270        INIT_PZVAL(array_ptr);
271        zval_copy_ctor(array_ptr);
272        return;
273    } else if (PG(http_globals)[TRACK_VARS_SERVER] &&
274        array_ptr != PG(http_globals)[TRACK_VARS_SERVER] &&
275        Z_TYPE_P(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
276        zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])) > 0
277    ) {
278        zval_dtor(array_ptr);
279        *array_ptr = *PG(http_globals)[TRACK_VARS_SERVER];
280        INIT_PZVAL(array_ptr);
281        zval_copy_ctor(array_ptr);
282        return;
283    }
284#endif
285
286    for (env = environ; env != NULL && *env != NULL; env++) {
287        p = strchr(*env, '=');
288        if (!p) {               /* malformed entry? */
289            continue;
290        }
291        nlen = p - *env;
292        if (nlen >= alloc_size) {
293            alloc_size = nlen + 64;
294            t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
295        }
296        memcpy(t, *env, nlen);
297        t[nlen] = '\0';
298        add_variable(t, nlen, p + 1, strlen( p + 1 ), array_ptr TSRMLS_CC);
299    }
300    if (t != buf && t != NULL) {
301        efree(t);
302    }
303}
304
305
306#if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || PHP_MAJOR_VERSION < 5)
307static int add_variable_magic_quote( const char * pKey, int keyLen, const char * pValue, int valLen,
308                         void * arg )
309{
310    zval * gpc_element, **gpc_element_p;
311    HashTable * symtable1 = Z_ARRVAL_P((zval * )arg);
312    register char * pKey1 = (char *)pKey;
313
314    MAKE_STD_ZVAL(gpc_element);
315    Z_STRLEN_P( gpc_element ) = valLen;
316    Z_STRVAL_P( gpc_element ) = php_addslashes((char *)pValue, valLen, &Z_STRLEN_P( gpc_element ), 0 );
317    Z_TYPE_P( gpc_element ) = IS_STRING;
318#if PHP_MAJOR_VERSION > 4
319    zend_symtable_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
320#else
321    zend_hash_update( symtable1, pKey1, keyLen + 1, &gpc_element, sizeof( zval *), (void **) &gpc_element_p );
322#endif
323    return 1;
324}
325
326#endif
327
328/* {{{ sapi_lsapi_register_variables
329 */
330static void sapi_lsapi_register_variables(zval *track_vars_array TSRMLS_DC)
331{
332    char * php_self = "";
333    if ( lsapi_mode ) {
334        if ( (SG(request_info).request_uri ) )
335            php_self = (SG(request_info).request_uri );
336
337        litespeed_php_import_environment_variables(track_vars_array TSRMLS_CC);
338
339#if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || PHP_MAJOR_VERSION < 5)
340        if (!PG(magic_quotes_gpc)) {
341#endif
342            LSAPI_ForeachHeader( add_variable, track_vars_array );
343            LSAPI_ForeachEnv( add_variable, track_vars_array );
344            add_variable("PHP_SELF", 8, php_self, strlen( php_self ), track_vars_array );
345#if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || PHP_MAJOR_VERSION < 5)
346        } else {
347            LSAPI_ForeachHeader( add_variable_magic_quote, track_vars_array );
348            LSAPI_ForeachEnv( add_variable_magic_quote, track_vars_array );
349            add_variable_magic_quote("PHP_SELF", 8, php_self, strlen( php_self ), track_vars_array );
350        }
351#endif
352    } else {
353        php_import_environment_variables(track_vars_array TSRMLS_CC);
354
355        php_register_variable("PHP_SELF", php_self, track_vars_array TSRMLS_CC);
356        php_register_variable("SCRIPT_NAME", php_self, track_vars_array TSRMLS_CC);
357        php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array TSRMLS_CC);
358        php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array TSRMLS_CC);
359        php_register_variable("DOCUMENT_ROOT", "", track_vars_array TSRMLS_CC);
360
361    }
362}
363/* }}} */
364
365
366/* {{{ sapi_lsapi_read_post
367 */
368static int sapi_lsapi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
369{
370    if ( lsapi_mode ) {
371        return LSAPI_ReadReqBody( buffer, (unsigned long long)count_bytes );
372    } else {
373        return 0;
374    }
375}
376/* }}} */
377
378
379
380
381/* {{{ sapi_lsapi_read_cookies
382 */
383static char *sapi_lsapi_read_cookies(TSRMLS_D)
384{
385    if ( lsapi_mode ) {
386        return LSAPI_GetHeader( H_COOKIE );
387    } else {
388        return NULL;
389    }
390}
391/* }}} */
392
393
394/* {{{ sapi_lsapi_send_headers
395 */
396static int sapi_lsapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
397{
398    sapi_header_struct  *h;
399    zend_llist_position pos;
400    if ( lsapi_mode ) {
401        LSAPI_SetRespStatus( SG(sapi_headers).http_response_code );
402
403        h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
404        while (h) {
405            if ( h->header_len > 0 ) {
406                LSAPI_AppendRespHeader(h->header, h->header_len);
407            }
408            h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
409        }
410        if (SG(sapi_headers).send_default_content_type) {
411            char    *hd;
412            int     len;
413            char    headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
414
415            hd = sapi_get_default_content_type(TSRMLS_C);
416            len = snprintf( headerBuf, SAPI_LSAPI_MAX_HEADER_LENGTH - 1,
417                            "Content-type: %s", hd );
418            efree(hd);
419
420            LSAPI_AppendRespHeader( headerBuf, len );
421        }
422    }
423    LSAPI_FinalizeRespHeaders();
424    return SAPI_HEADER_SENT_SUCCESSFULLY;
425
426
427}
428/* }}} */
429
430
431/* {{{ sapi_lsapi_send_headers
432 */
433static void sapi_lsapi_log_message(char *message TSRMLS_DC)
434{
435    char buf[8192];
436    int len = strlen( message );
437    if ( *(message + len - 1 ) != '\n' )
438    {
439        snprintf( buf, 8191, "%s\n", message );
440        message = buf;
441        ++len;
442    }
443    LSAPI_Write_Stderr( message, len);
444}
445/* }}} */
446
447
448/* {{{ sapi_module_struct cgi_sapi_module
449 */
450static sapi_module_struct lsapi_sapi_module =
451{
452    "litespeed",
453    "LiteSpeed V6.9",
454
455    php_lsapi_startup,              /* startup */
456    php_module_shutdown_wrapper,    /* shutdown */
457
458    NULL,                           /* activate */
459    sapi_lsapi_deactivate,          /* deactivate */
460
461    sapi_lsapi_ub_write,            /* unbuffered write */
462    sapi_lsapi_flush,               /* flush */
463    NULL,                           /* get uid */
464    sapi_lsapi_getenv,              /* getenv */
465
466    php_error,                      /* error handler */
467
468    NULL,                           /* header handler */
469    sapi_lsapi_send_headers,        /* send headers handler */
470    NULL,                           /* send header handler */
471
472    sapi_lsapi_read_post,           /* read POST data */
473    sapi_lsapi_read_cookies,        /* read Cookies */
474
475    sapi_lsapi_register_variables,  /* register server variables */
476    sapi_lsapi_log_message,         /* Log message */
477
478    NULL,                           /* php.ini path override */
479    NULL,                           /* block interruptions */
480    NULL,                           /* unblock interruptions */
481    NULL,                           /* default post reader */
482    NULL,                           /* treat data */
483    NULL,                           /* executable location */
484
485    0,                              /* php.ini ignore */
486
487    STANDARD_SAPI_MODULE_PROPERTIES
488
489};
490/* }}} */
491
492static void init_request_info( TSRMLS_D )
493{
494    char * pContentType = LSAPI_GetHeader( H_CONTENT_TYPE );
495    char * pAuth;
496
497    SG(request_info).content_type = pContentType ? pContentType : "";
498    SG(request_info).request_method = LSAPI_GetRequestMethod();
499    SG(request_info).query_string = LSAPI_GetQueryString();
500    SG(request_info).request_uri = LSAPI_GetScriptName();
501    SG(request_info).content_length = LSAPI_GetReqBodyLen();
502    SG(request_info).path_translated = estrdup( LSAPI_GetScriptFileName());
503
504    /* It is not reset by zend engine, set it to 200. */
505    SG(sapi_headers).http_response_code = 200;
506
507    pAuth = LSAPI_GetHeader( H_AUTHORIZATION );
508    php_handle_auth_data(pAuth TSRMLS_CC);
509}
510
511static char s_cur_chdir[4096] = "";
512
513static int lsapi_chdir_primary_script( zend_file_handle * file_handle )
514{
515#if PHP_MAJOR_VERSION > 4
516    char * p;
517    char ch;
518
519    SG(options) |= SAPI_OPTION_NO_CHDIR;
520    getcwd( s_cur_chdir, sizeof( s_cur_chdir ) );
521
522    p = strrchr( file_handle->filename, '/' );
523    if ( *p )
524    {
525        *p = 0;
526        if ( strcmp( file_handle->filename, s_cur_chdir ) != 0 ) {
527            chdir( file_handle->filename );
528        }
529        *p++ = '/';
530        ch = *p;
531        *p = 0;
532        if ( !CWDG(cwd).cwd ||
533             ( strcmp( file_handle->filename, CWDG(cwd).cwd ) != 0 ) ) {
534            CWDG(cwd).cwd_length = p - file_handle->filename;
535            CWDG(cwd).cwd = (char *) realloc(CWDG(cwd).cwd, CWDG(cwd).cwd_length+1);
536            memmove( CWDG(cwd).cwd, file_handle->filename, CWDG(cwd).cwd_length+1 );
537        }
538        *p = ch;
539    }
540    /* virtual_file_ex(&CWDG(cwd), file_handle->filename, NULL, CWD_REALPATH); */
541#else
542    VCWD_CHDIR_FILE( file_handle->filename );
543#endif
544    return 0;
545}
546
547static int lsapi_fopen_primary_script( zend_file_handle * file_handle )
548{
549    FILE * fp;
550    char * p;
551    fp = fopen( SG(request_info).path_translated, "rb" );
552    if ( !fp )
553    {
554        return -1;
555    }
556    file_handle->type = ZEND_HANDLE_FP;
557    file_handle->handle.fp = fp;
558    file_handle->filename = SG(request_info).path_translated;
559    file_handle->free_filename = 0;
560    file_handle->opened_path = NULL;
561
562    lsapi_chdir_primary_script( file_handle );
563
564    return 0;
565}
566
567static int lsapi_execute_script( zend_file_handle * file_handle TSRMLS_DC)
568{
569    char *p;
570    int len;
571    file_handle->type = ZEND_HANDLE_FILENAME;
572    file_handle->handle.fd = 0;
573    file_handle->filename = SG(request_info).path_translated;
574    file_handle->free_filename = 0;
575    file_handle->opened_path = NULL;
576
577    p = argv0;
578    *p++ = ':';
579    len = strlen( SG(request_info).path_translated );
580    if ( len > 45 )
581        len = len - 45;
582    else
583        len = 0;
584    memccpy( p, SG(request_info).path_translated + len, 0, 46 );
585
586    php_execute_script(file_handle TSRMLS_CC);
587    return 0;
588
589}
590
591
592static int lsapi_module_main(int show_source TSRMLS_DC)
593{
594    zend_file_handle file_handle = {0};
595
596    if (php_request_startup(TSRMLS_C) == FAILURE ) {
597        return -1;
598    }
599    if (show_source) {
600        zend_syntax_highlighter_ini syntax_highlighter_ini;
601
602        php_get_highlight_struct(&syntax_highlighter_ini);
603        highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini TSRMLS_CC);
604    } else {
605        lsapi_execute_script( &file_handle TSRMLS_CC);
606    }
607    zend_try {
608        php_request_shutdown(NULL);
609        memset( argv0, 0, 46 );
610    } zend_end_try();
611    return 0;
612}
613
614
615static int alter_ini( const char * pKey, int keyLen, const char * pValue, int valLen,
616                void * arg )
617{
618#if PHP_MAJOR_VERSION >= 7
619    zend_string * psKey;
620#endif
621    int type = ZEND_INI_PERDIR;
622    if ( '\001' == *pKey ) {
623        ++pKey;
624        if ( *pKey == 4 ) {
625            type = ZEND_INI_SYSTEM;
626        }
627        ++pKey;
628        --keyLen;
629        if (( keyLen == 7 )&&( strncasecmp( pKey, "engine", 6 )== 0 ))
630        {
631            if ( *pValue == '0' )
632                engine = 0;
633        }
634        else
635        {
636#if PHP_MAJOR_VERSION >= 7
637            psKey = STR_INIT( pKey, keyLen, 1 );
638            zend_alter_ini_entry(psKey,
639                             (char *)pValue, valLen,
640                             type, PHP_INI_STAGE_ACTIVATE);
641            STR_RELEASE( psKey );
642#else
643            zend_alter_ini_entry((char *)pKey, keyLen,
644                             (char *)pValue, valLen,
645                             type, PHP_INI_STAGE_ACTIVATE);
646#endif
647        }
648    }
649    return 1;
650}
651
652
653static void override_ini()
654{
655
656    LSAPI_ForeachSpecialEnv( alter_ini, NULL );
657
658}
659
660
661static int processReq( TSRMLS_D )
662{
663    int ret = 0;
664    zend_first_try {
665
666        /* avoid server_context==NULL checks */
667        SG(server_context) = (void *) 1;
668
669        engine = 1;
670        override_ini();
671
672        if ( engine ) {
673            init_request_info( TSRMLS_C );
674
675            if ( lsapi_module_main( source_highlight TSRMLS_CC ) == -1 ) {
676                ret = -1;
677            }
678        } else {
679            LSAPI_AppendRespHeader( "status: 403", 11 );
680            LSAPI_AppendRespHeader( "content-type: text/html", 23 );
681            LSAPI_Write( "Forbidden: PHP engine is disable.\n", 34 );
682        }
683    } zend_end_try();
684    return ret;
685}
686
687static void cli_usage( TSRMLS_D )
688{
689    static const char * usage =
690        "Usage: php\n"
691        "      php -[b|c|n|h|i|q|s|v|?] [<file>] [args...]\n"
692        "  Run in LSAPI mode, only '-b', '-s' and '-c' are effective\n"
693        "  Run in Command Line Interpreter mode when parameters are specified\n"
694        "\n"
695        "  -b <address:port>|<port> Bind Path for external LSAPI Server mode\n"
696        "  -c <path>|<file> Look for php.ini file in this directory\n"
697        "  -n    No php.ini file will be used\n"
698        "  -h    This help\n"
699        "  -i    PHP information\n"
700        "  -l    Syntax check\n"
701        "  -q    Quiet-mode.  Suppress HTTP Header output.\n"
702        "  -s    Display colour syntax highlighted source.\n"
703        "  -v    Version number\n"
704        "  -?    This help\n"
705        "\n"
706        "  args...    Arguments passed to script.\n";
707    php_output_startup();
708    php_output_activate(TSRMLS_C);
709    php_printf( "%s", usage );
710#ifdef PHP_OUTPUT_NEWAPI
711    php_output_end_all(TSRMLS_C);
712#else
713    php_end_ob_buffers(1 TSRMLS_CC);
714#endif
715}
716
717static int parse_opt( int argc, char * argv[], int *climode,
718                        char **php_ini_path, char ** php_bind )
719{
720    char ** p = &argv[1];
721    char ** argend= &argv[argc];
722    int c;
723    while (( p < argend )&&(**p == '-' )) {
724        c = *((*p)+1);
725        ++p;
726        switch( c ) {
727        case 'b':
728            if ( p >= argend ) {
729                fprintf( stderr, "TCP or socket address must be specified following '-b' option.\n");
730                return -1;
731            }
732            *php_bind = strdup(*p++);
733            break;
734
735        case 'c':
736            if ( p >= argend ) {
737                fprintf( stderr, "<path> or <file> must be specified following '-c' option.\n");
738
739                return -1;
740            }
741            *php_ini_path = strdup( *p++ );
742            break;
743        case 's':
744            source_highlight = 1;
745            break;
746        case 'n':
747            ignore_php_ini = 1;
748            break;
749        case '?':
750            if ( *((*(p-1))+2) == 's' )
751                exit( 99 );
752        case 'h':
753        case 'i':
754        case 'l':
755        case 'q':
756        case 'v':
757        default:
758            *climode = 1;
759            break;
760        }
761    }
762    if ( p - argv < argc ) {
763        *climode = 1;
764    }
765    return 0;
766}
767
768static int cli_main( int argc, char * argv[] )
769{
770
771    static const char * ini_defaults[] = {
772        "report_zend_debug",    "0",
773        "display_errors",       "1",
774        "register_argc_argv",   "1",
775        "html_errors",          "0",
776        "implicit_flush",       "1",
777        "output_buffering",     "0",
778        "max_execution_time",   "0",
779        "max_input_time",       "-1",
780        NULL
781    };
782
783    const char ** ini;
784    char ** p = &argv[1];
785    char ** argend= &argv[argc];
786    int ret = -1;
787    int c;
788#if PHP_MAJOR_VERSION >= 7
789    zend_string * psKey;
790#endif
791    lsapi_mode = 0;        /* enter CLI mode */
792
793#ifdef PHP_WIN32
794    _fmode = _O_BINARY;            /*sets default for file streams to binary */
795    setmode(_fileno(stdin), O_BINARY);    /* make the stdio mode be binary */
796    setmode(_fileno(stdout), O_BINARY);   /* make the stdio mode be binary */
797    setmode(_fileno(stderr), O_BINARY);   /* make the stdio mode be binary */
798#endif
799
800    zend_first_try     {
801        SG(server_context) = (void *) 1;
802
803        zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
804        CG(in_compilation) = 0; /* not initialized but needed for several options */
805        SG(options) |= SAPI_OPTION_NO_CHDIR;
806
807#if PHP_MAJOR_VERSION < 7
808        EG(uninitialized_zval_ptr) = NULL;
809#endif
810        for( ini = ini_defaults; *ini; ini+=2 ) {
811#if PHP_MAJOR_VERSION >= 7
812            psKey = STR_INIT( *ini, strlen( *ini ), 1 );
813            zend_alter_ini_entry( psKey,
814                                (char *)*(ini+1), strlen( *(ini+1) ),
815                                PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
816            STR_RELEASE( psKey );
817#else
818            zend_alter_ini_entry( (char *)*ini, strlen( *ini )+1,
819                                (char *)*(ini+1), strlen( *(ini+1) ),
820                                PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
821#endif
822        }
823
824        while (( p < argend )&&(**p == '-' )) {
825            c = *((*p)+1);
826            ++p;
827            switch( c ) {
828            case 'q':
829                break;
830            case 'i':
831                if (php_request_startup(TSRMLS_C) != FAILURE) {
832                    php_print_info(0xFFFFFFFF TSRMLS_CC);
833#ifdef PHP_OUTPUT_NEWAPI
834                    php_output_end_all(TSRMLS_C);
835#else
836                    php_end_ob_buffers(1 TSRMLS_CC);
837#endif
838                    php_request_shutdown( NULL );
839                    ret = 0;
840                }
841                break;
842            case 'v':
843                if (php_request_startup(TSRMLS_C) != FAILURE) {
844#if ZEND_DEBUG
845                    php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
846#else
847                    php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
848#endif
849#ifdef PHP_OUTPUT_NEWAPI
850                    php_output_end_all(TSRMLS_C);
851#else
852                    php_end_ob_buffers(1 TSRMLS_CC);
853#endif
854                    php_request_shutdown( NULL );
855                    ret = 0;
856                }
857                break;
858            case 'c':
859                ++p;
860            /* fall through */
861            case 's':
862                break;
863            case 'l':
864                source_highlight = 2;
865                break;
866            case 'h':
867            case '?':
868            default:
869                cli_usage(TSRMLS_C);
870                ret = 0;
871                break;
872
873            }
874        }
875        if ( ret == -1 ) {
876            if ( *p ) {
877                zend_file_handle file_handle = {0};
878
879                file_handle.type = ZEND_HANDLE_FP;
880                file_handle.handle.fp = VCWD_FOPEN(*p, "rb");
881
882                if ( file_handle.handle.fp ) {
883                    script_filename = *p;
884                    php_self = *p;
885
886                    SG(request_info).path_translated = estrdup(*p);
887                    SG(request_info).argc = argc - (p - argv);
888                    SG(request_info).argv = p;
889
890                    if (php_request_startup(TSRMLS_C) == FAILURE ) {
891                        fclose( file_handle.handle.fp );
892                        ret = 2;
893                    } else {
894                        if (source_highlight == 1) {
895                            zend_syntax_highlighter_ini syntax_highlighter_ini;
896
897                            php_get_highlight_struct(&syntax_highlighter_ini);
898                            highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini TSRMLS_CC);
899                        } else if (source_highlight == 2) {
900                            file_handle.filename = *p;
901                            file_handle.free_filename = 0;
902                            file_handle.opened_path = NULL;
903                            ret = php_lint_script(&file_handle TSRMLS_CC);
904                            if (ret==SUCCESS) {
905                                zend_printf("No syntax errors detected in %s\n", file_handle.filename);
906                            } else {
907                                zend_printf("Errors parsing %s\n", file_handle.filename);
908                            }
909
910                        } else {
911                            file_handle.filename = *p;
912                            file_handle.free_filename = 0;
913                            file_handle.opened_path = NULL;
914
915                            php_execute_script(&file_handle TSRMLS_CC);
916                            ret = EG(exit_status);
917                       }
918
919                        php_request_shutdown( NULL );
920                    }
921                } else {
922                    php_printf("Could not open input file: %s.\n", *p);
923                }
924            } else {
925                cli_usage(TSRMLS_C);
926            }
927        }
928
929    }zend_end_try();
930
931    php_module_shutdown(TSRMLS_C);
932
933#ifdef ZTS
934    tsrm_shutdown();
935#endif
936    return ret;
937}
938
939static int s_stop;
940void litespeed_cleanup(int signal)
941{
942    s_stop = signal;
943}
944
945
946void start_children( int children )
947{
948    struct sigaction act, old_term, old_quit, old_int, old_usr1;
949    int running = 0;
950    int status;
951    pid_t pid;
952
953    /* Create a process group */
954    setsid();
955
956    /* Set up handler to kill children upon exit */
957    act.sa_flags = 0;
958    act.sa_handler = litespeed_cleanup;
959    if( sigaction( SIGTERM, &act, &old_term ) ||
960        sigaction( SIGINT,  &act, &old_int  ) ||
961        sigaction( SIGUSR1, &act, &old_usr1 ) ||
962        sigaction( SIGQUIT, &act, &old_quit )) {
963        perror( "Can't set signals" );
964        exit( 1 );
965    }
966    s_stop = 0;
967    while( 1 ) {
968        while((!s_stop )&&( running < children )) {
969            pid = fork();
970            switch( pid ) {
971            case 0: /* children process */
972
973                /* don't catch our signals */
974                sigaction( SIGTERM, &old_term, 0 );
975                sigaction( SIGQUIT, &old_quit, 0 );
976                sigaction( SIGINT,  &old_int,  0 );
977                sigaction( SIGUSR1, &old_usr1, 0 );
978                return ;
979            case -1:
980                perror( "php (pre-forking)" );
981                exit( 1 );
982                break;
983            default: /* parent process */
984                running++;
985                break;
986            }
987        }
988        if ( s_stop ) {
989            break;
990        }
991        pid = wait( &status );
992        running--;
993    }
994    kill( -getpgrp(), SIGUSR1 );
995    exit( 0 );
996}
997
998void setArgv0( int argc, char * argv[] )
999{
1000    char * p;
1001    int i;
1002    argv0 = argv[0] + strlen( argv[0] );
1003    p = argv0;
1004    while(( p > argv[0] )&&( p[-1] != '/'))
1005        --p;
1006    if ( p > argv[0] )
1007    {
1008        memmove( argv[0], p, argv0 - p );
1009        memset( argv[0] + ( argv0 - p ), 0, p - argv[0] );
1010        argv0 = argv[0] + (argv0 - p);
1011    }
1012    for( i = 1; i < argc; ++i )
1013    {
1014        memset( argv[i], 0, strlen( argv[i] ) );
1015    }
1016}
1017
1018#include <fcntl.h>
1019int main( int argc, char * argv[] )
1020{
1021    int ret;
1022    int bindFd;
1023
1024    char * php_ini_path = NULL;
1025    char * php_bind     = NULL;
1026    int n;
1027    int climode = 0;
1028    struct timeval tv_req_begin;
1029    struct timeval tv_req_end;
1030    int slow_script_msec = 0;
1031    char time_buf[40];
1032
1033#ifdef HAVE_SIGNAL_H
1034#if defined(SIGPIPE) && defined(SIG_IGN)
1035    signal(SIGPIPE, SIG_IGN);
1036#endif
1037#endif
1038
1039#ifdef ZTS
1040    tsrm_startup(1, 1, 0, NULL);
1041#endif
1042
1043    if (argc > 1 ) {
1044        if ( parse_opt( argc, argv, &climode,
1045                &php_ini_path, &php_bind ) == -1 ) {
1046            return 1;
1047        }
1048    }
1049    if ( climode ) {
1050        lsapi_sapi_module.phpinfo_as_text = 1;
1051    } else {
1052        setArgv0(argc, argv );
1053    }
1054
1055    sapi_startup(&lsapi_sapi_module);
1056
1057#ifdef ZTS
1058    compiler_globals = ts_resource(compiler_globals_id);
1059    executor_globals = ts_resource(executor_globals_id);
1060    core_globals = ts_resource(core_globals_id);
1061    sapi_globals = ts_resource(sapi_globals_id);
1062    tsrm_ls = ts_resource(0);
1063
1064    SG(request_info).path_translated = NULL;
1065#endif
1066
1067    lsapi_sapi_module.executable_location = argv[0];
1068
1069    if ( ignore_php_ini )
1070        lsapi_sapi_module.php_ini_ignore = 1;
1071
1072    if ( php_ini_path ) {
1073        lsapi_sapi_module.php_ini_path_override = php_ini_path;
1074    }
1075
1076
1077    lsapi_sapi_module.ini_defaults = sapi_lsapi_ini_defaults;
1078
1079    if (php_module_startup(&lsapi_sapi_module, &litespeed_module_entry, 1) == FAILURE) {
1080#ifdef ZTS
1081        tsrm_shutdown();
1082#endif
1083        return FAILURE;
1084    }
1085
1086    if ( climode ) {
1087        return cli_main(argc, argv);
1088    }
1089
1090    if ( php_bind ) {
1091        bindFd = LSAPI_CreateListenSock( php_bind, 10 );
1092        if ( bindFd == -1 ) {
1093            fprintf( stderr,
1094                     "Failed to bind socket [%s]: %s\n", php_bind, strerror( errno ) );
1095            exit( 2 );
1096        }
1097        if ( bindFd != 0 ) {
1098            dup2( bindFd, 0 );
1099            close( bindFd );
1100        }
1101    }
1102
1103    LSAPI_Init();
1104
1105    LSAPI_Init_Env_Parameters( NULL );
1106    lsapi_mode = 1;
1107
1108    slow_script_msec = LSAPI_Get_Slow_Req_Msecs();
1109
1110    if ( php_bind ) {
1111        LSAPI_No_Check_ppid();
1112        free( php_bind );
1113        php_bind = NULL;
1114    }
1115
1116    while( LSAPI_Prefork_Accept_r( &g_req ) >= 0 ) {
1117        if ( slow_script_msec ) {
1118            gettimeofday( &tv_req_begin, NULL );
1119        }
1120        ret = processReq(TSRMLS_C);
1121        if ( slow_script_msec ) {
1122            gettimeofday( &tv_req_end, NULL );
1123            n = ((long) tv_req_end.tv_sec - tv_req_begin.tv_sec ) * 1000
1124                + (tv_req_end.tv_usec - tv_req_begin.tv_usec) / 1000;
1125            if ( n > slow_script_msec )
1126            {
1127                strftime( time_buf, 30, "%d/%b/%Y:%H:%M:%S", localtime( &tv_req_end.tv_sec ) );
1128                fprintf( stderr, "[%s] Slow PHP script: %d ms\n  URL: %s %s\n  Query String: %s\n  Script: %s\n",
1129                         time_buf, n,  LSAPI_GetRequestMethod(),
1130                         LSAPI_GetScriptName(), LSAPI_GetQueryString(),
1131                         LSAPI_GetScriptFileName() );
1132
1133            }
1134        }
1135        LSAPI_Finish();
1136        if ( ret ) {
1137            break;
1138        }
1139    }
1140    php_module_shutdown(TSRMLS_C);
1141
1142#ifdef ZTS
1143    tsrm_shutdown();
1144#endif
1145    return ret;
1146}
1147
1148
1149/*   LiteSpeed PHP module starts here */
1150
1151/* {{{ arginfo */
1152ZEND_BEGIN_ARG_INFO(arginfo_litespeed__void, 0)
1153ZEND_END_ARG_INFO()
1154/* }}} */
1155
1156PHP_FUNCTION(litespeed_request_headers);
1157PHP_FUNCTION(litespeed_response_headers);
1158PHP_FUNCTION(apache_get_modules);
1159
1160PHP_MINFO_FUNCTION(litespeed);
1161
1162zend_function_entry litespeed_functions[] = {
1163    PHP_FE(litespeed_request_headers,   arginfo_litespeed__void)
1164    PHP_FE(litespeed_response_headers,  arginfo_litespeed__void)
1165    PHP_FE(apache_get_modules,          arginfo_litespeed__void)
1166    PHP_FALIAS(getallheaders,           litespeed_request_headers,  arginfo_litespeed__void)
1167    PHP_FALIAS(apache_request_headers,  litespeed_request_headers,  arginfo_litespeed__void)
1168    PHP_FALIAS(apache_response_headers, litespeed_response_headers, arginfo_litespeed__void)
1169    {NULL, NULL, NULL}
1170};
1171
1172static PHP_MINIT_FUNCTION(litespeed)
1173{
1174    /* REGISTER_INI_ENTRIES(); */
1175    return SUCCESS;
1176}
1177
1178
1179static PHP_MSHUTDOWN_FUNCTION(litespeed)
1180{
1181    /* UNREGISTER_INI_ENTRIES(); */
1182    return SUCCESS;
1183}
1184
1185zend_module_entry litespeed_module_entry = {
1186    STANDARD_MODULE_HEADER,
1187    "litespeed",
1188    litespeed_functions,
1189    PHP_MINIT(litespeed),
1190    PHP_MSHUTDOWN(litespeed),
1191    NULL,
1192    NULL,
1193    NULL,
1194    NO_VERSION_YET,
1195    STANDARD_MODULE_PROPERTIES
1196};
1197
1198static int add_associate_array( const char * pKey, int keyLen, const char * pValue, int valLen,
1199                         void * arg )
1200{
1201    add_assoc_string_ex( (zval *)arg, (char *)pKey, keyLen+1, (char *)pValue
1202#if PHP_MAJOR_VERSION < 7
1203            , 1
1204#endif
1205    );
1206    return 1;
1207}
1208
1209
1210/* {{{ proto array litespeed_request_headers(void)
1211   Fetch all HTTP request headers */
1212PHP_FUNCTION(litespeed_request_headers)
1213{
1214    /* TODO: */
1215    if (ZEND_NUM_ARGS() > 0) {
1216        WRONG_PARAM_COUNT;
1217    }
1218    array_init(return_value);
1219
1220    LSAPI_ForeachOrgHeader( add_associate_array, return_value );
1221
1222}
1223/* }}} */
1224
1225
1226
1227/* {{{ proto array litespeed_response_headers(void)
1228   Fetch all HTTP response headers */
1229PHP_FUNCTION(litespeed_response_headers)
1230{
1231    sapi_header_struct  *h;
1232    zend_llist_position pos;
1233    char *       p;
1234    int          len;
1235    char         headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
1236
1237    if (ZEND_NUM_ARGS() > 0) {
1238        WRONG_PARAM_COUNT;
1239    }
1240
1241    if (!&SG(sapi_headers).headers) {
1242        RETURN_FALSE;
1243    }
1244    array_init(return_value);
1245
1246    h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos);
1247    while (h) {
1248        if ( h->header_len > 0 ) {
1249            p = strchr( h->header, ':' );
1250            len = p - h->header;
1251            if (( p )&&( len > 0 )) {
1252                memmove( headerBuf, h->header, len );
1253                while( len > 0 && (isspace( headerBuf[len-1])) ) {
1254                    --len;
1255                }
1256                headerBuf[len] = 0;
1257                if ( len ) {
1258                    while( isspace(*++p));
1259                    add_assoc_string_ex(return_value, headerBuf, len+1, p
1260#if PHP_MAJOR_VERSION < 7
1261                                        , 1
1262#endif
1263                    );
1264                }
1265            }
1266        }
1267        h = zend_llist_get_next_ex(&SG(sapi_headers).headers, &pos);
1268    }
1269}
1270
1271/* }}} */
1272
1273
1274/* {{{ proto array apache_get_modules(void)
1275   Fetch all loaded module names  */
1276PHP_FUNCTION(apache_get_modules)
1277{
1278    static const char * mod_names[] =
1279    {
1280        "mod_rewrite", "mod_mime", "mod_headers", "mod_expires", "mod_auth_basic", NULL
1281    };
1282    const char **name = mod_names;
1283    /* TODO: */
1284    if (ZEND_NUM_ARGS() > 0) {
1285        WRONG_PARAM_COUNT;
1286    }
1287    array_init(return_value);
1288    while( *name )
1289    {
1290        add_next_index_string(return_value, *name
1291#if PHP_MAJOR_VERSION < 7
1292                                        , 1
1293#endif
1294        );
1295        ++name;
1296    }
1297}
1298/* }}} */
1299
1300
1301/*
1302 * Local variables:
1303 * tab-width: 4
1304 * c-basic-offset: 4
1305 * End:
1306 * vim600: sw=4 ts=4 fdm=marker
1307 * vim<600: sw=4 ts=4
1308 */
1309
1310
1311