php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
session.c
Go to the documentation of this file.
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 | Authors: Sascha Schumann <sascha@schumann.cx> |
14 | Andrei Zmievski <andrei@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include "php.h"
23
24#ifdef PHP_WIN32
25# include "win32/winutil.h"
26# include "win32/time.h"
27#else
28# include <sys/time.h>
29#endif
30
31#include <sys/stat.h>
32#include <fcntl.h>
33
34#include "php_ini.h"
35#include "rfc1867.h"
36#include "php_variables.h"
37#include "php_session.h"
38#include "session_arginfo.h"
40#include "ext/date/php_date.h"
42#include "ext/standard/info.h"
43#include "zend_smart_str.h"
44#include "ext/standard/url.h"
46#include "ext/standard/head.h"
49
50#include "mod_files.h"
51#include "mod_user.h"
52
53#ifdef HAVE_LIBMM
54#include "mod_mm.h"
55#endif
56
58
59static zend_result php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra);
60static zend_result (*php_session_rfc1867_orig_callback)(unsigned int event, void *event_data, void **extra);
61static void php_session_track_init(void);
62
63/* SessionHandler class */
65
66/* SessionHandlerInterface */
68
69/* SessionIdInterface */
71
72/* SessionUpdateTimestampInterface */
74
75#define PS_MAX_SID_LENGTH 256
76
77/* ***********
78 * Helpers *
79 *********** */
80
81#define IF_SESSION_VARS() \
82 if (Z_ISREF_P(&PS(http_session_vars)) && Z_TYPE_P(Z_REFVAL(PS(http_session_vars))) == IS_ARRAY)
83
84#define SESSION_CHECK_ACTIVE_STATE \
85 if (PS(session_status) == php_session_active) { \
86 php_error_docref(NULL, E_WARNING, "Session ini settings cannot be changed when a session is active"); \
87 return FAILURE; \
88 }
89
90#define SESSION_CHECK_OUTPUT_STATE \
91 if (SG(headers_sent) && stage != ZEND_INI_STAGE_DEACTIVATE) { \
92 php_error_docref(NULL, E_WARNING, "Session ini settings cannot be changed after headers have already been sent"); \
93 return FAILURE; \
94 }
95
96#define SESSION_FORBIDDEN_CHARS "=,;.[ \t\r\n\013\014"
97#define SESSION_FORBIDDEN_CHARS_FOR_ERROR_MSG "=,;.[ \\t\\r\\n\\013\\014"
98
99#define APPLY_TRANS_SID (PS(use_trans_sid) && !PS(use_only_cookies))
100
101static zend_result php_session_send_cookie(void);
102static zend_result php_session_abort(void);
103
104/* Initialized in MINIT, readonly otherwise. */
105static int my_module_number = 0;
106
107/* Dispatched by RINIT and by php_session_destroy */
108static inline void php_rinit_session_globals(void) /* {{{ */
109{
110 /* Do NOT init PS(mod_user_names) here! */
111 /* TODO: These could be moved to MINIT and removed. These should be initialized by php_rshutdown_session_globals() always when execution is finished. */
112 PS(id) = NULL;
114 PS(in_save_handler) = 0;
115 PS(set_handler) = 0;
116 PS(mod_data) = NULL;
117 PS(mod_user_is_open) = 0;
118 PS(define_sid) = 1;
120 PS(module_number) = my_module_number;
121 ZVAL_UNDEF(&PS(http_session_vars));
122}
123/* }}} */
124
125static inline void php_session_cleanup_filename(void) /* {{{ */
126{
127 if (PS(session_started_filename)) {
128 zend_string_release(PS(session_started_filename));
129 PS(session_started_filename) = NULL;
130 PS(session_started_lineno) = 0;
131 }
132}
133/* }}} */
134
135/* Dispatched by RSHUTDOWN and by php_session_destroy */
136static void php_rshutdown_session_globals(void) /* {{{ */
137{
138 /* Do NOT destroy PS(mod_user_names) here! */
139 if (!Z_ISUNDEF(PS(http_session_vars))) {
140 zval_ptr_dtor(&PS(http_session_vars));
141 ZVAL_UNDEF(&PS(http_session_vars));
142 }
143 if (PS(mod_data) || PS(mod_user_implemented)) {
144 zend_try {
145 PS(mod)->s_close(&PS(mod_data));
146 } zend_end_try();
147 }
148 if (PS(id)) {
150 PS(id) = NULL;
151 }
152
153 if (PS(session_vars)) {
156 }
157
158 if (PS(mod_user_class_name)) {
159 zend_string_release(PS(mod_user_class_name));
160 PS(mod_user_class_name) = NULL;
161 }
162
163 php_session_cleanup_filename();
164
165 /* User save handlers may end up directly here by misuse, bugs in user script, etc. */
166 /* Set session status to prevent error while restoring save handler INI value. */
168}
169/* }}} */
170
172{
174
176 php_error_docref(NULL, E_WARNING, "Trying to destroy uninitialized session");
177 return FAILURE;
178 }
179
180 if (PS(id) && PS(mod)->s_destroy(&PS(mod_data), PS(id)) == FAILURE) {
181 retval = FAILURE;
182 if (!EG(exception)) {
183 php_error_docref(NULL, E_WARNING, "Session object destruction failed");
184 }
185 }
186
187 php_rshutdown_session_globals();
188 php_rinit_session_globals();
189
190 return retval;
191}
192/* }}} */
193
195{
197 zval *sess_var = Z_REFVAL(PS(http_session_vars));
198 SEPARATE_ARRAY(sess_var);
199 if (!zend_hash_exists(Z_ARRVAL_P(sess_var), name)) {
200 zval empty_var;
201 ZVAL_NULL(&empty_var);
202 zend_hash_update(Z_ARRVAL_P(sess_var), name, &empty_var);
203 }
204 }
205}
206/* }}} */
207
209{
211 zval *sess_var = Z_REFVAL(PS(http_session_vars));
212 SEPARATE_ARRAY(sess_var);
213 return zend_hash_update(Z_ARRVAL_P(sess_var), name, state_val);
214 }
215 return NULL;
216}
217/* }}} */
218
220{
222 return zend_hash_find(Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars))), name);
223 }
224 return NULL;
225}
226/* }}} */
227
228PHPAPI zval* php_get_session_var_str(const char *name, size_t name_len)
229{
231 return zend_hash_str_find(Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars))), name, name_len);
232 }
233 return NULL;
234}
235
236static void php_session_track_init(void) /* {{{ */
237{
239 zend_string *var_name = ZSTR_INIT_LITERAL("_SESSION", 0);
240 /* Unconditionally destroy existing array -- possible dirty data */
242
243 if (!Z_ISUNDEF(PS(http_session_vars))) {
244 zval_ptr_dtor(&PS(http_session_vars));
245 }
246
248 ZVAL_NEW_REF(&PS(http_session_vars), &session_vars);
249 Z_ADDREF_P(&PS(http_session_vars));
250 zend_hash_update_ind(&EG(symbol_table), var_name, &PS(http_session_vars));
252}
253/* }}} */
254
255static zend_string *php_session_encode(void) /* {{{ */
256{
258 ZEND_ASSERT(PS(serializer));
259 return PS(serializer)->encode();
260 } else {
261 php_error_docref(NULL, E_WARNING, "Cannot encode non-existent session");
262 }
263 return NULL;
264}
265/* }}} */
266
267static ZEND_COLD void php_session_cancel_decode(void)
268{
270 php_session_track_init();
271 php_error_docref(NULL, E_WARNING, "Failed to decode session object. Session has been destroyed");
272}
273
274static zend_result php_session_decode(zend_string *data) /* {{{ */
275{
276 ZEND_ASSERT(PS(serializer));
278 zend_try {
279 if (PS(serializer)->decode(ZSTR_VAL(data), ZSTR_LEN(data)) == FAILURE) {
280 php_session_cancel_decode();
281 result = FAILURE;
282 }
283 } zend_catch {
284 php_session_cancel_decode();
285 zend_bailout();
286 } zend_end_try();
287 return result;
288}
289/* }}} */
290
291/*
292 * Note that we cannot use the BASE64 alphabet here, because
293 * it contains "/" and "+": both are unacceptable for simple inclusion
294 * into URLs.
295 */
296
297static const char hexconvtab[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,-";
298
299static void bin_to_readable(unsigned char *in, size_t inlen, char *out, size_t outlen, char nbits) /* {{{ */
300{
301 unsigned char *p, *q;
302 unsigned short w;
303 int mask;
304 int have;
305
306 p = (unsigned char *)in;
307 q = (unsigned char *)in + inlen;
308
309 w = 0;
310 have = 0;
311 mask = (1 << nbits) - 1;
312
313 while (outlen--) {
314 if (have < nbits) {
315 if (p < q) {
316 w |= *p++ << have;
317 have += 8;
318 } else {
319 /* Should never happen. Input must be large enough. */
321 break;
322 }
323 }
324
325 /* consume nbits */
326 *out++ = hexconvtab[w & mask];
327 w >>= nbits;
328 have -= nbits;
329 }
330
331 *out = '\0';
332}
333/* }}} */
334
336{
337 unsigned char rbuf[PS_MAX_SID_LENGTH];
338 zend_string *outid;
339
340 /* It would be enough to read ceil(sid_length * sid_bits_per_character / 8) bytes here.
341 * We read sid_length bytes instead for simplicity. */
342 if (php_random_bytes_throw(rbuf, PS(sid_length)) == FAILURE) {
343 return NULL;
344 }
345
346 outid = zend_string_alloc(PS(sid_length), 0);
347 bin_to_readable(
348 rbuf, PS(sid_length),
349 ZSTR_VAL(outid), ZSTR_LEN(outid),
350 (char)PS(sid_bits_per_character));
351
352 return outid;
353}
354/* }}} */
355
356/* Default session id char validation function allowed by ps_modules.
357 * If you change the logic here, please also update the error message in
358 * ps_modules appropriately */
360{
361 size_t len;
362 const char *p;
363 char c;
364
365 for (p = key; (c = *p); p++) {
366 /* valid characters are [a-z], [A-Z], [0-9], - (hyphen) and , (comma) */
367 if (!((c >= 'a' && c <= 'z')
368 || (c >= 'A' && c <= 'Z')
369 || (c >= '0' && c <= '9')
370 || c == ','
371 || c == '-')) {
372 return FAILURE;
373 }
374 }
375
376 len = p - key;
377
378 /* Somewhat arbitrary length limit here, but should be way more than
379 anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */
380 if (len == 0 || len > PS_MAX_SID_LENGTH) {
381 return FAILURE;
382 }
383
384 return SUCCESS;
385}
386/* }}} */
387
388
389static zend_long php_session_gc(bool immediate) /* {{{ */
390{
391 zend_long num = -1;
392 bool collect = immediate;
393
394 /* GC must be done before reading session data. */
395 if ((PS(mod_data) || PS(mod_user_implemented))) {
396 if (!collect && PS(gc_probability) > 0) {
397 collect = php_random_range(PS(random), 0, PS(gc_divisor) - 1) < PS(gc_probability);
398 }
399
400 if (collect) {
401 PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &num);
402 }
403 }
404 return num;
405} /* }}} */
406
407static zend_result php_session_initialize(void) /* {{{ */
408{
410
412
413 if (!PS(mod)) {
415 php_error_docref(NULL, E_WARNING, "No storage module chosen - failed to initialize session");
416 return FAILURE;
417 }
418
419 /* Open session handler first */
420 if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE
421 /* || PS(mod_data) == NULL */ /* FIXME: open must set valid PS(mod_data) with success */
422 ) {
423 php_session_abort();
424 if (!EG(exception)) {
425 php_error_docref(NULL, E_WARNING, "Failed to initialize storage module: %s (path: %s)", PS(mod)->s_name, PS(save_path));
426 }
427 return FAILURE;
428 }
429
430 /* If there is no ID, use session module to create one */
431 if (!PS(id) || !ZSTR_VAL(PS(id))[0]) {
432 if (PS(id)) {
434 }
435 PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
436 if (!PS(id)) {
437 php_session_abort();
438 if (!EG(exception)) {
439 zend_throw_error(NULL, "Failed to create session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
440 }
441 return FAILURE;
442 }
443 if (PS(use_cookies)) {
444 PS(send_cookie) = 1;
445 }
446 } else if (PS(use_strict_mode) && PS(mod)->s_validate_sid &&
447 PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == FAILURE
448 ) {
449 if (PS(id)) {
451 }
452 PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
453 if (!PS(id)) {
455 }
456 if (PS(use_cookies)) {
457 PS(send_cookie) = 1;
458 }
459 }
460
461 if (php_session_reset_id() == FAILURE) {
462 php_session_abort();
463 return FAILURE;
464 }
465
466 /* Read data */
467 php_session_track_init();
468 if (PS(mod)->s_read(&PS(mod_data), PS(id), &val, PS(gc_maxlifetime)) == FAILURE) {
469 php_session_abort();
470 /* FYI: Some broken save handlers return FAILURE for non-existent session ID, this is incorrect */
471 if (!EG(exception)) {
472 php_error_docref(NULL, E_WARNING, "Failed to read session data: %s (path: %s)", PS(mod)->s_name, PS(save_path));
473 }
474 return FAILURE;
475 }
476
477 /* GC must be done after read */
478 php_session_gc(0);
479
480 if (PS(session_vars)) {
483 }
484 if (val) {
485 if (PS(lazy_write)) {
486 PS(session_vars) = zend_string_copy(val);
487 }
488 php_session_decode(val);
490 }
491
492 php_session_cleanup_filename();
493 zend_string *session_started_filename = zend_get_executed_filename_ex();
494 if (session_started_filename != NULL) {
495 PS(session_started_filename) = zend_string_copy(session_started_filename);
496 PS(session_started_lineno) = zend_get_executed_lineno();
497 }
498 return SUCCESS;
499}
500/* }}} */
501
502static void php_session_save_current_state(int write) /* {{{ */
503{
505
506 if (write) {
508 zend_string *handler_class_name = PS(mod_user_class_name);
509 const char *handler_function_name;
510
511 if (PS(mod_data) || PS(mod_user_implemented)) {
513
514 val = php_session_encode();
515 if (val) {
516 if (PS(lazy_write) && PS(session_vars)
517 && PS(mod)->s_update_timestamp
518 && PS(mod)->s_update_timestamp != php_session_update_timestamp
519 && zend_string_equals(val, PS(session_vars))
520 ) {
521 ret = PS(mod)->s_update_timestamp(&PS(mod_data), PS(id), val, PS(gc_maxlifetime));
522 handler_function_name = handler_class_name != NULL ? "updateTimestamp" : "update_timestamp";
523 } else {
524 ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, PS(gc_maxlifetime));
525 handler_function_name = "write";
526 }
528 } else {
529 ret = PS(mod)->s_write(&PS(mod_data), PS(id), ZSTR_EMPTY_ALLOC(), PS(gc_maxlifetime));
530 handler_function_name = "write";
531 }
532 }
533
534 if ((ret == FAILURE) && !EG(exception)) {
535 if (!PS(mod_user_implemented)) {
536 php_error_docref(NULL, E_WARNING, "Failed to write session data (%s). Please "
537 "verify that the current setting of session.save_path "
538 "is correct (%s)",
539 PS(mod)->s_name,
540 PS(save_path));
541 } else if (handler_class_name != NULL) {
542 php_error_docref(NULL, E_WARNING, "Failed to write session data using user "
543 "defined save handler. (session.save_path: %s, handler: %s::%s)", PS(save_path),
544 ZSTR_VAL(handler_class_name), handler_function_name);
545 } else {
546 php_error_docref(NULL, E_WARNING, "Failed to write session data using user "
547 "defined save handler. (session.save_path: %s, handler: %s)", PS(save_path),
548 handler_function_name);
549 }
550 }
551 }
552 }
553
554 if (PS(mod_data) || PS(mod_user_implemented)) {
555 PS(mod)->s_close(&PS(mod_data));
556 }
557}
558/* }}} */
559
560static void php_session_normalize_vars(void) /* {{{ */
561{
563
566 if (Z_TYPE_P(struc) == IS_PTR) {
567 zval *zv = (zval *)Z_PTR_P(struc);
568 ZVAL_COPY_VALUE(struc, zv);
569 ZVAL_UNDEF(zv);
570 }
571 );
572 }
573}
574/* }}} */
575
576/* *************************
577 * INI Settings/Handlers *
578 ************************* */
579
580static PHP_INI_MH(OnUpdateSaveHandler) /* {{{ */
581{
582 const ps_module *tmp;
583 int err_type = E_ERROR;
584
587
588 tmp = _php_find_ps_module(ZSTR_VAL(new_value));
589
590 if (stage == ZEND_INI_STAGE_RUNTIME) {
591 err_type = E_WARNING;
592 }
593
594 if (PG(modules_activated) && !tmp) {
595 /* Do not output error when restoring ini options. */
596 if (stage != ZEND_INI_STAGE_DEACTIVATE) {
597 php_error_docref(NULL, err_type, "Session save handler \"%s\" cannot be found", ZSTR_VAL(new_value));
598 }
599
600 return FAILURE;
601 }
602
603 /* "user" save handler should not be set by user */
604 if (!PS(set_handler) && tmp == ps_user_ptr) {
605 php_error_docref(NULL, err_type, "Session save handler \"user\" cannot be set by ini_set()");
606 return FAILURE;
607 }
608
609 PS(default_mod) = PS(mod);
610 PS(mod) = tmp;
611
612 return SUCCESS;
613}
614/* }}} */
615
616static PHP_INI_MH(OnUpdateSerializer) /* {{{ */
617{
618 const ps_serializer *tmp;
619
622
623 tmp = _php_find_ps_serializer(ZSTR_VAL(new_value));
624
625 if (PG(modules_activated) && !tmp) {
626 int err_type;
627
628 if (stage == ZEND_INI_STAGE_RUNTIME) {
629 err_type = E_WARNING;
630 } else {
631 err_type = E_ERROR;
632 }
633
634 /* Do not output error when restoring ini options. */
635 if (stage != ZEND_INI_STAGE_DEACTIVATE) {
636 php_error_docref(NULL, err_type, "Serialization handler \"%s\" cannot be found", ZSTR_VAL(new_value));
637 }
638 return FAILURE;
639 }
640 PS(serializer) = tmp;
641
642 return SUCCESS;
643}
644/* }}} */
645
646static PHP_INI_MH(OnUpdateSaveDir) /* {{{ */
647{
650
651 /* Only do the safemode/open_basedir check at runtime */
652 if (stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) {
653 char *p;
654
655 if (memchr(ZSTR_VAL(new_value), '\0', ZSTR_LEN(new_value)) != NULL) {
656 return FAILURE;
657 }
658
659 /* we do not use zend_memrchr() since path can contain ; itself */
660 if ((p = strchr(ZSTR_VAL(new_value), ';'))) {
661 char *p2;
662 p++;
663 if ((p2 = strchr(p, ';'))) {
664 p = p2 + 1;
665 }
666 } else {
667 p = ZSTR_VAL(new_value);
668 }
669
670 if (PG(open_basedir) && *p && php_check_open_basedir(p)) {
671 return FAILURE;
672 }
673 }
674
675 return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
676}
677/* }}} */
678
679
680static PHP_INI_MH(OnUpdateName) /* {{{ */
681{
684
685 /* Numeric session.name won't work at all */
686 if (
687 ZSTR_LEN(new_value) == 0
688 || zend_str_has_nul_byte(new_value)
689 || is_numeric_str_function(new_value, NULL, NULL)
691 ) {
692 int err_type;
693
694 if (stage == ZEND_INI_STAGE_RUNTIME || stage == ZEND_INI_STAGE_ACTIVATE || stage == ZEND_INI_STAGE_STARTUP) {
695 err_type = E_WARNING;
696 } else {
697 err_type = E_ERROR;
698 }
699
700 /* Do not output error when restoring ini options. */
701 if (stage != ZEND_INI_STAGE_DEACTIVATE) {
702 php_error_docref(NULL, err_type, "session.name \"%s\" must not be numeric, empty, contain null bytes or any of the following characters \"" SESSION_FORBIDDEN_CHARS_FOR_ERROR_MSG "\"", ZSTR_VAL(new_value));
703 }
704 return FAILURE;
705 }
706
707 return OnUpdateStringUnempty(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
708}
709/* }}} */
710
711
712static PHP_INI_MH(OnUpdateCookieLifetime) /* {{{ */
713{
716
717#ifdef ZEND_ENABLE_ZVAL_LONG64
718 const zend_long maxcookie = ZEND_LONG_MAX - INT_MAX - 1;
719#else
720 const zend_long maxcookie = ZEND_LONG_MAX / 2 - 1;
721#endif
722 zend_long v = (zend_long)atol(ZSTR_VAL(new_value));
723 if (v < 0) {
724 php_error_docref(NULL, E_WARNING, "CookieLifetime cannot be negative");
725 return FAILURE;
726 } else if (v > maxcookie) {
727 return SUCCESS;
728 }
729 return OnUpdateLongGEZero(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
730}
731/* }}} */
732
733
734static PHP_INI_MH(OnUpdateSessionLong) /* {{{ */
735{
738 return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
739}
740/* }}} */
741
742
743static PHP_INI_MH(OnUpdateSessionString) /* {{{ */
744{
747 return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
748}
749/* }}} */
750
751
752static PHP_INI_MH(OnUpdateSessionBool) /* {{{ */
753{
756 return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
757}
758/* }}} */
759
760
761static PHP_INI_MH(OnUpdateSidLength) /* {{{ */
762{
764 char *endptr = NULL;
765
768 val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
769 if (val != 32) {
770 php_error_docref("session.configuration", E_DEPRECATED, "session.sid_length INI setting is deprecated");
771 }
772 if (endptr && (*endptr == '\0')
773 && val >= 22 && val <= PS_MAX_SID_LENGTH) {
774 /* Numeric value */
775 PS(sid_length) = val;
776 return SUCCESS;
777 }
778
779 php_error_docref(NULL, E_WARNING, "session.configuration \"session.sid_length\" must be between 22 and 256");
780 return FAILURE;
781}
782/* }}} */
783
784static PHP_INI_MH(OnUpdateSidBits) /* {{{ */
785{
787 char *endptr = NULL;
788
791 val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
792 if (val != 4) {
793 php_error_docref("session.configuration", E_DEPRECATED, "session.sid_bits_per_character INI setting is deprecated");
794 }
795 if (endptr && (*endptr == '\0')
796 && val >= 4 && val <=6) {
797 /* Numeric value */
798 PS(sid_bits_per_character) = val;
799 return SUCCESS;
800 }
801
802 php_error_docref(NULL, E_WARNING, "session.configuration \"session.sid_bits_per_character\" must be between 4 and 6");
803 return FAILURE;
804}
805/* }}} */
806
807static PHP_INI_MH(OnUpdateSessionGcProbability) /* {{{ */
808{
811
812 zend_long tmp = zend_ini_parse_quantity_warn(new_value, entry->name);
813
814 if (tmp < 0) {
815 php_error_docref("session.gc_probability", E_WARNING, "session.gc_probability must be greater than or equal to 0");
816 return FAILURE;
817 }
818
820 *p = tmp;
821
822 return SUCCESS;
823}
824/* }}} */
825
826static PHP_INI_MH(OnUpdateSessionDivisor) /* {{{ */
827{
830
831 zend_long tmp = zend_ini_parse_quantity_warn(new_value, entry->name);
832
833 if (tmp <= 0) {
834 php_error_docref("session.gc_divisor", E_WARNING, "session.gc_divisor must be greater than 0");
835 return FAILURE;
836 }
837
839 *p = tmp;
840
841 return SUCCESS;
842}
843/* }}} */
844
845static PHP_INI_MH(OnUpdateRfc1867Freq) /* {{{ */
846{
847 int tmp = ZEND_ATOL(ZSTR_VAL(new_value));
848 if(tmp < 0) {
849 php_error_docref(NULL, E_WARNING, "session.upload_progress.freq must be greater than or equal to 0");
850 return FAILURE;
851 }
852 if(ZSTR_LEN(new_value) > 0 && ZSTR_VAL(new_value)[ZSTR_LEN(new_value)-1] == '%') {
853 if(tmp > 100) {
854 php_error_docref(NULL, E_WARNING, "session.upload_progress.freq must be less than or equal to 100%%");
855 return FAILURE;
856 }
857 PS(rfc1867_freq) = -tmp;
858 } else {
859 PS(rfc1867_freq) = tmp;
860 }
861 return SUCCESS;
862} /* }}} */
863
864static PHP_INI_MH(OnUpdateUseOnlyCookies)
865{
868 bool *p = (bool *) ZEND_INI_GET_ADDR();
869 *p = zend_ini_parse_bool(new_value);
870 if (!*p) {
871 php_error_docref("session.configuration", E_DEPRECATED, "Disabling session.use_only_cookies INI setting is deprecated");
872 }
873 return SUCCESS;
874}
875
876static PHP_INI_MH(OnUpdateUseTransSid)
877{
880 bool *p = (bool *) ZEND_INI_GET_ADDR();
881 *p = zend_ini_parse_bool(new_value);
882 if (*p) {
883 php_error_docref("session.configuration", E_DEPRECATED, "Enabling session.use_trans_sid INI setting is deprecated");
884 }
885 return SUCCESS;
886}
887
888static PHP_INI_MH(OnUpdateRefererCheck)
889{
892 if (ZSTR_LEN(new_value) != 0) {
893 php_error_docref("session.configuration", E_DEPRECATED, "Usage of session.referer_check INI setting is deprecated");
894 }
895 return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
896}
897
898/* {{{ PHP_INI */
900 STD_PHP_INI_ENTRY("session.save_path", "", PHP_INI_ALL, OnUpdateSaveDir, save_path, php_ps_globals, ps_globals)
901 STD_PHP_INI_ENTRY("session.name", "PHPSESSID", PHP_INI_ALL, OnUpdateName, session_name, php_ps_globals, ps_globals)
902 PHP_INI_ENTRY("session.save_handler", "files", PHP_INI_ALL, OnUpdateSaveHandler)
903 STD_PHP_INI_BOOLEAN("session.auto_start", "0", PHP_INI_PERDIR, OnUpdateBool, auto_start, php_ps_globals, ps_globals)
904 STD_PHP_INI_ENTRY("session.gc_probability", "1", PHP_INI_ALL, OnUpdateSessionGcProbability, gc_probability, php_ps_globals, ps_globals)
905 STD_PHP_INI_ENTRY("session.gc_divisor", "100", PHP_INI_ALL, OnUpdateSessionDivisor,gc_divisor, php_ps_globals, ps_globals)
906 STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateSessionLong, gc_maxlifetime, php_ps_globals, ps_globals)
907 PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer)
908 STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateCookieLifetime,cookie_lifetime, php_ps_globals, ps_globals)
909 STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateSessionString, cookie_path, php_ps_globals, ps_globals)
910 STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionString, cookie_domain, php_ps_globals, ps_globals)
911 STD_PHP_INI_BOOLEAN("session.cookie_secure", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_secure, php_ps_globals, ps_globals)
912 STD_PHP_INI_BOOLEAN("session.cookie_httponly", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_httponly, php_ps_globals, ps_globals)
913 STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateSessionString, cookie_samesite, php_ps_globals, ps_globals)
914 STD_PHP_INI_BOOLEAN("session.use_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_cookies, php_ps_globals, ps_globals)
915 STD_PHP_INI_BOOLEAN("session.use_only_cookies", "1", PHP_INI_ALL, OnUpdateUseOnlyCookies, use_only_cookies, php_ps_globals, ps_globals)
916 STD_PHP_INI_BOOLEAN("session.use_strict_mode", "0", PHP_INI_ALL, OnUpdateSessionBool, use_strict_mode, php_ps_globals, ps_globals)
917 STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateRefererCheck, extern_referer_chk, php_ps_globals, ps_globals)
918 STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateSessionString, cache_limiter, php_ps_globals, ps_globals)
919 STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateSessionLong, cache_expire, php_ps_globals, ps_globals)
920 STD_PHP_INI_BOOLEAN("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateUseTransSid, use_trans_sid, php_ps_globals, ps_globals)
921 PHP_INI_ENTRY("session.sid_length", "32", PHP_INI_ALL, OnUpdateSidLength)
922 PHP_INI_ENTRY("session.sid_bits_per_character", "4", PHP_INI_ALL, OnUpdateSidBits)
923 STD_PHP_INI_BOOLEAN("session.lazy_write", "1", PHP_INI_ALL, OnUpdateSessionBool, lazy_write, php_ps_globals, ps_globals)
924
925 /* Upload progress */
926 STD_PHP_INI_BOOLEAN("session.upload_progress.enabled",
927 "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_enabled, php_ps_globals, ps_globals)
928 STD_PHP_INI_BOOLEAN("session.upload_progress.cleanup",
929 "1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_cleanup, php_ps_globals, ps_globals)
930 STD_PHP_INI_ENTRY("session.upload_progress.prefix",
931 "upload_progress_", ZEND_INI_PERDIR, OnUpdateString, rfc1867_prefix, php_ps_globals, ps_globals)
932 STD_PHP_INI_ENTRY("session.upload_progress.name",
933 "PHP_SESSION_UPLOAD_PROGRESS", ZEND_INI_PERDIR, OnUpdateString, rfc1867_name, php_ps_globals, ps_globals)
934 STD_PHP_INI_ENTRY("session.upload_progress.freq", "1%", ZEND_INI_PERDIR, OnUpdateRfc1867Freq, rfc1867_freq, php_ps_globals, ps_globals)
935 STD_PHP_INI_ENTRY("session.upload_progress.min_freq",
936 "1", ZEND_INI_PERDIR, OnUpdateReal, rfc1867_min_freq,php_ps_globals, ps_globals)
937
938 /* Commented out until future discussion */
939 /* PHP_INI_ENTRY("session.encode_sources", "globals,track", PHP_INI_ALL, NULL) */
941/* }}} */
942
943/* ***************
944 * Serializers *
945 *************** */
946PS_SERIALIZER_ENCODE_FUNC(php_serialize) /* {{{ */
947{
948 smart_str buf = {0};
950
953 php_var_serialize(&buf, Z_REFVAL(PS(http_session_vars)), &var_hash);
955 }
956 return buf.s;
957}
958/* }}} */
959
960PS_SERIALIZER_DECODE_FUNC(php_serialize) /* {{{ */
961{
962 const char *endptr = val + vallen;
965 bool result;
967
971 &session_vars, (const unsigned char **)&val, (const unsigned char *)endptr, &var_hash);
977
978 if (!Z_ISUNDEF(PS(http_session_vars))) {
979 zval_ptr_dtor(&PS(http_session_vars));
980 }
984 ZVAL_NEW_REF(&PS(http_session_vars), &session_vars);
985 Z_ADDREF_P(&PS(http_session_vars));
986 zend_hash_update_ind(&EG(symbol_table), var_name, &PS(http_session_vars));
988 return result || !vallen ? SUCCESS : FAILURE;
989}
990/* }}} */
991
992#define PS_BIN_NR_OF_BITS 8
993#define PS_BIN_UNDEF (1<<(PS_BIN_NR_OF_BITS-1))
994#define PS_BIN_MAX (PS_BIN_UNDEF-1)
995
996PS_SERIALIZER_ENCODE_FUNC(php_binary) /* {{{ */
997{
998 smart_str buf = {0};
1001
1003
1005 if (ZSTR_LEN(key) > PS_BIN_MAX) continue;
1006 smart_str_appendc(&buf, (unsigned char)ZSTR_LEN(key));
1007 smart_str_appendl(&buf, ZSTR_VAL(key), ZSTR_LEN(key));
1008 php_var_serialize(&buf, struc, &var_hash);
1009 );
1010
1011 smart_str_0(&buf);
1013
1014 return buf.s;
1015}
1016/* }}} */
1017
1018PS_SERIALIZER_DECODE_FUNC(php_binary) /* {{{ */
1019{
1020 const char *p;
1021 const char *endptr = val + vallen;
1025
1027
1028 for (p = val; p < endptr; ) {
1029 size_t namelen = ((unsigned char)(*p)) & (~PS_BIN_UNDEF);
1030
1031 if (namelen > PS_BIN_MAX || (p + namelen) >= endptr) {
1033 return FAILURE;
1034 }
1035
1036 name = zend_string_init(p + 1, namelen, 0);
1037 p += namelen + 1;
1039
1040 if (php_var_unserialize(current, (const unsigned char **) &p, (const unsigned char *) endptr, &var_hash)) {
1041 ZVAL_PTR(&rv, current);
1043 } else {
1045 php_session_normalize_vars();
1047 return FAILURE;
1048 }
1050 }
1051
1052 php_session_normalize_vars();
1054
1055 return SUCCESS;
1056}
1057/* }}} */
1058
1059#define PS_DELIMITER '|'
1060
1061PS_SERIALIZER_ENCODE_FUNC(php) /* {{{ */
1062{
1063 smart_str buf = {0};
1065 bool fail = false;
1067
1069
1071 smart_str_appendl(&buf, ZSTR_VAL(key), ZSTR_LEN(key));
1072 if (memchr(ZSTR_VAL(key), PS_DELIMITER, ZSTR_LEN(key))) {
1074 smart_str_free(&buf);
1075 fail = true;
1076 break;
1077 }
1078 smart_str_appendc(&buf, PS_DELIMITER);
1079 php_var_serialize(&buf, struc, &var_hash);
1080 );
1081
1082 if (fail) {
1083 return NULL;
1084 }
1085
1086 smart_str_0(&buf);
1087
1089 return buf.s;
1090}
1091/* }}} */
1092
1093PS_SERIALIZER_DECODE_FUNC(php) /* {{{ */
1094{
1095 const char *p, *q;
1096 const char *endptr = val + vallen;
1101 zval *current, rv;
1102
1104
1106
1107 while (p < endptr) {
1108 q = p;
1109 while (*q != PS_DELIMITER) {
1110 if (++q >= endptr) {
1111 retval = FAILURE;
1112 goto break_outer_loop;
1113 }
1114 }
1115
1116 namelen = q - p;
1117 name = zend_string_init(p, namelen, 0);
1118 q++;
1119
1121 if (php_var_unserialize(current, (const unsigned char **)&q, (const unsigned char *)endptr, &var_hash)) {
1122 ZVAL_PTR(&rv, current);
1124 } else {
1126 retval = FAILURE;
1127 goto break_outer_loop;
1128 }
1130 p = q;
1131 }
1132
1133break_outer_loop:
1134 php_session_normalize_vars();
1135
1137
1138 return retval;
1139}
1140/* }}} */
1141
1142#define MAX_SERIALIZERS 32
1143#define PREDEFINED_SERIALIZERS 3
1144
1145static ps_serializer ps_serializers[MAX_SERIALIZERS + 1] = {
1146 PS_SERIALIZER_ENTRY(php_serialize),
1148 PS_SERIALIZER_ENTRY(php_binary)
1149};
1150
1152{
1154
1155 for (int i = 0; i < MAX_SERIALIZERS; i++) {
1156 if (ps_serializers[i].name == NULL) {
1157 ps_serializers[i].name = name;
1158 ps_serializers[i].encode = encode;
1159 ps_serializers[i].decode = decode;
1160 ps_serializers[i + 1].name = NULL;
1161 ret = SUCCESS;
1162 break;
1163 }
1164 }
1165 return ret;
1166}
1167/* }}} */
1168
1169/* *******************
1170 * Storage Modules *
1171 ******************* */
1172
1173#define MAX_MODULES 32
1174#define PREDEFINED_MODULES 2
1175
1176static const ps_module *ps_modules[MAX_MODULES + 1] = {
1179};
1180
1182{
1183 int ret = FAILURE;
1184
1185 for (int i = 0; i < MAX_MODULES; i++) {
1186 if (!ps_modules[i]) {
1187 ps_modules[i] = ptr;
1188 ret = SUCCESS;
1189 break;
1190 }
1191 }
1192 return ret;
1193}
1194/* }}} */
1195
1196/* Dummy PS module function */
1197/* We consider any ID valid (thus also implying that a session with such an ID exists),
1198 thus we always return SUCCESS */
1202
1203/* Dummy PS module function */
1207
1208
1209/* ******************
1210 * Cache Limiters *
1211 ****************** */
1212
1213typedef struct {
1214 char *name;
1217
1218#define CACHE_LIMITER(name) _php_cache_limiter_##name
1219#define CACHE_LIMITER_FUNC(name) static void CACHE_LIMITER(name)(void)
1220#define CACHE_LIMITER_ENTRY(name) { #name, CACHE_LIMITER(name) },
1221#define ADD_HEADER(a) sapi_add_header(a, strlen(a), 1);
1222#define MAX_STR 512
1223
1224static const char *month_names[] = {
1225 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1226 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1227};
1228
1229static const char *week_days[] = {
1230 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
1231};
1232
1233static inline void strcpy_gmt(char *ubuf, time_t *when) /* {{{ */
1234{
1235 char buf[MAX_STR];
1236 struct tm tm, *res;
1237 int n;
1238
1239 res = php_gmtime_r(when, &tm);
1240
1241 if (!res) {
1242 ubuf[0] = '\0';
1243 return;
1244 }
1245
1246 n = slprintf(buf, sizeof(buf), "%s, %02d %s %d %02d:%02d:%02d GMT", /* SAFE */
1247 week_days[tm.tm_wday], tm.tm_mday,
1248 month_names[tm.tm_mon], tm.tm_year + 1900,
1249 tm.tm_hour, tm.tm_min,
1250 tm.tm_sec);
1251 memcpy(ubuf, buf, n);
1252 ubuf[n] = '\0';
1253}
1254/* }}} */
1255
1256static inline void last_modified(void) /* {{{ */
1257{
1258 const char *path;
1259 zend_stat_t sb = {0};
1260 char buf[MAX_STR + 1];
1261
1262 path = SG(request_info).path_translated;
1263 if (path) {
1264 if (VCWD_STAT(path, &sb) == -1) {
1265 return;
1266 }
1267
1268#define LAST_MODIFIED "Last-Modified: "
1269 memcpy(buf, LAST_MODIFIED, sizeof(LAST_MODIFIED) - 1);
1270 strcpy_gmt(buf + sizeof(LAST_MODIFIED) - 1, &sb.st_mtime);
1271 ADD_HEADER(buf);
1272 }
1273}
1274/* }}} */
1275
1276#define EXPIRES "Expires: "
1277CACHE_LIMITER_FUNC(public) /* {{{ */
1278{
1279 char buf[MAX_STR + 1];
1280 struct timeval tv;
1281 time_t now;
1282
1283 gettimeofday(&tv, NULL);
1284 now = tv.tv_sec + PS(cache_expire) * 60;
1285 memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1);
1286 strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now);
1288
1289 snprintf(buf, sizeof(buf) , "Cache-Control: public, max-age=" ZEND_LONG_FMT, PS(cache_expire) * 60); /* SAFE */
1290 ADD_HEADER(buf);
1291
1292 last_modified();
1293}
1294/* }}} */
1295
1297{
1298 char buf[MAX_STR + 1];
1299
1300 snprintf(buf, sizeof(buf), "Cache-Control: private, max-age=" ZEND_LONG_FMT, PS(cache_expire) * 60); /* SAFE */
1301 ADD_HEADER(buf);
1302
1303 last_modified();
1304}
1305/* }}} */
1306
1307CACHE_LIMITER_FUNC(private) /* {{{ */
1308{
1309 ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
1311}
1312/* }}} */
1313
1314CACHE_LIMITER_FUNC(nocache) /* {{{ */
1315{
1316 ADD_HEADER("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
1317
1318 /* For HTTP/1.1 conforming clients */
1319 ADD_HEADER("Cache-Control: no-store, no-cache, must-revalidate");
1320
1321 /* For HTTP/1.0 conforming clients */
1322 ADD_HEADER("Pragma: no-cache");
1323}
1324/* }}} */
1325
1326static const php_session_cache_limiter_t php_session_cache_limiters[] = {
1327 CACHE_LIMITER_ENTRY(public)
1328 CACHE_LIMITER_ENTRY(private)
1330 CACHE_LIMITER_ENTRY(nocache)
1331 {0}
1332};
1333
1334static int php_session_cache_limiter(void) /* {{{ */
1335{
1336 const php_session_cache_limiter_t *lim;
1337
1338 if (PS(cache_limiter)[0] == '\0') return 0;
1339 if (PS(session_status) != php_session_active) return -1;
1340
1341 if (SG(headers_sent)) {
1344
1345 php_session_abort();
1347 php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be sent after headers have already been sent (output started at %s:%d)", output_start_filename, output_start_lineno);
1348 } else {
1349 php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be sent after headers have already been sent");
1350 }
1351 return -2;
1352 }
1353
1354 for (lim = php_session_cache_limiters; lim->name; lim++) {
1355 if (!strcasecmp(lim->name, PS(cache_limiter))) {
1356 lim->func();
1357 return 0;
1358 }
1359 }
1360
1361 return -1;
1362}
1363/* }}} */
1364
1365/* *********************
1366 * Cookie Management *
1367 ********************* */
1368
1369/*
1370 * Remove already sent session ID cookie.
1371 * It must be directly removed from SG(sapi_header) because sapi_add_header_ex()
1372 * removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
1373 */
1374static void php_session_remove_cookie(void) {
1376 zend_llist *l = &SG(sapi_headers).headers;
1379 char *session_cookie;
1380 size_t session_cookie_len;
1381 size_t len = sizeof("Set-Cookie")-1;
1382
1384 spprintf(&session_cookie, 0, "Set-Cookie: %s=", PS(session_name));
1385
1386 session_cookie_len = strlen(session_cookie);
1387 current = l->head;
1388 while (current) {
1389 header = (sapi_header_struct *)(current->data);
1390 next = current->next;
1391 if (header->header_len > len && header->header[len] == ':'
1392 && !strncmp(header->header, session_cookie, session_cookie_len)) {
1393 if (current->prev) {
1394 current->prev->next = next;
1395 } else {
1396 l->head = next;
1397 }
1398 if (next) {
1399 next->prev = current->prev;
1400 } else {
1401 l->tail = current->prev;
1402 }
1404 efree(current);
1405 --l->count;
1406 }
1407 current = next;
1408 }
1409 efree(session_cookie);
1410}
1411
1412static zend_result php_session_send_cookie(void) /* {{{ */
1413{
1414 smart_str ncookie = {0};
1415 zend_string *date_fmt = NULL;
1416 zend_string *e_id;
1417
1418 if (SG(headers_sent)) {
1421
1423 php_error_docref(NULL, E_WARNING, "Session cookie cannot be sent after headers have already been sent (output started at %s:%d)", output_start_filename, output_start_lineno);
1424 } else {
1425 php_error_docref(NULL, E_WARNING, "Session cookie cannot be sent after headers have already been sent");
1426 }
1427 return FAILURE;
1428 }
1429
1431
1432 /* URL encode id because it might be user supplied */
1433 e_id = php_url_encode(ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)));
1434
1435 smart_str_appendl(&ncookie, "Set-Cookie: ", sizeof("Set-Cookie: ")-1);
1436 smart_str_appendl(&ncookie, PS(session_name), strlen(PS(session_name)));
1437 smart_str_appendc(&ncookie, '=');
1438 smart_str_appendl(&ncookie, ZSTR_VAL(e_id), ZSTR_LEN(e_id));
1439
1440 zend_string_release_ex(e_id, 0);
1441
1442 if (PS(cookie_lifetime) > 0) {
1443 struct timeval tv;
1444 time_t t;
1445
1446 gettimeofday(&tv, NULL);
1447 t = tv.tv_sec + PS(cookie_lifetime);
1448
1449 if (t > 0) {
1450 date_fmt = php_format_date("D, d M Y H:i:s \\G\\M\\T", sizeof("D, d M Y H:i:s \\G\\M\\T")-1, t, 0);
1451 smart_str_appends(&ncookie, COOKIE_EXPIRES);
1452 smart_str_appendl(&ncookie, ZSTR_VAL(date_fmt), ZSTR_LEN(date_fmt));
1453 zend_string_release_ex(date_fmt, 0);
1454
1455 smart_str_appends(&ncookie, COOKIE_MAX_AGE);
1456 smart_str_append_long(&ncookie, PS(cookie_lifetime));
1457 }
1458 }
1459
1460 if (PS(cookie_path)[0]) {
1461 smart_str_appends(&ncookie, COOKIE_PATH);
1462 smart_str_appends(&ncookie, PS(cookie_path));
1463 }
1464
1465 if (PS(cookie_domain)[0]) {
1466 smart_str_appends(&ncookie, COOKIE_DOMAIN);
1467 smart_str_appends(&ncookie, PS(cookie_domain));
1468 }
1469
1470 if (PS(cookie_secure)) {
1471 smart_str_appends(&ncookie, COOKIE_SECURE);
1472 }
1473
1474 if (PS(cookie_httponly)) {
1475 smart_str_appends(&ncookie, COOKIE_HTTPONLY);
1476 }
1477
1478 if (PS(cookie_samesite)[0]) {
1479 smart_str_appends(&ncookie, COOKIE_SAMESITE);
1480 smart_str_appends(&ncookie, PS(cookie_samesite));
1481 }
1482
1483 smart_str_0(&ncookie);
1484
1485 php_session_remove_cookie(); /* remove already sent session ID cookie */
1486 /* 'replace' must be 0 here, else a previous Set-Cookie
1487 header, probably sent with setcookie() will be replaced! */
1488 sapi_add_header_ex(estrndup(ZSTR_VAL(ncookie.s), ZSTR_LEN(ncookie.s)), ZSTR_LEN(ncookie.s), 0, 0);
1489 smart_str_free(&ncookie);
1490
1491 return SUCCESS;
1492}
1493/* }}} */
1494
1495PHPAPI const ps_module *_php_find_ps_module(const char *name) /* {{{ */
1496{
1497 const ps_module *ret = NULL;
1498 const ps_module **mod;
1499 int i;
1500
1501 for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
1502 if (*mod && !strcasecmp(name, (*mod)->s_name)) {
1503 ret = *mod;
1504 break;
1505 }
1506 }
1507 return ret;
1508}
1509/* }}} */
1510
1512{
1513 const ps_serializer *ret = NULL;
1514 const ps_serializer *mod;
1515
1516 for (mod = ps_serializers; mod->name; mod++) {
1517 if (!strcasecmp(name, mod->name)) {
1518 ret = mod;
1519 break;
1520 }
1521 }
1522 return ret;
1523}
1524/* }}} */
1525
1526static void ppid2sid(zval *ppid) {
1527 ZVAL_DEREF(ppid);
1528 if (Z_TYPE_P(ppid) == IS_STRING) {
1529 PS(id) = zend_string_init(Z_STRVAL_P(ppid), Z_STRLEN_P(ppid), 0);
1530 PS(send_cookie) = 0;
1531 } else {
1532 PS(id) = NULL;
1533 PS(send_cookie) = 1;
1534 }
1535}
1536
1537
1539{
1540 int module_number = PS(module_number);
1541 zval *sid, *data, *ppid;
1542 bool apply_trans_sid;
1543
1544 if (!PS(id)) {
1545 php_error_docref(NULL, E_WARNING, "Cannot set session ID - session ID is not initialized");
1546 return FAILURE;
1547 }
1548
1549 if (PS(use_cookies) && PS(send_cookie)) {
1550 zend_result cookies_sent = php_session_send_cookie();
1551 if (UNEXPECTED(cookies_sent == FAILURE)) {
1552 return FAILURE;
1553 }
1554 PS(send_cookie) = 0;
1555 }
1556
1557 /* If the SID constant exists, destroy it. */
1558 /* We must not delete any items in EG(zend_constants) */
1559 /* zend_hash_str_del(EG(zend_constants), "sid", sizeof("sid") - 1); */
1560 sid = zend_get_constant_str("SID", sizeof("SID") - 1);
1561
1562 if (PS(define_sid)) {
1563 smart_str var = {0};
1564
1565 smart_str_appends(&var, PS(session_name));
1566 smart_str_appendc(&var, '=');
1567 smart_str_appends(&var, ZSTR_VAL(PS(id)));
1568 smart_str_0(&var);
1569 if (sid) {
1570 zval_ptr_dtor(sid);
1571 ZVAL_STR(sid, smart_str_extract(&var));
1572 } else {
1574 smart_str_free(&var);
1575 }
1576 } else {
1577 if (sid) {
1578 zval_ptr_dtor(sid);
1579 ZVAL_EMPTY_STRING(sid);
1580 } else {
1582 }
1583 }
1584
1585 /* Apply trans sid if sid cookie is not set */
1586 apply_trans_sid = 0;
1587 if (APPLY_TRANS_SID) {
1588 apply_trans_sid = 1;
1589 if (PS(use_cookies) &&
1590 (data = zend_hash_str_find(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE") - 1))) {
1592 if (Z_TYPE_P(data) == IS_ARRAY &&
1594 ZVAL_DEREF(ppid);
1595 apply_trans_sid = 0;
1596 }
1597 }
1598 }
1599 if (apply_trans_sid) {
1600 zend_string *sname;
1601 sname = zend_string_init(PS(session_name), strlen(PS(session_name)), 0);
1602 php_url_scanner_reset_session_var(sname, 1); /* This may fail when session name has changed */
1603 zend_string_release_ex(sname, 0);
1605 }
1606 return SUCCESS;
1607}
1608/* }}} */
1609
1610
1612{
1613 zval *ppid;
1614 zval *data;
1615 char *value;
1616 size_t lensess;
1617
1618 switch (PS(session_status)) {
1619 case php_session_active:
1620 if (PS(session_started_filename)) {
1621 php_error(E_NOTICE, "Ignoring session_start() because a session has already been started (started from %s on line %"PRIu32")", ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
1622 } else if (PS(auto_start)) {
1623 /* This option can't be changed at runtime, so we can assume it's because of this */
1624 php_error(E_NOTICE, "Ignoring session_start() because a session has already been started automatically");
1625 } else {
1626 php_error(E_NOTICE, "Ignoring session_start() because a session has already been started");
1627 }
1628 return FAILURE;
1629 break;
1630
1632 value = zend_ini_string("session.save_handler", sizeof("session.save_handler") - 1, 0);
1633 if (!PS(mod) && value) {
1635 if (!PS(mod)) {
1636 php_error_docref(NULL, E_WARNING, "Cannot find session save handler \"%s\" - session startup failed", value);
1637 return FAILURE;
1638 }
1639 }
1640 value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler") - 1, 0);
1641 if (!PS(serializer) && value) {
1642 PS(serializer) = _php_find_ps_serializer(value);
1643 if (!PS(serializer)) {
1644 php_error_docref(NULL, E_WARNING, "Cannot find session serialization handler \"%s\" - session startup failed", value);
1645 return FAILURE;
1646 }
1647 }
1650
1651 case php_session_none:
1652 default:
1653 /* Setup internal flags */
1654 PS(define_sid) = !PS(use_only_cookies); /* SID constant is defined when non-cookie ID is used */
1655 PS(send_cookie) = PS(use_cookies) || PS(use_only_cookies);
1656 }
1657
1658 lensess = strlen(PS(session_name));
1659
1660 /*
1661 * Cookies are preferred, because initially cookie and get
1662 * variables will be available.
1663 * URL/POST session ID may be used when use_only_cookies=Off.
1664 * session.use_strice_mode=On prevents session adoption.
1665 * Session based file upload progress uses non-cookie ID.
1666 */
1667
1668 if (!PS(id)) {
1669 if (PS(use_cookies) && (data = zend_hash_str_find(&EG(symbol_table), "_COOKIE", sizeof("_COOKIE") - 1))) {
1671 if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
1672 ppid2sid(ppid);
1673 PS(send_cookie) = 0;
1674 PS(define_sid) = 0;
1675 }
1676 }
1677 /* Initialize session ID from non cookie values */
1678 if (!PS(use_only_cookies)) {
1679 if (!PS(id) && (data = zend_hash_str_find(&EG(symbol_table), "_GET", sizeof("_GET") - 1))) {
1681 if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
1682 ppid2sid(ppid);
1683 }
1684 }
1685 if (!PS(id) && (data = zend_hash_str_find(&EG(symbol_table), "_POST", sizeof("_POST") - 1))) {
1687 if (Z_TYPE_P(data) == IS_ARRAY && (ppid = zend_hash_str_find(Z_ARRVAL_P(data), PS(session_name), lensess))) {
1688 ppid2sid(ppid);
1689 }
1690 }
1691 /* Check whether the current request was referred to by
1692 * an external site which invalidates the previously found id. */
1693 if (PS(id) && PS(extern_referer_chk)[0] != '\0' &&
1694 !Z_ISUNDEF(PG(http_globals)[TRACK_VARS_SERVER]) &&
1695 (data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_REFERER", sizeof("HTTP_REFERER") - 1)) &&
1696 Z_TYPE_P(data) == IS_STRING &&
1697 Z_STRLEN_P(data) != 0 &&
1698 strstr(Z_STRVAL_P(data), PS(extern_referer_chk)) == NULL
1699 ) {
1700 zend_string_release_ex(PS(id), 0);
1701 PS(id) = NULL;
1702 }
1703 }
1704 }
1705
1706 /* Finally check session id for dangerous characters
1707 * Security note: session id may be embedded in HTML pages.*/
1708 if (PS(id) && strpbrk(ZSTR_VAL(PS(id)), "\r\n\t <>'\"\\")) {
1709 zend_string_release_ex(PS(id), 0);
1710 PS(id) = NULL;
1711 }
1712
1713 if (php_session_initialize() == FAILURE
1714 || php_session_cache_limiter() == -2) {
1716 if (PS(id)) {
1717 zend_string_release_ex(PS(id), 0);
1718 PS(id) = NULL;
1719 }
1720 return FAILURE;
1721 }
1722
1723 return SUCCESS;
1724}
1725/* }}} */
1726
1728{
1730 php_session_save_current_state(write);
1732 return SUCCESS;
1733 }
1734 return FAILURE;
1735}
1736/* }}} */
1737
1742
1743static zend_result php_session_abort(void) /* {{{ */
1744{
1746 if (PS(mod_data) || PS(mod_user_implemented)) {
1747 PS(mod)->s_close(&PS(mod_data));
1748 }
1750 return SUCCESS;
1751 }
1752 return FAILURE;
1753}
1754/* }}} */
1755
1756static zend_result php_session_reset(void) /* {{{ */
1757{
1759 && php_session_initialize() == SUCCESS) {
1760 return SUCCESS;
1761 }
1762 return FAILURE;
1763}
1764/* }}} */
1765
1766
1767/* This API is not used by any PHP modules including session currently.
1768 session_adapt_url() may be used to set Session ID to target url without
1769 starting "URL-Rewriter" output handler. */
1770PHPAPI void session_adapt_url(const char *url, size_t url_len, char **new_url, size_t *new_len) /* {{{ */
1771{
1773 *new_url = php_url_scanner_adapt_single_url(url, url_len, PS(session_name), ZSTR_VAL(PS(id)), new_len, 1);
1774 }
1775}
1776/* }}} */
1777
1778/* ********************************
1779 * Userspace exported functions *
1780 ******************************** */
1781
1782/* {{{ session_set_cookie_params(array options)
1783 Set session cookie parameters */
1785{
1786 HashTable *options_ht;
1787 zend_long lifetime_long;
1788 zend_string *lifetime = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
1789 bool secure = 0, secure_null = 1;
1790 bool httponly = 0, httponly_null = 1;
1791 zend_string *ini_name;
1793 int found = 0;
1794
1796 Z_PARAM_ARRAY_HT_OR_LONG(options_ht, lifetime_long)
1799 Z_PARAM_STR_OR_NULL(domain)
1800 Z_PARAM_BOOL_OR_NULL(secure, secure_null)
1801 Z_PARAM_BOOL_OR_NULL(httponly, httponly_null)
1803
1804 if (!PS(use_cookies)) {
1805 php_error_docref(NULL, E_WARNING, "Session cookies cannot be used when session.use_cookies is disabled");
1807 }
1808
1810 php_error_docref(NULL, E_WARNING, "Session cookie parameters cannot be changed when a session is active");
1812 }
1813
1814 if (SG(headers_sent)) {
1815 php_error_docref(NULL, E_WARNING, "Session cookie parameters cannot be changed after headers have already been sent");
1817 }
1818
1819 if (options_ht) {
1821 zval *value;
1822
1823 if (path) {
1824 zend_argument_value_error(2, "must be null when argument #1 ($lifetime_or_options) is an array");
1825 RETURN_THROWS();
1826 }
1827
1828 if (domain) {
1829 zend_argument_value_error(3, "must be null when argument #1 ($lifetime_or_options) is an array");
1830 RETURN_THROWS();
1831 }
1832
1833 if (!secure_null) {
1834 zend_argument_value_error(4, "must be null when argument #1 ($lifetime_or_options) is an array");
1835 RETURN_THROWS();
1836 }
1837
1838 if (!httponly_null) {
1839 zend_argument_value_error(5, "must be null when argument #1 ($lifetime_or_options) is an array");
1840 RETURN_THROWS();
1841 }
1843 if (key) {
1845 if (zend_string_equals_literal_ci(key, "lifetime")) {
1846 lifetime = zval_get_string(value);
1847 found++;
1848 } else if (zend_string_equals_literal_ci(key, "path")) {
1849 path = zval_get_string(value);
1850 found++;
1851 } else if (zend_string_equals_literal_ci(key, "domain")) {
1852 domain = zval_get_string(value);
1853 found++;
1854 } else if (zend_string_equals_literal_ci(key, "secure")) {
1855 secure = zval_is_true(value);
1856 secure_null = 0;
1857 found++;
1858 } else if (zend_string_equals_literal_ci(key, "httponly")) {
1859 httponly = zval_is_true(value);
1860 httponly_null = 0;
1861 found++;
1862 } else if (zend_string_equals_literal_ci(key, "samesite")) {
1863 samesite = zval_get_string(value);
1864 found++;
1865 } else {
1866 php_error_docref(NULL, E_WARNING, "Argument #1 ($lifetime_or_options) contains an unrecognized key \"%s\"", ZSTR_VAL(key));
1867 }
1868 } else {
1869 php_error_docref(NULL, E_WARNING, "Argument #1 ($lifetime_or_options) cannot contain numeric keys");
1870 }
1872
1873 if (found == 0) {
1874 zend_argument_value_error(1, "must contain at least 1 valid key");
1875 RETURN_THROWS();
1876 }
1877 } else {
1878 lifetime = zend_long_to_str(lifetime_long);
1879 }
1880
1881 /* Exception during string conversion */
1882 if (EG(exception)) {
1883 goto cleanup;
1884 }
1885
1886 if (lifetime) {
1887 ini_name = ZSTR_INIT_LITERAL("session.cookie_lifetime", 0);
1889 zend_string_release_ex(ini_name, 0);
1890 if (result == FAILURE) {
1892 goto cleanup;
1893 }
1894 }
1895 if (path) {
1896 ini_name = ZSTR_INIT_LITERAL("session.cookie_path", 0);
1898 zend_string_release_ex(ini_name, 0);
1899 if (result == FAILURE) {
1901 goto cleanup;
1902 }
1903 }
1904 if (domain) {
1905 ini_name = ZSTR_INIT_LITERAL("session.cookie_domain", 0);
1907 zend_string_release_ex(ini_name, 0);
1908 if (result == FAILURE) {
1910 goto cleanup;
1911 }
1912 }
1913 if (!secure_null) {
1914 ini_name = ZSTR_INIT_LITERAL("session.cookie_secure", 0);
1915 result = zend_alter_ini_entry_chars(ini_name, secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1916 zend_string_release_ex(ini_name, 0);
1917 if (result == FAILURE) {
1919 goto cleanup;
1920 }
1921 }
1922 if (!httponly_null) {
1923 ini_name = ZSTR_INIT_LITERAL("session.cookie_httponly", 0);
1924 result = zend_alter_ini_entry_chars(ini_name, httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1925 zend_string_release_ex(ini_name, 0);
1926 if (result == FAILURE) {
1928 goto cleanup;
1929 }
1930 }
1931 if (samesite) {
1932 ini_name = ZSTR_INIT_LITERAL("session.cookie_samesite", 0);
1934 zend_string_release_ex(ini_name, 0);
1935 if (result == FAILURE) {
1937 goto cleanup;
1938 }
1939 }
1940
1942
1943cleanup:
1944 if (lifetime) zend_string_release(lifetime);
1945 if (found > 0) {
1946 if (path) zend_string_release(path);
1947 if (domain) zend_string_release(domain);
1948 if (samesite) zend_string_release(samesite);
1949 }
1950}
1951/* }}} */
1952
1953/* {{{ Return the session cookie parameters */
1955{
1957 RETURN_THROWS();
1958 }
1959
1961
1962 add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
1963 add_assoc_string(return_value, "path", PS(cookie_path));
1964 add_assoc_string(return_value, "domain", PS(cookie_domain));
1965 add_assoc_bool(return_value, "secure", PS(cookie_secure));
1966 add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
1967 add_assoc_string(return_value, "samesite", PS(cookie_samesite));
1968}
1969/* }}} */
1970
1971/* {{{ Return the current session name. If new name is given, the session name is replaced with new name */
1973{
1975 zend_string *ini_name;
1976
1977 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|P!", &name) == FAILURE) {
1978 RETURN_THROWS();
1979 }
1980
1982 php_error_docref(NULL, E_WARNING, "Session name cannot be changed when a session is active");
1984 }
1985
1986 if (name && SG(headers_sent)) {
1987 php_error_docref(NULL, E_WARNING, "Session name cannot be changed after headers have already been sent");
1989 }
1990
1992
1993 if (name) {
1994 ini_name = ZSTR_INIT_LITERAL("session.name", 0);
1996 zend_string_release_ex(ini_name, 0);
1997 }
1998}
1999/* }}} */
2000
2001/* {{{ Return the current module name used for accessing session data. If newname is given, the module name is replaced with newname */
2003{
2005 zend_string *ini_name;
2006
2007 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &name) == FAILURE) {
2008 RETURN_THROWS();
2009 }
2010
2012 php_error_docref(NULL, E_WARNING, "Session save handler module cannot be changed when a session is active");
2014 }
2015
2016 if (name && SG(headers_sent)) {
2017 php_error_docref(NULL, E_WARNING, "Session save handler module cannot be changed after headers have already been sent");
2019 }
2020
2021 /* Set return_value to current module name */
2022 if (PS(mod) && PS(mod)->s_name) {
2023 RETVAL_STRING(PS(mod)->s_name);
2024 } else {
2026 }
2027
2028 if (name) {
2029 if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_USER))) {
2030 zend_argument_value_error(1, "cannot be \"user\"");
2031 RETURN_THROWS();
2032 }
2034 php_error_docref(NULL, E_WARNING, "Session handler module \"%s\" cannot be found", ZSTR_VAL(name));
2035
2036 zval_ptr_dtor_str(return_value);
2038 }
2039 if (PS(mod_data) || PS(mod_user_implemented)) {
2040 PS(mod)->s_close(&PS(mod_data));
2041 }
2042 PS(mod_data) = NULL;
2043
2044 ini_name = ZSTR_INIT_LITERAL("session.save_handler", 0);
2046 zend_string_release_ex(ini_name, 0);
2047 }
2048}
2049/* }}} */
2050
2051static bool can_session_handler_be_changed(void) {
2053 php_error_docref(NULL, E_WARNING, "Session save handler cannot be changed when a session is active");
2054 return false;
2055 }
2056
2057 if (SG(headers_sent)) {
2058 php_error_docref(NULL, E_WARNING, "Session save handler cannot be changed after headers have already been sent");
2059 return false;
2060 }
2061
2062 return true;
2063}
2064
2065static inline void set_user_save_handler_ini(void) {
2066 zend_string *ini_name, *ini_val;
2067
2068 ini_name = ZSTR_INIT_LITERAL("session.save_handler", 0);
2069 ini_val = ZSTR_KNOWN(ZEND_STR_USER);
2070 PS(set_handler) = 1;
2072 PS(set_handler) = 0;
2073 zend_string_release_ex(ini_val, 0);
2074 zend_string_release_ex(ini_name, 0);
2075}
2076
2077#define SESSION_RELEASE_USER_HANDLER_OO(struct_name) \
2078 if (!Z_ISUNDEF(PS(mod_user_names).struct_name)) { \
2079 zval_ptr_dtor(&PS(mod_user_names).struct_name); \
2080 ZVAL_UNDEF(&PS(mod_user_names).struct_name); \
2081 }
2082
2083#define SESSION_SET_USER_HANDLER_OO(struct_name, zstr_method_name) \
2084 array_init_size(&PS(mod_user_names).struct_name, 2); \
2085 Z_ADDREF_P(obj); \
2086 add_next_index_zval(&PS(mod_user_names).struct_name, obj); \
2087 add_next_index_str(&PS(mod_user_names).struct_name, zstr_method_name);
2088
2089#define SESSION_SET_USER_HANDLER_OO_MANDATORY(struct_name, method_name) \
2090 if (!Z_ISUNDEF(PS(mod_user_names).struct_name)) { \
2091 zval_ptr_dtor(&PS(mod_user_names).struct_name); \
2092 } \
2093 array_init_size(&PS(mod_user_names).struct_name, 2); \
2094 Z_ADDREF_P(obj); \
2095 add_next_index_zval(&PS(mod_user_names).struct_name, obj); \
2096 add_next_index_str(&PS(mod_user_names).struct_name, zend_string_init(method_name, strlen(method_name), false));
2097
2098#define SESSION_SET_USER_HANDLER_PROCEDURAL(struct_name, fci) \
2099 if (!Z_ISUNDEF(PS(mod_user_names).struct_name)) { \
2100 zval_ptr_dtor(&PS(mod_user_names).struct_name); \
2101 } \
2102 ZVAL_COPY(&PS(mod_user_names).struct_name, &fci.function_name);
2103
2104#define SESSION_SET_USER_HANDLER_PROCEDURAL_OPTIONAL(struct_name, fci) \
2105 if (ZEND_FCI_INITIALIZED(fci)) { \
2106 SESSION_SET_USER_HANDLER_PROCEDURAL(struct_name, fci); \
2107 }
2108
2109/* {{{ Sets user-level functions */
2111{
2112 /* OOP Version */
2113 if (ZEND_NUM_ARGS() <= 2) {
2114 zval *obj = NULL;
2115 bool register_shutdown = 1;
2116
2117 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &obj, php_session_iface_entry, &register_shutdown) == FAILURE) {
2118 RETURN_THROWS();
2119 }
2120
2121 if (!can_session_handler_be_changed()) {
2123 }
2124
2125 if (PS(mod_user_class_name)) {
2126 zend_string_release(PS(mod_user_class_name));
2127 }
2128 PS(mod_user_class_name) = zend_string_copy(Z_OBJCE_P(obj)->name);
2129
2130 /* Define mandatory handlers */
2132 SESSION_SET_USER_HANDLER_OO_MANDATORY(ps_close, "close");
2134 SESSION_SET_USER_HANDLER_OO_MANDATORY(ps_write, "write");
2135 SESSION_SET_USER_HANDLER_OO_MANDATORY(ps_destroy, "destroy");
2137
2138 /* Elements of object_methods HashTable are zend_function *method */
2139 HashTable *object_methods = &Z_OBJCE_P(obj)->function_table;
2140
2141 /* Find implemented methods - SessionIdInterface (optional) */
2142 /* First release old handlers */
2143 SESSION_RELEASE_USER_HANDLER_OO(ps_create_sid);
2144 zend_string *create_sid_name = ZSTR_INIT_LITERAL("create_sid", false);
2145 if (instanceof_function(Z_OBJCE_P(obj), php_session_id_iface_entry)) {
2146 SESSION_SET_USER_HANDLER_OO(ps_create_sid, zend_string_copy(create_sid_name));
2147 } else if (zend_hash_find_ptr(object_methods, create_sid_name)) {
2148 /* For BC reasons we accept methods even if the class does not implement the interface */
2149 SESSION_SET_USER_HANDLER_OO(ps_create_sid, zend_string_copy(create_sid_name));
2150 }
2151 zend_string_release_ex(create_sid_name, false);
2152
2153 /* Find implemented methods - SessionUpdateTimestampInterface (optional) */
2154 /* First release old handlers */
2155 SESSION_RELEASE_USER_HANDLER_OO(ps_validate_sid);
2156 SESSION_RELEASE_USER_HANDLER_OO(ps_update_timestamp);
2157 /* Method names need to be lowercase */
2158 zend_string *validate_sid_name = ZSTR_INIT_LITERAL("validateid", false);
2159 zend_string *update_timestamp_name = ZSTR_INIT_LITERAL("updatetimestamp", false);
2160 if (instanceof_function(Z_OBJCE_P(obj), php_session_update_timestamp_iface_entry)) {
2161 /* Validate ID handler */
2162 SESSION_SET_USER_HANDLER_OO(ps_validate_sid, zend_string_copy(validate_sid_name));
2163 /* Update Timestamp handler */
2164 SESSION_SET_USER_HANDLER_OO(ps_update_timestamp, zend_string_copy(update_timestamp_name));
2165 } else {
2166 /* For BC reasons we accept methods even if the class does not implement the interface */
2167 if (zend_hash_find_ptr(object_methods, validate_sid_name)) {
2168 /* For BC reasons we accept methods even if the class does not implement the interface */
2169 SESSION_SET_USER_HANDLER_OO(ps_validate_sid, zend_string_copy(validate_sid_name));
2170 }
2171 if (zend_hash_find_ptr(object_methods, update_timestamp_name)) {
2172 /* For BC reasons we accept methods even if the class does not implement the interface */
2173 SESSION_SET_USER_HANDLER_OO(ps_update_timestamp, zend_string_copy(update_timestamp_name));
2174 }
2175 }
2176 zend_string_release_ex(validate_sid_name, false);
2177 zend_string_release_ex(update_timestamp_name, false);
2178
2179 if (register_shutdown) {
2180 /* create shutdown function */
2181 php_shutdown_function_entry shutdown_function_entry;
2182 zval callable;
2184
2185 ZVAL_STRING(&callable, "session_register_shutdown");
2186 result = zend_fcall_info_init(&callable, 0, &shutdown_function_entry.fci,
2187 &shutdown_function_entry.fci_cache, NULL, NULL);
2188
2190
2191 /* add shutdown function, removing the old one if it exists */
2192 if (!register_user_shutdown_function("session_shutdown", strlen("session_shutdown"), &shutdown_function_entry)) {
2193 zval_ptr_dtor(&callable);
2194 php_error_docref(NULL, E_WARNING, "Unable to register session shutdown function");
2196 }
2197 } else {
2198 /* remove shutdown function */
2199 remove_user_shutdown_function("session_shutdown", strlen("session_shutdown"));
2200 }
2201
2202 if (PS(session_status) != php_session_active && (!PS(mod) || PS(mod) != &ps_mod_user)) {
2203 set_user_save_handler_ini();
2204 }
2205
2207 }
2208
2209 php_error_docref(NULL, E_DEPRECATED, "Providing individual callbacks instead of an object implementing SessionHandlerInterface is deprecated");
2210 if (UNEXPECTED(EG(exception))) {
2211 RETURN_THROWS();
2212 }
2213
2214 /* Procedural version */
2215 zend_fcall_info open_fci = {0};
2216 zend_fcall_info_cache open_fcc;
2217 zend_fcall_info close_fci = {0};
2218 zend_fcall_info_cache close_fcc;
2219 zend_fcall_info read_fci = {0};
2220 zend_fcall_info_cache read_fcc;
2221 zend_fcall_info write_fci = {0};
2222 zend_fcall_info_cache write_fcc;
2223 zend_fcall_info destroy_fci = {0};
2224 zend_fcall_info_cache destroy_fcc;
2225 zend_fcall_info gc_fci = {0};
2226 zend_fcall_info_cache gc_fcc;
2227 zend_fcall_info create_id_fci = {0};
2228 zend_fcall_info_cache create_id_fcc;
2229 zend_fcall_info validate_id_fci = {0};
2230 zend_fcall_info_cache validate_id_fcc;
2231 zend_fcall_info update_timestamp_fci = {0};
2232 zend_fcall_info_cache update_timestamp_fcc;
2233
2235 "ffffff|f!f!f!",
2236 &open_fci, &open_fcc,
2237 &close_fci, &close_fcc,
2238 &read_fci, &read_fcc,
2239 &write_fci, &write_fcc,
2240 &destroy_fci, &destroy_fcc,
2241 &gc_fci, &gc_fcc,
2242 &create_id_fci, &create_id_fcc,
2243 &validate_id_fci, &validate_id_fcc,
2244 &update_timestamp_fci, &update_timestamp_fcc) == FAILURE
2245 ) {
2246 RETURN_THROWS();
2247 }
2248 if (!can_session_handler_be_changed()) {
2250 }
2251
2252 /* If a custom session handler is already set, release relevant info */
2253 if (PS(mod_user_class_name)) {
2254 zend_string_release(PS(mod_user_class_name));
2255 PS(mod_user_class_name) = NULL;
2256 }
2257
2258 /* remove shutdown function */
2259 remove_user_shutdown_function("session_shutdown", strlen("session_shutdown"));
2260
2261 if (!PS(mod) || PS(mod) != &ps_mod_user) {
2262 set_user_save_handler_ini();
2263 }
2264
2265 /* Define mandatory handlers */
2266 SESSION_SET_USER_HANDLER_PROCEDURAL(ps_open, open_fci);
2267 SESSION_SET_USER_HANDLER_PROCEDURAL(ps_close, close_fci);
2268 SESSION_SET_USER_HANDLER_PROCEDURAL(ps_read, read_fci);
2269 SESSION_SET_USER_HANDLER_PROCEDURAL(ps_write, write_fci);
2270 SESSION_SET_USER_HANDLER_PROCEDURAL(ps_destroy, destroy_fci);
2272
2273 /* Check for optional handlers */
2274 SESSION_SET_USER_HANDLER_PROCEDURAL_OPTIONAL(ps_create_sid, create_id_fci);
2275 SESSION_SET_USER_HANDLER_PROCEDURAL_OPTIONAL(ps_validate_sid, validate_id_fci);
2276 SESSION_SET_USER_HANDLER_PROCEDURAL_OPTIONAL(ps_update_timestamp, update_timestamp_fci);
2277
2279}
2280/* }}} */
2281
2282/* {{{ Return the current save path passed to module_name. If newname is given, the save path is replaced with newname */
2284{
2286 zend_string *ini_name;
2287
2288 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|P!", &name) == FAILURE) {
2289 RETURN_THROWS();
2290 }
2291
2293 php_error_docref(NULL, E_WARNING, "Session save path cannot be changed when a session is active");
2295 }
2296
2297 if (name && SG(headers_sent)) {
2298 php_error_docref(NULL, E_WARNING, "Session save path cannot be changed after headers have already been sent");
2300 }
2301
2302 RETVAL_STRING(PS(save_path));
2303
2304 if (name) {
2305 ini_name = ZSTR_INIT_LITERAL("session.save_path", 0);
2307 zend_string_release_ex(ini_name, 0);
2308 }
2309}
2310/* }}} */
2311
2312/* {{{ Return the current session id. If newid is given, the session id is replaced with newid */
2314{
2316
2317 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &name) == FAILURE) {
2318 RETURN_THROWS();
2319 }
2320
2322 php_error_docref(NULL, E_WARNING, "Session ID cannot be changed when a session is active");
2324 }
2325
2326 if (name && PS(use_cookies) && SG(headers_sent)) {
2327 php_error_docref(NULL, E_WARNING, "Session ID cannot be changed after headers have already been sent");
2329 }
2330
2331 if (PS(id)) {
2332 /* keep compatibility for "\0" characters ???
2333 * see: ext/session/tests/session_id_error3.phpt */
2334 size_t len = strlen(ZSTR_VAL(PS(id)));
2335 if (UNEXPECTED(len != ZSTR_LEN(PS(id)))) {
2336 RETVAL_NEW_STR(zend_string_init(ZSTR_VAL(PS(id)), len, 0));
2337 } else {
2338 RETVAL_STR_COPY(PS(id));
2339 }
2340 } else {
2342 }
2343
2344 if (name) {
2345 if (PS(id)) {
2346 zend_string_release_ex(PS(id), 0);
2347 }
2348 PS(id) = zend_string_copy(name);
2349 }
2350}
2351/* }}} */
2352
2353/* {{{ Update the current session id with a newly generated one. If delete_old_session is set to true, remove the old session. */
2355{
2356 bool del_ses = 0;
2358
2359 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &del_ses) == FAILURE) {
2360 RETURN_THROWS();
2361 }
2362
2364 php_error_docref(NULL, E_WARNING, "Session ID cannot be regenerated when there is no active session");
2366 }
2367
2368 if (SG(headers_sent)) {
2369 php_error_docref(NULL, E_WARNING, "Session ID cannot be regenerated after headers have already been sent");
2371 }
2372
2373 /* Process old session data */
2374 if (del_ses) {
2375 if (PS(mod)->s_destroy(&PS(mod_data), PS(id)) == FAILURE) {
2376 PS(mod)->s_close(&PS(mod_data));
2378 if (!EG(exception)) {
2379 php_error_docref(NULL, E_WARNING, "Session object destruction failed. ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2380 }
2382 }
2383 } else {
2385 data = php_session_encode();
2386 if (data) {
2387 ret = PS(mod)->s_write(&PS(mod_data), PS(id), data, PS(gc_maxlifetime));
2389 } else {
2390 ret = PS(mod)->s_write(&PS(mod_data), PS(id), ZSTR_EMPTY_ALLOC(), PS(gc_maxlifetime));
2391 }
2392 if (ret == FAILURE) {
2393 PS(mod)->s_close(&PS(mod_data));
2395 php_error_docref(NULL, E_WARNING, "Session write failed. ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2397 }
2398 }
2399 PS(mod)->s_close(&PS(mod_data));
2400
2401 /* New session data */
2402 if (PS(session_vars)) {
2404 PS(session_vars) = NULL;
2405 }
2406 zend_string_release_ex(PS(id), 0);
2407 PS(id) = NULL;
2408
2409 if (PS(mod)->s_open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE) {
2411 if (!EG(exception)) {
2412 zend_throw_error(NULL, "Failed to open session: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2413 }
2414 RETURN_THROWS();
2415 }
2416
2417 PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
2418 if (!PS(id)) {
2420 if (!EG(exception)) {
2421 zend_throw_error(NULL, "Failed to create new session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2422 }
2423 RETURN_THROWS();
2424 }
2425 if (PS(use_strict_mode)) {
2426 if ((!PS(mod_user_implemented) && PS(mod)->s_validate_sid) || !Z_ISUNDEF(PS(mod_user_names).ps_validate_sid)) {
2427 int limit = 3;
2428 /* Try to generate non-existing ID */
2429 while (limit-- && PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == SUCCESS) {
2430 zend_string_release_ex(PS(id), 0);
2431 PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
2432 if (!PS(id)) {
2433 PS(mod)->s_close(&PS(mod_data));
2435 if (!EG(exception)) {
2436 zend_throw_error(NULL, "Failed to create session ID by collision: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2437 }
2438 RETURN_THROWS();
2439 }
2440 }
2441 }
2442 // TODO warn that ID cannot be verified? else { }
2443 }
2444 /* Read is required to make new session data at this point. */
2445 if (PS(mod)->s_read(&PS(mod_data), PS(id), &data, PS(gc_maxlifetime)) == FAILURE) {
2446 PS(mod)->s_close(&PS(mod_data));
2448 if (!EG(exception)) {
2449 zend_throw_error(NULL, "Failed to create(read) session ID: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2450 }
2451 RETURN_THROWS();
2452 }
2453 if (data) {
2455 }
2456
2457 if (PS(use_cookies)) {
2458 PS(send_cookie) = 1;
2459 }
2460 if (php_session_reset_id() == FAILURE) {
2462 }
2463
2465}
2466/* }}} */
2467
2468/* {{{ Generate new session ID. Intended for user save handlers. */
2470{
2471 zend_string *prefix = NULL, *new_id;
2472 smart_str id = {0};
2473
2475 RETURN_THROWS();
2476 }
2477
2478 if (prefix && ZSTR_LEN(prefix)) {
2480 zend_argument_value_error(1, "cannot be longer than %d characters", PS_MAX_SID_LENGTH);
2481 RETURN_THROWS();
2482 }
2484 /* E_ERROR raised for security reason. */
2485 php_error_docref(NULL, E_WARNING, "Prefix cannot contain special characters. Only the A-Z, a-z, 0-9, \"-\", and \",\" characters are allowed");
2487 } else {
2488 smart_str_append(&id, prefix);
2489 }
2490 }
2491
2492 if (!PS(in_save_handler) && PS(session_status) == php_session_active) {
2493 int limit = 3;
2494 while (limit--) {
2495 new_id = PS(mod)->s_create_sid(&PS(mod_data));
2496 if (!PS(mod)->s_validate_sid || (PS(mod_user_implemented) && Z_ISUNDEF(PS(mod_user_names).ps_validate_sid))) {
2497 break;
2498 } else {
2499 /* Detect collision and retry */
2500 if (PS(mod)->s_validate_sid(&PS(mod_data), new_id) == SUCCESS) {
2501 zend_string_release_ex(new_id, 0);
2502 new_id = NULL;
2503 continue;
2504 }
2505 break;
2506 }
2507 }
2508 } else {
2509 new_id = php_session_create_id(NULL);
2510 }
2511
2512 if (new_id) {
2513 smart_str_append(&id, new_id);
2514 zend_string_release_ex(new_id, 0);
2515 } else {
2516 smart_str_free(&id);
2517 php_error_docref(NULL, E_WARNING, "Failed to create new ID");
2519 }
2520 RETVAL_STR(smart_str_extract(&id));
2521}
2522/* }}} */
2523
2524/* {{{ Return the current cache limiter. If new_cache_limited is given, the current cache_limiter is replaced with new_cache_limiter */
2526{
2527 zend_string *limiter = NULL;
2528 zend_string *ini_name;
2529
2530 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &limiter) == FAILURE) {
2531 RETURN_THROWS();
2532 }
2533
2534 if (limiter && PS(session_status) == php_session_active) {
2535 php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be changed when a session is active");
2537 }
2538
2539 if (limiter && SG(headers_sent)) {
2540 php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be changed after headers have already been sent");
2542 }
2543
2544 RETVAL_STRING(PS(cache_limiter));
2545
2546 if (limiter) {
2547 ini_name = ZSTR_INIT_LITERAL("session.cache_limiter", 0);
2549 zend_string_release_ex(ini_name, 0);
2550 }
2551}
2552/* }}} */
2553
2554/* {{{ Return the current cache expire. If new_cache_expire is given, the current cache_expire is replaced with new_cache_expire */
2556{
2557 zend_long expires;
2558 bool expires_is_null = 1;
2559
2560 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &expires, &expires_is_null) == FAILURE) {
2561 RETURN_THROWS();
2562 }
2563
2564 if (!expires_is_null && PS(session_status) == php_session_active) {
2565 php_error_docref(NULL, E_WARNING, "Session cache expiration cannot be changed when a session is active");
2566 RETURN_LONG(PS(cache_expire));
2567 }
2568
2569 if (!expires_is_null && SG(headers_sent)) {
2570 php_error_docref(NULL, E_WARNING, "Session cache expiration cannot be changed after headers have already been sent");
2572 }
2573
2574 RETVAL_LONG(PS(cache_expire));
2575
2576 if (!expires_is_null) {
2577 zend_string *ini_name = ZSTR_INIT_LITERAL("session.cache_expire", 0);
2578 zend_string *ini_value = zend_long_to_str(expires);
2580 zend_string_release_ex(ini_name, 0);
2581 zend_string_release_ex(ini_value, 0);
2582 }
2583}
2584/* }}} */
2585
2586/* {{{ Serializes the current setup and returns the serialized representation */
2588{
2589 zend_string *enc;
2590
2592 RETURN_THROWS();
2593 }
2594
2595 enc = php_session_encode();
2596 if (enc == NULL) {
2598 }
2599
2600 RETURN_STR(enc);
2601}
2602/* }}} */
2603
2604/* {{{ Deserializes data and reinitializes the variables */
2606{
2607 zend_string *str = NULL;
2608
2609 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &str) == FAILURE) {
2610 RETURN_THROWS();
2611 }
2612
2614 php_error_docref(NULL, E_WARNING, "Session data cannot be decoded when there is no active session");
2616 }
2617
2618 if (php_session_decode(str) == FAILURE) {
2620 }
2622}
2623/* }}} */
2624
2625static zend_result php_session_start_set_ini(zend_string *varname, zend_string *new_value) {
2627 smart_str buf ={0};
2628 smart_str_appends(&buf, "session");
2629 smart_str_appendc(&buf, '.');
2630 smart_str_append(&buf, varname);
2631 smart_str_0(&buf);
2633 smart_str_free(&buf);
2634 return ret;
2635}
2636
2637/* {{{ Begin session */
2639{
2640 zval *options = NULL;
2641 zval *value;
2642 zend_ulong num_idx;
2643 zend_string *str_idx;
2644 zend_long read_and_close = 0;
2645
2647 RETURN_THROWS();
2648 }
2649
2651 if (PS(session_started_filename)) {
2652 php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active (started from %s on line %"PRIu32")", ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
2653 } else if (PS(auto_start)) {
2654 /* This option can't be changed at runtime, so we can assume it's because of this */
2655 php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already automatically active");
2656 } else {
2657 php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active");
2658 }
2660 }
2661
2662 /*
2663 * TODO: To prevent unusable session with trans sid, actual output started status is
2664 * required. i.e. There shouldn't be any outputs in output buffer, otherwise session
2665 * module is unable to rewrite output.
2666 */
2667 if (PS(use_cookies) && SG(headers_sent)) {
2668 php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent");
2670 }
2671
2672 /* set options */
2673 if (options) {
2674 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(options), num_idx, str_idx, value) {
2675 if (str_idx) {
2676 switch(Z_TYPE_P(value)) {
2677 case IS_STRING:
2678 case IS_TRUE:
2679 case IS_FALSE:
2680 case IS_LONG:
2681 if (zend_string_equals_literal(str_idx, "read_and_close")) {
2682 read_and_close = zval_get_long(value);
2683 } else {
2684 zend_string *tmp_val;
2685 zend_string *val = zval_get_tmp_string(value, &tmp_val);
2686 if (php_session_start_set_ini(str_idx, val) == FAILURE) {
2687 php_error_docref(NULL, E_WARNING, "Setting option \"%s\" failed", ZSTR_VAL(str_idx));
2688 }
2689 zend_tmp_string_release(tmp_val);
2690 }
2691 break;
2692 default:
2693 zend_type_error("%s(): Option \"%s\" must be of type string|int|bool, %s given",
2695 );
2696 RETURN_THROWS();
2697 }
2698 }
2699 (void) num_idx;
2701 }
2702
2704
2706 IF_SESSION_VARS() {
2707 zval *sess_var = Z_REFVAL(PS(http_session_vars));
2708 SEPARATE_ARRAY(sess_var);
2709 /* Clean $_SESSION. */
2710 zend_hash_clean(Z_ARRVAL_P(sess_var));
2711 }
2713 }
2714
2715 if (read_and_close) {
2717 }
2718
2720}
2721/* }}} */
2722
2723/* {{{ Destroy the current session and all data associated with it */
2732/* }}} */
2733
2734/* {{{ Unset all registered variables */
2736{
2738 RETURN_THROWS();
2739 }
2740
2743 }
2744
2745 IF_SESSION_VARS() {
2746 zval *sess_var = Z_REFVAL(PS(http_session_vars));
2747 SEPARATE_ARRAY(sess_var);
2748
2749 /* Clean $_SESSION. */
2750 zend_hash_clean(Z_ARRVAL_P(sess_var));
2751 }
2753}
2754/* }}} */
2755
2756/* {{{ Perform GC and return number of deleted sessions */
2758{
2759 zend_long num;
2760
2762 RETURN_THROWS();
2763 }
2764
2766 php_error_docref(NULL, E_WARNING, "Session cannot be garbage collected when there is no active session");
2768 }
2769
2770 num = php_session_gc(1);
2771 if (num < 0) {
2773 }
2774
2775 RETURN_LONG(num);
2776}
2777/* }}} */
2778
2779
2780/* {{{ Write session data and end session */
2782{
2784 RETURN_THROWS();
2785 }
2786
2789 }
2792}
2793/* }}} */
2794
2795/* {{{ Abort session and end session. Session data will not be written */
2797{
2799 RETURN_THROWS();
2800 }
2801
2804 }
2805 php_session_abort();
2807}
2808/* }}} */
2809
2810/* {{{ Reset session data from saved session data */
2812{
2814 RETURN_THROWS();
2815 }
2816
2819 }
2820 php_session_reset();
2822}
2823/* }}} */
2824
2825/* {{{ Returns the current session status */
2834/* }}} */
2835
2836/* {{{ Registers session_write_close() as a shutdown function */
2838{
2839 php_shutdown_function_entry shutdown_function_entry;
2840 zval callable;
2842
2844
2845 /* This function is registered itself as a shutdown function by
2846 * session_set_save_handler($obj). The reason we now register another
2847 * shutdown function is in case the user registered their own shutdown
2848 * function after calling session_set_save_handler(), which expects
2849 * the session still to be available.
2850 */
2851 ZVAL_STRING(&callable, "session_write_close");
2852 result = zend_fcall_info_init(&callable, 0, &shutdown_function_entry.fci,
2853 &shutdown_function_entry.fci_cache, NULL, NULL);
2854
2856
2857 if (!append_user_shutdown_function(&shutdown_function_entry)) {
2858 zval_ptr_dtor(&callable);
2859
2860 /* Unable to register shutdown function, presumably because of lack
2861 * of memory, so flush the session now. It would be done in rshutdown
2862 * anyway but the handler will have had it's dtor called by then.
2863 * If the user does have a later shutdown function which needs the
2864 * session then tough luck.
2865 */
2867 php_error_docref(NULL, E_WARNING, "Session shutdown function cannot be registered");
2868 }
2869}
2870/* }}} */
2871
2872/* ********************************
2873 * Module Setup and Destruction *
2874 ******************************** */
2875
2876static zend_result php_rinit_session(bool auto_start) /* {{{ */
2877{
2878 php_rinit_session_globals();
2879
2880 PS(mod) = NULL;
2881 {
2882 char *value;
2883
2884 value = zend_ini_string("session.save_handler", sizeof("session.save_handler") - 1, 0);
2885 if (value) {
2887 }
2888 }
2889
2890 if (PS(serializer) == NULL) {
2891 char *value;
2892
2893 value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler") - 1, 0);
2894 if (value) {
2895 PS(serializer) = _php_find_ps_serializer(value);
2896 }
2897 }
2898
2899 if (PS(mod) == NULL || PS(serializer) == NULL) {
2900 /* current status is unusable */
2902 return SUCCESS;
2903 }
2904
2905 if (auto_start) {
2907 }
2908
2909 return SUCCESS;
2910} /* }}} */
2911
2912static PHP_RINIT_FUNCTION(session) /* {{{ */
2913{
2914 return php_rinit_session(PS(auto_start));
2915}
2916/* }}} */
2917
2918#define SESSION_FREE_USER_HANDLER(struct_name) \
2919 if (!Z_ISUNDEF(PS(mod_user_names).struct_name)) { \
2920 zval_ptr_dtor(&PS(mod_user_names).struct_name); \
2921 ZVAL_UNDEF(&PS(mod_user_names).struct_name); \
2922 }
2923
2924
2925static PHP_RSHUTDOWN_FUNCTION(session) /* {{{ */
2926{
2928 zend_try {
2930 } zend_end_try();
2931 }
2932 php_rshutdown_session_globals();
2933
2934 /* this should NOT be done in php_rshutdown_session_globals() */
2935 /* Free user defined handlers */
2937 SESSION_FREE_USER_HANDLER(ps_close);
2939 SESSION_FREE_USER_HANDLER(ps_write);
2940 SESSION_FREE_USER_HANDLER(ps_destroy);
2942 SESSION_FREE_USER_HANDLER(ps_create_sid);
2943 SESSION_FREE_USER_HANDLER(ps_validate_sid);
2944 SESSION_FREE_USER_HANDLER(ps_update_timestamp);
2945
2946 return SUCCESS;
2947}
2948/* }}} */
2949
2950static PHP_GINIT_FUNCTION(ps) /* {{{ */
2951{
2952#if defined(COMPILE_DL_SESSION) && defined(ZTS)
2954#endif
2955
2956 ps_globals->save_path = NULL;
2957 ps_globals->session_name = NULL;
2958 ps_globals->id = NULL;
2959 ps_globals->mod = NULL;
2960 ps_globals->serializer = NULL;
2961 ps_globals->mod_data = NULL;
2962 ps_globals->session_status = php_session_none;
2963 ps_globals->default_mod = NULL;
2964 ps_globals->mod_user_implemented = 0;
2965 ps_globals->mod_user_class_name = NULL;
2966 ps_globals->mod_user_is_open = 0;
2967 ps_globals->session_vars = NULL;
2968 ps_globals->set_handler = 0;
2969 ps_globals->session_started_filename = NULL;
2970 ps_globals->session_started_lineno = 0;
2971 /* Unset user defined handlers */
2972 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_open);
2973 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_close);
2974 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_read);
2975 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_write);
2976 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_destroy);
2977 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_gc);
2978 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_create_sid);
2979 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_validate_sid);
2980 ZVAL_UNDEF(&ps_globals->mod_user_names.ps_update_timestamp);
2981 ZVAL_UNDEF(&ps_globals->http_session_vars);
2982
2983 ps_globals->random = (php_random_algo_with_state){
2985 .state = &ps_globals->random_state,
2986 };
2988 if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
2989 seed = php_random_uint128_constant(
2992 );
2993 }
2994 php_random_pcgoneseq128xslrr64_seed128(ps_globals->random.state, seed);
2995}
2996/* }}} */
2997
2998static PHP_MINIT_FUNCTION(session) /* {{{ */
2999{
3000 zend_register_auto_global(zend_string_init_interned("_SESSION", sizeof("_SESSION") - 1, 1), 0, NULL);
3001
3002 my_module_number = module_number;
3003 PS(module_number) = module_number;
3004
3007
3008#ifdef HAVE_LIBMM
3010#endif
3011 php_session_rfc1867_orig_callback = php_rfc1867_callback;
3012 php_rfc1867_callback = php_session_rfc1867_callback;
3013
3014 /* Register interfaces */
3015 php_session_iface_entry = register_class_SessionHandlerInterface();
3016
3017 php_session_id_iface_entry = register_class_SessionIdInterface();
3018
3019 php_session_update_timestamp_iface_entry = register_class_SessionUpdateTimestampHandlerInterface();
3020
3021 /* Register base class */
3023
3024 register_session_symbols(module_number);
3025
3026 return SUCCESS;
3027}
3028/* }}} */
3029
3030static PHP_MSHUTDOWN_FUNCTION(session) /* {{{ */
3031{
3033
3034#ifdef HAVE_LIBMM
3036#endif
3037
3038 /* reset rfc1867 callbacks */
3039 php_session_rfc1867_orig_callback = NULL;
3040 if (php_rfc1867_callback == php_session_rfc1867_callback) {
3042 }
3043
3044 ps_serializers[PREDEFINED_SERIALIZERS].name = NULL;
3046
3047 return SUCCESS;
3048}
3049/* }}} */
3050
3051static PHP_MINFO_FUNCTION(session) /* {{{ */
3052{
3053 const ps_module **mod;
3054 ps_serializer *ser;
3055 smart_str save_handlers = {0};
3056 smart_str ser_handlers = {0};
3057 int i;
3058
3059 /* Get save handlers */
3060 for (i = 0, mod = ps_modules; i < MAX_MODULES; i++, mod++) {
3061 if (*mod && (*mod)->s_name) {
3062 smart_str_appends(&save_handlers, (*mod)->s_name);
3063 smart_str_appendc(&save_handlers, ' ');
3064 }
3065 }
3066
3067 /* Get serializer handlers */
3068 for (i = 0, ser = ps_serializers; i < MAX_SERIALIZERS; i++, ser++) {
3069 if (ser->name) {
3070 smart_str_appends(&ser_handlers, ser->name);
3071 smart_str_appendc(&ser_handlers, ' ');
3072 }
3073 }
3074
3076 php_info_print_table_row(2, "Session Support", "enabled" );
3077
3078 if (save_handlers.s) {
3079 smart_str_0(&save_handlers);
3080 php_info_print_table_row(2, "Registered save handlers", ZSTR_VAL(save_handlers.s));
3081 smart_str_free(&save_handlers);
3082 } else {
3083 php_info_print_table_row(2, "Registered save handlers", "none");
3084 }
3085
3086 if (ser_handlers.s) {
3087 smart_str_0(&ser_handlers);
3088 php_info_print_table_row(2, "Registered serializer handlers", ZSTR_VAL(ser_handlers.s));
3089 smart_str_free(&ser_handlers);
3090 } else {
3091 php_info_print_table_row(2, "Registered serializer handlers", "none");
3092 }
3093
3095
3097}
3098/* }}} */
3099
3100static const zend_module_dep session_deps[] = { /* {{{ */
3101 ZEND_MOD_OPTIONAL("spl")
3103};
3104/* }}} */
3105
3106/* ************************
3107 * Upload hook handling *
3108 ************************ */
3109
3110static bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progress *progress) /* {{{ */
3111{
3112 zval *ppid;
3113
3114 if (Z_ISUNDEF(PG(http_globals)[where])) {
3115 return 0;
3116 }
3117
3118 if ((ppid = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[where]), PS(session_name), progress->sname_len))
3119 && Z_TYPE_P(ppid) == IS_STRING) {
3120 zval_ptr_dtor(dest);
3121 ZVAL_COPY_DEREF(dest, ppid);
3122 return 1;
3123 }
3124
3125 return 0;
3126} /* }}} */
3127
3128static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress) /* {{{ */
3129{
3130
3131 if (PS(use_cookies)) {
3132 sapi_module.treat_data(PARSE_COOKIE, NULL, NULL);
3133 if (early_find_sid_in(&progress->sid, TRACK_VARS_COOKIE, progress)) {
3134 progress->apply_trans_sid = 0;
3135 return;
3136 }
3137 }
3138 if (PS(use_only_cookies)) {
3139 return;
3140 }
3141 sapi_module.treat_data(PARSE_GET, NULL, NULL);
3142 early_find_sid_in(&progress->sid, TRACK_VARS_GET, progress);
3143} /* }}} */
3144
3145static bool php_check_cancel_upload(php_session_rfc1867_progress *progress) /* {{{ */
3146{
3147 zval *progress_ary, *cancel_upload;
3148
3149 if ((progress_ary = zend_symtable_find(Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars))), progress->key.s)) == NULL) {
3150 return 0;
3151 }
3152 if (Z_TYPE_P(progress_ary) != IS_ARRAY) {
3153 return 0;
3154 }
3155 if ((cancel_upload = zend_hash_str_find(Z_ARRVAL_P(progress_ary), "cancel_upload", sizeof("cancel_upload") - 1)) == NULL) {
3156 return 0;
3157 }
3158 return Z_TYPE_P(cancel_upload) == IS_TRUE;
3159} /* }}} */
3160
3161static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, int force_update) /* {{{ */
3162{
3163 if (!force_update) {
3164 if (Z_LVAL_P(progress->post_bytes_processed) < progress->next_update) {
3165 return;
3166 }
3167#ifdef HAVE_GETTIMEOFDAY
3168 if (PS(rfc1867_min_freq) > 0.0) {
3169 struct timeval tv = {0};
3170 double dtv;
3171 gettimeofday(&tv, NULL);
3172 dtv = (double) tv.tv_sec + tv.tv_usec / 1000000.0;
3173 if (dtv < progress->next_update_time) {
3174 return;
3175 }
3176 progress->next_update_time = dtv + PS(rfc1867_min_freq);
3177 }
3178#endif
3179 progress->next_update = Z_LVAL_P(progress->post_bytes_processed) + progress->update_step;
3180 }
3181
3182 php_session_initialize();
3184 IF_SESSION_VARS() {
3185 zval *sess_var = Z_REFVAL(PS(http_session_vars));
3186 SEPARATE_ARRAY(sess_var);
3187
3188 progress->cancel_upload |= php_check_cancel_upload(progress);
3189 Z_TRY_ADDREF(progress->data);
3190 zend_hash_update(Z_ARRVAL_P(sess_var), progress->key.s, &progress->data);
3191 }
3193} /* }}} */
3194
3195static void php_session_rfc1867_cleanup(php_session_rfc1867_progress *progress) /* {{{ */
3196{
3197 php_session_initialize();
3199 IF_SESSION_VARS() {
3200 zval *sess_var = Z_REFVAL(PS(http_session_vars));
3201 SEPARATE_ARRAY(sess_var);
3202 zend_hash_del(Z_ARRVAL_P(sess_var), progress->key.s);
3203 }
3205} /* }}} */
3206
3207static zend_result php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra) /* {{{ */
3208{
3211
3212 if (php_session_rfc1867_orig_callback) {
3213 retval = php_session_rfc1867_orig_callback(event, event_data, extra);
3214 }
3215 if (!PS(rfc1867_enabled)) {
3216 return retval;
3217 }
3218
3219 progress = PS(rfc1867_progress);
3220
3221 switch(event) {
3222 case MULTIPART_EVENT_START: {
3224 progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
3225 progress->content_length = data->content_length;
3226 progress->sname_len = strlen(PS(session_name));
3227 PS(rfc1867_progress) = progress;
3228 }
3229 break;
3232 size_t value_len;
3233
3234 if (Z_TYPE(progress->sid) && progress->key.s) {
3235 break;
3236 }
3237
3238 /* orig callback may have modified *data->newlength */
3239 if (data->newlength) {
3240 value_len = *data->newlength;
3241 } else {
3242 value_len = data->length;
3243 }
3244
3245 if (data->name && data->value && value_len) {
3246 size_t name_len = strlen(data->name);
3247
3248 if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
3249 zval_ptr_dtor(&progress->sid);
3250 ZVAL_STRINGL(&progress->sid, (*data->value), value_len);
3251 } else if (name_len == strlen(PS(rfc1867_name)) && memcmp(data->name, PS(rfc1867_name), name_len + 1) == 0) {
3252 smart_str_free(&progress->key);
3253 smart_str_appends(&progress->key, PS(rfc1867_prefix));
3254 smart_str_appendl(&progress->key, *data->value, value_len);
3255 smart_str_0(&progress->key);
3256
3257 progress->apply_trans_sid = APPLY_TRANS_SID;
3258 php_session_rfc1867_early_find_sid(progress);
3259 }
3260 }
3261 }
3262 break;
3265
3266 /* Do nothing when $_POST["PHP_SESSION_UPLOAD_PROGRESS"] is not set
3267 * or when we have no session id */
3268 if (!Z_TYPE(progress->sid) || !progress->key.s) {
3269 break;
3270 }
3271
3272 /* First FILE_START event, initializing data */
3273 if (Z_ISUNDEF(progress->data)) {
3274
3275 if (PS(rfc1867_freq) >= 0) {
3276 progress->update_step = PS(rfc1867_freq);
3277 } else if (PS(rfc1867_freq) < 0) { /* % of total size */
3278 progress->update_step = progress->content_length * -PS(rfc1867_freq) / 100;
3279 }
3280 progress->next_update = 0;
3281 progress->next_update_time = 0.0;
3282
3283 array_init(&progress->data);
3284 array_init(&progress->files);
3285
3286 add_assoc_long_ex(&progress->data, "start_time", sizeof("start_time") - 1, (zend_long)sapi_get_request_time());
3287 add_assoc_long_ex(&progress->data, "content_length", sizeof("content_length") - 1, progress->content_length);
3288 add_assoc_long_ex(&progress->data, "bytes_processed", sizeof("bytes_processed") - 1, data->post_bytes_processed);
3289 add_assoc_bool_ex(&progress->data, "done", sizeof("done") - 1, 0);
3290 add_assoc_zval_ex(&progress->data, "files", sizeof("files") - 1, &progress->files);
3291
3292 progress->post_bytes_processed = zend_hash_str_find(Z_ARRVAL(progress->data), "bytes_processed", sizeof("bytes_processed") - 1);
3293
3294 php_rinit_session(0);
3295 PS(id) = zend_string_init(Z_STRVAL(progress->sid), Z_STRLEN(progress->sid), 0);
3296 if (progress->apply_trans_sid) {
3297 /* Enable trans sid by modifying flags */
3298 PS(use_trans_sid) = 1;
3299 PS(use_only_cookies) = 0;
3300 }
3301 PS(send_cookie) = 0;
3302 }
3303
3304 array_init(&progress->current_file);
3305
3306 /* Each uploaded file has its own array. Trying to make it close to $_FILES entries. */
3307 add_assoc_string_ex(&progress->current_file, "field_name", sizeof("field_name") - 1, data->name);
3308 add_assoc_string_ex(&progress->current_file, "name", sizeof("name") - 1, *data->filename);
3309 add_assoc_null_ex(&progress->current_file, "tmp_name", sizeof("tmp_name") - 1);
3310 add_assoc_long_ex(&progress->current_file, "error", sizeof("error") - 1, 0);
3311
3312 add_assoc_bool_ex(&progress->current_file, "done", sizeof("done") - 1, 0);
3313 add_assoc_long_ex(&progress->current_file, "start_time", sizeof("start_time") - 1, (zend_long)time(NULL));
3314 add_assoc_long_ex(&progress->current_file, "bytes_processed", sizeof("bytes_processed") - 1, 0);
3315
3316 add_next_index_zval(&progress->files, &progress->current_file);
3317
3318 progress->current_file_bytes_processed = zend_hash_str_find(Z_ARRVAL(progress->current_file), "bytes_processed", sizeof("bytes_processed") - 1);
3319
3320 Z_LVAL_P(progress->current_file_bytes_processed) = data->post_bytes_processed;
3321 php_session_rfc1867_update(progress, 0);
3322 }
3323 break;
3326
3327 if (!Z_TYPE(progress->sid) || !progress->key.s) {
3328 break;
3329 }
3330
3331 Z_LVAL_P(progress->current_file_bytes_processed) = data->offset + data->length;
3332 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
3333
3334 php_session_rfc1867_update(progress, 0);
3335 }
3336 break;
3339
3340 if (!Z_TYPE(progress->sid) || !progress->key.s) {
3341 break;
3342 }
3343
3344 if (data->temp_filename) {
3345 add_assoc_string_ex(&progress->current_file, "tmp_name", sizeof("tmp_name") - 1, data->temp_filename);
3346 }
3347
3348 add_assoc_long_ex(&progress->current_file, "error", sizeof("error") - 1, data->cancel_upload);
3349 add_assoc_bool_ex(&progress->current_file, "done", sizeof("done") - 1, 1);
3350
3351 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
3352
3353 php_session_rfc1867_update(progress, 0);
3354 }
3355 break;
3356 case MULTIPART_EVENT_END: {
3358
3359 if (Z_TYPE(progress->sid) && progress->key.s) {
3360 if (PS(rfc1867_cleanup)) {
3361 php_session_rfc1867_cleanup(progress);
3362 } else {
3363 if (!Z_ISUNDEF(progress->data)) {
3364 SEPARATE_ARRAY(&progress->data);
3365 add_assoc_bool_ex(&progress->data, "done", sizeof("done") - 1, 1);
3366 Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
3367 php_session_rfc1867_update(progress, 1);
3368 }
3369 }
3370 php_rshutdown_session_globals();
3371 }
3372
3373 if (!Z_ISUNDEF(progress->data)) {
3374 zval_ptr_dtor(&progress->data);
3375 }
3376 zval_ptr_dtor(&progress->sid);
3377 smart_str_free(&progress->key);
3378 efree(progress);
3379 progress = NULL;
3380 PS(rfc1867_progress) = NULL;
3381 }
3382 break;
3383 }
3384
3385 if (progress && progress->cancel_upload) {
3386 return FAILURE;
3387 }
3388 return retval;
3389
3390} /* }}} */
3391
3394 NULL,
3395 session_deps,
3396 "session",
3397 ext_functions,
3398 PHP_MINIT(session), PHP_MSHUTDOWN(session),
3399 PHP_RINIT(session), PHP_RSHUTDOWN(session),
3400 PHP_MINFO(session),
3403 PHP_GINIT(ps),
3404 NULL,
3405 NULL,
3407};
3408
3409#ifdef COMPILE_DL_SESSION
3410#ifdef ZTS
3412#endif
3413ZEND_GET_MODULE(session)
3414#endif
SAPI_API double sapi_get_request_time(void)
Definition SAPI.c:1088
SAPI_API sapi_module_struct sapi_module
Definition SAPI.c:65
SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
Definition SAPI.c:105
SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace)
Definition SAPI.c:628
#define SG(v)
Definition SAPI.h:160
size_t len
Definition apprentice.c:174
bool exception
Definition assert.c:30
PHPAPI bool remove_user_shutdown_function(const char *function_name, size_t function_len)
PHPAPI bool append_user_shutdown_function(php_shutdown_function_entry *shutdown_function_entry)
PHPAPI bool register_user_shutdown_function(const char *function_name, size_t function_len, php_shutdown_function_entry *shutdown_function_entry)
struct _php_shutdown_function_entry php_shutdown_function_entry
gettimeofday(bool $as_float=false)
strpbrk(string $string, string $characters)
header(string $header, bool $replace=true, int $response_code=0)
strstr(string $haystack, string $needle, bool $before_needle=false)
strchr(string $haystack, string $needle, bool $before_needle=false)
headers_sent(&$filename=null, &$line=null)
uint32_t v
Definition cdf.c:1237
zend_long ptrdiff_t
PHPAPI void php_random_pcgoneseq128xslrr64_seed128(php_random_status_state_pcgoneseq128xslrr64 *s, php_random_uint128_t seed)
PHPAPI const php_random_algo php_random_algo_pcgoneseq128xslrr64
zval * zv
Definition ffi.c:3975
zend_long n
Definition ffi.c:4979
zend_string * res
Definition ffi.c:4692
void * ptr
Definition ffi.c:3814
memset(ptr, 0, type->size)
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
PHPAPI int php_check_open_basedir(const char *path)
#define NULL
Definition gdcache.h:45
#define prefix
#define SUCCESS
Definition hash_sha3.c:261
#define COOKIE_HTTPONLY
Definition head.h:25
#define COOKIE_EXPIRES
Definition head.h:20
#define COOKIE_PATH
Definition head.h:23
#define COOKIE_SECURE
Definition head.h:24
#define COOKIE_SAMESITE
Definition head.h:26
#define COOKIE_MAX_AGE
Definition head.h:21
#define COOKIE_DOMAIN
Definition head.h:22
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
#define next(ls)
Definition minilua.c:2661
#define ps_files_ptr
Definition mod_files.h:21
const ps_module ps_mod_user
Definition mod_user.c:21
#define ps_user_ptr
Definition mod_user.h:21
PHPAPI const char * php_output_get_start_filename(void)
Definition output.c:743
PHPAPI int php_output_get_start_lineno(void)
Definition output.c:751
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
#define PHP_GINIT
Definition php.h:397
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_RINIT
Definition php.h:394
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_GINIT_FUNCTION
Definition php.h:405
#define INT_MAX
Definition php.h:237
#define PHP_RSHUTDOWN
Definition php.h:395
#define PHP_RINIT_FUNCTION
Definition php.h:402
#define PHP_RSHUTDOWN_FUNCTION
Definition php.h:403
#define PHP_MINIT
Definition php.h:392
#define PHPAPI
Definition php.h:71
#define PHP_MODULE_GLOBALS
Definition php.h:408
#define php_error
Definition php.h:310
PHPAPI zend_string * php_format_date(const char *format, size_t format_len, time_t ts, bool localtime)
Definition php_date.c:875
time()
#define TRACK_VARS_GET
Definition php_globals.h:41
#define TRACK_VARS_SERVER
Definition php_globals.h:43
#define TRACK_VARS_COOKIE
Definition php_globals.h:42
#define PG(v)
Definition php_globals.h:31
#define PHP_INI_PERDIR
Definition php_ini.h:42
#define PHP_INI_STAGE_RUNTIME
Definition php_ini.h:75
#define PHP_INI_ALL
Definition php_ini.h:45
#define PHP_INI_USER
Definition php_ini.h:41
#define PHP_INI_BEGIN
Definition php_ini.h:52
#define STD_PHP_INI_ENTRY
Definition php_ini.h:64
#define PHP_INI_ENTRY
Definition php_ini.h:62
#define STD_PHP_INI_BOOLEAN
Definition php_ini.h:66
#define PHP_INI_MH
Definition php_ini.h:49
#define PHP_INI_END
Definition php_ini.h:53
#define PHP_INI_STAGE_HTACCESS
Definition php_ini.h:76
PHP_JSON_API size_t int options
Definition php_json.h:102
zend_string * output_start_filename
Definition php_output.h:142
int output_start_lineno
Definition php_output.h:143
struct _php_random_algo_with_state php_random_algo_with_state
PHPAPI uint64_t php_random_generate_fallback_seed(void)
Definition random.c:716
PHPAPI zend_long php_random_range(php_random_algo_with_state engine, zend_long min, zend_long max)
Definition random.c:293
struct _php_random_uint128_t php_random_uint128_t
PHPAPI struct tm * php_gmtime_r(const time_t *const timep, struct tm *p_tm)
Definition reentrancy.c:173
unsigned char key[REFLECTION_KEY_LEN]
zend_module_entry session_module_entry
Definition session.c:3392
PHPAPI zend_class_entry * php_session_update_timestamp_iface_entry
#define PS_VALIDATE_SID_ARGS
Definition php_session.h:36
php_session_status
@ php_session_disabled
@ php_session_active
@ php_session_none
#define PS_CREATE_SID_ARGS
Definition php_session.h:35
#define PS_ENCODE_VARS
struct ps_module_struct ps_module
#define PS_UPDATE_TIMESTAMP_ARGS
Definition php_session.h:37
#define PS(v)
PHPAPI zend_result php_session_update_timestamp(PS_UPDATE_TIMESTAMP_ARGS)
Definition session.c:1204
struct ps_serializer_struct ps_serializer
#define PHP_SESSION_VERSION
Definition php_session.h:26
#define PS_SERIALIZER_DECODE_ARGS
#define PS_SERIALIZER_ENTRY(x)
PHPAPI zend_class_entry * php_session_iface_entry
struct _php_session_rfc1867_progress php_session_rfc1867_progress
#define PS_ENCODE_LOOP(code)
#define PS_SERIALIZER_DECODE_FUNC(x)
struct _php_ps_globals php_ps_globals
PHPAPI zend_class_entry * php_session_class_entry
#define PS_SERIALIZER_ENCODE_FUNC(x)
PHPAPI zend_class_entry * php_session_id_iface_entry
#define PS_SERIALIZER_ENCODE_ARGS
struct _encode encode
Definition php_soap.h:42
#define PHP_VAR_UNSERIALIZE_DESTROY(d)
Definition php_var.h:59
struct php_unserialize_data * php_unserialize_data_t
Definition php_var.h:32
struct php_serialize_data * php_serialize_data_t
Definition php_var.h:31
#define PHP_VAR_UNSERIALIZE_INIT(d)
Definition php_var.h:56
PHPAPI zval * var_tmp_var(php_unserialize_data_t *var_hashx)
PHPAPI int php_var_unserialize(zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash)
#define PHP_VAR_SERIALIZE_INIT(d)
Definition php_var.h:50
PHPAPI void php_var_serialize(smart_str *buf, zval *struc, php_serialize_data_t *data)
Definition var.c:1319
#define PHP_VAR_SERIALIZE_DESTROY(d)
Definition php_var.h:53
#define PARSE_COOKIE
#define PARSE_GET
zend_constant * data
PHPAPI zend_result(* php_rfc1867_callback)(unsigned int event, void *event_data, void **extra)
Definition rfc1867.c:53
struct _multipart_event_file_end multipart_event_file_end
#define MULTIPART_EVENT_FILE_END
Definition rfc1867.h:27
struct _multipart_event_formdata multipart_event_formdata
#define MULTIPART_EVENT_FORMDATA
Definition rfc1867.h:24
struct _multipart_event_start multipart_event_start
#define MULTIPART_EVENT_START
Definition rfc1867.h:23
struct _multipart_event_file_data multipart_event_file_data
#define MULTIPART_EVENT_END
Definition rfc1867.h:28
#define MULTIPART_EVENT_FILE_START
Definition rfc1867.h:25
#define MULTIPART_EVENT_FILE_DATA
Definition rfc1867.h:26
struct _multipart_event_end multipart_event_end
struct _multipart_event_file_start multipart_event_file_start
PHPAPI zend_result php_session_destroy(void)
Definition session.c:171
bool fail
Definition session.c:1065
struct timeval tv
Definition session.c:1280
PHPAPI zval * php_set_session_var(zend_string *name, zval *state_val, php_unserialize_data_t *var_hash)
Definition session.c:208
PHPAPI php_session_status php_get_session_status(void)
Definition session.c:1738
#define PS_DELIMITER
Definition session.c:1059
#define SESSION_CHECK_OUTPUT_STATE
memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1)
#define SESSION_SET_USER_HANDLER_OO(struct_name, zstr_method_name)
Definition session.c:2083
PHPAPI zend_result php_session_valid_key(const char *key)
Definition session.c:359
#define PS_BIN_MAX
Definition session.c:994
PHPAPI void session_adapt_url(const char *url, size_t url_len, char **new_url, size_t *new_len)
Definition session.c:1770
#define EXPIRES
Definition session.c:1276
zval * current
Definition session.c:1024
#define CACHE_LIMITER_FUNC(name)
Definition session.c:1219
PHPAPI zend_result php_session_reset_id(void)
Definition session.c:1538
#define SESSION_RELEASE_USER_HANDLER_OO(struct_name)
Definition session.c:2077
#define PS_BIN_UNDEF
Definition session.c:993
zval session_vars
Definition session.c:963
zval rv
Definition session.c:1024
PHPAPI void php_add_session_var(zend_string *name)
Definition session.c:194
#define SESSION_SET_USER_HANDLER_PROCEDURAL_OPTIONAL(struct_name, fci)
Definition session.c:2104
#define LAST_MODIFIED
PHPAPI zval * php_get_session_var(zend_string *name)
Definition session.c:219
#define SESSION_FORBIDDEN_CHARS
#define PREDEFINED_SERIALIZERS
Definition session.c:1143
PHPAPI zend_result php_session_register_serializer(const char *name, zend_string *(*encode)(PS_SERIALIZER_ENCODE_ARGS), zend_result(*decode)(PS_SERIALIZER_DECODE_ARGS))
Definition session.c:1151
#define MAX_MODULES
Definition session.c:1173
PHPAPI zend_result php_session_update_timestamp(PS_UPDATE_TIMESTAMP_ARGS)
Definition session.c:1204
zend_string_release_ex(var_name, 0)
#define PS_MAX_SID_LENGTH
#define PREDEFINED_MODULES
Definition session.c:1174
#define APPLY_TRANS_SID
#define MAX_STR
Definition session.c:1222
const char * endptr
Definition session.c:1021
PHPAPI zend_result php_session_flush(int write)
Definition session.c:1727
#define SESSION_FREE_USER_HANDLER(struct_name)
Definition session.c:2918
zend_string * var_name
Definition session.c:966
PHPAPI const ps_serializer * _php_find_ps_serializer(const char *name)
Definition session.c:1511
PHPAPI zend_result php_session_start(void)
Definition session.c:1611
#define CACHE_LIMITER(name)
Definition session.c:1218
#define SESSION_CHECK_ACTIVE_STATE
#define SESSION_SET_USER_HANDLER_PROCEDURAL(struct_name, fci)
Definition session.c:2098
PHPAPI const ps_module * _php_find_ps_module(const char *name)
Definition session.c:1495
p
Definition session.c:1105
PHPAPI zval * php_get_session_var_str(const char *name, size_t name_len)
Definition session.c:228
#define MAX_SERIALIZERS
Definition session.c:1142
php_unserialize_data_t var_hash
Definition session.c:964
#define ADD_HEADER(a)
Definition session.c:1221
#define SESSION_FORBIDDEN_CHARS_FOR_ERROR_MSG
#define SESSION_SET_USER_HANDLER_OO_MANDATORY(struct_name, method_name)
Definition session.c:2089
PHPAPI zend_result php_session_validate_sid(PS_VALIDATE_SID_ARGS)
Definition session.c:1199
PHPAPI zend_string * php_session_create_id(PS_CREATE_SID_ARGS)
Definition session.c:335
ptrdiff_t namelen
Definition session.c:1097
#define IF_SESSION_VARS()
CACHE_LIMITER private_no_expire()
PHPAPI zend_result php_session_register_module(const ps_module *ptr)
Definition session.c:1181
#define CACHE_LIMITER_ENTRY(name)
Definition session.c:1220
time_t now
Definition session.c:1281
session_write_close()
session_id(?string $id=null)
session_encode()
session_unset()
session_name(?string $name=null)
session_status()
session_cache_expire(?int $value=null)
session_create_id(string $prefix="")
session_set_save_handler( $open, $close=UNKNOWN, callable $read=UNKNOWN, callable $write=UNKNOWN, callable $destroy=UNKNOWN, callable $gc=UNKNOWN, ?callable $create_sid=null, ?callable $validate_sid=null, ?callable $update_timestamp=null)
session_decode(string $data)
session_get_cookie_params()
session_register_shutdown()
session_start(array $options=[])
session_reset()
session_save_path(?string $path=null)
session_gc()
session_set_cookie_params(array|int $lifetime_or_options, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httponly=null)
session_destroy()
session_abort()
session_module_name(?string $module=null)
session_cache_limiter(?string $value=null)
session_regenerate_id(bool $delete_old_session=false)
#define slprintf
Definition snprintf.h:89
#define spprintf
Definition spprintf.h:29
zend_fcall_info_cache fci_cache
zend_fcall_info fci
zend_llist_element * head
Definition zend_llist.h:38
size_t count
Definition zend_llist.h:40
zend_llist_element * tail
Definition zend_llist.h:39
zend_string * s
PHPAPI zend_string * php_url_encode(char const *s, size_t len)
Definition url.c:546
PHPAPI char * php_url_scanner_adapt_single_url(const char *url, size_t urllen, const char *name, const char *value, size_t *newlen, bool encode)
PHPAPI zend_result php_url_scanner_reset_session_var(zend_string *name, int encode)
PHPAPI zend_result php_url_scanner_add_session_var(const char *name, size_t name_len, const char *value, size_t value_len, bool encode)
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_type_error(const char *format,...)
Definition zend.c:1824
#define ZEND_TSRMLS_CACHE_UPDATE()
Definition zend.h:69
#define zend_catch
Definition zend.h:277
#define zend_try
Definition zend.h:270
#define zend_end_try()
Definition zend.h:280
#define ZEND_TSRMLS_CACHE_DEFINE()
Definition zend.h:68
#define zend_bailout()
Definition zend.h:268
ZEND_API const char * zend_zval_value_name(const zval *arg)
Definition zend_API.c:148
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b)
Definition zend_API.c:1946
ZEND_API zend_result zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error)
Definition zend_API.c:4310
ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str)
Definition zend_API.c:1982
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value)
Definition zend_API.c:2027
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n)
Definition zend_API.c:1928
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len)
Definition zend_API.c:1937
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
struct _zend_fcall_info_cache zend_fcall_info_cache
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define RETVAL_STRING(s)
Definition zend_API.h:1017
#define Z_PARAM_STR_OR_NULL(dest)
Definition zend_API.h:2089
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
Definition zend_API.h:268
#define RETVAL_STR_COPY(s)
Definition zend_API.h:1016
#define RETVAL_NEW_STR(s)
Definition zend_API.h:1015
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define ZEND_GET_MODULE(name)
Definition zend_API.h:241
#define zend_parse_parameters_none()
Definition zend_API.h:353
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define RETVAL_EMPTY_STRING()
Definition zend_API.h:1021
#define Z_PARAM_BOOL_OR_NULL(dest, is_null)
Definition zend_API.h:1729
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_BOOL(b)
Definition zend_API.h:1035
struct _zend_fcall_info zend_fcall_info
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETVAL_TRUE
Definition zend_API.h:1033
#define RETURN_STR(s)
Definition zend_API.h:1039
#define RETVAL_LONG(l)
Definition zend_API.h:1011
ZEND_API zend_result zend_delete_global_variable(zend_string *name)
#define RETVAL_STR(s)
Definition zend_API.h:1013
#define ZVAL_STRINGL(z, s, l)
Definition zend_API.h:952
#define RETVAL_FALSE
Definition zend_API.h:1032
#define RETURN_TRUE
Definition zend_API.h:1059
#define Z_PARAM_ARRAY_HT_OR_LONG(dest_ht, dest_long)
Definition zend_API.h:1866
#define array_init(arg)
Definition zend_API.h:537
#define ZVAL_EMPTY_STRING(z)
Definition zend_API.h:961
#define estrndup(s, length)
Definition zend_alloc.h:165
#define ecalloc(nmemb, size)
Definition zend_alloc.h:158
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
strlen(string $string)
strncmp(string $string1, string $string2, int $length)
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback)
#define strcasecmp(s1, s2)
#define snprintf
ZEND_API zval * zend_get_constant_str(const char *name, size_t name_len)
#define REGISTER_STRINGL_CONSTANT(name, str, len, flags)
#define CONST_DEPRECATED
#define E_NOTICE
Definition zend_errors.h:26
#define E_ERROR
Definition zend_errors.h:23
#define E_WARNING
Definition zend_errors.h:24
#define E_DEPRECATED
Definition zend_errors.h:37
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
ZEND_API const char * get_active_function_name(void)
ZEND_API uint32_t zend_get_executed_lineno(void)
ZEND_API zend_string * zend_get_executed_filename_ex(void)
#define EG(v)
ZEND_API zval *ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:2689
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
Definition zend_hash.c:1869
ZEND_API zval *ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:1002
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:997
ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key)
Definition zend_hash.c:1534
ZEND_API zval *ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key)
Definition zend_hash.c:2668
#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val)
Definition zend_hash.h:1181
#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val)
Definition zend_hash.h:1166
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
ZEND_API zend_result zend_alter_ini_entry_ex(zend_string *name, zend_string *new_value, int modify_type, int stage, bool force_change)
Definition zend_ini.c:356
ZEND_API zend_result zend_alter_ini_entry_chars(zend_string *name, const char *value, size_t value_length, int modify_type, int stage)
Definition zend_ini.c:332
ZEND_API bool zend_ini_parse_bool(zend_string *str)
Definition zend_ini.c:573
ZEND_API zend_long zend_ini_parse_quantity_warn(zend_string *value, zend_string *setting)
Definition zend_ini.c:869
ZEND_API char * zend_ini_string(const char *name, size_t name_length, int orig)
Definition zend_ini.c:505
ZEND_API zend_result zend_alter_ini_entry(zend_string *name, zend_string *new_value, int modify_type, int stage)
Definition zend_ini.c:325
#define ZEND_INI_STAGE_ACTIVATE
Definition zend_ini.h:229
#define ZEND_INI_STAGE_STARTUP
Definition zend_ini.h:227
#define UNREGISTER_INI_ENTRIES()
Definition zend_ini.h:204
#define REGISTER_INI_ENTRIES()
Definition zend_ini.h:203
#define DISPLAY_INI_ENTRIES()
Definition zend_ini.h:205
#define ZEND_INI_PERDIR
Definition zend_ini.h:25
#define ZEND_INI_STAGE_DEACTIVATE
Definition zend_ini.h:230
#define ZEND_INI_GET_ADDR()
Definition zend_ini.h:259
#define ZEND_INI_STAGE_RUNTIME
Definition zend_ini.h:231
#define ZEND_INI_USER
Definition zend_ini.h:24
struct _zend_llist_element zend_llist_element
struct _zend_llist zend_llist
int32_t zend_long
Definition zend_long.h:42
#define ZEND_ATOL(s)
Definition zend_long.h:101
uint32_t zend_ulong
Definition zend_long.h:43
#define ZEND_STRTOL(s0, s1, base)
Definition zend_long.h:85
#define ZEND_LONG_FMT
Definition zend_long.h:87
#define ZEND_LONG_MAX
Definition zend_long.h:45
struct _zend_string zend_string
#define SHUTDOWN_FUNC_ARGS_PASSTHRU
#define INIT_FUNC_ARGS_PASSTHRU
#define ZEND_MOD_END
#define ZEND_MOD_OPTIONAL(name)
struct _zend_module_dep zend_module_dep
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES_EX
#define STANDARD_MODULE_HEADER_EX
ZEND_API zend_string *ZEND_FASTCALL zend_long_to_str(zend_long num)
ZEND_API uint8_t ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval)
#define zval_is_true(op)
#define ZEND_FALLTHROUGH
#define ZEND_ASSERT(c)
#define ZEND_UNREACHABLE()
#define ZEND_VOIDP(ptr)
#define ZEND_COLD
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct stat zend_stat_t
Definition zend_stream.h:94
ZEND_API zend_string_init_interned_func_t zend_string_init_interned
Definition zend_string.c:31
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_INIT_LITERAL(s, persistent)
#define ZSTR_KNOWN(idx)
#define zend_string_equals_literal(str, literal)
#define ZSTR_EMPTY_ALLOC()
#define zend_string_equals_ci(s1, s2)
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define zend_string_equals_literal_ci(str, c)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_TRUE
Definition zend_types.h:603
#define ZVAL_STR(z, s)
#define ZVAL_UNDEF(z)
#define IS_FALSE
Definition zend_types.h:602
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_NULL(z)
#define ZVAL_DEREF(z)
#define IS_STRING
Definition zend_types.h:606
#define IS_PTR
Definition zend_types.h:624
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_ARRAY
Definition zend_types.h:607
#define ZVAL_COPY_DEREF(z, v)
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define Z_PTR_P(zval_p)
#define ZVAL_NEW_REF(z, r)
#define Z_REFVAL(zval)
#define Z_ADDREF_P(pz)
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
#define IS_NULL
Definition zend_types.h:601
#define Z_OBJCE_P(zval_p)
#define Z_STRVAL(zval)
Definition zend_types.h:974
@ FAILURE
Definition zend_types.h:61
#define Z_TRY_ADDREF(z)
#define Z_STRLEN(zval)
Definition zend_types.h:977
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_PTR(z, p)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define SEPARATE_ARRAY(zv)
#define Z_TYPE(zval)
Definition zend_types.h:659
#define Z_ARRVAL(zval)
Definition zend_types.h:986
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
#define ZVAL_COPY_VALUE(z, v)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
#define VCWD_STAT(path, buff)
zval retval
zval * return_value
zend_string * name
bool result
zval * ret
value
out($f, $s)