xref: /PHP-7.2/ext/session/mod_user_class.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Arpad Ray <arpad@php.net>                                    |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #include "php.h"
22 #include "php_session.h"
23 
24 #define PS_SANITY_CHECK						\
25 	if (PS(session_status) != php_session_active) { \
26 		php_error_docref(NULL, E_WARNING, "Session is not active"); \
27 		RETURN_FALSE; \
28 	} \
29 	if (PS(default_mod) == NULL) {				\
30 		php_error_docref(NULL, E_CORE_ERROR, "Cannot call default session handler"); \
31 		RETURN_FALSE;						\
32 	}
33 
34 #define PS_SANITY_CHECK_IS_OPEN				\
35 	PS_SANITY_CHECK; \
36 	if (!PS(mod_user_is_open)) {			\
37 		php_error_docref(NULL, E_WARNING, "Parent session handler is not open");	\
38 		RETURN_FALSE;						\
39 	}
40 
41 /* {{{ proto bool SessionHandler::open(string save_path, string session_name)
42    Wraps the old open handler */
PHP_METHOD(SessionHandler,open)43 PHP_METHOD(SessionHandler, open)
44 {
45 	char *save_path = NULL, *session_name = NULL;
46 	size_t save_path_len, session_name_len;
47 	int ret;
48 
49 	PS_SANITY_CHECK;
50 
51 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
52 		return;
53 	}
54 
55 	PS(mod_user_is_open) = 1;
56 
57 	zend_try {
58 		ret = PS(default_mod)->s_open(&PS(mod_data), save_path, session_name);
59 	} zend_catch {
60 		PS(session_status) = php_session_none;
61 		zend_bailout();
62 	} zend_end_try();
63 
64 	RETVAL_BOOL(SUCCESS == ret);
65 }
66 /* }}} */
67 
68 /* {{{ proto bool SessionHandler::close()
69    Wraps the old close handler */
PHP_METHOD(SessionHandler,close)70 PHP_METHOD(SessionHandler, close)
71 {
72 	int ret;
73 
74 	PS_SANITY_CHECK_IS_OPEN;
75 
76 	// don't return on failure, since not closing the default handler
77 	// could result in memory leaks or other nasties
78 	zend_parse_parameters_none();
79 
80 	PS(mod_user_is_open) = 0;
81 
82 	zend_try {
83 		ret = PS(default_mod)->s_close(&PS(mod_data));
84 	} zend_catch {
85 		PS(session_status) = php_session_none;
86 		zend_bailout();
87 	} zend_end_try();
88 
89 	RETVAL_BOOL(SUCCESS == ret);
90 }
91 /* }}} */
92 
93 /* {{{ proto bool SessionHandler::read(string id)
94    Wraps the old read handler */
PHP_METHOD(SessionHandler,read)95 PHP_METHOD(SessionHandler, read)
96 {
97 	zend_string *val;
98 	zend_string *key;
99 
100 	PS_SANITY_CHECK_IS_OPEN;
101 
102 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
103 		return;
104 	}
105 
106 	if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
107 		RETURN_FALSE;
108 	}
109 
110 	RETURN_STR(val);
111 }
112 /* }}} */
113 
114 /* {{{ proto bool SessionHandler::write(string id, string data)
115    Wraps the old write handler */
PHP_METHOD(SessionHandler,write)116 PHP_METHOD(SessionHandler, write)
117 {
118 	zend_string *key, *val;
119 
120 	PS_SANITY_CHECK_IS_OPEN;
121 
122 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
123 		return;
124 	}
125 
126 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
127 }
128 /* }}} */
129 
130 /* {{{ proto bool SessionHandler::destroy(string id)
131    Wraps the old destroy handler */
PHP_METHOD(SessionHandler,destroy)132 PHP_METHOD(SessionHandler, destroy)
133 {
134 	zend_string *key;
135 
136 	PS_SANITY_CHECK_IS_OPEN;
137 
138 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
139 		return;
140 	}
141 
142 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
143 }
144 /* }}} */
145 
146 /* {{{ proto bool SessionHandler::gc(int maxlifetime)
147    Wraps the old gc handler */
PHP_METHOD(SessionHandler,gc)148 PHP_METHOD(SessionHandler, gc)
149 {
150 	zend_long maxlifetime;
151 	zend_long nrdels = -1;
152 
153 	PS_SANITY_CHECK_IS_OPEN;
154 
155 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
156 		return;
157 	}
158 
159 	if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
160 		RETURN_FALSE;
161 	}
162 	RETURN_LONG(nrdels);
163 }
164 /* }}} */
165 
166 /* {{{ proto char SessionHandler::create_sid()
167    Wraps the old create_sid handler */
PHP_METHOD(SessionHandler,create_sid)168 PHP_METHOD(SessionHandler, create_sid)
169 {
170 	zend_string *id;
171 
172 	PS_SANITY_CHECK;
173 
174 	if (zend_parse_parameters_none() == FAILURE) {
175 	    return;
176 	}
177 
178 	id = PS(default_mod)->s_create_sid(&PS(mod_data));
179 
180 	RETURN_STR(id);
181 }
182 /* }}} */
183 
184 /* {{{ proto char SessionUpdateTimestampHandler::validateId(string id)
185    Simply return TRUE */
PHP_METHOD(SessionHandler,validateId)186 PHP_METHOD(SessionHandler, validateId)
187 {
188 	zend_string *key;
189 
190 	PS_SANITY_CHECK_IS_OPEN;
191 
192 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
193 		return;
194 	}
195 
196 	/* Legacy save handler may not support validate_sid API. Return TRUE. */
197 	RETURN_TRUE;
198 }
199 /* }}} */
200 
201 /* {{{ proto bool SessionUpdateTimestampHandler::updateTimestamp(string id, string data)
202    Simply call update_timestamp */
PHP_METHOD(SessionHandler,updateTimestamp)203 PHP_METHOD(SessionHandler, updateTimestamp)
204 {
205 	zend_string *key, *val;
206 
207 	PS_SANITY_CHECK_IS_OPEN;
208 
209 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
210 		return;
211 	}
212 
213 	/* Legacy save handler may not support update_timestamp API. Just write. */
214 	RETVAL_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
215 }
216 /* }}} */
217