PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - var/php_gcov/PHP_5_2/sapi/cgi - cgi_main.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 581
Code covered: 64.2 % Executed lines: 373
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 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 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                 :    | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
      16                 :    |          Stig Bakken <ssb@php.net>                                   |
      17                 :    |          Zeev Suraski <zeev@zend.com>                                |
      18                 :    | FastCGI: Ben Mansell <php@slimyhorror.com>                           |
      19                 :    |          Shane Caraveo <shane@caraveo.com>                           |
      20                 :    |          Dmitry Stogov <dmitry@zend.com>                             |
      21                 :    +----------------------------------------------------------------------+
      22                 : */
      23                 : 
      24                 : /* $Id: cgi_main.c 281870 2009-06-09 13:29:39Z dsp $ */
      25                 : 
      26                 : #include "php.h"
      27                 : #include "php_globals.h"
      28                 : #include "php_variables.h"
      29                 : #include "zend_modules.h"
      30                 : 
      31                 : #include "SAPI.h"
      32                 : 
      33                 : #include <stdio.h>
      34                 : #include "php.h"
      35                 : #ifdef PHP_WIN32
      36                 : #include "win32/time.h"
      37                 : #include "win32/signal.h"
      38                 : #include <process.h>
      39                 : #endif
      40                 : #if HAVE_SYS_TIME_H
      41                 : #include <sys/time.h>
      42                 : #endif
      43                 : #if HAVE_UNISTD_H
      44                 : #include <unistd.h>
      45                 : #endif
      46                 : #if HAVE_SIGNAL_H
      47                 : #include <signal.h>
      48                 : #endif
      49                 : #if HAVE_SETLOCALE
      50                 : #include <locale.h>
      51                 : #endif
      52                 : #if HAVE_SYS_TYPES_H
      53                 : #include <sys/types.h>
      54                 : #endif
      55                 : #if HAVE_SYS_WAIT_H
      56                 : #include <sys/wait.h>
      57                 : #endif
      58                 : #include "zend.h"
      59                 : #include "zend_extensions.h"
      60                 : #include "php_ini.h"
      61                 : #include "php_globals.h"
      62                 : #include "php_main.h"
      63                 : #include "fopen_wrappers.h"
      64                 : #include "ext/standard/php_standard.h"
      65                 : #ifdef PHP_WIN32
      66                 : #include <io.h>
      67                 : #include <fcntl.h>
      68                 : #include "win32/php_registry.h"
      69                 : #endif
      70                 : 
      71                 : #ifdef __riscos__
      72                 : #include <unixlib/local.h>
      73                 : int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
      74                 : #endif
      75                 : 
      76                 : #include "zend_compile.h"
      77                 : #include "zend_execute.h"
      78                 : #include "zend_highlight.h"
      79                 : #include "zend_indent.h"
      80                 : 
      81                 : #include "php_getopt.h"
      82                 : 
      83                 : #if PHP_FASTCGI
      84                 : #include "fastcgi.h"
      85                 : 
      86                 : #ifndef PHP_WIN32
      87                 : /* XXX this will need to change later when threaded fastcgi is
      88                 :    implemented.  shane */
      89                 : struct sigaction act, old_term, old_quit, old_int;
      90                 : #endif
      91                 : 
      92                 : static void (*php_php_import_environment_variables)(zval *array_ptr TSRMLS_DC);
      93                 : 
      94                 : #ifndef PHP_WIN32
      95                 : /* these globals used for forking children on unix systems */
      96                 : /**
      97                 :  * Number of child processes that will get created to service requests
      98                 :  */
      99                 : static int children = 0;
     100                 : 
     101                 : /**
     102                 :  * Set to non-zero if we are the parent process
     103                 :  */
     104                 : static int parent = 1;
     105                 : 
     106                 : /* Did parent received exit signals SIG_TERM/SIG_INT/SIG_QUIT */
     107                 : static int exit_signal = 0;
     108                 : 
     109                 : /* Is Parent waiting for children to exit */
     110                 : static int parent_waiting = 0;
     111                 : 
     112                 : /**
     113                 :  * Process group
     114                 :  */
     115                 : static pid_t pgroup;
     116                 : #endif
     117                 : 
     118                 : #endif
     119                 : 
     120                 : #define PHP_MODE_STANDARD       1
     121                 : #define PHP_MODE_HIGHLIGHT      2
     122                 : #define PHP_MODE_INDENT         3
     123                 : #define PHP_MODE_LINT           4
     124                 : #define PHP_MODE_STRIP          5
     125                 : 
     126                 : static char *php_optarg = NULL;
     127                 : static int php_optind = 1;
     128                 : static zend_module_entry cgi_module_entry;
     129                 : 
     130                 : static const opt_struct OPTIONS[] = {
     131                 :         {'a', 0, "interactive"},
     132                 :         {'b', 1, "bindpath"},
     133                 :         {'C', 0, "no-chdir"},
     134                 :         {'c', 1, "php-ini"},
     135                 :         {'d', 1, "define"},
     136                 :         {'e', 0, "profile-info"},
     137                 :         {'f', 1, "file"},
     138                 :         {'h', 0, "help"},
     139                 :         {'i', 0, "info"},
     140                 :         {'l', 0, "syntax-check"},
     141                 :         {'m', 0, "modules"},
     142                 :         {'n', 0, "no-php-ini"},
     143                 :         {'q', 0, "no-header"},
     144                 :         {'s', 0, "syntax-highlight"},
     145                 :         {'s', 0, "syntax-highlighting"},
     146                 :         {'w', 0, "strip"},
     147                 :         {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
     148                 :         {'v', 0, "version"},
     149                 :         {'z', 1, "zend-extension"},
     150                 : #if PHP_FASTCGI
     151                 :         {'T', 1, "timing"},
     152                 : #endif
     153                 :         {'-', 0, NULL} /* end of args */
     154                 : };
     155                 : 
     156                 : typedef struct _php_cgi_globals_struct {
     157                 :         zend_bool rfc2616_headers;
     158                 :         zend_bool nph;
     159                 :         zend_bool check_shebang_line;
     160                 : #if ENABLE_PATHINFO_CHECK
     161                 :         zend_bool fix_pathinfo;
     162                 : #endif
     163                 : #if FORCE_CGI_REDIRECT
     164                 :         zend_bool force_redirect;
     165                 :         char *redirect_status_env;
     166                 : #endif
     167                 : #if PHP_FASTCGI
     168                 :         zend_bool fcgi_logging;
     169                 : # ifdef PHP_WIN32
     170                 :         zend_bool impersonate;
     171                 : # endif
     172                 : #endif
     173                 : } php_cgi_globals_struct;
     174                 : 
     175                 : #ifdef ZTS
     176                 : static int php_cgi_globals_id;
     177                 : #define CGIG(v) TSRMG(php_cgi_globals_id, php_cgi_globals_struct *, v)
     178                 : #else
     179                 : static php_cgi_globals_struct php_cgi_globals;
     180                 : #define CGIG(v) (php_cgi_globals.v)
     181                 : #endif
     182                 : 
     183                 : #ifdef PHP_WIN32
     184                 : #define TRANSLATE_SLASHES(path) \
     185                 :         { \
     186                 :                 char *tmp = path; \
     187                 :                 while (*tmp) { \
     188                 :                         if (*tmp == '\\') *tmp = '/'; \
     189                 :                         tmp++; \
     190                 :                 } \
     191                 :         }
     192                 : #else
     193                 : #define TRANSLATE_SLASHES(path)
     194                 : #endif
     195                 : 
     196                 : static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC)
     197               0 : {
     198               0 :         php_printf("%s\n", module->name);
     199               0 :         return 0;
     200                 : }
     201                 : 
     202                 : static int module_name_cmp(const void *a, const void *b TSRMLS_DC)
     203               0 : {
     204               0 :         Bucket *f = *((Bucket **) a);
     205               0 :         Bucket *s = *((Bucket **) b);
     206                 : 
     207               0 :         return strcasecmp(((zend_module_entry *)f->pData)->name,
     208                 :                                           ((zend_module_entry *)s->pData)->name);
     209                 : }
     210                 : 
     211                 : static void print_modules(TSRMLS_D)
     212               0 : {
     213                 :         HashTable sorted_registry;
     214                 :         zend_module_entry tmp;
     215                 : 
     216               0 :         zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
     217               0 :         zend_hash_copy(&sorted_registry, &module_registry, NULL, &tmp, sizeof(zend_module_entry));
     218               0 :         zend_hash_sort(&sorted_registry, zend_qsort, module_name_cmp, 0 TSRMLS_CC);
     219               0 :         zend_hash_apply_with_argument(&sorted_registry, (apply_func_arg_t) print_module_info, NULL TSRMLS_CC);
     220               0 :         zend_hash_destroy(&sorted_registry);
     221               0 : }
     222                 : 
     223                 : static int print_extension_info(zend_extension *ext, void *arg TSRMLS_DC)
     224               0 : {
     225               0 :         php_printf("%s\n", ext->name);
     226               0 :         return 0;
     227                 : }
     228                 : 
     229                 : static int extension_name_cmp(const zend_llist_element **f,
     230                 :                                                           const zend_llist_element **s TSRMLS_DC)
     231               0 : {
     232               0 :         return strcmp(((zend_extension *)(*f)->data)->name,
     233                 :                                   ((zend_extension *)(*s)->data)->name);
     234                 : }
     235                 : 
     236                 : static void print_extensions(TSRMLS_D)
     237               0 : {
     238                 :         zend_llist sorted_exts;
     239                 : 
     240               0 :         zend_llist_copy(&sorted_exts, &zend_extensions);
     241               0 :         sorted_exts.dtor = NULL;
     242               0 :         zend_llist_sort(&sorted_exts, extension_name_cmp TSRMLS_CC);
     243               0 :         zend_llist_apply_with_argument(&sorted_exts, (llist_apply_with_arg_func_t) print_extension_info, NULL TSRMLS_CC);
     244               0 :         zend_llist_destroy(&sorted_exts);
     245               0 : }
     246                 : 
     247                 : #ifndef STDOUT_FILENO
     248                 : #define STDOUT_FILENO 1
     249                 : #endif
     250                 : 
     251                 : static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC)
     252            2698 : {
     253                 : #ifdef PHP_WRITE_STDOUT
     254                 :         long ret;
     255                 : #else
     256                 :         size_t ret;
     257                 : #endif
     258                 : 
     259                 : #if PHP_FASTCGI
     260                 :         if (fcgi_is_fastcgi()) {
     261                 :                 fcgi_request *request = (fcgi_request*) SG(server_context);
     262                 :                 long ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
     263                 :                 if (ret <= 0) {
     264                 :                         return 0;
     265                 :                 }
     266                 :                 return ret;
     267                 :         }
     268                 : #endif
     269                 : #ifdef PHP_WRITE_STDOUT
     270            2698 :         ret = write(STDOUT_FILENO, str, str_length);
     271            2698 :         if (ret <= 0) return 0;
     272            2698 :         return ret;
     273                 : #else
     274                 :         ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
     275                 :         return ret;
     276                 : #endif
     277                 : }
     278                 : 
     279                 : static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC)
     280            2700 : {
     281            2700 :         const char *ptr = str;
     282            2700 :         uint remaining = str_length;
     283                 :         size_t ret;
     284                 : 
     285            8098 :         while (remaining > 0) {
     286            2698 :                 ret = sapi_cgibin_single_write(ptr, remaining TSRMLS_CC);
     287            2698 :                 if (!ret) {
     288               0 :                         php_handle_aborted_connection();
     289               0 :                         return str_length - remaining;
     290                 :                 }
     291            2698 :                 ptr += ret;
     292            2698 :                 remaining -= ret;
     293                 :         }
     294                 : 
     295            2700 :         return str_length;
     296                 : }
     297                 : 
     298                 : 
     299                 : static void sapi_cgibin_flush(void *server_context)
     300             190 : {
     301                 : #if PHP_FASTCGI
     302                 :         if (fcgi_is_fastcgi()) {
     303                 :                 fcgi_request *request = (fcgi_request*) server_context;
     304                 :                 if (
     305                 : #ifndef PHP_WIN32
     306                 :                 !parent && 
     307                 : #endif
     308                 :                 request && !fcgi_flush(request, 0)) {
     309                 :                         php_handle_aborted_connection();
     310                 :                 }
     311                 :                 return;
     312                 :         }
     313                 : #endif
     314             190 :         if (fflush(stdout) == EOF) {
     315               0 :                 php_handle_aborted_connection();
     316                 :         }
     317             190 : }
     318                 : 
     319                 : #define SAPI_CGI_MAX_HEADER_LENGTH 1024
     320                 : 
     321                 : typedef struct _http_error {
     322                 :   int code;
     323                 :   const char* msg;
     324                 : } http_error;
     325                 : 
     326                 : static const http_error http_error_codes[] = {
     327                 :         {100, "Continue"},
     328                 :         {101, "Switching Protocols"},
     329                 :         {200, "OK"},
     330                 :         {201, "Created"},
     331                 :         {202, "Accepted"},
     332                 :         {203, "Non-Authoritative Information"},
     333                 :         {204, "No Content"},
     334                 :         {205, "Reset Content"},
     335                 :         {206, "Partial Content"},
     336                 :         {300, "Multiple Choices"},
     337                 :         {301, "Moved Permanently"},
     338                 :         {302, "Moved Temporarily"},
     339                 :         {303, "See Other"},
     340                 :         {304, "Not Modified"},
     341                 :         {305, "Use Proxy"},
     342                 :         {400, "Bad Request"},
     343                 :         {401, "Unauthorized"},
     344                 :         {402, "Payment Required"},
     345                 :         {403, "Forbidden"},
     346                 :         {404, "Not Found"},
     347                 :         {405, "Method Not Allowed"},
     348                 :         {406, "Not Acceptable"},
     349                 :         {407, "Proxy Authentication Required"},
     350                 :         {408, "Request Time-out"},
     351                 :         {409, "Conflict"},
     352                 :         {410, "Gone"},
     353                 :         {411, "Length Required"},
     354                 :         {412, "Precondition Failed"},
     355                 :         {413, "Request Entity Too Large"},
     356                 :         {414, "Request-URI Too Large"},
     357                 :         {415, "Unsupported Media Type"},
     358                 :         {500, "Internal Server Error"},
     359                 :         {501, "Not Implemented"},
     360                 :         {502, "Bad Gateway"},
     361                 :         {503, "Service Unavailable"},
     362                 :         {504, "Gateway Time-out"},
     363                 :         {505, "HTTP Version not supported"},
     364                 :         {0,   NULL}
     365                 : };
     366                 : 
     367                 : static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
     368              68 : {
     369                 :         char buf[SAPI_CGI_MAX_HEADER_LENGTH];
     370                 :         sapi_header_struct *h;
     371                 :         zend_llist_position pos;
     372              68 :         zend_bool ignore_status = 0;
     373              68 :         int response_status = SG(sapi_headers).http_response_code;
     374                 : 
     375              68 :         if (SG(request_info).no_headers == 1) {
     376               0 :                 return  SAPI_HEADER_SENT_SUCCESSFULLY;
     377                 :         }
     378                 : 
     379              68 :         if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
     380                 :         {
     381                 :                 int len;
     382               5 :                 zend_bool has_status = 0;
     383                 : 
     384               5 :                 if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
     385                 :                         char *s;
     386               0 :                         len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, "%s\r\n", SG(sapi_headers).http_status_line);
     387               0 :                         if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
     388               0 :                                 response_status = atoi((s + 1));
     389                 :                         }
     390                 : 
     391               0 :                         if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
     392               0 :                                 len = SAPI_CGI_MAX_HEADER_LENGTH;
     393                 :                         }
     394                 : 
     395                 :                 } else {
     396                 :                         char *s;
     397                 : 
     398               8 :                         if (SG(sapi_headers).http_status_line &&
     399                 :                             (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
     400                 :                             (s - SG(sapi_headers).http_status_line) >= 5 &&
     401                 :                             strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0) {
     402               3 :                                 len = slprintf(buf, sizeof(buf), "Status:%s\r\n", s);
     403               3 :                                 response_status = atoi((s + 1));
     404                 :                         } else {
     405               2 :                                 h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
     406               8 :                                 while (h) {
     407               4 :                                         if (h->header_len > sizeof("Status:")-1 &&
     408                 :                                             strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0) {
     409               0 :                                                 has_status = 1;
     410               0 :                                                 break;
     411                 :                                         }
     412               4 :                                         h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     413                 :                                 }
     414               2 :                                 if (!has_status) {
     415               2 :                                         http_error *err = (http_error*)http_error_codes;
     416                 : 
     417              42 :                                         while (err->code != 0) {
     418              40 :                                             if (err->code == SG(sapi_headers).http_response_code) {
     419               2 :                                                         break;
     420                 :                                                 }
     421              38 :                                                 err++;
     422                 :                                         }
     423               2 :                                         if (err->msg) {
     424               2 :                                                 len = slprintf(buf, sizeof(buf), "Status: %d %s\r\n", SG(sapi_headers).http_response_code, err->msg);
     425                 :                                         } else {
     426               0 :                                                 len = slprintf(buf, sizeof(buf), "Status: %d\r\n", SG(sapi_headers).http_response_code);
     427                 :                                         }
     428                 :                                 }
     429                 :                         }
     430                 :                 }
     431               5 :                 if (!has_status) {
     432               5 :                         PHPWRITE_H(buf, len);
     433               5 :                         ignore_status = 1;
     434                 :                 }
     435                 :         }
     436                 : 
     437              68 :         h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
     438             276 :         while (h) {
     439                 :                 /* prevent CRLFCRLF */
     440             140 :                 if (h->header_len) {
     441             142 :                         if (h->header_len > sizeof("Status:")-1 &&
     442                 :                             strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0) {
     443               2 :                             if (!ignore_status) {
     444               1 :                                     ignore_status = 1;
     445               1 :                                         PHPWRITE_H(h->header, h->header_len);
     446               1 :                                         PHPWRITE_H("\r\n", 2);
     447                 :                                 }
     448             138 :                         } else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 && 
     449                 :                                         strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0) {
     450               0 :                                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     451               0 :                                 continue;
     452                 :                         } else {
     453             138 :                                 PHPWRITE_H(h->header, h->header_len);
     454             138 :                                 PHPWRITE_H("\r\n", 2);
     455                 :                         }
     456                 :                 }
     457             140 :                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     458                 :         }
     459              68 :         PHPWRITE_H("\r\n", 2);
     460                 : 
     461              68 :         return SAPI_HEADER_SENT_SUCCESSFULLY;
     462                 : }
     463                 : 
     464                 : #ifndef STDIN_FILENO
     465                 : # define STDIN_FILENO 0
     466                 : #endif
     467                 : 
     468                 : static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
     469             157 : {
     470             157 :         int read_bytes=0, tmp_read_bytes;
     471                 : 
     472             157 :         count_bytes = MIN(count_bytes, (uint) SG(request_info).content_length - SG(read_post_bytes));
     473             351 :         while (read_bytes < count_bytes) {
     474                 : #if PHP_FASTCGI
     475                 :                 if (fcgi_is_fastcgi()) {
     476                 :                         fcgi_request *request = (fcgi_request*) SG(server_context);
     477                 :                         tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
     478                 :                 } else {
     479                 :                         tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
     480                 :                 }
     481                 : #else
     482              37 :                 tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
     483                 : #endif
     484                 : 
     485              37 :                 if (tmp_read_bytes <= 0) {
     486               0 :                         break;
     487                 :                 }
     488              37 :                 read_bytes += tmp_read_bytes;
     489                 :         }
     490             157 :         return read_bytes;
     491                 : }
     492                 : 
     493                 : static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC)
     494             979 : {
     495                 : #if PHP_FASTCGI
     496                 :         /* when php is started by mod_fastcgi, no regular environment
     497                 :            is provided to PHP.  It is always sent to PHP at the start
     498                 :            of a request.  So we have to do our own lookup to get env
     499                 :            vars.  This could probably be faster somehow.  */
     500                 :         if (fcgi_is_fastcgi()) {
     501                 :                 fcgi_request *request = (fcgi_request*) SG(server_context);
     502                 :                 return fcgi_getenv(request, name, name_len);
     503                 :         }
     504                 : #endif
     505                 :         /*  if cgi, or fastcgi and not found in fcgi env
     506                 :                 check the regular environment */
     507             979 :         return getenv(name);
     508                 : }
     509                 : 
     510                 : static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC)
     511              12 : {
     512                 :         int name_len;
     513                 : #if !HAVE_SETENV || !HAVE_UNSETENV
     514                 :         int len;
     515                 :         char *buf;
     516                 : #endif
     517                 : 
     518              12 :         if (!name) {
     519               0 :                 return NULL;
     520                 :         }
     521              12 :         name_len = strlen(name);
     522                 : 
     523                 : #if PHP_FASTCGI
     524                 :         /* when php is started by mod_fastcgi, no regular environment
     525                 :            is provided to PHP.  It is always sent to PHP at the start
     526                 :            of a request.  So we have to do our own lookup to get env
     527                 :            vars.  This could probably be faster somehow.  */
     528                 :         if (fcgi_is_fastcgi()) {
     529                 :                 fcgi_request *request = (fcgi_request*) SG(server_context);
     530                 :                 return fcgi_putenv(request, name, name_len, value);
     531                 :         }
     532                 : #endif
     533                 : #if HAVE_SETENV
     534              12 :         if (value) {
     535               8 :                 setenv(name, value, 1);
     536                 :         }
     537                 : #endif
     538                 : #if HAVE_UNSETENV
     539              12 :         if (!value) {
     540               4 :                 unsetenv(name);
     541                 :         }
     542                 : #endif
     543                 : 
     544                 : #if !HAVE_SETENV || !HAVE_UNSETENV
     545                 :         /*  if cgi, or fastcgi and not found in fcgi env
     546                 :                 check the regular environment 
     547                 :                 this leaks, but it's only cgi anyway, we'll fix
     548                 :                 it for 5.0
     549                 :         */
     550                 :         len = name_len + (value ? strlen(value) : 0) + sizeof("=") + 2;
     551                 :         buf = (char *) malloc(len);
     552                 :         if (buf == NULL) {
     553                 :                 return getenv(name);
     554                 :         }
     555                 : #endif
     556                 : #if !HAVE_SETENV
     557                 :         if (value) {
     558                 :                 len = slprintf(buf, len - 1, "%s=%s", name, value);
     559                 :                 putenv(buf);
     560                 :         }
     561                 : #endif
     562                 : #if !HAVE_UNSETENV
     563                 :         if (!value) {
     564                 :                 len = slprintf(buf, len - 1, "%s=", name);
     565                 :                 putenv(buf);
     566                 :         }
     567                 : #endif
     568              12 :         return getenv(name);
     569                 : }
     570                 : 
     571                 : static char *sapi_cgi_read_cookies(TSRMLS_D)
     572              98 : {
     573              98 :         return sapi_cgibin_getenv((char *) "HTTP_COOKIE", sizeof("HTTP_COOKIE")-1 TSRMLS_CC);
     574                 : }
     575                 : 
     576                 : #if PHP_FASTCGI
     577                 : void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
     578                 : {
     579                 :         if (PG(http_globals)[TRACK_VARS_ENV] &&
     580                 :             array_ptr != PG(http_globals)[TRACK_VARS_ENV] &&
     581                 :             Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
     582                 :             zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV])) > 0) {
     583                 :             zval_dtor(array_ptr);
     584                 :             *array_ptr = *PG(http_globals)[TRACK_VARS_ENV];
     585                 :             INIT_PZVAL(array_ptr);
     586                 :             zval_copy_ctor(array_ptr);
     587                 :             return;
     588                 :         } else if (PG(http_globals)[TRACK_VARS_SERVER] &&
     589                 :                 array_ptr != PG(http_globals)[TRACK_VARS_SERVER] &&
     590                 :             Z_TYPE_P(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
     591                 :             zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])) > 0) {
     592                 :             zval_dtor(array_ptr);
     593                 :             *array_ptr = *PG(http_globals)[TRACK_VARS_SERVER];
     594                 :             INIT_PZVAL(array_ptr);
     595                 :             zval_copy_ctor(array_ptr);
     596                 :             return;
     597                 :         }
     598                 :         
     599                 :         /* call php's original import as a catch-all */
     600                 :         php_php_import_environment_variables(array_ptr TSRMLS_CC);
     601                 : 
     602                 :         if (fcgi_is_fastcgi()) {
     603                 :                 fcgi_request *request = (fcgi_request*) SG(server_context);
     604                 :                 HashPosition pos;
     605                 :                 int magic_quotes_gpc = PG(magic_quotes_gpc);
     606                 :                 char *var, **val;
     607                 :                 uint var_len;
     608                 :                 ulong idx;
     609                 :                 int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
     610                 : 
     611                 :                 /* turn off magic_quotes while importing environment variables */
     612                 :                 PG(magic_quotes_gpc) = 0;
     613                 :                 for (zend_hash_internal_pointer_reset_ex(&request->env, &pos);
     614                 :                      zend_hash_get_current_key_ex(&request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING &&
     615                 :                      zend_hash_get_current_data_ex(&request->env, (void **) &val, &pos) == SUCCESS;
     616                 :                      zend_hash_move_forward_ex(&request->env, &pos)) {
     617                 :                         unsigned int new_val_len;
     618                 :                         if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) {
     619                 :                                 php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC);
     620                 :                         }
     621                 :                 }
     622                 :                 PG(magic_quotes_gpc) = magic_quotes_gpc;
     623                 :         }
     624                 : }
     625                 : #endif
     626                 : 
     627                 : static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC)
     628              95 : {
     629                 :         unsigned int php_self_len;
     630                 :         char *php_self;
     631                 : 
     632                 :         /* In CGI mode, we consider the environment to be a part of the server
     633                 :          * variables
     634                 :          */
     635              95 :         php_import_environment_variables(track_vars_array TSRMLS_CC);
     636                 : 
     637                 : #if ENABLE_PATHINFO_CHECK
     638              95 :         if (CGIG(fix_pathinfo)) {
     639              95 :                 char *script_name   = SG(request_info).request_uri;
     640              95 :                 unsigned int script_name_len = script_name ? strlen(script_name) : 0;
     641              95 :                 char *path_info     = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
     642              95 :                 unsigned int path_info_len = path_info ? strlen(path_info) : 0;
     643                 : 
     644              95 :                 php_self_len = script_name_len + path_info_len;
     645              95 :                 php_self = emalloc(php_self_len + 1);
     646              95 :                 if (script_name) {
     647               0 :                         memcpy(php_self, script_name, script_name_len + 1);
     648                 :                 }
     649              95 :                 if (path_info) {
     650               0 :                         memcpy(php_self + script_name_len, path_info, path_info_len + 1);
     651                 :                 }
     652                 : 
     653                 :                 /* Build the special-case PHP_SELF variable for the CGI version */
     654              95 :                 if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len TSRMLS_CC)) {
     655               0 :                         php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array TSRMLS_CC);
     656                 :                 }
     657              95 :                 efree(php_self);
     658              95 :                 return;
     659                 :         }
     660                 : #endif
     661                 : 
     662               0 :         php_self = SG(request_info).request_uri ? SG(request_info).request_uri : "";
     663               0 :         php_self_len = strlen(php_self);
     664               0 :         if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len TSRMLS_CC)) {
     665               0 :                 php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array TSRMLS_CC);
     666                 :         }
     667                 : }
     668                 : 
     669                 : static void sapi_cgi_log_message(char *message)
     670               0 : {
     671                 : #if PHP_FASTCGI
     672                 :         TSRMLS_FETCH();
     673                 : 
     674                 :         if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) {
     675                 :                 fcgi_request *request;
     676                 :                 
     677                 :                 request = (fcgi_request*) SG(server_context);
     678                 :                 if (request) {                  
     679                 :                         int len = strlen(message);
     680                 :                         char *buf = malloc(len+2);
     681                 : 
     682                 :                         memcpy(buf, message, len);
     683                 :                         memcpy(buf + len, "\n", sizeof("\n"));
     684                 :                         fcgi_write(request, FCGI_STDERR, buf, len+1);
     685                 :                         free(buf);
     686                 :                 } else {
     687                 :                         fprintf(stderr, "%s\n", message);
     688                 :                 }
     689                 :                 /* ignore return code */
     690                 :         } else
     691                 : #endif /* PHP_FASTCGI */
     692               0 :         fprintf(stderr, "%s\n", message);
     693               0 : }
     694                 : 
     695                 : static int sapi_cgi_deactivate(TSRMLS_D)
     696             193 : {
     697                 :         /* flush only when SAPI was started. The reasons are:
     698                 :                 1. SAPI Deactivate is called from two places: module init and request shutdown
     699                 :                 2. When the first call occurs and the request is not set up, flush fails on 
     700                 :                         FastCGI.
     701                 :         */
     702             193 :         if (SG(sapi_started)) {
     703              95 :                 sapi_cgibin_flush(SG(server_context));
     704                 :         }
     705             193 :         return SUCCESS;
     706                 : }
     707                 : 
     708                 : static int php_cgi_startup(sapi_module_struct *sapi_module)
     709              98 : {
     710              98 :         if (php_module_startup(sapi_module, &cgi_module_entry, 1) == FAILURE) {
     711               0 :                 return FAILURE;
     712                 :         }
     713              98 :         return SUCCESS;
     714                 : }
     715                 : 
     716                 : 
     717                 : /* {{{ sapi_module_struct cgi_sapi_module
     718                 :  */
     719                 : static sapi_module_struct cgi_sapi_module = {
     720                 : #if PHP_FASTCGI
     721                 :         "cgi-fcgi",                                           /* name */
     722                 :         "CGI/FastCGI",                                        /* pretty name */
     723                 : #else
     724                 :         "cgi",                                                        /* name */
     725                 :         "CGI",                                                        /* pretty name */
     726                 : #endif
     727                 : 
     728                 :         php_cgi_startup,                                /* startup */
     729                 :         php_module_shutdown_wrapper,    /* shutdown */
     730                 : 
     731                 :         NULL,                                                   /* activate */
     732                 :         sapi_cgi_deactivate,                    /* deactivate */
     733                 : 
     734                 :         sapi_cgibin_ub_write,                   /* unbuffered write */
     735                 :         sapi_cgibin_flush,                              /* flush */
     736                 :         NULL,                                                   /* get uid */
     737                 :         sapi_cgibin_getenv,                             /* getenv */
     738                 : 
     739                 :         php_error,                                              /* error handler */
     740                 : 
     741                 :         NULL,                                                   /* header handler */
     742                 :         sapi_cgi_send_headers,                  /* send headers handler */
     743                 :         NULL,                                                   /* send header handler */
     744                 : 
     745                 :         sapi_cgi_read_post,                             /* read POST data */
     746                 :         sapi_cgi_read_cookies,                  /* read Cookies */
     747                 : 
     748                 :         sapi_cgi_register_variables,    /* register server variables */
     749                 :         sapi_cgi_log_message,                   /* Log message */
     750                 :         NULL,                                                   /* Get request time */
     751                 : 
     752                 :         STANDARD_SAPI_MODULE_PROPERTIES
     753                 : };
     754                 : /* }}} */
     755                 : 
     756                 : /* {{{ php_cgi_usage
     757                 :  */
     758                 : static void php_cgi_usage(char *argv0)
     759               0 : {
     760                 :         char *prog;
     761                 : 
     762               0 :         prog = strrchr(argv0, '/');
     763               0 :         if (prog) {
     764               0 :                 prog++;
     765                 :         } else {
     766               0 :                 prog = "php";
     767                 :         }
     768                 : 
     769               0 :         php_printf("Usage: %s [-q] [-h] [-s] [-v] [-i] [-f <file>]\n"
     770                 :                            "       %s <file> [args...]\n"
     771                 :                            "  -a               Run interactively\n"
     772                 : #if PHP_FASTCGI
     773                 :                            "  -b <address:port>|<port> Bind Path for external FASTCGI Server mode\n"
     774                 : #endif
     775                 :                            "  -C               Do not chdir to the script's directory\n"
     776                 :                            "  -c <path>|<file> Look for php.ini file in this directory\n"
     777                 :                            "  -n               No php.ini file will be used\n"
     778                 :                            "  -d foo[=bar]     Define INI entry foo with value 'bar'\n"
     779                 :                            "  -e               Generate extended information for debugger/profiler\n"
     780                 :                            "  -f <file>        Parse <file>.  Implies `-q'\n"
     781                 :                            "  -h               This help\n"
     782                 :                            "  -i               PHP information\n"
     783                 :                            "  -l               Syntax check only (lint)\n"
     784                 :                            "  -m               Show compiled in modules\n"
     785                 :                            "  -q               Quiet-mode.  Suppress HTTP Header output.\n"
     786                 :                            "  -s               Display colour syntax highlighted source.\n"
     787                 :                            "  -v               Version number\n"
     788                 :                            "  -w               Display source with stripped comments and whitespace.\n"
     789                 :                            "  -z <file>        Load Zend extension <file>.\n"
     790                 : #if PHP_FASTCGI
     791                 :                            "  -T <count>       Measure execution time of script repeated <count> times.\n"
     792                 : #endif
     793                 :                            ,
     794                 :                            prog, prog);
     795               0 : }
     796                 : /* }}} */
     797                 : 
     798                 : /* {{{ is_valid_path
     799                 :  *
     800                 :  * some server configurations allow '..' to slip through in the
     801                 :  * translated path.   We'll just refuse to handle such a path.
     802                 :  */
     803                 : static int is_valid_path(const char *path)
     804              59 : {
     805                 :         const char *p;
     806                 : 
     807              59 :         if (!path) {
     808               0 :                 return 0;
     809                 :         }
     810              59 :         p = strstr(path, "..");
     811              59 :         if (p) {
     812               0 :                 if ((p == path || IS_SLASH(*(p-1))) &&
     813                 :                     (*(p+2) == 0 || IS_SLASH(*(p+2)))) {
     814               0 :                         return 0;
     815                 :                 }
     816                 :                 while (1) {
     817               0 :                         p = strstr(p+1, "..");
     818               0 :                         if (!p) {
     819               0 :                                 break;
     820                 :                         }
     821               0 :                         if (IS_SLASH(*(p-1)) &&
     822                 :                             (*(p+2) == 0 || IS_SLASH(*(p+2)))) {
     823               0 :                                         return 0;
     824                 :                         }
     825               0 :                 }
     826                 :         }
     827              59 :         return 1;
     828                 : }
     829                 : /* }}} */
     830                 : 
     831                 : /* {{{ init_request_info
     832                 : 
     833                 :   initializes request_info structure
     834                 : 
     835                 :   specificly in this section we handle proper translations
     836                 :   for:
     837                 : 
     838                 :   PATH_INFO
     839                 :         derived from the portion of the URI path following 
     840                 :         the script name but preceding any query data
     841                 :         may be empty
     842                 : 
     843                 :   PATH_TRANSLATED
     844                 :     derived by taking any path-info component of the 
     845                 :         request URI and performing any virtual-to-physical 
     846                 :         translation appropriate to map it onto the server's 
     847                 :         document repository structure
     848                 : 
     849                 :         empty if PATH_INFO is empty
     850                 : 
     851                 :         The env var PATH_TRANSLATED **IS DIFFERENT** than the
     852                 :         request_info.path_translated variable, the latter should
     853                 :         match SCRIPT_FILENAME instead.
     854                 : 
     855                 :   SCRIPT_NAME
     856                 :     set to a URL path that could identify the CGI script
     857                 :         rather than the interpreter.  PHP_SELF is set to this.
     858                 : 
     859                 :   REQUEST_URI
     860                 :     uri section following the domain:port part of a URI
     861                 : 
     862                 :   SCRIPT_FILENAME
     863                 :     The virtual-to-physical translation of SCRIPT_NAME (as per 
     864                 :         PATH_TRANSLATED)
     865                 : 
     866                 :   These settings are documented at
     867                 :   http://cgi-spec.golux.com/
     868                 : 
     869                 : 
     870                 :   Based on the following URL request:
     871                 :   
     872                 :   http://localhost/info.php/test?a=b 
     873                 :  
     874                 :   should produce, which btw is the same as if
     875                 :   we were running under mod_cgi on apache (ie. not
     876                 :   using ScriptAlias directives):
     877                 :  
     878                 :   PATH_INFO=/test
     879                 :   PATH_TRANSLATED=/docroot/test
     880                 :   SCRIPT_NAME=/info.php
     881                 :   REQUEST_URI=/info.php/test?a=b
     882                 :   SCRIPT_FILENAME=/docroot/info.php
     883                 :   QUERY_STRING=a=b
     884                 :  
     885                 :   but what we get is (cgi/mod_fastcgi under apache):
     886                 :   
     887                 :   PATH_INFO=/info.php/test
     888                 :   PATH_TRANSLATED=/docroot/info.php/test
     889                 :   SCRIPT_NAME=/php/php-cgi  (from the Action setting I suppose)
     890                 :   REQUEST_URI=/info.php/test?a=b
     891                 :   SCRIPT_FILENAME=/path/to/php/bin/php-cgi  (Action setting translated)
     892                 :   QUERY_STRING=a=b
     893                 :  
     894                 :   Comments in the code below refer to using the above URL in a request
     895                 : 
     896                 :  */
     897                 : static void init_request_info(TSRMLS_D)
     898              98 : {
     899              98 :         char *env_script_filename = sapi_cgibin_getenv("SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1 TSRMLS_CC);
     900              98 :         char *env_path_translated = sapi_cgibin_getenv("PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1 TSRMLS_CC);
     901              98 :         char *script_path_translated = env_script_filename;
     902                 : 
     903                 : #if !DISCARD_PATH
     904                 :         /* some broken servers do not have script_filename or argv0
     905                 :            an example, IIS configured in some ways.  then they do more
     906                 :            broken stuff and set path_translated to the cgi script location */
     907              98 :         if (!script_path_translated && env_path_translated) {
     908               0 :                 script_path_translated = env_path_translated;
     909                 :         }
     910                 : #endif
     911                 : 
     912                 :         /* initialize the defaults */
     913              98 :         SG(request_info).path_translated = NULL;
     914              98 :         SG(request_info).request_method = NULL;
     915              98 :         SG(request_info).proto_num = 1000;
     916              98 :         SG(request_info).query_string = NULL;
     917              98 :         SG(request_info).request_uri = NULL;
     918              98 :         SG(request_info).content_type = NULL;
     919              98 :         SG(request_info).content_length = 0;
     920              98 :         SG(sapi_headers).http_response_code = 200;
     921                 : 
     922                 :         /* script_path_translated being set is a good indication that
     923                 :            we are running in a cgi environment, since it is always
     924                 :            null otherwise.  otherwise, the filename
     925                 :            of the script will be retreived later via argc/argv */
     926              98 :         if (script_path_translated) {
     927                 :                 const char *auth;
     928              59 :                 char *content_length = sapi_cgibin_getenv("CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1 TSRMLS_CC);
     929              59 :                 char *content_type = sapi_cgibin_getenv("CONTENT_TYPE", sizeof("CONTENT_TYPE")-1 TSRMLS_CC);
     930              59 :                 char *env_path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
     931              59 :                 char *env_script_name = sapi_cgibin_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
     932                 : #if ENABLE_PATHINFO_CHECK
     933                 :                 struct stat st;
     934              59 :                 char *env_redirect_url = sapi_cgibin_getenv("REDIRECT_URL", sizeof("REDIRECT_URL")-1 TSRMLS_CC);
     935              59 :                 char *env_document_root = sapi_cgibin_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
     936                 :                 int script_path_translated_len;
     937                 : 
     938                 :                 /* Hack for buggy IIS that sets incorrect PATH_INFO */
     939              59 :                 char *env_server_software = sapi_cgibin_getenv("SERVER_SOFTWARE", sizeof("SERVER_SOFTWARE")-1 TSRMLS_CC);
     940              59 :                 if (env_server_software &&
     941                 :                     env_script_name &&
     942                 :                     env_path_info &&
     943                 :                     strncmp(env_server_software, "Microsoft-IIS", sizeof("Microsoft-IIS")-1) == 0 &&
     944                 :                     strncmp(env_path_info, env_script_name, strlen(env_script_name)) == 0) {
     945               0 :                         env_path_info = _sapi_cgibin_putenv("ORIG_PATH_INFO", env_path_info TSRMLS_CC);
     946               0 :                     env_path_info += strlen(env_script_name);
     947               0 :                     if (*env_path_info == 0) {
     948               0 :                         env_path_info = NULL;
     949                 :                     }
     950               0 :                         env_path_info = _sapi_cgibin_putenv("PATH_INFO", env_path_info TSRMLS_CC);
     951                 :                 }
     952                 : 
     953              59 :                 if (CGIG(fix_pathinfo)) {
     954              59 :                         char *real_path = NULL;
     955              59 :                         char *orig_path_translated = env_path_translated;
     956              59 :                         char *orig_path_info = env_path_info;
     957              59 :                         char *orig_script_name = env_script_name;
     958              59 :                         char *orig_script_filename = env_script_filename;
     959                 : 
     960              59 :                         if (!env_document_root && PG(doc_root)) {
     961               0 :                                 env_document_root = _sapi_cgibin_putenv("DOCUMENT_ROOT", PG(doc_root) TSRMLS_CC);
     962                 :                                 /* fix docroot */
     963                 :                                 TRANSLATE_SLASHES(env_document_root);
     964                 :                         }
     965                 : 
     966              59 :                         if (env_path_translated != NULL && env_redirect_url != NULL &&
     967                 :                             env_path_translated != script_path_translated &&
     968                 :                             strcmp(env_path_translated, script_path_translated) != 0) {
     969                 :                                 /* 
     970                 :                                    pretty much apache specific.  If we have a redirect_url
     971                 :                                    then our script_filename and script_name point to the
     972                 :                                    php executable
     973                 :                                 */
     974               0 :                                 script_path_translated = env_path_translated;
     975                 :                                 /* we correct SCRIPT_NAME now in case we don't have PATH_INFO */
     976               0 :                                 env_script_name = env_redirect_url;
     977                 :                         }
     978                 : 
     979                 : #ifdef __riscos__
     980                 :                         /* Convert path to unix format*/
     981                 :                         __riscosify_control |= __RISCOSIFY_DONT_CHECK_DIR;
     982                 :                         script_path_translated = __unixify(script_path_translated, 0, NULL, 1, 0);
     983                 : #endif
     984                 :                         
     985                 :                         /*
     986                 :                          * if the file doesn't exist, try to extract PATH_INFO out
     987                 :                          * of it by stat'ing back through the '/'
     988                 :                          * this fixes url's like /info.php/test
     989                 :                          */
     990              63 :                         if (script_path_translated &&
     991                 :                                 (script_path_translated_len = strlen(script_path_translated)) > 0 &&
     992                 :                                 (script_path_translated[script_path_translated_len-1] == '/' ||
     993                 : #ifdef PHP_WIN32
     994                 :                                  script_path_translated[script_path_translated_len-1] == '\\' ||
     995                 : #endif
     996                 :                              (real_path = tsrm_realpath(script_path_translated, NULL TSRMLS_CC)) == NULL)) {
     997               4 :                                 char *pt = estrndup(script_path_translated, script_path_translated_len);
     998               4 :                                 int len = script_path_translated_len;
     999                 :                                 char *ptr;
    1000                 : 
    1001               8 :                                 while ((ptr = strrchr(pt, '/')) || (ptr = strrchr(pt, '\\'))) {
    1002               4 :                                         *ptr = 0;
    1003               4 :                                         if (stat(pt, &st) == 0 && S_ISREG(st.st_mode)) {
    1004                 :                                                 /*
    1005                 :                                                  * okay, we found the base script!
    1006                 :                                                  * work out how many chars we had to strip off;
    1007                 :                                                  * then we can modify PATH_INFO
    1008                 :                                                  * accordingly
    1009                 :                                                  *
    1010                 :                                                  * we now have the makings of
    1011                 :                                                  * PATH_INFO=/test
    1012                 :                                                  * SCRIPT_FILENAME=/docroot/info.php
    1013                 :                                                  *
    1014                 :                                                  * we now need to figure out what docroot is.
    1015                 :                                                  * if DOCUMENT_ROOT is set, this is easy, otherwise,
    1016                 :                                                  * we have to play the game of hide and seek to figure
    1017                 :                                                  * out what SCRIPT_NAME should be
    1018                 :                                                  */
    1019               4 :                                                 int slen = len - strlen(pt);
    1020               4 :                                                 int pilen = env_path_info ? strlen(env_path_info) : 0;
    1021               4 :                                                 char *path_info = env_path_info ? env_path_info + pilen - slen : NULL;
    1022                 : 
    1023               4 :                                                 if (orig_path_info != path_info) {
    1024               0 :                                                         if (orig_path_info) {
    1025                 :                                                                 char old;
    1026                 : 
    1027               0 :                                                                 _sapi_cgibin_putenv("ORIG_PATH_INFO", orig_path_info TSRMLS_CC);
    1028               0 :                                                                 old = path_info[0];
    1029               0 :                                                                 path_info[0] = 0;
    1030               0 :                                                                 if (!orig_script_name ||
    1031                 :                                                                         strcmp(orig_script_name, env_path_info) != 0) {
    1032               0 :                                                                         if (orig_script_name) {
    1033               0 :                                                                                 _sapi_cgibin_putenv("ORIG_SCRIPT_NAME", orig_script_name TSRMLS_CC);
    1034                 :                                                                         }
    1035               0 :                                                                         SG(request_info).request_uri = _sapi_cgibin_putenv("SCRIPT_NAME", env_path_info TSRMLS_CC);
    1036                 :                                                                 } else {
    1037               0 :                                                                         SG(request_info).request_uri = orig_script_name;
    1038                 :                                                                 }
    1039               0 :                                                                 path_info[0] = old;
    1040                 :                                                         }
    1041               0 :                                                         env_path_info = _sapi_cgibin_putenv("PATH_INFO", path_info TSRMLS_CC);
    1042                 :                                                 }
    1043               4 :                                                 if (!orig_script_filename ||
    1044                 :                                                         strcmp(orig_script_filename, pt) != 0) {
    1045               4 :                                                         if (orig_script_filename) {
    1046               4 :                                                                 _sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME", orig_script_filename TSRMLS_CC);
    1047                 :                                                         }
    1048               4 :                                                         script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME", pt TSRMLS_CC);
    1049                 :                                                 }
    1050                 :                                                 TRANSLATE_SLASHES(pt);
    1051                 : 
    1052                 :                                                 /* figure out docroot
    1053                 :                                                    SCRIPT_FILENAME minus SCRIPT_NAME
    1054                 :                                                 */
    1055                 : 
    1056               4 :                                                 if (env_document_root) {
    1057               0 :                                                         int l = strlen(env_document_root);
    1058               0 :                                                         int path_translated_len = 0;
    1059               0 :                                                         char *path_translated = NULL;
    1060                 :                                                         
    1061               0 :                                                         if (l && env_document_root[l - 1] == '/') {
    1062               0 :                                                                 --l;
    1063                 :                                                         }
    1064                 : 
    1065                 :                                                         /* we have docroot, so we should have:
    1066                 :                                                          * DOCUMENT_ROOT=/docroot
    1067                 :                                                          * SCRIPT_FILENAME=/docroot/info.php
    1068                 :                                                          */
    1069                 : 
    1070                 :                                                         /* PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO */
    1071               0 :                                                         path_translated_len = l + (env_path_info ? strlen(env_path_info) : 0);
    1072               0 :                                                         path_translated = (char *) emalloc(path_translated_len + 1);
    1073               0 :                                                         memcpy(path_translated, env_document_root, l);
    1074               0 :                                                         if (env_path_info) {
    1075               0 :                                                                 memcpy(path_translated + l, env_path_info, (path_translated_len - l));
    1076                 :                                                         }
    1077               0 :                                                         path_translated[path_translated_len] = '\0';
    1078               0 :                                                         if (orig_path_translated) {
    1079               0 :                                                                 _sapi_cgibin_putenv("ORIG_PATH_TRANSLATED", orig_path_translated TSRMLS_CC);
    1080                 :                                                         }
    1081               0 :                                                         env_path_translated = _sapi_cgibin_putenv("PATH_TRANSLATED", path_translated TSRMLS_CC);
    1082               0 :                                                         efree(path_translated);
    1083               4 :                                                 } else if (env_script_name && 
    1084                 :                                                                    strstr(pt, env_script_name)
    1085                 :                                                 ) {
    1086                 :                                                         /* PATH_TRANSLATED = PATH_TRANSLATED - SCRIPT_NAME + PATH_INFO */
    1087               0 :                                                         int ptlen = strlen(pt) - strlen(env_script_name);
    1088               0 :                                                         int path_translated_len = ptlen + (env_path_info ? strlen(env_path_info) : 0);
    1089               0 :                                                         char *path_translated = NULL;
    1090                 : 
    1091               0 :                                                         path_translated = (char *) emalloc(path_translated_len + 1);
    1092               0 :                                                         memcpy(path_translated, pt, ptlen);
    1093               0 :                                                         if (env_path_info) {
    1094               0 :                                                                 memcpy(path_translated + ptlen, env_path_info, path_translated_len - ptlen);
    1095                 :                                                         }
    1096               0 :                                                         path_translated[path_translated_len] = '\0';
    1097               0 :                                                         if (orig_path_translated) {
    1098               0 :                                                                 _sapi_cgibin_putenv("ORIG_PATH_TRANSLATED", orig_path_translated TSRMLS_CC);
    1099                 :                                                         }
    1100               0 :                                                         env_path_translated = _sapi_cgibin_putenv("PATH_TRANSLATED", path_translated TSRMLS_CC);
    1101               0 :                                                         efree(path_translated);
    1102                 :                                                 }
    1103               4 :                                                 break;
    1104                 :                                         }
    1105                 :                                 }
    1106               4 :                                 if (!ptr) {
    1107                 :                                         /*
    1108                 :                                          * if we stripped out all the '/' and still didn't find
    1109                 :                                          * a valid path... we will fail, badly. of course we would
    1110                 :                                          * have failed anyway... we output 'no input file' now.
    1111                 :                                          */
    1112               0 :                                         if (orig_script_filename) {
    1113               0 :                                                 _sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME", orig_script_filename TSRMLS_CC);
    1114                 :                                         }
    1115               0 :                                         script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME", NULL TSRMLS_CC);
    1116               0 :                                         SG(sapi_headers).http_response_code = 404;
    1117                 :                                 }
    1118               4 :                                 if (!SG(request_info).request_uri) {
    1119               8 :                                         if (!orig_script_name ||
    1120                 :                                                 strcmp(orig_script_name, env_script_name) != 0) {
    1121               4 :                                                 if (orig_script_name) {
    1122               0 :                                                         _sapi_cgibin_putenv("ORIG_SCRIPT_NAME", orig_script_name TSRMLS_CC);
    1123                 :                                                 }
    1124               4 :                                                 SG(request_info).request_uri = _sapi_cgibin_putenv("SCRIPT_NAME", env_script_name TSRMLS_CC);
    1125                 :                                         } else {
    1126               0 :                                                 SG(request_info).request_uri = orig_script_name;
    1127                 :                                         }
    1128                 :                                 }       
    1129               4 :                                 if (pt) {
    1130               4 :                                         efree(pt);
    1131                 :                                 }
    1132               4 :                                 if (is_valid_path(script_path_translated)) {
    1133               4 :                                         SG(request_info).path_translated = estrdup(script_path_translated);
    1134                 :                                 }
    1135                 :                         } else {
    1136                 :                                 /* make sure path_info/translated are empty */
    1137              55 :                                 if (!orig_script_filename ||
    1138                 :                                         (script_path_translated != orig_script_filename &&
    1139                 :                                         strcmp(script_path_translated, orig_script_filename) != 0)) {
    1140               0 :                                         if (orig_script_filename) {
    1141               0 :                                                 _sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME", orig_script_filename TSRMLS_CC);
    1142                 :                                         }
    1143               0 :                                         script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME", script_path_translated TSRMLS_CC);
    1144                 :                                 }
    1145              55 :                                 if (env_redirect_url) {
    1146               0 :                                         if (orig_path_info) {
    1147               0 :                                                 _sapi_cgibin_putenv("ORIG_PATH_INFO", orig_path_info TSRMLS_CC);
    1148               0 :                                                 _sapi_cgibin_putenv("PATH_INFO", NULL TSRMLS_CC);
    1149                 :                                         }
    1150               0 :                                         if (orig_path_translated) {
    1151               0 :                                                 _sapi_cgibin_putenv("ORIG_PATH_TRANSLATED", orig_path_translated TSRMLS_CC);
    1152               0 :                                                 _sapi_cgibin_putenv("PATH_TRANSLATED", NULL TSRMLS_CC);
    1153                 :                                         }
    1154                 :                                 }
    1155              55 :                                 if (env_script_name != orig_script_name) {
    1156               0 :                                         if (orig_script_name) {
    1157               0 :                                                 _sapi_cgibin_putenv("ORIG_SCRIPT_NAME", orig_script_name TSRMLS_CC);
    1158                 :                                         }
    1159               0 :                                         SG(request_info).request_uri = _sapi_cgibin_putenv("SCRIPT_NAME", env_script_name TSRMLS_CC);
    1160                 :                                 } else {
    1161              55 :                                         SG(request_info).request_uri = env_script_name;
    1162                 :                                 }
    1163              55 :                                 if (is_valid_path(script_path_translated)) {
    1164              55 :                                         SG(request_info).path_translated = estrdup(script_path_translated);
    1165                 :                                 }
    1166              55 :                                 free(real_path);
    1167                 :                         }
    1168                 :                 } else {
    1169                 : #endif
    1170                 :                         /* pre 4.3 behaviour, shouldn't be used but provides BC */
    1171               0 :                         if (env_path_info) {
    1172               0 :                                 SG(request_info).request_uri = env_path_info;
    1173                 :                         } else {
    1174               0 :                                 SG(request_info).request_uri = env_script_name;
    1175                 :                         }
    1176                 : #if !DISCARD_PATH
    1177               0 :                         if (env_path_translated) {
    1178               0 :                                 script_path_translated = env_path_translated;
    1179                 :                         }
    1180                 : #endif
    1181               0 :                         if (is_valid_path(script_path_translated)) {
    1182               0 :                                 SG(request_info).path_translated = estrdup(script_path_translated);
    1183                 :                         }
    1184                 : #if ENABLE_PATHINFO_CHECK
    1185                 :                 }
    1186                 : #endif
    1187              59 :                 SG(request_info).request_method = sapi_cgibin_getenv("REQUEST_METHOD", sizeof("REQUEST_METHOD")-1 TSRMLS_CC);
    1188                 :                 /* FIXME - Work out proto_num here */
    1189              59 :                 SG(request_info).query_string = sapi_cgibin_getenv("QUERY_STRING", sizeof("QUERY_STRING")-1 TSRMLS_CC);
    1190              59 :                 SG(request_info).content_type = (content_type ? content_type : "" );
    1191              59 :                 SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
    1192                 :                 
    1193                 :                 /* The CGI RFC allows servers to pass on unvalidated Authorization data */
    1194              59 :                 auth = sapi_cgibin_getenv("HTTP_AUTHORIZATION", sizeof("HTTP_AUTHORIZATION")-1 TSRMLS_CC);
    1195              59 :                 php_handle_auth_data(auth TSRMLS_CC);
    1196                 :         }
    1197              98 : }
    1198                 : /* }}} */
    1199                 : 
    1200                 : #if PHP_FASTCGI && !defined(PHP_WIN32)
    1201                 : /**
    1202                 :  * Clean up child processes upon exit
    1203                 :  */
    1204                 : void fastcgi_cleanup(int signal)
    1205                 : {
    1206                 : #ifdef DEBUG_FASTCGI
    1207                 :         fprintf(stderr, "FastCGI shutdown, pid %d\n", getpid());
    1208                 : #endif
    1209                 : 
    1210                 :         sigaction(SIGTERM, &old_term, 0);
    1211                 : 
    1212                 :         /* Kill all the processes in our process group */
    1213                 :         kill(-pgroup, SIGTERM);
    1214                 : 
    1215                 :         if (parent && parent_waiting) {
    1216                 :                 exit_signal = 1;
    1217                 :         } else {
    1218                 :                 exit(0);
    1219                 :         }
    1220                 : }
    1221                 : #endif
    1222                 : 
    1223                 : PHP_INI_BEGIN()
    1224                 :         STD_PHP_INI_ENTRY("cgi.rfc2616_headers",     "0",  PHP_INI_ALL,    OnUpdateBool,   rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
    1225                 :         STD_PHP_INI_ENTRY("cgi.nph",                 "0",  PHP_INI_ALL,    OnUpdateBool,   nph, php_cgi_globals_struct, php_cgi_globals)
    1226                 :         STD_PHP_INI_ENTRY("cgi.check_shebang_line",  "1",  PHP_INI_SYSTEM, OnUpdateBool,   check_shebang_line, php_cgi_globals_struct, php_cgi_globals)
    1227                 : #if FORCE_CGI_REDIRECT
    1228                 :         STD_PHP_INI_ENTRY("cgi.force_redirect",      "1",  PHP_INI_SYSTEM, OnUpdateBool,   force_redirect, php_cgi_globals_struct, php_cgi_globals)
    1229                 :         STD_PHP_INI_ENTRY("cgi.redirect_status_env", NULL, PHP_INI_SYSTEM, OnUpdateString, redirect_status_env, php_cgi_globals_struct, php_cgi_globals)
    1230                 : #endif
    1231                 : #if ENABLE_PATHINFO_CHECK
    1232                 :         STD_PHP_INI_ENTRY("cgi.fix_pathinfo",        "1",  PHP_INI_SYSTEM, OnUpdateBool,   fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
    1233                 : #endif
    1234                 : #if PHP_FASTCGI
    1235                 :         STD_PHP_INI_ENTRY("fastcgi.logging",         "1",  PHP_INI_SYSTEM, OnUpdateBool,   fcgi_logging, php_cgi_globals_struct, php_cgi_globals)
    1236                 : # ifdef PHP_WIN32
    1237                 :         STD_PHP_INI_ENTRY("fastcgi.impersonate",     "0",  PHP_INI_SYSTEM, OnUpdateBool,   impersonate, php_cgi_globals_struct, php_cgi_globals)
    1238                 : # endif
    1239                 : #endif
    1240                 : PHP_INI_END()
    1241                 : 
    1242                 : /* {{{ php_cgi_globals_ctor
    1243                 :  */
    1244                 : static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals TSRMLS_DC)
    1245              98 : {
    1246              98 :         php_cgi_globals->rfc2616_headers = 0;
    1247              98 :         php_cgi_globals->nph = 0;
    1248              98 :         php_cgi_globals->check_shebang_line = 1;
    1249                 : #if FORCE_CGI_REDIRECT
    1250                 :         php_cgi_globals->force_redirect = 1;
    1251                 :         php_cgi_globals->redirect_status_env = NULL;
    1252                 : #endif
    1253                 : #if ENABLE_PATHINFO_CHECK
    1254              98 :         php_cgi_globals->fix_pathinfo = 1;
    1255                 : #endif
    1256                 : #if PHP_FASTCGI
    1257                 :         php_cgi_globals->fcgi_logging = 1;
    1258                 : # ifdef PHP_WIN32
    1259                 :         php_cgi_globals->impersonate = 0;
    1260                 : # endif
    1261                 : #endif
    1262              98 : }
    1263                 : /* }}} */
    1264                 : 
    1265                 : /* {{{ PHP_MINIT_FUNCTION
    1266                 :  */
    1267                 : static PHP_MINIT_FUNCTION(cgi)
    1268              98 : {
    1269                 : #ifdef ZTS
    1270                 :         ts_allocate_id(&php_cgi_globals_id, sizeof(php_cgi_globals_struct), (ts_allocate_ctor) php_cgi_globals_ctor, NULL);
    1271                 : #else
    1272              98 :         php_cgi_globals_ctor(&php_cgi_globals TSRMLS_CC);
    1273                 : #endif
    1274              98 :         REGISTER_INI_ENTRIES();
    1275              98 :         return SUCCESS;
    1276                 : }
    1277                 : /* }}} */
    1278                 : 
    1279                 : /* {{{ PHP_MSHUTDOWN_FUNCTION
    1280                 :  */
    1281                 : static PHP_MSHUTDOWN_FUNCTION(cgi)
    1282              95 : {
    1283              95 :         UNREGISTER_INI_ENTRIES();
    1284              95 :         return SUCCESS;
    1285                 : }
    1286                 : /* }}} */
    1287                 : 
    1288                 : /* {{{ PHP_MINFO_FUNCTION
    1289                 :  */
    1290                 : static PHP_MINFO_FUNCTION(cgi)
    1291               1 : {
    1292               1 :         DISPLAY_INI_ENTRIES();
    1293               1 : }
    1294                 : /* }}} */
    1295                 : 
    1296                 : static zend_module_entry cgi_module_entry = {
    1297                 :         STANDARD_MODULE_HEADER,
    1298                 : #if PHP_FASTCGI
    1299                 :         "cgi-fcgi",
    1300                 : #else
    1301                 :         "cgi",
    1302                 : #endif
    1303                 :         NULL, 
    1304                 :         PHP_MINIT(cgi), 
    1305                 :         PHP_MSHUTDOWN(cgi), 
    1306                 :         NULL, 
    1307                 :         NULL, 
    1308                 :         PHP_MINFO(cgi), 
    1309                 :         NO_VERSION_YET,
    1310                 :         STANDARD_MODULE_PROPERTIES
    1311                 : };
    1312                 : 
    1313                 : /* {{{ main
    1314                 :  */
    1315                 : int main(int argc, char *argv[])
    1316              98 : {
    1317              98 :         int free_query_string = 0;
    1318              98 :         int exit_status = SUCCESS;
    1319              98 :         int cgi = 0, c, i, len;
    1320                 :         zend_file_handle file_handle;
    1321              98 :         int retval = FAILURE;
    1322                 :         char *s;
    1323                 : /* temporary locals */
    1324              98 :         int behavior = PHP_MODE_STANDARD;
    1325              98 :         int no_headers = 0;
    1326              98 :         int orig_optind = php_optind;
    1327              98 :         char *orig_optarg = php_optarg;
    1328              98 :         char *script_file = NULL;
    1329              98 :         int ini_entries_len = 0;
    1330                 : 
    1331                 : /* end of temporary locals */
    1332                 : #ifdef ZTS
    1333                 :         void ***tsrm_ls;
    1334                 : #endif
    1335                 : 
    1336                 : #if PHP_FASTCGI
    1337                 :         int max_requests = 500;
    1338                 :         int requests = 0;
    1339                 :         int fastcgi = fcgi_is_fastcgi();
    1340                 :         char *bindpath = NULL;
    1341                 :         int fcgi_fd = 0;
    1342                 :         fcgi_request request;
    1343                 :         int repeats = 1;
    1344                 :         int benchmark = 0;
    1345                 : #if HAVE_GETTIMEOFDAY
    1346                 :         struct timeval start, end;
    1347                 : #else
    1348                 :         time_t start, end;
    1349                 : #endif
    1350                 : #ifndef PHP_WIN32
    1351                 :         int status = 0;
    1352                 : #endif
    1353                 : #endif /* PHP_FASTCGI */
    1354                 : 
    1355                 : #if 0 && defined(PHP_DEBUG)
    1356                 :         /* IIS is always making things more difficult.  This allows
    1357                 :            us to stop PHP and attach a debugger before much gets started */
    1358                 :         {
    1359                 :                 char szMessage [256];
    1360                 :                 wsprintf (szMessage, "Please attach a debugger to the process 0x%X [%d] (%s) and click OK",
    1361                 :                           GetCurrentProcessId(), GetCurrentProcessId(), argv[0]);
    1362                 :                 MessageBox(NULL, szMessage, "CGI Debug Time!", MB_OK|MB_SERVICE_NOTIFICATION);
    1363                 :         }
    1364                 : #endif
    1365                 : 
    1366                 : #ifdef HAVE_SIGNAL_H
    1367                 : #if defined(SIGPIPE) && defined(SIG_IGN)
    1368              98 :         signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
    1369                 :                                                                 that sockets created via fsockopen()
    1370                 :                                                                 don't kill PHP if the remote site
    1371                 :                                                                 closes it.  in apache|apxs mode apache
    1372                 :                                                                 does that for us!  thies@thieso.net
    1373                 :                                                                 20000419 */
    1374                 : #endif
    1375                 : #endif
    1376                 : 
    1377                 : #ifdef ZTS
    1378                 :         tsrm_startup(1, 1, 0, NULL);
    1379                 :         tsrm_ls = ts_resource(0);
    1380                 : #endif
    1381                 : 
    1382              98 :         sapi_startup(&cgi_sapi_module);
    1383              98 :         cgi_sapi_module.php_ini_path_override = NULL;
    1384                 : 
    1385                 : #ifdef PHP_WIN32
    1386                 :         _fmode = _O_BINARY; /* sets default for file streams to binary */
    1387                 :         setmode(_fileno(stdin),  O_BINARY);     /* make the stdio mode be binary */
    1388                 :         setmode(_fileno(stdout), O_BINARY);     /* make the stdio mode be binary */
    1389                 :         setmode(_fileno(stderr), O_BINARY);     /* make the stdio mode be binary */
    1390                 : #endif
    1391                 : 
    1392                 : #if PHP_FASTCGI
    1393                 :         if (!fastcgi) {
    1394                 : #endif
    1395                 :         /* Make sure we detect we are a cgi - a bit redundancy here,
    1396                 :            but the default case is that we have to check only the first one. */
    1397              98 :         if (getenv("SERVER_SOFTWARE") ||
    1398                 :                 getenv("SERVER_NAME") ||
    1399                 :                 getenv("GATEWAY_INTERFACE") ||
    1400                 :                 getenv("REQUEST_METHOD")
    1401                 :         ) {
    1402              55 :                 cgi = 1;
    1403                 :         }
    1404                 : #if PHP_FASTCGI
    1405                 :         }
    1406                 : #endif
    1407                 : 
    1408            2698 :         while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
    1409            2502 :                 switch (c) {
    1410                 :                         case 'c':
    1411               0 :                                 if (cgi_sapi_module.php_ini_path_override) {
    1412               0 :                                         free(cgi_sapi_module.php_ini_path_override);
    1413                 :                                 }
    1414               0 :                                 cgi_sapi_module.php_ini_path_override = strdup(php_optarg);
    1415               0 :                                 break;
    1416                 :                         case 'n':
    1417              23 :                                 cgi_sapi_module.php_ini_ignore = 1;
    1418              23 :                                 break;
    1419                 :                         case 'd': { 
    1420                 :                                 /* define ini entries on command line */
    1421            2309 :                                 int len = strlen(php_optarg);
    1422                 :                                 char *val;
    1423                 : 
    1424            2309 :                                 if ((val = strchr(php_optarg, '='))) {
    1425            2309 :                                         val++;
    1426            2385 :                                         if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
    1427              76 :                                                 cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
    1428              76 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
    1429              76 :                                                 ini_entries_len += (val - php_optarg);
    1430              76 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"", 1);
    1431              76 :                                                 ini_entries_len++;
    1432              76 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, val, len - (val - php_optarg));
    1433              76 :                                                 ini_entries_len += len - (val - php_optarg);
    1434              76 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
    1435              76 :                                                 ini_entries_len += sizeof("\n\0\"") - 2;
    1436                 :                                         } else {
    1437            2233 :                                                 cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\n\0"));
    1438            2233 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
    1439            2233 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
    1440            2233 :                                                 ini_entries_len += len + sizeof("\n\0") - 2;
    1441                 :                                         }
    1442                 :                                 } else {
    1443               0 :                                         cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
    1444               0 :                                         memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
    1445               0 :                                         memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
    1446               0 :                                         ini_entries_len += len + sizeof("=1\n\0") - 2;
    1447                 :                                 }
    1448            2309 :                                 break;
    1449                 :                         }
    1450                 : #if PHP_FASTCGI
    1451                 :                         /* if we're started on command line, check to see if
    1452                 :                            we are being started as an 'external' fastcgi
    1453                 :                            server by accepting a bindpath parameter. */
    1454                 :                         case 'b':
    1455                 :                                 if (!fastcgi) {
    1456                 :                                         bindpath = strdup(php_optarg);
    1457                 :                                 }
    1458                 :                                 break;
    1459                 : #endif
    1460                 :                         case 's': /* generate highlighted HTML from source */
    1461               3 :                                 behavior = PHP_MODE_HIGHLIGHT;
    1462                 :                                 break;
    1463                 : 
    1464                 :                 }
    1465                 : 
    1466                 :         }
    1467              98 :         php_optind = orig_optind;
    1468              98 :         php_optarg = orig_optarg;
    1469                 : 
    1470                 : #ifdef ZTS
    1471                 :         SG(request_info).path_translated = NULL;
    1472                 : #endif
    1473                 : 
    1474              98 :         cgi_sapi_module.executable_location = argv[0];
    1475                 : 
    1476                 :         /* startup after we get the above ini override se we get things right */
    1477              98 :         if (cgi_sapi_module.startup(&cgi_sapi_module) == FAILURE) {
    1478                 : #ifdef ZTS
    1479                 :                 tsrm_shutdown();
    1480                 : #endif
    1481               0 :                 return FAILURE;
    1482                 :         }
    1483                 : 
    1484                 : #if FORCE_CGI_REDIRECT
    1485                 :         /* check force_cgi after startup, so we have proper output */
    1486                 :         if (cgi && CGIG(force_redirect)) {
    1487                 :                 /* Apache will generate REDIRECT_STATUS,
    1488                 :                  * Netscape and redirect.so will generate HTTP_REDIRECT_STATUS.
    1489                 :                  * redirect.so and installation instructions available from
    1490                 :                  * http://www.koehntopp.de/php.
    1491                 :                  *   -- kk@netuse.de
    1492                 :                  */
    1493                 :                 if (!getenv("REDIRECT_STATUS")
    1494                 :                         && !getenv ("HTTP_REDIRECT_STATUS")
    1495                 :                         /* this is to allow a different env var to be configured
    1496                 :                            in case some server does something different than above */
    1497                 :                         && (!CGIG(redirect_status_env) || !getenv(CGIG(redirect_status_env)))
    1498                 :                         ) {
    1499                 :                         SG(sapi_headers).http_response_code = 400;
    1500                 :                         PUTS("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\
    1501                 : <p>This PHP CGI binary was compiled with force-cgi-redirect enabled.  This\n\
    1502                 : means that a page will only be served up if the REDIRECT_STATUS CGI variable is\n\
    1503                 : set, e.g. via an Apache Action directive.</p>\n\
    1504                 : <p>For more information as to <i>why</i> this behaviour exists, see the <a href=\"http://php.net/security.cgi-bin\">\
    1505                 : manual page for CGI security</a>.</p>\n\
    1506                 : <p>For more information about changing this behaviour or re-enabling this webserver,\n\
    1507                 : consult the installation file that came with this distribution, or visit \n\
    1508                 : <a href=\"http://php.net/install.windows\">the manual page</a>.</p>\n");
    1509                 : 
    1510                 : #if defined(ZTS) && !defined(PHP_DEBUG)
    1511                 :                         /* XXX we're crashing here in msvc6 debug builds at
    1512                 :                            php_message_handler_for_zend:839 because
    1513                 :                            SG(request_info).path_translated is an invalid pointer.
    1514                 :                            It still happens even though I set it to null, so something
    1515                 :                            weird is going on.
    1516                 :                         */
    1517                 :                         tsrm_shutdown();
    1518                 : #endif
    1519                 :                         return FAILURE;
    1520                 :                 }
    1521                 :         }
    1522                 : #endif  /* FORCE_CGI_REDIRECT */
    1523                 : 
    1524                 : #if PHP_FASTCGI
    1525                 :         if (bindpath) {
    1526                 :                 fcgi_fd = fcgi_listen(bindpath, 128);
    1527                 :                 if (fcgi_fd < 0) {
    1528                 :                         fprintf(stderr, "Couldn't create FastCGI listen socket on port %s\n", bindpath);
    1529                 : #ifdef ZTS
    1530                 :                         tsrm_shutdown();
    1531                 : #endif
    1532                 :                         return FAILURE;
    1533                 :                 }
    1534                 :                 fastcgi = fcgi_is_fastcgi();
    1535                 :         }
    1536                 :         
    1537                 :         if (fastcgi) {
    1538                 :                 /* How many times to run PHP scripts before dying */
    1539                 :                 if (getenv("PHP_FCGI_MAX_REQUESTS")) {
    1540                 :                         max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
    1541                 :                         if (max_requests < 0) {
    1542                 :                                 fprintf(stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n");
    1543                 :                                 return FAILURE;
    1544                 :                         }
    1545                 :                 }
    1546                 : 
    1547                 :                 /* make php call us to get _ENV vars */
    1548                 :                 php_php_import_environment_variables = php_import_environment_variables;
    1549                 :                 php_import_environment_variables = cgi_php_import_environment_variables;
    1550                 : 
    1551                 :                 /* library is already initialized, now init our request */
    1552                 :                 fcgi_init_request(&request, fcgi_fd);
    1553                 : 
    1554                 : #ifndef PHP_WIN32
    1555                 :         /* Pre-fork, if required */
    1556                 :         if (getenv("PHP_FCGI_CHILDREN")) {
    1557                 :                 char * children_str = getenv("PHP_FCGI_CHILDREN");
    1558                 :                 children = atoi(children_str);
    1559                 :                 if (children < 0) {
    1560                 :                         fprintf(stderr, "PHP_FCGI_CHILDREN is not valid\n");
    1561                 :                         return FAILURE;
    1562                 :                 }
    1563                 :                 fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, children_str, strlen(children_str));
    1564                 :                 /* This is the number of concurrent requests, equals FCGI_MAX_CONNS */
    1565                 :                 fcgi_set_mgmt_var("FCGI_MAX_REQS",  sizeof("FCGI_MAX_REQS")-1,  children_str, strlen(children_str));
    1566                 :         } else {
    1567                 :                 fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, "1", sizeof("1")-1);
    1568                 :                 fcgi_set_mgmt_var("FCGI_MAX_REQS",  sizeof("FCGI_MAX_REQS")-1,  "1", sizeof("1")-1);
    1569                 :         }
    1570                 : 
    1571                 :         if (children) {
    1572                 :                 int running = 0;
    1573                 :                 pid_t pid;
    1574                 : 
    1575                 :                 /* Create a process group for ourself & children */
    1576                 :                 setsid();
    1577                 :                 pgroup = getpgrp();
    1578                 : #ifdef DEBUG_FASTCGI
    1579                 :                 fprintf(stderr, "Process group %d\n", pgroup);
    1580                 : #endif
    1581                 : 
    1582                 :                 /* Set up handler to kill children upon exit */
    1583                 :                 act.sa_flags = 0;
    1584                 :                 act.sa_handler = fastcgi_cleanup;
    1585                 :                 if (sigaction(SIGTERM, &act, &old_term) ||
    1586                 :                     sigaction(SIGINT,  &act, &old_int) ||
    1587                 :                     sigaction(SIGQUIT, &act, &old_quit)) {
    1588                 :                         perror("Can't set signals");
    1589                 :                         exit(1);
    1590                 :                 }
    1591                 : 
    1592                 :                 if (fcgi_in_shutdown()) {
    1593                 :                         goto parent_out;
    1594                 :                 }
    1595                 : 
    1596                 :                 while (parent) {
    1597                 :                         do {
    1598                 : #ifdef DEBUG_FASTCGI
    1599                 :                                 fprintf(stderr, "Forking, %d running\n", running);
    1600                 : #endif
    1601                 :                                 pid = fork();
    1602                 :                                 switch (pid) {
    1603                 :                                 case 0:
    1604                 :                                         /* One of the children.
    1605                 :                                          * Make sure we don't go round the
    1606                 :                                          * fork loop any more
    1607                 :                                          */
    1608                 :                                         parent = 0;
    1609                 : 
    1610                 :                                         /* don't catch our signals */
    1611                 :                                         sigaction(SIGTERM, &old_term, 0);
    1612                 :                                         sigaction(SIGQUIT, &old_quit, 0);
    1613                 :                                         sigaction(SIGINT,  &old_int,  0);
    1614                 :                                         break;
    1615                 :                                 case -1:
    1616                 :                                         perror("php (pre-forking)");
    1617                 :                                         exit(1);
    1618                 :                                         break;
    1619                 :                                 default:
    1620                 :                                         /* Fine */
    1621                 :                                         running++;
    1622                 :                                         break;
    1623                 :                                 }
    1624                 :                         } while (parent && (running < children));
    1625                 : 
    1626                 :                         if (parent) {
    1627                 : #ifdef DEBUG_FASTCGI
    1628                 :                                 fprintf(stderr, "Wait for kids, pid %d\n", getpid());
    1629                 : #endif
    1630                 :                                 parent_waiting = 1;
    1631                 :                                 while (1) {
    1632                 :                                         if (wait(&status) >= 0) {
    1633                 :                                                 running--;
    1634                 :                                                 break;
    1635                 :                                         } else if (exit_signal) {
    1636                 :                                                 break;
    1637                 :                                         }                                               
    1638                 :                                 }
    1639                 :                                 if (exit_signal) {
    1640                 : #if 0
    1641                 :                                         while (running > 0) {
    1642                 :                                                 while (wait(&status) < 0) {
    1643                 :                                                 }
    1644                 :                                                 running--;
    1645                 :                                         }
    1646                 : #endif
    1647                 :                                         goto parent_out;
    1648                 :                                 }
    1649                 :                         }
    1650                 :                 }
    1651                 :         } else {
    1652                 :                 parent = 0;
    1653                 :         }
    1654                 : 
    1655                 : #endif /* WIN32 */
    1656                 :         }
    1657                 : #endif /* FASTCGI */
    1658                 : 
    1659              98 :         zend_first_try {
    1660            2698 :                 while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1)) != -1) {
    1661            2502 :                         switch (c) {
    1662                 : #if PHP_FASTCGI
    1663                 :                                 case 'T':
    1664                 :                                 benchmark = 1;
    1665                 :                                         repeats = atoi(php_optarg);
    1666                 : #ifdef HAVE_GETTIMEOFDAY
    1667                 :                                         gettimeofday(&start, NULL);
    1668                 : #else
    1669                 :                                         time(&start);
    1670                 : #endif
    1671                 :                                         break;
    1672                 : #endif
    1673                 :                                 case 'h':
    1674                 :                                 case '?':
    1675                 : #if PHP_FASTCGI
    1676                 :                                         fcgi_shutdown();
    1677                 : #endif
    1678               0 :                                         no_headers = 1;
    1679               0 :                                         php_output_startup();
    1680               0 :                                         php_output_activate(TSRMLS_C);
    1681               0 :                                         SG(headers_sent) = 1;
    1682               0 :                                         php_cgi_usage(argv[0]);
    1683               0 :                                         php_end_ob_buffers(1 TSRMLS_CC);
    1684               0 :                                         exit_status = 0;
    1685               0 :                                         goto out;
    1686                 :                         }
    1687                 :                 }
    1688              98 :                 php_optind = orig_optind;
    1689              98 :                 php_optarg = orig_optarg;
    1690                 : 
    1691                 : #if PHP_FASTCGI
    1692                 :                 /* start of FAST CGI loop */
    1693                 :                 /* Initialise FastCGI request structure */
    1694                 : #ifdef PHP_WIN32
    1695                 :                 /* attempt to set security impersonation for fastcgi
    1696                 :                    will only happen on NT based OS, others will ignore it. */
    1697                 :                 if (fastcgi && CGIG(impersonate)) {
    1698                 :                         fcgi_impersonate();
    1699                 :                 }
    1700                 : #endif
    1701                 :                 while (!fastcgi || fcgi_accept_request(&request) >= 0) {
    1702                 : #endif
    1703                 : 
    1704                 : #if PHP_FASTCGI
    1705                 :                 SG(server_context) = (void *) &request;
    1706                 : #else
    1707              98 :                 SG(server_context) = (void *) 1; /* avoid server_context==NULL checks */
    1708                 : #endif
    1709              98 :                 init_request_info(TSRMLS_C);
    1710              98 :                 CG(interactive) = 0;
    1711                 : 
    1712              98 :                 if (!cgi
    1713                 : #if PHP_FASTCGI
    1714                 :                         && !fastcgi
    1715                 : #endif
    1716                 :                 ) {
    1717             792 :                         while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
    1718             707 :                                 switch (c) {
    1719                 : 
    1720                 :                                 case 'a':       /* interactive mode */
    1721               2 :                                                 printf("Interactive mode enabled\n\n");
    1722               2 :                                                 CG(interactive) = 1;
    1723               2 :                                                 break;
    1724                 : 
    1725                 :                                 case 'C': /* don't chdir to the script directory */
    1726              20 :                                                 SG(options) |= SAPI_OPTION_NO_CHDIR;
    1727              20 :                                                 break;
    1728                 : 
    1729                 :                                 case 'e': /* enable extended info output */
    1730               0 :                                                 CG(extended_info) = 1;
    1731               0 :                                                 break;
    1732                 : 
    1733                 :                                 case 'f': /* parse file */
    1734               6 :                                                 if (script_file) {
    1735               1 :                                                         efree(script_file);
    1736                 :                                                 }
    1737               6 :                                                 script_file = estrdup(php_optarg);
    1738               6 :                                                 no_headers = 1;
    1739               6 :                                                 break;
    1740                 : 
    1741                 :                                 case 'i': /* php info & quit */
    1742               0 :                                                 if (script_file) {
    1743               0 :                                                         efree(script_file);
    1744                 :                                                 }
    1745               0 :                                                 if (php_request_startup(TSRMLS_C) == FAILURE) {
    1746               0 :                                                         SG(server_context) = NULL;
    1747               0 :                                                         php_module_shutdown(TSRMLS_C);
    1748               0 :                                                         return FAILURE;
    1749                 :                                                 }
    1750               0 :                                                 if (no_headers) {
    1751               0 :                                                         SG(headers_sent) = 1;
    1752               0 :                                                         SG(request_info).no_headers = 1;
    1753                 :                                                 }
    1754               0 :                                                 php_print_info(0xFFFFFFFF TSRMLS_CC);
    1755               0 :                                                 php_request_shutdown((void *) 0);
    1756               0 :                                                 exit_status = 0;
    1757               0 :                                                 goto out;
    1758                 : 
    1759                 :                                 case 'l': /* syntax check mode */
    1760               4 :                                                 no_headers = 1;
    1761               4 :                                                 behavior = PHP_MODE_LINT;
    1762               4 :                                                 break;
    1763                 : 
    1764                 :                                 case 'm': /* list compiled in modules */
    1765               0 :                                         if (script_file) {
    1766               0 :                                                 efree(script_file);
    1767                 :                                         }
    1768               0 :                                         php_output_startup();
    1769               0 :                                         php_output_activate(TSRMLS_C);
    1770               0 :                                         SG(headers_sent) = 1;
    1771               0 :                                         php_printf("[PHP Modules]\n");
    1772               0 :                                         print_modules(TSRMLS_C);
    1773               0 :                                         php_printf("\n[Zend Modules]\n");
    1774               0 :                                         print_extensions(TSRMLS_C);
    1775               0 :                                         php_printf("\n");
    1776               0 :                                         php_end_ob_buffers(1 TSRMLS_CC);
    1777               0 :                                         exit_status = 0;
    1778               0 :                                         goto out;
    1779                 : 
    1780                 : #if 0 /* not yet operational, see also below ... */
    1781                 :                                 case '': /* generate indented source mode*/
    1782                 :                                                 behavior=PHP_MODE_INDENT;
    1783                 :                                                 break;
    1784                 : #endif
    1785                 : 
    1786                 :                                 case 'q': /* do not generate HTTP headers */
    1787              20 :                                                 no_headers = 1;
    1788              20 :                                                 break;
    1789                 : 
    1790                 :                                 case 'v': /* show php version & quit */
    1791               1 :                                                 if (script_file) {
    1792               0 :                                                         efree(script_file);
    1793                 :                                                 }
    1794               1 :                                                 no_headers = 1;
    1795               1 :                                                 if (php_request_startup(TSRMLS_C) == FAILURE) {
    1796               0 :                                                         SG(server_context) = NULL;
    1797               0 :                                                         php_module_shutdown(TSRMLS_C);
    1798               0 :                                                         return FAILURE;
    1799                 :                                                 }
    1800               1 :                                                 if (no_headers) {
    1801               1 :                                                         SG(headers_sent) = 1;
    1802               1 :                                                         SG(request_info).no_headers = 1;
    1803                 :                                                 }
    1804                 : #if ZEND_DEBUG
    1805                 :                                                 php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
    1806                 : #else
    1807               1 :                                                 php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2009 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
    1808                 : #endif
    1809               1 :                                                 php_request_shutdown((void *) 0);
    1810               1 :                                                 exit_status = 0;
    1811               1 :                                                 goto out;
    1812                 : 
    1813                 :                                 case 'w':
    1814               4 :                                                 behavior = PHP_MODE_STRIP;
    1815               4 :                                                 break;
    1816                 : 
    1817                 :                                 case 'z': /* load extension file */
    1818               0 :                                                 zend_load_extension(php_optarg);
    1819                 :                                                 break;
    1820                 : 
    1821                 :                                         default:
    1822                 :                                                 break;
    1823                 :                                 }
    1824                 :                         }
    1825                 : 
    1826              42 :                         if (script_file) {
    1827                 :                                 /* override path_translated if -f on command line */
    1828               5 :                                 STR_FREE(SG(request_info).path_translated);
    1829               5 :                                 SG(request_info).path_translated = script_file;
    1830                 :                                 /* before registering argv to module exchange the *new* argv[0] */
    1831                 :                                 /* we can achieve this without allocating more memory */
    1832               5 :                                 SG(request_info).argc = argc - (php_optind - 1);
    1833               5 :                                 SG(request_info).argv = &argv[php_optind - 1];
    1834               5 :                                 SG(request_info).argv[0] = script_file;
    1835              37 :                         } else if (argc > php_optind) {
    1836                 :                                 /* file is on command line, but not in -f opt */
    1837              35 :                                 STR_FREE(SG(request_info).path_translated);
    1838              35 :                                 SG(request_info).path_translated = estrdup(argv[php_optind]);
    1839                 :                                 /* arguments after the file are considered script args */
    1840              35 :                                 SG(request_info).argc = argc - php_optind;
    1841              35 :                                 SG(request_info).argv = &argv[php_optind];
    1842                 :                         }
    1843                 : 
    1844              42 :                         if (no_headers) {
    1845              29 :                                 SG(headers_sent) = 1;
    1846              29 :                                 SG(request_info).no_headers = 1;
    1847                 :                         }
    1848                 : 
    1849                 :                         /* all remaining arguments are part of the query string
    1850                 :                            this section of code concatenates all remaining arguments
    1851                 :                            into a single string, seperating args with a &
    1852                 :                            this allows command lines like:
    1853                 : 
    1854                 :                            test.php v1=test v2=hello+world!
    1855                 :                            test.php "v1=test&v2=hello world!"
    1856                 :                            test.php v1=test "v2=hello world!"
    1857                 :                         */
    1858              42 :                         if (!SG(request_info).query_string && argc > php_optind) {
    1859              35 :                                 int slen = strlen(PG(arg_separator).input);
    1860              35 :                                 len = 0;
    1861              70 :                                 for (i = php_optind; i < argc; i++) {
    1862              35 :                                         if (i < (argc - 1)) {
    1863               0 :                                                 len += strlen(argv[i]) + slen;
    1864                 :                                         } else {
    1865              35 :                                                 len += strlen(argv[i]);
    1866                 :                                         }
    1867                 :                                 }
    1868                 : 
    1869              35 :                                 len += 2;
    1870              35 :                                 s = malloc(len);
    1871              35 :                                 *s = '\0';                      /* we are pretending it came from the environment  */
    1872              70 :                                 for (i = php_optind; i < argc; i++) {
    1873              35 :                                         strlcat(s, argv[i], len);
    1874              35 :                                         if (i < (argc - 1)) {
    1875               0 :                                                 strlcat(s, PG(arg_separator).input, len);
    1876                 :                                         }
    1877                 :                                 }
    1878              35 :                                 SG(request_info).query_string = s;
    1879              35 :                                 free_query_string = 1;
    1880                 :                         }
    1881                 :                 } /* end !cgi && !fastcgi */
    1882                 : 
    1883                 :                 /* 
    1884                 :                         we never take stdin if we're (f)cgi, always
    1885                 :                         rely on the web server giving us the info
    1886                 :                         we need in the environment. 
    1887                 :                 */
    1888             192 :                 if (SG(request_info).path_translated || cgi 
    1889                 : #if PHP_FASTCGI
    1890                 :                         || fastcgi
    1891                 : #endif
    1892                 :                 )
    1893                 :                 {
    1894              95 :                         file_handle.type = ZEND_HANDLE_FILENAME;
    1895              95 :                         file_handle.filename = SG(request_info).path_translated;
    1896              95 :                         file_handle.handle.fp = NULL;
    1897                 :                 } else {
    1898               2 :                         file_handle.filename = "-";
    1899               2 :                         file_handle.type = ZEND_HANDLE_FP;
    1900               2 :                         file_handle.handle.fp = stdin;
    1901                 :                 }
    1902                 : 
    1903              97 :                 file_handle.opened_path = NULL;
    1904              97 :                 file_handle.free_filename = 0;
    1905                 : 
    1906                 :                 /* request startup only after we've done all we can to
    1907                 :                    get path_translated */
    1908              97 :                 if (php_request_startup(TSRMLS_C) == FAILURE) {
    1909                 : #if PHP_FASTCGI
    1910                 :                         if (fastcgi) {
    1911                 :                                 fcgi_finish_request(&request);
    1912                 :                         }
    1913                 : #endif
    1914               0 :                         SG(server_context) = NULL;
    1915               0 :                         php_module_shutdown(TSRMLS_C);
    1916               0 :                         return FAILURE;
    1917                 :                 }
    1918              97 :                 if (no_headers) {
    1919              29 :                         SG(headers_sent) = 1;
    1920              29 :                         SG(request_info).no_headers = 1;
    1921                 :                 }
    1922                 : 
    1923                 :                 /* 
    1924                 :                         at this point path_translated will be set if:
    1925                 :                         1. we are running from shell and got filename was there
    1926                 :                         2. we are running as cgi or fastcgi
    1927                 :                 */
    1928              97 :                 retval = FAILURE;
    1929              97 :                 if (cgi || SG(request_info).path_translated) {
    1930              95 :                         if (!php_check_open_basedir(SG(request_info).path_translated TSRMLS_CC)) {
    1931              95 :                                 retval = php_fopen_primary_script(&file_handle TSRMLS_CC);
    1932                 :                         }
    1933                 :                 }
    1934                 :                 /* 
    1935                 :                         if we are unable to open path_translated and we are not
    1936                 :                         running from shell (so fp == NULL), then fail.
    1937                 :                 */
    1938              97 :                 if (retval == FAILURE && file_handle.handle.fp == NULL) {
    1939               7 :                         if (errno == EACCES) {
    1940               0 :                                 SG(sapi_headers).http_response_code = 403;
    1941               0 :                                 PUTS("Access denied.\n");
    1942                 :                         } else {
    1943               7 :                                 SG(sapi_headers).http_response_code = 404;
    1944               7 :                                 PUTS("No input file specified.\n");
    1945                 :                         }
    1946                 : #if PHP_FASTCGI
    1947                 :                         /* we want to serve more requests if this is fastcgi
    1948                 :                            so cleanup and continue, request shutdown is
    1949                 :                            handled later */
    1950                 :                         if (fastcgi) {
    1951                 :                                 goto fastcgi_request_done;
    1952                 :                         }
    1953                 : #endif
    1954                 : 
    1955               7 :                         STR_FREE(SG(request_info).path_translated);
    1956                 : 
    1957               7 :                         if (free_query_string && SG(request_info).query_string) {
    1958               3 :                                 free(SG(request_info).query_string);
    1959               3 :                                 SG(request_info).query_string = NULL;
    1960                 :                         }
    1961                 : 
    1962               7 :                         php_request_shutdown((void *) 0);
    1963               7 :                         SG(server_context) = NULL;
    1964               7 :                         php_module_shutdown(TSRMLS_C);
    1965               7 :                         sapi_shutdown();
    1966                 : #ifdef ZTS
    1967                 :                         tsrm_shutdown();
    1968                 : #endif
    1969               7 :                         return FAILURE;
    1970                 :                 }
    1971                 : 
    1972              90 :                 if (CGIG(check_shebang_line) && file_handle.handle.fp && (file_handle.handle.fp != stdin)) {
    1973                 :                         /* #!php support */
    1974              88 :                         c = fgetc(file_handle.handle.fp);
    1975              88 :                         if (c == '#') {
    1976               0 :                                 while (c != '\n' && c != '\r' && c != EOF) {
    1977               0 :                                         c = fgetc(file_handle.handle.fp);       /* skip to end of line */
    1978                 :                                 }
    1979                 :                                 /* handle situations where line is terminated by \r\n */
    1980               0 :                                 if (c == '\r') {
    1981               0 :                                         if (fgetc(file_handle.handle.fp) != '\n') {
    1982               0 :                                                 long pos = ftell(file_handle.handle.fp);
    1983               0 :                                                 fseek(file_handle.handle.fp, pos - 1, SEEK_SET);
    1984                 :                                         }
    1985                 :                                 }
    1986               0 :                                 CG(start_lineno) = 2;
    1987                 :                         } else {
    1988              88 :                                 rewind(file_handle.handle.fp);
    1989                 :                         }
    1990                 :                 }
    1991                 : 
    1992              90 :                 switch (behavior) {
    1993                 :                         case PHP_MODE_STANDARD:
    1994              84 :                                 php_execute_script(&file_handle TSRMLS_CC);
    1995              84 :                                 break;
    1996                 :                         case PHP_MODE_LINT:
    1997               3 :                                 PG(during_request_startup) = 0;
    1998               3 :                                 exit_status = php_lint_script(&file_handle TSRMLS_CC);
    1999               3 :                                 if (exit_status == SUCCESS) {
    2000               2 :                                         zend_printf("No syntax errors detected in %s\n", file_handle.filename);
    2001                 :                                 } else {
    2002               1 :                                         zend_printf("Errors parsing %s\n", file_handle.filename);
    2003                 :                                 }
    2004               3 :                                 break;
    2005                 :                         case PHP_MODE_STRIP:
    2006               2 :                                 if (open_file_for_scanning(&file_handle TSRMLS_CC) == SUCCESS) {
    2007               2 :                                         zend_strip(TSRMLS_C);
    2008               2 :                                         fclose(file_handle.handle.fp);
    2009               2 :                                         php_end_ob_buffers(1 TSRMLS_CC);
    2010                 :                                 }
    2011               2 :                                 return SUCCESS;
    2012                 :                                 break;
    2013                 :                         case PHP_MODE_HIGHLIGHT:
    2014                 :                                 {
    2015                 :                                         zend_syntax_highlighter_ini syntax_highlighter_ini;
    2016                 : 
    2017               1 :                                         if (open_file_for_scanning(&file_handle TSRMLS_CC) == SUCCESS) {
    2018               1 :                                                 php_get_highlight_struct(&syntax_highlighter_ini);
    2019               1 :                                                 zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
    2020                 : #if PHP_FASTCGI
    2021                 :                                                 if (fastcgi) {
    2022                 :                                                         goto fastcgi_request_done;
    2023                 :                                                 }
    2024                 : #endif
    2025               1 :                                                 fclose(file_handle.handle.fp);
    2026               1 :                                                 php_end_ob_buffers(1 TSRMLS_CC);
    2027                 :                                         }
    2028               1 :                                         return SUCCESS;
    2029                 :                                 }
    2030                 :                                 break;
    2031                 : #if 0
    2032                 :                         /* Zeev might want to do something with this one day */
    2033                 :                         case PHP_MODE_INDENT:
    2034                 :                                 open_file_for_scanning(&file_handle TSRMLS_CC);
    2035                 :                                 zend_indent();
    2036                 :                                 fclose(file_handle.handle.fp);
    2037                 :                                 return SUCCESS;
    2038                 :                                 break;
    2039                 : #endif
    2040                 :                 }
    2041                 : 
    2042                 : #if PHP_FASTCGI
    2043                 : fastcgi_request_done:
    2044                 : #endif
    2045                 :                 {
    2046                 :                         char *path_translated;
    2047                 : 
    2048                 :                         /*      Go through this trouble so that the memory manager doesn't warn
    2049                 :                          *      about SG(request_info).path_translated leaking
    2050                 :                          */
    2051              87 :                         if (SG(request_info).path_translated) {
    2052              86 :                                 path_translated = strdup(SG(request_info).path_translated);
    2053              86 :                                 STR_FREE(SG(request_info).path_translated);
    2054              86 :                                 SG(request_info).path_translated = path_translated;
    2055                 :                         }
    2056                 : 
    2057              87 :                         php_request_shutdown((void *) 0);
    2058              87 :                         if (exit_status == 0) {
    2059              86 :                                 exit_status = EG(exit_status);
    2060                 :                         }
    2061                 : 
    2062              87 :                         if (SG(request_info).path_translated) {
    2063              86 :                                 free(SG(request_info).path_translated);
    2064              86 :                                 SG(request_info).path_translated = NULL;
    2065                 :                         }
    2066              87 :                         if (free_query_string && SG(request_info).query_string) {
    2067              30 :                                 free(SG(request_info).query_string);
    2068              30 :                                 SG(request_info).query_string = NULL;
    2069                 :                         }
    2070                 : 
    2071                 :                 }
    2072                 : 
    2073                 : #if PHP_FASTCGI
    2074                 :                         if (!fastcgi) {
    2075                 :                                 if (benchmark) {
    2076                 :                                         repeats--;
    2077                 :                                         if (repeats > 0) {
    2078                 :                                                 script_file = NULL;
    2079                 :                                                 php_optind = orig_optind;
    2080                 :                                                 php_optarg = orig_optarg;
    2081                 :                                                 continue;
    2082                 :                                         }
    2083                 :                                 }
    2084                 :                                 break;
    2085                 :                         }
    2086                 : 
    2087                 :                         /* only fastcgi will get here */
    2088                 :                         requests++;
    2089                 :                         if (max_requests && (requests == max_requests)) {
    2090                 :                                 fcgi_finish_request(&request);
    2091                 :                                 if (bindpath) {
    2092                 :                                         free(bindpath);
    2093                 :                                 }
    2094                 :                                 if (max_requests != 1) {
    2095                 :                                         /* no need to return exit_status of the last request */
    2096                 :                                         exit_status = 0;
    2097                 :                                 }
    2098                 :                                 break;
    2099                 :                         }
    2100                 :                         /* end of fastcgi loop */
    2101                 :                 }
    2102                 :                 fcgi_shutdown();
    2103                 : #endif
    2104                 : 
    2105              87 :                 if (cgi_sapi_module.php_ini_path_override) {
    2106               0 :                         free(cgi_sapi_module.php_ini_path_override);
    2107                 :                 }
    2108              87 :                 if (cgi_sapi_module.ini_entries) {
    2109              79 :                         free(cgi_sapi_module.ini_entries);
    2110                 :                 }
    2111               0 :         } zend_catch {
    2112               0 :                 exit_status = 255;
    2113              87 :         } zend_end_try();
    2114                 : 
    2115              88 : out:
    2116                 : #if PHP_FASTCGI
    2117                 :         if (benchmark) {
    2118                 :                 int sec;
    2119                 : #ifdef HAVE_GETTIMEOFDAY
    2120                 :                 int usec;
    2121                 : 
    2122                 :                 gettimeofday(&end, NULL);
    2123                 :                 sec = (int)(end.tv_sec - start.tv_sec);
    2124                 :                 if (end.tv_usec >= start.tv_usec) {
    2125                 :                         usec = (int)(end.tv_usec - start.tv_usec);
    2126                 :                 } else {
    2127                 :                         sec -= 1;
    2128                 :                         usec = (int)(end.tv_usec + 1000000 - start.tv_usec);
    2129                 :                 }
    2130                 :                 fprintf(stderr, "\nElapsed time: %d.%06d sec\n", sec, usec);
    2131                 : #else
    2132                 :                 time(&end);
    2133                 :                 sec = (int)(end - start);
    2134                 :                 fprintf(stderr, "\nElapsed time: %d sec\n", sec);
    2135                 : #endif
    2136                 :         }
    2137                 : #endif
    2138                 : 
    2139                 : #ifndef PHP_WIN32
    2140              88 : parent_out:
    2141                 : #endif
    2142                 : 
    2143              88 :         SG(server_context) = NULL;
    2144              88 :         php_module_shutdown(TSRMLS_C);
    2145              88 :         sapi_shutdown();
    2146                 : 
    2147                 : #ifdef ZTS
    2148                 :         /*tsrm_shutdown();*/
    2149                 : #endif
    2150                 : 
    2151                 : #if defined(PHP_WIN32) && ZEND_DEBUG && 0
    2152                 :         _CrtDumpMemoryLeaks();
    2153                 : #endif
    2154                 : 
    2155              88 :         return exit_status;
    2156                 : }
    2157                 : /* }}} */
    2158                 : 
    2159                 : /*
    2160                 :  * Local variables:
    2161                 :  * tab-width: 4
    2162                 :  * c-basic-offset: 4
    2163                 :  * End:
    2164                 :  * vim600: sw=4 ts=4 fdm=marker
    2165                 :  * vim<600: sw=4 ts=4
    2166                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:29 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.