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