/src/php-src/ext/standard/head.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | http://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #include <stdio.h> |
18 | | #include "php.h" |
19 | | #include "ext/standard/php_standard.h" |
20 | | #include "ext/date/php_date.h" |
21 | | #include "SAPI.h" |
22 | | #include "php_main.h" |
23 | | #include "head.h" |
24 | | #include <time.h> |
25 | | |
26 | | #include "php_globals.h" |
27 | | #include "zend_smart_str.h" |
28 | | |
29 | | |
30 | | /* Implementation of the language Header() function */ |
31 | | /* {{{ proto void header(string header [, bool replace, [int http_response_code]]) |
32 | | Sends a raw HTTP header */ |
33 | | PHP_FUNCTION(header) |
34 | 0 | { |
35 | 0 | zend_bool rep = 1; |
36 | 0 | sapi_header_line ctr = {0}; |
37 | 0 | char *line; |
38 | 0 | size_t len; |
39 | |
|
40 | 0 | ZEND_PARSE_PARAMETERS_START(1, 3) |
41 | 0 | Z_PARAM_STRING(line, len) |
42 | 0 | Z_PARAM_OPTIONAL |
43 | 0 | Z_PARAM_BOOL(rep) |
44 | 0 | Z_PARAM_LONG(ctr.response_code) |
45 | 0 | ZEND_PARSE_PARAMETERS_END(); |
46 | |
|
47 | 0 | ctr.line = line; |
48 | 0 | ctr.line_len = (uint32_t)len; |
49 | 0 | sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr); |
50 | 0 | } |
51 | | /* }}} */ |
52 | | |
53 | | /* {{{ proto void header_remove([string name]) |
54 | | Removes an HTTP header previously set using header() */ |
55 | | PHP_FUNCTION(header_remove) |
56 | 0 | { |
57 | 0 | sapi_header_line ctr = {0}; |
58 | 0 | char *line = NULL; |
59 | 0 | size_t len = 0; |
60 | |
|
61 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
62 | 0 | Z_PARAM_OPTIONAL |
63 | 0 | Z_PARAM_STRING(line, len) |
64 | 0 | ZEND_PARSE_PARAMETERS_END(); |
65 | |
|
66 | 0 | ctr.line = line; |
67 | 0 | ctr.line_len = (uint32_t)len; |
68 | 0 | sapi_header_op(ZEND_NUM_ARGS() == 0 ? SAPI_HEADER_DELETE_ALL : SAPI_HEADER_DELETE, &ctr); |
69 | 0 | } |
70 | | /* }}} */ |
71 | | |
72 | | PHPAPI int php_header(void) |
73 | 0 | { |
74 | 0 | if (sapi_send_headers()==FAILURE || SG(request_info).headers_only) { |
75 | 0 | return 0; /* don't allow output */ |
76 | 0 | } else { |
77 | 0 | return 1; /* allow output */ |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | PHPAPI int php_setcookie(zend_string *name, zend_string *value, time_t expires, zend_string *path, zend_string *domain, int secure, int httponly, zend_string *samesite, int url_encode) |
82 | 0 | { |
83 | 0 | zend_string *dt; |
84 | 0 | sapi_header_line ctr = {0}; |
85 | 0 | int result; |
86 | 0 | smart_str buf = {0}; |
87 | |
|
88 | 0 | if (!ZSTR_LEN(name)) { |
89 | 0 | zend_error( E_WARNING, "Cookie names must not be empty" ); |
90 | 0 | return FAILURE; |
91 | 0 | } else if (strpbrk(ZSTR_VAL(name), "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */ |
92 | 0 | zend_error(E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" ); |
93 | 0 | return FAILURE; |
94 | 0 | } |
95 | | |
96 | 0 | if (!url_encode && value && |
97 | 0 | strpbrk(ZSTR_VAL(value), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */ |
98 | 0 | zend_error(E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" ); |
99 | 0 | return FAILURE; |
100 | 0 | } |
101 | | |
102 | 0 | if (path && strpbrk(ZSTR_VAL(path), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */ |
103 | 0 | zend_error(E_WARNING, "Cookie paths cannot contain any of the following ',; \\t\\r\\n\\013\\014'" ); |
104 | 0 | return FAILURE; |
105 | 0 | } |
106 | | |
107 | 0 | if (domain && strpbrk(ZSTR_VAL(domain), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */ |
108 | 0 | zend_error(E_WARNING, "Cookie domains cannot contain any of the following ',; \\t\\r\\n\\013\\014'" ); |
109 | 0 | return FAILURE; |
110 | 0 | } |
111 | | |
112 | 0 | if (value == NULL || ZSTR_LEN(value) == 0) { |
113 | | /* |
114 | | * MSIE doesn't delete a cookie when you set it to a null value |
115 | | * so in order to force cookies to be deleted, even on MSIE, we |
116 | | * pick an expiry date in the past |
117 | | */ |
118 | 0 | dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, 1, 0); |
119 | 0 | smart_str_appends(&buf, "Set-Cookie: "); |
120 | 0 | smart_str_append(&buf, name); |
121 | 0 | smart_str_appends(&buf, "=deleted; expires="); |
122 | 0 | smart_str_append(&buf, dt); |
123 | 0 | smart_str_appends(&buf, "; Max-Age=0"); |
124 | 0 | zend_string_free(dt); |
125 | 0 | } else { |
126 | 0 | smart_str_appends(&buf, "Set-Cookie: "); |
127 | 0 | smart_str_append(&buf, name); |
128 | 0 | smart_str_appendc(&buf, '='); |
129 | 0 | if (url_encode) { |
130 | 0 | zend_string *encoded_value = php_raw_url_encode(ZSTR_VAL(value), ZSTR_LEN(value)); |
131 | 0 | smart_str_append(&buf, encoded_value); |
132 | 0 | zend_string_release_ex(encoded_value, 0); |
133 | 0 | } else { |
134 | 0 | smart_str_append(&buf, value); |
135 | 0 | } |
136 | 0 | if (expires > 0) { |
137 | 0 | const char *p; |
138 | 0 | double diff; |
139 | |
|
140 | 0 | smart_str_appends(&buf, COOKIE_EXPIRES); |
141 | 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); |
142 | | /* check to make sure that the year does not exceed 4 digits in length */ |
143 | 0 | p = zend_memrchr(ZSTR_VAL(dt), '-', ZSTR_LEN(dt)); |
144 | 0 | if (!p || *(p + 5) != ' ') { |
145 | 0 | zend_string_free(dt); |
146 | 0 | smart_str_free(&buf); |
147 | 0 | zend_error(E_WARNING, "Expiry date cannot have a year greater than 9999"); |
148 | 0 | return FAILURE; |
149 | 0 | } |
150 | | |
151 | 0 | smart_str_append(&buf, dt); |
152 | 0 | zend_string_free(dt); |
153 | |
|
154 | 0 | diff = difftime(expires, php_time()); |
155 | 0 | if (diff < 0) { |
156 | 0 | diff = 0; |
157 | 0 | } |
158 | |
|
159 | 0 | smart_str_appends(&buf, COOKIE_MAX_AGE); |
160 | 0 | smart_str_append_long(&buf, (zend_long) diff); |
161 | 0 | } |
162 | 0 | } |
163 | |
|
164 | 0 | if (path && ZSTR_LEN(path)) { |
165 | 0 | smart_str_appends(&buf, COOKIE_PATH); |
166 | 0 | smart_str_append(&buf, path); |
167 | 0 | } |
168 | 0 | if (domain && ZSTR_LEN(domain)) { |
169 | 0 | smart_str_appends(&buf, COOKIE_DOMAIN); |
170 | 0 | smart_str_append(&buf, domain); |
171 | 0 | } |
172 | 0 | if (secure) { |
173 | 0 | smart_str_appends(&buf, COOKIE_SECURE); |
174 | 0 | } |
175 | 0 | if (httponly) { |
176 | 0 | smart_str_appends(&buf, COOKIE_HTTPONLY); |
177 | 0 | } |
178 | 0 | if (samesite && ZSTR_LEN(samesite)) { |
179 | 0 | smart_str_appends(&buf, COOKIE_SAMESITE); |
180 | 0 | smart_str_append(&buf, samesite); |
181 | 0 | } |
182 | |
|
183 | 0 | ctr.line = ZSTR_VAL(buf.s); |
184 | 0 | ctr.line_len = (uint32_t) ZSTR_LEN(buf.s); |
185 | |
|
186 | 0 | result = sapi_header_op(SAPI_HEADER_ADD, &ctr); |
187 | 0 | zend_string_release(buf.s); |
188 | 0 | return result; |
189 | 0 | } |
190 | | |
191 | 0 | static void php_head_parse_cookie_options_array(zval *options, zend_long *expires, zend_string **path, zend_string **domain, zend_bool *secure, zend_bool *httponly, zend_string **samesite) { |
192 | 0 | int found = 0; |
193 | 0 | zend_string *key; |
194 | 0 | zval *value; |
195 | |
|
196 | 0 | ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), key, value) { |
197 | 0 | if (key) { |
198 | 0 | if (zend_string_equals_literal_ci(key, "expires")) { |
199 | 0 | *expires = zval_get_long(value); |
200 | 0 | found++; |
201 | 0 | } else if (zend_string_equals_literal_ci(key, "path")) { |
202 | 0 | *path = zval_get_string(value); |
203 | 0 | found++; |
204 | 0 | } else if (zend_string_equals_literal_ci(key, "domain")) { |
205 | 0 | *domain = zval_get_string(value); |
206 | 0 | found++; |
207 | 0 | } else if (zend_string_equals_literal_ci(key, "secure")) { |
208 | 0 | *secure = zval_is_true(value); |
209 | 0 | found++; |
210 | 0 | } else if (zend_string_equals_literal_ci(key, "httponly")) { |
211 | 0 | *httponly = zval_is_true(value); |
212 | 0 | found++; |
213 | 0 | } else if (zend_string_equals_literal_ci(key, "samesite")) { |
214 | 0 | *samesite = zval_get_string(value); |
215 | 0 | found++; |
216 | 0 | } else { |
217 | 0 | php_error_docref(NULL, E_WARNING, "Unrecognized key '%s' found in the options array", ZSTR_VAL(key)); |
218 | 0 | } |
219 | 0 | } else { |
220 | 0 | php_error_docref(NULL, E_WARNING, "Numeric key found in the options array"); |
221 | 0 | } |
222 | 0 | } ZEND_HASH_FOREACH_END(); |
223 | | |
224 | | /* Array is not empty but no valid keys were found */ |
225 | 0 | if (found == 0 && zend_hash_num_elements(Z_ARRVAL_P(options)) > 0) { |
226 | 0 | php_error_docref(NULL, E_WARNING, "No valid options were found in the given array"); |
227 | 0 | } |
228 | 0 | } |
229 | | |
230 | | /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]]) |
231 | | setcookie(string name [, string value [, array options]]) |
232 | | Send a cookie */ |
233 | | PHP_FUNCTION(setcookie) |
234 | 0 | { |
235 | 0 | zval *expires_or_options = NULL; |
236 | 0 | zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL; |
237 | 0 | zend_long expires = 0; |
238 | 0 | zend_bool secure = 0, httponly = 0; |
239 | |
|
240 | 0 | ZEND_PARSE_PARAMETERS_START(1, 7) |
241 | 0 | Z_PARAM_STR(name) |
242 | 0 | Z_PARAM_OPTIONAL |
243 | 0 | Z_PARAM_STR(value) |
244 | 0 | Z_PARAM_ZVAL(expires_or_options) |
245 | 0 | Z_PARAM_STR(path) |
246 | 0 | Z_PARAM_STR(domain) |
247 | 0 | Z_PARAM_BOOL(secure) |
248 | 0 | Z_PARAM_BOOL(httponly) |
249 | 0 | ZEND_PARSE_PARAMETERS_END(); |
250 | |
|
251 | 0 | if (expires_or_options) { |
252 | 0 | if (Z_TYPE_P(expires_or_options) == IS_ARRAY) { |
253 | 0 | if (UNEXPECTED(ZEND_NUM_ARGS() > 3)) { |
254 | 0 | php_error_docref(NULL, E_WARNING, "Cannot pass arguments after the options array"); |
255 | 0 | RETURN_FALSE; |
256 | 0 | } |
257 | 0 | php_head_parse_cookie_options_array(expires_or_options, &expires, &path, &domain, &secure, &httponly, &samesite); |
258 | 0 | } else { |
259 | 0 | expires = zval_get_long(expires_or_options); |
260 | 0 | } |
261 | 0 | } |
262 | |
|
263 | 0 | if (!EG(exception)) { |
264 | 0 | if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, 1) == SUCCESS) { |
265 | 0 | RETVAL_TRUE; |
266 | 0 | } else { |
267 | 0 | RETVAL_FALSE; |
268 | 0 | } |
269 | 0 | } |
270 | |
|
271 | 0 | if (expires_or_options && Z_TYPE_P(expires_or_options) == IS_ARRAY) { |
272 | 0 | if (path) { |
273 | 0 | zend_string_release(path); |
274 | 0 | } |
275 | 0 | if (domain) { |
276 | 0 | zend_string_release(domain); |
277 | 0 | } |
278 | 0 | if (samesite) { |
279 | 0 | zend_string_release(samesite); |
280 | 0 | } |
281 | 0 | } |
282 | 0 | } |
283 | | /* }}} */ |
284 | | |
285 | | /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]]) |
286 | | setrawcookie(string name [, string value [, array options]]) |
287 | | Send a cookie with no url encoding of the value */ |
288 | | PHP_FUNCTION(setrawcookie) |
289 | 0 | { |
290 | 0 | zval *expires_or_options = NULL; |
291 | 0 | zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL; |
292 | 0 | zend_long expires = 0; |
293 | 0 | zend_bool secure = 0, httponly = 0; |
294 | |
|
295 | 0 | ZEND_PARSE_PARAMETERS_START(1, 7) |
296 | 0 | Z_PARAM_STR(name) |
297 | 0 | Z_PARAM_OPTIONAL |
298 | 0 | Z_PARAM_STR(value) |
299 | 0 | Z_PARAM_ZVAL(expires_or_options) |
300 | 0 | Z_PARAM_STR(path) |
301 | 0 | Z_PARAM_STR(domain) |
302 | 0 | Z_PARAM_BOOL(secure) |
303 | 0 | Z_PARAM_BOOL(httponly) |
304 | 0 | ZEND_PARSE_PARAMETERS_END(); |
305 | |
|
306 | 0 | if (expires_or_options) { |
307 | 0 | if (Z_TYPE_P(expires_or_options) == IS_ARRAY) { |
308 | 0 | if (UNEXPECTED(ZEND_NUM_ARGS() > 3)) { |
309 | 0 | php_error_docref(NULL, E_WARNING, "Cannot pass arguments after the options array"); |
310 | 0 | RETURN_FALSE; |
311 | 0 | } |
312 | 0 | php_head_parse_cookie_options_array(expires_or_options, &expires, &path, &domain, &secure, &httponly, &samesite); |
313 | 0 | } else { |
314 | 0 | expires = zval_get_long(expires_or_options); |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | if (!EG(exception)) { |
319 | 0 | if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, 0) == SUCCESS) { |
320 | 0 | RETVAL_TRUE; |
321 | 0 | } else { |
322 | 0 | RETVAL_FALSE; |
323 | 0 | } |
324 | 0 | } |
325 | |
|
326 | 0 | if (expires_or_options && Z_TYPE_P(expires_or_options) == IS_ARRAY) { |
327 | 0 | if (path) { |
328 | 0 | zend_string_release(path); |
329 | 0 | } |
330 | 0 | if (domain) { |
331 | 0 | zend_string_release(domain); |
332 | 0 | } |
333 | 0 | if (samesite) { |
334 | 0 | zend_string_release(samesite); |
335 | 0 | } |
336 | 0 | } |
337 | 0 | } |
338 | | /* }}} */ |
339 | | |
340 | | |
341 | | /* {{{ proto bool headers_sent([string &$file [, int &$line]]) |
342 | | Returns true if headers have already been sent, false otherwise */ |
343 | | PHP_FUNCTION(headers_sent) |
344 | 0 | { |
345 | 0 | zval *arg1 = NULL, *arg2 = NULL; |
346 | 0 | const char *file=""; |
347 | 0 | int line=0; |
348 | |
|
349 | 0 | ZEND_PARSE_PARAMETERS_START(0, 2) |
350 | 0 | Z_PARAM_OPTIONAL |
351 | 0 | Z_PARAM_ZVAL(arg1) |
352 | 0 | Z_PARAM_ZVAL(arg2) |
353 | 0 | ZEND_PARSE_PARAMETERS_END(); |
354 | |
|
355 | 0 | if (SG(headers_sent)) { |
356 | 0 | line = php_output_get_start_lineno(); |
357 | 0 | file = php_output_get_start_filename(); |
358 | 0 | } |
359 | |
|
360 | 0 | switch(ZEND_NUM_ARGS()) { |
361 | 0 | case 2: |
362 | 0 | ZEND_TRY_ASSIGN_REF_LONG(arg2, line); |
363 | 0 | case 1: |
364 | 0 | if (file) { |
365 | 0 | ZEND_TRY_ASSIGN_REF_STRING(arg1, file); |
366 | 0 | } else { |
367 | 0 | ZEND_TRY_ASSIGN_REF_EMPTY_STRING(arg1); |
368 | 0 | } |
369 | 0 | break; |
370 | 0 | } |
371 | | |
372 | 0 | if (SG(headers_sent)) { |
373 | 0 | RETURN_TRUE; |
374 | 0 | } else { |
375 | 0 | RETURN_FALSE; |
376 | 0 | } |
377 | 0 | } |
378 | | /* }}} */ |
379 | | |
380 | | /* {{{ php_head_apply_header_list_to_hash |
381 | | Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */ |
382 | | static void php_head_apply_header_list_to_hash(void *data, void *arg) |
383 | 0 | { |
384 | 0 | sapi_header_struct *sapi_header = (sapi_header_struct *)data; |
385 | |
|
386 | 0 | if (arg && sapi_header) { |
387 | 0 | add_next_index_string((zval *)arg, (char *)(sapi_header->header)); |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | /* {{{ proto array headers_list(void) |
392 | | Return list of headers to be sent / already sent */ |
393 | | PHP_FUNCTION(headers_list) |
394 | 0 | { |
395 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
396 | |
|
397 | 0 | array_init(return_value); |
398 | 0 | zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value); |
399 | 0 | } |
400 | | /* }}} */ |
401 | | |
402 | | /* {{{ proto int http_response_code([int response_code]) |
403 | | Sets a response code, or returns the current HTTP response code */ |
404 | | PHP_FUNCTION(http_response_code) |
405 | 0 | { |
406 | 0 | zend_long response_code = 0; |
407 | |
|
408 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
409 | 0 | Z_PARAM_OPTIONAL |
410 | 0 | Z_PARAM_LONG(response_code) |
411 | 0 | ZEND_PARSE_PARAMETERS_END(); |
412 | |
|
413 | 0 | if (response_code) |
414 | 0 | { |
415 | 0 | zend_long old_response_code; |
416 | |
|
417 | 0 | old_response_code = SG(sapi_headers).http_response_code; |
418 | 0 | SG(sapi_headers).http_response_code = (int)response_code; |
419 | |
|
420 | 0 | if (old_response_code) { |
421 | 0 | RETURN_LONG(old_response_code); |
422 | 0 | } |
423 | |
|
424 | 0 | RETURN_TRUE; |
425 | 0 | } |
426 | |
|
427 | 0 | if (!SG(sapi_headers).http_response_code) { |
428 | 0 | RETURN_FALSE; |
429 | 0 | } |
430 | |
|
431 | 0 | RETURN_LONG(SG(sapi_headers).http_response_code); |
432 | 0 | } |
433 | | /* }}} */ |