@@ -83,30 +83,6 @@ PHPAPI int php_setcookie(zend_string *name, zend_string *value, time_t expires,
83
83
int result ;
84
84
smart_str buf = {0 };
85
85
86
- if (!ZSTR_LEN (name )) {
87
- zend_error ( E_WARNING , "Cookie names must not be empty" );
88
- return FAILURE ;
89
- } else if (strpbrk (ZSTR_VAL (name ), "=,; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
90
- zend_error (E_WARNING , "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
91
- return FAILURE ;
92
- }
93
-
94
- if (!url_encode && value &&
95
- strpbrk (ZSTR_VAL (value ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
96
- zend_error (E_WARNING , "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
97
- return FAILURE ;
98
- }
99
-
100
- if (path && strpbrk (ZSTR_VAL (path ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
101
- zend_error (E_WARNING , "Cookie paths cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
102
- return FAILURE ;
103
- }
104
-
105
- if (domain && strpbrk (ZSTR_VAL (domain ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
106
- zend_error (E_WARNING , "Cookie domains cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
107
- return FAILURE ;
108
- }
109
-
110
86
if (value == NULL || ZSTR_LEN (value ) == 0 ) {
111
87
/*
112
88
* MSIE doesn't delete a cookie when you set it to a null value
@@ -225,10 +201,10 @@ static void php_head_parse_cookie_options_array(zval *options, zend_long *expire
225
201
}
226
202
}
227
203
228
- /* {{{ setcookie(string name [, string value [, array options]])
229
- Send a cookie */
230
- PHP_FUNCTION (setcookie )
204
+ #define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", and \"\\014\""
205
+ static void php_setcookie_common (INTERNAL_FUNCTION_PARAMETERS , bool is_raw )
231
206
{
207
+ /* to handle overloaded function array|int */
232
208
zval * expires_or_options = NULL ;
233
209
zend_string * name , * value = NULL , * path = NULL , * domain = NULL , * samesite = NULL ;
234
210
zend_long expires = 0 ;
@@ -248,24 +224,61 @@ PHP_FUNCTION(setcookie)
248
224
if (expires_or_options ) {
249
225
if (Z_TYPE_P (expires_or_options ) == IS_ARRAY ) {
250
226
if (UNEXPECTED (ZEND_NUM_ARGS () > 3 )) {
251
- php_error_docref (NULL , E_WARNING , "Cannot pass arguments after the options array" );
252
- RETURN_FALSE ;
227
+ zend_argument_count_error ("%s(): Expects exactly 3 arguments when argument #3 "
228
+ "($expires_or_options) is an array" , get_active_function_name ());
229
+ RETURN_THROWS ();
253
230
}
254
231
php_head_parse_cookie_options_array (expires_or_options , & expires , & path , & domain , & secure , & httponly , & samesite );
232
+ if (path && strpbrk (ZSTR_VAL (path ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
233
+ zend_value_error ("%s(): Argument #3 ($expires_or_options[\"path\"]) cannot contain "
234
+ ILLEGAL_COOKIE_CHARACTER , get_active_function_name ());
235
+ goto cleanup ;
236
+ RETURN_THROWS ();
237
+ }
238
+ if (domain && strpbrk (ZSTR_VAL (domain ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
239
+ zend_value_error ("%s(): Argument #3 ($expires_or_options[\"domain\"]) cannot contain "
240
+ ILLEGAL_COOKIE_CHARACTER , get_active_function_name ());
241
+ goto cleanup ;
242
+ RETURN_THROWS ();
243
+ }
244
+ /* Should check value of SameSite? */
255
245
} else {
256
246
expires = zval_get_long (expires_or_options );
247
+ if (path && strpbrk (ZSTR_VAL (path ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
248
+ zend_argument_value_error (4 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
249
+ RETURN_THROWS ();
250
+ }
251
+ if (domain && strpbrk (ZSTR_VAL (domain ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
252
+ zend_argument_value_error (5 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
253
+ RETURN_THROWS ();
254
+ }
257
255
}
258
256
}
259
257
258
+ if (!ZSTR_LEN (name )) {
259
+ zend_argument_value_error (1 , "cannot be empty" );
260
+ RETURN_THROWS ();
261
+ }
262
+ if (strpbrk (ZSTR_VAL (name ), "=,; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
263
+ zend_argument_value_error (1 , "cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER );
264
+ RETURN_THROWS ();
265
+ }
266
+ if (is_raw && value &&
267
+ strpbrk (ZSTR_VAL (value ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
268
+ zend_argument_value_error (2 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
269
+ RETURN_THROWS ();
270
+ }
271
+
260
272
if (!EG (exception )) {
261
- if (php_setcookie (name , value , expires , path , domain , secure , httponly , samesite , 1 ) == SUCCESS ) {
273
+ if (php_setcookie (name , value , expires , path , domain , secure , httponly , samesite , ! is_raw ) == SUCCESS ) {
262
274
RETVAL_TRUE ;
263
275
} else {
264
276
RETVAL_FALSE ;
265
277
}
266
278
}
267
279
268
280
if (expires_or_options && Z_TYPE_P (expires_or_options ) == IS_ARRAY ) {
281
+ cleanup :
269
282
if (path ) {
270
283
zend_string_release (path );
271
284
}
@@ -277,59 +290,20 @@ PHP_FUNCTION(setcookie)
277
290
}
278
291
}
279
292
}
293
+
294
+ /* {{{ setcookie(string name [, string value [, array options]])
295
+ Send a cookie */
296
+ PHP_FUNCTION (setcookie )
297
+ {
298
+ php_setcookie_common (INTERNAL_FUNCTION_PARAM_PASSTHRU , false);
299
+ }
280
300
/* }}} */
281
301
282
302
/* {{{ setrawcookie(string name [, string value [, array options]])
283
303
Send a cookie with no url encoding of the value */
284
304
PHP_FUNCTION (setrawcookie )
285
305
{
286
- zval * expires_or_options = NULL ;
287
- zend_string * name , * value = NULL , * path = NULL , * domain = NULL , * samesite = NULL ;
288
- zend_long expires = 0 ;
289
- zend_bool secure = 0 , httponly = 0 ;
290
-
291
- ZEND_PARSE_PARAMETERS_START (1 , 7 )
292
- Z_PARAM_STR (name )
293
- Z_PARAM_OPTIONAL
294
- Z_PARAM_STR (value )
295
- Z_PARAM_ZVAL (expires_or_options )
296
- Z_PARAM_STR (path )
297
- Z_PARAM_STR (domain )
298
- Z_PARAM_BOOL (secure )
299
- Z_PARAM_BOOL (httponly )
300
- ZEND_PARSE_PARAMETERS_END ();
301
-
302
- if (expires_or_options ) {
303
- if (Z_TYPE_P (expires_or_options ) == IS_ARRAY ) {
304
- if (UNEXPECTED (ZEND_NUM_ARGS () > 3 )) {
305
- php_error_docref (NULL , E_WARNING , "Cannot pass arguments after the options array" );
306
- RETURN_FALSE ;
307
- }
308
- php_head_parse_cookie_options_array (expires_or_options , & expires , & path , & domain , & secure , & httponly , & samesite );
309
- } else {
310
- expires = zval_get_long (expires_or_options );
311
- }
312
- }
313
-
314
- if (!EG (exception )) {
315
- if (php_setcookie (name , value , expires , path , domain , secure , httponly , samesite , 0 ) == SUCCESS ) {
316
- RETVAL_TRUE ;
317
- } else {
318
- RETVAL_FALSE ;
319
- }
320
- }
321
-
322
- if (expires_or_options && Z_TYPE_P (expires_or_options ) == IS_ARRAY ) {
323
- if (path ) {
324
- zend_string_release (path );
325
- }
326
- if (domain ) {
327
- zend_string_release (domain );
328
- }
329
- if (samesite ) {
330
- zend_string_release (samesite );
331
- }
332
- }
306
+ php_setcookie_common (INTERNAL_FUNCTION_PARAM_PASSTHRU , true);
333
307
}
334
308
/* }}} */
335
309
0 commit comments