head.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. | https://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. #include <stdio.h>
  17. #include "php.h"
  18. #include "ext/standard/php_standard.h"
  19. #include "ext/date/php_date.h"
  20. #include "SAPI.h"
  21. #include "php_main.h"
  22. #include "head.h"
  23. #include <time.h>
  24. #include "php_globals.h"
  25. #include "zend_smart_str.h"
  26. /* Implementation of the language Header() function */
  27. /* {{{ Sends a raw HTTP header */
  28. PHP_FUNCTION(header)
  29. {
  30. bool rep = 1;
  31. sapi_header_line ctr = {0};
  32. char *line;
  33. size_t len;
  34. ZEND_PARSE_PARAMETERS_START(1, 3)
  35. Z_PARAM_STRING(line, len)
  36. Z_PARAM_OPTIONAL
  37. Z_PARAM_BOOL(rep)
  38. Z_PARAM_LONG(ctr.response_code)
  39. ZEND_PARSE_PARAMETERS_END();
  40. ctr.line = line;
  41. ctr.line_len = (uint32_t)len;
  42. sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr);
  43. }
  44. /* }}} */
  45. /* {{{ Removes an HTTP header previously set using header() */
  46. PHP_FUNCTION(header_remove)
  47. {
  48. sapi_header_line ctr = {0};
  49. char *line = NULL;
  50. size_t len = 0;
  51. ZEND_PARSE_PARAMETERS_START(0, 1)
  52. Z_PARAM_OPTIONAL
  53. Z_PARAM_STRING_OR_NULL(line, len)
  54. ZEND_PARSE_PARAMETERS_END();
  55. ctr.line = line;
  56. ctr.line_len = (uint32_t)len;
  57. sapi_header_op(line == NULL ? SAPI_HEADER_DELETE_ALL : SAPI_HEADER_DELETE, &ctr);
  58. }
  59. /* }}} */
  60. PHPAPI int php_header(void)
  61. {
  62. if (sapi_send_headers()==FAILURE || SG(request_info).headers_only) {
  63. return 0; /* don't allow output */
  64. } else {
  65. return 1; /* allow output */
  66. }
  67. }
  68. #define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", or \"\\014\""
  69. PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t expires,
  70. zend_string *path, zend_string *domain, bool secure, bool httponly,
  71. zend_string *samesite, bool url_encode)
  72. {
  73. zend_string *dt;
  74. sapi_header_line ctr = {0};
  75. zend_result result;
  76. smart_str buf = {0};
  77. if (!ZSTR_LEN(name)) {
  78. zend_argument_value_error(1, "cannot be empty");
  79. return FAILURE;
  80. }
  81. if (strpbrk(ZSTR_VAL(name), "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
  82. zend_argument_value_error(1, "cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER);
  83. return FAILURE;
  84. }
  85. if (!url_encode && value &&
  86. strpbrk(ZSTR_VAL(value), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
  87. zend_argument_value_error(2, "cannot contain " ILLEGAL_COOKIE_CHARACTER);
  88. return FAILURE;
  89. }
  90. if (path && strpbrk(ZSTR_VAL(path), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
  91. zend_value_error("%s(): \"path\" option cannot contain " ILLEGAL_COOKIE_CHARACTER,
  92. get_active_function_name());
  93. return FAILURE;
  94. }
  95. if (domain && strpbrk(ZSTR_VAL(domain), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
  96. zend_value_error("%s(): \"domain\" option cannot contain " ILLEGAL_COOKIE_CHARACTER,
  97. get_active_function_name());
  98. return FAILURE;
  99. }
  100. /* Should check value of SameSite? */
  101. if (value == NULL || ZSTR_LEN(value) == 0) {
  102. /*
  103. * MSIE doesn't delete a cookie when you set it to a null value
  104. * so in order to force cookies to be deleted, even on MSIE, we
  105. * pick an expiry date in the past
  106. */
  107. dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, 1, 0);
  108. smart_str_appends(&buf, "Set-Cookie: ");
  109. smart_str_append(&buf, name);
  110. smart_str_appends(&buf, "=deleted; expires=");
  111. smart_str_append(&buf, dt);
  112. smart_str_appends(&buf, "; Max-Age=0");
  113. zend_string_free(dt);
  114. } else {
  115. smart_str_appends(&buf, "Set-Cookie: ");
  116. smart_str_append(&buf, name);
  117. smart_str_appendc(&buf, '=');
  118. if (url_encode) {
  119. zend_string *encoded_value = php_raw_url_encode(ZSTR_VAL(value), ZSTR_LEN(value));
  120. smart_str_append(&buf, encoded_value);
  121. zend_string_release_ex(encoded_value, 0);
  122. } else {
  123. smart_str_append(&buf, value);
  124. }
  125. if (expires > 0) {
  126. const char *p;
  127. double diff;
  128. smart_str_appends(&buf, COOKIE_EXPIRES);
  129. dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0);
  130. /* check to make sure that the year does not exceed 4 digits in length */
  131. p = zend_memrchr(ZSTR_VAL(dt), '-', ZSTR_LEN(dt));
  132. if (!p || *(p + 5) != ' ') {
  133. zend_string_free(dt);
  134. smart_str_free(&buf);
  135. zend_value_error("%s(): \"expires\" option cannot have a year greater than 9999",
  136. get_active_function_name());
  137. return FAILURE;
  138. }
  139. smart_str_append(&buf, dt);
  140. zend_string_free(dt);
  141. diff = difftime(expires, php_time());
  142. if (diff < 0) {
  143. diff = 0;
  144. }
  145. smart_str_appends(&buf, COOKIE_MAX_AGE);
  146. smart_str_append_long(&buf, (zend_long) diff);
  147. }
  148. }
  149. if (path && ZSTR_LEN(path)) {
  150. smart_str_appends(&buf, COOKIE_PATH);
  151. smart_str_append(&buf, path);
  152. }
  153. if (domain && ZSTR_LEN(domain)) {
  154. smart_str_appends(&buf, COOKIE_DOMAIN);
  155. smart_str_append(&buf, domain);
  156. }
  157. if (secure) {
  158. smart_str_appends(&buf, COOKIE_SECURE);
  159. }
  160. if (httponly) {
  161. smart_str_appends(&buf, COOKIE_HTTPONLY);
  162. }
  163. if (samesite && ZSTR_LEN(samesite)) {
  164. smart_str_appends(&buf, COOKIE_SAMESITE);
  165. smart_str_append(&buf, samesite);
  166. }
  167. ctr.line = ZSTR_VAL(buf.s);
  168. ctr.line_len = (uint32_t) ZSTR_LEN(buf.s);
  169. result = sapi_header_op(SAPI_HEADER_ADD, &ctr);
  170. zend_string_release(buf.s);
  171. return result;
  172. }
  173. static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_long *expires, zend_string **path,
  174. zend_string **domain, bool *secure, bool *httponly, zend_string **samesite)
  175. {
  176. zend_string *key;
  177. zval *value;
  178. ZEND_HASH_FOREACH_STR_KEY_VAL(options, key, value) {
  179. if (!key) {
  180. zend_value_error("%s(): option array cannot have numeric keys", get_active_function_name());
  181. return FAILURE;
  182. }
  183. if (zend_string_equals_literal_ci(key, "expires")) {
  184. *expires = zval_get_long(value);
  185. } else if (zend_string_equals_literal_ci(key, "path")) {
  186. *path = zval_get_string(value);
  187. } else if (zend_string_equals_literal_ci(key, "domain")) {
  188. *domain = zval_get_string(value);
  189. } else if (zend_string_equals_literal_ci(key, "secure")) {
  190. *secure = zval_is_true(value);
  191. } else if (zend_string_equals_literal_ci(key, "httponly")) {
  192. *httponly = zval_is_true(value);
  193. } else if (zend_string_equals_literal_ci(key, "samesite")) {
  194. *samesite = zval_get_string(value);
  195. } else {
  196. zend_value_error("%s(): option \"%s\" is invalid", get_active_function_name(), ZSTR_VAL(key));
  197. return FAILURE;
  198. }
  199. } ZEND_HASH_FOREACH_END();
  200. return SUCCESS;
  201. }
  202. static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
  203. {
  204. HashTable *options = NULL;
  205. zend_long expires = 0;
  206. zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
  207. bool secure = 0, httponly = 0;
  208. ZEND_PARSE_PARAMETERS_START(1, 7)
  209. Z_PARAM_STR(name)
  210. Z_PARAM_OPTIONAL
  211. Z_PARAM_STR(value)
  212. Z_PARAM_ARRAY_HT_OR_LONG(options, expires)
  213. Z_PARAM_STR(path)
  214. Z_PARAM_STR(domain)
  215. Z_PARAM_BOOL(secure)
  216. Z_PARAM_BOOL(httponly)
  217. ZEND_PARSE_PARAMETERS_END();
  218. if (options) {
  219. if (UNEXPECTED(ZEND_NUM_ARGS() > 3)) {
  220. zend_argument_count_error("%s(): Expects exactly 3 arguments when argument #3 "
  221. "($expires_or_options) is an array", get_active_function_name());
  222. RETURN_THROWS();
  223. }
  224. if (FAILURE == php_head_parse_cookie_options_array(options, &expires, &path,
  225. &domain, &secure, &httponly, &samesite)
  226. ) {
  227. goto cleanup;
  228. }
  229. }
  230. if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, !is_raw) == SUCCESS) {
  231. RETVAL_TRUE;
  232. } else {
  233. RETVAL_FALSE;
  234. }
  235. if (options) {
  236. cleanup:
  237. if (path) {
  238. zend_string_release(path);
  239. }
  240. if (domain) {
  241. zend_string_release(domain);
  242. }
  243. if (samesite) {
  244. zend_string_release(samesite);
  245. }
  246. }
  247. }
  248. /* {{{ setcookie(string name [, string value [, array options]])
  249. Send a cookie */
  250. PHP_FUNCTION(setcookie)
  251. {
  252. php_setcookie_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
  253. }
  254. /* }}} */
  255. /* {{{ setrawcookie(string name [, string value [, array options]])
  256. Send a cookie with no url encoding of the value */
  257. PHP_FUNCTION(setrawcookie)
  258. {
  259. php_setcookie_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
  260. }
  261. /* }}} */
  262. /* {{{ Returns true if headers have already been sent, false otherwise */
  263. PHP_FUNCTION(headers_sent)
  264. {
  265. zval *arg1 = NULL, *arg2 = NULL;
  266. const char *file="";
  267. int line=0;
  268. ZEND_PARSE_PARAMETERS_START(0, 2)
  269. Z_PARAM_OPTIONAL
  270. Z_PARAM_ZVAL(arg1)
  271. Z_PARAM_ZVAL(arg2)
  272. ZEND_PARSE_PARAMETERS_END();
  273. if (SG(headers_sent)) {
  274. line = php_output_get_start_lineno();
  275. file = php_output_get_start_filename();
  276. }
  277. switch(ZEND_NUM_ARGS()) {
  278. case 2:
  279. ZEND_TRY_ASSIGN_REF_LONG(arg2, line);
  280. ZEND_FALLTHROUGH;
  281. case 1:
  282. if (file) {
  283. ZEND_TRY_ASSIGN_REF_STRING(arg1, file);
  284. } else {
  285. ZEND_TRY_ASSIGN_REF_EMPTY_STRING(arg1);
  286. }
  287. break;
  288. }
  289. if (SG(headers_sent)) {
  290. RETURN_TRUE;
  291. } else {
  292. RETURN_FALSE;
  293. }
  294. }
  295. /* }}} */
  296. /* {{{ php_head_apply_header_list_to_hash
  297. Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
  298. static void php_head_apply_header_list_to_hash(void *data, void *arg)
  299. {
  300. sapi_header_struct *sapi_header = (sapi_header_struct *)data;
  301. if (arg && sapi_header) {
  302. add_next_index_string((zval *)arg, (char *)(sapi_header->header));
  303. }
  304. }
  305. /* {{{ Return list of headers to be sent / already sent */
  306. PHP_FUNCTION(headers_list)
  307. {
  308. ZEND_PARSE_PARAMETERS_NONE();
  309. array_init(return_value);
  310. zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value);
  311. }
  312. /* }}} */
  313. /* {{{ Sets a response code, or returns the current HTTP response code */
  314. PHP_FUNCTION(http_response_code)
  315. {
  316. zend_long response_code = 0;
  317. ZEND_PARSE_PARAMETERS_START(0, 1)
  318. Z_PARAM_OPTIONAL
  319. Z_PARAM_LONG(response_code)
  320. ZEND_PARSE_PARAMETERS_END();
  321. if (response_code)
  322. {
  323. zend_long old_response_code;
  324. old_response_code = SG(sapi_headers).http_response_code;
  325. SG(sapi_headers).http_response_code = (int)response_code;
  326. if (old_response_code) {
  327. RETURN_LONG(old_response_code);
  328. }
  329. RETURN_TRUE;
  330. }
  331. if (!SG(sapi_headers).http_response_code) {
  332. RETURN_FALSE;
  333. }
  334. RETURN_LONG(SG(sapi_headers).http_response_code);
  335. }
  336. /* }}} */