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

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:35 +0000 (3 days ago)

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