summaryrefslogtreecommitdiff
path: root/ext/standard/head.c
blob: 86e3a7d835da4d68addcf343beae0d3dd24f6c1c (plain)
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
/*
   +----------------------------------------------------------------------+
   | 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: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
   +----------------------------------------------------------------------+
 */
/* $Id$ */

#include <stdio.h>
#include "php.h"
#include "ext/standard/php3_standard.h"
#include "SAPI.h"
#include "main.h"
#include "head.h"
#include "post.h"
#ifdef TM_IN_SYS_TIME
#include <sys/time.h>
#else
#include <time.h>
#endif

#include "php_globals.h"
#include "safe_mode.h"


/* need to figure out some nice way to get rid of these */
#ifndef THREAD_SAFE
static int php3_HeaderPrinted = 0;
static int php3_PrintHeader = 1;
static CookieList *top = NULL;
static char *cont_type = NULL;
static int header_called = 0;
#endif

void php3_PushCookieList(char *, char *, time_t, char *, char *, int);
CookieList *php3_PopCookieList(void);

int php3_init_head(INIT_FUNC_ARGS)
{
	php3_HeaderPrinted = 0;
	if (header_called == 0)
		php3_PrintHeader = 1;
	top = NULL;
	cont_type = NULL;

	return SUCCESS;
}


PHPAPI void php3_noheader(void)
{
	php3_PrintHeader = 0;
	header_called = 1;
}


#if 0
/* Adds header information */
void php4i_add_header_information(char *header_information, uint header_length)
{
	char *r;
#if APACHE
	char *rr = NULL;
	char *temp = NULL;
	long myuid = 0L;
	char temp2[32];
	request_rec *req;
	SLS_FETCH();
	
	req = ((request_rec *) SG(server_context));
#endif

	if (php3_HeaderPrinted == 1) {
#if DEBUG
		php3_error(E_WARNING, "Cannot add more header information - the header was already sent "
							  "(header information may be added only before any output is generated from the script - "
							  "check for text or whitespace outside PHP tags, or calls to functions that output text)");
#endif
		return;					/* too late, already sent */
	}
#if APACHE
	/*
	 * Not entirely sure this is the right way to support the header
	 * command in the Apache module.  Comments?
	 */
	r = strchr(header_information, ':');
	if (r) {
		*r = '\0';
		if (!strcasecmp(header_information, "Content-type")) {
			if (*(r + 1) == ' ')
				req->content_type = pstrdup(req->pool,r + 2);
			else
				req->content_type = pstrdup(req->pool,r + 1);
			cont_type = (char *) req->content_type;
		} else {
			if (*(r + 1) == ' ') {
				rr = r + 2;
			} else {
				rr = r + 1;
			}
			if (PG(safe_mode) && (!strcasecmp(header_information, "WWW-authenticate"))) {
				myuid = _php3_getuid();
				sprintf(temp2, "realm=\"%ld ", myuid);  /* SAFE */
				temp = _php3_regreplace("realm=\"", temp2, rr, 1, 0);
				if (!strcmp(temp, rr)) {
					sprintf(temp2, "realm=%ld", myuid);  /* SAFE */
					temp = _php3_regreplace("realm=", temp2, rr, 1, 0);
					if (!strcmp(temp, rr)) {
						sprintf(temp2, " realm=%ld", myuid);  /* SAFE */
						temp = _php3_regreplace("$", temp2, rr, 0, 0);
					}
				}
				table_set(req->headers_out, header_information, temp);
			} else
				table_set(req->headers_out, header_information, rr);
		}
		if (!strcasecmp(header_information, "location")) {
			req->status = REDIRECT;
		}
		*r = ':';
		php3_HeaderPrinted = 2;
	}
	if (!strncasecmp(header_information, "http/", 5)) {
		if (strlen(header_information) > 9) {
			req->status = atoi(&((header_information)[9]));
		}
		/* Use a pstrdup here to get the memory straight from Apache's per-request pool to
		 * avoid having our own memory manager complain about this memory not being freed
		 * because it really shouldn't be freed until the end of the request and it isn't
		 * easy for us to figure out when we allocated it vs. when something else might have.
		 */
		req->status_line = pstrdup(req->pool,&((header_information)[9]));
	}
#else
	r = strchr(header_information, ':');
	if (r) {
		*r = '\0';
		if (!strcasecmp(header_information, "Content-type")) {
			if (cont_type) efree(cont_type);
			cont_type = estrdup(r + 1);
#if 0 /*WIN32|WINNT / *M$ does us again*/
			if (!strcmp(cont_type," text/html")){
				*r=':';
				PUTS_H(header_information);
				PUTS_H("\015\012");
			}
#endif
		} else {
			*r = ':';
#if USE_SAPI
			{
				char *tempstr=emalloc(strlen(header_information)+2);
				
				sprintf(tempstr,"%s\015\012",tempstr);
				sapi_rqst->header(sapi_rqst->scid,tempstr);
				efree(tempstr);
			}
#elif FHTTPD
            php3_fhttpd_puts_header(header_information);
            php3_fhttpd_puts_header("\r\n");
#else
			PUTS_H(header_information);
			PUTS_H("\015\012");
#endif
		}
	} else {
#if USE_SAPI
		{
		char *tempstr=emalloc(strlen(header_information)+2);
		sprintf(tempstr,"%s\015\012",tempstr);
		sapi_rqst->header(sapi_rqst->scid,tempstr);
		efree(tempstr);
		}
#elif FHTTPD
        php3_fhttpd_puts_header(header_information);
        php3_fhttpd_puts_header("\r\n");
#else
		PUTS_H(header_information);
		PUTS_H("\015\012");
#endif
	}
#endif
}
#else
void php4i_add_header_information(char *header_information, uint header_length)
{
	sapi_add_header(header_information, header_length);
}
#endif


/* Implementation of the language Header() function */
void php3_Header(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *arg1;

	if (getParameters(ht, 1, &arg1) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	convert_to_string(arg1);
	php4i_add_header_information(arg1->value.str.val, arg1->value.str.len);
	arg1->type = IS_LONG; /* change arg1's type so that it doesn't get freed */
	arg1->value.lval = 0;
}





#if 0
/*
 * php3_header() flushes the header info built up using calls to
 * the Header() function.  If type is 1, a redirect to str is done.
 * Otherwise type should be 0 and str NULL.
 *
 * The function returns non-zero if output is allowed after the
 * call, and zero otherwise.  Any call to php3_header() must check
 * the return status and if false, no output must be sent.  This
 * is in order to correctly handle HEAD requests.
 */
PHPAPI int php3_header(void)
{
#if APACHE
	CookieList *cookie;
	int len = 0;
	time_t t;
	char *dt, *cookievalue = NULL;
#endif
#if APACHE || defined(USE_SAPI) || FHTTPD
	char *tempstr;
#endif
	PLS_FETCH();
	SLS_FETCH();

	if (PG(header_is_being_sent)) {
		return 0;
	} else {
		PG(header_is_being_sent) = 1;
	}

#if APACHE
	if (!((request_rec *) SG(server_context))) {  /* we're not in a request, allow output */
		PG(header_is_being_sent) = 0;
		return 1;
	}
	if ((php3_PrintHeader && !php3_HeaderPrinted) || (php3_PrintHeader && php3_HeaderPrinted == 2)) {
		cookie = php3_PopCookieList();
		while (cookie) {
			if (cookie->name)
				len += strlen(cookie->name);
                      if (cookie->value) {
                              cookievalue = _php3_urlencode(cookie->value, strlen (cookie->value));
                              len += strlen(cookievalue);
                      }
			if (cookie->path)
				len += strlen(cookie->path);
			if (cookie->domain)
				len += strlen(cookie->domain);
			tempstr = emalloc(len + 100);
			if (!cookie->value || (cookie->value && !*cookie->value)) {
				/* 
				 * MSIE doesn't delete a cookie when you set it to a null value
				 * so in order to force cookies to be deleted, even on MSIE, we
				 * pick an expiry date 1 year and 1 second in the past
				 */
				sprintf(tempstr, "%s=deleted", cookie->name);
				t = time(NULL) - 31536001;
				strcat(tempstr, "; expires=");
				dt = php3_std_date(t);
				strcat(tempstr, dt);
				efree(dt);
			} else {
				/* FIXME: XXX: this is not binary data safe */
                              sprintf(tempstr, "%s=%s", cookie->name, cookie->value ? cookievalue : "");
				if (cookie->name) efree(cookie->name);
				if (cookie->value) efree(cookie->value);
                              if (cookievalue) efree(cookievalue);
				cookie->name=NULL;
				cookie->value=NULL;
                              cookievalue=NULL;
				if (cookie->expires > 0) {
					strcat(tempstr, "; expires=");
					dt = php3_std_date(cookie->expires);
					strcat(tempstr, dt);
					efree(dt);
				}
			}
			if (cookie->path && strlen(cookie->path)) {
				strcat(tempstr, "; path=");
				strcat(tempstr, cookie->path);
				efree(cookie->path);
				cookie->path=NULL;
			}
			if (cookie->domain && strlen(cookie->domain)) {
				strcat(tempstr, "; domain=");
				strcat(tempstr, cookie->domain);
				efree(cookie->domain);
				cookie->domain=NULL;
			}
			if (cookie->secure) {
				strcat(tempstr, "; secure");
			}
			table_add(((request_rec *) SG(server_context))->headers_out, "Set-Cookie", tempstr);
			if (cookie->domain) efree(cookie->domain);
			if (cookie->path) efree(cookie->path);
			if (cookie->name) efree(cookie->name);
			if (cookie->value) efree(cookie->value);
                      if (cookievalue) efree(cookievalue);
			efree(cookie);
			cookie = php3_PopCookieList();
			efree(tempstr);
		}
		php3_HeaderPrinted = 1;
		header_called = 1;
		send_http_header(((request_rec *) SG(server_context)));
		if (((request_rec *) SG(server_context))->header_only) {
			set_header_request(1);
			PG(header_is_being_sent) = 0;
			return(0);
		}
	}
#else
	if (php3_PrintHeader && !php3_HeaderPrinted) {
		if (!cont_type) {
#if USE_SAPI
			sapi_rqst->header(sapi_rqst->scid,"Content-type: text/html\015\012\015\012");
#elif FHTTPD
			php3_fhttpd_puts_header("Content-type: text/html\r\n");
#else
			PUTS_H("Content-type: text/html\015\012\015\012");
#endif
		} else {
#if 0 /*WIN32|WINNT / *M$ does us again*/
			if (!strcmp(cont_type,"text/html")){
#endif
#if USE_SAPI
			tempstr=emalloc(strlen(cont_type)+18);
			sprintf(tempstr,"Content-type: %s\015\012\015\012",cont_type);
			sapi_rqst->header(sapi_rqst->scid,tempstr);
			efree(tempstr);
#elif FHTTPD
			tempstr = emalloc(strlen(cont_type)
							  + sizeof("Content-type:") + 2);
			if(tempstr) {
				strcpy(tempstr, "Content-type:");
				strcpy(tempstr + sizeof("Content-type:") - 1,
					   cont_type);
				strcat(tempstr, "\r\n");
				php3_fhttpd_puts_header(tempstr);
				efree(tempstr);
			}
#else
			PUTS_H("Content-type:");
			PUTS_H(cont_type);
			PUTS_H("\015\012\015\012");
#endif
			efree(cont_type);
#if 0 /*WIN32|WINNT / *M$ does us again*/
			} else {
				PUTS_H("\015\012");
			}/*end excluding output of text/html*/
#endif
		}
#if USE_SAPI
		sapi_rqst->flush(sapi_rqst->scid);
#else
		fflush(stdout);
#endif
		php3_HeaderPrinted = 1;
		header_called = 1;
	}
#endif
	PG(header_is_being_sent) = 0;
	return(1);
}
#else
PHPAPI int php3_header()
{
	SLS_FETCH();

	if (sapi_send_headers()==FAILURE || SG(request_info).headers_only) {
		return 0; /* don't allow output */
	} else {
		return 1; /* allow output */
	}
}
#endif



void php3_PushCookieList(char *name, char *value, time_t expires, char *path, char *domain, int secure)
{
	CookieList *new;

	new = emalloc(sizeof(CookieList));
	new->next = top;
	new->name = name;
	new->value = value;
	new->expires = expires;
	new->path = path;
	new->domain = domain;
	new->secure = secure;
	top = new;
}

CookieList *php3_PopCookieList(void)
{
	CookieList *ret;

	ret = top;
	if (top)
		top = top->next;
	return (ret);
}

/* php3_SetCookie(name,value,expires,path,domain,secure) */
void php3_SetCookie(INTERNAL_FUNCTION_PARAMETERS)
{
	char *cookie;
	int len=sizeof("Set-Cookie: ");
	time_t t;
	char *r, *dt;
	char *name = NULL, *value = NULL, *path = NULL, *domain = NULL;
	time_t expires = 0;
	int secure = 0;
	pval *arg[6];
	int arg_count;

	arg_count = ARG_COUNT(ht);
	if (arg_count < 1 || arg_count > 6 || getParametersArray(ht, arg_count, arg) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	if (php3_HeaderPrinted == 1) {
		php3_error(E_WARNING, "Oops, php3_SetCookie called after header has been sent\n");
		return;
	}
	switch (arg_count) {
		case 6:
			convert_to_boolean(arg[5]);
			secure = arg[5]->value.lval;
			/* break missing intentionally */
		case 5:
			convert_to_string(arg[4]);
			domain = estrndup(arg[4]->value.str.val,arg[4]->value.str.len);
			/* break missing intentionally */
		case 4:
			convert_to_string(arg[3]);
			path = estrndup(arg[3]->value.str.val,arg[3]->value.str.len);
			/* break missing intentionally */
		case 3:
			convert_to_long(arg[2]);
			expires = arg[2]->value.lval;
			/* break missing intentionally */
		case 2:
			convert_to_string(arg[1]);
			value = estrndup(arg[1]->value.str.val,arg[1]->value.str.len);
			/* break missing intentionally */
		case 1:
			convert_to_string(arg[0]);
			name = estrndup(arg[0]->value.str.val,arg[0]->value.str.len);
			break;
	}
#if 0
	php3_PushCookieList(name, value, expires, path, domain, secure);
#else
	if (name) {
		len += strlen(name);
	}
	if (value) {
		len += strlen(value);
	}
	if (path) {
		len += strlen(path);
	}
	if (domain) {
		len += strlen(domain);
	}
	cookie = emalloc(len + 100);
	if (!value || (value && !*value)) {
		/* 
		 * MSIE doesn't delete a cookie when you set it to a null value
		 * so in order to force cookies to be deleted, even on MSIE, we
		 * pick an expiry date 1 year and 1 second in the past
		 */
		sprintf(cookie, "Set-Cookie: %s=deleted", name);
		strcat(cookie, "; expires=");
		t = time(NULL) - 31536001;
		dt = php3_std_date(t);
		strcat(cookie, dt);
		efree(dt);
	} else {
		/* FIXME: XXX: this is not binary data safe */
		r = _php3_urlencode(value, strlen (value));
		sprintf(cookie, "Set-Cookie: %s=%s", name, value ? r : "");
		if (r) efree(r);
		if (value) efree(value);
		value=NULL;
		if (name) efree(name);
		name=NULL;
		if (expires > 0) {
			strcat(cookie, "; expires=");
			dt = php3_std_date(expires);
			strcat(cookie, dt);
			efree(dt);
		}
	}
	if (path && strlen(path)) {
		strcat(cookie, "; path=");
		strcat(cookie, path);
		efree(path);
		path=NULL;
	}
	if (domain && strlen(domain)) {
		strcat(cookie, "; domain=");
		strcat(cookie, domain);
		efree(domain);
		domain=NULL;
	}
	if (secure) {
		strcat(cookie, "; secure");
	}


	if (sapi_add_header(cookie, strlen(cookie))==SUCCESS) {
		RETVAL_TRUE;
	} else {
		RETVAL_FALSE;
	}

	if (domain) {
		efree(domain);
	}
	if (path) {
		efree(path);
	}
	if (name) {
		efree(name);
	}
	if (value) {
		efree(value);
	}
#endif
}


int php3_headers_unsent(void)
{
	if (php3_HeaderPrinted!=1 || !php3_PrintHeader) {
		return 1;
	} else {
		return 0;
	}
}


function_entry php3_header_functions[] = {
	{NULL, NULL, NULL}
};


php3_module_entry php3_header_module_entry = {
	"PHP_head", php3_header_functions, NULL, NULL, php3_init_head, NULL, NULL, STANDARD_MODULE_PROPERTIES
};

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */