xref: /PHP-5.3/ext/session/mod_user.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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(user)
27 };
28 
29 #define SESS_ZVAL_LONG(val, a)						\
30 {													\
31 	MAKE_STD_ZVAL(a);								\
32 	ZVAL_LONG(a, val);								\
33 }
34 
35 #define SESS_ZVAL_STRING(vl, a)						\
36 {													\
37 	char *__vl = vl;								\
38 	SESS_ZVAL_STRINGN(__vl, strlen(__vl), a);		\
39 }
40 
41 #define SESS_ZVAL_STRINGN(vl, ln, a)				\
42 {													\
43 	MAKE_STD_ZVAL(a);								\
44 	ZVAL_STRINGL(a, vl, ln, 1);						\
45 }
46 
ps_call_handler(zval * func,int argc,zval ** argv TSRMLS_DC)47 static zval *ps_call_handler(zval *func, int argc, zval **argv TSRMLS_DC)
48 {
49 	int i;
50 	zval *retval = NULL;
51 
52 	MAKE_STD_ZVAL(retval);
53 	if (call_user_function(EG(function_table), NULL, func, retval, argc, argv TSRMLS_CC) == FAILURE) {
54 		zval_ptr_dtor(&retval);
55 		retval = NULL;
56 	}
57 
58 	for (i = 0; i < argc; i++) {
59 		zval_ptr_dtor(&argv[i]);
60 	}
61 
62 	return retval;
63 }
64 
65 #define STDVARS1							\
66 	zval *retval = NULL;					\
67 	int ret = FAILURE
68 
69 #define STDVARS								\
70 	STDVARS1;								\
71 	char *mdata = PS_GET_MOD_DATA();		\
72 	if (!mdata) { return FAILURE; }
73 
74 #define PSF(a) PS(mod_user_names).name.ps_##a
75 
76 #define FINISH								\
77 	if (retval) {							\
78 		convert_to_long(retval);			\
79 		ret = Z_LVAL_P(retval);				\
80 		zval_ptr_dtor(&retval);				\
81 	}										\
82 	return ret
83 
PS_OPEN_FUNC(user)84 PS_OPEN_FUNC(user)
85 {
86 	zval *args[2];
87 	static char dummy = 0;
88 	STDVARS1;
89 
90 	if (PSF(open) == NULL) {
91 		php_error_docref(NULL TSRMLS_CC, E_WARNING,
92 			"user session functions not defined");
93 
94 		return FAILURE;
95 	}
96 
97 	SESS_ZVAL_STRING((char*)save_path, args[0]);
98 	SESS_ZVAL_STRING((char*)session_name, args[1]);
99 
100 	retval = ps_call_handler(PSF(open), 2, args TSRMLS_CC);
101 	if (retval) {
102 		/* This is necessary to fool the session module. Yes, it's safe to
103 		 * use a static. Neither mod_user nor the session module itself will
104 		 * ever touch this pointer. It could be set to 0xDEADBEEF for all the
105 		 * difference it makes, but for the sake of paranoia it's set to some
106 		 * valid value. */
107 		PS_SET_MOD_DATA(&dummy);
108 	}
109 
110 	FINISH;
111 }
112 
PS_CLOSE_FUNC(user)113 PS_CLOSE_FUNC(user)
114 {
115 	STDVARS1;
116 
117 	retval = ps_call_handler(PSF(close), 0, NULL TSRMLS_CC);
118 
119 	PS_SET_MOD_DATA(NULL);
120 
121 	FINISH;
122 }
123 
PS_READ_FUNC(user)124 PS_READ_FUNC(user)
125 {
126 	zval *args[1];
127 	STDVARS;
128 
129 	SESS_ZVAL_STRING((char*)key, args[0]);
130 
131 	retval = ps_call_handler(PSF(read), 1, args TSRMLS_CC);
132 
133 	if (retval) {
134 		if (Z_TYPE_P(retval) == IS_STRING) {
135 			*val = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
136 			*vallen = Z_STRLEN_P(retval);
137 			ret = SUCCESS;
138 		}
139 		zval_ptr_dtor(&retval);
140 	}
141 
142 	return ret;
143 }
144 
PS_WRITE_FUNC(user)145 PS_WRITE_FUNC(user)
146 {
147 	zval *args[2];
148 	STDVARS;
149 
150 	SESS_ZVAL_STRING((char*)key, args[0]);
151 	SESS_ZVAL_STRINGN((char*)val, vallen, args[1]);
152 
153 	retval = ps_call_handler(PSF(write), 2, args TSRMLS_CC);
154 
155 	FINISH;
156 }
157 
PS_DESTROY_FUNC(user)158 PS_DESTROY_FUNC(user)
159 {
160 	zval *args[1];
161 	STDVARS;
162 
163 	SESS_ZVAL_STRING((char*)key, args[0]);
164 
165 	retval = ps_call_handler(PSF(destroy), 1, args TSRMLS_CC);
166 
167 	FINISH;
168 }
169 
PS_GC_FUNC(user)170 PS_GC_FUNC(user)
171 {
172 	zval *args[1];
173 	STDVARS;
174 
175 	SESS_ZVAL_LONG(maxlifetime, args[0]);
176 
177 	retval = ps_call_handler(PSF(gc), 1, args TSRMLS_CC);
178 
179 	FINISH;
180 }
181 
182 /*
183  * Local variables:
184  * tab-width: 4
185  * c-basic-offset: 4
186  * End:
187  * vim600: sw=4 ts=4 fdm=marker
188  * vim<600: sw=4 ts=4
189  */
190