1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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 286508 2009-07-29 13:44:16Z iliaa $ */
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 28 : {
42 28 : zend_bool rep = 1;
43 28 : sapi_header_line ctr = {0};
44 :
45 28 : 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 28 : sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr TSRMLS_CC);
50 : }
51 : /* }}} */
52 :
53 : /* {{{ proto void header_remove([string name])
54 : Removes an HTTP header previously set using header() */
55 : PHP_FUNCTION(header_remove)
56 9 : {
57 9 : sapi_header_line ctr = {0};
58 :
59 9 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ctr.line,
60 : &ctr.line_len) == FAILURE)
61 0 : return;
62 :
63 9 : sapi_header_op(ZEND_NUM_ARGS() == 0 ? SAPI_HEADER_DELETE_ALL : SAPI_HEADER_DELETE, &ctr TSRMLS_CC);
64 : }
65 : /* }}} */
66 :
67 : PHPAPI int php_header(TSRMLS_D)
68 11516 : {
69 11516 : if (sapi_send_headers(TSRMLS_C)==FAILURE || SG(request_info).headers_only) {
70 0 : return 0; /* don't allow output */
71 : } else {
72 11516 : return 1; /* allow output */
73 : }
74 : }
75 :
76 :
77 : 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)
78 0 : {
79 0 : char *cookie, *encoded_value = NULL;
80 0 : int len=sizeof("Set-Cookie: ");
81 : char *dt;
82 0 : sapi_header_line ctr = {0};
83 : int result;
84 :
85 0 : if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
86 0 : zend_error( E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
87 0 : return FAILURE;
88 : }
89 :
90 0 : if (!url_encode && value && strpbrk(value, ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
91 0 : zend_error( E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
92 0 : return FAILURE;
93 : }
94 :
95 0 : len += name_len;
96 0 : if (value && url_encode) {
97 : int encoded_value_len;
98 :
99 0 : encoded_value = php_url_encode(value, value_len, &encoded_value_len);
100 0 : len += encoded_value_len;
101 0 : } else if ( value ) {
102 0 : encoded_value = estrdup(value);
103 0 : len += value_len;
104 : }
105 0 : if (path) {
106 0 : len += path_len;
107 : }
108 0 : if (domain) {
109 0 : len += domain_len;
110 : }
111 :
112 0 : cookie = emalloc(len + 100);
113 :
114 0 : if (value && value_len == 0) {
115 : /*
116 : * MSIE doesn't delete a cookie when you set it to a null value
117 : * so in order to force cookies to be deleted, even on MSIE, we
118 : * pick an expiry date 1 year and 1 second in the past
119 : */
120 0 : time_t t = time(NULL) - 31536001;
121 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);
122 0 : snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s", name, dt);
123 0 : efree(dt);
124 : } else {
125 0 : snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
126 0 : if (expires > 0) {
127 : char *p;
128 0 : strlcat(cookie, "; expires=", len + 100);
129 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);
130 : /* check to make sure that the year does not exceed 4 digits in length */
131 0 : p = zend_memrchr(dt, '-', strlen(dt));
132 0 : if (*(p + 5) != ' ') {
133 0 : efree(dt);
134 0 : efree(cookie);
135 0 : efree(encoded_value);
136 0 : zend_error(E_WARNING, "Expiry date cannot have a year greater then 9999");
137 0 : return FAILURE;
138 : }
139 0 : strlcat(cookie, dt, len + 100);
140 0 : efree(dt);
141 : }
142 : }
143 :
144 0 : if (encoded_value) {
145 0 : efree(encoded_value);
146 : }
147 :
148 0 : if (path && path_len > 0) {
149 0 : strlcat(cookie, "; path=", len + 100);
150 0 : strlcat(cookie, path, len + 100);
151 : }
152 0 : if (domain && domain_len > 0) {
153 0 : strlcat(cookie, "; domain=", len + 100);
154 0 : strlcat(cookie, domain, len + 100);
155 : }
156 0 : if (secure) {
157 0 : strlcat(cookie, "; secure", len + 100);
158 : }
159 0 : if (httponly) {
160 0 : strlcat(cookie, "; httponly", len + 100);
161 : }
162 :
163 0 : ctr.line = cookie;
164 0 : ctr.line_len = strlen(cookie);
165 :
166 0 : result = sapi_header_op(SAPI_HEADER_ADD, &ctr TSRMLS_CC);
167 0 : efree(cookie);
168 0 : return result;
169 : }
170 :
171 :
172 : /* php_set_cookie(name, value, expires, path, domain, secure) */
173 : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
174 : Send a cookie */
175 : PHP_FUNCTION(setcookie)
176 0 : {
177 0 : char *name, *value = NULL, *path = NULL, *domain = NULL;
178 0 : long expires = 0;
179 0 : zend_bool secure = 0, httponly = 0;
180 0 : int name_len, value_len = 0, path_len = 0, domain_len = 0;
181 :
182 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
183 : &name_len, &value, &value_len, &expires, &path,
184 : &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
185 0 : return;
186 : }
187 :
188 0 : if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) {
189 0 : RETVAL_TRUE;
190 : } else {
191 0 : RETVAL_FALSE;
192 : }
193 : }
194 : /* }}} */
195 :
196 : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
197 : Send a cookie with no url encoding of the value */
198 : PHP_FUNCTION(setrawcookie)
199 0 : {
200 0 : char *name, *value = NULL, *path = NULL, *domain = NULL;
201 0 : long expires = 0;
202 0 : zend_bool secure = 0, httponly = 0;
203 0 : int name_len, value_len = 0, path_len = 0, domain_len = 0;
204 :
205 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
206 : &name_len, &value, &value_len, &expires, &path,
207 : &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
208 0 : return;
209 : }
210 :
211 0 : if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly TSRMLS_CC) == SUCCESS) {
212 0 : RETVAL_TRUE;
213 : } else {
214 0 : RETVAL_FALSE;
215 : }
216 : }
217 : /* }}} */
218 :
219 :
220 : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
221 : Returns true if headers have already been sent, false otherwise */
222 : PHP_FUNCTION(headers_sent)
223 3 : {
224 3 : zval *arg1 = NULL, *arg2 = NULL;
225 3 : char *file="";
226 3 : int line=0;
227 :
228 3 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &arg1, &arg2) == FAILURE)
229 0 : return;
230 :
231 3 : if (SG(headers_sent)) {
232 2 : line = php_get_output_start_lineno(TSRMLS_C);
233 2 : file = php_get_output_start_filename(TSRMLS_C);
234 : }
235 :
236 3 : switch(ZEND_NUM_ARGS()) {
237 : case 2:
238 0 : zval_dtor(arg2);
239 0 : ZVAL_LONG(arg2, line);
240 : case 1:
241 0 : zval_dtor(arg1);
242 0 : if (file) {
243 0 : ZVAL_STRING(arg1, file, 1);
244 : } else {
245 0 : ZVAL_STRING(arg1, "", 1);
246 : }
247 : break;
248 : }
249 :
250 3 : if (SG(headers_sent)) {
251 2 : RETURN_TRUE;
252 : } else {
253 1 : RETURN_FALSE;
254 : }
255 : }
256 : /* }}} */
257 :
258 : /* {{{ php_head_apply_header_list_to_hash
259 : Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
260 : static void php_head_apply_header_list_to_hash(void *data, void *arg TSRMLS_DC)
261 0 : {
262 0 : sapi_header_struct *sapi_header = (sapi_header_struct *)data;
263 :
264 0 : if (arg && sapi_header) {
265 0 : add_next_index_string((zval *)arg, (char *)(sapi_header->header), 1);
266 : }
267 0 : }
268 :
269 : /* {{{ proto array headers_list(void)
270 : Return list of headers to be sent / already sent */
271 : PHP_FUNCTION(headers_list)
272 3 : {
273 3 : if (zend_parse_parameters_none() == FAILURE) {
274 0 : return;
275 : }
276 :
277 : if (!&SG(sapi_headers).headers) {
278 : RETURN_FALSE;
279 : }
280 3 : array_init(return_value);
281 3 : zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value TSRMLS_CC);
282 : }
283 : /* }}} */
284 :
285 : /*
286 : * Local variables:
287 : * tab-width: 4
288 : * c-basic-offset: 4
289 : * vim600: sw=4 ts=4 fdm=marker
290 : * vim<600: sw=4 ts=4 * End:
291 : */
|