~ubuntu-branches/ubuntu/breezy/php5/breezy-security

« back to all changes in this revision

Viewing changes to main/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
4
   +----------------------------------------------------------------------+
 
5
   | Copyright (c) 1997-2004 The PHP Group                                |
 
6
   +----------------------------------------------------------------------+
 
7
   | This source file is subject to version 3.0 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_0.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: main.c,v 1.604.2.22 2005/08/16 18:11:34 helly Exp $ */
 
22
 
 
23
/* {{{ includes
 
24
 */
 
25
 
 
26
#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
 
27
 
 
28
#include "php.h"
 
29
#include <stdio.h>
 
30
#ifdef PHP_WIN32
 
31
#include "win32/time.h"
 
32
#include "win32/signal.h"
 
33
#include <process.h>
 
34
#elif defined(NETWARE)
 
35
#include <sys/timeval.h>
 
36
#ifdef USE_WINSOCK
 
37
#include <novsock2.h>
 
38
#endif
 
39
#endif
 
40
#if HAVE_SYS_TIME_H
 
41
#include <sys/time.h>
 
42
#endif
 
43
#if HAVE_UNISTD_H
 
44
#include <unistd.h>
 
45
#endif
 
46
#if HAVE_SIGNAL_H
 
47
#include <signal.h>
 
48
#endif
 
49
#if HAVE_SETLOCALE
 
50
#include <locale.h>
 
51
#endif
 
52
#include "zend.h"
 
53
#include "zend_extensions.h"
 
54
#include "php_ini.h"
 
55
#include "php_globals.h"
 
56
#include "php_main.h"
 
57
#include "fopen_wrappers.h"
 
58
#include "ext/standard/php_standard.h"
 
59
#include "php_variables.h"
 
60
#include "ext/standard/credits.h"
 
61
#ifdef PHP_WIN32
 
62
#include <io.h>
 
63
#include <fcntl.h>
 
64
#include "win32/php_registry.h"
 
65
#endif
 
66
#include "php_syslog.h"
 
67
#include "Zend/zend_exceptions.h"
 
68
 
 
69
#if PHP_SIGCHILD
 
70
#include <sys/types.h>
 
71
#include <sys/wait.h>
 
72
#endif
 
73
 
 
74
#include "zend_compile.h"
 
75
#include "zend_execute.h"
 
76
#include "zend_highlight.h"
 
77
#include "zend_indent.h"
 
78
#include "zend_extensions.h"
 
79
#include "zend_ini.h"
 
80
 
 
81
#include "php_content_types.h"
 
82
#include "php_ticks.h"
 
83
#include "php_logos.h"
 
84
#include "php_streams.h"
 
85
 
 
86
#include "SAPI.h"
 
87
#include "rfc1867.h"
 
88
/* }}} */
 
89
 
 
90
#ifndef ZTS
 
91
php_core_globals core_globals;
 
92
#else
 
93
PHPAPI int core_globals_id;
 
94
#endif
 
95
 
 
96
#define SAFE_FILENAME(f) ((f)?(f):"-")
 
97
 
 
98
/* {{{ PHP_INI_MH
 
99
 */
 
100
static PHP_INI_MH(OnSetPrecision)
 
101
{
 
102
        EG(precision) = atoi(new_value);
 
103
        return SUCCESS;
 
104
}
 
105
/* }}} */
 
106
 
 
107
#if MEMORY_LIMIT
 
108
/* {{{ PHP_INI_MH
 
109
 */
 
110
static PHP_INI_MH(OnChangeMemoryLimit)
 
111
{
 
112
        if (new_value) {
 
113
                PG(memory_limit) = zend_atoi(new_value, new_value_length);
 
114
        } else {
 
115
                PG(memory_limit) = 1<<30;               /* effectively, no limit */
 
116
        }
 
117
        return zend_set_memory_limit(PG(memory_limit));
 
118
}
 
119
/* }}} */
 
120
#endif
 
121
 
 
122
 
 
123
/* {{{ php_disable_functions
 
124
 */
 
125
static void php_disable_functions(TSRMLS_D)
 
126
{
 
127
        char *s = NULL, *e;
 
128
 
 
129
        if (!*(INI_STR("disable_functions"))) {
 
130
                return;
 
131
        }
 
132
 
 
133
        e = PG(disable_functions) = strdup(INI_STR("disable_functions"));
 
134
 
 
135
        while (*e) {
 
136
                switch (*e) {
 
137
                        case ' ':
 
138
                        case ',':
 
139
                                if (s) {
 
140
                                        *e = '\0';
 
141
                                        zend_disable_function(s, e-s TSRMLS_CC);
 
142
                                        s = NULL;
 
143
                                }
 
144
                                break;
 
145
                        default:
 
146
                                if (!s) {
 
147
                                        s = e;
 
148
                                }
 
149
                                break;
 
150
                }
 
151
                e++;
 
152
        }
 
153
        if (s) {
 
154
                zend_disable_function(s, e-s TSRMLS_CC);
 
155
        }
 
156
}
 
157
/* }}} */
 
158
 
 
159
/* {{{ php_disable_classes
 
160
 */
 
161
static void php_disable_classes(TSRMLS_D)
 
162
{
 
163
        char *s = NULL, *e;
 
164
 
 
165
        if (!*(INI_STR("disable_classes"))) {
 
166
                return;
 
167
        }
 
168
 
 
169
        e = PG(disable_classes) = strdup(INI_STR("disable_classes"));
 
170
 
 
171
        while (*e) {
 
172
                switch (*e) {
 
173
                        case ' ':
 
174
                        case ',':
 
175
                                if (s) {
 
176
                                        *e = '\0';
 
177
                                        zend_disable_class(s, e-s TSRMLS_CC);
 
178
                                        s = NULL;
 
179
                                }
 
180
                                break;
 
181
                        default:
 
182
                                if (!s) {
 
183
                                        s = e;
 
184
                                }
 
185
                                break;
 
186
                }
 
187
                e++;
 
188
        }
 
189
        if (s) {
 
190
                zend_disable_class(s, e-s TSRMLS_CC);
 
191
        }
 
192
}
 
193
/* }}} */
 
194
 
 
195
/* {{{ PHP_INI_MH
 
196
 */
 
197
static PHP_INI_MH(OnUpdateTimeout)
 
198
{
 
199
        EG(timeout_seconds) = atoi(new_value);
 
200
        if (stage==PHP_INI_STAGE_STARTUP) {
 
201
                /* Don't set a timeout on startup, only per-request */
 
202
                return SUCCESS;
 
203
        }
 
204
        zend_unset_timeout(TSRMLS_C);
 
205
        zend_set_timeout(EG(timeout_seconds));
 
206
        return SUCCESS;
 
207
}
 
208
/* }}} */
 
209
 
 
210
/* Need to convert to strings and make use of:
 
211
 * PHP_SAFE_MODE
 
212
 *
 
213
 * Need to be read from the environment (?):
 
214
 * PHP_AUTO_PREPEND_FILE
 
215
 * PHP_AUTO_APPEND_FILE
 
216
 * PHP_DOCUMENT_ROOT
 
217
 * PHP_USER_DIR
 
218
 * PHP_INCLUDE_PATH
 
219
 */
 
220
 
 
221
#ifndef PHP_SAFE_MODE_EXEC_DIR
 
222
#       define PHP_SAFE_MODE_EXEC_DIR ""
 
223
#endif
 
224
 
 
225
#if defined(PHP_PROG_SENDMAIL) && !defined(NETWARE) 
 
226
#       define DEFAULT_SENDMAIL_PATH PHP_PROG_SENDMAIL " -t -i "
 
227
#else
 
228
#       define DEFAULT_SENDMAIL_PATH NULL
 
229
#endif
 
230
/* {{{ PHP_INI
 
231
 */
 
232
PHP_INI_BEGIN()
 
233
        PHP_INI_ENTRY_EX("define_syslog_variables",     "0",                            PHP_INI_ALL,    NULL,                   php_ini_boolean_displayer_cb)                   
 
234
        PHP_INI_ENTRY_EX("highlight.bg",                        HL_BG_COLOR,            PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
 
235
        PHP_INI_ENTRY_EX("highlight.comment",           HL_COMMENT_COLOR,       PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
 
236
        PHP_INI_ENTRY_EX("highlight.default",           HL_DEFAULT_COLOR,       PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
 
237
        PHP_INI_ENTRY_EX("highlight.html",                      HL_HTML_COLOR,          PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
 
238
        PHP_INI_ENTRY_EX("highlight.keyword",           HL_KEYWORD_COLOR,       PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
 
239
        PHP_INI_ENTRY_EX("highlight.string",            HL_STRING_COLOR,        PHP_INI_ALL,    NULL,                   php_ini_color_displayer_cb)
 
240
 
 
241
        STD_PHP_INI_BOOLEAN("allow_call_time_pass_reference",   "1",    PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,   allow_call_time_pass_reference, zend_compiler_globals,  compiler_globals)
 
242
        STD_PHP_INI_BOOLEAN("asp_tags",                         "0",            PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   asp_tags,                               zend_compiler_globals,  compiler_globals)
 
243
        STD_PHP_INI_BOOLEAN("display_errors",           "1",            PHP_INI_ALL,            OnUpdateBool,                   display_errors,                 php_core_globals,       core_globals)
 
244
        STD_PHP_INI_BOOLEAN("display_startup_errors",   "0",    PHP_INI_ALL,            OnUpdateBool,                   display_startup_errors, php_core_globals,       core_globals)
 
245
        STD_PHP_INI_BOOLEAN("enable_dl",                        "1",            PHP_INI_SYSTEM,         OnUpdateBool,                   enable_dl,                              php_core_globals,       core_globals)
 
246
        STD_PHP_INI_BOOLEAN("expose_php",                       "1",            PHP_INI_SYSTEM,         OnUpdateBool,                   expose_php,                             php_core_globals,       core_globals)
 
247
        STD_PHP_INI_ENTRY("docref_root",                        "",             PHP_INI_ALL,            OnUpdateString,                 docref_root,                    php_core_globals,       core_globals)
 
248
        STD_PHP_INI_ENTRY("docref_ext",                         "",                     PHP_INI_ALL,            OnUpdateString,                 docref_ext,                             php_core_globals,       core_globals)
 
249
        STD_PHP_INI_BOOLEAN("html_errors",                      "1",            PHP_INI_ALL,            OnUpdateBool,                   html_errors,                    php_core_globals,       core_globals)
 
250
        STD_PHP_INI_BOOLEAN("xmlrpc_errors",            "0",            PHP_INI_SYSTEM,         OnUpdateBool,                   xmlrpc_errors,                  php_core_globals,       core_globals)
 
251
        STD_PHP_INI_ENTRY("xmlrpc_error_number",        "0",            PHP_INI_ALL,            OnUpdateLong,                   xmlrpc_error_number,    php_core_globals,       core_globals)
 
252
        STD_PHP_INI_ENTRY("max_input_time",                     "-1",   PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLong,                   max_input_time, php_core_globals,       core_globals)
 
253
        STD_PHP_INI_BOOLEAN("ignore_user_abort",        "0",            PHP_INI_ALL,            OnUpdateBool,                   ignore_user_abort,              php_core_globals,       core_globals)
 
254
        STD_PHP_INI_BOOLEAN("implicit_flush",           "0",            PHP_INI_ALL,            OnUpdateBool,                   implicit_flush,                 php_core_globals,       core_globals)
 
255
        STD_PHP_INI_BOOLEAN("log_errors",                       "0",            PHP_INI_ALL,            OnUpdateBool,                   log_errors,                             php_core_globals,       core_globals)
 
256
        STD_PHP_INI_ENTRY("log_errors_max_len",  "1024",                PHP_INI_ALL,            OnUpdateLong,                   log_errors_max_len,             php_core_globals,       core_globals)
 
257
        STD_PHP_INI_BOOLEAN("ignore_repeated_errors",   "0",    PHP_INI_ALL,            OnUpdateBool,                   ignore_repeated_errors, php_core_globals,       core_globals)
 
258
        STD_PHP_INI_BOOLEAN("ignore_repeated_source",   "0",    PHP_INI_ALL,            OnUpdateBool,                   ignore_repeated_source, php_core_globals,       core_globals)
 
259
        STD_PHP_INI_BOOLEAN("report_memleaks",          "1",            PHP_INI_ALL,            OnUpdateBool,                   report_memleaks,                php_core_globals,       core_globals)
 
260
        STD_PHP_INI_BOOLEAN("report_zend_debug",        "1",            PHP_INI_ALL,            OnUpdateBool,                   report_zend_debug,              php_core_globals,       core_globals)
 
261
        STD_PHP_INI_BOOLEAN("magic_quotes_gpc",         "1",            PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   magic_quotes_gpc,               php_core_globals,       core_globals)
 
262
        STD_PHP_INI_BOOLEAN("magic_quotes_runtime",     "0",            PHP_INI_ALL,            OnUpdateBool,                   magic_quotes_runtime,   php_core_globals,       core_globals)
 
263
        STD_PHP_INI_BOOLEAN("magic_quotes_sybase",      "0",            PHP_INI_ALL,            OnUpdateBool,                   magic_quotes_sybase,    php_core_globals,       core_globals)
 
264
        STD_PHP_INI_ENTRY("output_buffering",           "0",            PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateLong,   output_buffering,               php_core_globals,       core_globals)
 
265
        STD_PHP_INI_ENTRY("output_handler",                     NULL,           PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateString, output_handler,         php_core_globals,       core_globals)
 
266
        STD_PHP_INI_BOOLEAN("register_argc_argv",       "1",            PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   register_argc_argv,             php_core_globals,       core_globals)
 
267
        STD_PHP_INI_BOOLEAN("register_globals",         "0",            PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   register_globals,               php_core_globals,       core_globals)
 
268
        STD_PHP_INI_BOOLEAN("register_long_arrays",     "1",            PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   register_long_arrays,   php_core_globals,       core_globals)
 
269
        STD_PHP_INI_BOOLEAN("auto_globals_jit",         "1",            PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool,   auto_globals_jit,       php_core_globals,       core_globals)
 
270
#if PHP_SAFE_MODE
 
271
        STD_PHP_INI_BOOLEAN("safe_mode",                        "1",            PHP_INI_SYSTEM,         OnUpdateBool,                   safe_mode,                              php_core_globals,       core_globals)
 
272
#else
 
273
        STD_PHP_INI_BOOLEAN("safe_mode",                        "0",            PHP_INI_SYSTEM,         OnUpdateBool,                   safe_mode,                              php_core_globals,       core_globals)
 
274
#endif
 
275
        STD_PHP_INI_ENTRY("safe_mode_include_dir",      NULL,           PHP_INI_SYSTEM,         OnUpdateString,                 safe_mode_include_dir,  php_core_globals,       core_globals)
 
276
        STD_PHP_INI_BOOLEAN("safe_mode_gid",            "0",            PHP_INI_SYSTEM,         OnUpdateBool,                   safe_mode_gid,                  php_core_globals,       core_globals)
 
277
        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)
 
278
        STD_PHP_INI_BOOLEAN("sql.safe_mode",            "0",            PHP_INI_SYSTEM,         OnUpdateBool,                   sql_safe_mode,                  php_core_globals,       core_globals)
 
279
        STD_PHP_INI_BOOLEAN("track_errors",                     "0",            PHP_INI_ALL,            OnUpdateBool,                   track_errors,                   php_core_globals,       core_globals)
 
280
        STD_PHP_INI_BOOLEAN("y2k_compliance",           "1",            PHP_INI_ALL,            OnUpdateBool,                   y2k_compliance,                 php_core_globals,       core_globals)
 
281
 
 
282
        STD_PHP_INI_ENTRY("unserialize_callback_func",  NULL,   PHP_INI_ALL,            OnUpdateString,                 unserialize_callback_func,      php_core_globals,       core_globals)
 
283
        STD_PHP_INI_ENTRY("serialize_precision",        "100",  PHP_INI_ALL,            OnUpdateLong,                   serialize_precision,    php_core_globals,       core_globals)
 
284
        STD_PHP_INI_ENTRY("arg_separator.output",       "&",            PHP_INI_ALL,            OnUpdateStringUnempty,  arg_separator.output,   php_core_globals,       core_globals)
 
285
        STD_PHP_INI_ENTRY("arg_separator.input",        "&",            PHP_INI_SYSTEM|PHP_INI_PERDIR,  OnUpdateStringUnempty,  arg_separator.input,    php_core_globals,       core_globals)
 
286
 
 
287
        STD_PHP_INI_ENTRY("auto_append_file",           NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString,                 auto_append_file,               php_core_globals,       core_globals)
 
288
        STD_PHP_INI_ENTRY("auto_prepend_file",          NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString,                 auto_prepend_file,              php_core_globals,       core_globals)
 
289
        STD_PHP_INI_ENTRY("doc_root",                           NULL,           PHP_INI_SYSTEM,         OnUpdateStringUnempty,  doc_root,                               php_core_globals,       core_globals)
 
290
        STD_PHP_INI_ENTRY("default_charset",            SAPI_DEFAULT_CHARSET,   PHP_INI_ALL,    OnUpdateString,                 default_charset,                sapi_globals_struct,sapi_globals)
 
291
        STD_PHP_INI_ENTRY("default_mimetype",           SAPI_DEFAULT_MIMETYPE,  PHP_INI_ALL,    OnUpdateString,                 default_mimetype,               sapi_globals_struct,sapi_globals)
 
292
        STD_PHP_INI_ENTRY("error_log",                          NULL,           PHP_INI_ALL,            OnUpdateString,                 error_log,                              php_core_globals,       core_globals)
 
293
        STD_PHP_INI_ENTRY("extension_dir",                      PHP_EXTENSION_DIR,              PHP_INI_SYSTEM,         OnUpdateStringUnempty,  extension_dir,                  php_core_globals,       core_globals)
 
294
        STD_PHP_INI_ENTRY("include_path",                       PHP_INCLUDE_PATH,               PHP_INI_ALL,            OnUpdateStringUnempty,  include_path,                   php_core_globals,       core_globals)
 
295
        PHP_INI_ENTRY("max_execution_time",                     "30",           PHP_INI_ALL,                    OnUpdateTimeout)
 
296
        STD_PHP_INI_ENTRY("open_basedir",                       NULL,           PHP_INI_SYSTEM,         OnUpdateString,                 open_basedir,                   php_core_globals,       core_globals)
 
297
        STD_PHP_INI_ENTRY("safe_mode_exec_dir",         PHP_SAFE_MODE_EXEC_DIR, PHP_INI_SYSTEM,         OnUpdateString,                 safe_mode_exec_dir,             php_core_globals,       core_globals)
 
298
 
 
299
        STD_PHP_INI_BOOLEAN("file_uploads",                     "1",            PHP_INI_SYSTEM,         OnUpdateBool,                   file_uploads,                   php_core_globals,       core_globals)
 
300
        STD_PHP_INI_ENTRY("upload_max_filesize",        "2M",           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLong,                   upload_max_filesize,    php_core_globals,       core_globals)
 
301
        STD_PHP_INI_ENTRY("post_max_size",                      "8M",           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateLong,                   post_max_size,                  sapi_globals_struct,sapi_globals)
 
302
        STD_PHP_INI_ENTRY("upload_tmp_dir",                     NULL,           PHP_INI_SYSTEM,         OnUpdateStringUnempty,  upload_tmp_dir,                 php_core_globals,       core_globals)
 
303
 
 
304
        STD_PHP_INI_ENTRY("user_dir",                           NULL,           PHP_INI_SYSTEM,         OnUpdateString,                 user_dir,                               php_core_globals,       core_globals)
 
305
        STD_PHP_INI_ENTRY("variables_order",            "EGPCS",        PHP_INI_ALL,            OnUpdateStringUnempty,  variables_order,                php_core_globals,       core_globals)
 
306
 
 
307
        STD_PHP_INI_ENTRY("error_append_string",        NULL,           PHP_INI_ALL,            OnUpdateString,                 error_append_string,    php_core_globals,       core_globals)
 
308
        STD_PHP_INI_ENTRY("error_prepend_string",       NULL,           PHP_INI_ALL,            OnUpdateString,                 error_prepend_string,   php_core_globals,       core_globals)
 
309
 
 
310
        PHP_INI_ENTRY("SMTP",                                           "localhost",PHP_INI_ALL,                NULL)
 
311
        PHP_INI_ENTRY("smtp_port",                                      "25",           PHP_INI_ALL,            NULL)
 
312
        PHP_INI_ENTRY("browscap",                                       NULL,           PHP_INI_SYSTEM,         NULL)
 
313
#if MEMORY_LIMIT
 
314
        PHP_INI_ENTRY("memory_limit",                           "8M",           PHP_INI_ALL,            OnChangeMemoryLimit)
 
315
#endif
 
316
        PHP_INI_ENTRY("precision",                                      "14",           PHP_INI_ALL,            OnSetPrecision)
 
317
        PHP_INI_ENTRY("sendmail_from",                          NULL,           PHP_INI_ALL,            NULL)
 
318
        PHP_INI_ENTRY("sendmail_path",  DEFAULT_SENDMAIL_PATH,  PHP_INI_SYSTEM,         NULL)
 
319
        PHP_INI_ENTRY("mail.force_extra_parameters",NULL,               PHP_INI_SYSTEM|PHP_INI_PERDIR,          NULL)
 
320
        PHP_INI_ENTRY("disable_functions",                      "",                     PHP_INI_SYSTEM,         NULL)
 
321
        PHP_INI_ENTRY("disable_classes",                        "",                     PHP_INI_SYSTEM,         NULL)
 
322
 
 
323
        STD_PHP_INI_BOOLEAN("allow_url_fopen",          "1",            PHP_INI_SYSTEM,         OnUpdateBool,                   allow_url_fopen,                        php_core_globals,       core_globals)
 
324
        STD_PHP_INI_BOOLEAN("always_populate_raw_post_data",            "0",            PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   always_populate_raw_post_data,                  php_core_globals,       core_globals)
 
325
 
 
326
PHP_INI_END()
 
327
/* }}} */
 
328
 
 
329
/* True globals (no need for thread safety */
 
330
/* But don't make them a single int bitfield */
 
331
static int module_initialized = 0;
 
332
static int module_startup = 1;
 
333
static int module_shutdown = 0;
 
334
 
 
335
/* {{{ php_log_err
 
336
 */
 
337
PHPAPI void php_log_err(char *log_message TSRMLS_DC)
 
338
{
 
339
        FILE *log_file;
 
340
        char error_time_str[128];
 
341
        struct tm tmbuf;
 
342
        time_t error_time;
 
343
 
 
344
        /* Try to use the specified logging location. */
 
345
        if (PG(error_log) != NULL) {
 
346
#ifdef HAVE_SYSLOG_H
 
347
                if (!strcmp(PG(error_log), "syslog")) {
 
348
                        php_syslog(LOG_NOTICE, "%.500s", log_message);
 
349
                        return;
 
350
                }
 
351
#endif
 
352
                log_file = VCWD_FOPEN(PG(error_log), "a");
 
353
                if (log_file != NULL) {
 
354
                        time(&error_time);
 
355
                        strftime(error_time_str, sizeof(error_time_str), "%d-%b-%Y %H:%M:%S", php_localtime_r(&error_time, &tmbuf)); 
 
356
                        fprintf(log_file, "[%s] ", error_time_str);
 
357
                        fprintf(log_file, "%s", log_message);
 
358
                        fprintf(log_file, "%s", PHP_EOL);
 
359
                        fclose(log_file);
 
360
                        return;
 
361
                }
 
362
        }
 
363
 
 
364
        /* Otherwise fall back to the default logging location, if we have one */
 
365
 
 
366
        if (sapi_module.log_message) {
 
367
                sapi_module.log_message(log_message);
 
368
        }
 
369
}
 
370
/* }}} */
 
371
 
 
372
/* {{{ php_write
 
373
   wrapper for modules to use PHPWRITE */
 
374
PHPAPI int php_write(void *buf, uint size TSRMLS_DC)
 
375
{
 
376
        return PHPWRITE(buf, size);
 
377
}
 
378
/* }}} */
 
379
 
 
380
/* {{{ php_printf
 
381
 */
 
382
PHPAPI int php_printf(const char *format, ...)
 
383
{
 
384
        va_list args;
 
385
        int ret;
 
386
        char *buffer;
 
387
        int size;
 
388
        TSRMLS_FETCH();
 
389
 
 
390
        va_start(args, format);
 
391
        size = vspprintf(&buffer, 0, format, args);
 
392
        ret = PHPWRITE(buffer, size);
 
393
        efree(buffer);
 
394
        va_end(args);
 
395
        
 
396
        return ret;
 
397
}
 
398
/* }}} */
 
399
 
 
400
/* {{{ php_verror helpers */
 
401
 
 
402
/* {{{ php_during_module_startup */
 
403
static int php_during_module_startup()
 
404
{
 
405
        return module_startup;
 
406
}
 
407
/* }}} */
 
408
 
 
409
/* {{{ php_during_module_shutdown */
 
410
static int php_during_module_shutdown()
 
411
{
 
412
        return module_shutdown;
 
413
}
 
414
/* }}} */
 
415
 
 
416
/* }}} */
 
417
 
 
418
/* {{{ php_verror */
 
419
/* php_verror is called from php_error_docref<n> functions.
 
420
 * Its purpose is to unify error messages and automatically generate clickable
 
421
 * html error messages if correcponding ini setting (html_errors) is activated.
 
422
 * See: CODING_STANDARDS for details.
 
423
 */
 
424
PHPAPI void php_verror(const char *docref, const char *params, int type, const char *format, va_list args TSRMLS_DC)
 
425
{
 
426
        char *buffer = NULL, *docref_buf = NULL, *target = NULL;
 
427
        char *docref_target = "", *docref_root = "";
 
428
        char *p;
 
429
        int buffer_len = 0;
 
430
        char *space;
 
431
        char *class_name = get_active_class_name(&space TSRMLS_CC);
 
432
        char *function;
 
433
        char *origin;
 
434
        char *message;
 
435
        int is_function = 0;
 
436
 
 
437
        /* get error text into buffer and escape for html if necessary */
 
438
        buffer_len = vspprintf(&buffer, 0, format, args);
 
439
        if (PG(html_errors)) {
 
440
                int len;
 
441
                char *replace = php_escape_html_entities(buffer, buffer_len, &len, 0, ENT_COMPAT, NULL TSRMLS_CC);
 
442
                efree(buffer);
 
443
                buffer = replace;
 
444
                buffer_len = len;
 
445
        }
 
446
 
 
447
        /* which function caused the problem if any at all */
 
448
        if (php_during_module_startup()) {
 
449
                function = "PHP Startup";
 
450
        } else if (php_during_module_shutdown()) {
 
451
                function = "PHP Shutdown";
 
452
        } else {
 
453
                function = get_active_function_name(TSRMLS_C);
 
454
                if (!function || !strlen(function)) {
 
455
                        function = "Unknown";
 
456
                } else {
 
457
                        is_function = 1;
 
458
                }
 
459
        }
 
460
 
 
461
        /* if we still have memory then format the origin */
 
462
        if (is_function) {
 
463
                spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);        
 
464
        } else {
 
465
                spprintf(&origin, 0, "%s", function);   
 
466
        }
 
467
 
 
468
        /* origin and buffer available, so lets come up with the error message */
 
469
        if (docref && docref[0] == '#') {
 
470
                docref_target = strchr(docref, '#');
 
471
                docref = NULL;
 
472
        }
 
473
 
 
474
        /* no docref given but function is known (the default) */
 
475
        if (!docref && is_function) {
 
476
                spprintf(&docref_buf, 0, "function.%s", function);
 
477
                while((p = strchr(docref_buf, '_')) != NULL) {
 
478
                        *p = '-';
 
479
                }
 
480
                docref = docref_buf;
 
481
        }
 
482
 
 
483
        /* we have a docref for a function AND
 
484
         * - we show erroes in html mode OR
 
485
         * - the user wants to see the links anyway
 
486
         */
 
487
        if (docref && is_function && (PG(html_errors) || strlen(PG(docref_root)))) {
 
488
                if (strncmp(docref, "http://", 7)) {
 
489
                        /* We don't have 'http://' so we use docref_root */
 
490
 
 
491
                        char *ref;  /* temp copy for duplicated docref */
 
492
 
 
493
                        docref_root = PG(docref_root);
 
494
 
 
495
                        ref = estrdup(docref);
 
496
                        if (docref_buf) {
 
497
                                efree(docref_buf);
 
498
                        }
 
499
                        docref_buf = ref;
 
500
                        /* strip of the target if any */
 
501
                        p = strrchr(ref, '#');
 
502
                        if (p) {
 
503
                                target = estrdup(p);
 
504
                                if (target) {
 
505
                                        docref_target = target;
 
506
                                        *p = '\0';
 
507
                                }
 
508
                        }
 
509
                        /* add the extension if it is set in ini */
 
510
                        if (PG(docref_ext) && strlen(PG(docref_ext))) {
 
511
                                spprintf(&docref_buf, 0, "%s%s", ref, PG(docref_ext));
 
512
                                efree(ref);
 
513
                        }
 
514
                        docref = docref_buf;
 
515
                }
 
516
                /* display html formatted or only show the additional links */
 
517
                if (PG(html_errors)) {
 
518
                        spprintf(&message, 0, "%s [<a href='%s%s%s'>%s</a>]: %s", origin, docref_root, docref, docref_target, docref, buffer);
 
519
                } else {
 
520
                        spprintf(&message, 0, "%s [%s%s%s]: %s", origin, docref_root, docref, docref_target, buffer);
 
521
                }
 
522
                if (target) {
 
523
                        efree(target);
 
524
                }
 
525
        } else {
 
526
                spprintf(&message, 0, "%s: %s", origin, buffer);
 
527
        }
 
528
        efree(origin);
 
529
        if (docref_buf) {
 
530
                efree(docref_buf);
 
531
        }
 
532
        php_error(type, "%s", message);
 
533
        efree(message);
 
534
 
 
535
        if (PG(track_errors) && module_initialized && EG(active_symbol_table)) {
 
536
                zval *tmp;
 
537
                ALLOC_INIT_ZVAL(tmp);
 
538
                ZVAL_STRINGL(tmp, buffer, buffer_len, 1);
 
539
                zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) &tmp, sizeof(pval *), NULL);
 
540
        }
 
541
        efree(buffer);
 
542
}
 
543
/* }}} */
 
544
 
 
545
/* {{{ php_error_docref0 */
 
546
/* See: CODING_STANDARDS for details. */
 
547
PHPAPI void php_error_docref0(const char *docref TSRMLS_DC, int type, const char *format, ...)
 
548
{
 
549
        va_list args;
 
550
        
 
551
        va_start(args, format);
 
552
        php_verror(docref, "", type, format, args TSRMLS_CC);
 
553
        va_end(args);
 
554
}
 
555
/* }}} */
 
556
 
 
557
/* {{{ php_error_docref1 */
 
558
/* See: CODING_STANDARDS for details. */
 
559
PHPAPI void php_error_docref1(const char *docref TSRMLS_DC, const char *param1, int type, const char *format, ...)
 
560
{
 
561
        va_list args;
 
562
        
 
563
        va_start(args, format);
 
564
        php_verror(docref, param1, type, format, args TSRMLS_CC);
 
565
        va_end(args);
 
566
}
 
567
/* }}} */
 
568
 
 
569
/* {{{ php_error_docref2 */
 
570
/* See: CODING_STANDARDS for details. */
 
571
PHPAPI void php_error_docref2(const char *docref TSRMLS_DC, const char *param1, const char *param2, int type, const char *format, ...)
 
572
{
 
573
        char *params;
 
574
        va_list args;
 
575
        
 
576
        spprintf(&params, 0, "%s,%s", param1, param2);
 
577
        va_start(args, format);
 
578
        php_verror(docref, params ? params : "...", type, format, args TSRMLS_CC);
 
579
        va_end(args);
 
580
        if (params) {
 
581
                efree(params);
 
582
        }
 
583
}
 
584
/* }}} */
 
585
 
 
586
/* {{{ php_html_puts */
 
587
PHPAPI void php_html_puts(const char *str, uint size TSRMLS_DC)
 
588
{
 
589
        zend_html_puts(str, size TSRMLS_CC);
 
590
}
 
591
/* }}} */
 
592
 
 
593
/* {{{ php_suppress_errors */
 
594
PHPAPI void php_set_error_handling(error_handling_t error_handling, zend_class_entry *exception_class TSRMLS_DC) {
 
595
        PG(error_handling) = error_handling;
 
596
        PG(exception_class) = exception_class;
 
597
        if (PG(last_error_message)) {
 
598
                free(PG(last_error_message));
 
599
                PG(last_error_message) = NULL;
 
600
        }
 
601
        if (PG(last_error_file)) {
 
602
                free(PG(last_error_file));
 
603
                PG(last_error_file) = NULL;
 
604
        }
 
605
        PG(last_error_lineno) = 0;
 
606
}
 
607
/* }}} */
 
608
 
 
609
/* {{{ php_error_cb
 
610
 extended error handling function */
 
611
static void php_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
 
612
{
 
613
        char *buffer;
 
614
        int buffer_len, display;
 
615
        TSRMLS_FETCH();
 
616
 
 
617
        buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args);
 
618
 
 
619
        /* check for repeated errors to be ignored */
 
620
        if (PG(ignore_repeated_errors) && PG(last_error_message)) {
 
621
                /* no check for PG(last_error_file) is needed since it cannot
 
622
                 * be NULL if PG(last_error_message) is not NULL */
 
623
                if (strcmp(PG(last_error_message), buffer)
 
624
                        || (!PG(ignore_repeated_source)
 
625
                                && ((PG(last_error_lineno) != error_lineno)
 
626
                                        || strcmp(PG(last_error_file), error_filename)))) {
 
627
                        display = 1;
 
628
                } else {
 
629
                        display = 0;
 
630
                }
 
631
        } else {
 
632
                display = 1;
 
633
        }
 
634
 
 
635
        /* store the error if it has changed */
 
636
        if (display) {
 
637
                if (PG(last_error_message)) {
 
638
                        free(PG(last_error_message));
 
639
                }
 
640
                if (PG(last_error_file)) {
 
641
                        free(PG(last_error_file));
 
642
                }
 
643
                PG(last_error_message) = strdup(buffer);
 
644
                PG(last_error_file) = strdup(error_filename);
 
645
                PG(last_error_lineno) = error_lineno;
 
646
        }
 
647
 
 
648
        /* according to error handling mode, suppress error, throw exception or show it */
 
649
        if (PG(error_handling) != EH_NORMAL) {
 
650
                switch (type) {
 
651
                        case E_CORE_ERROR:
 
652
                        case E_COMPILE_ERROR:
 
653
                        case E_PARSE:
 
654
                                /* fatal errors are real errors and cannot be made exceptions */
 
655
                                break;
 
656
                        case E_STRICT:
 
657
                                /* for the sake of BC to old damaged code */
 
658
                                break;
 
659
                        case E_NOTICE:
 
660
                        case E_USER_NOTICE:
 
661
                                /* notices are no errors and are not treated as such like E_WARNINGS */
 
662
                                break;
 
663
                        default:
 
664
                                /* throw an exception if we are in EH_THROW mode
 
665
                                 * but DO NOT overwrite a pending exception
 
666
                                 */
 
667
                                if (PG(error_handling) == EH_THROW && !EG(exception)) {
 
668
                                        zend_throw_exception(PG(exception_class), buffer, 0 TSRMLS_CC);
 
669
                                }
 
670
                                efree(buffer);
 
671
                                return;
 
672
                }
 
673
        }
 
674
 
 
675
        /* display/log the error if necessary */
 
676
        if (display && (EG(error_reporting) & type || (type & E_CORE))
 
677
                && (PG(log_errors) || PG(display_errors) || (!module_initialized))) {
 
678
                char *error_type_str;
 
679
 
 
680
                switch (type) {
 
681
                        case E_ERROR:
 
682
                        case E_CORE_ERROR:
 
683
                        case E_COMPILE_ERROR:
 
684
                        case E_USER_ERROR:
 
685
                                error_type_str = "Fatal error";
 
686
                                break;
 
687
                        case E_WARNING:
 
688
                        case E_CORE_WARNING:
 
689
                        case E_COMPILE_WARNING:
 
690
                        case E_USER_WARNING:
 
691
                                error_type_str = "Warning";
 
692
                                break;
 
693
                        case E_PARSE:
 
694
                                error_type_str = "Parse error";
 
695
                                break;
 
696
                        case E_NOTICE:
 
697
                        case E_USER_NOTICE:
 
698
                                error_type_str = "Notice";
 
699
                                break;
 
700
                        case E_STRICT:
 
701
                                error_type_str = "Strict Standards";
 
702
                                break;
 
703
                        default:
 
704
                                error_type_str = "Unknown error";
 
705
                                break;
 
706
                }
 
707
 
 
708
                if (!module_initialized || PG(log_errors)) {
 
709
                        char *log_buffer;
 
710
 
 
711
#ifdef PHP_WIN32
 
712
                        if (type==E_CORE_ERROR || type==E_CORE_WARNING) {
 
713
                                MessageBox(NULL, buffer, error_type_str, MB_OK|ZEND_SERVICE_MB_STYLE);
 
714
                        }
 
715
#endif
 
716
                        spprintf(&log_buffer, 0, "PHP %s:  %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno);
 
717
                        php_log_err(log_buffer TSRMLS_CC);
 
718
                        efree(log_buffer);
 
719
                }
 
720
                if (PG(display_errors)
 
721
                        && ((module_initialized && !PG(during_request_startup))
 
722
                                || (PG(display_startup_errors) 
 
723
                                        && (OG(php_body_write)==php_default_output_func || OG(php_body_write)==php_ub_body_write_no_header || OG(php_body_write)==php_ub_body_write)
 
724
                                        )
 
725
                                )
 
726
                        ) {
 
727
 
 
728
                        if (PG(xmlrpc_errors)) {
 
729
                                php_printf("<?xml version=\"1.0\"?><methodResponse><fault><value><struct><member><name>faultCode</name><value><int>%ld</int></value></member><member><name>faultString</name><value><string>%s:%s in %s on line %d</string></value></member></struct></value></fault></methodResponse>", PG(xmlrpc_error_number), error_type_str, buffer, error_filename, error_lineno);
 
730
                        } else {
 
731
                                char *prepend_string = INI_STR("error_prepend_string");
 
732
                                char *append_string = INI_STR("error_append_string");
 
733
                                char *error_format = PG(html_errors) ?
 
734
                                        "%s<br />\n<b>%s</b>:  %s in <b>%s</b> on line <b>%d</b><br />\n%s"
 
735
                                        : "%s\n%s: %s in %s on line %d\n%s";    
 
736
                                php_printf(error_format, STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string));
 
737
                        }
 
738
                }
 
739
#if ZEND_DEBUG
 
740
                if (PG(report_zend_debug)) {
 
741
                        zend_bool trigger_break;
 
742
 
 
743
                        switch (type) {
 
744
                                case E_ERROR:
 
745
                                case E_CORE_ERROR:
 
746
                                case E_COMPILE_ERROR:
 
747
                                case E_USER_ERROR:
 
748
                                        trigger_break=1;
 
749
                                        break;
 
750
                                default:
 
751
                                        trigger_break=0;
 
752
                                        break;
 
753
                        }
 
754
                        zend_output_debug_string(trigger_break, "%s(%d) : %s - %s", error_filename, error_lineno, error_type_str, buffer);
 
755
                }
 
756
#endif
 
757
        }
 
758
 
 
759
        /* Bail out if we can't recover */
 
760
        switch (type) {
 
761
                case E_CORE_ERROR:
 
762
                        if(!module_initialized) {
 
763
                                /* bad error in module startup - no way we can live with this */
 
764
                                exit(-2);
 
765
                        }
 
766
                /* no break - intentionally */
 
767
                case E_ERROR:
 
768
                /* case E_PARSE: the parser would return 1 (failure), we can bail out nicely */
 
769
                case E_COMPILE_ERROR:
 
770
                case E_USER_ERROR:
 
771
                        EG(exit_status) = 255;
 
772
                        if (module_initialized) {
 
773
#if MEMORY_LIMIT
 
774
                                /* restore memory limit */
 
775
                                AG(memory_limit) = PG(memory_limit); 
 
776
#endif
 
777
                                efree(buffer);
 
778
                                zend_bailout();
 
779
                                return;
 
780
                        }
 
781
                        break;
 
782
        }
 
783
 
 
784
        /* Log if necessary */
 
785
        if (!display) {
 
786
                efree(buffer);
 
787
                return;
 
788
        }
 
789
        if (PG(track_errors) && module_initialized && EG(active_symbol_table)) {
 
790
                pval *tmp;
 
791
 
 
792
                ALLOC_ZVAL(tmp);
 
793
                INIT_PZVAL(tmp);
 
794
                Z_STRVAL_P(tmp) = (char *) estrndup(buffer, buffer_len);
 
795
                Z_STRLEN_P(tmp) = buffer_len;
 
796
                Z_TYPE_P(tmp) = IS_STRING;
 
797
                zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) & tmp, sizeof(pval *), NULL);
 
798
        }
 
799
        efree(buffer);
 
800
}
 
801
/* }}} */
 
802
 
 
803
/* {{{ proto bool set_time_limit(int seconds)
 
804
   Sets the maximum time a script can run */
 
805
PHP_FUNCTION(set_time_limit)
 
806
{
 
807
        zval **new_timeout;
 
808
 
 
809
        if (PG(safe_mode)) {
 
810
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot set time limit in safe mode");
 
811
                RETURN_FALSE;
 
812
        }
 
813
 
 
814
        if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &new_timeout) == FAILURE) {
 
815
                WRONG_PARAM_COUNT;
 
816
        }
 
817
 
 
818
        convert_to_string_ex(new_timeout);
 
819
        if (zend_alter_ini_entry("max_execution_time", sizeof("max_execution_time"), Z_STRVAL_PP(new_timeout), Z_STRLEN_PP(new_timeout), PHP_INI_USER, PHP_INI_STAGE_RUNTIME) == SUCCESS) {
 
820
                RETURN_TRUE;
 
821
        } else {
 
822
                RETURN_FALSE;
 
823
        }
 
824
}
 
825
/* }}} */
 
826
 
 
827
/* {{{ php_fopen_wrapper_for_zend
 
828
 */
 
829
static FILE *php_fopen_wrapper_for_zend(const char *filename, char **opened_path)
 
830
{
 
831
        TSRMLS_FETCH();
 
832
 
 
833
        return php_stream_open_wrapper_as_file((char *)filename, "rb", ENFORCE_SAFE_MODE|USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, opened_path);
 
834
}
 
835
/* }}} */
 
836
 
 
837
static void stream_closer_for_zend(void *handle TSRMLS_DC)
 
838
{
 
839
        php_stream_close((php_stream*)handle);
 
840
}
 
841
 
 
842
static int php_stream_open_for_zend(const char *filename, zend_file_handle *handle TSRMLS_DC)
 
843
{
 
844
        php_stream *stream;
 
845
 
 
846
        stream = php_stream_open_wrapper((char *)filename, "rb", ENFORCE_SAFE_MODE|USE_PATH|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, &handle->opened_path);
 
847
 
 
848
        if (stream) {
 
849
                handle->type = ZEND_HANDLE_STREAM;
 
850
                handle->filename = (char*)filename;
 
851
                handle->free_filename = 0;
 
852
                handle->handle.stream.handle = stream;
 
853
                handle->handle.stream.reader = (zend_stream_reader_t)_php_stream_read;
 
854
                handle->handle.stream.closer = stream_closer_for_zend;
 
855
                handle->handle.stream.interactive = 0;
 
856
 
 
857
                return SUCCESS;
 
858
        }
 
859
        return FAILURE;
 
860
}
 
861
 
 
862
 
 
863
/* {{{ php_get_configuration_directive_for_zend
 
864
 */
 
865
static int php_get_configuration_directive_for_zend(char *name, uint name_length, zval *contents)
 
866
{
 
867
        zval *retval = cfg_get_entry(name, name_length);
 
868
 
 
869
        if (retval) {
 
870
                *contents = *retval;
 
871
                return SUCCESS;
 
872
        } else {
 
873
                return FAILURE;
 
874
        }
 
875
}
 
876
/* }}} */
 
877
 
 
878
/* {{{ php_message_handler_for_zend
 
879
 */
 
880
static void php_message_handler_for_zend(long message, void *data)
 
881
{
 
882
        TSRMLS_FETCH();
 
883
 
 
884
        switch (message) {
 
885
                case ZMSG_FAILED_INCLUDE_FOPEN:
 
886
                        php_error_docref("function.include" TSRMLS_CC, E_WARNING, "Failed opening '%s' for inclusion (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
 
887
                        break;
 
888
                case ZMSG_FAILED_REQUIRE_FOPEN:
 
889
                        php_error_docref("function.require" TSRMLS_CC, E_COMPILE_ERROR, "Failed opening required '%s' (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
 
890
                        break;
 
891
                case ZMSG_FAILED_HIGHLIGHT_FOPEN:
 
892
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed opening '%s' for highlighting", php_strip_url_passwd((char *) data));
 
893
                        break;
 
894
                case ZMSG_MEMORY_LEAK_DETECTED:
 
895
                case ZMSG_MEMORY_LEAK_REPEATED:
 
896
#if ZEND_DEBUG
 
897
                        if (EG(error_reporting) & E_WARNING) {
 
898
                                char memory_leak_buf[512];
 
899
 
 
900
                                if (message==ZMSG_MEMORY_LEAK_DETECTED) {
 
901
                                        zend_mem_header *t = (zend_mem_header *) data;
 
902
                                        void *ptr = (void *)((char *)t+sizeof(zend_mem_header)+MEM_HEADER_PADDING);
 
903
 
 
904
                                        snprintf(memory_leak_buf, 512, "%s(%d) :  Freeing 0x%.8lX (%d bytes), script=%s\n", t->filename, t->lineno, (unsigned long)ptr, t->size, SAFE_FILENAME(SG(request_info).path_translated));
 
905
                                        if (t->orig_filename) {
 
906
                                                char relay_buf[512];
 
907
 
 
908
                                                snprintf(relay_buf, 512, "%s(%d) : Actual location (location was relayed)\n", t->orig_filename, t->orig_lineno);
 
909
                                                strcat(memory_leak_buf, relay_buf);
 
910
                                        }
 
911
                                } else {
 
912
                                        unsigned long leak_count = (unsigned long) data;
 
913
 
 
914
                                        snprintf(memory_leak_buf, 512, "Last leak repeated %ld time%s\n", leak_count, (leak_count>1?"s":""));
 
915
                                }
 
916
#       if defined(PHP_WIN32)
 
917
                                OutputDebugString(memory_leak_buf);
 
918
#       else
 
919
                                fprintf(stderr, "%s", memory_leak_buf);
 
920
#       endif
 
921
                        }
 
922
#endif
 
923
                        break;
 
924
                case ZMSG_MEMORY_LEAKS_GRAND_TOTAL:
 
925
#if ZEND_DEBUG
 
926
                        if (EG(error_reporting) & E_WARNING) {
 
927
                                char memory_leak_buf[512];
 
928
 
 
929
                                snprintf(memory_leak_buf, 512, "=== Total %d memory leaks detected ===\n", *((zend_uint *) data));
 
930
#       if defined(PHP_WIN32)
 
931
                                OutputDebugString(memory_leak_buf);
 
932
#       else
 
933
                                fprintf(stderr, "%s", memory_leak_buf);
 
934
#       endif
 
935
                        }
 
936
#endif
 
937
                        break;
 
938
                case ZMSG_LOG_SCRIPT_NAME: {
 
939
                                struct tm *ta, tmbuf;
 
940
                                time_t curtime;
 
941
                                char *datetime_str, asctimebuf[52];
 
942
 
 
943
                                time(&curtime);
 
944
                                ta = php_localtime_r(&curtime, &tmbuf);
 
945
                                datetime_str = php_asctime_r(ta, asctimebuf);
 
946
                                datetime_str[strlen(datetime_str)-1]=0; /* get rid of the trailing newline */
 
947
                                fprintf(stderr, "[%s]  Script:  '%s'\n", datetime_str, SAFE_FILENAME(SG(request_info).path_translated));
 
948
                        }
 
949
                        break;
 
950
        }
 
951
}
 
952
/* }}} */
 
953
 
 
954
 
 
955
void php_on_timeout(int seconds TSRMLS_DC)
 
956
{
 
957
        PG(connection_status) |= PHP_CONNECTION_TIMEOUT;
 
958
        zend_set_timeout(EG(timeout_seconds));
 
959
}
 
960
 
 
961
#if PHP_SIGCHILD
 
962
/* {{{ sigchld_handler
 
963
 */
 
964
static void sigchld_handler(int apar)
 
965
{
 
966
        while (waitpid(-1, NULL, WNOHANG) > 0);
 
967
        signal(SIGCHLD, sigchld_handler);
 
968
}
 
969
/* }}} */
 
970
#endif
 
971
 
 
972
/* {{{ php_start_sapi()
 
973
 */
 
974
static int php_start_sapi(TSRMLS_D)
 
975
{
 
976
        int retval = SUCCESS;
 
977
 
 
978
        if(!SG(sapi_started)) {
 
979
                zend_try {
 
980
                        PG(during_request_startup) = 1;
 
981
 
 
982
                        /* initialize global variables */
 
983
                        PG(modules_activated) = 0;
 
984
                        PG(header_is_being_sent) = 0;
 
985
                        PG(connection_status) = PHP_CONNECTION_NORMAL;
 
986
 
 
987
                        zend_activate(TSRMLS_C);
 
988
                        zend_set_timeout(EG(timeout_seconds));
 
989
                        zend_activate_modules(TSRMLS_C);
 
990
                        PG(modules_activated)=1;
 
991
                } zend_catch {
 
992
                        retval = FAILURE;
 
993
                } zend_end_try();
 
994
 
 
995
                SG(sapi_started) = 1;
 
996
        }
 
997
        return retval;
 
998
}
 
999
 
 
1000
/* }}} */
 
1001
 
 
1002
/* {{{ php_request_startup
 
1003
 */
 
1004
 #ifndef APACHE_HOOKS
 
1005
int php_request_startup(TSRMLS_D)
 
1006
{
 
1007
        int retval = SUCCESS;
 
1008
 
 
1009
#ifdef PHP_WIN32
 
1010
        CoInitialize(NULL);
 
1011
#endif
 
1012
 
 
1013
#if PHP_SIGCHILD
 
1014
        signal(SIGCHLD, sigchld_handler);
 
1015
#endif
 
1016
 
 
1017
        zend_try {
 
1018
                PG(during_request_startup) = 1;
 
1019
                
 
1020
                php_output_activate(TSRMLS_C);
 
1021
 
 
1022
                /* initialize global variables */
 
1023
                PG(modules_activated) = 0;
 
1024
                PG(header_is_being_sent) = 0;
 
1025
                PG(connection_status) = PHP_CONNECTION_NORMAL;
 
1026
                
 
1027
                zend_activate(TSRMLS_C);
 
1028
                sapi_activate(TSRMLS_C);
 
1029
 
 
1030
                if (PG(max_input_time) == -1) {
 
1031
                        zend_set_timeout(EG(timeout_seconds));
 
1032
                } else {
 
1033
                        zend_set_timeout(PG(max_input_time));
 
1034
                }
 
1035
 
 
1036
                if (PG(expose_php)) {
 
1037
                        sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1);
 
1038
                }
 
1039
 
 
1040
                if (PG(output_handler) && PG(output_handler)[0]) {
 
1041
                        php_start_ob_buffer_named(PG(output_handler), 0, 1 TSRMLS_CC);
 
1042
                } else if (PG(output_buffering)) {
 
1043
                        if (PG(output_buffering)>1) {
 
1044
                                php_start_ob_buffer(NULL, PG(output_buffering), 1 TSRMLS_CC);
 
1045
                        } else {
 
1046
                                php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC);
 
1047
                        }
 
1048
                } else if (PG(implicit_flush)) {
 
1049
                        php_start_implicit_flush(TSRMLS_C);
 
1050
                }
 
1051
 
 
1052
                /* We turn this off in php_execute_script() */
 
1053
                /* PG(during_request_startup) = 0; */
 
1054
 
 
1055
                php_hash_environment(TSRMLS_C);
 
1056
                zend_activate_modules(TSRMLS_C);
 
1057
                PG(modules_activated)=1;
 
1058
        } zend_catch {
 
1059
                retval = FAILURE;
 
1060
        } zend_end_try();
 
1061
 
 
1062
        return retval;
 
1063
}
 
1064
# else
 
1065
int php_request_startup(TSRMLS_D)
 
1066
{   
 
1067
        int retval = SUCCESS;
 
1068
 
 
1069
#if PHP_SIGCHILD
 
1070
        signal(SIGCHLD, sigchld_handler);
 
1071
#endif
 
1072
 
 
1073
        if (php_start_sapi() == FAILURE) {
 
1074
                return FAILURE;
 
1075
        }
 
1076
        
 
1077
        php_output_activate(TSRMLS_C);
 
1078
        sapi_activate(TSRMLS_C);
 
1079
        php_hash_environment(TSRMLS_C);
 
1080
 
 
1081
        zend_try {
 
1082
                PG(during_request_startup) = 1;
 
1083
                php_output_activate(TSRMLS_C);
 
1084
                if (PG(expose_php)) {
 
1085
                        sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1);
 
1086
                }
 
1087
        } zend_catch {
 
1088
                retval = FAILURE;
 
1089
        } zend_end_try();
 
1090
 
 
1091
        return retval;
 
1092
}
 
1093
# endif
 
1094
/* }}} */
 
1095
 
 
1096
/* {{{ php_request_startup_for_hook
 
1097
 */
 
1098
int php_request_startup_for_hook(TSRMLS_D)
 
1099
{
 
1100
        int retval = SUCCESS;
 
1101
 
 
1102
#if PHP_SIGCHLD
 
1103
        signal(SIGCHLD, sigchld_handler);
 
1104
#endif
 
1105
 
 
1106
        if (php_start_sapi(TSRMLS_C) == FAILURE) {
 
1107
                return FAILURE;
 
1108
        }
 
1109
        
 
1110
        php_output_activate(TSRMLS_C);
 
1111
        sapi_activate_headers_only(TSRMLS_C);
 
1112
        php_hash_environment(TSRMLS_C);
 
1113
 
 
1114
        return retval;
 
1115
}
 
1116
/* }}} */
 
1117
 
 
1118
/* {{{ php_request_shutdown_for_exec
 
1119
 */
 
1120
void php_request_shutdown_for_exec(void *dummy)
 
1121
{
 
1122
        TSRMLS_FETCH();
 
1123
 
 
1124
        /* used to close fd's in the 3..255 range here, but it's problematic
 
1125
         */
 
1126
        shutdown_memory_manager(1, 1 TSRMLS_CC);
 
1127
}
 
1128
/* }}} */
 
1129
 
 
1130
/* {{{ php_request_shutdown_for_hook
 
1131
 */
 
1132
void php_request_shutdown_for_hook(void *dummy)
 
1133
{
 
1134
        TSRMLS_FETCH();
 
1135
        if (PG(modules_activated)) zend_try {
 
1136
                php_call_shutdown_functions(TSRMLS_C);
 
1137
        } zend_end_try();
 
1138
 
 
1139
        if (PG(modules_activated)) {
 
1140
                zend_deactivate_modules(TSRMLS_C);
 
1141
                php_free_shutdown_functions(TSRMLS_C);
 
1142
        }
 
1143
 
 
1144
        zend_try {
 
1145
                int i;
 
1146
 
 
1147
                for (i = 0; i < NUM_TRACK_VARS; i++) {
 
1148
                        if (PG(http_globals)[i]) {
 
1149
                                zval_ptr_dtor(&PG(http_globals)[i]);
 
1150
                        }
 
1151
                }
 
1152
        } zend_end_try();
 
1153
 
 
1154
        zend_deactivate(TSRMLS_C);
 
1155
 
 
1156
        zend_try {
 
1157
                sapi_deactivate(TSRMLS_C);
 
1158
        } zend_end_try();
 
1159
 
 
1160
        zend_try {
 
1161
                php_shutdown_stream_hashes(TSRMLS_C);
 
1162
        } zend_end_try();
 
1163
 
 
1164
        zend_try {
 
1165
                shutdown_memory_manager(CG(unclean_shutdown), 0 TSRMLS_CC);
 
1166
        } zend_end_try();
 
1167
 
 
1168
        zend_try {
 
1169
                zend_unset_timeout(TSRMLS_C);
 
1170
        } zend_end_try();
 
1171
}
 
1172
 
 
1173
/* }}} */
 
1174
 
 
1175
/* {{{ php_request_shutdown
 
1176
 */
 
1177
void php_request_shutdown(void *dummy)
 
1178
{
 
1179
        zend_bool report_memleaks;
 
1180
        TSRMLS_FETCH();
 
1181
 
 
1182
        report_memleaks = PG(report_memleaks);
 
1183
        /* EG(opline_ptr) points into nirvana and therefore cannot be safely accessed
 
1184
         * inside zend_executor callback functions.
 
1185
         */
 
1186
        EG(opline_ptr) = NULL;
 
1187
        EG(active_op_array) = NULL;
 
1188
 
 
1189
        zend_try {
 
1190
                php_end_ob_buffers((zend_bool)(SG(request_info).headers_only?0:1) TSRMLS_CC);
 
1191
        } zend_end_try();
 
1192
 
 
1193
        zend_try {
 
1194
                sapi_send_headers(TSRMLS_C);
 
1195
        } zend_end_try();
 
1196
 
 
1197
        zend_try {
 
1198
                zend_call_destructors(TSRMLS_C);
 
1199
        } zend_end_try();
 
1200
 
 
1201
        if (PG(modules_activated)) zend_try {
 
1202
                php_call_shutdown_functions(TSRMLS_C);
 
1203
        } zend_end_try();
 
1204
        
 
1205
        if (PG(modules_activated)) {
 
1206
                zend_deactivate_modules(TSRMLS_C);
 
1207
                php_free_shutdown_functions(TSRMLS_C);
 
1208
        }
 
1209
 
 
1210
        zend_try {
 
1211
                int i;
 
1212
 
 
1213
                for (i=0; i<NUM_TRACK_VARS; i++) {
 
1214
                        if (PG(http_globals)[i]) {
 
1215
                                zval_ptr_dtor(&PG(http_globals)[i]);
 
1216
                        }
 
1217
                }
 
1218
        } zend_end_try();
 
1219
 
 
1220
                
 
1221
        zend_deactivate(TSRMLS_C);
 
1222
 
 
1223
        zend_try {
 
1224
                zend_post_deactivate_modules(TSRMLS_C);
 
1225
        } zend_end_try();
 
1226
 
 
1227
        zend_try {
 
1228
                sapi_deactivate(TSRMLS_C);
 
1229
        } zend_end_try();
 
1230
 
 
1231
        zend_try {
 
1232
                php_shutdown_stream_hashes(TSRMLS_C);
 
1233
        } zend_end_try();
 
1234
 
 
1235
        zend_try {
 
1236
                shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0 TSRMLS_CC);
 
1237
        } zend_end_try();
 
1238
 
 
1239
        zend_try { 
 
1240
                zend_unset_timeout(TSRMLS_C);
 
1241
        } zend_end_try();
 
1242
 
 
1243
#ifdef PHP_WIN32
 
1244
        CoUninitialize();
 
1245
#endif
 
1246
}
 
1247
/* }}} */
 
1248
 
 
1249
 
 
1250
/* {{{ php_body_write_wrapper
 
1251
 */
 
1252
static int php_body_write_wrapper(const char *str, uint str_length)
 
1253
{
 
1254
        TSRMLS_FETCH();
 
1255
        return php_body_write(str, str_length TSRMLS_CC);
 
1256
}
 
1257
/* }}} */
 
1258
 
 
1259
#ifdef ZTS
 
1260
/* {{{ core_globals_ctor
 
1261
 */
 
1262
static void core_globals_ctor(php_core_globals *core_globals TSRMLS_DC)
 
1263
{
 
1264
        memset(core_globals, 0, sizeof(*core_globals));
 
1265
}
 
1266
/* }}} */
 
1267
#endif
 
1268
 
 
1269
/* {{{ php_startup_extensions
 
1270
 */
 
1271
int php_startup_extensions(zend_module_entry **ptr, int count)
 
1272
{
 
1273
        zend_module_entry **end = ptr+count;
 
1274
 
 
1275
        while (ptr < end) {
 
1276
                if (*ptr) {
 
1277
                        if (zend_startup_module(*ptr)==FAILURE) {
 
1278
                                return FAILURE;
 
1279
                        }
 
1280
                }
 
1281
                ptr++;
 
1282
        }
 
1283
        return SUCCESS;
 
1284
}
 
1285
/* }}} */
 
1286
 
 
1287
 
 
1288
/* {{{ php_module_startup
 
1289
 */
 
1290
int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules)
 
1291
{
 
1292
        zend_utility_functions zuf;
 
1293
        zend_utility_values zuv;
 
1294
        int module_number=0;    /* for REGISTER_INI_ENTRIES() */
 
1295
        char *php_os;
 
1296
#ifdef ZTS
 
1297
        zend_executor_globals *executor_globals;
 
1298
        void ***tsrm_ls;
 
1299
 
 
1300
        php_core_globals *core_globals;
 
1301
#endif
 
1302
#if defined(PHP_WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
 
1303
        WORD wVersionRequested = MAKEWORD(2, 0);
 
1304
        WSADATA wsaData;
 
1305
#endif
 
1306
#ifdef PHP_WIN32
 
1307
        {
 
1308
                DWORD dwVersion = GetVersion();
 
1309
 
 
1310
                /* Get build numbers for Windows NT or Win95 */
 
1311
                if (dwVersion < 0x80000000){
 
1312
                        php_os="WINNT";
 
1313
                } else {
 
1314
                        php_os="WIN32";
 
1315
                }
 
1316
        }
 
1317
#else
 
1318
        php_os=PHP_OS;
 
1319
#endif
 
1320
 
 
1321
#ifdef ZTS
 
1322
        tsrm_ls = ts_resource(0);
 
1323
#endif
 
1324
 
 
1325
        module_shutdown = 0;
 
1326
        module_startup = 1;
 
1327
        sapi_initialize_empty_request(TSRMLS_C);
 
1328
        sapi_activate(TSRMLS_C);
 
1329
 
 
1330
        if (module_initialized) {
 
1331
                return SUCCESS;
 
1332
        }
 
1333
 
 
1334
        sapi_module = *sf;
 
1335
 
 
1336
        php_output_startup();
 
1337
 
 
1338
        zuf.error_function = php_error_cb;
 
1339
        zuf.printf_function = php_printf;
 
1340
        zuf.write_function = php_body_write_wrapper;
 
1341
        zuf.fopen_function = php_fopen_wrapper_for_zend;
 
1342
        zuf.message_handler = php_message_handler_for_zend;
 
1343
        zuf.block_interruptions = sapi_module.block_interruptions;
 
1344
        zuf.unblock_interruptions = sapi_module.unblock_interruptions;
 
1345
        zuf.get_configuration_directive = php_get_configuration_directive_for_zend;
 
1346
        zuf.ticks_function = php_run_ticks;
 
1347
        zuf.on_timeout = php_on_timeout;
 
1348
        zuf.stream_open_function = php_stream_open_for_zend;
 
1349
        zuf.vspprintf_function = vspprintf;
 
1350
        zend_startup(&zuf, NULL, 1);
 
1351
 
 
1352
#ifdef ZTS
 
1353
        executor_globals = ts_resource(executor_globals_id);
 
1354
        ts_allocate_id(&core_globals_id, sizeof(php_core_globals), (ts_allocate_ctor) core_globals_ctor, NULL);
 
1355
        core_globals = ts_resource(core_globals_id);
 
1356
#endif
 
1357
        EG(bailout_set) = 0;
 
1358
        EG(error_reporting) = E_ALL & ~E_NOTICE;
 
1359
        
 
1360
        PG(header_is_being_sent) = 0;
 
1361
        SG(request_info).headers_only = 0;
 
1362
        SG(request_info).argv0 = NULL;
 
1363
        SG(request_info).argc=0;
 
1364
        SG(request_info).argv=(char **)NULL;
 
1365
        PG(connection_status) = PHP_CONNECTION_NORMAL;
 
1366
        PG(during_request_startup) = 0;
 
1367
        PG(last_error_message) = NULL;
 
1368
        PG(last_error_file) = NULL;
 
1369
        PG(last_error_lineno) = 0;
 
1370
        PG(error_handling) = EH_NORMAL;
 
1371
        PG(disable_functions) = NULL;
 
1372
        PG(disable_classes) = NULL;
 
1373
 
 
1374
#if HAVE_SETLOCALE
 
1375
        setlocale(LC_CTYPE, "");
 
1376
#endif
 
1377
 
 
1378
#if HAVE_TZSET
 
1379
        tzset();
 
1380
#endif
 
1381
 
 
1382
#if defined(PHP_WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
 
1383
        /* start up winsock services */
 
1384
        if (WSAStartup(wVersionRequested, &wsaData) != 0) {
 
1385
                php_printf("\nwinsock.dll unusable. %d\n", WSAGetLastError());
 
1386
                return FAILURE;
 
1387
        }
 
1388
#endif
 
1389
 
 
1390
        le_index_ptr = zend_register_list_destructors_ex(NULL, NULL, "index pointer", 0);
 
1391
 
 
1392
 
 
1393
        /* this will read in php.ini, set up the configuration parameters,
 
1394
           load zend extensions and register php function extensions 
 
1395
           to be loaded later */
 
1396
        if (php_init_config() == FAILURE) {
 
1397
                return FAILURE;
 
1398
        }
 
1399
 
 
1400
        REGISTER_INI_ENTRIES();
 
1401
        zend_register_standard_ini_entries(TSRMLS_C);
 
1402
 
 
1403
        /* initialize stream wrappers registry
 
1404
         * (this uses configuration parameters from php.ini)
 
1405
         */
 
1406
        if (php_init_stream_wrappers(module_number TSRMLS_CC) == FAILURE)       {
 
1407
                php_printf("PHP:  Unable to initialize stream url wrappers.\n");
 
1408
                return FAILURE;
 
1409
        }
 
1410
        
 
1411
        /* initialize registry for images to be used in phpinfo() 
 
1412
           (this uses configuration parameters from php.ini)
 
1413
         */
 
1414
        if (php_init_info_logos() == FAILURE) {
 
1415
                php_printf("PHP:  Unable to initialize info phpinfo logos.\n");
 
1416
                return FAILURE;
 
1417
        }
 
1418
 
 
1419
        zuv.html_errors = 1;
 
1420
        zuv.import_use_extension = ".php";
 
1421
        php_startup_auto_globals(TSRMLS_C);
 
1422
        zend_set_utility_values(&zuv);
 
1423
        php_startup_sapi_content_types();
 
1424
 
 
1425
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_VERSION", PHP_VERSION, sizeof(PHP_VERSION)-1, CONST_PERSISTENT | CONST_CS);
 
1426
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS", php_os, strlen(php_os), CONST_PERSISTENT | CONST_CS);
 
1427
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_SAPI", sapi_module.name, strlen(sapi_module.name), CONST_PERSISTENT | CONST_CS);
 
1428
        REGISTER_MAIN_STRINGL_CONSTANT("DEFAULT_INCLUDE_PATH", PHP_INCLUDE_PATH, sizeof(PHP_INCLUDE_PATH)-1, CONST_PERSISTENT | CONST_CS);
 
1429
        REGISTER_MAIN_STRINGL_CONSTANT("PEAR_INSTALL_DIR", PEAR_INSTALLDIR, sizeof(PEAR_INSTALLDIR)-1, CONST_PERSISTENT | CONST_CS);
 
1430
        REGISTER_MAIN_STRINGL_CONSTANT("PEAR_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
 
1431
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
 
1432
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_PREFIX", PHP_PREFIX, sizeof(PHP_PREFIX)-1, CONST_PERSISTENT | CONST_CS);
 
1433
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINDIR", PHP_BINDIR, sizeof(PHP_BINDIR)-1, CONST_PERSISTENT | CONST_CS);
 
1434
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_LIBDIR", PHP_LIBDIR, sizeof(PHP_LIBDIR)-1, CONST_PERSISTENT | CONST_CS);
 
1435
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_DATADIR", PHP_DATADIR, sizeof(PHP_DATADIR)-1, CONST_PERSISTENT | CONST_CS);
 
1436
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_SYSCONFDIR", PHP_SYSCONFDIR, sizeof(PHP_SYSCONFDIR)-1, CONST_PERSISTENT | CONST_CS);
 
1437
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_LOCALSTATEDIR", PHP_LOCALSTATEDIR, sizeof(PHP_LOCALSTATEDIR)-1, CONST_PERSISTENT | CONST_CS);
 
1438
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_PATH", PHP_CONFIG_FILE_PATH, sizeof(PHP_CONFIG_FILE_PATH)-1, CONST_PERSISTENT | CONST_CS);
 
1439
        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);
 
1440
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
 
1441
        REGISTER_MAIN_STRINGL_CONSTANT("PHP_EOL", PHP_EOL, sizeof(PHP_EOL)-1, CONST_PERSISTENT | CONST_CS);
 
1442
        REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", LONG_MAX, CONST_PERSISTENT | CONST_CS);
 
1443
        REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", sizeof(long), CONST_PERSISTENT | CONST_CS);
 
1444
        php_output_register_constants(TSRMLS_C);
 
1445
        php_rfc1867_register_constants(TSRMLS_C);
 
1446
 
 
1447
        if (php_startup_ticks(TSRMLS_C) == FAILURE) {
 
1448
                php_printf("Unable to start PHP ticks\n");
 
1449
                return FAILURE;
 
1450
        }
 
1451
 
 
1452
        /* Register internal Zend classes */
 
1453
        zend_register_default_classes(TSRMLS_C);
 
1454
 
 
1455
        /* startup extensions staticly compiled in */
 
1456
        if (php_startup_internal_extensions() == FAILURE) {
 
1457
                php_printf("Unable to start builtin modules\n");
 
1458
                return FAILURE;
 
1459
        }
 
1460
 
 
1461
        /* start additional PHP extensions */
 
1462
        php_startup_extensions(&additional_modules, num_additional_modules);
 
1463
 
 
1464
 
 
1465
        /* load and startup extensions compiled as shared objects (aka DLLs)
 
1466
           as requested by php.ini entries
 
1467
           theese are loaded after initialization of internal extensions
 
1468
           as extensions *might* rely on things from ext/standard
 
1469
           which is always an internal extension and to be initialized
 
1470
           ahead of all other internals
 
1471
         */
 
1472
        php_ini_delayed_modules_startup(TSRMLS_C);
 
1473
 
 
1474
        /* disable certain classes and functions as requested by php.ini */
 
1475
        php_disable_functions(TSRMLS_C);
 
1476
        php_disable_classes(TSRMLS_C);
 
1477
 
 
1478
        /* start Zend extensions */
 
1479
        zend_startup_extensions();
 
1480
 
 
1481
#ifdef ZTS
 
1482
        zend_post_startup(TSRMLS_C);
 
1483
#endif
 
1484
 
 
1485
        module_initialized = 1;
 
1486
        sapi_deactivate(TSRMLS_C);
 
1487
        module_startup = 0;
 
1488
 
 
1489
        /* we're done */
 
1490
        return SUCCESS;
 
1491
}
 
1492
/* }}} */
 
1493
 
 
1494
void php_module_shutdown_for_exec()
 
1495
{
 
1496
        /* used to close fd's in the range 3.255 here, but it's problematic */
 
1497
}
 
1498
 
 
1499
/* {{{ php_module_shutdown_wrapper
 
1500
 */
 
1501
int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals)
 
1502
{
 
1503
        TSRMLS_FETCH();
 
1504
        php_module_shutdown(TSRMLS_C);
 
1505
        return SUCCESS;
 
1506
}
 
1507
/* }}} */
 
1508
 
 
1509
/* {{{ php_module_shutdown
 
1510
 */
 
1511
void php_module_shutdown(TSRMLS_D)
 
1512
{
 
1513
        int module_number=0;    /* for UNREGISTER_INI_ENTRIES() */
 
1514
 
 
1515
        module_shutdown = 1;
 
1516
 
 
1517
        if (!module_initialized) {
 
1518
                return;
 
1519
        }
 
1520
 
 
1521
#if defined(PHP_WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
 
1522
        /*close winsock */
 
1523
        WSACleanup();
 
1524
#endif
 
1525
 
 
1526
        php_shutdown_ticks(TSRMLS_C);
 
1527
        sapi_flush(TSRMLS_C);
 
1528
 
 
1529
        zend_shutdown(TSRMLS_C);
 
1530
 
 
1531
        php_shutdown_stream_wrappers(module_number TSRMLS_CC);
 
1532
 
 
1533
        php_shutdown_info_logos();
 
1534
        UNREGISTER_INI_ENTRIES();
 
1535
 
 
1536
        /* close down the ini config */
 
1537
        php_shutdown_config();
 
1538
 
 
1539
#ifndef ZTS
 
1540
        zend_ini_shutdown(TSRMLS_C);
 
1541
        shutdown_memory_manager(CG(unclean_shutdown), 1 TSRMLS_CC);
 
1542
#else
 
1543
        zend_ini_global_shutdown(TSRMLS_C);
 
1544
#endif
 
1545
 
 
1546
        module_initialized = 0;
 
1547
        if (PG(last_error_message)) {
 
1548
                free(PG(last_error_message));
 
1549
        }
 
1550
        if (PG(last_error_file)) {
 
1551
                free(PG(last_error_file));
 
1552
        }
 
1553
        if (PG(disable_functions)) {
 
1554
                free(PG(disable_functions));
 
1555
        }
 
1556
        if (PG(disable_classes)) {
 
1557
                free(PG(disable_classes));
 
1558
        }
 
1559
}
 
1560
/* }}} */
 
1561
 
 
1562
 
 
1563
/* {{{ php_execute_script
 
1564
 */
 
1565
PHPAPI int php_execute_script(zend_file_handle *primary_file TSRMLS_DC)
 
1566
{
 
1567
        zend_file_handle *prepend_file_p, *append_file_p;
 
1568
        zend_file_handle prepend_file = {0}, append_file = {0};
 
1569
#if HAVE_BROKEN_GETCWD 
 
1570
        int old_cwd_fd = -1;
 
1571
#else
 
1572
        char *old_cwd;
 
1573
#endif
 
1574
        char *old_primary_file_path = NULL;
 
1575
        int retval = 0;
 
1576
 
 
1577
        EG(exit_status) = 0;
 
1578
        if (php_handle_special_queries(TSRMLS_C)) {
 
1579
                zend_file_handle_dtor(primary_file);
 
1580
                return 0;
 
1581
        }
 
1582
#ifndef HAVE_BROKEN_GETCWD
 
1583
# define OLD_CWD_SIZE 4096
 
1584
        old_cwd = do_alloca(OLD_CWD_SIZE);
 
1585
        old_cwd[0] = '\0';
 
1586
#endif
 
1587
 
 
1588
        zend_try {
 
1589
#ifdef PHP_WIN32
 
1590
                UpdateIniFromRegistry(primary_file->filename TSRMLS_CC);
 
1591
#endif
 
1592
 
 
1593
                PG(during_request_startup) = 0;
 
1594
 
 
1595
                if (primary_file->type == ZEND_HANDLE_FILENAME 
 
1596
                                && primary_file->filename) {
 
1597
#if HAVE_BROKEN_GETCWD
 
1598
                        /* this looks nasty to me */
 
1599
                        old_cwd_fd = open(".", 0);
 
1600
#else
 
1601
                        VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1);
 
1602
#endif
 
1603
                        VCWD_CHDIR_FILE(primary_file->filename);
 
1604
                }
 
1605
 
 
1606
                if (primary_file->filename) {                   
 
1607
                        char realfile[MAXPATHLEN];
 
1608
                        int realfile_len;
 
1609
                        int dummy = 1;
 
1610
                        if (VCWD_REALPATH(primary_file->filename, realfile)) {
 
1611
                                realfile_len =  strlen(realfile);
 
1612
                                zend_hash_add(&EG(included_files), realfile, realfile_len+1, (void *)&dummy, sizeof(int), NULL);
 
1613
                                if (strncmp(realfile, primary_file->filename, realfile_len)) {
 
1614
                                        old_primary_file_path = primary_file->filename;
 
1615
                                        primary_file->filename = realfile;
 
1616
                                }       
 
1617
                        }
 
1618
                }
 
1619
 
 
1620
                if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
 
1621
                        prepend_file.filename = PG(auto_prepend_file);
 
1622
                        prepend_file.opened_path = NULL;
 
1623
                        prepend_file.free_filename = 0;
 
1624
                        prepend_file.type = ZEND_HANDLE_FILENAME;
 
1625
                        prepend_file_p = &prepend_file;
 
1626
                } else {
 
1627
                        prepend_file_p = NULL;
 
1628
                }
 
1629
 
 
1630
                if (PG(auto_append_file) && PG(auto_append_file)[0]) {
 
1631
                        append_file.filename = PG(auto_append_file);
 
1632
                        append_file.opened_path = NULL;
 
1633
                        append_file.free_filename = 0;
 
1634
                        append_file.type = ZEND_HANDLE_FILENAME;
 
1635
                        append_file_p = &append_file;
 
1636
                } else {
 
1637
                        append_file_p = NULL;
 
1638
                }
 
1639
#ifdef PHP_WIN32
 
1640
                zend_unset_timeout(TSRMLS_C);
 
1641
#endif
 
1642
                zend_set_timeout(INI_INT("max_execution_time"));
 
1643
                retval = (zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
 
1644
                
 
1645
                if (old_primary_file_path) {
 
1646
                        primary_file->filename = old_primary_file_path;
 
1647
                }
 
1648
                
 
1649
        } zend_end_try();
 
1650
 
 
1651
#if HAVE_BROKEN_GETCWD
 
1652
        if (old_cwd_fd != -1) {
 
1653
                fchdir(old_cwd_fd);
 
1654
                close(old_cwd_fd);
 
1655
        }
 
1656
#else
 
1657
        if (old_cwd[0] != '\0') {
 
1658
                VCWD_CHDIR(old_cwd);
 
1659
        }
 
1660
        free_alloca(old_cwd);
 
1661
#endif
 
1662
        return retval;
 
1663
}
 
1664
/* }}} */
 
1665
 
 
1666
/* {{{ php_execute_simple_script
 
1667
 */
 
1668
PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval **ret TSRMLS_DC)
 
1669
{
 
1670
        char *old_cwd;
 
1671
 
 
1672
        EG(exit_status) = 0;
 
1673
#define OLD_CWD_SIZE 4096
 
1674
        old_cwd = do_alloca(OLD_CWD_SIZE);
 
1675
        old_cwd[0] = '\0';
 
1676
        
 
1677
        zend_try {
 
1678
#ifdef PHP_WIN32
 
1679
                UpdateIniFromRegistry(primary_file->filename TSRMLS_CC);
 
1680
#endif
 
1681
 
 
1682
                PG(during_request_startup) = 0;
 
1683
 
 
1684
                if (primary_file->type == ZEND_HANDLE_FILENAME && primary_file->filename) {
 
1685
                        VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1);
 
1686
                        VCWD_CHDIR_FILE(primary_file->filename);
 
1687
                }
 
1688
                zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, ret, 1, primary_file);
 
1689
        } zend_end_try();
 
1690
        
 
1691
        if (old_cwd[0] != '\0') {
 
1692
                VCWD_CHDIR(old_cwd);
 
1693
        }
 
1694
 
 
1695
        free_alloca(old_cwd);
 
1696
        return EG(exit_status);
 
1697
}
 
1698
/* }}} */
 
1699
 
 
1700
/* {{{ php_handle_aborted_connection
 
1701
 */
 
1702
PHPAPI void php_handle_aborted_connection(void)
 
1703
{
 
1704
        TSRMLS_FETCH();
 
1705
 
 
1706
        PG(connection_status) = PHP_CONNECTION_ABORTED;
 
1707
        php_output_set_status(0 TSRMLS_CC);
 
1708
 
 
1709
        if (!PG(ignore_user_abort)) {
 
1710
                zend_bailout();
 
1711
        }
 
1712
}
 
1713
/* }}} */
 
1714
 
 
1715
/* {{{ php_handle_auth_data
 
1716
 */
 
1717
PHPAPI int php_handle_auth_data(const char *auth TSRMLS_DC)
 
1718
{
 
1719
        int ret = -1;
 
1720
 
 
1721
        if (auth && auth[0] != '\0' && strncmp(auth, "Basic ", 6) == 0) {
 
1722
                char *pass;
 
1723
                char *user;
 
1724
 
 
1725
                user = php_base64_decode(auth + 6, strlen(auth) - 6, NULL);
 
1726
                if (user) {
 
1727
                        pass = strchr(user, ':');
 
1728
                        if (pass) {
 
1729
                                *pass++ = '\0';
 
1730
                                SG(request_info).auth_user = user;
 
1731
                                SG(request_info).auth_password = estrdup(pass);
 
1732
                                ret = 0;
 
1733
                        } else {
 
1734
                                efree(user);
 
1735
                        }
 
1736
                }
 
1737
        }
 
1738
 
 
1739
        if (ret == -1) {
 
1740
                SG(request_info).auth_user = SG(request_info).auth_password = NULL;
 
1741
        }
 
1742
        
 
1743
        return ret;
 
1744
}
 
1745
/* }}} */
 
1746
 
 
1747
/* {{{ php_lint_script
 
1748
 */
 
1749
PHPAPI int php_lint_script(zend_file_handle *file TSRMLS_DC)
 
1750
{
 
1751
        zend_op_array *op_array;
 
1752
        zend_bool retval = FAILURE;
 
1753
 
 
1754
        zend_try {
 
1755
                op_array = zend_compile_file(file, ZEND_INCLUDE TSRMLS_CC);
 
1756
                zend_destroy_file_handle(file TSRMLS_CC);
 
1757
 
 
1758
                if (op_array) {
 
1759
                        destroy_op_array(op_array TSRMLS_CC);
 
1760
                        efree(op_array);
 
1761
                        retval = SUCCESS;
 
1762
                }
 
1763
        } zend_end_try();
 
1764
 
 
1765
        return retval;
 
1766
}
 
1767
/* }}} */
 
1768
 
 
1769
#ifdef PHP_WIN32
 
1770
/* {{{ dummy_indent
 
1771
   just so that this symbol gets exported... */
 
1772
PHPAPI void dummy_indent()
 
1773
{
 
1774
        zend_indent();
 
1775
}
 
1776
/* }}} */
 
1777
#endif
 
1778
 
 
1779
/*
 
1780
 * Local variables:
 
1781
 * tab-width: 4
 
1782
 * c-basic-offset: 4
 
1783
 * End:
 
1784
 * vim600: sw=4 ts=4 fdm=marker
 
1785
 * vim<600: sw=4 ts=4
 
1786
 */