rename info tests
[m6w6/ext-http] / php_http_cookie.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 php_http_cookie_list_t *php_http_cookie_list_init(php_http_cookie_list_t *list TSRMLS_DC)
16 {
17 if (!list) {
18 list = emalloc(sizeof(*list));
19 }
20
21 zend_hash_init(&list->cookies, 0, NULL, ZVAL_PTR_DTOR, 0);
22 zend_hash_init(&list->extras, 0, NULL, ZVAL_PTR_DTOR, 0);
23
24 list->path = NULL;
25 list->domain = NULL;
26 list->expires = -1;
27 list->max_age = -1;
28 list->flags = 0;
29
30 TSRMLS_SET_CTX(list->ts);
31
32 return list;
33 }
34
35 php_http_cookie_list_t *php_http_cookie_list_copy(php_http_cookie_list_t *from, php_http_cookie_list_t *to)
36 {
37 TSRMLS_FETCH_FROM_CTX(from->ts);
38
39 to = php_http_cookie_list_init(to TSRMLS_CC);
40
41 array_copy(&from->cookies, &to->cookies);
42 array_copy(&from->extras, &to->extras);
43
44 PTR_SET(to->path, from->path ? estrdup(from->path) : NULL);
45 PTR_SET(to->domain, from->domain ? estrdup(from->domain) : NULL);
46 to->expires = from->expires;
47 to->max_age = from->max_age;
48 to->flags = from->flags;
49
50 return to;
51 }
52
53 void php_http_cookie_list_dtor(php_http_cookie_list_t *list)
54 {
55 if (list) {
56 zend_hash_destroy(&list->cookies);
57 zend_hash_destroy(&list->extras);
58
59 PTR_SET(list->path, NULL);
60 PTR_SET(list->domain, NULL);
61 }
62 }
63
64
65
66 void php_http_cookie_list_free(php_http_cookie_list_t **list)
67 {
68 if (*list) {
69 php_http_cookie_list_dtor(*list);
70 efree(*list);
71 *list = NULL;
72 }
73 }
74
75 const char *php_http_cookie_list_get_cookie(php_http_cookie_list_t *list, const char *name, size_t name_len, zval **zcookie)
76 {
77 zval **cookie;
78 if ((SUCCESS != zend_symtable_find(&list->cookies, name, name_len + 1, (void *) &cookie)) || (Z_TYPE_PP(cookie) != IS_STRING)) {
79 return NULL;
80 }
81 if (zcookie) {
82 *zcookie = *cookie;
83 }
84 return Z_STRVAL_PP(cookie);
85 }
86
87 const char *php_http_cookie_list_get_extra(php_http_cookie_list_t *list, const char *name, size_t name_len, zval **zextra)
88 {
89 zval **extra;
90
91 if ((SUCCESS != zend_symtable_find(&list->extras, name, name_len + 1, (void *) &extra)) || (Z_TYPE_PP(extra) != IS_STRING)) {
92 return NULL;
93 }
94 if (zextra) {
95 *zextra = *extra;
96 }
97 return Z_STRVAL_PP(extra);
98 }
99
100 void php_http_cookie_list_add_cookie(php_http_cookie_list_t *list, const char *name, size_t name_len, const char *value, size_t value_len)
101 {
102 zval *cookie_value;
103
104 MAKE_STD_ZVAL(cookie_value);
105 ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
106 zend_symtable_update(&list->cookies, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
107 }
108
109 void php_http_cookie_list_add_extra(php_http_cookie_list_t *list, const char *name, size_t name_len, const char *value, size_t value_len)
110 {
111 zval *cookie_value;
112
113 MAKE_STD_ZVAL(cookie_value);
114 ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
115 zend_symtable_update(&list->extras, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
116 }
117
118 #define _KEY_IS(s) (key->len == sizeof(s) && !strncasecmp(key->str, (s), key->len))
119 static void add_entry(php_http_cookie_list_t *list, char **allowed_extras, long flags, php_http_array_hashkey_t *key, zval *val)
120 {
121 zval *arg = php_http_zsep(1, IS_STRING, val);
122
123 if (!(flags & PHP_HTTP_COOKIE_PARSE_RAW)) {
124 Z_STRLEN_P(arg) = php_raw_url_decode(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
125 }
126
127 if _KEY_IS("path") {
128 PTR_SET(list->path, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
129 } else if _KEY_IS("domain") {
130 PTR_SET(list->domain, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
131 } else if _KEY_IS("expires") {
132 char *date = estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
133 list->expires = php_parse_date(date, NULL);
134 efree(date);
135 } else if _KEY_IS("max-age") {
136 list->max_age = strtol(Z_STRVAL_P(arg), NULL, 10);
137 } else if _KEY_IS("secure") {
138 list->flags |= PHP_HTTP_COOKIE_SECURE;
139 } else if _KEY_IS("httpOnly") {
140 list->flags |= PHP_HTTP_COOKIE_HTTPONLY;
141 } else {
142 /* check for extra */
143 if (allowed_extras) {
144 char **ae = allowed_extras;
145
146 php_http_array_hashkey_stringify(key);
147 for (; *ae; ++ae) {
148 if (!strncasecmp(key->str, *ae, key->len)) {
149 if (key->type == HASH_KEY_IS_LONG) {
150 zend_hash_index_update(&list->extras, key->num, (void *) &arg, sizeof(zval *), NULL);
151 } else {
152 zend_hash_update(&list->extras, key->str, key->len, (void *) &arg, sizeof(zval *), NULL);
153 }
154 php_http_array_hashkey_stringfree(key);
155 return;
156 }
157 }
158 php_http_array_hashkey_stringfree(key);
159 }
160
161 /* cookie */
162 if (key->type == HASH_KEY_IS_LONG) {
163 zend_hash_index_update(&list->cookies, key->num, (void *) &arg, sizeof(zval *), NULL);
164 } else {
165 zend_hash_update(&list->cookies, key->str, key->len, (void *) &arg, sizeof(zval *), NULL);
166 }
167 return;
168 }
169 zval_ptr_dtor(&arg);
170 }
171
172 php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_list_t *list, const char *str, size_t len, long flags, char **allowed_extras TSRMLS_DC)
173 {
174 php_http_params_opts_t opts;
175 HashTable params;
176 HashPosition pos1, pos2;
177 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
178 zval **param, **val, **args, **arg;
179
180 php_http_params_opts_default_get(&opts);
181 opts.input.str = estrndup(str, len);
182 opts.input.len = len;
183 opts.param = NULL;
184 zend_hash_init(&params, 10, NULL, ZVAL_PTR_DTOR, 0);
185 php_http_params_parse(&params, &opts TSRMLS_CC);
186 efree(opts.input.str);
187
188 list = php_http_cookie_list_init(list TSRMLS_CC);
189 FOREACH_HASH_KEYVAL(pos1, &params, key, param) {
190 if (Z_TYPE_PP(param) == IS_ARRAY) {
191 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("value"), (void *) &val)) {
192 add_entry(list, NULL, flags, &key, *val);
193 }
194 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("arguments"), (void *) &args) && Z_TYPE_PP(args) == IS_ARRAY) {
195 FOREACH_KEYVAL(pos2, *args, key, arg) {
196 add_entry(list, allowed_extras, flags, &key, *arg);
197 }
198 }
199 }
200 }
201 zend_hash_destroy(&params);
202
203 return list;
204 }
205
206 void php_http_cookie_list_to_struct(php_http_cookie_list_t *list, zval *strct)
207 {
208 zval array, *cookies, *extras;
209 TSRMLS_FETCH_FROM_CTX(list->ts);
210
211 INIT_PZVAL_ARRAY(&array, HASH_OF(strct));
212
213 MAKE_STD_ZVAL(cookies);
214 array_init(cookies);
215 zend_hash_copy(Z_ARRVAL_P(cookies), &list->cookies, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
216 add_assoc_zval(&array, "cookies", cookies);
217
218 MAKE_STD_ZVAL(extras);
219 array_init(extras);
220 zend_hash_copy(Z_ARRVAL_P(extras), &list->extras, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
221 add_assoc_zval(&array, "extras", extras);
222
223 add_assoc_long(&array, "flags", list->flags);
224 add_assoc_long(&array, "expires", (long) list->expires);
225 add_assoc_long(&array, "max-age", (long) list->max_age);
226 add_assoc_string(&array, "path", STR_PTR(list->path), 1);
227 add_assoc_string(&array, "domain", STR_PTR(list->domain), 1);
228 }
229
230 php_http_cookie_list_t *php_http_cookie_list_from_struct(php_http_cookie_list_t *list, zval *strct TSRMLS_DC)
231 {
232 zval **tmp, *cpy;
233 HashTable *ht = HASH_OF(strct);
234
235 list = php_http_cookie_list_init(list TSRMLS_CC);
236
237 if (SUCCESS == zend_hash_find(ht, "cookies", sizeof("cookies"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
238 zend_hash_copy(&list->cookies, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
239 }
240 if (SUCCESS == zend_hash_find(ht, "extras", sizeof("extras"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
241 zend_hash_copy(&list->extras, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
242 }
243 if (SUCCESS == zend_hash_find(ht, "flags", sizeof("flags"), (void *) &tmp)) {
244 cpy = php_http_ztyp(IS_LONG, *tmp);
245 list->flags = Z_LVAL_P(cpy);
246 zval_ptr_dtor(&cpy);
247 }
248 if (SUCCESS == zend_hash_find(ht, "expires", sizeof("expires"), (void *) &tmp)) {
249 if (Z_TYPE_PP(tmp) == IS_LONG) {
250 list->expires = Z_LVAL_PP(tmp);
251 } else {
252 long lval;
253
254 cpy = php_http_ztyp(IS_STRING, *tmp);
255 if (IS_LONG == is_numeric_string(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), &lval, NULL, 0)) {
256 list->expires = lval;
257 } else {
258 list->expires = php_parse_date(Z_STRVAL_P(cpy), NULL);
259 }
260
261 zval_ptr_dtor(&cpy);
262 }
263 }
264 if (SUCCESS == zend_hash_find(ht, "max-age", sizeof("max-age"), (void *) &tmp)) {
265 if (Z_TYPE_PP(tmp) == IS_LONG) {
266 list->max_age = Z_LVAL_PP(tmp);
267 } else {
268 long lval;
269
270 cpy = php_http_ztyp(IS_STRING, *tmp);
271 if (IS_LONG == is_numeric_string(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), &lval, NULL, 0)) {
272 list->max_age = lval;
273 }
274
275 zval_ptr_dtor(&cpy);
276 }
277 }
278 if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
279 list->path = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
280 }
281 if (SUCCESS == zend_hash_find(ht, "domain", sizeof("domain"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
282 list->domain = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
283 }
284
285 return list;
286 }
287
288 static inline void append_encoded(php_http_buffer_t *buf, const char *key, size_t key_len, const char *val, size_t val_len)
289 {
290 char *enc_str[2];
291 int enc_len[2];
292
293 enc_str[0] = php_raw_url_encode(key, key_len, &enc_len[0]);
294 enc_str[1] = php_raw_url_encode(val, val_len, &enc_len[1]);
295
296 php_http_buffer_append(buf, enc_str[0], enc_len[0]);
297 php_http_buffer_appends(buf, "=");
298 php_http_buffer_append(buf, enc_str[1], enc_len[1]);
299 php_http_buffer_appends(buf, "; ");
300
301 efree(enc_str[0]);
302 efree(enc_str[1]);
303 }
304
305 void php_http_cookie_list_to_string(php_http_cookie_list_t *list, char **str, size_t *len)
306 {
307 php_http_buffer_t buf;
308 zval **val;
309 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
310 HashPosition pos;
311 TSRMLS_FETCH_FROM_CTX(list->ts);
312
313 php_http_buffer_init(&buf);
314
315 FOREACH_HASH_KEYVAL(pos, &list->cookies, key, val) {
316 zval *tmp = php_http_ztyp(IS_STRING, *val);
317
318 php_http_array_hashkey_stringify(&key);
319 append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
320 php_http_array_hashkey_stringfree(&key);
321
322 zval_ptr_dtor(&tmp);
323 }
324
325 if (list->domain && *list->domain) {
326 php_http_buffer_appendf(&buf, "domain=%s; ", list->domain);
327 }
328 if (list->path && *list->path) {
329 php_http_buffer_appendf(&buf, "path=%s; ", list->path);
330 }
331 if (list->expires >= 0) {
332 char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), list->expires, 0 TSRMLS_CC);
333 php_http_buffer_appendf(&buf, "expires=%s; ", date);
334 efree(date);
335 }
336 if (list->max_age >= 0) {
337 php_http_buffer_appendf(&buf, "max-age=%ld; ", list->max_age);
338 }
339
340 FOREACH_HASH_KEYVAL(pos, &list->extras, key, val) {
341 zval *tmp = php_http_ztyp(IS_STRING, *val);
342
343 php_http_array_hashkey_stringify(&key);
344 append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
345 php_http_array_hashkey_stringfree(&key);
346
347 zval_ptr_dtor(&tmp);
348 }
349
350 if (list->flags & PHP_HTTP_COOKIE_SECURE) {
351 php_http_buffer_appends(&buf, "secure; ");
352 }
353 if (list->flags & PHP_HTTP_COOKIE_HTTPONLY) {
354 php_http_buffer_appends(&buf, "httpOnly; ");
355 }
356
357 php_http_buffer_fix(&buf);
358 *str = buf.data;
359 *len = buf.used;
360 }
361
362
363
364 static zend_object_handlers php_http_cookie_object_handlers;
365
366 zend_object_value php_http_cookie_object_new(zend_class_entry *ce TSRMLS_DC)
367 {
368 return php_http_cookie_object_new_ex(ce, NULL, NULL TSRMLS_CC);
369 }
370
371 zend_object_value php_http_cookie_object_new_ex(zend_class_entry *ce, php_http_cookie_list_t *list, php_http_cookie_object_t **ptr TSRMLS_DC)
372 {
373 php_http_cookie_object_t *o;
374
375 o = ecalloc(sizeof(*o), 1);
376 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
377 object_properties_init((zend_object *) o, ce);
378
379 if (list) {
380 o->list = list;
381 }
382
383 if (ptr) {
384 *ptr = o;
385 }
386
387 o->zv.handle = zend_objects_store_put(o, NULL, php_http_cookie_object_free, NULL TSRMLS_CC);
388 o->zv.handlers = &php_http_cookie_object_handlers;
389
390 return o->zv;
391 }
392
393 #define PHP_HTTP_COOKIE_OBJECT_INIT(obj) \
394 do { \
395 if (!obj->list) { \
396 obj->list = php_http_cookie_list_init(NULL TSRMLS_CC); \
397 } \
398 } while(0)
399
400 zend_object_value php_http_cookie_object_clone(zval *this_ptr TSRMLS_DC)
401 {
402 php_http_cookie_object_t *new_obj, *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
403 zend_object_value ov;
404
405 PHP_HTTP_COOKIE_OBJECT_INIT(old_obj);
406
407 ov = php_http_cookie_object_new_ex(old_obj->zo.ce, php_http_cookie_list_copy(old_obj->list, NULL), &new_obj TSRMLS_CC);
408 zend_objects_clone_members((zend_object *) new_obj, ov, (zend_object *) old_obj, Z_OBJ_HANDLE_P(getThis()) TSRMLS_CC);
409
410 return ov;
411 }
412
413 void php_http_cookie_object_free(void *object TSRMLS_DC)
414 {
415 php_http_cookie_object_t *obj = object;
416
417 php_http_cookie_list_free(&obj->list);
418 zend_object_std_dtor((zend_object *) obj TSRMLS_CC);
419 efree(obj);
420 }
421
422 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie___construct, 0, 0, 0)
423 ZEND_ARG_INFO(0, cookie_string)
424 ZEND_ARG_INFO(0, parser_flags)
425 ZEND_ARG_INFO(0, allowed_extras)
426 ZEND_END_ARG_INFO();
427 static PHP_METHOD(HttpCookie, __construct)
428 {
429 php_http_cookie_object_t *obj;
430 zval *zcookie = NULL;
431 long flags = 0;
432 char **ae = NULL;
433 HashTable *allowed_extras = NULL;
434 zend_error_handling zeh;
435
436 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!lH", &zcookie, &flags, &allowed_extras), invalid_arg, return);
437
438 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
439
440 zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh TSRMLS_CC);
441 if (zcookie) {
442
443 if (allowed_extras && zend_hash_num_elements(allowed_extras)) {
444 char **ae_ptr = safe_emalloc(zend_hash_num_elements(allowed_extras) + 1, sizeof(char *), 0);
445 HashPosition pos;
446 zval **val;
447
448 ae = ae_ptr;
449 FOREACH_HASH_VAL(pos, allowed_extras, val) {
450 zval *cpy = php_http_ztyp(IS_STRING, *val);
451
452 *ae_ptr++ = estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
453 zval_ptr_dtor(&cpy);
454 }
455 *ae_ptr = NULL;
456 }
457
458 switch (Z_TYPE_P(zcookie)) {
459 case IS_OBJECT:
460 if (instanceof_function(Z_OBJCE_P(zcookie), php_http_cookie_class_entry TSRMLS_CC)) {
461 php_http_cookie_object_t *zco = zend_object_store_get_object(zcookie TSRMLS_CC);
462
463 if (zco->list) {
464 obj->list = php_http_cookie_list_copy(zco->list, NULL);
465 }
466 break;
467 }
468 /* no break */
469 case IS_ARRAY:
470 obj->list = php_http_cookie_list_from_struct(obj->list, zcookie TSRMLS_CC);
471 break;
472 default: {
473 zval *cpy = php_http_ztyp(IS_STRING, zcookie);
474
475 obj->list = php_http_cookie_list_parse(obj->list, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), flags, ae TSRMLS_CC);
476 zval_ptr_dtor(&cpy);
477 break;
478 }
479 }
480
481 if (ae) {
482 char **ae_ptr;
483
484 for (ae_ptr = ae; *ae_ptr; ++ae_ptr) {
485 efree(*ae_ptr);
486 }
487 efree(ae);
488 }
489 }
490 zend_restore_error_handling(&zeh TSRMLS_CC);
491
492 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
493 }
494
495 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getCookies, 0, 0, 0)
496 ZEND_END_ARG_INFO();;
497 static PHP_METHOD(HttpCookie, getCookies)
498 {
499 php_http_cookie_object_t *obj;
500
501 if (SUCCESS != zend_parse_parameters_none()) {
502 return;
503 }
504
505 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
506
507 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
508
509 array_init(return_value);
510 array_copy(&obj->list->cookies, Z_ARRVAL_P(return_value));
511 }
512
513 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setCookies, 0, 0, 0)
514 ZEND_ARG_INFO(0, cookies)
515 ZEND_END_ARG_INFO();
516 static PHP_METHOD(HttpCookie, setCookies)
517 {
518 HashTable *cookies = NULL;
519 php_http_cookie_object_t *obj;
520
521 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H", &cookies), invalid_arg, return);
522
523 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
524
525 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
526
527 zend_hash_clean(&obj->list->cookies);
528 if (cookies) {
529 array_copy_strings(cookies, &obj->list->cookies);
530 }
531
532 RETVAL_ZVAL(getThis(), 1, 0);
533 }
534
535 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_addCookies, 0, 0, 1)
536 ZEND_ARG_INFO(0, cookies)
537 ZEND_END_ARG_INFO();
538 static PHP_METHOD(HttpCookie, addCookies)
539 {
540 HashTable *cookies = NULL;
541 php_http_cookie_object_t *obj;
542
543 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &cookies), invalid_arg, return);
544
545 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
546
547 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
548
549 array_join(cookies, &obj->list->cookies, 1, ARRAY_JOIN_STRONLY|ARRAY_JOIN_STRINGIFY);
550
551 RETVAL_ZVAL(getThis(), 1, 0);
552 }
553
554 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getExtras, 0, 0, 0)
555 ZEND_END_ARG_INFO();
556 static PHP_METHOD(HttpCookie, getExtras)
557 {
558 php_http_cookie_object_t *obj;
559
560 if (SUCCESS != zend_parse_parameters_none()) {
561 return;
562 }
563
564 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
565
566 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
567
568 array_init(return_value);
569 array_copy(&obj->list->extras, Z_ARRVAL_P(return_value));
570 }
571
572 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setExtras, 0, 0, 0)
573 ZEND_ARG_INFO(0, extras)
574 ZEND_END_ARG_INFO();
575 static PHP_METHOD(HttpCookie, setExtras)
576 {
577 HashTable *extras = NULL;
578 php_http_cookie_object_t *obj;
579
580 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H", &extras), invalid_arg, return);
581
582 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
583
584 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
585
586 zend_hash_clean(&obj->list->extras);
587 if (extras) {
588 array_copy_strings(extras, &obj->list->extras);
589 }
590
591 RETVAL_ZVAL(getThis(), 1, 0);
592 }
593
594 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_addExtras, 0, 0, 1)
595 ZEND_ARG_INFO(0, extras)
596 ZEND_END_ARG_INFO();
597 static PHP_METHOD(HttpCookie, addExtras)
598 {
599 HashTable *extras = NULL;
600 php_http_cookie_object_t *obj;
601
602 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &extras), invalid_arg, return);
603
604 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
605
606 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
607
608 array_join(extras, &obj->list->extras, 1, ARRAY_JOIN_STRONLY|ARRAY_JOIN_STRINGIFY);
609
610 RETVAL_ZVAL(getThis(), 1, 0);
611 }
612
613 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getCookie, 0, 0, 1)
614 ZEND_ARG_INFO(0, name)
615 ZEND_END_ARG_INFO();
616 static PHP_METHOD(HttpCookie, getCookie)
617 {
618 char *name_str;
619 int name_len;
620 zval *zvalue;
621 php_http_cookie_object_t *obj;
622
623 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) {
624 return;
625 }
626
627 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
628
629 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
630
631 if (php_http_cookie_list_get_cookie(obj->list, name_str, name_len, &zvalue)) {
632 RETURN_ZVAL(zvalue, 1, 0);
633 }
634 }
635
636 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setCookie, 0, 0, 1)
637 ZEND_ARG_INFO(0, cookie_name)
638 ZEND_ARG_INFO(0, cookie_value)
639 ZEND_END_ARG_INFO();
640 static PHP_METHOD(HttpCookie, setCookie)
641 {
642 char *name_str, *value_str = NULL;
643 int name_len, value_len = 0;
644 php_http_cookie_object_t *obj;
645
646 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
647
648 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
649
650 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
651
652 if (!value_str) {
653 php_http_cookie_list_del_cookie(obj->list, name_str, name_len);
654 } else {
655 php_http_cookie_list_add_cookie(obj->list, name_str, name_len, value_str, value_len);
656 }
657
658 RETVAL_ZVAL(getThis(), 1, 0);
659 }
660
661 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_addCookie, 0, 0, 2)
662 ZEND_ARG_INFO(0, cookie_name)
663 ZEND_ARG_INFO(0, cookie_value)
664 ZEND_END_ARG_INFO();
665 static PHP_METHOD(HttpCookie, addCookie)
666 {
667 char *name_str, *value_str;
668 int name_len, value_len;
669 php_http_cookie_object_t *obj;
670
671 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
672
673 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
674
675 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
676
677 php_http_cookie_list_add_cookie(obj->list, name_str, name_len, value_str, value_len);
678
679 RETVAL_ZVAL(getThis(), 1, 0);
680 }
681
682 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getExtra, 0, 0, 1)
683 ZEND_ARG_INFO(0, name)
684 ZEND_END_ARG_INFO();
685 static PHP_METHOD(HttpCookie, getExtra)
686 {
687 char *name_str;
688 int name_len;
689 zval *zvalue;
690 php_http_cookie_object_t *obj;
691
692 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) {
693 return;
694 }
695
696 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
697
698 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
699
700 if (php_http_cookie_list_get_extra(obj->list, name_str, name_len, &zvalue)) {
701 RETURN_ZVAL(zvalue, 1, 0);
702 }
703 }
704
705 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setExtra, 0, 0, 1)
706 ZEND_ARG_INFO(0, extra_name)
707 ZEND_ARG_INFO(0, extra_value)
708 ZEND_END_ARG_INFO();
709 static PHP_METHOD(HttpCookie, setExtra)
710 {
711 char *name_str, *value_str = NULL;
712 int name_len, value_len = 0;
713 php_http_cookie_object_t *obj;
714
715 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
716
717 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
718
719 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
720
721 if (!value_str) {
722 php_http_cookie_list_del_extra(obj->list, name_str, name_len);
723 } else {
724 php_http_cookie_list_add_extra(obj->list, name_str, name_len, value_str, value_len);
725 }
726
727 RETVAL_ZVAL(getThis(), 1, 0);
728 }
729
730 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_addExtra, 0, 0, 2)
731 ZEND_ARG_INFO(0, extra_name)
732 ZEND_ARG_INFO(0, extra_value)
733 ZEND_END_ARG_INFO();
734 static PHP_METHOD(HttpCookie, addExtra)
735 {
736 char *name_str, *value_str;
737 int name_len, value_len;
738 php_http_cookie_object_t *obj;
739
740 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
741
742 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
743
744 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
745
746 php_http_cookie_list_add_extra(obj->list, name_str, name_len, value_str, value_len);
747
748 RETVAL_ZVAL(getThis(), 1, 0);
749 }
750
751 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getDomain, 0, 0, 0)
752 ZEND_END_ARG_INFO();;
753 static PHP_METHOD(HttpCookie, getDomain)
754 {
755 php_http_cookie_object_t *obj;
756
757 if (SUCCESS != zend_parse_parameters_none()) {
758 return;
759 }
760
761 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
762
763 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
764
765 if (obj->list->domain) {
766 RETURN_STRING(obj->list->domain, 1);
767 }
768 }
769
770 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setDomain, 0, 0, 0)
771 ZEND_ARG_INFO(0, value)
772 ZEND_END_ARG_INFO();
773 static PHP_METHOD(HttpCookie, setDomain)
774 {
775 char *domain_str = NULL;
776 int domain_len = 0;
777 php_http_cookie_object_t *obj;
778
779 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &domain_str, &domain_len), invalid_arg, return);
780
781 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
782
783 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
784
785 PTR_SET(obj->list->domain, domain_str ? estrndup(domain_str, domain_len) : NULL);
786
787 RETVAL_ZVAL(getThis(), 1, 0);
788 }
789
790 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getPath, 0, 0, 0)
791 ZEND_END_ARG_INFO();;
792 static PHP_METHOD(HttpCookie, getPath)
793 {
794 php_http_cookie_object_t *obj;
795
796 if (SUCCESS != zend_parse_parameters_none()) {
797 return;
798 }
799
800 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
801
802 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
803
804 if (obj->list->path) {
805 RETURN_STRING(obj->list->path, 1);
806 }
807 }
808
809 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setPath, 0, 0, 0)
810 ZEND_ARG_INFO(0, value)
811 ZEND_END_ARG_INFO();
812 static PHP_METHOD(HttpCookie, setPath)
813 {
814 char *path_str = NULL;
815 int path_len = 0;
816 php_http_cookie_object_t *obj;
817
818 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &path_str, &path_len), invalid_arg, return);
819
820 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
821
822 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
823
824 PTR_SET(obj->list->path, path_str ? estrndup(path_str, path_len) : NULL);
825
826 RETVAL_ZVAL(getThis(), 1, 0);
827 }
828
829 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getExpires, 0, 0, 0)
830 ZEND_END_ARG_INFO();;
831 static PHP_METHOD(HttpCookie, getExpires)
832 {
833 php_http_cookie_object_t *obj;
834
835 if (SUCCESS != zend_parse_parameters_none()) {
836 return;
837 }
838
839 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
840
841 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
842
843 RETURN_LONG(obj->list->expires);
844 }
845
846 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setExpires, 0, 0, 0)
847 ZEND_ARG_INFO(0, value)
848 ZEND_END_ARG_INFO();
849 static PHP_METHOD(HttpCookie, setExpires)
850 {
851 long ts = -1;
852 php_http_cookie_object_t *obj;
853
854 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts), invalid_arg, return);
855
856 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
857
858 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
859
860 obj->list->expires = ts;
861
862 RETVAL_ZVAL(getThis(), 1, 0);
863 }
864
865 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getMaxAge, 0, 0, 0)
866 ZEND_END_ARG_INFO();;
867 static PHP_METHOD(HttpCookie, getMaxAge)
868 {
869 php_http_cookie_object_t *obj;
870
871 if (SUCCESS != zend_parse_parameters_none()) {
872 return;
873 }
874
875 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
876
877 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
878
879 RETURN_LONG(obj->list->max_age);
880 }
881
882 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setMaxAge, 0, 0, 0)
883 ZEND_ARG_INFO(0, value)
884 ZEND_END_ARG_INFO();
885 static PHP_METHOD(HttpCookie, setMaxAge)
886 {
887 long ts = -1;
888 php_http_cookie_object_t *obj;
889
890 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts), invalid_arg, return);
891
892 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
893
894 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
895
896 obj->list->max_age = ts;
897
898 RETVAL_ZVAL(getThis(), 1, 0);
899 }
900
901 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getFlags, 0, 0, 0)
902 ZEND_END_ARG_INFO();
903 static PHP_METHOD(HttpCookie, getFlags)
904 {
905 php_http_cookie_object_t *obj;
906
907 if (SUCCESS != zend_parse_parameters_none()) {
908 return;
909 }
910
911 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
912
913 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
914
915 RETURN_LONG(obj->list->flags);
916 }
917
918 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setFlags, 0, 0, 0)
919 ZEND_ARG_INFO(0, value)
920 ZEND_END_ARG_INFO();
921 static PHP_METHOD(HttpCookie, setFlags)
922 {
923 long flags = 0;
924 php_http_cookie_object_t *obj;
925
926 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags), invalid_arg, return);
927
928 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
929
930 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
931
932 obj->list->flags = flags;
933
934 RETVAL_ZVAL(getThis(), 1, 0);
935 }
936
937 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_toString, 0, 0, 0)
938 ZEND_END_ARG_INFO();;
939 static PHP_METHOD(HttpCookie, toString)
940 {
941 php_http_cookie_object_t *obj;
942 char *str;
943 size_t len;
944
945 if (SUCCESS != zend_parse_parameters_none()) {
946 RETURN_EMPTY_STRING();
947 }
948
949 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
950
951 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
952
953 php_http_cookie_list_to_string(obj->list, &str, &len);
954
955 RETURN_STRINGL(str, len, 0);
956 }
957
958 ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_toArray, 0, 0, 0)
959 ZEND_END_ARG_INFO();;
960 static PHP_METHOD(HttpCookie, toArray)
961 {
962 php_http_cookie_object_t *obj;
963
964 if (SUCCESS != zend_parse_parameters_none()) {
965 return;
966 }
967
968 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
969
970 PHP_HTTP_COOKIE_OBJECT_INIT(obj);
971
972 array_init(return_value);
973 php_http_cookie_list_to_struct(obj->list, return_value);
974 }
975
976 static zend_function_entry php_http_cookie_methods[] = {
977 PHP_ME(HttpCookie, __construct, ai_HttpCookie___construct, ZEND_ACC_PUBLIC)
978 PHP_ME(HttpCookie, getCookies, ai_HttpCookie_getCookies, ZEND_ACC_PUBLIC)
979 PHP_ME(HttpCookie, setCookies, ai_HttpCookie_setCookies, ZEND_ACC_PUBLIC)
980 PHP_ME(HttpCookie, addCookies, ai_HttpCookie_addCookies, ZEND_ACC_PUBLIC)
981 PHP_ME(HttpCookie, getCookie, ai_HttpCookie_getCookie, ZEND_ACC_PUBLIC)
982 PHP_ME(HttpCookie, setCookie, ai_HttpCookie_setCookie, ZEND_ACC_PUBLIC)
983 PHP_ME(HttpCookie, addCookie, ai_HttpCookie_addCookie, ZEND_ACC_PUBLIC)
984
985 PHP_ME(HttpCookie, getExtras, ai_HttpCookie_getExtras, ZEND_ACC_PUBLIC)
986 PHP_ME(HttpCookie, setExtras, ai_HttpCookie_setExtras, ZEND_ACC_PUBLIC)
987 PHP_ME(HttpCookie, addExtras, ai_HttpCookie_addExtras, ZEND_ACC_PUBLIC)
988 PHP_ME(HttpCookie, getExtra, ai_HttpCookie_getExtra, ZEND_ACC_PUBLIC)
989 PHP_ME(HttpCookie, setExtra, ai_HttpCookie_setExtra, ZEND_ACC_PUBLIC)
990 PHP_ME(HttpCookie, addExtra, ai_HttpCookie_addExtra, ZEND_ACC_PUBLIC)
991
992 PHP_ME(HttpCookie, getDomain, ai_HttpCookie_getDomain, ZEND_ACC_PUBLIC)
993 PHP_ME(HttpCookie, setDomain, ai_HttpCookie_setDomain, ZEND_ACC_PUBLIC)
994 PHP_ME(HttpCookie, getPath, ai_HttpCookie_getPath, ZEND_ACC_PUBLIC)
995 PHP_ME(HttpCookie, setPath, ai_HttpCookie_setPath, ZEND_ACC_PUBLIC)
996 PHP_ME(HttpCookie, getExpires, ai_HttpCookie_getExpires, ZEND_ACC_PUBLIC)
997 PHP_ME(HttpCookie, setExpires, ai_HttpCookie_setExpires, ZEND_ACC_PUBLIC)
998 PHP_ME(HttpCookie, getMaxAge, ai_HttpCookie_getMaxAge, ZEND_ACC_PUBLIC)
999 PHP_ME(HttpCookie, setMaxAge, ai_HttpCookie_setMaxAge, ZEND_ACC_PUBLIC)
1000 PHP_ME(HttpCookie, getFlags, ai_HttpCookie_getFlags, ZEND_ACC_PUBLIC)
1001 PHP_ME(HttpCookie, setFlags, ai_HttpCookie_setFlags, ZEND_ACC_PUBLIC)
1002
1003 PHP_ME(HttpCookie, toArray, ai_HttpCookie_toArray, ZEND_ACC_PUBLIC)
1004 PHP_ME(HttpCookie, toString, ai_HttpCookie_toString, ZEND_ACC_PUBLIC)
1005 ZEND_MALIAS(HttpCookie, __toString, toString, ai_HttpCookie_toString, ZEND_ACC_PUBLIC)
1006
1007 EMPTY_FUNCTION_ENTRY
1008 };
1009
1010 zend_class_entry *php_http_cookie_class_entry;
1011
1012 PHP_MINIT_FUNCTION(http_cookie)
1013 {
1014 zend_class_entry ce = {0};
1015
1016 INIT_NS_CLASS_ENTRY(ce, "http", "Cookie", php_http_cookie_methods);
1017 php_http_cookie_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
1018 php_http_cookie_class_entry->create_object = php_http_cookie_object_new;
1019 memcpy(&php_http_cookie_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1020 php_http_cookie_object_handlers.clone_obj = php_http_cookie_object_clone;
1021
1022 zend_declare_class_constant_long(php_http_cookie_class_entry, ZEND_STRL("PARSE_RAW"), PHP_HTTP_COOKIE_PARSE_RAW TSRMLS_CC);
1023 zend_declare_class_constant_long(php_http_cookie_class_entry, ZEND_STRL("SECURE"), PHP_HTTP_COOKIE_SECURE TSRMLS_CC);
1024 zend_declare_class_constant_long(php_http_cookie_class_entry, ZEND_STRL("HTTPONLY"), PHP_HTTP_COOKIE_HTTPONLY TSRMLS_CC);
1025
1026 return SUCCESS;
1027 }
1028
1029
1030
1031 /*
1032 * Local variables:
1033 * tab-width: 4
1034 * c-basic-offset: 4
1035 * End:
1036 * vim600: noet sw=4 ts=4 fdm=marker
1037 * vim<600: noet sw=4 ts=4
1038 */