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