1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 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)
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 )
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(void)
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 )
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)) {
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)
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);
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)
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);
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);
354
355        php_register_variable("PHP_SELF", php_self, track_vars_array);
356        php_register_variable("SCRIPT_NAME", php_self, track_vars_array);
357        php_register_variable("SCRIPT_FILENAME", script_filename, track_vars_array);
358        php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array);
359        php_register_variable("DOCUMENT_ROOT", "", track_vars_array);
360
361    }
362}
363/* }}} */
364
365
366/* {{{ sapi_lsapi_read_post
367 */
368static int sapi_lsapi_read_post(char *buffer, uint count_bytes)
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(void)
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)
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();
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)
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.7",
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 int init_request_info( void )
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);
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)
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);
587    return 0;
588
589}
590
591
592static int lsapi_module_main(int show_source)
593{
594    zend_file_handle file_handle = {0};
595
596    if (php_request_startup() == 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);
604    } else {
605        lsapi_execute_script( &file_handle);
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( void )
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(  );
674
675            if ( lsapi_module_main( source_highlight ) == -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( void )
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();
709    php_printf( "%s", usage );
710#ifdef PHP_OUTPUT_NEWAPI
711    php_output_end_all();
712#else
713    php_end_ob_buffers(1);
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#if PHP_MAJOR_VERSION < 7
806        EG(uninitialized_zval_ptr) = NULL;
807#endif
808        for( ini = ini_defaults; *ini; ini+=2 ) {
809#if PHP_MAJOR_VERSION >= 7
810            psKey = STR_INIT( *ini, strlen( *ini ), 1 );
811            zend_alter_ini_entry( psKey,
812                                (char *)*(ini+1), strlen( *(ini+1) ),
813                                PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
814            STR_RELEASE( psKey );
815#else
816            zend_alter_ini_entry( (char *)*ini, strlen( *ini )+1,
817                                (char *)*(ini+1), strlen( *(ini+1) ),
818                                PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
819#endif
820        }
821
822        while (( p < argend )&&(**p == '-' )) {
823            c = *((*p)+1);
824            ++p;
825            switch( c ) {
826            case 'q':
827                break;
828            case 'i':
829                if (php_request_startup() != FAILURE) {
830                    php_print_info(0xFFFFFFFF);
831#ifdef PHP_OUTPUT_NEWAPI
832                    php_output_end_all();
833#else
834                    php_end_ob_buffers(1);
835#endif
836                    php_request_shutdown( NULL );
837                    ret = 0;
838                }
839                break;
840            case 'v':
841                if (php_request_startup() != FAILURE) {
842#if ZEND_DEBUG
843                    php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2014 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
844#else
845                    php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2014 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
846#endif
847#ifdef PHP_OUTPUT_NEWAPI
848                    php_output_end_all();
849#else
850                    php_end_ob_buffers(1);
851#endif
852                    php_request_shutdown( NULL );
853                    ret = 0;
854                }
855                break;
856            case 'c':
857                ++p;
858            /* fall through */
859            case 's':
860                break;
861            case 'l':
862                source_highlight = 2;
863                break;
864            case 'h':
865            case '?':
866            default:
867                cli_usage();
868                ret = 0;
869                break;
870
871            }
872        }
873        if ( ret == -1 ) {
874            if ( *p ) {
875                zend_file_handle file_handle = {0};
876
877                file_handle.type = ZEND_HANDLE_FP;
878                file_handle.handle.fp = VCWD_FOPEN(*p, "rb");
879
880                if ( file_handle.handle.fp ) {
881                    script_filename = *p;
882                    php_self = *p;
883
884                    SG(request_info).path_translated = estrdup(*p);
885                    SG(request_info).argc = argc - (p - argv);
886                    SG(request_info).argv = p;
887
888                    if (php_request_startup() == FAILURE ) {
889                        fclose( file_handle.handle.fp );
890                        ret = 2;
891                    } else {
892                        if (source_highlight == 1) {
893                            zend_syntax_highlighter_ini syntax_highlighter_ini;
894
895                            php_get_highlight_struct(&syntax_highlighter_ini);
896                            highlight_file(SG(request_info).path_translated, &syntax_highlighter_ini);
897                        } else if (source_highlight == 2) {
898                            file_handle.filename = *p;
899                            file_handle.free_filename = 0;
900                            file_handle.opened_path = NULL;
901                            ret = php_lint_script(&file_handle);
902                            if (ret==SUCCESS) {
903                                zend_printf("No syntax errors detected in %s\n", file_handle.filename);
904                            } else {
905                                zend_printf("Errors parsing %s\n", file_handle.filename);
906                            }
907
908                        } else {
909                            file_handle.filename = *p;
910                            file_handle.free_filename = 0;
911                            file_handle.opened_path = NULL;
912
913                            php_execute_script(&file_handle);
914                            ret = EG(exit_status);
915                       }
916
917                        php_request_shutdown( NULL );
918                    }
919                } else {
920                    php_printf("Could not open input file: %s.\n", *p);
921                }
922            } else {
923                cli_usage();
924            }
925        }
926
927    }zend_end_try();
928
929    php_module_shutdown();
930
931#ifdef ZTS
932    tsrm_shutdown();
933#endif
934    return ret;
935}
936
937static int s_stop;
938void litespeed_cleanup(int signal)
939{
940    s_stop = signal;
941}
942
943
944void start_children( int children )
945{
946    struct sigaction act, old_term, old_quit, old_int, old_usr1;
947    int running = 0;
948    int status;
949    pid_t pid;
950
951    /* Create a process group */
952    setsid();
953
954    /* Set up handler to kill children upon exit */
955    act.sa_flags = 0;
956    act.sa_handler = litespeed_cleanup;
957    if( sigaction( SIGTERM, &act, &old_term ) ||
958        sigaction( SIGINT,  &act, &old_int  ) ||
959        sigaction( SIGUSR1, &act, &old_usr1 ) ||
960        sigaction( SIGQUIT, &act, &old_quit )) {
961        perror( "Can't set signals" );
962        exit( 1 );
963    }
964    s_stop = 0;
965    while( 1 ) {
966        while((!s_stop )&&( running < children )) {
967            pid = fork();
968            switch( pid ) {
969            case 0: /* children process */
970
971                /* don't catch our signals */
972                sigaction( SIGTERM, &old_term, 0 );
973                sigaction( SIGQUIT, &old_quit, 0 );
974                sigaction( SIGINT,  &old_int,  0 );
975                sigaction( SIGUSR1, &old_usr1, 0 );
976                return ;
977            case -1:
978                perror( "php (pre-forking)" );
979                exit( 1 );
980                break;
981            default: /* parent process */
982                running++;
983                break;
984            }
985        }
986        if ( s_stop ) {
987            break;
988        }
989        pid = wait( &status );
990        running--;
991    }
992    kill( -getpgrp(), SIGUSR1 );
993    exit( 0 );
994}
995
996void setArgv0( int argc, char * argv[] )
997{
998    char * p;
999    int i;
1000    argv0 = argv[0] + strlen( argv[0] );
1001    p = argv0;
1002    while(( p > argv[0] )&&( p[-1] != '/'))
1003        --p;
1004    if ( p > argv[0] )
1005    {
1006        memmove( argv[0], p, argv0 - p );
1007        memset( argv[0] + ( argv0 - p ), 0, p - argv[0] );
1008        argv0 = argv[0] + (argv0 - p);
1009    }
1010    for( i = 1; i < argc; ++i )
1011    {
1012        memset( argv[i], 0, strlen( argv[i] ) );
1013    }
1014}
1015
1016#include <fcntl.h>
1017int main( int argc, char * argv[] )
1018{
1019    int ret;
1020    int bindFd;
1021
1022    char * php_ini_path = NULL;
1023    char * php_bind     = NULL;
1024    int n;
1025    int climode = 0;
1026    struct timeval tv_req_begin;
1027    struct timeval tv_req_end;
1028    int slow_script_msec = 0;
1029    char time_buf[40];
1030
1031#ifdef HAVE_SIGNAL_H
1032#if defined(SIGPIPE) && defined(SIG_IGN)
1033    signal(SIGPIPE, SIG_IGN);
1034#endif
1035#endif
1036
1037#ifdef ZTS
1038    tsrm_startup(1, 1, 0, NULL);
1039#endif
1040
1041    if (argc > 1 ) {
1042        if ( parse_opt( argc, argv, &climode,
1043                &php_ini_path, &php_bind ) == -1 ) {
1044            return 1;
1045        }
1046    }
1047    if ( climode ) {
1048        lsapi_sapi_module.phpinfo_as_text = 1;
1049    } else {
1050        setArgv0(argc, argv );
1051    }
1052
1053    sapi_startup(&lsapi_sapi_module);
1054
1055#ifdef ZTS
1056    compiler_globals = ts_resource(compiler_globals_id);
1057    executor_globals = ts_resource(executor_globals_id);
1058    core_globals = ts_resource(core_globals_id);
1059    sapi_globals = ts_resource(sapi_globals_id);
1060    tsrm_ls = ts_resource(0);
1061
1062    SG(request_info).path_translated = NULL;
1063#endif
1064
1065    lsapi_sapi_module.executable_location = argv[0];
1066
1067    if ( ignore_php_ini )
1068        lsapi_sapi_module.php_ini_ignore = 1;
1069
1070    if ( php_ini_path ) {
1071        lsapi_sapi_module.php_ini_path_override = php_ini_path;
1072    }
1073
1074
1075    lsapi_sapi_module.ini_defaults = sapi_lsapi_ini_defaults;
1076
1077    if (php_module_startup(&lsapi_sapi_module, &litespeed_module_entry, 1) == FAILURE) {
1078#ifdef ZTS
1079        tsrm_shutdown();
1080#endif
1081        return FAILURE;
1082    }
1083
1084    if ( climode ) {
1085        return cli_main(argc, argv);
1086    }
1087
1088    if ( php_bind ) {
1089        bindFd = LSAPI_CreateListenSock( php_bind, 10 );
1090        if ( bindFd == -1 ) {
1091            fprintf( stderr,
1092                     "Failed to bind socket [%s]: %s\n", php_bind, strerror( errno ) );
1093            exit( 2 );
1094        }
1095        if ( bindFd != 0 ) {
1096            dup2( bindFd, 0 );
1097            close( bindFd );
1098        }
1099    }
1100
1101    LSAPI_Init();
1102
1103    LSAPI_Init_Env_Parameters( NULL );
1104    lsapi_mode = 1;
1105
1106    slow_script_msec = LSAPI_Get_Slow_Req_Msecs();
1107
1108    if ( php_bind ) {
1109        LSAPI_No_Check_ppid();
1110        free( php_bind );
1111        php_bind = NULL;
1112    }
1113
1114    while( LSAPI_Prefork_Accept_r( &g_req ) >= 0 ) {
1115        if ( slow_script_msec ) {
1116            gettimeofday( &tv_req_begin, NULL );
1117        }
1118        ret = processReq();
1119        if ( slow_script_msec ) {
1120            gettimeofday( &tv_req_end, NULL );
1121            n = ((long) tv_req_end.tv_sec - tv_req_begin.tv_sec ) * 1000
1122                + (tv_req_end.tv_usec - tv_req_begin.tv_usec) / 1000;
1123            if ( n > slow_script_msec )
1124            {
1125                strftime( time_buf, 30, "%d/%b/%Y:%H:%M:%S", localtime( &tv_req_end.tv_sec ) );
1126                fprintf( stderr, "[%s] Slow PHP script: %d ms\n  URL: %s %s\n  Query String: %s\n  Script: %s\n",
1127                         time_buf, n,  LSAPI_GetRequestMethod(),
1128                         LSAPI_GetScriptName(), LSAPI_GetQueryString(),
1129                         LSAPI_GetScriptFileName() );
1130
1131            }
1132        }
1133        LSAPI_Finish();
1134        if ( ret ) {
1135            break;
1136        }
1137    }
1138    php_module_shutdown();
1139
1140#ifdef ZTS
1141    tsrm_shutdown();
1142#endif
1143    return ret;
1144}
1145
1146
1147/*   LiteSpeed PHP module starts here */
1148
1149/* {{{ arginfo */
1150ZEND_BEGIN_ARG_INFO(arginfo_litespeed__void, 0)
1151ZEND_END_ARG_INFO()
1152/* }}} */
1153
1154PHP_FUNCTION(litespeed_request_headers);
1155PHP_FUNCTION(litespeed_response_headers);
1156PHP_FUNCTION(apache_get_modules);
1157
1158PHP_MINFO_FUNCTION(litespeed);
1159
1160zend_function_entry litespeed_functions[] = {
1161    PHP_FE(litespeed_request_headers,   arginfo_litespeed__void)
1162    PHP_FE(litespeed_response_headers,  arginfo_litespeed__void)
1163    PHP_FE(apache_get_modules,          arginfo_litespeed__void)
1164    PHP_FALIAS(getallheaders,           litespeed_request_headers,  arginfo_litespeed__void)
1165    PHP_FALIAS(apache_request_headers,  litespeed_request_headers,  arginfo_litespeed__void)
1166    PHP_FALIAS(apache_response_headers, litespeed_response_headers, arginfo_litespeed__void)
1167    {NULL, NULL, NULL}
1168};
1169
1170static PHP_MINIT_FUNCTION(litespeed)
1171{
1172    /* REGISTER_INI_ENTRIES(); */
1173    return SUCCESS;
1174}
1175
1176
1177static PHP_MSHUTDOWN_FUNCTION(litespeed)
1178{
1179    /* UNREGISTER_INI_ENTRIES(); */
1180    return SUCCESS;
1181}
1182
1183zend_module_entry litespeed_module_entry = {
1184    STANDARD_MODULE_HEADER,
1185    "litespeed",
1186    litespeed_functions,
1187    PHP_MINIT(litespeed),
1188    PHP_MSHUTDOWN(litespeed),
1189    NULL,
1190    NULL,
1191    NULL,
1192    NO_VERSION_YET,
1193    STANDARD_MODULE_PROPERTIES
1194};
1195
1196static int add_associate_array( const char * pKey, int keyLen, const char * pValue, int valLen,
1197                         void * arg )
1198{
1199    add_assoc_string_ex( (zval *)arg, (char *)pKey, keyLen+1, (char *)pValue
1200#if PHP_MAJOR_VERSION < 7
1201            , 1
1202#endif
1203    );
1204    return 1;
1205}
1206
1207
1208/* {{{ proto array litespeed_request_headers(void)
1209   Fetch all HTTP request headers */
1210PHP_FUNCTION(litespeed_request_headers)
1211{
1212    /* TODO: */
1213    if (ZEND_NUM_ARGS() > 0) {
1214        WRONG_PARAM_COUNT;
1215    }
1216    array_init(return_value);
1217
1218    LSAPI_ForeachOrgHeader( add_associate_array, return_value );
1219
1220}
1221/* }}} */
1222
1223
1224
1225/* {{{ proto array litespeed_response_headers(void)
1226   Fetch all HTTP response headers */
1227PHP_FUNCTION(litespeed_response_headers)
1228{
1229    sapi_header_struct  *h;
1230    zend_llist_position pos;
1231    char *       p;
1232    int          len;
1233    char         headerBuf[SAPI_LSAPI_MAX_HEADER_LENGTH];
1234
1235    if (ZEND_NUM_ARGS() > 0) {
1236        WRONG_PARAM_COUNT;
1237    }
1238
1239    if (!&SG(sapi_headers).headers) {
1240        RETURN_FALSE;
1241    }
1242    array_init(return_value);
1243
1244    h = zend_llist_get_first_ex(&SG(sapi_headers).headers, &pos);
1245    while (h) {
1246        if ( h->header_len > 0 ) {
1247            p = strchr( h->header, ':' );
1248            len = p - h->header;
1249            if (( p )&&( len > 0 )) {
1250                memmove( headerBuf, h->header, len );
1251                while( len > 0 && (isspace( headerBuf[len-1])) ) {
1252                    --len;
1253                }
1254                headerBuf[len] = 0;
1255                if ( len ) {
1256                    while( isspace(*++p));
1257                    add_assoc_string_ex(return_value, headerBuf, len+1, p
1258#if PHP_MAJOR_VERSION < 7
1259                                        , 1
1260#endif
1261                    );
1262                }
1263            }
1264        }
1265        h = zend_llist_get_next_ex(&SG(sapi_headers).headers, &pos);
1266    }
1267}
1268
1269/* }}} */
1270
1271
1272/* {{{ proto array apache_get_modules(void)
1273   Fetch all loaded module names  */
1274PHP_FUNCTION(apache_get_modules)
1275{
1276    static const char * mod_names[] =
1277    {
1278        "mod_rewrite", "mod_mime", "mod_headers", "mod_expires", NULL
1279    };
1280    const char **name = mod_names;
1281    /* TODO: */
1282    if (ZEND_NUM_ARGS() > 0) {
1283        WRONG_PARAM_COUNT;
1284    }
1285    array_init(return_value);
1286    while( *name )
1287    {
1288        add_next_index_string(return_value, *name
1289#if PHP_MAJOR_VERSION < 7
1290                                        , 1
1291#endif
1292        );
1293        ++name;
1294    }
1295}
1296/* }}} */
1297
1298
1299/*
1300 * Local variables:
1301 * tab-width: 4
1302 * c-basic-offset: 4
1303 * End:
1304 * vim600: sw=4 ts=4 fdm=marker
1305 * vim<600: sw=4 ts=4
1306 */
1307
1308
1309