xref: /PHP-7.4/ext/session/mod_user_class.c (revision 0cf7de1c)
147cfae87SArpad Ray /*
247cfae87SArpad Ray    +----------------------------------------------------------------------+
3d0cb7153SJohannes Schlüter    | PHP Version 7                                                        |
447cfae87SArpad Ray    +----------------------------------------------------------------------+
5*0cf7de1cSZeev Suraski    | Copyright (c) The PHP Group                                          |
647cfae87SArpad Ray    +----------------------------------------------------------------------+
747cfae87SArpad Ray    | This source file is subject to version 3.01 of the PHP license,      |
847cfae87SArpad Ray    | that is bundled with this package in the file LICENSE, and is        |
947cfae87SArpad Ray    | available through the world-wide-web at the following url:           |
1047cfae87SArpad Ray    | http://www.php.net/license/3_01.txt                                  |
1147cfae87SArpad Ray    | If you did not receive a copy of the PHP license and are unable to   |
1247cfae87SArpad Ray    | obtain it through the world-wide-web, please send a note to          |
1347cfae87SArpad Ray    | license@php.net so we can mail you a copy immediately.               |
1447cfae87SArpad Ray    +----------------------------------------------------------------------+
1547cfae87SArpad Ray    | Author: Arpad Ray <arpad@php.net>                                    |
1647cfae87SArpad Ray    +----------------------------------------------------------------------+
1747cfae87SArpad Ray  */
1847cfae87SArpad Ray 
1947cfae87SArpad Ray #include "php.h"
2047cfae87SArpad Ray #include "php_session.h"
2147cfae87SArpad Ray 
2247cfae87SArpad Ray #define PS_SANITY_CHECK						\
23bfb9307bSYasuo Ohgaki 	if (PS(session_status) != php_session_active) { \
2434ff7bbeSYasuo Ohgaki 		php_error_docref(NULL, E_WARNING, "Session is not active"); \
25bfb9307bSYasuo Ohgaki 		RETURN_FALSE; \
26bfb9307bSYasuo Ohgaki 	} \
2747cfae87SArpad Ray 	if (PS(default_mod) == NULL) {				\
286065b29fSAnatol Belski 		php_error_docref(NULL, E_CORE_ERROR, "Cannot call default session handler"); \
2947cfae87SArpad Ray 		RETURN_FALSE;						\
30b7a7b1a6SStanislav Malyshev 	}
3147cfae87SArpad Ray 
3247cfae87SArpad Ray #define PS_SANITY_CHECK_IS_OPEN				\
3347cfae87SArpad Ray 	PS_SANITY_CHECK; \
3447cfae87SArpad Ray 	if (!PS(mod_user_is_open)) {			\
35bdeb220fSAnatol Belski 		php_error_docref(NULL, E_WARNING, "Parent session handler is not open");	\
3647cfae87SArpad Ray 		RETURN_FALSE;						\
37b7a7b1a6SStanislav Malyshev 	}
3847cfae87SArpad Ray 
3947cfae87SArpad Ray /* {{{ proto bool SessionHandler::open(string save_path, string session_name)
4047cfae87SArpad Ray    Wraps the old open handler */
PHP_METHOD(SessionHandler,open)4147cfae87SArpad Ray PHP_METHOD(SessionHandler, open)
4247cfae87SArpad Ray {
4347cfae87SArpad Ray 	char *save_path = NULL, *session_name = NULL;
4432344808SAnatol Belski 	size_t save_path_len, session_name_len;
4534ff7bbeSYasuo Ohgaki 	int ret;
4647cfae87SArpad Ray 
4747cfae87SArpad Ray 	PS_SANITY_CHECK;
4847cfae87SArpad Ray 
49bdeb220fSAnatol Belski 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
5047cfae87SArpad Ray 		return;
5147cfae87SArpad Ray 	}
5247cfae87SArpad Ray 
5347cfae87SArpad Ray 	PS(mod_user_is_open) = 1;
5434ff7bbeSYasuo Ohgaki 
55bfb9307bSYasuo Ohgaki 	zend_try {
5634ff7bbeSYasuo Ohgaki 		ret = PS(default_mod)->s_open(&PS(mod_data), save_path, session_name);
57bfb9307bSYasuo Ohgaki 	} zend_catch {
58bfb9307bSYasuo Ohgaki 		PS(session_status) = php_session_none;
59bfb9307bSYasuo Ohgaki 		zend_bailout();
60bfb9307bSYasuo Ohgaki 	} zend_end_try();
61bfb9307bSYasuo Ohgaki 
62bfb9307bSYasuo Ohgaki 	RETVAL_BOOL(SUCCESS == ret);
6347cfae87SArpad Ray }
6447cfae87SArpad Ray /* }}} */
6547cfae87SArpad Ray 
6647cfae87SArpad Ray /* {{{ proto bool SessionHandler::close()
6747cfae87SArpad Ray    Wraps the old close handler */
PHP_METHOD(SessionHandler,close)6847cfae87SArpad Ray PHP_METHOD(SessionHandler, close)
6947cfae87SArpad Ray {
70bfb9307bSYasuo Ohgaki 	int ret;
71bfb9307bSYasuo Ohgaki 
7247cfae87SArpad Ray 	PS_SANITY_CHECK_IS_OPEN;
7347cfae87SArpad Ray 
7447cfae87SArpad Ray 	// don't return on failure, since not closing the default handler
7547cfae87SArpad Ray 	// could result in memory leaks or other nasties
7647cfae87SArpad Ray 	zend_parse_parameters_none();
77b7a7b1a6SStanislav Malyshev 
7847cfae87SArpad Ray 	PS(mod_user_is_open) = 0;
79bfb9307bSYasuo Ohgaki 
80bfb9307bSYasuo Ohgaki 	zend_try {
8134ff7bbeSYasuo Ohgaki 		ret = PS(default_mod)->s_close(&PS(mod_data));
82bfb9307bSYasuo Ohgaki 	} zend_catch {
83bfb9307bSYasuo Ohgaki 		PS(session_status) = php_session_none;
84bfb9307bSYasuo Ohgaki 		zend_bailout();
85bfb9307bSYasuo Ohgaki 	} zend_end_try();
86bfb9307bSYasuo Ohgaki 
87bfb9307bSYasuo Ohgaki 	RETVAL_BOOL(SUCCESS == ret);
8847cfae87SArpad Ray }
8947cfae87SArpad Ray /* }}} */
9047cfae87SArpad Ray 
9147cfae87SArpad Ray /* {{{ proto bool SessionHandler::read(string id)
9247cfae87SArpad Ray    Wraps the old read handler */
PHP_METHOD(SessionHandler,read)9347cfae87SArpad Ray PHP_METHOD(SessionHandler, read)
9447cfae87SArpad Ray {
953647fc6fSXinchen Hui 	zend_string *val;
963647fc6fSXinchen Hui 	zend_string *key;
9747cfae87SArpad Ray 
9847cfae87SArpad Ray 	PS_SANITY_CHECK_IS_OPEN;
9947cfae87SArpad Ray 
100bdeb220fSAnatol Belski 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
10147cfae87SArpad Ray 		return;
10247cfae87SArpad Ray 	}
10347cfae87SArpad Ray 
104741b5952SYasuo Ohgaki 	if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
105d42d0460SXinchen Hui 		RETURN_FALSE;
10647cfae87SArpad Ray 	}
10747cfae87SArpad Ray 
1083647fc6fSXinchen Hui 	RETURN_STR(val);
10947cfae87SArpad Ray }
11047cfae87SArpad Ray /* }}} */
11147cfae87SArpad Ray 
11247cfae87SArpad Ray /* {{{ proto bool SessionHandler::write(string id, string data)
11347cfae87SArpad Ray    Wraps the old write handler */
PHP_METHOD(SessionHandler,write)11447cfae87SArpad Ray PHP_METHOD(SessionHandler, write)
11547cfae87SArpad Ray {
1163647fc6fSXinchen Hui 	zend_string *key, *val;
11747cfae87SArpad Ray 
11847cfae87SArpad Ray 	PS_SANITY_CHECK_IS_OPEN;
11947cfae87SArpad Ray 
120bdeb220fSAnatol Belski 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
12147cfae87SArpad Ray 		return;
12247cfae87SArpad Ray 	}
12347cfae87SArpad Ray 
124741b5952SYasuo Ohgaki 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
12547cfae87SArpad Ray }
12647cfae87SArpad Ray /* }}} */
12747cfae87SArpad Ray 
12847cfae87SArpad Ray /* {{{ proto bool SessionHandler::destroy(string id)
12947cfae87SArpad Ray    Wraps the old destroy handler */
PHP_METHOD(SessionHandler,destroy)13047cfae87SArpad Ray PHP_METHOD(SessionHandler, destroy)
13147cfae87SArpad Ray {
1323647fc6fSXinchen Hui 	zend_string *key;
13347cfae87SArpad Ray 
13447cfae87SArpad Ray 	PS_SANITY_CHECK_IS_OPEN;
13547cfae87SArpad Ray 
136bdeb220fSAnatol Belski 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
13747cfae87SArpad Ray 		return;
13847cfae87SArpad Ray 	}
139b7a7b1a6SStanislav Malyshev 
140bdeb220fSAnatol Belski 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
14147cfae87SArpad Ray }
14247cfae87SArpad Ray /* }}} */
14347cfae87SArpad Ray 
14447cfae87SArpad Ray /* {{{ proto bool SessionHandler::gc(int maxlifetime)
14547cfae87SArpad Ray    Wraps the old gc handler */
PHP_METHOD(SessionHandler,gc)14647cfae87SArpad Ray PHP_METHOD(SessionHandler, gc)
14747cfae87SArpad Ray {
148c3e3c98eSAnatol Belski 	zend_long maxlifetime;
1491cf179e4SYasuo Ohgaki 	zend_long nrdels = -1;
15047cfae87SArpad Ray 
151ed425186SXinchen Hui 	PS_SANITY_CHECK_IS_OPEN;
15247cfae87SArpad Ray 
153bdeb220fSAnatol Belski 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
15447cfae87SArpad Ray 		return;
15547cfae87SArpad Ray 	}
156b7a7b1a6SStanislav Malyshev 
1571cf179e4SYasuo Ohgaki 	if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
1581cf179e4SYasuo Ohgaki 		RETURN_FALSE;
1591cf179e4SYasuo Ohgaki 	}
1601cf179e4SYasuo Ohgaki 	RETURN_LONG(nrdels);
16147cfae87SArpad Ray }
16247cfae87SArpad Ray /* }}} */
163074c26a6SLeigh 
164074c26a6SLeigh /* {{{ proto char SessionHandler::create_sid()
165074c26a6SLeigh    Wraps the old create_sid handler */
PHP_METHOD(SessionHandler,create_sid)166074c26a6SLeigh PHP_METHOD(SessionHandler, create_sid)
167074c26a6SLeigh {
1683647fc6fSXinchen Hui 	zend_string *id;
169074c26a6SLeigh 
170bc44eb61SAdam Harvey 	PS_SANITY_CHECK;
171bc44eb61SAdam Harvey 
172b794cce4SLeigh 	if (zend_parse_parameters_none() == FAILURE) {
173b794cce4SLeigh 	    return;
174b794cce4SLeigh 	}
175074c26a6SLeigh 
176bdeb220fSAnatol Belski 	id = PS(default_mod)->s_create_sid(&PS(mod_data));
177074c26a6SLeigh 
1783647fc6fSXinchen Hui 	RETURN_STR(id);
179074c26a6SLeigh }
180074c26a6SLeigh /* }}} */
181e6c8640aSYasuo Ohgaki 
182e6c8640aSYasuo Ohgaki /* {{{ proto char SessionUpdateTimestampHandler::validateId(string id)
183e6c8640aSYasuo Ohgaki    Simply return TRUE */
PHP_METHOD(SessionHandler,validateId)184e6c8640aSYasuo Ohgaki PHP_METHOD(SessionHandler, validateId)
185e6c8640aSYasuo Ohgaki {
186e6c8640aSYasuo Ohgaki 	zend_string *key;
187e6c8640aSYasuo Ohgaki 
188e6c8640aSYasuo Ohgaki 	PS_SANITY_CHECK_IS_OPEN;
189e6c8640aSYasuo Ohgaki 
190e6c8640aSYasuo Ohgaki 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
191e6c8640aSYasuo Ohgaki 		return;
192e6c8640aSYasuo Ohgaki 	}
193e6c8640aSYasuo Ohgaki 
194e6c8640aSYasuo Ohgaki 	/* Legacy save handler may not support validate_sid API. Return TRUE. */
195e6c8640aSYasuo Ohgaki 	RETURN_TRUE;
196e6c8640aSYasuo Ohgaki }
197e6c8640aSYasuo Ohgaki /* }}} */
198e6c8640aSYasuo Ohgaki 
199e6c8640aSYasuo Ohgaki /* {{{ proto bool SessionUpdateTimestampHandler::updateTimestamp(string id, string data)
200e6c8640aSYasuo Ohgaki    Simply call update_timestamp */
PHP_METHOD(SessionHandler,updateTimestamp)201e6c8640aSYasuo Ohgaki PHP_METHOD(SessionHandler, updateTimestamp)
202e6c8640aSYasuo Ohgaki {
203e6c8640aSYasuo Ohgaki 	zend_string *key, *val;
204e6c8640aSYasuo Ohgaki 
205e6c8640aSYasuo Ohgaki 	PS_SANITY_CHECK_IS_OPEN;
206e6c8640aSYasuo Ohgaki 
207e6c8640aSYasuo Ohgaki 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
208e6c8640aSYasuo Ohgaki 		return;
209e6c8640aSYasuo Ohgaki 	}
210e6c8640aSYasuo Ohgaki 
211e6c8640aSYasuo Ohgaki 	/* Legacy save handler may not support update_timestamp API. Just write. */
212741b5952SYasuo Ohgaki 	RETVAL_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
213e6c8640aSYasuo Ohgaki }
214e6c8640aSYasuo Ohgaki /* }}} */
215