xref: /PHP-8.0/ext/session/mod_user_class.c (revision 2ca5d663)
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    | http://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    | Author: Arpad Ray <arpad@php.net>                                    |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include "php.h"
18 #include "php_session.h"
19 
20 #define PS_SANITY_CHECK						\
21 	if (PS(session_status) != php_session_active) { \
22 		php_error_docref(NULL, E_WARNING, "Session is not active"); \
23 		RETURN_FALSE; \
24 	} \
25 	if (PS(default_mod) == NULL) { \
26 		zend_throw_error(NULL, "Cannot call default session handler"); \
27 		RETURN_THROWS(); \
28 	}
29 
30 #define PS_SANITY_CHECK_IS_OPEN				\
31 	PS_SANITY_CHECK; \
32 	if (!PS(mod_user_is_open)) {			\
33 		php_error_docref(NULL, E_WARNING, "Parent session handler is not open");	\
34 		RETURN_FALSE;						\
35 	}
36 
37 /* {{{ Wraps the old open handler */
PHP_METHOD(SessionHandler,open)38 PHP_METHOD(SessionHandler, open)
39 {
40 	char *save_path = NULL, *session_name = NULL;
41 	size_t save_path_len, session_name_len;
42 	int ret;
43 
44 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
45 		RETURN_THROWS();
46 	}
47 
48 	PS_SANITY_CHECK;
49 
50 	PS(mod_user_is_open) = 1;
51 
52 	zend_try {
53 		ret = PS(default_mod)->s_open(&PS(mod_data), save_path, session_name);
54 	} zend_catch {
55 		PS(session_status) = php_session_none;
56 		zend_bailout();
57 	} zend_end_try();
58 
59 	RETVAL_BOOL(SUCCESS == ret);
60 }
61 /* }}} */
62 
63 /* {{{ Wraps the old close handler */
PHP_METHOD(SessionHandler,close)64 PHP_METHOD(SessionHandler, close)
65 {
66 	int ret;
67 
68 	// don't return on failure, since not closing the default handler
69 	// could result in memory leaks or other nasties
70 	zend_parse_parameters_none();
71 
72 	PS_SANITY_CHECK_IS_OPEN;
73 
74 	PS(mod_user_is_open) = 0;
75 
76 	zend_try {
77 		ret = PS(default_mod)->s_close(&PS(mod_data));
78 	} zend_catch {
79 		PS(session_status) = php_session_none;
80 		zend_bailout();
81 	} zend_end_try();
82 
83 	RETVAL_BOOL(SUCCESS == ret);
84 }
85 /* }}} */
86 
87 /* {{{ Wraps the old read handler */
PHP_METHOD(SessionHandler,read)88 PHP_METHOD(SessionHandler, read)
89 {
90 	zend_string *val;
91 	zend_string *key;
92 
93 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
94 		RETURN_THROWS();
95 	}
96 
97 	PS_SANITY_CHECK_IS_OPEN;
98 
99 	if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
100 		RETURN_FALSE;
101 	}
102 
103 	RETURN_STR(val);
104 }
105 /* }}} */
106 
107 /* {{{ Wraps the old write handler */
PHP_METHOD(SessionHandler,write)108 PHP_METHOD(SessionHandler, write)
109 {
110 	zend_string *key, *val;
111 
112 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
113 		RETURN_THROWS();
114 	}
115 
116 	PS_SANITY_CHECK_IS_OPEN;
117 
118 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
119 }
120 /* }}} */
121 
122 /* {{{ Wraps the old destroy handler */
PHP_METHOD(SessionHandler,destroy)123 PHP_METHOD(SessionHandler, destroy)
124 {
125 	zend_string *key;
126 
127 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
128 		RETURN_THROWS();
129 	}
130 
131 	PS_SANITY_CHECK_IS_OPEN;
132 
133 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
134 }
135 /* }}} */
136 
137 /* {{{ Wraps the old gc handler */
PHP_METHOD(SessionHandler,gc)138 PHP_METHOD(SessionHandler, gc)
139 {
140 	zend_long maxlifetime;
141 	zend_long nrdels = -1;
142 
143 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
144 		RETURN_THROWS();
145 	}
146 
147 	PS_SANITY_CHECK_IS_OPEN;
148 
149 	if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
150 		RETURN_FALSE;
151 	}
152 	RETURN_LONG(nrdels);
153 }
154 /* }}} */
155 
156 /* {{{ Wraps the old create_sid handler */
PHP_METHOD(SessionHandler,create_sid)157 PHP_METHOD(SessionHandler, create_sid)
158 {
159 	zend_string *id;
160 
161 	if (zend_parse_parameters_none() == FAILURE) {
162 	    RETURN_THROWS();
163 	}
164 
165 	PS_SANITY_CHECK;
166 
167 	id = PS(default_mod)->s_create_sid(&PS(mod_data));
168 
169 	RETURN_STR(id);
170 }
171 /* }}} */
172