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: 741 959 77.3 %
Date: 2016-09-18 Functions: 53 63 84.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:21 +0000 (6 days ago)

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