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

LCOV - code coverage report
Current view: top level - main - main.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 729 945 77.1 %
Date: 2014-07-21 Functions: 50 60 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available 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: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
      17             :    |          Zeev Suraski <zeev@zend.com>                                |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : /* {{{ includes
      24             :  */
      25             : 
      26             : #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
      27             : 
      28             : #include "php.h"
      29             : #include <stdio.h>
      30             : #include <fcntl.h>
      31             : #ifdef PHP_WIN32
      32             : #include "win32/time.h"
      33             : #include "win32/signal.h"
      34             : #include "win32/php_win32_globals.h"
      35             : #include "win32/winutil.h"
      36             : #include <process.h>
      37             : #elif defined(NETWARE)
      38             : #include <sys/timeval.h>
      39             : #ifdef USE_WINSOCK
      40             : #include <novsock2.h>
      41             : #endif
      42             : #endif
      43             : #if HAVE_SYS_TIME_H
      44             : #include <sys/time.h>
      45             : #endif
      46             : #if HAVE_UNISTD_H
      47             : #include <unistd.h>
      48             : #endif
      49             : #if HAVE_SIGNAL_H
      50             : #include <signal.h>
      51             : #endif
      52             : #if HAVE_SETLOCALE
      53             : #include <locale.h>
      54             : #endif
      55             : #include "zend.h"
      56             : #include "zend_extensions.h"
      57             : #include "php_ini.h"
      58             : #include "php_globals.h"
      59             : #include "php_main.h"
      60             : #include "fopen_wrappers.h"
      61             : #include "ext/standard/php_standard.h"
      62             : #include "ext/standard/php_string.h"
      63             : #include "ext/date/php_date.h"
      64             : #include "php_variables.h"
      65             : #include "ext/standard/credits.h"
      66             : #ifdef PHP_WIN32
      67             : #include <io.h>
      68             : #include "win32/php_registry.h"
      69             : #include "ext/standard/flock_compat.h"
      70             : #endif
      71             : #include "php_syslog.h"
      72             : #include "Zend/zend_exceptions.h"
      73             : 
      74             : #if PHP_SIGCHILD
      75             : #include <sys/types.h>
      76             : #include <sys/wait.h>
      77             : #endif
      78             : 
      79             : #include "zend_compile.h"
      80             : #include "zend_execute.h"
      81             : #include "zend_highlight.h"
      82             : #include "zend_indent.h"
      83             : #include "zend_extensions.h"
      84             : #include "zend_ini.h"
      85             : #include "zend_dtrace.h"
      86             : 
      87             : #include "php_content_types.h"
      88             : #include "php_ticks.h"
      89             : #include "php_streams.h"
      90             : #include "php_open_temporary_file.h"
      91             : 
      92             : #include "SAPI.h"
      93             : #include "rfc1867.h"
      94             : 
      95             : #if HAVE_MMAP || defined(PHP_WIN32)
      96             : # if HAVE_UNISTD_H
      97             : #  include <unistd.h>
      98             : #  if defined(_SC_PAGESIZE)
      99             : #    define REAL_PAGE_SIZE sysconf(_SC_PAGESIZE);
     100             : #  elif defined(_SC_PAGE_SIZE)
     101             : #    define REAL_PAGE_SIZE sysconf(_SC_PAGE_SIZE);
     102             : #  endif
     103             : # endif
     104             : # if HAVE_SYS_MMAN_H
     105             : #  include <sys/mman.h>
     106             : # endif
     107             : # ifndef REAL_PAGE_SIZE
     108             : #  ifdef PAGE_SIZE
     109             : #   define REAL_PAGE_SIZE PAGE_SIZE
     110             : #  else
     111             : #   define REAL_PAGE_SIZE 4096
     112             : #  endif
     113             : # endif
     114             : #endif
     115             : /* }}} */
     116             : 
     117             : #ifndef S_ISREG
     118             : #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
     119             : #endif
     120             : 
     121             : PHPAPI int (*php_register_internal_extensions_func)(TSRMLS_D) = php_register_internal_extensions;
     122             : 
     123             : #ifndef ZTS
     124             : php_core_globals core_globals;
     125             : #else
     126             : PHPAPI int core_globals_id;
     127             : #endif
     128             : 
     129             : #ifdef PHP_WIN32
     130             : #include "win32_internal_function_disabled.h"
     131             : 
     132             : static php_win32_disable_functions(TSRMLS_D)
     133             : {
     134             :         int i;
     135             : 
     136             :         if (EG(windows_version_info).dwMajorVersion < 5) {
     137             :                 for (i = 0; i < function_name_cnt_5; i++) {
     138             :                         if (zend_hash_del(CG(function_table), function_name_5[i], strlen(function_name_5[i]) + 1)==FAILURE) {
     139             :                                 php_printf("Unable to disable function '%s'\n", function_name_5[i]);
     140             :                                 return FAILURE;
     141             :                         }
     142             :                 }
     143             :         }
     144             : 
     145             :         if (EG(windows_version_info).dwMajorVersion < 6) {
     146             :                 for (i = 0; i < function_name_cnt_6; i++) {
     147             :                         if (zend_hash_del(CG(function_table), function_name_6[i], strlen(function_name_6[i]) + 1)==FAILURE) {
     148             :                                 php_printf("Unable to disable function '%s'\n", function_name_6[i]);
     149             :                                 return FAILURE;
     150             :                         }
     151             :                 }
     152             :         }
     153             :         return SUCCESS;
     154             : }
     155             : #endif
     156             : 
     157             : #define SAFE_FILENAME(f) ((f)?(f):"-")
     158             : 
     159             : /* {{{ PHP_INI_MH
     160             :  */
     161       21840 : static PHP_INI_MH(OnSetPrecision)
     162             : {
     163       21840 :         int i = atoi(new_value);
     164       21840 :         if (i >= 0) {
     165       21840 :                 EG(precision) = i;
     166       21840 :                 return SUCCESS;
     167             :         } else {
     168           0 :                 return FAILURE;
     169             :         }
     170             : }
     171             : /* }}} */
     172             : 
     173             : /* {{{ PHP_INI_MH
     174             :  */
     175       21261 : static PHP_INI_MH(OnChangeMemoryLimit)
     176             : {
     177       21261 :         if (new_value) {
     178       21261 :                 PG(memory_limit) = zend_atol(new_value, new_value_length);
     179             :         } else {
     180           0 :                 PG(memory_limit) = 1<<30;         /* effectively, no limit */
     181             :         }
     182       21261 :         return zend_set_memory_limit(PG(memory_limit) TSRMLS_CC);
     183             : }
     184             : /* }}} */
     185             : 
     186             : 
     187             : /* {{{ php_disable_functions
     188             :  */
     189       21257 : static void php_disable_functions(TSRMLS_D)
     190             : {
     191       21257 :         char *s = NULL, *e;
     192             : 
     193       21257 :         if (!*(INI_STR("disable_functions"))) {
     194       21254 :                 return;
     195             :         }
     196             : 
     197           3 :         e = PG(disable_functions) = strdup(INI_STR("disable_functions"));
     198           3 :         if (e == NULL) {
     199           0 :                 return;
     200             :         }
     201          27 :         while (*e) {
     202          21 :                 switch (*e) {
     203             :                         case ' ':
     204             :                         case ',':
     205           0 :                                 if (s) {
     206           0 :                                         *e = '\0';
     207           0 :                                         zend_disable_function(s, e-s TSRMLS_CC);
     208           0 :                                         s = NULL;
     209             :                                 }
     210           0 :                                 break;
     211             :                         default:
     212          21 :                                 if (!s) {
     213           3 :                                         s = e;
     214             :                                 }
     215             :                                 break;
     216             :                 }
     217          21 :                 e++;
     218             :         }
     219           3 :         if (s) {
     220           3 :                 zend_disable_function(s, e-s TSRMLS_CC);
     221             :         }
     222             : }
     223             : /* }}} */
     224             : 
     225             : /* {{{ php_disable_classes
     226             :  */
     227       21257 : static void php_disable_classes(TSRMLS_D)
     228             : {
     229       21257 :         char *s = NULL, *e;
     230             : 
     231       21257 :         if (!*(INI_STR("disable_classes"))) {
     232       21256 :                 return;
     233             :         }
     234             : 
     235           1 :         e = PG(disable_classes) = strdup(INI_STR("disable_classes"));
     236             : 
     237          10 :         while (*e) {
     238           8 :                 switch (*e) {
     239             :                         case ' ':
     240             :                         case ',':
     241           0 :                                 if (s) {
     242           0 :                                         *e = '\0';
     243           0 :                                         zend_disable_class(s, e-s TSRMLS_CC);
     244           0 :                                         s = NULL;
     245             :                                 }
     246           0 :                                 break;
     247             :                         default:
     248           8 :                                 if (!s) {
     249           1 :                                         s = e;
     250             :                                 }
     251             :                                 break;
     252             :                 }
     253           8 :                 e++;
     254             :         }
     255           1 :         if (s) {
     256           1 :                 zend_disable_class(s, e-s TSRMLS_CC);
     257             :         }
     258             : }
     259             : /* }}} */
     260             : 
     261             : /* {{{ php_binary_init
     262             :  */
     263       21257 : static void php_binary_init(TSRMLS_D)
     264             : {
     265             :         char *binary_location;
     266             : #ifdef PHP_WIN32
     267             :         binary_location = (char *)malloc(MAXPATHLEN);
     268             :         if (GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) {
     269             :                 free(binary_location);
     270             :                 PG(php_binary) = NULL;
     271             :         }
     272             : #else
     273       21257 :         if (sapi_module.executable_location) {
     274       21257 :                 binary_location = (char *)malloc(MAXPATHLEN);
     275       21257 :                 if (!strchr(sapi_module.executable_location, '/')) {
     276             :                         char *envpath, *path;
     277           0 :                         int found = 0;
     278             : 
     279           0 :                         if ((envpath = getenv("PATH")) != NULL) {
     280             :                                 char *search_dir, search_path[MAXPATHLEN];
     281           0 :                                 char *last = NULL;
     282             :                                 struct stat s;
     283             : 
     284           0 :                                 path = estrdup(envpath);
     285           0 :                                 search_dir = php_strtok_r(path, ":", &last);
     286             : 
     287           0 :                                 while (search_dir) {
     288           0 :                                         snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location);
     289           0 :                                         if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK) && VCWD_STAT(binary_location, &s) == 0 && S_ISREG(s.st_mode)) {
     290           0 :                                                 found = 1;
     291           0 :                                                 break;
     292             :                                         }
     293           0 :                                         search_dir = php_strtok_r(NULL, ":", &last);
     294             :                                 }
     295           0 :                                 efree(path);
     296             :                         }
     297           0 :                         if (!found) {
     298           0 :                                 free(binary_location);
     299           0 :                                 binary_location = NULL;
     300             :                         }
     301       21257 :                 } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) {
     302           0 :                         free(binary_location);
     303           0 :                         binary_location = NULL;
     304             :                 }
     305             :         } else {
     306           0 :                 binary_location = NULL;
     307             :         }
     308             : #endif
     309       21257 :         PG(php_binary) = binary_location;
     310       21257 : }
     311             : /* }}} */
     312             : 
     313             : /* {{{ PHP_INI_MH
     314             :  */
     315       21292 : static PHP_INI_MH(OnUpdateTimeout)
     316             : {
     317       21292 :         if (stage==PHP_INI_STAGE_STARTUP) {
     318             :                 /* Don't set a timeout on startup, only per-request */
     319       21257 :                 EG(timeout_seconds) = atoi(new_value);
     320       21257 :                 return SUCCESS;
     321             :         }
     322          35 :         zend_unset_timeout(TSRMLS_C);
     323          35 :         EG(timeout_seconds) = atoi(new_value);
     324          35 :         zend_set_timeout(EG(timeout_seconds), 0);
     325          35 :         return SUCCESS;
     326             : }
     327             : /* }}} */
     328             : 
     329             : /* {{{ php_get_display_errors_mode() helper function
     330             :  */
     331       21575 : static int php_get_display_errors_mode(char *value, int value_length)
     332             : {
     333             :         int mode;
     334             : 
     335       21575 :         if (!value) {
     336           0 :                 return PHP_DISPLAY_ERRORS_STDOUT;
     337             :         }
     338             : 
     339       21576 :         if (value_length == 2 && !strcasecmp("on", value)) {
     340           1 :                 mode = PHP_DISPLAY_ERRORS_STDOUT;
     341       21574 :         } else if (value_length == 3 && !strcasecmp("yes", value)) {
     342           0 :                 mode = PHP_DISPLAY_ERRORS_STDOUT;
     343       21574 :         } else if (value_length == 4 && !strcasecmp("true", value)) {
     344           0 :                 mode = PHP_DISPLAY_ERRORS_STDOUT;
     345       21576 :         } else if (value_length == 6 && !strcasecmp(value, "stderr")) {
     346           2 :                 mode = PHP_DISPLAY_ERRORS_STDERR;
     347       21573 :         } else if (value_length == 6 && !strcasecmp(value, "stdout")) {
     348           1 :                 mode = PHP_DISPLAY_ERRORS_STDOUT;
     349             :         } else {
     350       21571 :                 mode = atoi(value);
     351       21571 :                 if (mode && mode != PHP_DISPLAY_ERRORS_STDOUT && mode != PHP_DISPLAY_ERRORS_STDERR) {
     352           0 :                         mode = PHP_DISPLAY_ERRORS_STDOUT;
     353             :                 }
     354             :         }
     355             : 
     356       21575 :         return mode;
     357             : }
     358             : /* }}} */
     359             : 
     360             : /* {{{ PHP_INI_MH
     361             :  */
     362       21275 : static PHP_INI_MH(OnUpdateDisplayErrors)
     363             : {
     364       21275 :         PG(display_errors) = (zend_bool) php_get_display_errors_mode(new_value, new_value_length);
     365             : 
     366       21275 :         return SUCCESS;
     367             : }
     368             : /* }}} */
     369             : 
     370             : /* {{{ PHP_INI_DISP
     371             :  */
     372         300 : static PHP_INI_DISP(display_errors_mode)
     373             : {
     374             :         int mode, tmp_value_length, cgi_or_cli;
     375             :         char *tmp_value;
     376             :         TSRMLS_FETCH();
     377             : 
     378         300 :         if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
     379           0 :                 tmp_value = (ini_entry->orig_value ? ini_entry->orig_value : NULL );
     380           0 :                 tmp_value_length = ini_entry->orig_value_length;
     381         300 :         } else if (ini_entry->value) {
     382         300 :                 tmp_value = ini_entry->value;
     383         300 :                 tmp_value_length = ini_entry->value_length;
     384             :         } else {
     385           0 :                 tmp_value = NULL;
     386           0 :                 tmp_value_length = 0;
     387             :         }
     388             : 
     389         300 :         mode = php_get_display_errors_mode(tmp_value, tmp_value_length);
     390             : 
     391             :         /* Display 'On' for other SAPIs instead of STDOUT or STDERR */
     392         300 :         cgi_or_cli = (!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi"));
     393             : 
     394         300 :         switch (mode) {
     395             :                 case PHP_DISPLAY_ERRORS_STDERR:
     396           0 :                         if (cgi_or_cli ) {
     397           0 :                                 PUTS("STDERR");
     398             :                         } else {
     399           0 :                                 PUTS("On");
     400             :                         }
     401           0 :                         break;
     402             : 
     403             :                 case PHP_DISPLAY_ERRORS_STDOUT:
     404         258 :                         if (cgi_or_cli ) {
     405         256 :                                 PUTS("STDOUT");
     406             :                         } else {
     407           2 :                                 PUTS("On");
     408             :                         }
     409         258 :                         break;
     410             : 
     411             :                 default:
     412          42 :                         PUTS("Off");
     413             :                         break;
     414             :         }
     415         300 : }
     416             : /* }}} */
     417             : 
     418             : /* {{{ PHP_INI_MH
     419             :  */
     420       21265 : static PHP_INI_MH(OnUpdateInternalEncoding)
     421             : {
     422       21265 :         if (new_value) {
     423          14 :                 OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     424             :         } else {
     425       21251 :                 OnUpdateString(entry, SG(default_charset), strlen(SG(default_charset))+1, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     426             :         }
     427       21265 :         return SUCCESS;
     428             : }
     429             : /* }}} */
     430             : 
     431             : /* {{{ PHP_INI_MH
     432             :  */
     433       21265 : static PHP_INI_MH(OnUpdateInputEncoding)
     434             : {
     435       21265 :         if (new_value) {
     436          14 :                 OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     437             :         } else {
     438       21251 :                 OnUpdateString(entry, SG(default_charset), strlen(SG(default_charset))+1, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     439             :         }
     440       21265 :         return SUCCESS;
     441             : }
     442             : /* }}} */
     443             : 
     444             : /* {{{ PHP_INI_MH
     445             :  */
     446       21265 : static PHP_INI_MH(OnUpdateOutputEncoding)
     447             : {
     448       21265 :         if (new_value) {
     449          14 :                 OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     450             :         } else {
     451       21251 :                 OnUpdateString(entry, SG(default_charset), strlen(SG(default_charset))+1, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     452             :         }
     453       21265 :         return SUCCESS;
     454             : }
     455             : /* }}} */
     456             : 
     457             : /* {{{ PHP_INI_MH
     458             :  */
     459       21262 : static PHP_INI_MH(OnUpdateErrorLog)
     460             : {
     461             :         /* Only do the safemode/open_basedir check at runtime */
     462       21262 :         if ((stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) && new_value && strcmp(new_value, "syslog")) {
     463           3 :                 if (PG(open_basedir) && php_check_open_basedir(new_value TSRMLS_CC)) {
     464           0 :                         return FAILURE;
     465             :                 }
     466             :         }
     467       21262 :         OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     468       21262 :         return SUCCESS;
     469             : }
     470             : /* }}} */
     471             : 
     472             : /* {{{ PHP_INI_MH
     473             :  */
     474       21257 : static PHP_INI_MH(OnUpdateMailLog)
     475             : {
     476             :         /* Only do the safemode/open_basedir check at runtime */
     477       21257 :         if ((stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) && new_value) {
     478           0 :                 if (PG(open_basedir) && php_check_open_basedir(new_value TSRMLS_CC)) {
     479           0 :                         return FAILURE;
     480             :                 }
     481             :         }
     482       21257 :         OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     483       21257 :         return SUCCESS;
     484             : }
     485             : /* }}} */
     486             : 
     487             : /* {{{ PHP_INI_MH
     488             :  */
     489       21257 : static PHP_INI_MH(OnChangeMailForceExtra)
     490             : {
     491             :         /* Don't allow changing it in htaccess */
     492       21257 :         if (stage == PHP_INI_STAGE_HTACCESS) {
     493           0 :                         return FAILURE;
     494             :         }
     495       21257 :         return SUCCESS;
     496             : }
     497             : /* }}} */
     498             : 
     499             : /* defined in browscap.c */
     500             : PHP_INI_MH(OnChangeBrowscap);
     501             : 
     502             : 
     503             : /* Need to be read from the environment (?):
     504             :  * PHP_AUTO_PREPEND_FILE
     505             :  * PHP_AUTO_APPEND_FILE
     506             :  * PHP_DOCUMENT_ROOT
     507             :  * PHP_USER_DIR
     508             :  * PHP_INCLUDE_PATH
     509             :  */
     510             : 
     511             :  /* Windows and Netware use the internal mail */
     512             : #if defined(PHP_WIN32) || defined(NETWARE)
     513             : # define DEFAULT_SENDMAIL_PATH NULL
     514             : #elif defined(PHP_PROG_SENDMAIL)
     515             : # define DEFAULT_SENDMAIL_PATH PHP_PROG_SENDMAIL " -t -i "
     516             : #else
     517             : # define DEFAULT_SENDMAIL_PATH "/usr/sbin/sendmail -t -i"
     518             : #endif
     519             : 
     520             : /* {{{ PHP_INI
     521             :  */
     522             : PHP_INI_BEGIN()
     523             :         PHP_INI_ENTRY_EX("highlight.comment",         HL_COMMENT_COLOR,       PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
     524             :         PHP_INI_ENTRY_EX("highlight.default",         HL_DEFAULT_COLOR,       PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
     525             :         PHP_INI_ENTRY_EX("highlight.html",                    HL_HTML_COLOR,          PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
     526             :         PHP_INI_ENTRY_EX("highlight.keyword",         HL_KEYWORD_COLOR,       PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
     527             :         PHP_INI_ENTRY_EX("highlight.string",          HL_STRING_COLOR,        PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
     528             : 
     529             :         STD_PHP_INI_BOOLEAN("asp_tags",                               "0",          PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   asp_tags,                               zend_compiler_globals,  compiler_globals)
     530             :         STD_PHP_INI_ENTRY_EX("display_errors",                "1",          PHP_INI_ALL,            OnUpdateDisplayErrors,  display_errors,                 php_core_globals,       core_globals, display_errors_mode)
     531             :         STD_PHP_INI_BOOLEAN("display_startup_errors", "0",  PHP_INI_ALL,            OnUpdateBool,                   display_startup_errors, php_core_globals,       core_globals)
     532             :         STD_PHP_INI_BOOLEAN("enable_dl",                      "1",          PHP_INI_SYSTEM,         OnUpdateBool,                   enable_dl,                              php_core_globals,       core_globals)
     533             :         STD_PHP_INI_BOOLEAN("expose_php",                     "1",          PHP_INI_SYSTEM,         OnUpdateBool,                   expose_php,                             php_core_globals,       core_globals)
     534             :         STD_PHP_INI_ENTRY("docref_root",                      "",           PHP_INI_ALL,            OnUpdateString,                 docref_root,                    php_core_globals,       core_globals)
     535             :         STD_PHP_INI_ENTRY("docref_ext",                               "",                   PHP_INI_ALL,            OnUpdateString,                 docref_ext,                             php_core_globals,       core_globals)
     536             :         STD_PHP_INI_BOOLEAN("html_errors",                    "1",          PHP_INI_ALL,            OnUpdateBool,                   html_errors,                    php_core_globals,       core_globals)
     537             :         STD_PHP_INI_BOOLEAN("xmlrpc_errors",          "0",          PHP_INI_SYSTEM,         OnUpdateBool,                   xmlrpc_errors,                  php_core_globals,       core_globals)
     538             :         STD_PHP_INI_ENTRY("xmlrpc_error_number",      "0",          PHP_INI_ALL,            OnUpdateLong,                   xmlrpc_error_number,    php_core_globals,       core_globals)
     539             :         STD_PHP_INI_ENTRY("max_input_time",                   "-1", PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLong,                   max_input_time, php_core_globals,       core_globals)
     540             :         STD_PHP_INI_BOOLEAN("ignore_user_abort",      "0",          PHP_INI_ALL,            OnUpdateBool,                   ignore_user_abort,              php_core_globals,       core_globals)
     541             :         STD_PHP_INI_BOOLEAN("implicit_flush",         "0",          PHP_INI_ALL,            OnUpdateBool,                   implicit_flush,                 php_core_globals,       core_globals)
     542             :         STD_PHP_INI_BOOLEAN("log_errors",                     "0",          PHP_INI_ALL,            OnUpdateBool,                   log_errors,                             php_core_globals,       core_globals)
     543             :         STD_PHP_INI_ENTRY("log_errors_max_len",        "1024",              PHP_INI_ALL,            OnUpdateLong,                   log_errors_max_len,             php_core_globals,       core_globals)
     544             :         STD_PHP_INI_BOOLEAN("ignore_repeated_errors", "0",  PHP_INI_ALL,            OnUpdateBool,                   ignore_repeated_errors, php_core_globals,       core_globals)
     545             :         STD_PHP_INI_BOOLEAN("ignore_repeated_source", "0",  PHP_INI_ALL,            OnUpdateBool,                   ignore_repeated_source, php_core_globals,       core_globals)
     546             :         STD_PHP_INI_BOOLEAN("report_memleaks",                "1",          PHP_INI_ALL,            OnUpdateBool,                   report_memleaks,                php_core_globals,       core_globals)
     547             :         STD_PHP_INI_BOOLEAN("report_zend_debug",      "1",          PHP_INI_ALL,            OnUpdateBool,                   report_zend_debug,              php_core_globals,       core_globals)
     548             :         STD_PHP_INI_ENTRY("output_buffering",         "0",          PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateLong,   output_buffering,               php_core_globals,       core_globals)
     549             :         STD_PHP_INI_ENTRY("output_handler",                   NULL,           PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateString, output_handler,         php_core_globals,       core_globals)
     550             :         STD_PHP_INI_BOOLEAN("register_argc_argv",     "1",          PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   register_argc_argv,             php_core_globals,       core_globals)
     551             :         STD_PHP_INI_BOOLEAN("auto_globals_jit",               "1",          PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   auto_globals_jit,       php_core_globals,       core_globals)
     552             :         STD_PHP_INI_BOOLEAN("short_open_tag", DEFAULT_SHORT_OPEN_TAG, PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   short_tags,                             zend_compiler_globals,  compiler_globals)
     553             :         STD_PHP_INI_BOOLEAN("sql.safe_mode",          "0",          PHP_INI_SYSTEM,         OnUpdateBool,                   sql_safe_mode,                  php_core_globals,       core_globals)
     554             :         STD_PHP_INI_BOOLEAN("track_errors",                   "0",          PHP_INI_ALL,            OnUpdateBool,                   track_errors,                   php_core_globals,       core_globals)
     555             : 
     556             :         STD_PHP_INI_ENTRY("unserialize_callback_func",        NULL,   PHP_INI_ALL,            OnUpdateString,                 unserialize_callback_func,      php_core_globals,       core_globals)
     557             :         STD_PHP_INI_ENTRY("serialize_precision",      "17", PHP_INI_ALL,            OnUpdateLongGEZero,                     serialize_precision,    php_core_globals,       core_globals)
     558             :         STD_PHP_INI_ENTRY("arg_separator.output",     "&",              PHP_INI_ALL,            OnUpdateStringUnempty,  arg_separator.output,   php_core_globals,       core_globals)
     559             :         STD_PHP_INI_ENTRY("arg_separator.input",      "&",              PHP_INI_SYSTEM|PHP_INI_PERDIR,  OnUpdateStringUnempty,  arg_separator.input,    php_core_globals,       core_globals)
     560             : 
     561             :         STD_PHP_INI_ENTRY("auto_append_file",         NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString,                 auto_append_file,               php_core_globals,       core_globals)
     562             :         STD_PHP_INI_ENTRY("auto_prepend_file",                NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString,                 auto_prepend_file,              php_core_globals,       core_globals)
     563             :         STD_PHP_INI_ENTRY("doc_root",                         NULL,           PHP_INI_SYSTEM,         OnUpdateStringUnempty,  doc_root,                               php_core_globals,       core_globals)
     564             :         STD_PHP_INI_ENTRY("default_charset",          PHP_DEFAULT_CHARSET,    PHP_INI_ALL,    OnUpdateString,                 default_charset,                sapi_globals_struct, sapi_globals)
     565             :         STD_PHP_INI_ENTRY("default_mimetype",         SAPI_DEFAULT_MIMETYPE,  PHP_INI_ALL,    OnUpdateString,                 default_mimetype,               sapi_globals_struct, sapi_globals)
     566             :         STD_PHP_INI_ENTRY("internal_encoding",                NULL,                   PHP_INI_ALL,    OnUpdateInternalEncoding,       internal_encoding,      php_core_globals, core_globals)
     567             :         STD_PHP_INI_ENTRY("input_encoding",                   NULL,                   PHP_INI_ALL,    OnUpdateInputEncoding,                          input_encoding,         php_core_globals, core_globals)
     568             :         STD_PHP_INI_ENTRY("output_encoding",          NULL,                   PHP_INI_ALL,    OnUpdateOutputEncoding,                         output_encoding,        php_core_globals, core_globals)
     569             :         STD_PHP_INI_ENTRY("error_log",                                NULL,           PHP_INI_ALL,            OnUpdateErrorLog,                       error_log,                              php_core_globals,       core_globals)
     570             :         STD_PHP_INI_ENTRY("extension_dir",                    PHP_EXTENSION_DIR,              PHP_INI_SYSTEM,         OnUpdateStringUnempty,  extension_dir,                  php_core_globals,       core_globals)
     571             :         STD_PHP_INI_ENTRY("sys_temp_dir",                     NULL,           PHP_INI_SYSTEM,         OnUpdateStringUnempty,  sys_temp_dir,                   php_core_globals,       core_globals)
     572             :         STD_PHP_INI_ENTRY("include_path",                     PHP_INCLUDE_PATH,               PHP_INI_ALL,            OnUpdateStringUnempty,  include_path,                   php_core_globals,       core_globals)
     573             :         PHP_INI_ENTRY("max_execution_time",                   "30",         PHP_INI_ALL,                    OnUpdateTimeout)
     574             :         STD_PHP_INI_ENTRY("open_basedir",                     NULL,           PHP_INI_ALL,            OnUpdateBaseDir,                        open_basedir,                   php_core_globals,       core_globals)
     575             : 
     576             :         STD_PHP_INI_BOOLEAN("file_uploads",                   "1",          PHP_INI_SYSTEM,         OnUpdateBool,                   file_uploads,                   php_core_globals,       core_globals)
     577             :         STD_PHP_INI_ENTRY("upload_max_filesize",      "2M",         PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLong,                   upload_max_filesize,    php_core_globals,       core_globals)
     578             :         STD_PHP_INI_ENTRY("post_max_size",                    "8M",         PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLong,                   post_max_size,                  sapi_globals_struct,sapi_globals)
     579             :         STD_PHP_INI_ENTRY("upload_tmp_dir",                   NULL,           PHP_INI_SYSTEM,         OnUpdateStringUnempty,  upload_tmp_dir,                 php_core_globals,       core_globals)
     580             :         STD_PHP_INI_ENTRY("max_input_nesting_level", "64",          PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLongGEZero,     max_input_nesting_level,                        php_core_globals,       core_globals)
     581             :         STD_PHP_INI_ENTRY("max_input_vars",                   "1000",               PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLongGEZero,     max_input_vars,                                         php_core_globals,       core_globals)
     582             : 
     583             :         STD_PHP_INI_ENTRY("user_dir",                         NULL,           PHP_INI_SYSTEM,         OnUpdateString,                 user_dir,                               php_core_globals,       core_globals)
     584             :         STD_PHP_INI_ENTRY("variables_order",          "EGPCS",      PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateStringUnempty,  variables_order,                php_core_globals,       core_globals)
     585             :         STD_PHP_INI_ENTRY("request_order",                    NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString, request_order,          php_core_globals,       core_globals)
     586             : 
     587             :         STD_PHP_INI_ENTRY("error_append_string",      NULL,           PHP_INI_ALL,            OnUpdateString,                 error_append_string,    php_core_globals,       core_globals)
     588             :         STD_PHP_INI_ENTRY("error_prepend_string",     NULL,           PHP_INI_ALL,            OnUpdateString,                 error_prepend_string,   php_core_globals,       core_globals)
     589             : 
     590             :         PHP_INI_ENTRY("SMTP",                                         "localhost",PHP_INI_ALL,              NULL)
     591             :         PHP_INI_ENTRY("smtp_port",                                    "25",         PHP_INI_ALL,            NULL)
     592             :         STD_PHP_INI_BOOLEAN("mail.add_x_header",                      "0",          PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   mail_x_header,                  php_core_globals,       core_globals)
     593             :         STD_PHP_INI_ENTRY("mail.log",                                 NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateMailLog,                        mail_log,                       php_core_globals,       core_globals)
     594             :         PHP_INI_ENTRY("browscap",                                     NULL,           PHP_INI_SYSTEM,         OnChangeBrowscap)
     595             :         PHP_INI_ENTRY("memory_limit",                         "128M",               PHP_INI_ALL,            OnChangeMemoryLimit)
     596             :         PHP_INI_ENTRY("precision",                                    "14",         PHP_INI_ALL,            OnSetPrecision)
     597             :         PHP_INI_ENTRY("sendmail_from",                                NULL,           PHP_INI_ALL,            NULL)
     598             :         PHP_INI_ENTRY("sendmail_path",        DEFAULT_SENDMAIL_PATH,  PHP_INI_SYSTEM,         NULL)
     599             :         PHP_INI_ENTRY("mail.force_extra_parameters",NULL,             PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnChangeMailForceExtra)
     600             :         PHP_INI_ENTRY("disable_functions",                    "",                   PHP_INI_SYSTEM,         NULL)
     601             :         PHP_INI_ENTRY("disable_classes",                      "",                   PHP_INI_SYSTEM,         NULL)
     602             :         PHP_INI_ENTRY("max_file_uploads",                     "20",                 PHP_INI_SYSTEM|PHP_INI_PERDIR,          NULL)
     603             : 
     604             :         STD_PHP_INI_BOOLEAN("allow_url_fopen",                "1",          PHP_INI_SYSTEM,         OnUpdateBool,           allow_url_fopen,                php_core_globals,               core_globals)
     605             :         STD_PHP_INI_BOOLEAN("allow_url_include",      "0",          PHP_INI_SYSTEM,         OnUpdateBool,           allow_url_include,              php_core_globals,               core_globals)
     606             :         STD_PHP_INI_BOOLEAN("enable_post_data_reading",       "1",  PHP_INI_SYSTEM|PHP_INI_PERDIR,  OnUpdateBool,   enable_post_data_reading,       php_core_globals,       core_globals)
     607             : 
     608             :         STD_PHP_INI_ENTRY("realpath_cache_size",      "16K",                PHP_INI_SYSTEM,         OnUpdateLong,   realpath_cache_size_limit,      virtual_cwd_globals,    cwd_globals)
     609             :         STD_PHP_INI_ENTRY("realpath_cache_ttl",               "120",                PHP_INI_SYSTEM,         OnUpdateLong,   realpath_cache_ttl,                     virtual_cwd_globals,    cwd_globals)
     610             : 
     611             :         STD_PHP_INI_ENTRY("user_ini.filename",                ".user.ini",  PHP_INI_SYSTEM,         OnUpdateString,         user_ini_filename,      php_core_globals,               core_globals)
     612             :         STD_PHP_INI_ENTRY("user_ini.cache_ttl",               "300",                        PHP_INI_SYSTEM,         OnUpdateLong,           user_ini_cache_ttl,     php_core_globals,               core_globals)
     613             :         STD_PHP_INI_BOOLEAN("exit_on_timeout",                "0",          PHP_INI_ALL,            OnUpdateBool,                   exit_on_timeout,                        php_core_globals,       core_globals)
     614             : #ifdef PHP_WIN32
     615             :         STD_PHP_INI_BOOLEAN("windows.show_crt_warning",               "0",          PHP_INI_ALL,            OnUpdateBool,                   windows_show_crt_warning,                       php_core_globals,       core_globals)
     616             : #endif
     617             : PHP_INI_END()
     618             : /* }}} */
     619             : 
     620             : /* True globals (no need for thread safety */
     621             : /* But don't make them a single int bitfield */
     622             : static int module_initialized = 0;
     623             : static int module_startup = 1;
     624             : static int module_shutdown = 0;
     625             : 
     626             : /* {{{ php_during_module_startup */
     627      202166 : static int php_during_module_startup(void)
     628             : {
     629      202166 :         return module_startup;
     630             : }
     631             : /* }}} */
     632             : 
     633             : /* {{{ php_during_module_shutdown */
     634      202157 : static int php_during_module_shutdown(void)
     635             : {
     636      202157 :         return module_shutdown;
     637             : }
     638             : /* }}} */
     639             : 
     640             : /* {{{ php_get_module_initialized
     641             :  */
     642           0 : PHPAPI int php_get_module_initialized(void)
     643             : {
     644           0 :         return module_initialized;
     645             : }
     646             : /* }}} */
     647             : 
     648             : /* {{{ php_log_err
     649             :  */
     650          13 : PHPAPI void php_log_err(char *log_message TSRMLS_DC)
     651             : {
     652          13 :         int fd = -1;
     653             :         time_t error_time;
     654             : 
     655          13 :         if (PG(in_error_log)) {
     656             :                 /* prevent recursive invocation */
     657           0 :                 return;
     658             :         }
     659          13 :         PG(in_error_log) = 1;
     660             : 
     661             :         /* Try to use the specified logging location. */
     662          13 :         if (PG(error_log) != NULL) {
     663             : #ifdef HAVE_SYSLOG_H
     664          10 :                 if (!strcmp(PG(error_log), "syslog")) {
     665           0 :                         php_syslog(LOG_NOTICE, "%s", log_message);
     666           0 :                         PG(in_error_log) = 0;
     667           0 :                         return;
     668             :                 }
     669             : #endif
     670          10 :                 fd = VCWD_OPEN_MODE(PG(error_log), O_CREAT | O_APPEND | O_WRONLY, 0644);
     671          10 :                 if (fd != -1) {
     672             :                         char *tmp;
     673             :                         int len;
     674             :                         char *error_time_str;
     675             : 
     676           2 :                         time(&error_time);
     677             : #ifdef ZTS
     678             :                         if (!php_during_module_startup()) {
     679             :                                 error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1 TSRMLS_CC);
     680             :                         } else {
     681             :                                 error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 0 TSRMLS_CC);
     682             :                         }
     683             : #else
     684           2 :                         error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1 TSRMLS_CC);
     685             : #endif
     686           2 :                         len = spprintf(&tmp, 0, "[%s] %s%s", error_time_str, log_message, PHP_EOL);
     687             : #ifdef PHP_WIN32
     688             :                         php_flock(fd, 2);
     689             : #endif
     690           2 :                         php_ignore_value(write(fd, tmp, len));
     691           2 :                         efree(tmp);
     692           2 :                         efree(error_time_str);
     693           2 :                         close(fd);
     694           2 :                         PG(in_error_log) = 0;
     695           2 :                         return;
     696             :                 }
     697             :         }
     698             : 
     699             :         /* Otherwise fall back to the default logging location, if we have one */
     700             : 
     701          11 :         if (sapi_module.log_message) {
     702          11 :                 sapi_module.log_message(log_message TSRMLS_CC);
     703             :         }
     704          11 :         PG(in_error_log) = 0;
     705             : }
     706             : /* }}} */
     707             : 
     708             : /* {{{ php_write
     709             :    wrapper for modules to use PHPWRITE */
     710         664 : PHPAPI int php_write(void *buf, uint size TSRMLS_DC)
     711             : {
     712         664 :         return PHPWRITE(buf, size);
     713             : }
     714             : /* }}} */
     715             : 
     716             : /* {{{ php_printf
     717             :  */
     718      437348 : PHPAPI int php_printf(const char *format, ...)
     719             : {
     720             :         va_list args;
     721             :         int ret;
     722             :         char *buffer;
     723             :         int size;
     724             :         TSRMLS_FETCH();
     725             : 
     726      437348 :         va_start(args, format);
     727      437348 :         size = vspprintf(&buffer, 0, format, args);
     728      437348 :         ret = PHPWRITE(buffer, size);
     729      437347 :         efree(buffer);
     730      437347 :         va_end(args);
     731             : 
     732      437347 :         return ret;
     733             : }
     734             : /* }}} */
     735             : 
     736             : /* {{{ php_verror */
     737             : /* php_verror is called from php_error_docref<n> functions.
     738             :  * Its purpose is to unify error messages and automatically generate clickable
     739             :  * html error messages if correcponding ini setting (html_errors) is activated.
     740             :  * See: CODING_STANDARDS for details.
     741             :  */
     742      202166 : PHPAPI void php_verror(const char *docref, const char *params, int type, const char *format, va_list args TSRMLS_DC)
     743             : {
     744      202166 :         char *buffer = NULL, *docref_buf = NULL, *target = NULL;
     745      202166 :         char *docref_target = "", *docref_root = "";
     746             :         char *p;
     747      202166 :         int buffer_len = 0;
     748      202166 :         const char *space = "";
     749      202166 :         const char *class_name = "";
     750             :         const char *function;
     751             :         int origin_len;
     752             :         char *origin;
     753             :         char *message;
     754      202166 :         int is_function = 0;
     755             : 
     756             :         /* get error text into buffer and escape for html if necessary */
     757      202166 :         buffer_len = vspprintf(&buffer, 0, format, args);
     758             : 
     759      202166 :         if (PG(html_errors)) {
     760             :                 size_t len;
     761           2 :                 char *replace = php_escape_html_entities(buffer, buffer_len, &len, 0, ENT_COMPAT, NULL TSRMLS_CC);
     762           2 :                 efree(buffer);
     763           2 :                 buffer = replace;
     764           2 :                 buffer_len = len;
     765             :         }
     766             : 
     767             :         /* which function caused the problem if any at all */
     768      202166 :         if (php_during_module_startup()) {
     769           9 :                 function = "PHP Startup";
     770      202157 :         } else if (php_during_module_shutdown()) {
     771           1 :                 function = "PHP Shutdown";
     772      606481 :         } else if (EG(current_execute_data) &&
     773      202140 :                                 EG(current_execute_data)->opline &&
     774      202118 :                                 EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL
     775             :         ) {
     776          67 :                 switch (EG(current_execute_data)->opline->extended_value) {
     777             :                         case ZEND_EVAL:
     778           0 :                                 function = "eval";
     779           0 :                                 is_function = 1;
     780           0 :                                 break;
     781             :                         case ZEND_INCLUDE:
     782          62 :                                 function = "include";
     783          62 :                                 is_function = 1;
     784          62 :                                 break;
     785             :                         case ZEND_INCLUDE_ONCE:
     786           0 :                                 function = "include_once";
     787           0 :                                 is_function = 1;
     788           0 :                                 break;
     789             :                         case ZEND_REQUIRE:
     790           1 :                                 function = "require";
     791           1 :                                 is_function = 1;
     792           1 :                                 break;
     793             :                         case ZEND_REQUIRE_ONCE:
     794           4 :                                 function = "require_once";
     795           4 :                                 is_function = 1;
     796           4 :                                 break;
     797             :                         default:
     798           0 :                                 function = "Unknown";
     799             :                 }
     800             :         } else {
     801      202089 :                 function = get_active_function_name(TSRMLS_C);
     802      202108 :                 if (!function || !strlen(function)) {
     803          19 :                         function = "Unknown";
     804             :                 } else {
     805      202070 :                         is_function = 1;
     806      202070 :                         class_name = get_active_class_name(&space TSRMLS_CC);
     807             :                 }
     808             :         }
     809             : 
     810             :         /* if we still have memory then format the origin */
     811      202166 :         if (is_function) {
     812      202137 :                 origin_len = spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);
     813             :         } else {
     814          29 :                 origin_len = spprintf(&origin, 0, "%s", function);
     815             :         }
     816             : 
     817      202166 :         if (PG(html_errors)) {
     818             :                 size_t len;
     819           2 :                 char *replace = php_escape_html_entities(origin, origin_len, &len, 0, ENT_COMPAT, NULL TSRMLS_CC);
     820           2 :                 efree(origin);
     821           2 :                 origin = replace;
     822             :         }
     823             : 
     824             :         /* origin and buffer available, so lets come up with the error message */
     825      202166 :         if (docref && docref[0] == '#') {
     826           0 :                 docref_target = strchr(docref, '#');
     827           0 :                 docref = NULL;
     828             :         }
     829             : 
     830             :         /* no docref given but function is known (the default) */
     831      202166 :         if (!docref && is_function) {
     832             :                 int doclen;
     833      404092 :                 while (*function == '_') {
     834         218 :                         function++;
     835             :                 }
     836      201937 :                 if (space[0] == '\0') {
     837      200530 :                         doclen = spprintf(&docref_buf, 0, "function.%s", function);
     838             :                 } else {
     839        1407 :                         doclen = spprintf(&docref_buf, 0, "%s.%s", class_name, function);
     840             :                 }
     841      413491 :                 while((p = strchr(docref_buf, '_')) != NULL) {
     842        9617 :                         *p = '-';
     843             :                 }
     844      201937 :                 docref = php_strtolower(docref_buf, doclen);
     845             :         }
     846             : 
     847             :         /* we have a docref for a function AND
     848             :          * - we show errors in html mode AND
     849             :          * - the user wants to see the links
     850             :          */
     851      202168 :         if (docref && is_function && PG(html_errors) && strlen(PG(docref_root))) {
     852           2 :                 if (strncmp(docref, "http://", 7)) {
     853             :                         /* We don't have 'http://' so we use docref_root */
     854             : 
     855             :                         char *ref;  /* temp copy for duplicated docref */
     856             : 
     857           2 :                         docref_root = PG(docref_root);
     858             : 
     859           2 :                         ref = estrdup(docref);
     860           2 :                         if (docref_buf) {
     861           1 :                                 efree(docref_buf);
     862             :                         }
     863           2 :                         docref_buf = ref;
     864             :                         /* strip of the target if any */
     865           2 :                         p = strrchr(ref, '#');
     866           2 :                         if (p) {
     867           0 :                                 target = estrdup(p);
     868           0 :                                 if (target) {
     869           0 :                                         docref_target = target;
     870           0 :                                         *p = '\0';
     871             :                                 }
     872             :                         }
     873             :                         /* add the extension if it is set in ini */
     874           2 :                         if (PG(docref_ext) && strlen(PG(docref_ext))) {
     875           2 :                                 spprintf(&docref_buf, 0, "%s%s", ref, PG(docref_ext));
     876           2 :                                 efree(ref);
     877             :                         }
     878           2 :                         docref = docref_buf;
     879             :                 }
     880             :                 /* display html formatted or only show the additional links */
     881           2 :                 if (PG(html_errors)) {
     882           2 :                         spprintf(&message, 0, "%s [<a href='%s%s%s'>%s</a>]: %s", origin, docref_root, docref, docref_target, docref, buffer);
     883             :                 } else {
     884           0 :                         spprintf(&message, 0, "%s [%s%s%s]: %s", origin, docref_root, docref, docref_target, buffer);
     885             :                 }
     886           2 :                 if (target) {
     887           0 :                         efree(target);
     888             :                 }
     889             :         } else {
     890      202164 :                 spprintf(&message, 0, "%s: %s", origin, buffer);
     891             :         }
     892      202166 :         efree(origin);
     893      202166 :         if (docref_buf) {
     894      201938 :                 efree(docref_buf);
     895             :         }
     896             : 
     897      216703 :         if (PG(track_errors) && module_initialized &&
     898       14537 :                         (!EG(user_error_handler) || !(EG(user_error_handler_error_reporting) & type))) {
     899       12719 :                 if (!EG(active_symbol_table)) {
     900         425 :                         zend_rebuild_symbol_table(TSRMLS_C);
     901             :                 }
     902       12719 :                 if (EG(active_symbol_table)) {
     903             :                         zval *tmp;
     904       12718 :                         ALLOC_INIT_ZVAL(tmp);
     905       12718 :                         ZVAL_STRINGL(tmp, buffer, buffer_len, 1);
     906       12718 :                         zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) &tmp, sizeof(zval *), NULL);
     907             :                 }
     908             :         }
     909      202166 :         efree(buffer);
     910             : 
     911      202166 :         php_error(type, "%s", message);
     912      202120 :         efree(message);
     913      202120 : }
     914             : /* }}} */
     915             : 
     916             : /* {{{ php_error_docref0 */
     917             : /* See: CODING_STANDARDS for details. */
     918       12724 : PHPAPI void php_error_docref0(const char *docref TSRMLS_DC, int type, const char *format, ...)
     919             : {
     920             :         va_list args;
     921             : 
     922       12724 :         va_start(args, format);
     923       12724 :         php_verror(docref, "", type, format, args TSRMLS_CC);
     924       12678 :         va_end(args);
     925       12678 : }
     926             : /* }}} */
     927             : 
     928             : /* {{{ php_error_docref1 */
     929             : /* See: CODING_STANDARDS for details. */
     930      189312 : PHPAPI void php_error_docref1(const char *docref TSRMLS_DC, const char *param1, int type, const char *format, ...)
     931             : {
     932             :         va_list args;
     933             : 
     934      189312 :         va_start(args, format);
     935      189312 :         php_verror(docref, param1, type, format, args TSRMLS_CC);
     936      189312 :         va_end(args);
     937      189312 : }
     938             : /* }}} */
     939             : 
     940             : /* {{{ php_error_docref2 */
     941             : /* See: CODING_STANDARDS for details. */
     942          44 : PHPAPI void php_error_docref2(const char *docref TSRMLS_DC, const char *param1, const char *param2, int type, const char *format, ...)
     943             : {
     944             :         char *params;
     945             :         va_list args;
     946             : 
     947          44 :         spprintf(&params, 0, "%s,%s", param1, param2);
     948          44 :         va_start(args, format);
     949          44 :         php_verror(docref, params ? params : "...", type, format, args TSRMLS_CC);
     950          44 :         va_end(args);
     951          44 :         if (params) {
     952          44 :                 efree(params);
     953             :         }
     954          44 : }
     955             : /* }}} */
     956             : 
     957             : #ifdef PHP_WIN32
     958             : #define PHP_WIN32_ERROR_MSG_BUFFER_SIZE 512
     959             : PHPAPI void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2 TSRMLS_DC) {
     960             :         if (error == 0) {
     961             :                 php_error_docref2(NULL TSRMLS_CC, param1, param2, E_WARNING, "%s", strerror(errno));
     962             :         } else {
     963             :                 char buf[PHP_WIN32_ERROR_MSG_BUFFER_SIZE + 1];
     964             :                 int buf_len;
     965             : 
     966             :                 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, buf, PHP_WIN32_ERROR_MSG_BUFFER_SIZE, NULL);
     967             :                 buf_len = strlen(buf);
     968             :                 if (buf_len >= 2) {
     969             :                         buf[buf_len - 1] = '\0';
     970             :                         buf[buf_len - 2] = '\0';
     971             :                 }
     972             :                 php_error_docref2(NULL TSRMLS_CC, param1, param2, E_WARNING, "%s (code: %lu)", (char *)buf, error);
     973             :         }
     974             : }
     975             : #undef PHP_WIN32_ERROR_MSG_BUFFER_SIZE
     976             : #endif
     977             : 
     978             : /* {{{ php_html_puts */
     979         236 : PHPAPI void php_html_puts(const char *str, uint size TSRMLS_DC)
     980             : {
     981         236 :         zend_html_puts(str, size TSRMLS_CC);
     982         236 : }
     983             : /* }}} */
     984             : 
     985             : /* {{{ php_error_cb
     986             :  extended error handling function */
     987      378453 : static void php_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
     988             : {
     989             :         char *buffer;
     990             :         int buffer_len, display;
     991             :         TSRMLS_FETCH();
     992             : 
     993      378453 :         buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args);
     994             : 
     995             :         /* check for repeated errors to be ignored */
     996      378453 :         if (PG(ignore_repeated_errors) && PG(last_error_message)) {
     997             :                 /* no check for PG(last_error_file) is needed since it cannot
     998             :                  * be NULL if PG(last_error_message) is not NULL */
     999           0 :                 if (strcmp(PG(last_error_message), buffer)
    1000           0 :                         || (!PG(ignore_repeated_source)
    1001           0 :                                 && ((PG(last_error_lineno) != (int)error_lineno)
    1002           0 :                                         || strcmp(PG(last_error_file), error_filename)))) {
    1003           0 :                         display = 1;
    1004             :                 } else {
    1005           0 :                         display = 0;
    1006             :                 }
    1007             :         } else {
    1008      378453 :                 display = 1;
    1009             :         }
    1010             : 
    1011             :         /* store the error if it has changed */
    1012      378453 :         if (display) {
    1013             : #ifdef ZEND_SIGNALS
    1014             :                 HANDLE_BLOCK_INTERRUPTIONS();
    1015             : #endif
    1016      378453 :                 if (PG(last_error_message)) {
    1017      372891 :                         free(PG(last_error_message));
    1018      372891 :                         PG(last_error_message) = NULL;
    1019             :                 }
    1020      378453 :                 if (PG(last_error_file)) {
    1021      372891 :                         free(PG(last_error_file));
    1022      372891 :                         PG(last_error_file) = NULL;
    1023             :                 }
    1024             : #ifdef ZEND_SIGNALS
    1025             :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    1026             : #endif
    1027      378453 :                 if (!error_filename) {
    1028           2 :                         error_filename = "Unknown";
    1029             :                 }
    1030      378453 :                 PG(last_error_type) = type;
    1031      378453 :                 PG(last_error_message) = strdup(buffer);
    1032      378453 :                 PG(last_error_file) = strdup(error_filename);
    1033      378453 :                 PG(last_error_lineno) = error_lineno;
    1034             :         }
    1035             : 
    1036             :         /* according to error handling mode, suppress error, throw exception or show it */
    1037      378453 :         if (EG(error_handling) != EH_NORMAL) {
    1038         151 :                 switch (type) {
    1039             :                         case E_ERROR:
    1040             :                         case E_CORE_ERROR:
    1041             :                         case E_COMPILE_ERROR:
    1042             :                         case E_USER_ERROR:
    1043             :                         case E_PARSE:
    1044             :                                 /* fatal errors are real errors and cannot be made exceptions */
    1045           0 :                                 break;
    1046             :                         case E_STRICT:
    1047             :                         case E_DEPRECATED:
    1048             :                         case E_USER_DEPRECATED:
    1049             :                                 /* for the sake of BC to old damaged code */
    1050           0 :                                 break;
    1051             :                         case E_NOTICE:
    1052             :                         case E_USER_NOTICE:
    1053             :                                 /* notices are no errors and are not treated as such like E_WARNINGS */
    1054           1 :                                 break;
    1055             :                         default:
    1056             :                                 /* throw an exception if we are in EH_THROW mode
    1057             :                                  * but DO NOT overwrite a pending exception
    1058             :                                  */
    1059         150 :                                 if (EG(error_handling) == EH_THROW && !EG(exception)) {
    1060         144 :                                         zend_throw_error_exception(EG(exception_class), buffer, 0, type TSRMLS_CC);
    1061             :                                 }
    1062         150 :                                 efree(buffer);
    1063         150 :                                 return;
    1064             :                 }
    1065             :         }
    1066             : 
    1067             :         /* display/log the error if necessary */
    1068      424733 :         if (display && (EG(error_reporting) & type || (type & E_CORE))
    1069       46430 :                 && (PG(log_errors) || PG(display_errors) || (!module_initialized))) {
    1070             :                 char *error_type_str;
    1071             : 
    1072       22252 :                 switch (type) {
    1073             :                         case E_ERROR:
    1074             :                         case E_CORE_ERROR:
    1075             :                         case E_COMPILE_ERROR:
    1076             :                         case E_USER_ERROR:
    1077         680 :                                 error_type_str = "Fatal error";
    1078         680 :                                 break;
    1079             :                         case E_RECOVERABLE_ERROR:
    1080          92 :                                 error_type_str = "Catchable fatal error";
    1081          92 :                                 break;
    1082             :                         case E_WARNING:
    1083             :                         case E_CORE_WARNING:
    1084             :                         case E_COMPILE_WARNING:
    1085             :                         case E_USER_WARNING:
    1086       18666 :                                 error_type_str = "Warning";
    1087       18666 :                                 break;
    1088             :                         case E_PARSE:
    1089          33 :                                 error_type_str = "Parse error";
    1090          33 :                                 break;
    1091             :                         case E_NOTICE:
    1092             :                         case E_USER_NOTICE:
    1093        2098 :                                 error_type_str = "Notice";
    1094        2098 :                                 break;
    1095             :                         case E_STRICT:
    1096         138 :                                 error_type_str = "Strict Standards";
    1097         138 :                                 break;
    1098             :                         case E_DEPRECATED:
    1099             :                         case E_USER_DEPRECATED:
    1100         545 :                                 error_type_str = "Deprecated";
    1101         545 :                                 break;
    1102             :                         default:
    1103           0 :                                 error_type_str = "Unknown error";
    1104             :                                 break;
    1105             :                 }
    1106             : 
    1107       22252 :                 if (!module_initialized || PG(log_errors)) {
    1108             :                         char *log_buffer;
    1109             : #ifdef PHP_WIN32
    1110             :                         if ((type == E_CORE_ERROR || type == E_CORE_WARNING) && PG(display_startup_errors)) {
    1111             :                                 MessageBox(NULL, buffer, error_type_str, MB_OK|ZEND_SERVICE_MB_STYLE);
    1112             :                         }
    1113             : #endif
    1114          12 :                         spprintf(&log_buffer, 0, "PHP %s:  %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno);
    1115          12 :                         php_log_err(log_buffer TSRMLS_CC);
    1116          12 :                         efree(log_buffer);
    1117             :                 }
    1118             : 
    1119       22252 :                 if (PG(display_errors) && ((module_initialized && !PG(during_request_startup)) || (PG(display_startup_errors)))) {
    1120       22243 :                         if (PG(xmlrpc_errors)) {
    1121           0 :                                 php_printf("<?xml version=\"1.0\"?><methodResponse><fault><value><struct><member><name>faultCode</name><value><int>%ld</int></value></member><member><name>faultString</name><value><string>%s:%s in %s on line %d</string></value></member></struct></value></fault></methodResponse>", PG(xmlrpc_error_number), error_type_str, buffer, error_filename, error_lineno);
    1122             :                         } else {
    1123       22243 :                                 char *prepend_string = INI_STR("error_prepend_string");
    1124       22243 :                                 char *append_string = INI_STR("error_append_string");
    1125             : 
    1126       22243 :                                 if (PG(html_errors)) {
    1127           6 :                                         if (type == E_ERROR || type == E_PARSE) {
    1128             :                                                 size_t len;
    1129           2 :                                                 char *buf = php_escape_html_entities(buffer, buffer_len, &len, 0, ENT_COMPAT, NULL TSRMLS_CC);
    1130           2 :                                                 php_printf("%s<br />\n<b>%s</b>:  %s in <b>%s</b> on line <b>%d</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, buf, error_filename, error_lineno, STR_PRINT(append_string));
    1131           2 :                                                 efree(buf);
    1132             :                                         } else {
    1133           2 :                                                 php_printf("%s<br />\n<b>%s</b>:  %s in <b>%s</b> on line <b>%d</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string));
    1134             :                                         }
    1135             :                                 } else {
    1136             :                                         /* Write CLI/CGI errors to stderr if display_errors = "stderr" */
    1137       44414 :                                         if ((!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi")) &&
    1138       22175 :                                                 PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR
    1139             :                                         ) {
    1140             : #ifdef PHP_WIN32
    1141             :                                                 fprintf(stderr, "%s: %s in %s on line %d\n", error_type_str, buffer, error_filename, error_lineno);
    1142             :                                                 fflush(stderr);
    1143             : #else
    1144           0 :                                                 fprintf(stderr, "%s: %s in %s on line %d\n", error_type_str, buffer, error_filename, error_lineno);
    1145             : #endif
    1146             :                                         } else {
    1147       22239 :                                                 php_printf("%s\n%s: %s in %s on line %d\n%s", STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string));
    1148             :                                         }
    1149             :                                 }
    1150             :                         }
    1151             :                 }
    1152             : #if ZEND_DEBUG
    1153             :                 if (PG(report_zend_debug)) {
    1154             :                         zend_bool trigger_break;
    1155             : 
    1156             :                         switch (type) {
    1157             :                                 case E_ERROR:
    1158             :                                 case E_CORE_ERROR:
    1159             :                                 case E_COMPILE_ERROR:
    1160             :                                 case E_USER_ERROR:
    1161             :                                         trigger_break=1;
    1162             :                                         break;
    1163             :                                 default:
    1164             :                                         trigger_break=0;
    1165             :                                         break;
    1166             :                         }
    1167             :                         zend_output_debug_string(trigger_break, "%s(%d) : %s - %s", error_filename, error_lineno, error_type_str, buffer);
    1168             :                 }
    1169             : #endif
    1170             :         }
    1171             : 
    1172             :         /* Bail out if we can't recover */
    1173      378302 :         switch (type) {
    1174             :                 case E_CORE_ERROR:
    1175           5 :                         if(!module_initialized) {
    1176             :                                 /* bad error in module startup - no way we can live with this */
    1177           0 :                                 exit(-2);
    1178             :                         }
    1179             :                 /* no break - intentionally */
    1180             :                 case E_ERROR:
    1181             :                 case E_RECOVERABLE_ERROR:
    1182             :                 case E_PARSE:
    1183             :                 case E_COMPILE_ERROR:
    1184             :                 case E_USER_ERROR:
    1185             :                 { /* new block to allow variable definition */
    1186             :                         /* eval() errors do not affect exit_status or response code */
    1187         868 :                         zend_bool during_eval = (type == E_PARSE) && (EG(current_execute_data) &&
    1188          15 :                                                 EG(current_execute_data)->opline &&
    1189          15 :                                                 EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
    1190         838 :                                                 EG(current_execute_data)->opline->extended_value == ZEND_EVAL);
    1191         832 :                         if (!during_eval) {
    1192         830 :                                 EG(exit_status) = 255;
    1193             :                         }
    1194         832 :                         if (module_initialized) {
    1195         881 :                                 if (!PG(display_errors) &&
    1196          25 :                                     !SG(headers_sent) &&
    1197          24 :                                         SG(sapi_headers).http_response_code == 200 &&
    1198             :                                     !during_eval
    1199             :                                 ) {
    1200           0 :                                         sapi_header_line ctr = {0};
    1201             : 
    1202           0 :                                         ctr.line = "HTTP/1.0 500 Internal Server Error";
    1203           0 :                                         ctr.line_len = sizeof("HTTP/1.0 500 Internal Server Error") - 1;
    1204           0 :                                         sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
    1205             :                                 }
    1206             :                                 /* the parser would return 1 (failure), we can bail out nicely */
    1207         832 :                                 if (type == E_PARSE) {
    1208          38 :                                         CG(parse_error) = 0;
    1209             :                                 } else {
    1210             :                                         /* restore memory limit */
    1211         794 :                                         zend_set_memory_limit(PG(memory_limit) TSRMLS_CC);
    1212         794 :                                         efree(buffer);
    1213         794 :                                         zend_objects_store_mark_destructed(&EG(objects_store) TSRMLS_CC);
    1214         794 :                                         zend_bailout();
    1215           0 :                                         return;
    1216             :                                 }
    1217             :                         }
    1218             :                         break;
    1219             :                 }
    1220             :         }
    1221             : 
    1222             :         /* Log if necessary */
    1223      377508 :         if (!display) {
    1224           0 :                 efree(buffer);
    1225           0 :                 return;
    1226             :         }
    1227             : 
    1228      377508 :         if (PG(track_errors) && module_initialized) {
    1229       28068 :                 if (!EG(active_symbol_table)) {
    1230         272 :                         zend_rebuild_symbol_table(TSRMLS_C);
    1231             :                 }
    1232       28068 :                 if (EG(active_symbol_table)) {
    1233             :                         zval *tmp;
    1234       28067 :                         ALLOC_INIT_ZVAL(tmp);
    1235       28067 :                         ZVAL_STRINGL(tmp, buffer, buffer_len, 1);
    1236       28067 :                         zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) & tmp, sizeof(zval *), NULL);
    1237             :                 }
    1238             :         }
    1239             : 
    1240      377508 :         efree(buffer);
    1241             : }
    1242             : /* }}} */
    1243             : 
    1244             : /* {{{ php_get_current_user
    1245             :  */
    1246          11 : PHPAPI char *php_get_current_user(TSRMLS_D)
    1247             : {
    1248             :         struct stat *pstat;
    1249             : 
    1250          11 :         if (SG(request_info).current_user) {
    1251           2 :                 return SG(request_info).current_user;
    1252             :         }
    1253             : 
    1254             :         /* FIXME: I need to have this somehow handled if
    1255             :         USE_SAPI is defined, because cgi will also be
    1256             :         interfaced in USE_SAPI */
    1257             : 
    1258           9 :         pstat = sapi_get_stat(TSRMLS_C);
    1259             : 
    1260           9 :         if (!pstat) {
    1261           0 :                 return "";
    1262             :         } else {
    1263             : #ifdef PHP_WIN32
    1264             :                 char name[256];
    1265             :                 DWORD len = sizeof(name)-1;
    1266             : 
    1267             :                 if (!GetUserName(name, &len)) {
    1268             :                         return "";
    1269             :                 }
    1270             :                 name[len] = '\0';
    1271             :                 SG(request_info).current_user_length = len;
    1272             :                 SG(request_info).current_user = estrndup(name, len);
    1273             :                 return SG(request_info).current_user;
    1274             : #else
    1275             :                 struct passwd *pwd;
    1276             : #if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
    1277             :                 struct passwd _pw;
    1278             :                 struct passwd *retpwptr = NULL;
    1279             :                 int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
    1280             :                 char *pwbuf;
    1281             : 
    1282             :                 if (pwbuflen < 1) {
    1283             :                         return "";
    1284             :                 }
    1285             :                 pwbuf = emalloc(pwbuflen);
    1286             :                 if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
    1287             :                         efree(pwbuf);
    1288             :                         return "";
    1289             :                 }
    1290             :                 pwd = &_pw;
    1291             : #else
    1292           9 :                 if ((pwd=getpwuid(pstat->st_uid))==NULL) {
    1293           0 :                         return "";
    1294             :                 }
    1295             : #endif
    1296           9 :                 SG(request_info).current_user_length = strlen(pwd->pw_name);
    1297           9 :                 SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
    1298             : #if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
    1299             :                 efree(pwbuf);
    1300             : #endif
    1301           9 :                 return SG(request_info).current_user;
    1302             : #endif
    1303             :         }
    1304             : }
    1305             : /* }}} */
    1306             : 
    1307             : /* {{{ proto bool set_time_limit(int seconds)
    1308             :    Sets the maximum time a script can run */
    1309          18 : PHP_FUNCTION(set_time_limit)
    1310             : {
    1311             :         long new_timeout;
    1312             :         char *new_timeout_str;
    1313             :         int new_timeout_strlen;
    1314             : 
    1315          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &new_timeout) == FAILURE) {
    1316           0 :                 return;
    1317             :         }
    1318             : 
    1319          18 :         new_timeout_strlen = zend_spprintf(&new_timeout_str, 0, "%ld", new_timeout);
    1320             : 
    1321          18 :         if (zend_alter_ini_entry_ex("max_execution_time", sizeof("max_execution_time"), new_timeout_str, new_timeout_strlen, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC) == SUCCESS) {
    1322          18 :                 RETVAL_TRUE;
    1323             :         } else {
    1324           0 :                 RETVAL_FALSE;
    1325             :         }
    1326          18 :         efree(new_timeout_str);
    1327             : }
    1328             : /* }}} */
    1329             : 
    1330             : /* {{{ php_fopen_wrapper_for_zend
    1331             :  */
    1332           0 : static FILE *php_fopen_wrapper_for_zend(const char *filename, char **opened_path TSRMLS_DC)
    1333             : {
    1334           0 :         return php_stream_open_wrapper_as_file((char *)filename, "rb", USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, opened_path);
    1335             : }
    1336             : /* }}} */
    1337             : 
    1338       10335 : static void php_zend_stream_closer(void *handle TSRMLS_DC) /* {{{ */
    1339             : {
    1340       10335 :         php_stream_close((php_stream*)handle);
    1341       10335 : }
    1342             : /* }}} */
    1343             : 
    1344       10053 : static void php_zend_stream_mmap_closer(void *handle TSRMLS_DC) /* {{{ */
    1345             : {
    1346       10053 :         php_stream_mmap_unmap((php_stream*)handle);
    1347       10053 :         php_zend_stream_closer(handle TSRMLS_CC);
    1348       10053 : }
    1349             : /* }}} */
    1350             : 
    1351       10650 : static size_t php_zend_stream_fsizer(void *handle TSRMLS_DC) /* {{{ */
    1352             : {
    1353             :         php_stream_statbuf  ssb;
    1354       10650 :         if (php_stream_stat((php_stream*)handle, &ssb) == 0) {
    1355       10648 :                 return ssb.sb.st_size;
    1356             :         }
    1357           2 :         return 0;
    1358             : }
    1359             : /* }}} */
    1360             : 
    1361       10414 : static int php_stream_open_for_zend(const char *filename, zend_file_handle *handle TSRMLS_DC) /* {{{ */
    1362             : {
    1363       10414 :         return php_stream_open_for_zend_ex(filename, handle, USE_PATH|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
    1364             : }
    1365             : /* }}} */
    1366             : 
    1367       10429 : PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *handle, int mode TSRMLS_DC) /* {{{ */
    1368             : {
    1369             :         char *p;
    1370             :         size_t len, mapped_len;
    1371       10429 :         php_stream *stream = php_stream_open_wrapper((char *)filename, "rb", mode, &handle->opened_path);
    1372             : 
    1373       10429 :         if (stream) {
    1374             : #if HAVE_MMAP || defined(PHP_WIN32)
    1375       10369 :                 size_t page_size = REAL_PAGE_SIZE;
    1376             : #endif
    1377             : 
    1378       10369 :                 handle->filename = (char*)filename;
    1379       10369 :                 handle->free_filename = 0;
    1380       10369 :                 handle->handle.stream.handle  = stream;
    1381       10369 :                 handle->handle.stream.reader  = (zend_stream_reader_t)_php_stream_read;
    1382       10369 :                 handle->handle.stream.fsizer  = php_zend_stream_fsizer;
    1383       10369 :                 handle->handle.stream.isatty  = 0;
    1384             :                 /* can we mmap immediately? */
    1385       10369 :                 memset(&handle->handle.stream.mmap, 0, sizeof(handle->handle.stream.mmap));
    1386       10369 :                 len = php_zend_stream_fsizer(stream TSRMLS_CC);
    1387       51261 :                 if (len != 0
    1388             : #if HAVE_MMAP || defined(PHP_WIN32)
    1389       10361 :                 && ((len - 1) % page_size) <= page_size - ZEND_MMAP_AHEAD
    1390             : #endif
    1391       20720 :                 && php_stream_mmap_possible(stream)
    1392       10086 :                 && (p = php_stream_mmap_range(stream, 0, len, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped_len)) != NULL) {
    1393       10086 :                         handle->handle.stream.closer   = php_zend_stream_mmap_closer;
    1394       10086 :                         handle->handle.stream.mmap.buf = p;
    1395       10086 :                         handle->handle.stream.mmap.len = mapped_len;
    1396       10086 :                         handle->type = ZEND_HANDLE_MAPPED;
    1397             :                 } else {
    1398         283 :                         handle->handle.stream.closer = php_zend_stream_closer;
    1399         283 :                         handle->type = ZEND_HANDLE_STREAM;
    1400             :                 }
    1401             :                 /* suppress warning if this stream is not explicitly closed */
    1402             :                 php_stream_auto_cleanup(stream);
    1403             : 
    1404       10369 :                 return SUCCESS;
    1405             :         }
    1406          60 :         return FAILURE;
    1407             : }
    1408             : /* }}} */
    1409             : 
    1410       17293 : static char *php_resolve_path_for_zend(const char *filename, int filename_len TSRMLS_DC) /* {{{ */
    1411             : {
    1412       17293 :         return php_resolve_path(filename, filename_len, PG(include_path) TSRMLS_CC);
    1413             : }
    1414             : /* }}} */
    1415             : 
    1416             : /* {{{ php_get_configuration_directive_for_zend
    1417             :  */
    1418     6099832 : static int php_get_configuration_directive_for_zend(const char *name, uint name_length, zval *contents)
    1419             : {
    1420     6099832 :         zval *retval = cfg_get_entry(name, name_length);
    1421             : 
    1422     6099832 :         if (retval) {
    1423      679147 :                 *contents = *retval;
    1424      679147 :                 return SUCCESS;
    1425             :         } else {
    1426     5420685 :                 return FAILURE;
    1427             :         }
    1428             : }
    1429             : /* }}} */
    1430             : 
    1431             : /* {{{ php_message_handler_for_zend
    1432             :  */
    1433          33 : static void php_message_handler_for_zend(long message, const void *data TSRMLS_DC)
    1434             : {
    1435          33 :         switch (message) {
    1436             :                 case ZMSG_FAILED_INCLUDE_FOPEN:
    1437          28 :                         php_error_docref("function.include" TSRMLS_CC, E_WARNING, "Failed opening '%s' for inclusion (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
    1438          28 :                         break;
    1439             :                 case ZMSG_FAILED_REQUIRE_FOPEN:
    1440           3 :                         php_error_docref("function.require" TSRMLS_CC, E_COMPILE_ERROR, "Failed opening required '%s' (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
    1441           0 :                         break;
    1442             :                 case ZMSG_FAILED_HIGHLIGHT_FOPEN:
    1443           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed opening '%s' for highlighting", php_strip_url_passwd((char *) data));
    1444           2 :                         break;
    1445             :                 case ZMSG_MEMORY_LEAK_DETECTED:
    1446             :                 case ZMSG_MEMORY_LEAK_REPEATED:
    1447             : #if ZEND_DEBUG
    1448             :                         if (EG(error_reporting) & E_WARNING) {
    1449             :                                 char memory_leak_buf[1024];
    1450             : 
    1451             :                                 if (message==ZMSG_MEMORY_LEAK_DETECTED) {
    1452             :                                         zend_leak_info *t = (zend_leak_info *) data;
    1453             : 
    1454             :                                         snprintf(memory_leak_buf, 512, "%s(%d) :  Freeing 0x%.8lX (%zu bytes), script=%s\n", t->filename, t->lineno, (zend_uintptr_t)t->addr, t->size, SAFE_FILENAME(SG(request_info).path_translated));
    1455             :                                         if (t->orig_filename) {
    1456             :                                                 char relay_buf[512];
    1457             : 
    1458             :                                                 snprintf(relay_buf, 512, "%s(%d) : Actual location (location was relayed)\n", t->orig_filename, t->orig_lineno);
    1459             :                                                 strlcat(memory_leak_buf, relay_buf, sizeof(memory_leak_buf));
    1460             :                                         }
    1461             :                                 } else {
    1462             :                                         unsigned long leak_count = (zend_uintptr_t) data;
    1463             : 
    1464             :                                         snprintf(memory_leak_buf, 512, "Last leak repeated %ld time%s\n", leak_count, (leak_count>1?"s":""));
    1465             :                                 }
    1466             : #       if defined(PHP_WIN32)
    1467             :                                 OutputDebugString(memory_leak_buf);
    1468             : #       else
    1469             :                                 fprintf(stderr, "%s", memory_leak_buf);
    1470             : #       endif
    1471             :                         }
    1472             : #endif
    1473           0 :                         break;
    1474             :                 case ZMSG_MEMORY_LEAKS_GRAND_TOTAL:
    1475             : #if ZEND_DEBUG
    1476             :                         if (EG(error_reporting) & E_WARNING) {
    1477             :                                 char memory_leak_buf[512];
    1478             : 
    1479             :                                 snprintf(memory_leak_buf, 512, "=== Total %d memory leaks detected ===\n", *((zend_uint *) data));
    1480             : #       if defined(PHP_WIN32)
    1481             :                                 OutputDebugString(memory_leak_buf);
    1482             : #       else
    1483             :                                 fprintf(stderr, "%s", memory_leak_buf);
    1484             : #       endif
    1485             :                         }
    1486             : #endif
    1487           0 :                         break;
    1488             :                 case ZMSG_LOG_SCRIPT_NAME: {
    1489             :                                 struct tm *ta, tmbuf;
    1490             :                                 time_t curtime;
    1491             :                                 char *datetime_str, asctimebuf[52];
    1492             :                                 char memory_leak_buf[4096];
    1493             : 
    1494           0 :                                 time(&curtime);
    1495           0 :                                 ta = php_localtime_r(&curtime, &tmbuf);
    1496           0 :                                 datetime_str = php_asctime_r(ta, asctimebuf);
    1497           0 :                                 if (datetime_str) {
    1498           0 :                                         datetime_str[strlen(datetime_str)-1]=0; /* get rid of the trailing newline */
    1499           0 :                                         snprintf(memory_leak_buf, sizeof(memory_leak_buf), "[%s]  Script:  '%s'\n", datetime_str, SAFE_FILENAME(SG(request_info).path_translated));
    1500             :                                 } else {
    1501           0 :                                         snprintf(memory_leak_buf, sizeof(memory_leak_buf), "[null]  Script:  '%s'\n", SAFE_FILENAME(SG(request_info).path_translated));
    1502             :                                 }
    1503             : #       if defined(PHP_WIN32)
    1504             :                                 OutputDebugString(memory_leak_buf);
    1505             : #       else
    1506           0 :                                 fprintf(stderr, "%s", memory_leak_buf);
    1507             : #       endif
    1508             :                         }
    1509             :                         break;
    1510             :         }
    1511          30 : }
    1512             : /* }}} */
    1513             : 
    1514             : 
    1515           3 : void php_on_timeout(int seconds TSRMLS_DC)
    1516             : {
    1517           3 :         PG(connection_status) |= PHP_CONNECTION_TIMEOUT;
    1518           3 :         zend_set_timeout(EG(timeout_seconds), 1);
    1519           3 :         if(PG(exit_on_timeout)) sapi_terminate_process(TSRMLS_C);
    1520           3 : }
    1521             : 
    1522             : #if PHP_SIGCHILD
    1523             : /* {{{ sigchld_handler
    1524             :  */
    1525             : static void sigchld_handler(int apar)
    1526             : {
    1527             :         int errno_save = errno;
    1528             : 
    1529             :         while (waitpid(-1, NULL, WNOHANG) > 0);
    1530             :         signal(SIGCHLD, sigchld_handler);
    1531             : 
    1532             :         errno = errno_save;
    1533             : }
    1534             : /* }}} */
    1535             : #endif
    1536             : 
    1537             : /* {{{ php_start_sapi()
    1538             :  */
    1539           0 : static int php_start_sapi(TSRMLS_D)
    1540             : {
    1541           0 :         int retval = SUCCESS;
    1542             : 
    1543           0 :         if(!SG(sapi_started)) {
    1544           0 :                 zend_try {
    1545           0 :                         PG(during_request_startup) = 1;
    1546             : 
    1547             :                         /* initialize global variables */
    1548           0 :                         PG(modules_activated) = 0;
    1549           0 :                         PG(header_is_being_sent) = 0;
    1550           0 :                         PG(connection_status) = PHP_CONNECTION_NORMAL;
    1551             : 
    1552           0 :                         zend_activate(TSRMLS_C);
    1553           0 :                         zend_set_timeout(EG(timeout_seconds), 1);
    1554           0 :                         zend_activate_modules(TSRMLS_C);
    1555           0 :                         PG(modules_activated)=1;
    1556           0 :                 } zend_catch {
    1557           0 :                         retval = FAILURE;
    1558           0 :                 } zend_end_try();
    1559             : 
    1560           0 :                 SG(sapi_started) = 1;
    1561             :         }
    1562           0 :         return retval;
    1563             : }
    1564             : 
    1565             : /* }}} */
    1566             : 
    1567             : /* {{{ php_request_startup
    1568             :  */
    1569             : #ifndef APACHE_HOOKS
    1570       21214 : int php_request_startup(TSRMLS_D)
    1571             : {
    1572       21214 :         int retval = SUCCESS;
    1573             : 
    1574             : #ifdef HAVE_DTRACE
    1575             :         DTRACE_REQUEST_STARTUP(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
    1576             : #endif /* HAVE_DTRACE */
    1577             : 
    1578             : #ifdef PHP_WIN32
    1579             :         PG(com_initialized) = 0;
    1580             : #endif
    1581             : 
    1582             : #if PHP_SIGCHILD
    1583             :         signal(SIGCHLD, sigchld_handler);
    1584             : #endif
    1585             : 
    1586       21214 :         zend_try {
    1587       21214 :                 PG(in_error_log) = 0;
    1588       21214 :                 PG(during_request_startup) = 1;
    1589             : 
    1590       21214 :                 php_output_activate(TSRMLS_C);
    1591             : 
    1592             :                 /* initialize global variables */
    1593       21214 :                 PG(modules_activated) = 0;
    1594       21214 :                 PG(header_is_being_sent) = 0;
    1595       21214 :                 PG(connection_status) = PHP_CONNECTION_NORMAL;
    1596       21214 :                 PG(in_user_include) = 0;
    1597             : 
    1598       21214 :                 zend_activate(TSRMLS_C);
    1599       21214 :                 sapi_activate(TSRMLS_C);
    1600             : 
    1601             : #ifdef ZEND_SIGNALS
    1602             :                 zend_signal_activate(TSRMLS_C);
    1603             : #endif
    1604             : 
    1605       21214 :                 if (PG(max_input_time) == -1) {
    1606       21214 :                         zend_set_timeout(EG(timeout_seconds), 1);
    1607             :                 } else {
    1608           0 :                         zend_set_timeout(PG(max_input_time), 1);
    1609             :                 }
    1610             : 
    1611             :                 /* Disable realpath cache if an open_basedir is set */
    1612       21214 :                 if (PG(open_basedir) && *PG(open_basedir)) {
    1613          72 :                         CWDG(realpath_cache_size_limit) = 0;
    1614             :                 }
    1615             : 
    1616       21214 :                 if (PG(expose_php)) {
    1617       21214 :                         sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1);
    1618             :                 }
    1619             : 
    1620       21221 :                 if (PG(output_handler) && PG(output_handler)[0]) {
    1621             :                         zval *oh;
    1622             : 
    1623           7 :                         MAKE_STD_ZVAL(oh);
    1624           7 :                         ZVAL_STRING(oh, PG(output_handler), 1);
    1625           7 :                         php_output_start_user(oh, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
    1626           7 :                         zval_ptr_dtor(&oh);
    1627       21207 :                 } else if (PG(output_buffering)) {
    1628           1 :                         php_output_start_user(NULL, PG(output_buffering) > 1 ? PG(output_buffering) : 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
    1629       21206 :                 } else if (PG(implicit_flush)) {
    1630       20804 :                         php_output_set_implicit_flush(1 TSRMLS_CC);
    1631             :                 }
    1632             : 
    1633             :                 /* We turn this off in php_execute_script() */
    1634             :                 /* PG(during_request_startup) = 0; */
    1635             : 
    1636       21214 :                 php_hash_environment(TSRMLS_C);
    1637       21214 :                 zend_activate_modules(TSRMLS_C);
    1638       21214 :                 PG(modules_activated)=1;
    1639           0 :         } zend_catch {
    1640           0 :                 retval = FAILURE;
    1641       21214 :         } zend_end_try();
    1642             : 
    1643       21214 :         SG(sapi_started) = 1;
    1644             : 
    1645       21214 :         return retval;
    1646             : }
    1647             : # else
    1648             : int php_request_startup(TSRMLS_D)
    1649             : {
    1650             :         int retval = SUCCESS;
    1651             : 
    1652             : #if PHP_SIGCHILD
    1653             :         signal(SIGCHLD, sigchld_handler);
    1654             : #endif
    1655             : 
    1656             :         if (php_start_sapi() == FAILURE) {
    1657             :                 return FAILURE;
    1658             :         }
    1659             : 
    1660             :         php_output_activate(TSRMLS_C);
    1661             :         sapi_activate(TSRMLS_C);
    1662             :         php_hash_environment(TSRMLS_C);
    1663             : 
    1664             :         zend_try {
    1665             :                 PG(during_request_startup) = 1;
    1666             :                 if (PG(expose_php)) {
    1667             :                         sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1);
    1668             :                 }
    1669             :         } zend_catch {
    1670             :                 retval = FAILURE;
    1671             :         } zend_end_try();
    1672             : 
    1673             :         return retval;
    1674             : }
    1675             : # endif
    1676             : /* }}} */
    1677             : 
    1678             : /* {{{ php_request_startup_for_hook
    1679             :  */
    1680           0 : int php_request_startup_for_hook(TSRMLS_D)
    1681             : {
    1682           0 :         int retval = SUCCESS;
    1683             : 
    1684             : #if PHP_SIGCHLD
    1685             :         signal(SIGCHLD, sigchld_handler);
    1686             : #endif
    1687             : 
    1688           0 :         if (php_start_sapi(TSRMLS_C) == FAILURE) {
    1689           0 :                 return FAILURE;
    1690             :         }
    1691             : 
    1692           0 :         php_output_activate(TSRMLS_C);
    1693           0 :         sapi_activate_headers_only(TSRMLS_C);
    1694           0 :         php_hash_environment(TSRMLS_C);
    1695             : 
    1696           0 :         return retval;
    1697             : }
    1698             : /* }}} */
    1699             : 
    1700             : /* {{{ php_request_shutdown_for_exec
    1701             :  */
    1702           0 : void php_request_shutdown_for_exec(void *dummy)
    1703             : {
    1704             :         TSRMLS_FETCH();
    1705             : 
    1706             :         /* used to close fd's in the 3..255 range here, but it's problematic
    1707             :          */
    1708           0 :         shutdown_memory_manager(1, 1 TSRMLS_CC);
    1709           0 :         zend_interned_strings_restore(TSRMLS_C);
    1710           0 : }
    1711             : /* }}} */
    1712             : 
    1713             : /* {{{ php_request_shutdown_for_hook
    1714             :  */
    1715           0 : void php_request_shutdown_for_hook(void *dummy)
    1716             : {
    1717             :         TSRMLS_FETCH();
    1718             : 
    1719           0 :         if (PG(modules_activated)) zend_try {
    1720           0 :                 php_call_shutdown_functions(TSRMLS_C);
    1721           0 :         } zend_end_try();
    1722             : 
    1723           0 :         if (PG(modules_activated)) {
    1724           0 :                 zend_deactivate_modules(TSRMLS_C);
    1725           0 :                 php_free_shutdown_functions(TSRMLS_C);
    1726             :         }
    1727             : 
    1728           0 :         zend_try {
    1729           0 :                 zend_unset_timeout(TSRMLS_C);
    1730           0 :         } zend_end_try();
    1731             : 
    1732           0 :         zend_try {
    1733             :                 int i;
    1734             : 
    1735           0 :                 for (i = 0; i < NUM_TRACK_VARS; i++) {
    1736           0 :                         if (PG(http_globals)[i]) {
    1737           0 :                                 zval_ptr_dtor(&PG(http_globals)[i]);
    1738             :                         }
    1739             :                 }
    1740           0 :         } zend_end_try();
    1741             : 
    1742           0 :         zend_deactivate(TSRMLS_C);
    1743             : 
    1744           0 :         zend_try {
    1745           0 :                 sapi_deactivate(TSRMLS_C);
    1746           0 :         } zend_end_try();
    1747             : 
    1748           0 :         zend_try {
    1749           0 :                 php_shutdown_stream_hashes(TSRMLS_C);
    1750           0 :         } zend_end_try();
    1751             : 
    1752           0 :         zend_try {
    1753           0 :                 shutdown_memory_manager(CG(unclean_shutdown), 0 TSRMLS_CC);
    1754           0 :         } zend_end_try();
    1755             : 
    1756           0 :         zend_interned_strings_restore(TSRMLS_C);
    1757             : 
    1758             : #ifdef ZEND_SIGNALS
    1759             :         zend_try {
    1760             :                 zend_signal_deactivate(TSRMLS_C);
    1761             :         } zend_end_try();
    1762             : #endif
    1763           0 : }
    1764             : 
    1765             : /* }}} */
    1766             : 
    1767             : /* {{{ php_request_shutdown
    1768             :  */
    1769       21249 : void php_request_shutdown(void *dummy)
    1770             : {
    1771             :         zend_bool report_memleaks;
    1772             :         TSRMLS_FETCH();
    1773             : 
    1774       21249 :         report_memleaks = PG(report_memleaks);
    1775             : 
    1776             :         /* EG(opline_ptr) points into nirvana and therefore cannot be safely accessed
    1777             :          * inside zend_executor callback functions.
    1778             :          */
    1779       21249 :         EG(opline_ptr) = NULL;
    1780       21249 :         EG(active_op_array) = NULL;
    1781             : 
    1782       21249 :         php_deactivate_ticks(TSRMLS_C);
    1783             : 
    1784             :         /* 1. Call all possible shutdown functions registered with register_shutdown_function() */
    1785       21249 :         if (PG(modules_activated)) zend_try {
    1786       21249 :                 php_call_shutdown_functions(TSRMLS_C);
    1787       21249 :         } zend_end_try();
    1788             : 
    1789             :         /* 2. Call all possible __destruct() functions */
    1790       21249 :         zend_try {
    1791       21249 :                 zend_call_destructors(TSRMLS_C);
    1792       21249 :         } zend_end_try();
    1793             : 
    1794             :         /* 3. Flush all output buffers */
    1795       21249 :         zend_try {
    1796       21249 :                 zend_bool send_buffer = SG(request_info).headers_only ? 0 : 1;
    1797             : 
    1798       21694 :                 if (CG(unclean_shutdown) && PG(last_error_type) == E_ERROR &&
    1799         445 :                         (size_t)PG(memory_limit) < zend_memory_usage(1 TSRMLS_CC)
    1800             :                 ) {
    1801           0 :                         send_buffer = 0;
    1802             :                 }
    1803             : 
    1804       21249 :                 if (!send_buffer) {
    1805           0 :                         php_output_discard_all(TSRMLS_C);
    1806             :                 } else {
    1807       21249 :                         php_output_end_all(TSRMLS_C);
    1808             :                 }
    1809       21249 :         } zend_end_try();
    1810             : 
    1811             :         /* 4. Reset max_execution_time (no longer executing php code after response sent) */
    1812       21249 :         zend_try {
    1813       21249 :                 zend_unset_timeout(TSRMLS_C);
    1814       21249 :         } zend_end_try();
    1815             : 
    1816             :         /* 5. Call all extensions RSHUTDOWN functions */
    1817       21249 :         if (PG(modules_activated)) {
    1818       21249 :                 zend_deactivate_modules(TSRMLS_C);
    1819       21249 :                 php_free_shutdown_functions(TSRMLS_C);
    1820             :         }
    1821             : 
    1822             :         /* 6. Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */
    1823       21249 :         zend_try {
    1824       21249 :                 php_output_deactivate(TSRMLS_C);
    1825       21249 :         } zend_end_try();
    1826             : 
    1827             :         /* 7. Destroy super-globals */
    1828       21249 :         zend_try {
    1829             :                 int i;
    1830             : 
    1831      148743 :                 for (i=0; i<NUM_TRACK_VARS; i++) {
    1832      127494 :                         if (PG(http_globals)[i]) {
    1833      107408 :                                 zval_ptr_dtor(&PG(http_globals)[i]);
    1834             :                         }
    1835             :                 }
    1836       21249 :         } zend_end_try();
    1837             : 
    1838             :         /* 8. free last error information */
    1839       21249 :         if (PG(last_error_message)) {
    1840        5557 :                 free(PG(last_error_message));
    1841        5557 :                 PG(last_error_message) = NULL;
    1842             :         }
    1843       21249 :         if (PG(last_error_file)) {
    1844        5557 :                 free(PG(last_error_file));
    1845        5557 :                 PG(last_error_file) = NULL;
    1846             :         }
    1847             : 
    1848             :         /* 9. Shutdown scanner/executor/compiler and restore ini entries */
    1849       21249 :         zend_deactivate(TSRMLS_C);
    1850             : 
    1851             :         /* 10. Call all extensions post-RSHUTDOWN functions */
    1852       21249 :         zend_try {
    1853       21249 :                 zend_post_deactivate_modules(TSRMLS_C);
    1854       21249 :         } zend_end_try();
    1855             : 
    1856             :         /* 11. SAPI related shutdown (free stuff) */
    1857       21249 :         zend_try {
    1858       21249 :                 sapi_deactivate(TSRMLS_C);
    1859       21249 :         } zend_end_try();
    1860             : 
    1861             :         /* 12. free virtual CWD memory */
    1862       21249 :         virtual_cwd_deactivate(TSRMLS_C);
    1863             : 
    1864             :         /* 13. Destroy stream hashes */
    1865       21249 :         zend_try {
    1866       21249 :                 php_shutdown_stream_hashes(TSRMLS_C);
    1867       21249 :         } zend_end_try();
    1868             : 
    1869             :         /* 14. Free Willy (here be crashes) */
    1870       21249 :         zend_try {
    1871       21249 :                 shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0 TSRMLS_CC);
    1872       21249 :         } zend_end_try();
    1873       21249 :         zend_interned_strings_restore(TSRMLS_C);
    1874             : 
    1875             :         /* 15. Reset max_execution_time */
    1876       21249 :         zend_try {
    1877       21249 :                 zend_unset_timeout(TSRMLS_C);
    1878       21249 :         } zend_end_try();
    1879             : 
    1880             : #ifdef PHP_WIN32
    1881             :         if (PG(com_initialized)) {
    1882             :                 CoUninitialize();
    1883             :                 PG(com_initialized) = 0;
    1884             :         }
    1885             : #endif
    1886             : 
    1887             : #ifdef HAVE_DTRACE
    1888             :         DTRACE_REQUEST_SHUTDOWN(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
    1889             : #endif /* HAVE_DTRACE */
    1890       21249 : }
    1891             : /* }}} */
    1892             : 
    1893             : /* {{{ php_com_initialize
    1894             :  */
    1895           0 : PHPAPI void php_com_initialize(TSRMLS_D)
    1896             : {
    1897             : #ifdef PHP_WIN32
    1898             :         if (!PG(com_initialized)) {
    1899             :                 if (CoInitialize(NULL) == S_OK) {
    1900             :                         PG(com_initialized) = 1;
    1901             :                 }
    1902             :         }
    1903             : #endif
    1904           0 : }
    1905             : /* }}} */
    1906             : 
    1907             : /* {{{ php_output_wrapper
    1908             :  */
    1909      349995 : static int php_output_wrapper(const char *str, uint str_length)
    1910             : {
    1911             :         TSRMLS_FETCH();
    1912      349995 :         return php_output_write(str, str_length TSRMLS_CC);
    1913             : }
    1914             : /* }}} */
    1915             : 
    1916             : #ifdef ZTS
    1917             : /* {{{ core_globals_ctor
    1918             :  */
    1919             : static void core_globals_ctor(php_core_globals *core_globals TSRMLS_DC)
    1920             : {
    1921             :         memset(core_globals, 0, sizeof(*core_globals));
    1922             : 
    1923             :         php_startup_ticks(TSRMLS_C);
    1924             : }
    1925             : /* }}} */
    1926             : #endif
    1927             : 
    1928             : /* {{{ core_globals_dtor
    1929             :  */
    1930       21290 : static void core_globals_dtor(php_core_globals *core_globals TSRMLS_DC)
    1931             : {
    1932       21290 :         if (core_globals->last_error_message) {
    1933           4 :                 free(core_globals->last_error_message);
    1934             :         }
    1935       21290 :         if (core_globals->last_error_file) {
    1936           4 :                 free(core_globals->last_error_file);
    1937             :         }
    1938       21290 :         if (core_globals->disable_functions) {
    1939           3 :                 free(core_globals->disable_functions);
    1940             :         }
    1941       21290 :         if (core_globals->disable_classes) {
    1942           1 :                 free(core_globals->disable_classes);
    1943             :         }
    1944       21290 :         if (core_globals->php_binary) {
    1945       21290 :                 free(core_globals->php_binary);
    1946             :         }
    1947             : 
    1948       21290 :         php_shutdown_ticks(TSRMLS_C);
    1949       21290 : }
    1950             : /* }}} */
    1951             : 
    1952         150 : PHP_MINFO_FUNCTION(php_core) { /* {{{ */
    1953         150 :         php_info_print_table_start();
    1954         150 :         php_info_print_table_row(2, "PHP Version", PHP_VERSION);
    1955         150 :         php_info_print_table_end();
    1956         150 :         DISPLAY_INI_ENTRIES();
    1957         150 : }
    1958             : /* }}} */
    1959             : 
    1960             : /* {{{ php_register_extensions
    1961             :  */
    1962       21257 : int php_register_extensions(zend_module_entry **ptr, int count TSRMLS_DC)
    1963             : {
    1964       21257 :         zend_module_entry **end = ptr + count;
    1965             : 
    1966     1573018 :         while (ptr < end) {
    1967     1530504 :                 if (*ptr) {
    1968     1530504 :                         if (zend_register_internal_module(*ptr TSRMLS_CC)==NULL) {
    1969           0 :                                 return FAILURE;
    1970             :                         }
    1971             :                 }
    1972     1530504 :                 ptr++;
    1973             :         }
    1974       21257 :         return SUCCESS;
    1975             : }
    1976             : 
    1977             : /* A very long time ago php_module_startup() was refactored in a way
    1978             :  * which broke calling it with more than one additional module.
    1979             :  * This alternative to php_register_extensions() works around that
    1980             :  * by walking the shallower structure.
    1981             :  *
    1982             :  * See algo: https://bugs.php.net/bug.php?id=63159
    1983             :  */
    1984       21257 : static int php_register_extensions_bc(zend_module_entry *ptr, int count TSRMLS_DC)
    1985             : {
    1986       42918 :         while (count--) {
    1987         404 :                 if (zend_register_internal_module(ptr++ TSRMLS_CC) == NULL) {
    1988           0 :                         return FAILURE;
    1989             :                 }
    1990             :         }
    1991       21257 :         return SUCCESS;
    1992             : }
    1993             : /* }}} */
    1994             : 
    1995             : #if defined(PHP_WIN32) && _MSC_VER >= 1400
    1996             : static _invalid_parameter_handler old_invalid_parameter_handler;
    1997             : 
    1998             : void dummy_invalid_parameter_handler(
    1999             :                 const wchar_t *expression,
    2000             :                 const wchar_t *function,
    2001             :                 const wchar_t *file,
    2002             :                 unsigned int   line,
    2003             :                 uintptr_t      pEwserved)
    2004             : {
    2005             :         static int called = 0;
    2006             :         char buf[1024];
    2007             :         int len;
    2008             : 
    2009             :         if (!called) {
    2010             :                 TSRMLS_FETCH();
    2011             :                 if(PG(windows_show_crt_warning)) {
    2012             :                         called = 1;
    2013             :                         if (function) {
    2014             :                                 if (file) {
    2015             :                                         len = _snprintf(buf, sizeof(buf)-1, "Invalid parameter detected in CRT function '%ws' (%ws:%d)", function, file, line);
    2016             :                                 } else {
    2017             :                                         len = _snprintf(buf, sizeof(buf)-1, "Invalid parameter detected in CRT function '%ws'", function);
    2018             :                                 }
    2019             :                         } else {
    2020             :                                 len = _snprintf(buf, sizeof(buf)-1, "Invalid CRT parameter detected (function not known)");
    2021             :                         }
    2022             :                         zend_error(E_WARNING, "%s", buf);
    2023             :                         called = 0;
    2024             :                 }
    2025             :         }
    2026             : }
    2027             : #endif
    2028             : 
    2029             : /* {{{ php_module_startup
    2030             :  */
    2031       21257 : int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules)
    2032             : {
    2033             :         zend_utility_functions zuf;
    2034             :         zend_utility_values zuv;
    2035       21257 :         int retval = SUCCESS, module_number=0;  /* for REGISTER_INI_ENTRIES() */
    2036             :         char *php_os;
    2037             :         zend_module_entry *module;
    2038             : #ifdef ZTS
    2039             :         zend_executor_globals *executor_globals;
    2040             :         void ***tsrm_ls;
    2041             :         php_core_globals *core_globals;
    2042             : #endif
    2043             : 
    2044             : #if defined(PHP_WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
    2045             :         WORD wVersionRequested = MAKEWORD(2, 0);
    2046             :         WSADATA wsaData;
    2047             : #endif
    2048             : #ifdef PHP_WIN32
    2049             :         php_os = "WINNT";
    2050             : #if _MSC_VER >= 1400
    2051             :         old_invalid_parameter_handler =
    2052             :                 _set_invalid_parameter_handler(dummy_invalid_parameter_handler);
    2053             :         if (old_invalid_parameter_handler != NULL) {
    2054             :                 _set_invalid_parameter_handler(old_invalid_parameter_handler);
    2055             :         }
    2056             : 
    2057             :         /* Disable the message box for assertions.*/
    2058             :         _CrtSetReportMode(_CRT_ASSERT, 0);
    2059             : #endif
    2060             : #else
    2061       21257 :         php_os=PHP_OS;
    2062             : #endif
    2063             : 
    2064             : #ifdef ZTS
    2065             :         tsrm_ls = ts_resource(0);
    2066             : #endif
    2067             : 
    2068             : #ifdef PHP_WIN32
    2069             :         php_win32_init_rng_lock();
    2070             : #endif
    2071             : 
    2072       21257 :         module_shutdown = 0;
    2073       21257 :         module_startup = 1;
    2074       21257 :         sapi_initialize_empty_request(TSRMLS_C);
    2075       21257 :         sapi_activate(TSRMLS_C);
    2076             : 
    2077       21257 :         if (module_initialized) {
    2078           0 :                 return SUCCESS;
    2079             :         }
    2080             : 
    2081       21257 :         sapi_module = *sf;
    2082             : 
    2083       21257 :         php_output_startup();
    2084             : 
    2085       21257 :         zuf.error_function = php_error_cb;
    2086       21257 :         zuf.printf_function = php_printf;
    2087       21257 :         zuf.write_function = php_output_wrapper;
    2088       21257 :         zuf.fopen_function = php_fopen_wrapper_for_zend;
    2089       21257 :         zuf.message_handler = php_message_handler_for_zend;
    2090       21257 :         zuf.block_interruptions = sapi_module.block_interruptions;
    2091       21257 :         zuf.unblock_interruptions = sapi_module.unblock_interruptions;
    2092       21257 :         zuf.get_configuration_directive = php_get_configuration_directive_for_zend;
    2093       21257 :         zuf.ticks_function = php_run_ticks;
    2094       21257 :         zuf.on_timeout = php_on_timeout;
    2095       21257 :         zuf.stream_open_function = php_stream_open_for_zend;
    2096       21257 :         zuf.vspprintf_function = vspprintf;
    2097       21257 :         zuf.getenv_function = sapi_getenv;
    2098       21257 :         zuf.resolve_path_function = php_resolve_path_for_zend;
    2099       21257 :         zend_startup(&zuf, NULL TSRMLS_CC);
    2100             : 
    2101             : #ifdef ZTS
    2102             :         executor_globals = ts_resource(executor_globals_id);
    2103             :         ts_allocate_id(&core_globals_id, sizeof(php_core_globals), (ts_allocate_ctor) core_globals_ctor, (ts_allocate_dtor) core_globals_dtor);
    2104             :         core_globals = ts_resource(core_globals_id);
    2105             : #ifdef PHP_WIN32
    2106             :         ts_allocate_id(&php_win32_core_globals_id, sizeof(php_win32_core_globals), (ts_allocate_ctor) php_win32_core_globals_ctor, (ts_allocate_dtor) php_win32_core_globals_dtor);
    2107             : #endif
    2108             : #else
    2109       21257 :         php_startup_ticks(TSRMLS_C);
    2110             : #endif
    2111       21257 :         gc_globals_ctor(TSRMLS_C);
    2112             : 
    2113             : #ifdef PHP_WIN32
    2114             :         {
    2115             :                 OSVERSIONINFOEX *osvi = &EG(windows_version_info);
    2116             : 
    2117             :                 ZeroMemory(osvi, sizeof(OSVERSIONINFOEX));
    2118             :                 osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    2119             :                 if( !GetVersionEx((OSVERSIONINFO *) osvi)) {
    2120             :                         php_printf("\nGetVersionEx unusable. %d\n", GetLastError());
    2121             :                         return FAILURE;
    2122             :                 }
    2123             :         }
    2124             : #endif
    2125       21257 :         EG(bailout) = NULL;
    2126       21257 :         EG(error_reporting) = E_ALL & ~E_NOTICE;
    2127       21257 :         EG(active_symbol_table) = NULL;
    2128       21257 :         PG(header_is_being_sent) = 0;
    2129       21257 :         SG(request_info).headers_only = 0;
    2130       21257 :         SG(request_info).argv0 = NULL;
    2131       21257 :         SG(request_info).argc=0;
    2132       21257 :         SG(request_info).argv=(char **)NULL;
    2133       21257 :         PG(connection_status) = PHP_CONNECTION_NORMAL;
    2134       21257 :         PG(during_request_startup) = 0;
    2135       21257 :         PG(last_error_message) = NULL;
    2136       21257 :         PG(last_error_file) = NULL;
    2137       21257 :         PG(last_error_lineno) = 0;
    2138       21257 :         EG(error_handling)  = EH_NORMAL;
    2139       21257 :         EG(exception_class) = NULL;
    2140       21257 :         PG(disable_functions) = NULL;
    2141       21257 :         PG(disable_classes) = NULL;
    2142       21257 :         EG(exception) = NULL;
    2143       21257 :         EG(objects_store).object_buckets = NULL;
    2144             : 
    2145             : #if HAVE_SETLOCALE
    2146       21257 :         setlocale(LC_CTYPE, "");
    2147             :         zend_update_current_locale();
    2148             : #endif
    2149             : 
    2150             : #if HAVE_TZSET
    2151       21257 :         tzset();
    2152             : #endif
    2153             : 
    2154             : #if defined(PHP_WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
    2155             :         /* start up winsock services */
    2156             :         if (WSAStartup(wVersionRequested, &wsaData) != 0) {
    2157             :                 php_printf("\nwinsock.dll unusable. %d\n", WSAGetLastError());
    2158             :                 return FAILURE;
    2159             :         }
    2160             : #endif
    2161             : 
    2162       21257 :         le_index_ptr = zend_register_list_destructors_ex(NULL, NULL, "index pointer", 0);
    2163             : 
    2164             :         /* Register constants */
    2165       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_VERSION", PHP_VERSION, sizeof(PHP_VERSION)-1, CONST_PERSISTENT | CONST_CS);
    2166       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_MAJOR_VERSION", PHP_MAJOR_VERSION, CONST_PERSISTENT | CONST_CS);
    2167       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_MINOR_VERSION", PHP_MINOR_VERSION, CONST_PERSISTENT | CONST_CS);
    2168       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_RELEASE_VERSION", PHP_RELEASE_VERSION, CONST_PERSISTENT | CONST_CS);
    2169       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTRA_VERSION", PHP_EXTRA_VERSION, sizeof(PHP_EXTRA_VERSION) - 1, CONST_PERSISTENT | CONST_CS);
    2170       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_VERSION_ID", PHP_VERSION_ID, CONST_PERSISTENT | CONST_CS);
    2171             : #ifdef ZTS
    2172             :         REGISTER_MAIN_LONG_CONSTANT("PHP_ZTS", 1, CONST_PERSISTENT | CONST_CS);
    2173             : #else
    2174       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_ZTS", 0, CONST_PERSISTENT | CONST_CS);
    2175             : #endif
    2176       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_DEBUG", PHP_DEBUG, CONST_PERSISTENT | CONST_CS);
    2177       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS", php_os, strlen(php_os), CONST_PERSISTENT | CONST_CS);
    2178       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_SAPI", sapi_module.name, strlen(sapi_module.name), CONST_PERSISTENT | CONST_CS);
    2179       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("DEFAULT_INCLUDE_PATH", PHP_INCLUDE_PATH, sizeof(PHP_INCLUDE_PATH)-1, CONST_PERSISTENT | CONST_CS);
    2180       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PEAR_INSTALL_DIR", PEAR_INSTALLDIR, sizeof(PEAR_INSTALLDIR)-1, CONST_PERSISTENT | CONST_CS);
    2181       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PEAR_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
    2182       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
    2183       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_PREFIX", PHP_PREFIX, sizeof(PHP_PREFIX)-1, CONST_PERSISTENT | CONST_CS);
    2184       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINDIR", PHP_BINDIR, sizeof(PHP_BINDIR)-1, CONST_PERSISTENT | CONST_CS);
    2185             : #ifndef PHP_WIN32
    2186       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_MANDIR", PHP_MANDIR, sizeof(PHP_MANDIR)-1, CONST_PERSISTENT | CONST_CS);
    2187             : #endif
    2188       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_LIBDIR", PHP_LIBDIR, sizeof(PHP_LIBDIR)-1, CONST_PERSISTENT | CONST_CS);
    2189       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_DATADIR", PHP_DATADIR, sizeof(PHP_DATADIR)-1, CONST_PERSISTENT | CONST_CS);
    2190       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_SYSCONFDIR", PHP_SYSCONFDIR, sizeof(PHP_SYSCONFDIR)-1, CONST_PERSISTENT | CONST_CS);
    2191       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_LOCALSTATEDIR", PHP_LOCALSTATEDIR, sizeof(PHP_LOCALSTATEDIR)-1, CONST_PERSISTENT | CONST_CS);
    2192       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_PATH", PHP_CONFIG_FILE_PATH, strlen(PHP_CONFIG_FILE_PATH), CONST_PERSISTENT | CONST_CS);
    2193       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_SCAN_DIR", PHP_CONFIG_FILE_SCAN_DIR, sizeof(PHP_CONFIG_FILE_SCAN_DIR)-1, CONST_PERSISTENT | CONST_CS);
    2194       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
    2195       21257 :         REGISTER_MAIN_STRINGL_CONSTANT("PHP_EOL", PHP_EOL, sizeof(PHP_EOL)-1, CONST_PERSISTENT | CONST_CS);
    2196       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
    2197       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", LONG_MAX, CONST_PERSISTENT | CONST_CS);
    2198       21257 :         REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", sizeof(long), CONST_PERSISTENT | CONST_CS);
    2199             : 
    2200             : #ifdef PHP_WIN32
    2201             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_MAJOR",      EG(windows_version_info).dwMajorVersion, CONST_PERSISTENT | CONST_CS);
    2202             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_MINOR",      EG(windows_version_info).dwMinorVersion, CONST_PERSISTENT | CONST_CS);
    2203             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_BUILD",      EG(windows_version_info).dwBuildNumber, CONST_PERSISTENT | CONST_CS);
    2204             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_PLATFORM",   EG(windows_version_info).dwPlatformId, CONST_PERSISTENT | CONST_CS);
    2205             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SP_MAJOR",   EG(windows_version_info).wServicePackMajor, CONST_PERSISTENT | CONST_CS);
    2206             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SP_MINOR",   EG(windows_version_info).wServicePackMinor, CONST_PERSISTENT | CONST_CS);
    2207             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SUITEMASK",  EG(windows_version_info).wSuiteMask, CONST_PERSISTENT | CONST_CS);
    2208             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_PRODUCTTYPE", EG(windows_version_info).wProductType, CONST_PERSISTENT | CONST_CS);
    2209             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_DOMAIN_CONTROLLER", VER_NT_DOMAIN_CONTROLLER, CONST_PERSISTENT | CONST_CS);
    2210             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_SERVER", VER_NT_SERVER, CONST_PERSISTENT | CONST_CS);
    2211             :         REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_WORKSTATION", VER_NT_WORKSTATION, CONST_PERSISTENT | CONST_CS);
    2212             : #endif
    2213             : 
    2214       21257 :         php_binary_init(TSRMLS_C);
    2215       21257 :         if (PG(php_binary)) {
    2216       21257 :                 REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", PG(php_binary), strlen(PG(php_binary)), CONST_PERSISTENT | CONST_CS);
    2217             :         } else {
    2218           0 :                 REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", "", 0, CONST_PERSISTENT | CONST_CS);
    2219             :         }
    2220             : 
    2221       21257 :         php_output_register_constants(TSRMLS_C);
    2222       21257 :         php_rfc1867_register_constants(TSRMLS_C);
    2223             : 
    2224             :         /* this will read in php.ini, set up the configuration parameters,
    2225             :            load zend extensions and register php function extensions
    2226             :            to be loaded later */
    2227       21257 :         if (php_init_config(TSRMLS_C) == FAILURE) {
    2228           0 :                 return FAILURE;
    2229             :         }
    2230             : 
    2231             :         /* Register PHP core ini entries */
    2232       21257 :         REGISTER_INI_ENTRIES();
    2233             : 
    2234             :         /* Register Zend ini entries */
    2235       21257 :         zend_register_standard_ini_entries(TSRMLS_C);
    2236             : 
    2237             :         /* Disable realpath cache if an open_basedir is set */
    2238       21257 :         if (PG(open_basedir) && *PG(open_basedir)) {
    2239          72 :                 CWDG(realpath_cache_size_limit) = 0;
    2240             :         }
    2241             : 
    2242             :         /* initialize stream wrappers registry
    2243             :          * (this uses configuration parameters from php.ini)
    2244             :          */
    2245       21257 :         if (php_init_stream_wrappers(module_number TSRMLS_CC) == FAILURE)       {
    2246           0 :                 php_printf("PHP:  Unable to initialize stream url wrappers.\n");
    2247           0 :                 return FAILURE;
    2248             :         }
    2249             : 
    2250       21257 :         zuv.html_errors = 1;
    2251       21257 :         zuv.import_use_extension = ".php";
    2252       21257 :         php_startup_auto_globals(TSRMLS_C);
    2253       21257 :         zend_set_utility_values(&zuv);
    2254       21257 :         php_startup_sapi_content_types(TSRMLS_C);
    2255             : 
    2256             :         /* startup extensions statically compiled in */
    2257       21257 :         if (php_register_internal_extensions_func(TSRMLS_C) == FAILURE) {
    2258           0 :                 php_printf("Unable to start builtin modules\n");
    2259           0 :                 return FAILURE;
    2260             :         }
    2261             : 
    2262             :         /* start additional PHP extensions */
    2263       21257 :         php_register_extensions_bc(additional_modules, num_additional_modules TSRMLS_CC);
    2264             : 
    2265             :         /* load and startup extensions compiled as shared objects (aka DLLs)
    2266             :            as requested by php.ini entries
    2267             :            theese are loaded after initialization of internal extensions
    2268             :            as extensions *might* rely on things from ext/standard
    2269             :            which is always an internal extension and to be initialized
    2270             :            ahead of all other internals
    2271             :          */
    2272       21257 :         php_ini_register_extensions(TSRMLS_C);
    2273       21257 :         zend_startup_modules(TSRMLS_C);
    2274             : 
    2275             :         /* start Zend extensions */
    2276       21257 :         zend_startup_extensions();
    2277             : 
    2278       21257 :         zend_collect_module_handlers(TSRMLS_C);
    2279             : 
    2280             :         /* register additional functions */
    2281       21257 :         if (sapi_module.additional_functions) {
    2282       21044 :                 if (zend_hash_find(&module_registry, "standard", sizeof("standard"), (void**)&module)==SUCCESS) {
    2283       21044 :                         EG(current_module) = module;
    2284       21044 :                         zend_register_functions(NULL, sapi_module.additional_functions, NULL, MODULE_PERSISTENT TSRMLS_CC);
    2285       21044 :                         EG(current_module) = NULL;
    2286             :                 }
    2287             :         }
    2288             : 
    2289             :         /* disable certain classes and functions as requested by php.ini */
    2290       21257 :         php_disable_functions(TSRMLS_C);
    2291       21257 :         php_disable_classes(TSRMLS_C);
    2292             : 
    2293             :         /* make core report what it should */
    2294       21257 :         if (zend_hash_find(&module_registry, "core", sizeof("core"), (void**)&module)==SUCCESS) {
    2295       21257 :                 module->version = PHP_VERSION;
    2296       21257 :                 module->info_func = PHP_MINFO(php_core);
    2297             :         }
    2298             : 
    2299             : 
    2300             : #ifdef PHP_WIN32
    2301             :         /* Disable incompatible functions for the running platform */
    2302             :         if (php_win32_disable_functions(TSRMLS_C) == FAILURE) {
    2303             :                 php_printf("Unable to disable unsupported functions\n");
    2304             :                 return FAILURE;
    2305             :         }
    2306             : #endif
    2307             : 
    2308       21257 :         zend_post_startup(TSRMLS_C);
    2309             : 
    2310       21257 :         module_initialized = 1;
    2311             : 
    2312             :         /* Check for deprecated directives */
    2313             :         /* NOTE: If you add anything here, remember to add it to Makefile.global! */
    2314             :         {
    2315             :                 struct {
    2316             :                         const long error_level;
    2317             :                         const char *phrase;
    2318             :                         const char *directives[16]; /* Remember to change this if the number of directives change */
    2319             :                 } directives[2] = {
    2320             :                         {
    2321             :                                 E_DEPRECATED,
    2322             :                                 "Directive '%s' is deprecated in PHP 5.3 and greater",
    2323             :                                 {
    2324             :                                         NULL
    2325             :                                 }
    2326             :                         },
    2327             :                         {
    2328             :                                 E_CORE_ERROR,
    2329             :                                 "Directive '%s' is no longer available in PHP",
    2330             :                                 {
    2331             :                                         "allow_call_time_pass_reference",
    2332             :                                         "define_syslog_variables",
    2333             :                                         "highlight.bg",
    2334             :                                         "magic_quotes_gpc",
    2335             :                                         "magic_quotes_runtime",
    2336             :                                         "magic_quotes_sybase",
    2337             :                                         "register_globals",
    2338             :                                         "register_long_arrays",
    2339             :                                         "safe_mode",
    2340             :                                         "safe_mode_gid",
    2341             :                                         "safe_mode_include_dir",
    2342             :                                         "safe_mode_exec_dir",
    2343             :                                         "safe_mode_allowed_env_vars",
    2344             :                                         "safe_mode_protected_env_vars",
    2345             :                                         "zend.ze1_compatibility_mode",
    2346             :                                         NULL
    2347             :                                 }
    2348             :                         }
    2349       21257 :                 };
    2350             : 
    2351             :                 unsigned int i;
    2352             : 
    2353       21257 :                 zend_try {
    2354             :                         /* 2 = Count of deprecation structs */
    2355       63769 :                         for (i = 0; i < 2; i++) {
    2356       42514 :                                 const char **p = directives[i].directives;
    2357             : 
    2358      403859 :                                 while(*p) {
    2359             :                                         long value;
    2360             : 
    2361      318833 :                                         if (cfg_get_long((char*)*p, &value) == SUCCESS && value) {
    2362           2 :                                                 zend_error(directives[i].error_level, directives[i].phrase, *p);
    2363             :                                         }
    2364             : 
    2365      318831 :                                         ++p;
    2366             :                                 }
    2367             :                         }
    2368           2 :                 } zend_catch {
    2369           2 :                         retval = FAILURE;
    2370       21257 :                 } zend_end_try();
    2371             :         }
    2372             : 
    2373       21257 :         sapi_deactivate(TSRMLS_C);
    2374       21257 :         module_startup = 0;
    2375             : 
    2376       21257 :         shutdown_memory_manager(1, 0 TSRMLS_CC);
    2377       21257 :         zend_interned_strings_snapshot(TSRMLS_C);
    2378       21257 :         virtual_cwd_activate(TSRMLS_C);
    2379             : 
    2380             :         /* we're done */
    2381       21257 :         return retval;
    2382             : }
    2383             : /* }}} */
    2384             : 
    2385           0 : void php_module_shutdown_for_exec(void)
    2386             : {
    2387             :         /* used to close fd's in the range 3.255 here, but it's problematic */
    2388           0 : }
    2389             : 
    2390             : /* {{{ php_module_shutdown_wrapper
    2391             :  */
    2392           0 : int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals)
    2393             : {
    2394             :         TSRMLS_FETCH();
    2395           0 :         php_module_shutdown(TSRMLS_C);
    2396           0 :         return SUCCESS;
    2397             : }
    2398             : /* }}} */
    2399             : 
    2400             : /* {{{ php_module_shutdown
    2401             :  */
    2402       21290 : void php_module_shutdown(TSRMLS_D)
    2403             : {
    2404       21290 :         int module_number=0;    /* for UNREGISTER_INI_ENTRIES() */
    2405             : 
    2406       21290 :         module_shutdown = 1;
    2407             : 
    2408       21290 :         if (!module_initialized) {
    2409           0 :                 return;
    2410             :         }
    2411             : 
    2412             : #ifdef ZTS
    2413             :         ts_free_worker_threads();
    2414             : #endif
    2415             : 
    2416             : #if defined(PHP_WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
    2417             :         /*close winsock */
    2418             :         WSACleanup();
    2419             : #endif
    2420             : 
    2421             : #ifdef PHP_WIN32
    2422             :         php_win32_free_rng_lock();
    2423             : #endif
    2424             : 
    2425       21290 :         sapi_flush(TSRMLS_C);
    2426             : 
    2427       21290 :         zend_shutdown(TSRMLS_C);
    2428             : 
    2429             :         /* Destroys filter & transport registries too */
    2430       21290 :         php_shutdown_stream_wrappers(module_number TSRMLS_CC);
    2431             : 
    2432       21290 :         UNREGISTER_INI_ENTRIES();
    2433             : 
    2434             :         /* close down the ini config */
    2435       21290 :         php_shutdown_config();
    2436             : 
    2437             : #ifndef ZTS
    2438       21290 :         zend_ini_shutdown(TSRMLS_C);
    2439       21290 :         shutdown_memory_manager(CG(unclean_shutdown), 1 TSRMLS_CC);
    2440             : #else
    2441             :         zend_ini_global_shutdown(TSRMLS_C);
    2442             : #endif
    2443             : 
    2444       21290 :         php_output_shutdown();
    2445       21290 :         php_shutdown_temporary_directory();
    2446             : 
    2447       21290 :         module_initialized = 0;
    2448             : 
    2449             : #ifndef ZTS
    2450       21290 :         core_globals_dtor(&core_globals TSRMLS_CC);
    2451       21290 :         gc_globals_dtor(TSRMLS_C);
    2452             : #else
    2453             :         ts_free_id(core_globals_id);
    2454             : #endif
    2455             : 
    2456             : #if defined(PHP_WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1400)
    2457             :         if (old_invalid_parameter_handler == NULL) {
    2458             :                 _set_invalid_parameter_handler(old_invalid_parameter_handler);
    2459             :         }
    2460             : #endif
    2461             : }
    2462             : /* }}} */
    2463             : 
    2464             : /* {{{ php_execute_script
    2465             :  */
    2466       21157 : PHPAPI int php_execute_script(zend_file_handle *primary_file TSRMLS_DC)
    2467             : {
    2468             :         zend_file_handle *prepend_file_p, *append_file_p;
    2469       21157 :         zend_file_handle prepend_file = {0}, append_file = {0};
    2470             : #if HAVE_BROKEN_GETCWD 
    2471             :         volatile int old_cwd_fd = -1;
    2472             : #else
    2473             :         char *old_cwd;
    2474             :         ALLOCA_FLAG(use_heap)
    2475             : #endif
    2476       21157 :         int retval = 0;
    2477             : 
    2478       21157 :         EG(exit_status) = 0;
    2479             : #ifndef HAVE_BROKEN_GETCWD
    2480             : # define OLD_CWD_SIZE 4096
    2481       21157 :         old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);
    2482       21157 :         old_cwd[0] = '\0';
    2483             : #endif
    2484             : 
    2485       21157 :         zend_try {
    2486             :                 char realfile[MAXPATHLEN];
    2487             : 
    2488             : #ifdef PHP_WIN32
    2489             :                 if(primary_file->filename) {
    2490             :                         UpdateIniFromRegistry(primary_file->filename TSRMLS_CC);
    2491             :                 }
    2492             : #endif
    2493             : 
    2494       21157 :                 PG(during_request_startup) = 0;
    2495             : 
    2496       21157 :                 if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
    2497             : #if HAVE_BROKEN_GETCWD
    2498             :                         /* this looks nasty to me */
    2499             :                         old_cwd_fd = open(".", 0);
    2500             : #else
    2501         237 :                         php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
    2502             : #endif
    2503         237 :                         VCWD_CHDIR_FILE(primary_file->filename);
    2504             :                 }
    2505             : 
    2506             :                 /* Only lookup the real file path and add it to the included_files list if already opened
    2507             :                  *   otherwise it will get opened and added to the included_files list in zend_execute_scripts
    2508             :                  */
    2509       84235 :                 if (primary_file->filename &&
    2510       21160 :                     (primary_file->filename[0] != '-' || primary_file->filename[1] != 0) &&
    2511       21154 :                         primary_file->opened_path == NULL &&
    2512       20764 :                         primary_file->type != ZEND_HANDLE_FILENAME
    2513             :                 ) {
    2514             :                         int realfile_len;
    2515       20764 :                         int dummy = 1;
    2516             : 
    2517       20764 :                         if (expand_filepath(primary_file->filename, realfile TSRMLS_CC)) {
    2518       20764 :                                 realfile_len =  strlen(realfile);
    2519       20764 :                                 zend_hash_add(&EG(included_files), realfile, realfile_len+1, (void *)&dummy, sizeof(int), NULL);
    2520       20764 :                                 primary_file->opened_path = estrndup(realfile, realfile_len);
    2521             :                         }
    2522             :                 }
    2523             : 
    2524       21158 :                 if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
    2525           1 :                         prepend_file.filename = PG(auto_prepend_file);
    2526           1 :                         prepend_file.opened_path = NULL;
    2527           1 :                         prepend_file.free_filename = 0;
    2528           1 :                         prepend_file.type = ZEND_HANDLE_FILENAME;
    2529           1 :                         prepend_file_p = &prepend_file;
    2530             :                 } else {
    2531       21156 :                         prepend_file_p = NULL;
    2532             :                 }
    2533             : 
    2534       21157 :                 if (PG(auto_append_file) && PG(auto_append_file)[0]) {
    2535           0 :                         append_file.filename = PG(auto_append_file);
    2536           0 :                         append_file.opened_path = NULL;
    2537           0 :                         append_file.free_filename = 0;
    2538           0 :                         append_file.type = ZEND_HANDLE_FILENAME;
    2539           0 :                         append_file_p = &append_file;
    2540             :                 } else {
    2541       21157 :                         append_file_p = NULL;
    2542             :                 }
    2543       21157 :                 if (PG(max_input_time) != -1) {
    2544             : #ifdef PHP_WIN32
    2545             :                         zend_unset_timeout(TSRMLS_C);
    2546             : #endif
    2547           0 :                         zend_set_timeout(INI_INT("max_execution_time"), 0);
    2548             :                 }
    2549       21157 :                 retval = (zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
    2550             : 
    2551       21195 :         } zend_end_try();
    2552             : 
    2553             : #if HAVE_BROKEN_GETCWD
    2554             :         if (old_cwd_fd != -1) {
    2555             :                 fchdir(old_cwd_fd);
    2556             :                 close(old_cwd_fd);
    2557             :         }
    2558             : #else
    2559       21195 :         if (old_cwd[0] != '\0') {
    2560         237 :                 php_ignore_value(VCWD_CHDIR(old_cwd));
    2561             :         }
    2562       21195 :         free_alloca(old_cwd, use_heap);
    2563             : #endif
    2564       21195 :         return retval;
    2565             : }
    2566             : /* }}} */
    2567             : 
    2568             : /* {{{ php_execute_simple_script
    2569             :  */
    2570           0 : PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval **ret TSRMLS_DC)
    2571             : {
    2572             :         char *old_cwd;
    2573             :         ALLOCA_FLAG(use_heap)
    2574             : 
    2575           0 :         EG(exit_status) = 0;
    2576             : #define OLD_CWD_SIZE 4096
    2577           0 :         old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);
    2578           0 :         old_cwd[0] = '\0';
    2579             : 
    2580           0 :         zend_try {
    2581             : #ifdef PHP_WIN32
    2582             :                 if(primary_file->filename) {
    2583             :                         UpdateIniFromRegistry(primary_file->filename TSRMLS_CC);
    2584             :                 }
    2585             : #endif
    2586             : 
    2587           0 :                 PG(during_request_startup) = 0;
    2588             : 
    2589           0 :                 if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
    2590           0 :                         php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
    2591           0 :                         VCWD_CHDIR_FILE(primary_file->filename);
    2592             :                 }
    2593           0 :                 zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, ret, 1, primary_file);
    2594           0 :         } zend_end_try();
    2595             : 
    2596           0 :         if (old_cwd[0] != '\0') {
    2597           0 :                 php_ignore_value(VCWD_CHDIR(old_cwd));
    2598             :         }
    2599             : 
    2600           0 :         free_alloca(old_cwd, use_heap);
    2601           0 :         return EG(exit_status);
    2602             : }
    2603             : /* }}} */
    2604             : 
    2605             : /* {{{ php_handle_aborted_connection
    2606             :  */
    2607           2 : PHPAPI void php_handle_aborted_connection(void)
    2608             : {
    2609             :         TSRMLS_FETCH();
    2610             : 
    2611           2 :         PG(connection_status) = PHP_CONNECTION_ABORTED;
    2612           2 :         php_output_set_status(PHP_OUTPUT_DISABLED TSRMLS_CC);
    2613             : 
    2614           2 :         if (!PG(ignore_user_abort)) {
    2615           2 :                 zend_bailout();
    2616             :         }
    2617           0 : }
    2618             : /* }}} */
    2619             : 
    2620             : /* {{{ php_handle_auth_data
    2621             :  */
    2622         217 : PHPAPI int php_handle_auth_data(const char *auth TSRMLS_DC)
    2623             : {
    2624         217 :         int ret = -1;
    2625             : 
    2626         217 :         if (auth && auth[0] != '\0' && strncmp(auth, "Basic ", 6) == 0) {
    2627             :                 char *pass;
    2628             :                 char *user;
    2629             : 
    2630           0 :                 user = php_base64_decode(auth + 6, strlen(auth) - 6, NULL);
    2631           0 :                 if (user) {
    2632           0 :                         pass = strchr(user, ':');
    2633           0 :                         if (pass) {
    2634           0 :                                 *pass++ = '\0';
    2635           0 :                                 SG(request_info).auth_user = user;
    2636           0 :                                 SG(request_info).auth_password = estrdup(pass);
    2637           0 :                                 ret = 0;
    2638             :                         } else {
    2639           0 :                                 efree(user);
    2640             :                         }
    2641             :                 }
    2642             :         }
    2643             : 
    2644         217 :         if (ret == -1) {
    2645         217 :                 SG(request_info).auth_user = SG(request_info).auth_password = NULL;
    2646             :         } else {
    2647           0 :                 SG(request_info).auth_digest = NULL;
    2648             :         }
    2649             : 
    2650         217 :         if (ret == -1 && auth && auth[0] != '\0' && strncmp(auth, "Digest ", 7) == 0) {
    2651           0 :                 SG(request_info).auth_digest = estrdup(auth + 7);
    2652           0 :                 ret = 0;
    2653             :         }
    2654             : 
    2655         217 :         if (ret == -1) {
    2656         217 :                 SG(request_info).auth_digest = NULL;
    2657             :         }
    2658             : 
    2659         217 :         return ret;
    2660             : }
    2661             : /* }}} */
    2662             : 
    2663             : /* {{{ php_lint_script
    2664             :  */
    2665           5 : PHPAPI int php_lint_script(zend_file_handle *file TSRMLS_DC)
    2666             : {
    2667             :         zend_op_array *op_array;
    2668           5 :         int retval = FAILURE;
    2669             : 
    2670           5 :         zend_try {
    2671           5 :                 op_array = zend_compile_file(file, ZEND_INCLUDE TSRMLS_CC);
    2672           3 :                 zend_destroy_file_handle(file TSRMLS_CC);
    2673             : 
    2674           3 :                 if (op_array) {
    2675           3 :                         destroy_op_array(op_array TSRMLS_CC);
    2676           3 :                         efree(op_array);
    2677           3 :                         retval = SUCCESS;
    2678             :                 }
    2679           5 :         } zend_end_try();
    2680             : 
    2681           5 :         return retval;
    2682             : }
    2683             : /* }}} */
    2684             : 
    2685             : #ifdef PHP_WIN32
    2686             : /* {{{ dummy_indent
    2687             :    just so that this symbol gets exported... */
    2688             : PHPAPI void dummy_indent(TSRMLS_D)
    2689             : {
    2690             :         zend_indent(TSRMLS_C);
    2691             : }
    2692             : /* }}} */
    2693             : #endif
    2694             : 
    2695             : /*
    2696             :  * Local variables:
    2697             :  * tab-width: 4
    2698             :  * c-basic-offset: 4
    2699             :  * End:
    2700             :  * vim600: sw=4 ts=4 fdm=marker
    2701             :  * vim<600: sw=4 ts=4
    2702             :  */

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:24 +0000 (3 days ago)

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