xref: /php-src/ext/session/mod_user_class.c (revision 46f45a51)
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    | 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 		zend_throw_error(NULL, "Session is not active"); \
23 		RETURN_THROWS(); \
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 	zend_result 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 	zend_try {
51 		ret = PS(default_mod)->s_open(&PS(mod_data), save_path, session_name);
52 	} zend_catch {
53 		PS(session_status) = php_session_none;
54 		zend_bailout();
55 	} zend_end_try();
56 
57 	if (SUCCESS == ret) {
58 		PS(mod_user_is_open) = 1;
59 	}
60 
61 	RETURN_BOOL(SUCCESS == ret);
62 }
63 /* }}} */
64 
65 /* {{{ Wraps the old close handler */
PHP_METHOD(SessionHandler,close)66 PHP_METHOD(SessionHandler, close)
67 {
68 	zend_result ret;
69 
70 	// don't return on failure, since not closing the default handler
71 	// could result in memory leaks or other nasties
72 	zend_parse_parameters_none();
73 
74 	PS_SANITY_CHECK_IS_OPEN;
75 
76 	PS(mod_user_is_open) = 0;
77 
78 	zend_try {
79 		ret = PS(default_mod)->s_close(&PS(mod_data));
80 	} zend_catch {
81 		PS(session_status) = php_session_none;
82 		zend_bailout();
83 	} zend_end_try();
84 
85 	RETURN_BOOL(SUCCESS == ret);
86 }
87 /* }}} */
88 
89 /* {{{ Wraps the old read handler */
PHP_METHOD(SessionHandler,read)90 PHP_METHOD(SessionHandler, read)
91 {
92 	zend_string *val;
93 	zend_string *key;
94 
95 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
96 		RETURN_THROWS();
97 	}
98 
99 	PS_SANITY_CHECK_IS_OPEN;
100 
101 	if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
102 		RETURN_FALSE;
103 	}
104 
105 	RETURN_STR(val);
106 }
107 /* }}} */
108 
109 /* {{{ Wraps the old write handler */
PHP_METHOD(SessionHandler,write)110 PHP_METHOD(SessionHandler, write)
111 {
112 	zend_string *key, *val;
113 
114 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
115 		RETURN_THROWS();
116 	}
117 
118 	PS_SANITY_CHECK_IS_OPEN;
119 
120 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
121 }
122 /* }}} */
123 
124 /* {{{ Wraps the old destroy handler */
PHP_METHOD(SessionHandler,destroy)125 PHP_METHOD(SessionHandler, destroy)
126 {
127 	zend_string *key;
128 
129 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
130 		RETURN_THROWS();
131 	}
132 
133 	PS_SANITY_CHECK_IS_OPEN;
134 
135 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
136 }
137 /* }}} */
138 
139 /* {{{ Wraps the old gc handler */
PHP_METHOD(SessionHandler,gc)140 PHP_METHOD(SessionHandler, gc)
141 {
142 	zend_long maxlifetime;
143 	zend_long nrdels = -1;
144 
145 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
146 		RETURN_THROWS();
147 	}
148 
149 	PS_SANITY_CHECK_IS_OPEN;
150 
151 	if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
152 		RETURN_FALSE;
153 	}
154 	RETURN_LONG(nrdels);
155 }
156 /* }}} */
157 
158 /* {{{ Wraps the old create_sid handler */
PHP_METHOD(SessionHandler,create_sid)159 PHP_METHOD(SessionHandler, create_sid)
160 {
161 	zend_string *id;
162 
163 	if (zend_parse_parameters_none() == FAILURE) {
164 	    RETURN_THROWS();
165 	}
166 
167 	PS_SANITY_CHECK;
168 
169 	id = PS(default_mod)->s_create_sid(&PS(mod_data));
170 
171 	RETURN_STR(id);
172 }
173 /* }}} */
174