xref: /PHP-5.3/ext/session/php_session.h (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 #ifndef PHP_SESSION_H
22 #define PHP_SESSION_H
23 
24 #include "ext/standard/php_var.h"
25 
26 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
27 # include "ext/hash/php_hash.h"
28 #endif
29 
30 #define PHP_SESSION_API 20020330
31 
32 #define PS_OPEN_ARGS void **mod_data, const char *save_path, const char *session_name TSRMLS_DC
33 #define PS_CLOSE_ARGS void **mod_data TSRMLS_DC
34 #define PS_READ_ARGS void **mod_data, const char *key, char **val, int *vallen TSRMLS_DC
35 #define PS_WRITE_ARGS void **mod_data, const char *key, const char *val, const int vallen TSRMLS_DC
36 #define PS_DESTROY_ARGS void **mod_data, const char *key TSRMLS_DC
37 #define PS_GC_ARGS void **mod_data, int maxlifetime, int *nrdels TSRMLS_DC
38 #define PS_CREATE_SID_ARGS void **mod_data, int *newlen TSRMLS_DC
39 
40 /* default create id function */
41 PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS);
42 
43 typedef struct ps_module_struct {
44 	const char *s_name;
45 	int (*s_open)(PS_OPEN_ARGS);
46 	int (*s_close)(PS_CLOSE_ARGS);
47 	int (*s_read)(PS_READ_ARGS);
48 	int (*s_write)(PS_WRITE_ARGS);
49 	int (*s_destroy)(PS_DESTROY_ARGS);
50 	int (*s_gc)(PS_GC_ARGS);
51 	char *(*s_create_sid)(PS_CREATE_SID_ARGS);
52 } ps_module;
53 
54 #define PS_GET_MOD_DATA() *mod_data
55 #define PS_SET_MOD_DATA(a) *mod_data = (a)
56 
57 #define PS_OPEN_FUNC(x) 	int ps_open_##x(PS_OPEN_ARGS)
58 #define PS_CLOSE_FUNC(x) 	int ps_close_##x(PS_CLOSE_ARGS)
59 #define PS_READ_FUNC(x) 	int ps_read_##x(PS_READ_ARGS)
60 #define PS_WRITE_FUNC(x) 	int ps_write_##x(PS_WRITE_ARGS)
61 #define PS_DESTROY_FUNC(x) 	int ps_delete_##x(PS_DESTROY_ARGS)
62 #define PS_GC_FUNC(x) 		int ps_gc_##x(PS_GC_ARGS)
63 #define PS_CREATE_SID_FUNC(x)	char *ps_create_sid_##x(PS_CREATE_SID_ARGS)
64 
65 #define PS_FUNCS(x) \
66 	PS_OPEN_FUNC(x); \
67 	PS_CLOSE_FUNC(x); \
68 	PS_READ_FUNC(x); \
69 	PS_WRITE_FUNC(x); \
70 	PS_DESTROY_FUNC(x); \
71 	PS_GC_FUNC(x);	\
72 	PS_CREATE_SID_FUNC(x)
73 
74 #define PS_MOD(x) \
75 	#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
76 	 ps_delete_##x, ps_gc_##x, php_session_create_id
77 
78 /* SID enabled module handler definitions */
79 #define PS_FUNCS_SID(x) \
80 	PS_OPEN_FUNC(x); \
81 	PS_CLOSE_FUNC(x); \
82 	PS_READ_FUNC(x); \
83 	PS_WRITE_FUNC(x); \
84 	PS_DESTROY_FUNC(x); \
85 	PS_GC_FUNC(x); \
86 	PS_CREATE_SID_FUNC(x)
87 
88 #define PS_MOD_SID(x) \
89 	#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
90 	 ps_delete_##x, ps_gc_##x, ps_create_sid_##x
91 
92 typedef enum {
93 	php_session_disabled,
94 	php_session_none,
95 	php_session_active
96 } php_session_status;
97 
98 typedef struct _php_ps_globals {
99 	char *save_path;
100 	char *session_name;
101 	char *id;
102 	char *extern_referer_chk;
103 	char *entropy_file;
104 	char *cache_limiter;
105 	long entropy_length;
106 	long cookie_lifetime;
107 	char *cookie_path;
108 	char *cookie_domain;
109 	zend_bool  cookie_secure;
110 	zend_bool  cookie_httponly;
111 	ps_module *mod;
112 	void *mod_data;
113 	php_session_status session_status;
114 	long gc_probability;
115 	long gc_divisor;
116 	long gc_maxlifetime;
117 	int module_number;
118 	long cache_expire;
119 	union {
120 		zval *names[6];
121 		struct {
122 			zval *ps_open;
123 			zval *ps_close;
124 			zval *ps_read;
125 			zval *ps_write;
126 			zval *ps_destroy;
127 			zval *ps_gc;
128 		} name;
129 	} mod_user_names;
130 	zend_bool bug_compat; /* Whether to behave like PHP 4.2 and earlier */
131 	zend_bool bug_compat_warn; /* Whether to warn about it */
132 	const struct ps_serializer_struct *serializer;
133 	zval *http_session_vars;
134 	zend_bool auto_start;
135 	zend_bool use_cookies;
136 	zend_bool use_only_cookies;
137 	zend_bool use_trans_sid;	/* contains the INI value of whether to use trans-sid */
138 	zend_bool apply_trans_sid;	/* whether or not to enable trans-sid for the current request */
139 
140 	long hash_func;
141 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
142 	php_hash_ops *hash_ops;
143 #endif
144 	long hash_bits_per_character;
145 	int send_cookie;
146 	int define_sid;
147 	zend_bool invalid_session_id;	/* allows the driver to report about an invalid session id and request id regeneration */
148 } php_ps_globals;
149 
150 typedef php_ps_globals zend_ps_globals;
151 
152 extern zend_module_entry session_module_entry;
153 #define phpext_session_ptr &session_module_entry
154 
155 #ifdef ZTS
156 #define PS(v) TSRMG(ps_globals_id, php_ps_globals *, v)
157 #else
158 #define PS(v) (ps_globals.v)
159 #endif
160 
161 #define PS_SERIALIZER_ENCODE_ARGS char **newstr, int *newlen TSRMLS_DC
162 #define PS_SERIALIZER_DECODE_ARGS const char *val, int vallen TSRMLS_DC
163 
164 typedef struct ps_serializer_struct {
165 	const char *name;
166 	int (*encode)(PS_SERIALIZER_ENCODE_ARGS);
167 	int (*decode)(PS_SERIALIZER_DECODE_ARGS);
168 } ps_serializer;
169 
170 #define PS_SERIALIZER_ENCODE_NAME(x) ps_srlzr_encode_##x
171 #define PS_SERIALIZER_DECODE_NAME(x) ps_srlzr_decode_##x
172 
173 #define PS_SERIALIZER_ENCODE_FUNC(x) \
174 	int PS_SERIALIZER_ENCODE_NAME(x)(PS_SERIALIZER_ENCODE_ARGS)
175 #define PS_SERIALIZER_DECODE_FUNC(x) \
176 	int PS_SERIALIZER_DECODE_NAME(x)(PS_SERIALIZER_DECODE_ARGS)
177 
178 #define PS_SERIALIZER_FUNCS(x) \
179 	PS_SERIALIZER_ENCODE_FUNC(x); \
180 	PS_SERIALIZER_DECODE_FUNC(x)
181 
182 #define PS_SERIALIZER_ENTRY(x) \
183 	{ #x, PS_SERIALIZER_ENCODE_NAME(x), PS_SERIALIZER_DECODE_NAME(x) }
184 
185 PHPAPI void session_adapt_url(const char *, size_t, char **, size_t * TSRMLS_DC);
186 
187 PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC);
188 PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC);
189 PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC);
190 
191 PHPAPI int php_session_register_module(ps_module *);
192 
193 PHPAPI int php_session_register_serializer(const char *name,
194 	        int (*encode)(PS_SERIALIZER_ENCODE_ARGS),
195 	        int (*decode)(PS_SERIALIZER_DECODE_ARGS));
196 
197 PHPAPI void php_session_set_id(char *id TSRMLS_DC);
198 PHPAPI void php_session_start(TSRMLS_D);
199 
200 PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC);
201 PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC);
202 
203 #define PS_ADD_VARL(name,namelen) do {										\
204 	php_add_session_var(name, namelen TSRMLS_CC);							\
205 } while (0)
206 
207 #define PS_ADD_VAR(name) PS_ADD_VARL(name, strlen(name))
208 
209 #define PS_DEL_VARL(name,namelen) do {										\
210 	if (PS(http_session_vars)) {											\
211 		zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), name, namelen+1);	\
212 	}																		\
213 } while (0)
214 
215 
216 #define PS_ENCODE_VARS 											\
217 	char *key;													\
218 	uint key_length;											\
219 	ulong num_key;												\
220 	zval **struc;
221 
222 #define PS_ENCODE_LOOP(code) do {									\
223 		HashTable *_ht = Z_ARRVAL_P(PS(http_session_vars));			\
224 		int key_type;												\
225 																	\
226 		for (zend_hash_internal_pointer_reset(_ht);					\
227 				(key_type = zend_hash_get_current_key_ex(_ht, &key, &key_length, &num_key, 0, NULL)) != HASH_KEY_NON_EXISTANT; \
228 					zend_hash_move_forward(_ht)) {					\
229 			if (key_type == HASH_KEY_IS_LONG) {						\
230 				php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Skipping numeric key %ld", num_key);	\
231 				continue;											\
232 			}														\
233 			key_length--;											\
234 			if (php_get_session_var(key, key_length, &struc TSRMLS_CC) == SUCCESS) {	\
235 				code;		 										\
236 			} 														\
237 		}															\
238 	} while(0)
239 
240 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(ps)
241 
242 void php_session_auto_start(void *data);
243 void php_session_shutdown(void *data);
244 
245 #endif
246