1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2017 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: Sascha Schumann <sascha@schumann.cx> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "php.h"
22 #include "php_session.h"
23 #include "mod_user.h"
24
25 ps_module ps_mod_user = {
26 PS_MOD_UPDATE_TIMESTAMP(user)
27 };
28
29
ps_call_handler(zval * func,int argc,zval * argv,zval * retval)30 static void ps_call_handler(zval *func, int argc, zval *argv, zval *retval)
31 {
32 int i;
33 if (call_user_function(EG(function_table), NULL, func, retval, argc, argv) == FAILURE) {
34 zval_ptr_dtor(retval);
35 ZVAL_UNDEF(retval);
36 } else if (Z_ISUNDEF_P(retval)) {
37 ZVAL_NULL(retval);
38 }
39 for (i = 0; i < argc; i++) {
40 zval_ptr_dtor(&argv[i]);
41 }
42 }
43
44 #define STDVARS \
45 zval retval; \
46 int ret = FAILURE
47
48 #define PSF(a) PS(mod_user_names).name.ps_##a
49
50 #define FINISH \
51 if (Z_TYPE(retval) != IS_UNDEF) { \
52 if (Z_TYPE(retval) == IS_TRUE) { \
53 ret = SUCCESS; \
54 } else if (Z_TYPE(retval) == IS_FALSE) { \
55 ret = FAILURE; \
56 } else if ((Z_TYPE(retval) == IS_LONG) && (Z_LVAL(retval) == -1)) { \
57 /* BC for clever users - Deprecate me */ \
58 ret = FAILURE; \
59 } else if ((Z_TYPE(retval) == IS_LONG) && (Z_LVAL(retval) == 0)) { \
60 /* BC for clever users - Deprecate me */ \
61 ret = SUCCESS; \
62 } else { \
63 if (!EG(exception)) { \
64 php_error_docref(NULL, E_WARNING, \
65 "Session callback expects true/false return value"); \
66 } \
67 ret = FAILURE; \
68 zval_ptr_dtor(&retval); \
69 } \
70 } \
71 return ret
72
PS_OPEN_FUNC(user)73 PS_OPEN_FUNC(user)
74 {
75 zval args[2];
76 STDVARS;
77
78 if (Z_ISUNDEF(PSF(open))) {
79 php_error_docref(NULL, E_WARNING,
80 "user session functions not defined");
81
82 return FAILURE;
83 }
84
85 ZVAL_STRING(&args[0], (char*)save_path);
86 ZVAL_STRING(&args[1], (char*)session_name);
87
88 ps_call_handler(&PSF(open), 2, args, &retval);
89 PS(mod_user_implemented) = 1;
90
91 FINISH;
92 }
93
PS_CLOSE_FUNC(user)94 PS_CLOSE_FUNC(user)
95 {
96 zend_bool bailout = 0;
97 STDVARS;
98
99 if (!PS(mod_user_implemented)) {
100 /* already closed */
101 return SUCCESS;
102 }
103
104 zend_try {
105 ps_call_handler(&PSF(close), 0, NULL, &retval);
106 } zend_catch {
107 bailout = 1;
108 } zend_end_try();
109
110 PS(mod_user_implemented) = 0;
111
112 if (bailout) {
113 if (!Z_ISUNDEF(retval)) {
114 zval_ptr_dtor(&retval);
115 }
116 zend_bailout();
117 }
118
119 FINISH;
120 }
121
PS_READ_FUNC(user)122 PS_READ_FUNC(user)
123 {
124 zval args[1];
125 STDVARS;
126
127 ZVAL_STR_COPY(&args[0], key);
128
129 ps_call_handler(&PSF(read), 1, args, &retval);
130
131 if (!Z_ISUNDEF(retval)) {
132 if (Z_TYPE(retval) == IS_STRING) {
133 *val = zend_string_copy(Z_STR(retval));
134 ret = SUCCESS;
135 }
136 zval_ptr_dtor(&retval);
137 }
138
139 return ret;
140 }
141
PS_WRITE_FUNC(user)142 PS_WRITE_FUNC(user)
143 {
144 zval args[2];
145 STDVARS;
146
147 ZVAL_STR_COPY(&args[0], key);
148 ZVAL_STR_COPY(&args[1], val);
149
150 ps_call_handler(&PSF(write), 2, args, &retval);
151
152 FINISH;
153 }
154
PS_DESTROY_FUNC(user)155 PS_DESTROY_FUNC(user)
156 {
157 zval args[1];
158 STDVARS;
159
160 ZVAL_STR_COPY(&args[0], key);
161
162 ps_call_handler(&PSF(destroy), 1, args, &retval);
163
164 FINISH;
165 }
166
PS_GC_FUNC(user)167 PS_GC_FUNC(user)
168 {
169 zval args[1];
170 STDVARS;
171
172 ZVAL_LONG(&args[0], maxlifetime);
173
174 ps_call_handler(&PSF(gc), 1, args, &retval);
175
176 FINISH;
177 }
178
PS_CREATE_SID_FUNC(user)179 PS_CREATE_SID_FUNC(user)
180 {
181 /* maintain backwards compatibility */
182 if (!Z_ISUNDEF(PSF(create_sid))) {
183 zend_string *id = NULL;
184 zval retval;
185
186 ps_call_handler(&PSF(create_sid), 0, NULL, &retval);
187
188 if (!Z_ISUNDEF(retval)) {
189 if (Z_TYPE(retval) == IS_STRING) {
190 id = zend_string_copy(Z_STR(retval));
191 }
192 zval_ptr_dtor(&retval);
193 } else {
194 php_error_docref(NULL, E_ERROR, "No session id returned by function");
195 return NULL;
196 }
197
198 if (!id) {
199 php_error_docref(NULL, E_ERROR, "Session id must be a string");
200 return NULL;
201 }
202
203 return id;
204 }
205
206 /* function as defined by PS_MOD */
207 return php_session_create_id(mod_data);
208 }
209
PS_VALIDATE_SID_FUNC(user)210 PS_VALIDATE_SID_FUNC(user)
211 {
212 /* maintain backwards compatibility */
213 if (!Z_ISUNDEF(PSF(validate_sid))) {
214 zval args[1];
215 STDVARS;
216
217 ZVAL_STR_COPY(&args[0], key);
218
219 ps_call_handler(&PSF(validate_sid), 1, args, &retval);
220
221 FINISH;
222 }
223
224 /* dummy function defined by PS_MOD */
225 return php_session_validate_sid(mod_data, key);
226 }
227
PS_UPDATE_TIMESTAMP_FUNC(user)228 PS_UPDATE_TIMESTAMP_FUNC(user)
229 {
230 zval args[2];
231 STDVARS;
232
233 ZVAL_STR_COPY(&args[0], key);
234 ZVAL_STR_COPY(&args[1], val);
235
236 /* maintain backwards compatibility */
237 if (!Z_ISUNDEF(PSF(update_timestamp))) {
238 ps_call_handler(&PSF(update_timestamp), 2, args, &retval);
239 } else {
240 ps_call_handler(&PSF(write), 2, args, &retval);
241 }
242
243 FINISH;
244 }
245
246 /*
247 * Local variables:
248 * tab-width: 4
249 * c-basic-offset: 4
250 * End:
251 * vim600: sw=4 ts=4 fdm=marker
252 * vim<600: sw=4 ts=4
253 */
254