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: 727 941 77.3 %
Date: 2015-04-14 Functions: 50 60 83.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:57 +0000 (13 days ago)

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