Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,16 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_setrawcookie, 0, 0, 1)
ZEND_ARG_INFO(0, httponly)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_http_cookie_set, 0, 0, 2)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_http_cookie_remove, 0)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_headers_sent, 0, 0, 0)
ZEND_ARG_INFO(1, file)
ZEND_ARG_INFO(1, line)
Expand Down Expand Up @@ -3034,6 +3044,8 @@ const zend_function_entry basic_functions[] = { /* {{{ */

PHP_FE(setcookie, arginfo_setcookie)
PHP_FE(setrawcookie, arginfo_setrawcookie)
PHP_FE(http_cookie_set, arginfo_http_cookie_set)
PHP_FE(http_cookie_remove, arginfo_http_cookie_remove)
PHP_FE(header, arginfo_header)
PHP_FE(header_remove, arginfo_header_remove)
PHP_FE(headers_sent, arginfo_headers_sent)
Expand Down Expand Up @@ -3620,6 +3632,10 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
REGISTER_LONG_CONSTANT("PHP_QUERY_RFC1738", PHP_QUERY_RFC1738, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PHP_QUERY_RFC3986", PHP_QUERY_RFC3986, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("HTTP_COOKIE_ENCODE_NONE", HTTP_COOKIE_ENCODE_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("HTTP_COOKIE_ENCODE_RFC1738", HTTP_COOKIE_ENCODE_RFC1738, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("HTTP_COOKIE_ENCODE_RFC3986", HTTP_COOKIE_ENCODE_RFC3986, CONST_CS | CONST_PERSISTENT);

#define REGISTER_MATH_CONSTANT(x) REGISTER_DOUBLE_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
REGISTER_MATH_CONSTANT(M_E);
REGISTER_MATH_CONSTANT(M_LOG2E);
Expand Down
114 changes: 100 additions & 14 deletions ext/standard/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,40 @@ PHPAPI int php_setcookie(zend_string *name, zend_string *value, time_t expires,
if (!ZSTR_LEN(name)) {
zend_error( E_WARNING, "Cookie names must not be empty" );
return FAILURE;
} else if (strpbrk(ZSTR_VAL(name), "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
zend_error(E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
return FAILURE;
}

if (!url_encode && value &&
strpbrk(ZSTR_VAL(value), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
zend_error(E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
/* man isspace for \013 and \014 */
if (strpbrk(ZSTR_VAL(name), "=,; \t\r\n\013\014") != NULL) {
zend_error(E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
return FAILURE;
}

len += ZSTR_LEN(name);
if (value) {
if (url_encode) {
encoded_value = php_url_encode(ZSTR_VAL(value), ZSTR_LEN(value));
len += ZSTR_LEN(encoded_value);
} else {
encoded_value = zend_string_copy(value);
len += ZSTR_LEN(encoded_value);
switch(url_encode) {
case HTTP_COOKIE_ENCODE_NONE:
/* man isspace for \013 and \014 */
if (strpbrk(ZSTR_VAL(value), ",; \t\r\n\013\014") != NULL) {
zend_error(E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
return FAILURE;
}
encoded_value = zend_string_copy(value);
break;

case HTTP_COOKIE_ENCODE_RFC1738:
encoded_value = php_url_encode(ZSTR_VAL(value), ZSTR_LEN(value));
break;

case HTTP_COOKIE_ENCODE_RFC3986:
encoded_value = php_raw_url_encode(ZSTR_VAL(value), ZSTR_LEN(value));
break;

default: {
zend_error(E_WARNING, "Invalid cookie encoding.");
return FAILURE;
}
}
len += ZSTR_LEN(encoded_value);
}

if (path) {
Expand Down Expand Up @@ -212,7 +226,7 @@ PHP_FUNCTION(setcookie)
Z_PARAM_BOOL(httponly)
ZEND_PARSE_PARAMETERS_END();

if (php_setcookie(name, value, expires, path, domain, secure, 1, httponly) == SUCCESS) {
if (php_setcookie(name, value, expires, path, domain, secure, HTTP_COOKIE_ENCODE_RFC1738, httponly) == SUCCESS) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
Expand All @@ -239,14 +253,86 @@ PHP_FUNCTION(setrawcookie)
Z_PARAM_BOOL(httponly)
ZEND_PARSE_PARAMETERS_END();

if (php_setcookie(name, value, expires, path, domain, secure, 0, httponly) == SUCCESS) {
if (php_setcookie(name, value, expires, path, domain, secure, HTTP_COOKIE_ENCODE_NONE, httponly) == SUCCESS) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
}
}
/* }}} */

/* http_cookie_set(name, value, options) */
/* {{{ proto bool http_cookie_set(string name, string value, [array options])
Send a cookie */
PHP_FUNCTION(http_cookie_set)
{
zend_string *name, *value = NULL, *path = NULL, *domain = NULL;
zend_long expires = 0;
zend_bool secure = 0, httponly = 0;
int encode = HTTP_COOKIE_ENCODE_RFC1738;
HashTable *options;

zend_ulong option_index;
zend_string *option_key;
zval *option_value;

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(name)
Z_PARAM_STR(value)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_OBJECT_HT(options)
ZEND_PARSE_PARAMETERS_END();

ZEND_HASH_FOREACH_KEY_VAL(options, option_index, option_key, option_value) {
if(strcmp("expires", ZSTR_VAL(option_key)) == 0) {
expires = zval_get_long(option_value);
}
else if(strcmp("path", ZSTR_VAL(option_key)) == 0) {
path = zval_get_string(option_value);
}
else if(strcmp("domain", ZSTR_VAL(option_key)) == 0) {
domain = zval_get_string(option_value);
}
else if(strcmp("secure", ZSTR_VAL(option_key)) == 0) {
secure = zval_get_long(option_value);
}
else if(strcmp("httponly", ZSTR_VAL(option_key)) == 0) {
httponly = zval_get_long(option_value);
}
else if(strcmp("encode", ZSTR_VAL(option_key)) == 0) {
encode = zval_get_long(option_value);
}
else {
php_error_docref(NULL, E_WARNING, "Ignore unsupported option '%s'.", ZSTR_VAL(option_key));
}
} ZEND_HASH_FOREACH_END();

if (php_setcookie(name, value, expires, path, domain, secure, encode, httponly) == SUCCESS) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
}
}
/* }}} */

/* http_cookie_remove(name) */
/* {{{ proto bool http_cookie_remove(string name)
Remove a cookie */
PHP_FUNCTION(http_cookie_remove)
{
zend_string *name;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(name)
ZEND_PARSE_PARAMETERS_END();

if (php_setcookie(name, NULL, 0, NULL, NULL, 0, HTTP_COOKIE_ENCODE_NONE, 0) == SUCCESS) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
}
}
/* }}} */

/* {{{ proto bool headers_sent([string &$file [, int &$line]])
Returns true if headers have already been sent, false otherwise */
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/head.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@
#define COOKIE_SECURE "; secure"
#define COOKIE_HTTPONLY "; HttpOnly"

#define HTTP_COOKIE_ENCODE_NONE 0
#define HTTP_COOKIE_ENCODE_RFC1738 1
#define HTTP_COOKIE_ENCODE_RFC3986 2

extern PHP_RINIT_FUNCTION(head);
PHP_FUNCTION(header);
PHP_FUNCTION(header_remove);
PHP_FUNCTION(setcookie);
PHP_FUNCTION(setrawcookie);
PHP_FUNCTION(http_cookie_set);
PHP_FUNCTION(http_cookie_remove);
PHP_FUNCTION(headers_sent);
PHP_FUNCTION(headers_list);
PHP_FUNCTION(http_response_code);
Expand Down