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