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