/
main.c
2231 lines (1988 loc) · 58.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
| Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* #define CRASH_DETECTION */
#define SHUTDOWN_DEBUG(resource) fprintf(stderr, "*** Shutting down " resource "\n" )
#undef SHUTDOWN_DEBUG
#define SHUTDOWN_DEBUG(resource)
#ifdef THREAD_SAFE
#include "tls.h"
#endif
#include <stdio.h>
#include "php.h"
#ifdef MSVC5
#include "win32/time.h"
#include "win32/signal.h"
#include <process.h>
#else
#include "build-defs.h"
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_SETLOCALE
#include <locale.h>
#endif
#include "zend.h"
#include "php_ini.h"
#include "php_globals.h"
#include "main.h"
#include "control_structures.h"
#include "fopen-wrappers.h"
#include "functions/basic_functions.h"
#include "functions/info.h"
#include "functions/head.h"
#include "functions/post.h"
#include "functions/head.h"
#include "functions/type.h"
#include "snprintf.h"
#if WIN32|WINNT
#include <io.h>
#include <fcntl.h>
#include "win32/syslog.h"
#else
#include <syslog.h>
#endif
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_highlight.h"
#include "zend_indent.h"
#if USE_SAPI
#include "serverapi/sapi.h"
void *gLock;
#ifndef THREAD_SAFE
struct sapi_request_info *sapi_rqst;
#endif
#endif
#if MSVC5 || !defined(HAVE_GETOPT)
#include "getopt.h"
#endif
#ifdef ZTS
int compiler_globals_id;
int executor_globals_id;
#endif
#ifndef ZTS
php_core_globals core_globals;
#endif
void *gLock; /*mutex variable */
#define PHP_MODE_STANDARD 1
#define PHP_MODE_HIGHLIGHT 2
#define PHP_MODE_INDENT 3
/* True globals (no need for thread safety) */
HashTable configuration_hash;
char *php3_ini_path = NULL;
PHP_INI_MH(OnSetPrecision)
{
ELS_FETCH();
EG(precision) = atoi(new_value);
return SUCCESS;
}
PHP_INI_BEGIN()
PHP_INI_ENTRY("short_open_tag", "1", PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("asp_tags", "0", PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision, NULL)
PHP_INI_ENTRY("highlight.comment", HL_COMMENT_COLOR, PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("highlight.default", HL_DEFAULT_COLOR, PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("highlight.html", HL_HTML_COLOR, PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("highlight.string", HL_STRING_COLOR, PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("highlight.bg", HL_BG_COLOR, PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("highlight.keyword", HL_KEYWORD_COLOR, PHP_INI_ALL, NULL, NULL)
PHP_INI_ENTRY("magic_quotes_gpc", "1", PHP_INI_ALL, OnUpdateInt, (void *) XtOffsetOf(php_core_globals, magic_quotes_gpc))
PHP_INI_ENTRY("magic_quotes_runtime", "0", PHP_INI_ALL, OnUpdateInt, (void *) XtOffsetOf(php_core_globals, magic_quotes_runtime))
PHP_INI_ENTRY("magic_quotes_sybase", "0", PHP_INI_ALL, OnUpdateInt, (void *) XtOffsetOf(php_core_globals, magic_quotes_sybase))
PHP_INI_END()
#ifndef THREAD_SAFE
/*
* Globals yet to be protected
*/
int initialized; /* keep track of which resources were successfully initialized */
static int module_initialized = 0;
unsigned char header_is_being_sent;
#if WIN32|WINNT
unsigned int wintimer;
unsigned int timerstart;
unsigned int wintimer_counter = 0;
#endif
#if APACHE
request_rec *php3_rqst = NULL; /* request record pointer for apache module version */
#endif
/*
* End of globals to be protected
*/
#endif
php3_ini_structure php3_ini;
php3_ini_structure php3_ini_master;
void _php3_build_argv(char * ELS_DC);
static void php3_timeout(int dummy);
static void php3_set_timeout(long seconds INLINE_TLS);
#if APACHE
void php3_apache_puts(const char *s)
{
TLS_VARS;
if (GLOBAL(php3_rqst)) {
rputs(s, GLOBAL(php3_rqst));
} else {
fputs(s, stdout);
}
}
void php3_apache_putc(char c)
{
TLS_VARS;
if (GLOBAL(php3_rqst)) {
rputc(c, GLOBAL(php3_rqst));
} else {
fputc(c, stdout);
}
}
#endif
void php3_log_err(char *log_message)
{
FILE *log_file;
TLS_VARS;
/* Try to use the specified logging location. */
if (php3_ini.error_log != NULL) {
#if HAVE_SYSLOG_H
if (strcmp(php3_ini.error_log, "syslog")) {
syslog(LOG_NOTICE, log_message);
return;
} else {
#endif
log_file = fopen(php3_ini.error_log, "a");
if (log_file != NULL) {
fprintf(log_file, log_message);
fprintf(log_file, "\n");
fclose(log_file);
return;
}
#if HAVE_SYSLOG_H
}
#endif
}
/* Otherwise fall back to the default logging location. */
#if APACHE
if (GLOBAL(php3_rqst)) {
#if MODULE_MAGIC_NUMBER >= 19970831
aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, php3_rqst->server, log_message);
#else
log_error(log_message, php3_rqst->server);
#endif
} else {
fprintf(stderr, log_message);
fprintf(stderr, "\n");
}
#endif /*APACHE */
#if CGI_BINARY
if (php3_header()) {
fprintf(stderr, log_message);
fprintf(stderr, "\n");
}
#endif
}
/* is 4K big enough? */
#define PRINTF_BUFFER_SIZE 1024*4
/* wrapper for modules to use PHPWRITE */
PHPAPI int php3_write(void *buf, int size)
{
TLS_VARS;
return PHPWRITE(buf, size);
}
PHPAPI int php3_printf(const char *format,...)
{
va_list args;
int ret;
char buffer[PRINTF_BUFFER_SIZE];
int size;
va_start(args, format);
size = vsprintf(buffer, format, args);
ret = PHPWRITE(buffer, size);
va_end(args);
return ret;
}
/* extended error handling function */
PHPAPI void php3_error(int type, const char *format,...)
{
va_list args;
char *error_filename = NULL;
uint error_lineno;
char buffer[1024];
int size = 0;
ELS_FETCH();
if (!(type & E_CORE)) {
if (!GLOBAL(initialized)) { /* don't display further errors after php3_request_shutdown() */
return;
}
}
switch (type) {
case E_CORE_ERROR:
case E_CORE_WARNING:
error_filename = NULL;
error_lineno = 0;
break;
case E_PARSE:
case E_COMPILE_ERROR:
case E_COMPILE_WARNING: {
CLS_FETCH();
error_filename = zend_get_compiled_filename();
error_lineno = CG(zend_lineno);
}
break;
case E_ERROR:
case E_NOTICE:
case E_WARNING:
error_filename = zend_get_executed_filename(ELS_C);
error_lineno = zend_get_executed_lineno(ELS_C);
break;
default:
error_filename = NULL;
error_lineno = 0;
break;
}
if (EG(error_reporting) & type || (type & E_CORE)) {
char *error_type_str;
switch (type) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
error_type_str = "Fatal error";
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
error_type_str = "Warning";
break;
case E_PARSE:
error_type_str = "Parse error";
break;
case E_NOTICE:
error_type_str = "Warning";
break;
default:
error_type_str = "Unknown error";
break;
}
/* get include file name */
if (php3_ini.log_errors || php3_ini.display_errors) {
va_start(args, format);
size = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
va_end(args);
buffer[sizeof(buffer) - 1] = 0;
if (php3_ini.log_errors) {
char log_buffer[1024];
snprintf(log_buffer, 1024, "PHP 3 %s: %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno);
php3_log_err(log_buffer);
}
if (php3_ini.display_errors) {
if(php3_ini.error_prepend_string) {
PUTS(php3_ini.error_prepend_string);
}
php3_printf("<br>\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br>\n", error_type_str, buffer, error_filename, error_lineno);
if(php3_ini.error_append_string) {
PUTS(php3_ini.error_append_string);
}
}
}
}
if (php3_ini.track_errors) {
pval tmp;
va_start(args, format);
size = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
va_end(args);
buffer[sizeof(buffer) - 1] = 0;
tmp.value.str.val = (char *) estrndup(buffer, size);
tmp.value.str.len = size;
tmp.type = IS_STRING;
_php3_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void *) & tmp, sizeof(pval), NULL);
}
switch (type) {
case E_ERROR:
case E_CORE_ERROR:
/*case E_PARSE: the parser would return 1 (failure), we can bail out nicely */
case E_COMPILE_ERROR:
zend_bailout();
break;
}
}
#if HAVE_SETITIMER
static void php3_timeout(int dummy)
{
TLS_VARS;
php3_error(E_ERROR, "Maximum execution time of %d seconds exceeded", php3_ini.max_execution_time);
}
#endif
/* This one doesn't exists on QNX */
#ifndef SIGPROF
#define SIGPROF 27
#endif
static void php3_set_timeout(long seconds INLINE_TLS)
{
#if WIN32|WINNT
if (seconds > 0) {
GLOBAL(timerstart) = (unsigned int) clock();
GLOBAL(wintimer) = GLOBAL(timerstart) + (CLOCKS_PER_SEC * seconds);
} else {
GLOBAL(wintimer) = 0;
}
#else
#if HAVE_SETITIMER
struct itimerval t_r; /* timeout requested */
t_r.it_value.tv_sec = seconds;
t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
setitimer(ITIMER_PROF, &t_r, NULL);
signal(SIGPROF, php3_timeout);
#endif
#endif
}
static void php3_unset_timeout(INLINE_TLS_VOID)
{
#if WIN32|WINNT
GLOBAL(wintimer) = 0;
#else
#if HAVE_SETITIMER
struct itimerval no_timeout;
no_timeout.it_value.tv_sec = no_timeout.it_value.tv_usec = no_timeout.it_interval.tv_sec = no_timeout.it_interval.tv_usec = 0;
setitimer(ITIMER_PROF, &no_timeout, NULL);
#endif
#endif
}
void php3_set_time_limit(INTERNAL_FUNCTION_PARAMETERS)
{
pval *new_timeout;
if (php3_ini.safe_mode) {
php3_error(E_WARNING, "Cannot set time limit in safe mode");
RETURN_FALSE;
}
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &new_timeout) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(new_timeout);
/* FIXME ** This is BAD...in a threaded situation, any user
can set the timeout for php on a server wide basis.
INI variables should not be reset via a user script
Fix what? At least on Unix, timers like these are
per-thread timers. Well, with a little work they will
be. If we use a bound thread and proper masking it
should work fine. Is this FIXME a WIN32 problem? Is
there no way to do per-thread timers on WIN32?
*/
php3_unset_timeout(_INLINE_TLS_VOID);
php3_set_timeout(new_timeout->value.lval _INLINE_TLS);
}
static FILE *php_fopen_wrapper_for_zend(const char *filename)
{
int issock=0, socketd=0;
FILE *retval=php3_fopen_wrapper((char *) filename, "r", USE_PATH|IGNORE_URL_WIN, &issock, &socketd);
if (issock) {
retval = fdopen(socketd, "r");
}
return retval;
}
static void php_message_handler_for_zend(long message, void *data)
{
switch (message) {
case ZMSG_ENABLE_TRACK_VARS:
php3_ini.track_vars = 1;
break;
case ZMSG_FAILED_INCLUDE_FOPEN:
php3_error(E_WARNING, "Failed opening '%s' for inclusion", php3_strip_url_passwd((char *) data));
break;
case ZMSG_FAILED_REQUIRE_FOPEN:
php3_error(E_ERROR, "Failed opening required '%s'", php3_strip_url_passwd((char *) data));
break;
case ZMSG_FAILED_HIGHLIGHT_FOPEN:
php3_error(E_WARNING, "Failed opening '%s' for highlighting", php3_strip_url_passwd((char *) data));
break;
case ZMSG_MEMORY_LEAK_DETECTED: {
mem_header *t = (mem_header *) data;
ELS_FETCH();
if (EG(error_reporting)&E_WARNING) {
#if ZEND_DEBUG
# if APACHE /* log into the errorlog, since at this time we can't send messages to the browser */
char memory_leak_buf[512];
snprintf(memory_leak_buf,512,"Possible PHP3 memory leak detected (harmless): %d bytes from %s:%d",t->size,t->filename,t->lineno);
# if MODULE_MAGIC_NUMBER >= 19970831
aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, GLOBAL(php3_rqst)->server, memory_leak_buf);
# else
log_error(memory_leak_buf,GLOBAL(php3_rqst)->server);
# endif
# else
php3_printf("Freeing 0x%0.8X (%d bytes), allocated in %s on line %d<br>\n",(void *)((char *)t+sizeof(mem_header)+PLATFORM_PADDING),t->size,t->filename,t->lineno);
# endif
#endif
}
}
break;
}
}
int php3_request_startup(CLS_D ELS_DC)
{
zend_output_startup();
#if APACHE && defined(CRASH_DETECTION)
{
char log_message[256];
snprintf(log_message,256,"php3_request_startup(): script='%s', pid=%d",php3_rqst->filename,getpid());
log_error(log_message, php3_rqst->server);
}
#endif
php3_set_timeout(php3_ini.max_execution_time _INLINE_TLS);
GLOBAL(initialized) = 0;
#if APACHE
/*
* For the Apache module version, this bit of code registers a cleanup
* function that gets triggered when our request pool is destroyed.
* We need this because at any point in our code we can be interrupted
* and that may happen before we have had time to free our memory.
* The php3_shutdown function needs to free all outstanding allocated
* memory.
*/
block_alarms();
register_cleanup(GLOBAL(php3_rqst)->pool, NULL, php3_request_shutdown, php3_request_shutdown_for_exec);
unblock_alarms();
#endif
/* initialize global variables */
{
EG(error_reporting) = php3_ini.errors;
GLOBAL(header_is_being_sent) = 0;
GLOBAL(php3_track_vars) = php3_ini.track_vars;
}
if (php3_init_request_info((void *) &php3_ini)) {
php3_printf("Unable to initialize request info.\n");
return FAILURE;
}
GLOBAL(initialized) |= INIT_REQUEST_INFO;
init_compiler(CLS_C ELS_CC);
init_executor(CLS_C ELS_CC);
startup_scanner(CLS_C);
return SUCCESS;
}
void php3_request_shutdown_for_exec(void *dummy)
{
TLS_VARS;
/* used to close fd's in the 3..255 range here, but it's problematic
*/
shutdown_memory_manager(1, 1);
}
int return_one(void *p)
{
return 1;
}
void php3_request_shutdown(void *dummy INLINE_TLS)
{
#if FHTTPD
char tmpline[128];
int i, serverdefined;
#endif
#if APACHE && defined(CRASH_DETECTION)
{
char log_message[256];
snprintf(log_message,256,"php3_request_shutdown(): script='%s', pid=%d",php3_rqst->filename,getpid());
log_error(log_message, php3_rqst->server);
}
#endif
CLS_FETCH();
ELS_FETCH();
php3_header();
zend_end_ob_buffering(1);
php3_call_shutdown_functions();
GLOBAL(initialized) &= ~INIT_ENVIRONMENT; /* does not require any special shutdown */
php_ini_rshutdown();
shutdown_scanner(CLS_C);
shutdown_compiler(CLS_C);
shutdown_executor(ELS_C);
if (GLOBAL(initialized) & INIT_REQUEST_INFO) {
SHUTDOWN_DEBUG("Request info");
php3_destroy_request_info((void *) &php3_ini);
GLOBAL(initialized) &= ~INIT_REQUEST_INFO;
}
if (GLOBAL(initialized) & INIT_SCANNER) {
SHUTDOWN_DEBUG("Scanner");
reset_scanner(CLS_C);
GLOBAL(initialized) &= ~INIT_SCANNER;
}
SHUTDOWN_DEBUG("Memory manager");
shutdown_memory_manager(0, 0);
if (GLOBAL(initialized)) {
php3_error(E_WARNING, "Unknown resources in request shutdown function");
}
php3_unset_timeout(_INLINE_TLS_VOID);
#if CGI_BINARY
fflush(stdout);
if(GLOBAL(request_info).php_argv0) {
free(GLOBAL(request_info).php_argv0);
GLOBAL(request_info).php_argv0 = NULL;
}
#endif
#if FHTTPD
if (response) {
if (!headermade) {
makestandardheader(response, 200, "text/html", "fhttpd", req && req->keepalive);
} else {
if (headerfirstline)
putlinetoheader(response, headerfirstline);
else
putlinetoheader(response, "HTTP/1.0 200 OK\r\n");
serverdefined = 0;
for (i = 0; i < headerlines; i++) {
if (!strncmp(currentheader[i], "Server:", 7))
serverdefined = 1;
putlinetoheader(response, currentheader[i]);
}
if (!serverdefined)
putlinetoheader(response, "Server: fhttpd\r\n");
if (response->datasize) {
sprintf(tmpline, "Content-Length: %ld\r\n", response->datasize);
putlinetoheader(response, tmpline);
if (req && req->keepalive)
putlinetoheader(response,
"Connection: Keep-Alive\r\nKeep-Alive: max=0, timeout=30\r\n");
}
php3_fhttpd_free_header();
}
sendresponse(server, response);
if (response->datasize)
finishresponse(server, response);
else
finishdropresponse(server, response);
deleteresponse(response);
}
response = NULL;
if (req)
deleterequest(req);
req = NULL;
#endif
#if USE_SAPI
GLOBAL(sapi_rqst)->flush(GLOBAL(sapi_rqst)->scid);
#endif
}
static int php3_config_ini_startup(ELS_D)
{
/* set the memory limit to a reasonable number so that we can get
* through this startup phase properly
*/
php3_ini.memory_limit=1<<23; /* 8MB */
if (php3_init_config() == FAILURE) {
php3_printf("PHP: Unable to parse configuration file.\n");
return FAILURE;
}
#if !(USE_SAPI)
GLOBAL(module_initialized) |= INIT_CONFIG;
#endif
/* initialize run-time variables */
/* I have remarked out some stuff
that may or may not be needed */
{
char *temp;
if (cfg_get_long("max_execution_time", &php3_ini.max_execution_time) == FAILURE) {
php3_ini.max_execution_time = 30;
}
if (cfg_get_long("memory_limit", &php3_ini.memory_limit) == FAILURE) {
php3_ini.memory_limit = 1<<23; /* 8MB */
}
if (cfg_get_string("SMTP", &php3_ini.smtp) == FAILURE) {
php3_ini.smtp = "localhost";
}
if (cfg_get_string("sendmail_path", &php3_ini.sendmail_path) == FAILURE
|| !php3_ini.sendmail_path[0]) {
#ifdef PHP_PROG_SENDMAIL
php3_ini.sendmail_path = PHP_PROG_SENDMAIL " -t";
#else
php3_ini.sendmail_path = NULL;
#endif
}
if (cfg_get_string("sendmail_from", &php3_ini.sendmail_from) == FAILURE) {
php3_ini.sendmail_from = NULL;
}
if (cfg_get_long("error_reporting", &php3_ini.errors) == FAILURE) {
php3_ini.errors = E_ALL & ~E_NOTICE;
}
EG(error_reporting) = php3_ini.errors;
if (cfg_get_string("error_log", &php3_ini.error_log) == FAILURE) {
php3_ini.error_log = NULL;
}
if (cfg_get_long("track_errors", &php3_ini.track_errors) == FAILURE) {
php3_ini.track_errors = 0;
}
if (cfg_get_long("display_errors", &php3_ini.display_errors) == FAILURE) {
php3_ini.display_errors = 1;
}
if (cfg_get_long("log_errors", &php3_ini.log_errors) == FAILURE) {
php3_ini.log_errors = 0;
}
if (cfg_get_long("warn_plus_overloading", &php3_ini.warn_plus_overloading) == FAILURE) {
php3_ini.warn_plus_overloading = 0;
}
if (cfg_get_long("y2k_compliance", &php3_ini.y2k_compliance) == FAILURE) {
php3_ini.y2k_compliance = 0;
}
if (cfg_get_long("define_syslog_variables", &php3_ini.define_syslog_variables) == FAILURE) {
php3_ini.define_syslog_variables = 0;
}
if (cfg_get_string("doc_root", &php3_ini.doc_root) == FAILURE) {
if ((temp = getenv("PHP_DOCUMENT_ROOT"))) {
php3_ini.doc_root = temp;
} else {
php3_ini.doc_root = NULL;
}
}
if (cfg_get_long("short_open_tag", &php3_ini.short_open_tag) == FAILURE) {
php3_ini.short_open_tag = DEFAULT_SHORT_OPEN_TAG;
}
if (cfg_get_long("asp_tags", &php3_ini.asp_tags) == FAILURE) {
php3_ini.asp_tags = 0;
}
if (cfg_get_string("user_dir", &php3_ini.user_dir) == FAILURE) {
if ((temp = getenv("PHP_USER_DIR"))) {
php3_ini.user_dir = temp;
} else {
php3_ini.user_dir = NULL;
}
}
if (cfg_get_long("safe_mode", &php3_ini.safe_mode) == FAILURE) {
php3_ini.safe_mode = PHP_SAFE_MODE;
}
if (cfg_get_string("safe_mode_exec_dir", &php3_ini.safe_mode_exec_dir) == FAILURE) {
#ifdef PHP_SAFE_MODE_EXEC_DIR
php3_ini.safe_mode_exec_dir = PHP_SAFE_MODE_EXEC_DIR;
#else
php3_ini.safe_mode_exec_dir = "/";
#endif
}
if (cfg_get_long("track_vars", &php3_ini.track_vars) == FAILURE) {
php3_ini.track_vars = PHP_TRACK_VARS;
}
if (cfg_get_string("include_path", &php3_ini.include_path) == FAILURE) {
if ((temp = getenv("PHP_INCLUDE_PATH"))) {
php3_ini.include_path = temp;
} else {
php3_ini.include_path = NULL;
}
}
if (cfg_get_string("auto_prepend_file", &php3_ini.auto_prepend_file) == FAILURE) {
if ((temp = getenv("PHP_AUTO_PREPEND_FILE"))) {
php3_ini.auto_prepend_file = temp;
} else {
php3_ini.auto_prepend_file = NULL;
}
}
if (cfg_get_string("auto_append_file", &php3_ini.auto_append_file) == FAILURE) {
if ((temp = getenv("PHP_AUTO_APPEND_FILE"))) {
php3_ini.auto_append_file = temp;
} else {
php3_ini.auto_append_file = NULL;
}
}
if (cfg_get_string("upload_tmp_dir", &php3_ini.upload_tmp_dir) == FAILURE) {
/* php3_ini.upload_tmp_dir = UPLOAD_TMPDIR; */
php3_ini.upload_tmp_dir = NULL;
}
if (cfg_get_long("upload_max_filesize", &php3_ini.upload_max_filesize) == FAILURE) {
php3_ini.upload_max_filesize = 2097152; /* 2 Meg default */
}
if (cfg_get_string("extension_dir", &php3_ini.extension_dir) == FAILURE) {
php3_ini.extension_dir = NULL;
}
if (cfg_get_long("sql.safe_mode", &php3_ini.sql_safe_mode) == FAILURE) {
php3_ini.sql_safe_mode = 0;
}
if (cfg_get_long("engine", &php3_ini.engine) == FAILURE) {
php3_ini.engine = 1;
}
if (cfg_get_long("last_modified", &php3_ini.last_modified) == FAILURE) {
php3_ini.last_modified = 0;
}
if (cfg_get_long("xbithack", &php3_ini.xbithack) == FAILURE) {
php3_ini.xbithack = 0;
}
if (cfg_get_string("browscap", &php3_ini.browscap) == FAILURE) {
php3_ini.browscap = NULL;
}
if (cfg_get_string("arg_separator", &php3_ini.arg_separator) == FAILURE) {
php3_ini.arg_separator = "&";
}
if (cfg_get_string("gpc_order", &php3_ini.gpc_order) == FAILURE) {
php3_ini.gpc_order = "GPC";
}
if (cfg_get_string("error_prepend_string", &php3_ini.error_prepend_string) == FAILURE) {
php3_ini.error_prepend_string = NULL;
}
if (cfg_get_string("error_append_string", &php3_ini.error_append_string) == FAILURE) {
php3_ini.error_append_string = NULL;
}
if (cfg_get_string("open_basedir", &php3_ini.open_basedir) == FAILURE) {
php3_ini.open_basedir = NULL;
}
if (cfg_get_long("enable_dl", &php3_ini.enable_dl) == FAILURE) {
php3_ini.enable_dl = 1;
}
/* THREADX Will have to look into this on windows
* Make a master copy to use as a basis for every per-dir config.
* Without two copies we would have a previous requst's per-dir
* config carry forward to the next one.
*/
memcpy(&php3_ini_master, &php3_ini, sizeof(php3_ini));
}
return SUCCESS;
}
static void php3_config_ini_shutdown(INLINE_TLS_VOID)
{
#if USE_SAPI
php3_shutdown_config();
#else
if (GLOBAL(module_initialized) & INIT_CONFIG) {
php3_shutdown_config();
GLOBAL(module_initialized) &= ~INIT_CONFIG;
}
#endif
}
int php3_module_startup(CLS_D ELS_DC)
{
zend_utility_functions zuf;
zend_utility_values zuv;
int module_number=0; /* for REGISTER_INI_ENTRIES() */
#if (WIN32|WINNT) && !(USE_SAPI)
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 0);
#else
if (GLOBAL(module_initialized)) {
return SUCCESS;
}
#endif
zend_output_startup();
zuf.error_function = php3_error;
zuf.printf_function = php3_printf;
zuf.write_function = zend_body_write;
zuf.fopen_function = php_fopen_wrapper_for_zend;
zuf.message_handler = php_message_handler_for_zend;
zuf.block_interruptions = BLOCK_INTERRUPTIONS;
zuf.unblock_interruptions = UNBLOCK_INTERRUPTIONS;
zuv.short_tags = DEFAULT_SHORT_OPEN_TAG;
zuv.asp_tags = 0;
zend_startup(&zuf, &zuv, NULL);
#if HAVE_SETLOCALE
setlocale(LC_CTYPE, "");
#endif
EG(error_reporting) = E_ALL;
#if (WIN32|WINNT) && !(USE_SAPI)
/* start up winsock services */
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
php3_printf("\nwinsock.dll unusable. %d\n", WSAGetLastError());
return FAILURE;
}
GLOBAL(module_initialized) |= INIT_WINSOCK;
#endif
SET_MUTEX(gLock);
le_index_ptr = _register_list_destructors(NULL, NULL, 0);
FREE_MUTEX(gLock);
/* We cannot do the config starup until after all the above
happens, otherwise loading modules from ini file breaks */
#if !USE_SAPI
if (php3_config_ini_startup(ELS_C) == FAILURE) {
return FAILURE;
}
#endif
php_ini_mstartup();
REGISTER_INI_ENTRIES();
if (module_startup_modules() == FAILURE) {
php3_printf("Unable to start modules\n");
return FAILURE;
}
return SUCCESS;
}
void php3_module_shutdown_for_exec(void)
{
/* used to close fd's in the range 3.255 here, but it's problematic */
}
void php3_module_shutdown()
{
int module_number=0; /* for UNREGISTER_INI_ENTRIES() */
CLS_FETCH();
ELS_FETCH();
#if !USE_SAPI
/* close down the ini config */
php3_config_ini_shutdown(_INLINE_TLS_VOID);
#endif
#if (WIN32|WINNT) && !(USE_SAPI)
/*close winsock */
if (GLOBAL(module_initialized) & INIT_WINSOCK) {
WSACleanup();
GLOBAL(module_initialized) &= ~INIT_WINSOCK;
}
#endif
if (GLOBAL(module_initialized)) {
php3_error(E_WARNING, "Unknown resource in module shutdown");
}
#if CGI_BINARY
fflush(stdout);
#endif
#if 0 /* SAPI */
GLOBAL(sapi_rqst)->flush(GLOBAL(sapi_rqst)->scid);
#endif
zend_shutdown();
UNREGISTER_INI_ENTRIES();
php_ini_mshutdown();
shutdown_memory_manager(0, 1);