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 : | Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
16 : +----------------------------------------------------------------------+
17 : */
18 : /* $Id: head.c,v 1.84.2.1.2.7 2007/02/26 02:12:36 iliaa Exp $ */
19 :
20 : #include <stdio.h>
21 : #include "php.h"
22 : #include "ext/standard/php_standard.h"
23 : #include "ext/date/php_date.h"
24 : #include "SAPI.h"
25 : #include "php_main.h"
26 : #include "head.h"
27 : #ifdef TM_IN_SYS_TIME
28 : #include <sys/time.h>
29 : #else
30 : #include <time.h>
31 : #endif
32 :
33 : #include "php_globals.h"
34 : #include "safe_mode.h"
35 :
36 :
37 : /* Implementation of the language Header() function */
38 : /* {{{ proto void header(string header [, bool replace, [int http_response_code]])
39 : Sends a raw HTTP header */
40 : PHP_FUNCTION(header)
41 0 : {
42 0 : zend_bool rep = 1;
43 0 : sapi_header_line ctr = {0};
44 :
45 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &ctr.line,
46 : &ctr.line_len, &rep, &ctr.response_code) == FAILURE)
47 0 : return;
48 :
49 0 : sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr TSRMLS_CC);
50 : }
51 : /* }}} */
52 :
53 : PHPAPI int php_header(TSRMLS_D)
54 107 : {
55 107 : if (sapi_send_headers(TSRMLS_C)==FAILURE || SG(request_info).headers_only) {
56 0 : return 0; /* don't allow output */
57 : } else {
58 107 : return 1; /* allow output */
59 : }
60 : }
61 :
62 :
63 : PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC)
64 0 : {
65 0 : char *cookie, *encoded_value = NULL;
66 0 : int len=sizeof("Set-Cookie: ");
67 : char *dt;
68 0 : sapi_header_line ctr = {0};
69 : int result;
70 :
71 0 : if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
72 0 : zend_error( E_WARNING, "Cookie names can not contain any of the folllowing '=,; \\t\\r\\n\\013\\014' (%s)", name );
73 0 : return FAILURE;
74 : }
75 :
76 0 : if (!url_encode && value && strpbrk(value, ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
77 0 : zend_error( E_WARNING, "Cookie values can not contain any of the folllowing ',; \\t\\r\\n\\013\\014' (%s)", value );
78 0 : return FAILURE;
79 : }
80 :
81 0 : len += name_len;
82 0 : if (value && url_encode) {
83 : int encoded_value_len;
84 :
85 0 : encoded_value = php_url_encode(value, value_len, &encoded_value_len);
86 0 : len += encoded_value_len;
87 0 : } else if ( value ) {
88 0 : encoded_value = estrdup(value);
89 0 : len += value_len;
90 : }
91 0 : if (path) {
92 0 : len += path_len;
93 : }
94 0 : if (domain) {
95 0 : len += domain_len;
96 : }
97 :
98 0 : cookie = emalloc(len + 100);
99 :
100 0 : if (value && value_len == 0) {
101 : /*
102 : * MSIE doesn't delete a cookie when you set it to a null value
103 : * so in order to force cookies to be deleted, even on MSIE, we
104 : * pick an expiry date 1 year and 1 second in the past
105 : */
106 0 : time_t t = time(NULL) - 31536001;
107 0 : dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, t, 0 TSRMLS_CC);
108 0 : snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s", name, dt);
109 0 : efree(dt);
110 : } else {
111 0 : snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
112 0 : if (expires > 0) {
113 0 : strlcat(cookie, "; expires=", len + 100);
114 0 : dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0 TSRMLS_CC);
115 0 : strlcat(cookie, dt, len + 100);
116 0 : efree(dt);
117 : }
118 : }
119 :
120 0 : if (encoded_value) {
121 0 : efree(encoded_value);
122 : }
123 :
124 0 : if (path && path_len > 0) {
125 0 : strlcat(cookie, "; path=", len + 100);
126 0 : strlcat(cookie, path, len + 100);
127 : }
128 0 : if (domain && domain_len > 0) {
129 0 : strlcat(cookie, "; domain=", len + 100);
130 0 : strlcat(cookie, domain, len + 100);
131 : }
132 0 : if (secure) {
133 0 : strlcat(cookie, "; secure", len + 100);
134 : }
135 0 : if (httponly) {
136 0 : strlcat(cookie, "; httponly", len + 100);
137 : }
138 :
139 0 : ctr.line = cookie;
140 0 : ctr.line_len = strlen(cookie);
141 :
142 0 : result = sapi_header_op(SAPI_HEADER_ADD, &ctr TSRMLS_CC);
143 0 : efree(cookie);
144 0 : return result;
145 : }
146 :
147 :
148 : /* php_set_cookie(name, value, expires, path, domain, secure) */
149 : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
150 : Send a cookie */
151 : PHP_FUNCTION(setcookie)
152 0 : {
153 0 : char *name, *value = NULL, *path = NULL, *domain = NULL;
154 0 : long expires = 0;
155 0 : zend_bool secure = 0, httponly = 0;
156 0 : int name_len, value_len = 0, path_len = 0, domain_len = 0;
157 :
158 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
159 : &name_len, &value, &value_len, &expires, &path,
160 : &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
161 0 : return;
162 : }
163 :
164 0 : if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) {
165 0 : RETVAL_TRUE;
166 : } else {
167 0 : RETVAL_FALSE;
168 : }
169 : }
170 : /* }}} */
171 :
172 : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
173 : Send a cookie with no url encoding of the value */
174 : PHP_FUNCTION(setrawcookie)
175 0 : {
176 0 : char *name, *value = NULL, *path = NULL, *domain = NULL;
177 0 : long expires = 0;
178 0 : zend_bool secure = 0, httponly = 0;
179 0 : int name_len, value_len = 0, path_len = 0, domain_len = 0;
180 :
181 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
182 : &name_len, &value, &value_len, &expires, &path,
183 : &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
184 0 : return;
185 : }
186 :
187 0 : if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly TSRMLS_CC) == SUCCESS) {
188 0 : RETVAL_TRUE;
189 : } else {
190 0 : RETVAL_FALSE;
191 : }
192 : }
193 : /* }}} */
194 :
195 :
196 : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
197 : Returns true if headers have already been sent, false otherwise */
198 : PHP_FUNCTION(headers_sent)
199 0 : {
200 : zval *arg1, *arg2;
201 0 : char *file="";
202 0 : int line=0;
203 :
204 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &arg1, &arg2) == FAILURE)
205 0 : return;
206 :
207 0 : if (SG(headers_sent)) {
208 0 : line = php_get_output_start_lineno(TSRMLS_C);
209 0 : file = php_get_output_start_filename(TSRMLS_C);
210 : }
211 :
212 0 : switch(ZEND_NUM_ARGS()) {
213 : case 2:
214 0 : zval_dtor(arg2);
215 0 : ZVAL_LONG(arg2, line);
216 : case 1:
217 0 : zval_dtor(arg1);
218 0 : if (file) {
219 0 : ZVAL_STRING(arg1, file, 1);
220 : } else {
221 0 : ZVAL_STRING(arg1, "", 1);
222 : }
223 : break;
224 : }
225 :
226 0 : if (SG(headers_sent)) {
227 0 : RETURN_TRUE;
228 : } else {
229 0 : RETURN_FALSE;
230 : }
231 : }
232 : /* }}} */
233 :
234 : /* {{{ php_head_apply_header_list_to_hash
235 : Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
236 : static void php_head_apply_header_list_to_hash(void *data, void *arg TSRMLS_DC)
237 0 : {
238 0 : sapi_header_struct *sapi_header = (sapi_header_struct *)data;
239 :
240 0 : if (arg && sapi_header) {
241 0 : add_next_index_string((zval *)arg, (char *)(sapi_header->header), 1);
242 : }
243 0 : }
244 :
245 : /* {{{ proto array headers_list(void)
246 : Return list of headers to be sent / already sent */
247 : PHP_FUNCTION(headers_list)
248 0 : {
249 0 : if (ZEND_NUM_ARGS() > 0) {
250 0 : WRONG_PARAM_COUNT;
251 : }
252 :
253 : if (!&SG(sapi_headers).headers) {
254 : RETURN_FALSE;
255 : }
256 0 : array_init(return_value);
257 0 : zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value TSRMLS_CC);
258 : }
259 : /* }}} */
260 :
261 : /*
262 : * Local variables:
263 : * tab-width: 4
264 : * c-basic-offset: 4
265 : * vim600: sw=4 ts=4 fdm=marker
266 : * vim<600: sw=4 ts=4 * End:
267 : */
|