xref: /PHP-5.4/ext/session/php_session.h (revision c0d060f5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2014 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_session_rfc1867_progress {
99 
100 	size_t    sname_len;
101 	zval      sid;
102 	smart_str key;
103 
104 	long      update_step;
105 	long      next_update;
106 	double    next_update_time;
107 	zend_bool cancel_upload;
108 	zend_bool apply_trans_sid;
109 	size_t    content_length;
110 
111 	zval      *data;                 /* the array exported to session data */
112 	zval      *post_bytes_processed; /* data["bytes_processed"] */
113 	zval      *files;                /* data["files"] array */
114 	zval      *current_file;         /* array of currently uploading file */
115 	zval      *current_file_bytes_processed;
116 } php_session_rfc1867_progress;
117 
118 typedef struct _php_ps_globals {
119 	char *save_path;
120 	char *session_name;
121 	char *id;
122 	char *extern_referer_chk;
123 	char *entropy_file;
124 	char *cache_limiter;
125 	long entropy_length;
126 	long cookie_lifetime;
127 	char *cookie_path;
128 	char *cookie_domain;
129 	zend_bool  cookie_secure;
130 	zend_bool  cookie_httponly;
131 	ps_module *mod;
132 	ps_module *default_mod;
133 	void *mod_data;
134 	php_session_status session_status;
135 	long gc_probability;
136 	long gc_divisor;
137 	long gc_maxlifetime;
138 	int module_number;
139 	long cache_expire;
140 	union {
141 		zval *names[6];
142 		struct {
143 			zval *ps_open;
144 			zval *ps_close;
145 			zval *ps_read;
146 			zval *ps_write;
147 			zval *ps_destroy;
148 			zval *ps_gc;
149 		} name;
150 	} mod_user_names;
151 	int mod_user_implemented;
152 	int mod_user_is_open;
153 	const struct ps_serializer_struct *serializer;
154 	zval *http_session_vars;
155 	zend_bool auto_start;
156 	zend_bool use_cookies;
157 	zend_bool use_only_cookies;
158 	zend_bool use_trans_sid;	/* contains the INI value of whether to use trans-sid */
159 	zend_bool apply_trans_sid;	/* whether or not to enable trans-sid for the current request */
160 
161 	long hash_func;
162 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
163 	php_hash_ops *hash_ops;
164 #endif
165 	long hash_bits_per_character;
166 	int send_cookie;
167 	int define_sid;
168 	zend_bool invalid_session_id;	/* allows the driver to report about an invalid session id and request id regeneration */
169 
170 	php_session_rfc1867_progress *rfc1867_progress;
171 	zend_bool rfc1867_enabled; /* session.upload_progress.enabled */
172 	zend_bool rfc1867_cleanup; /* session.upload_progress.cleanup */
173 	smart_str rfc1867_prefix;  /* session.upload_progress.prefix */
174 	smart_str rfc1867_name;    /* session.upload_progress.name */
175 	long rfc1867_freq;         /* session.upload_progress.freq */
176 	double rfc1867_min_freq;   /* session.upload_progress.min_freq */
177 } php_ps_globals;
178 
179 typedef php_ps_globals zend_ps_globals;
180 
181 extern zend_module_entry session_module_entry;
182 #define phpext_session_ptr &session_module_entry
183 
184 #ifdef ZTS
185 #define PS(v) TSRMG(ps_globals_id, php_ps_globals *, v)
186 #else
187 #define PS(v) (ps_globals.v)
188 #endif
189 
190 #define PS_SERIALIZER_ENCODE_ARGS char **newstr, int *newlen TSRMLS_DC
191 #define PS_SERIALIZER_DECODE_ARGS const char *val, int vallen TSRMLS_DC
192 
193 typedef struct ps_serializer_struct {
194 	const char *name;
195 	int (*encode)(PS_SERIALIZER_ENCODE_ARGS);
196 	int (*decode)(PS_SERIALIZER_DECODE_ARGS);
197 } ps_serializer;
198 
199 #define PS_SERIALIZER_ENCODE_NAME(x) ps_srlzr_encode_##x
200 #define PS_SERIALIZER_DECODE_NAME(x) ps_srlzr_decode_##x
201 
202 #define PS_SERIALIZER_ENCODE_FUNC(x) \
203 	int PS_SERIALIZER_ENCODE_NAME(x)(PS_SERIALIZER_ENCODE_ARGS)
204 #define PS_SERIALIZER_DECODE_FUNC(x) \
205 	int PS_SERIALIZER_DECODE_NAME(x)(PS_SERIALIZER_DECODE_ARGS)
206 
207 #define PS_SERIALIZER_FUNCS(x) \
208 	PS_SERIALIZER_ENCODE_FUNC(x); \
209 	PS_SERIALIZER_DECODE_FUNC(x)
210 
211 #define PS_SERIALIZER_ENTRY(x) \
212 	{ #x, PS_SERIALIZER_ENCODE_NAME(x), PS_SERIALIZER_DECODE_NAME(x) }
213 
214 PHPAPI void session_adapt_url(const char *, size_t, char **, size_t * TSRMLS_DC);
215 
216 PHPAPI void php_add_session_var(char *name, size_t namelen TSRMLS_DC);
217 PHPAPI void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC);
218 PHPAPI int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC);
219 
220 PHPAPI int php_session_register_module(ps_module *);
221 
222 PHPAPI int php_session_register_serializer(const char *name,
223 	        int (*encode)(PS_SERIALIZER_ENCODE_ARGS),
224 	        int (*decode)(PS_SERIALIZER_DECODE_ARGS));
225 
226 PHPAPI void php_session_set_id(char *id TSRMLS_DC);
227 PHPAPI void php_session_start(TSRMLS_D);
228 
229 PHPAPI ps_module *_php_find_ps_module(char *name TSRMLS_DC);
230 PHPAPI const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC);
231 
232 #define PS_ADD_VARL(name,namelen) do {										\
233 	php_add_session_var(name, namelen TSRMLS_CC);							\
234 } while (0)
235 
236 #define PS_ADD_VAR(name) PS_ADD_VARL(name, strlen(name))
237 
238 #define PS_DEL_VARL(name,namelen) do {										\
239 	if (PS(http_session_vars)) {											\
240 		zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), name, namelen+1);	\
241 	}																		\
242 } while (0)
243 
244 
245 #define PS_ENCODE_VARS 											\
246 	char *key;													\
247 	uint key_length;											\
248 	ulong num_key;												\
249 	zval **struc;
250 
251 #define PS_ENCODE_LOOP(code) do {									\
252 		HashTable *_ht = Z_ARRVAL_P(PS(http_session_vars));			\
253 		int key_type;												\
254 																	\
255 		for (zend_hash_internal_pointer_reset(_ht);					\
256 				(key_type = zend_hash_get_current_key_ex(_ht, &key, &key_length, &num_key, 0, NULL)) != HASH_KEY_NON_EXISTANT; \
257 					zend_hash_move_forward(_ht)) {					\
258 			if (key_type == HASH_KEY_IS_LONG) {						\
259 				php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Skipping numeric key %ld", num_key);	\
260 				continue;											\
261 			}														\
262 			key_length--;											\
263 			if (php_get_session_var(key, key_length, &struc TSRMLS_CC) == SUCCESS) {	\
264 				code;		 										\
265 			} 														\
266 		}															\
267 	} while(0)
268 
269 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(ps)
270 
271 void php_session_auto_start(void *data);
272 void php_session_shutdown(void *data);
273 
274 #define PS_CLASS_NAME "SessionHandler"
275 extern zend_class_entry *php_session_class_entry;
276 
277 #define PS_IFACE_NAME "SessionHandlerInterface"
278 extern zend_class_entry *php_session_iface_entry;
279 
280 extern PHP_METHOD(SessionHandler, open);
281 extern PHP_METHOD(SessionHandler, close);
282 extern PHP_METHOD(SessionHandler, read);
283 extern PHP_METHOD(SessionHandler, write);
284 extern PHP_METHOD(SessionHandler, destroy);
285 extern PHP_METHOD(SessionHandler, gc);
286 
287 #endif
288