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