forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgi_main.c
1593 lines (1390 loc) · 42.3 KB
/
cgi_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 Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <[email protected]> |
| Stig Bakken <[email protected]> |
| Zeev Suraski <[email protected]> |
| FastCGI: Ben Mansell <[email protected]> |
| Shane Caraveo <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "php_globals.h"
#include "php_variables.h"
#include "zend_modules.h"
#include "SAPI.h"
#include <stdio.h>
#include "php.h"
#ifdef PHP_WIN32
#include "win32/time.h"
#include "win32/signal.h"
#include <process.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 "zend_extensions.h"
#include "php_ini.h"
#include "php_globals.h"
#include "php_main.h"
#include "fopen_wrappers.h"
#include "ext/standard/php_standard.h"
#ifdef PHP_WIN32
#include <io.h>
#include <fcntl.h>
#include "win32/php_registry.h"
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef __riscos__
#include <unixlib/local.h>
#endif
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_highlight.h"
#include "zend_indent.h"
#include "php_getopt.h"
#if PHP_FASTCGI
#include "fcgi_config.h"
#include "fcgiapp.h"
/* don't want to include fcgios.h, causes conflicts */
#ifdef PHP_WIN32
extern int OS_SetImpersonate(void);
#else
/* XXX this will need to change later when threaded fastcgi is
implemented. shane */
struct sigaction act, old_term, old_quit, old_int;
#endif
static void (*php_php_import_environment_variables)(zval *array_ptr TSRMLS_DC);
#ifndef PHP_WIN32
/* these globals used for forking children on unix systems */
/**
* Number of child processes that will get created to service requests
*/
static int children = 0;
/**
* Set to non-zero if we are the parent process
*/
static int parent = 1;
/**
* Process group
*/
static pid_t pgroup;
#endif
#endif
#define PHP_MODE_STANDARD 1
#define PHP_MODE_HIGHLIGHT 2
#define PHP_MODE_INDENT 3
#define PHP_MODE_LINT 4
#define PHP_MODE_STRIP 5
static char *optarg = NULL;
static int optind = 1;
static const opt_struct OPTIONS[] = {
{'a', 0, "interactive"},
{'C', 0, "no-chdir"},
{'c', 1, "php-ini"},
{'d', 1, "define"},
{'e', 0, "profile-info"},
{'f', 1, "file"},
{'g', 1, "global"},
{'h', 0, "help"},
{'i', 0, "info"},
{'l', 0, "syntax-check"},
{'m', 0, "modules"},
{'n', 0, "no-php-ini"},
{'q', 0, "no-header"},
{'s', 0, "syntax-highlight"},
{'s', 0, "syntax-highlighting"},
{'w', 0, "strip"},
{'?', 0, "usage"},/* help alias (both '?' and 'usage') */
{'v', 0, "version"},
{'z', 1, "zend-extension"},
{'-', 0, NULL} /* end of args */
};
#if ENABLE_PATHINFO_CHECK
/* true global. this is retreived once only, even for fastcgi */
long fix_pathinfo=1;
#endif
#ifdef PHP_WIN32
#define TRANSLATE_SLASHES(path) \
{ \
char *tmp = path; \
while (*tmp) { \
if (*tmp == '\\') *tmp = '/'; \
tmp++; \
} \
}
#else
#define TRANSLATE_SLASHES(path)
#endif
static int print_module_info(zend_module_entry *module, void *arg TSRMLS_DC)
{
php_printf("%s\n", module->name);
return 0;
}
static int module_name_cmp(const void *a, const void *b TSRMLS_DC)
{
Bucket *f = *((Bucket **) a);
Bucket *s = *((Bucket **) b);
return strcmp(((zend_module_entry *)f->pData)->name,
((zend_module_entry *)s->pData)->name);
}
static void print_modules(TSRMLS_D)
{
HashTable sorted_registry;
zend_module_entry tmp;
zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
zend_hash_copy(&sorted_registry, &module_registry, NULL, &tmp, sizeof(zend_module_entry));
zend_hash_sort(&sorted_registry, zend_qsort, module_name_cmp, 0 TSRMLS_CC);
zend_hash_apply_with_argument(&sorted_registry, (apply_func_arg_t) print_module_info, NULL TSRMLS_CC);
zend_hash_destroy(&sorted_registry);
}
static int print_extension_info(zend_extension *ext, void *arg TSRMLS_DC)
{
php_printf("%s\n", ext->name);
return 0;
}
static int extension_name_cmp(const zend_llist_element **f,
const zend_llist_element **s TSRMLS_DC)
{
return strcmp(((zend_extension *)(*f)->data)->name,
((zend_extension *)(*s)->data)->name);
}
static void print_extensions(TSRMLS_D)
{
zend_llist sorted_exts;
zend_llist_copy(&sorted_exts, &zend_extensions);
zend_llist_sort(&sorted_exts, extension_name_cmp TSRMLS_CC);
zend_llist_apply_with_argument(&sorted_exts, (llist_apply_with_arg_func_t) print_extension_info, NULL TSRMLS_CC);
zend_llist_destroy(&sorted_exts);
}
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC)
{
#ifdef PHP_WRITE_STDOUT
long ret;
#else
size_t ret;
#endif
#if PHP_FASTCGI
if (!FCGX_IsCGI()) {
FCGX_Request *request = (FCGX_Request *)SG(server_context);
long ret = FCGX_PutStr( str, str_length, request->out );
if (ret <= 0) {
return 0;
}
return ret;
}
#endif
#ifdef PHP_WRITE_STDOUT
ret = write(STDOUT_FILENO, str, str_length);
if (ret <= 0) return 0;
return ret;
#else
ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
return ret;
#endif
}
static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC)
{
const char *ptr = str;
uint remaining = str_length;
size_t ret;
while (remaining > 0)
{
ret = sapi_cgibin_single_write(ptr, remaining TSRMLS_CC);
if (!ret) {
php_handle_aborted_connection();
}
ptr += ret;
remaining -= ret;
}
return str_length;
}
static void sapi_cgibin_flush(void *server_context)
{
#if PHP_FASTCGI
if (!FCGX_IsCGI()) {
FCGX_Request *request = (FCGX_Request *)server_context;
if(!request || FCGX_FFlush( request->out ) == -1 ) {
php_handle_aborted_connection();
}
} else
#endif
if (fflush(stdout)==EOF) {
php_handle_aborted_connection();
}
}
#define SAPI_CGI_MAX_HEADER_LENGTH 1024
static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
{
char buf[SAPI_CGI_MAX_HEADER_LENGTH];
sapi_header_struct *h;
zend_llist_position pos;
long rfc2616_headers = 0;
if(SG(request_info).no_headers == 1) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
/* Check wheater to send RFC2616 style headers compatible with
* PHP versions 4.2.3 and earlier compatible with web servers
* such as IIS. Default is informal CGI RFC header compatible
* with Apache.
*/
if (cfg_get_long("cgi.rfc2616_headers", &rfc2616_headers) == FAILURE) {
rfc2616_headers = 0;
}
if (SG(sapi_headers).http_response_code != 200) {
int len;
if (rfc2616_headers) {
len = snprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH,
"%s\r\n", SG(sapi_headers).http_status_line);
if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
len = SAPI_CGI_MAX_HEADER_LENGTH;
}
} else {
len = sprintf(buf, "Status: %d\r\n", SG(sapi_headers).http_response_code);
}
PHPWRITE_H(buf, len);
}
if (SG(sapi_headers).send_default_content_type) {
char *hd;
hd = sapi_get_default_content_type(TSRMLS_C);
PHPWRITE_H("Content-type: ", sizeof("Content-type: ")-1);
PHPWRITE_H(hd, strlen(hd));
PHPWRITE_H("\r\n", 2);
efree(hd);
}
h = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
while (h) {
PHPWRITE_H(h->header, h->header_len);
PHPWRITE_H("\r\n", 2);
h = zend_llist_get_next_ex(&sapi_headers->headers, &pos);
}
PHPWRITE_H("\r\n", 2);
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
{
uint read_bytes=0, tmp_read_bytes;
#if PHP_FASTCGI
char *pos = buffer;
#endif
count_bytes = MIN(count_bytes, (uint)SG(request_info).content_length-SG(read_post_bytes));
while (read_bytes < count_bytes) {
#if PHP_FASTCGI
if (!FCGX_IsCGI()) {
FCGX_Request *request = (FCGX_Request *)SG(server_context);
tmp_read_bytes = FCGX_GetStr( pos, count_bytes-read_bytes, request->in );
pos += tmp_read_bytes;
} else
#endif
tmp_read_bytes = read(0, buffer+read_bytes, count_bytes-read_bytes);
if (tmp_read_bytes<=0) {
break;
}
read_bytes += tmp_read_bytes;
}
return read_bytes;
}
static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC)
{
#if PHP_FASTCGI
/* when php is started by mod_fastcgi, no regular environment
is provided to PHP. It is always sent to PHP at the start
of a request. So we have to do our own lookup to get env
vars. This could probably be faster somehow. */
if (!FCGX_IsCGI()) {
FCGX_Request *request = (FCGX_Request *)SG(server_context);
return FCGX_GetParam(name,request->envp);
}
#endif
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment */
return getenv(name);
}
static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC)
{
int len=0;
char *buf = NULL;
if (!name) return NULL;
len = strlen(name) + (value?strlen(value):0) + sizeof("=") + 2;
buf = (char *)emalloc(len);
if (value) {
snprintf(buf,len-1,"%s=%s", name, value);
} else {
snprintf(buf,len-1,"%s=", name);
}
#if PHP_FASTCGI
/* when php is started by mod_fastcgi, no regular environment
is provided to PHP. It is always sent to PHP at the start
of a request. So we have to do our own lookup to get env
vars. This could probably be faster somehow. */
if (!FCGX_IsCGI()) {
FCGX_Request *request = (FCGX_Request *)SG(server_context);
FCGX_PutEnv(request,buf);
efree(buf);
return sapi_cgibin_getenv(name,0 TSRMLS_CC);
}
#endif
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment
this leaks, but it's only cgi anyway, we'll fix
it for 5.0
*/
if (value)
putenv(strdup(buf));
else
putenv(buf);
efree(buf);
return getenv(name);
}
static char *sapi_cgi_read_cookies(TSRMLS_D)
{
return sapi_cgibin_getenv((char *)"HTTP_COOKIE",0 TSRMLS_CC);
}
#if PHP_FASTCGI
void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
{
if (!FCGX_IsCGI()) {
FCGX_Request *request = (FCGX_Request *)SG(server_context);
char **env, *p, *t;
for (env = request->envp; env != NULL && *env != NULL; env++) {
p = strchr(*env, '=');
if (!p) { /* malformed entry? */
continue;
}
t = estrndup(*env, p - *env);
php_register_variable(t, p+1, array_ptr TSRMLS_CC);
efree(t);
}
}
/* call php's original import as a catch-all */
php_php_import_environment_variables(array_ptr TSRMLS_CC);
}
#endif
static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC)
{
/* In CGI mode, we consider the environment to be a part of the server
* variables
*/
php_import_environment_variables(track_vars_array TSRMLS_CC);
/* Build the special-case PHP_SELF variable for the CGI version */
php_register_variable("PHP_SELF", (SG(request_info).request_uri ? SG(request_info).request_uri:""), track_vars_array TSRMLS_CC);
}
static void sapi_cgi_log_message(char *message)
{
if (php_header()) {
fprintf(stderr, "%s", message);
fprintf(stderr, "\n");
}
}
static int sapi_cgi_deactivate(TSRMLS_D)
{
fflush(stdout);
if(SG(request_info).argv0) {
free(SG(request_info).argv0);
SG(request_info).argv0 = NULL;
}
return SUCCESS;
}
static int php_cgi_startup(sapi_module_struct *sapi_module)
{
if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
return FAILURE;
}
return SUCCESS;
}
/* {{{ sapi_module_struct cgi_sapi_module
*/
static sapi_module_struct cgi_sapi_module = {
#if PHP_FASTCGI
"cgi-fcgi", /* name */
"CGI/FastCGI", /* pretty name */
#else
"cgi", /* name */
"CGI", /* pretty name */
#endif
php_cgi_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
sapi_cgi_deactivate, /* deactivate */
sapi_cgibin_ub_write, /* unbuffered write */
sapi_cgibin_flush, /* flush */
NULL, /* get uid */
sapi_cgibin_getenv, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
sapi_cgi_send_headers, /* send headers handler */
NULL, /* send header handler */
sapi_cgi_read_post, /* read POST data */
sapi_cgi_read_cookies, /* read Cookies */
sapi_cgi_register_variables, /* register server variables */
sapi_cgi_log_message, /* Log message */
STANDARD_SAPI_MODULE_PROPERTIES
};
/* }}} */
/* {{{ php_cgi_usage
*/
static void php_cgi_usage(char *argv0)
{
char *prog;
prog = strrchr(argv0, '/');
if (prog) {
prog++;
} else {
prog = "php";
}
php_printf("Usage: %s [-q] [-h] [-s] [-v] [-i] [-f <file>]\n"
" %s <file> [args...]\n"
" -a Run interactively\n"
#if PHP_FASTCGI
" -b <address:port>|<port> Bind Path for external FASTCGI Server mode\n"
#endif
" -C Do not chdir to the script's directory\n"
" -c <path>|<file> Look for php.ini file in this directory\n"
" -n No php.ini file will be used\n"
" -d foo[=bar] Define INI entry foo with value 'bar'\n"
" -e Generate extended information for debugger/profiler\n"
" -f <file> Parse <file>. Implies `-q'\n"
" -h This help\n"
" -i PHP information\n"
" -l Syntax check only (lint)\n"
" -m Show compiled in modules\n"
" -q Quiet-mode. Suppress HTTP Header output.\n"
" -s Display colour syntax highlighted source.\n"
" -v Version number\n"
" -w Display source with stripped comments and whitespace.\n"
" -z <file> Load Zend extension <file>.\n",
prog, prog);
}
/* }}} */
/* {{{ init_request_info
initializes request_info structure
specificly in this section we handle proper translations
for:
PATH_INFO
derived from the portion of the URI path following
the script name but preceding any query data
may be empty
PATH_TRANSLATED
derived by taking any path-info component of the
request URI and performing any virtual-to-physical
translation appropriate to map it onto the server's
document repository structure
empty if PATH_INFO is empty
The env var PATH_TRANSLATED **IS DIFFERENT** than the
request_info.path_translated variable, the latter should
match SCRIPT_FILENAME instead.
SCRIPT_NAME
set to a URL path that could identify the CGI script
rather than the interpreter. PHP_SELF is set to this.
REQUEST_URI
uri section following the domain:port part of a URI
SCRIPT_FILENAME
The virtual-to-physical translation of SCRIPT_NAME (as per
PATH_TRANSLATED)
These settings are documented at
http://cgi-spec.golux.com/
Based on the following URL request:
http://localhost/info.php/test?a=b
should produce, which btw is the same as if
we were running under mod_cgi on apache (ie. not
using ScriptAlias directives):
PATH_INFO=/test
PATH_TRANSLATED=/docroot/test
SCRIPT_NAME=/info.php
REQUEST_URI=/info.php/test?a=b
SCRIPT_FILENAME=/docroot/info.php
QUERY_STRING=a=b
but what we get is (cgi/mod_fastcgi under apache):
PATH_INFO=/info.php/test
PATH_TRANSLATED=/docroot/info.php/test
SCRIPT_NAME=/php/php-cgi (from the Action setting I suppose)
REQUEST_URI=/info.php/test?a=b
SCRIPT_FILENAME=/path/to/php/bin/php-cgi (Action setting translated)
QUERY_STRING=a=b
Comments in the code below refer to using the above URL in a request
*/
static void init_request_info(TSRMLS_D)
{
char *env_script_filename = sapi_cgibin_getenv("SCRIPT_FILENAME",0 TSRMLS_CC);
char *env_path_translated = sapi_cgibin_getenv("PATH_TRANSLATED",0 TSRMLS_CC);
char *script_path_translated = env_script_filename;
#if !DISCARD_PATH
/* some broken servers do not have script_filename or argv0
an example, IIS configured in some ways. then they do more
broken stuff and set path_translated to the cgi script location */
if (!script_path_translated && env_path_translated)
script_path_translated = env_path_translated;
#endif
/* initialize the defaults */
SG(request_info).path_translated = NULL;
SG(request_info).request_method = NULL;
SG(request_info).query_string = NULL;
SG(request_info).request_uri = NULL;
SG(request_info).content_type = NULL;
SG(request_info).content_length = 0;
SG(sapi_headers).http_response_code = 200;
/* script_path_translated being set is a good indication that
we are running in a cgi environment, since it is always
null otherwise. otherwise, the filename
of the script will be retreived later via argc/argv */
if (script_path_translated) {
const char *auth;
char *content_length = sapi_cgibin_getenv("CONTENT_LENGTH",0 TSRMLS_CC);
char *content_type = sapi_cgibin_getenv("CONTENT_TYPE",0 TSRMLS_CC);
char *env_path_info = sapi_cgibin_getenv("PATH_INFO",0 TSRMLS_CC);
char *env_script_name = sapi_cgibin_getenv("SCRIPT_NAME",0 TSRMLS_CC);
#if ENABLE_PATHINFO_CHECK
struct stat st;
char *env_redirect_url = sapi_cgibin_getenv("REDIRECT_URL",0 TSRMLS_CC);
char *env_document_root = sapi_cgibin_getenv("DOCUMENT_ROOT",0 TSRMLS_CC);
if (fix_pathinfo) {
/* save the originals first for anything we change later */
if (env_path_translated)
_sapi_cgibin_putenv("ORIG_PATH_TRANSLATED",env_path_translated TSRMLS_CC);
if (env_path_info)
_sapi_cgibin_putenv("ORIG_PATH_INFO",env_path_info TSRMLS_CC);
if (env_script_name)
_sapi_cgibin_putenv("ORIG_SCRIPT_NAME",env_script_name TSRMLS_CC);
if (env_script_filename)
_sapi_cgibin_putenv("ORIG_SCRIPT_FILENAME",env_script_filename TSRMLS_CC);
if (!env_document_root) {
/* IIS version of DOCUMENT_ROOT, not avail in cgi, but is in fastcgi */
env_document_root = sapi_cgibin_getenv("APPL_PHYSICAL_PATH",0 TSRMLS_CC);
/* ini version of document root */
if (!env_document_root)
env_document_root = PG(doc_root);
/* set the document root, this makes a more
consistent env for php scripts */
if (env_document_root) {
env_document_root = _sapi_cgibin_putenv("DOCUMENT_ROOT",env_document_root TSRMLS_CC);
/* fix docroot */
TRANSLATE_SLASHES(env_document_root);
}
}
if (env_redirect_url) {
/*
pretty much apache specific. If we have a redirect_url
then our script_filename and script_name point to the
php executable
*/
script_path_translated = env_path_translated;
/* we correct SCRIPT_NAME now in case we don't have PATH_INFO */
env_script_name = _sapi_cgibin_putenv("SCRIPT_NAME",env_redirect_url TSRMLS_CC);
}
#ifdef __riscos__
/* Convert path to unix format*/
__riscosify_control|=__RISCOSIFY_DONT_CHECK_DIR;
script_path_translated=__unixify(script_path_translated,0,NULL,1,0);
#endif
/*
* if the file doesn't exist, try to extract PATH_INFO out
* of it by stat'ing back through the '/'
* this fixes url's like /info.php/test
*/
if (script_path_translated && stat( script_path_translated, &st ) == -1 ) {
char *pt = estrdup(script_path_translated);
int len = strlen(pt);
char *ptr;
while( (ptr = strrchr(pt,'/')) || (ptr = strrchr(pt,'\\')) ) {
*ptr = 0;
if ( stat(pt, &st) == 0 && S_ISREG(st.st_mode) ) {
/*
* okay, we found the base script!
* work out how many chars we had to strip off;
* then we can modify PATH_INFO
* accordingly
*
* we now have the makings of
* PATH_INFO=/test
* SCRIPT_FILENAME=/docroot/info.php
*
* we now need to figure out what docroot is.
* if DOCUMENT_ROOT is set, this is easy, otherwise,
* we have to play the game of hide and seek to figure
* out what SCRIPT_NAME should be
*/
int slen = len - strlen(pt);
int pilen = strlen( env_path_info );
char *path_info = env_path_info + pilen - slen;
env_path_info = _sapi_cgibin_putenv("PATH_INFO",path_info TSRMLS_CC);
script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME",pt TSRMLS_CC);
TRANSLATE_SLASHES(pt);
/* figure out docroot
SCRIPT_FILENAME minus SCRIPT_NAME
*/
if (env_document_root) {
int l = strlen(env_document_root);
int path_translated_len = 0;
char *path_translated = NULL;
if (env_document_root[l-1]=='/') --l;
/* we have docroot, so we should have:
* DOCUMENT_ROOT=/docroot
* SCRIPT_FILENAME=/docroot/info.php
*
* SCRIPT_NAME is the portion of the path beyond docroot
*/
env_script_name = _sapi_cgibin_putenv("SCRIPT_NAME",pt+l TSRMLS_CC);
/*
* PATH_TRANSATED = DOCUMENT_ROOT + PATH_INFO
*/
path_translated_len = l + strlen(env_path_info) + 2;
path_translated = (char *)emalloc(path_translated_len);
*path_translated = 0;
strncat(path_translated,env_document_root,l);
strcat(path_translated,env_path_info);
env_path_translated = _sapi_cgibin_putenv("PATH_TRANSLATED",path_translated TSRMLS_CC);
efree(path_translated);
} else if (env_script_name &&
strstr(pt,env_script_name)) {
/*
* PATH_TRANSATED = PATH_TRANSATED - SCRIPT_NAME + PATH_INFO
*/
int ptlen = strlen(pt)-strlen(env_script_name);
int path_translated_len = ptlen + strlen(env_path_info) + 2;
char *path_translated = NULL;
path_translated = (char *)emalloc(path_translated_len);
*path_translated = 0;
strncat(path_translated,pt,ptlen);
strcat(path_translated,env_path_info);
env_path_translated = _sapi_cgibin_putenv("PATH_TRANSLATED",path_translated TSRMLS_CC);
efree(path_translated);
}
break;
}
}
if (!ptr) {
/*
* if we stripped out all the '/' and still didn't find
* a valid path... we will fail, badly. of course we would
* have failed anyway... we output 'no input file' now.
*/
script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME",NULL TSRMLS_CC);
SG(sapi_headers).http_response_code = 404;
}
if (pt) efree(pt);
} else {
/* make sure path_info/translated are empty */
script_path_translated = _sapi_cgibin_putenv("SCRIPT_FILENAME",script_path_translated TSRMLS_CC);
_sapi_cgibin_putenv("PATH_INFO",NULL TSRMLS_CC);
_sapi_cgibin_putenv("PATH_TRANSLATED",NULL TSRMLS_CC);
}
SG(request_info).request_uri = sapi_cgibin_getenv("SCRIPT_NAME",0 TSRMLS_CC);
} else {
#endif
/* pre 4.3 behaviour, shouldn't be used but provides BC */
if (env_path_info)
SG(request_info).request_uri = env_path_info;
else
SG(request_info).request_uri = env_script_name;
#if !DISCARD_PATH
script_path_translated = env_path_translated;
#endif
#if ENABLE_PATHINFO_CHECK
}
#endif
SG(request_info).request_method = sapi_cgibin_getenv("REQUEST_METHOD",0 TSRMLS_CC);
SG(request_info).query_string = sapi_cgibin_getenv("QUERY_STRING",0 TSRMLS_CC);
if (script_path_translated)
SG(request_info).path_translated = estrdup(script_path_translated);
SG(request_info).content_type = (content_type ? content_type : "" );
SG(request_info).content_length = (content_length?atoi(content_length):0);
/* The CGI RFC allows servers to pass on unvalidated Authorization data */
auth = sapi_cgibin_getenv("HTTP_AUTHORIZATION",0 TSRMLS_CC);
php_handle_auth_data(auth TSRMLS_CC);
}
}
/* }}} */
static void define_command_line_ini_entry(char *arg)
{
char *name, *value;
name = arg;
value = strchr(arg, '=');
if (value) {
*value = 0;
value++;
} else {
value = "1";
}
zend_alter_ini_entry(name, strlen(name)+1, value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
}
static void php_register_command_line_global_vars(char **arg TSRMLS_DC)
{
char *var, *val;
var = *arg;
val = strchr(var, '=');
if (!val) {
printf("No value specified for variable '%s'\n", var);
} else {
*val++ = '\0';
php_register_variable(var, val, NULL TSRMLS_CC);
}
efree(*arg);
}
#if PHP_FASTCGI
/**
* Clean up child processes upon exit
*/
void fastcgi_cleanup(int signal)
{
#ifdef DEBUG_FASTCGI
fprintf( stderr, "FastCGI shutdown, pid %d\n", getpid() );
#endif
#ifndef PHP_WIN32
sigaction( SIGTERM, &old_term, 0 );
/* Kill all the processes in our process group */
kill( -pgroup, SIGTERM );
#endif
/* We should exit at this point, but MacOSX doesn't seem to */
exit( 0 );
}
#endif
/* {{{ main
*/
int main(int argc, char *argv[])
{
int exit_status = SUCCESS;
int cgi = 0, c, i, len;
zend_file_handle file_handle;
int retval = FAILURE;
char *s;
/* temporary locals */
int behavior=PHP_MODE_STANDARD;
int no_headers=0;
int orig_optind=optind;
char *orig_optarg=optarg;
char *script_file=NULL;
zend_llist global_vars;
int interactive=0;
#if FORCE_CGI_REDIRECT
int force_redirect = 1;
char *redirect_status_env = NULL;
#endif
/* end of temporary locals */
#ifdef ZTS
zend_compiler_globals *compiler_globals;
zend_executor_globals *executor_globals;
php_core_globals *core_globals;
sapi_globals_struct *sapi_globals;
void ***tsrm_ls;
#endif
#if PHP_FASTCGI
int max_requests = 500;
int requests = 0;
int fastcgi = !FCGX_IsCGI();
char *bindpath = NULL;
int fcgi_fd = 0;
FCGX_Request request;
#ifdef PHP_WIN32
int impersonate = 0;
#else
int status = 0;
#endif
#endif /* PHP_FASTCGI */
#ifdef HAVE_SIGNAL_H
#if defined(SIGPIPE) && defined(SIG_IGN)
signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
that sockets created via fsockopen()
don't kill PHP if the remote site
closes it. in apache|apxs mode apache
does that for us! [email protected]
20000419 */
#endif
#endif
#ifdef ZTS
tsrm_startup(1, 1, 0, NULL);
#endif
sapi_startup(&cgi_sapi_module);
#ifdef PHP_WIN32
_fmode = _O_BINARY; /*sets default for file streams to binary */
setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
#endif
#if PHP_FASTCGI
if (!fastcgi) {
#endif
/* Make sure we detect we are a cgi - a bit redundancy here,
but the default case is that we have to check only the first one. */
if (getenv("SERVER_SOFTWARE")
|| getenv("SERVER_NAME")
|| getenv("GATEWAY_INTERFACE")
|| getenv("REQUEST_METHOD")) {
cgi = 1;
}
#if PHP_FASTCGI
}
#endif
if (!cgi
#if PHP_FASTCGI
/* allow ini override for fastcgi */
#endif
) {
while ((c=php_getopt(argc, argv, OPTIONS, &optarg, &optind, 0))!=-1) {
switch (c) {
case 'c':
cgi_sapi_module.php_ini_path_override = strdup(optarg);
break;
case 'n':
cgi_sapi_module.php_ini_ignore = 1;
break;
#if PHP_FASTCGI
/* if we're started on command line, check to see if
we are being started as an 'external' fastcgi
server by accepting a bindpath parameter. */
case 'b':
if (!fastcgi) bindpath= strdup(optarg);
break;
#endif
}
}