xref: /PHP-7.3/ext/session/php_session.h (revision 08b9310e)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 #ifndef PHP_SESSION_H
20 #define PHP_SESSION_H
21 
22 #include "ext/standard/php_var.h"
23 
24 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)
25 # include "ext/hash/php_hash.h"
26 #endif
27 
28 #define PHP_SESSION_API 20161017
29 
30 #include "php_version.h"
31 #define PHP_SESSION_VERSION PHP_VERSION
32 
33 /* save handler macros */
34 #define PS_NUM_APIS      9
35 #define PS_OPEN_ARGS     void **mod_data, const char *save_path, const char *session_name
36 #define PS_CLOSE_ARGS    void **mod_data
37 #define PS_READ_ARGS     void **mod_data, zend_string *key, zend_string **val, zend_long maxlifetime
38 #define PS_WRITE_ARGS    void **mod_data, zend_string *key, zend_string *val, zend_long maxlifetime
39 #define PS_DESTROY_ARGS  void **mod_data, zend_string *key
40 #define PS_GC_ARGS       void **mod_data, zend_long maxlifetime, zend_long *nrdels
41 #define PS_CREATE_SID_ARGS void **mod_data
42 #define PS_VALIDATE_SID_ARGS void **mod_data, zend_string *key
43 #define PS_UPDATE_TIMESTAMP_ARGS void **mod_data, zend_string *key, zend_string *val, zend_long maxlifetime
44 
45 typedef struct ps_module_struct {
46 	const char *s_name;
47 	int (*s_open)(PS_OPEN_ARGS);
48 	int (*s_close)(PS_CLOSE_ARGS);
49 	int (*s_read)(PS_READ_ARGS);
50 	int (*s_write)(PS_WRITE_ARGS);
51 	int (*s_destroy)(PS_DESTROY_ARGS);
52 	zend_long (*s_gc)(PS_GC_ARGS);
53 	zend_string *(*s_create_sid)(PS_CREATE_SID_ARGS);
54 	int (*s_validate_sid)(PS_VALIDATE_SID_ARGS);
55 	int (*s_update_timestamp)(PS_UPDATE_TIMESTAMP_ARGS);
56 } ps_module;
57 
58 #define PS_GET_MOD_DATA() *mod_data
59 #define PS_SET_MOD_DATA(a) *mod_data = (a)
60 
61 #define PS_OPEN_FUNC(x) 	int ps_open_##x(PS_OPEN_ARGS)
62 #define PS_CLOSE_FUNC(x) 	int ps_close_##x(PS_CLOSE_ARGS)
63 #define PS_READ_FUNC(x) 	int ps_read_##x(PS_READ_ARGS)
64 #define PS_WRITE_FUNC(x) 	int ps_write_##x(PS_WRITE_ARGS)
65 #define PS_DESTROY_FUNC(x) 	int ps_delete_##x(PS_DESTROY_ARGS)
66 #define PS_GC_FUNC(x) 		zend_long ps_gc_##x(PS_GC_ARGS)
67 #define PS_CREATE_SID_FUNC(x)	zend_string *ps_create_sid_##x(PS_CREATE_SID_ARGS)
68 #define PS_VALIDATE_SID_FUNC(x)	int ps_validate_sid_##x(PS_VALIDATE_SID_ARGS)
69 #define PS_UPDATE_TIMESTAMP_FUNC(x) 	int ps_update_timestamp_##x(PS_UPDATE_TIMESTAMP_ARGS)
70 
71 /* Legacy save handler module definitions */
72 #define PS_FUNCS(x) \
73 	PS_OPEN_FUNC(x); \
74 	PS_CLOSE_FUNC(x); \
75 	PS_READ_FUNC(x); \
76 	PS_WRITE_FUNC(x); \
77 	PS_DESTROY_FUNC(x); \
78 	PS_GC_FUNC(x);	\
79 	PS_CREATE_SID_FUNC(x)
80 
81 #define PS_MOD(x) \
82 	#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
83 	 ps_delete_##x, ps_gc_##x, php_session_create_id, \
84 	 php_session_validate_sid, php_session_update_timestamp
85 
86 /* Legacy SID creation enabled save handler module definitions */
87 #define PS_FUNCS_SID(x) \
88 	PS_OPEN_FUNC(x); \
89 	PS_CLOSE_FUNC(x); \
90 	PS_READ_FUNC(x); \
91 	PS_WRITE_FUNC(x); \
92 	PS_DESTROY_FUNC(x); \
93 	PS_GC_FUNC(x); \
94 	PS_CREATE_SID_FUNC(x); \
95 	PS_VALIDATE_SID_FUNC(x); \
96 	PS_UPDATE_TIMESTAMP_FUNC(x);
97 
98 #define PS_MOD_SID(x) \
99 	#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
100 	 ps_delete_##x, ps_gc_##x, ps_create_sid_##x, \
101 	 php_session_validate_sid, php_session_update_timestamp
102 
103 /* Update timestamp enabled save handler module definitions
104    New save handlers should use this API */
105 #define PS_FUNCS_UPDATE_TIMESTAMP(x) \
106 	PS_OPEN_FUNC(x); \
107 	PS_CLOSE_FUNC(x); \
108 	PS_READ_FUNC(x); \
109 	PS_WRITE_FUNC(x); \
110 	PS_DESTROY_FUNC(x); \
111 	PS_GC_FUNC(x); \
112 	PS_CREATE_SID_FUNC(x); \
113 	PS_VALIDATE_SID_FUNC(x); \
114 	PS_UPDATE_TIMESTAMP_FUNC(x);
115 
116 #define PS_MOD_UPDATE_TIMESTAMP(x) \
117 	#x, ps_open_##x, ps_close_##x, ps_read_##x, ps_write_##x, \
118 	 ps_delete_##x, ps_gc_##x, ps_create_sid_##x, \
119 	 ps_validate_sid_##x, ps_update_timestamp_##x
120 
121 
122 typedef enum {
123 	php_session_disabled,
124 	php_session_none,
125 	php_session_active
126 } php_session_status;
127 
128 typedef struct _php_session_rfc1867_progress {
129 	size_t    sname_len;
130 	zval      sid;
131 	smart_str key;
132 
133 	zend_long      update_step;
134 	zend_long      next_update;
135 	double    next_update_time;
136 	zend_bool cancel_upload;
137 	zend_bool apply_trans_sid;
138 	size_t    content_length;
139 
140 	zval      data;                 /* the array exported to session data */
141 	zval	 *post_bytes_processed; /* data["bytes_processed"] */
142 	zval      files;                /* data["files"] array */
143 	zval      current_file;         /* array of currently uploading file */
144 	zval	 *current_file_bytes_processed;
145 } php_session_rfc1867_progress;
146 
147 typedef struct _php_ps_globals {
148 	char *save_path;
149 	char *session_name;
150 	zend_string *id;
151 	char *extern_referer_chk;
152 	char *cache_limiter;
153 	zend_long cookie_lifetime;
154 	char *cookie_path;
155 	char *cookie_domain;
156 	zend_bool  cookie_secure;
157 	zend_bool  cookie_httponly;
158 	char *cookie_samesite;
159 	const ps_module *mod;
160 	const ps_module *default_mod;
161 	void *mod_data;
162 	php_session_status session_status;
163 	zend_long gc_probability;
164 	zend_long gc_divisor;
165 	zend_long gc_maxlifetime;
166 	int module_number;
167 	zend_long cache_expire;
168 	union {
169 		zval names[PS_NUM_APIS];
170 		struct {
171 			zval ps_open;
172 			zval ps_close;
173 			zval ps_read;
174 			zval ps_write;
175 			zval ps_destroy;
176 			zval ps_gc;
177 			zval ps_create_sid;
178 			zval ps_validate_sid;
179 			zval ps_update_timestamp;
180 		} name;
181 	} mod_user_names;
182 	int mod_user_implemented;
183 	int mod_user_is_open;
184 	const struct ps_serializer_struct *serializer;
185 	zval http_session_vars;
186 	zend_bool auto_start;
187 	zend_bool use_cookies;
188 	zend_bool use_only_cookies;
189 	zend_bool use_trans_sid; /* contains the INI value of whether to use trans-sid */
190 
191 	zend_long sid_length;
192 	zend_long sid_bits_per_character;
193 	int send_cookie;
194 	int define_sid;
195 
196 	php_session_rfc1867_progress *rfc1867_progress;
197 	zend_bool rfc1867_enabled; /* session.upload_progress.enabled */
198 	zend_bool rfc1867_cleanup; /* session.upload_progress.cleanup */
199 	char *rfc1867_prefix;  /* session.upload_progress.prefix */
200 	char *rfc1867_name;    /* session.upload_progress.name */
201 	zend_long rfc1867_freq;         /* session.upload_progress.freq */
202 	double rfc1867_min_freq;   /* session.upload_progress.min_freq */
203 
204 	zend_bool use_strict_mode; /* whether or not PHP accepts unknown session ids */
205 	zend_bool lazy_write; /* omit session write when it is possible */
206 	zend_bool in_save_handler; /* state if session is in save handler or not */
207 	zend_bool set_handler;     /* state if session module i setting handler or not */
208 	zend_string *session_vars; /* serialized original session data */
209 } php_ps_globals;
210 
211 typedef php_ps_globals zend_ps_globals;
212 
213 extern zend_module_entry session_module_entry;
214 #define phpext_session_ptr &session_module_entry
215 
216 #ifdef ZTS
217 #define PS(v) ZEND_TSRMG(ps_globals_id, php_ps_globals *, v)
218 #ifdef COMPILE_DL_SESSION
219 ZEND_TSRMLS_CACHE_EXTERN()
220 #endif
221 #else
222 #define PS(v) (ps_globals.v)
223 #endif
224 
225 #define PS_SERIALIZER_ENCODE_ARGS void
226 #define PS_SERIALIZER_DECODE_ARGS const char *val, size_t vallen
227 
228 typedef struct ps_serializer_struct {
229 	const char *name;
230 	zend_string *(*encode)(PS_SERIALIZER_ENCODE_ARGS);
231 	int (*decode)(PS_SERIALIZER_DECODE_ARGS);
232 } ps_serializer;
233 
234 #define PS_SERIALIZER_ENCODE_NAME(x) ps_srlzr_encode_##x
235 #define PS_SERIALIZER_DECODE_NAME(x) ps_srlzr_decode_##x
236 
237 #define PS_SERIALIZER_ENCODE_FUNC(x) \
238 	zend_string *PS_SERIALIZER_ENCODE_NAME(x)(PS_SERIALIZER_ENCODE_ARGS)
239 #define PS_SERIALIZER_DECODE_FUNC(x) \
240 	int PS_SERIALIZER_DECODE_NAME(x)(PS_SERIALIZER_DECODE_ARGS)
241 
242 #define PS_SERIALIZER_FUNCS(x) \
243 	PS_SERIALIZER_ENCODE_FUNC(x); \
244 	PS_SERIALIZER_DECODE_FUNC(x)
245 
246 #define PS_SERIALIZER_ENTRY(x) \
247 	{ #x, PS_SERIALIZER_ENCODE_NAME(x), PS_SERIALIZER_DECODE_NAME(x) }
248 
249 /* default create id function */
250 PHPAPI zend_string *php_session_create_id(PS_CREATE_SID_ARGS);
251 /* Dummy PS module functions */
252 PHPAPI int php_session_validate_sid(PS_VALIDATE_SID_ARGS);
253 PHPAPI int php_session_update_timestamp(PS_UPDATE_TIMESTAMP_ARGS);
254 
255 PHPAPI void session_adapt_url(const char *, size_t, char **, size_t *);
256 
257 PHPAPI int php_session_destroy(void);
258 PHPAPI void php_add_session_var(zend_string *name);
259 PHPAPI zval *php_set_session_var(zend_string *name, zval *state_val, php_unserialize_data_t *var_hash);
260 PHPAPI zval *php_get_session_var(zend_string *name);
261 
262 PHPAPI int php_session_register_module(const ps_module *);
263 
264 PHPAPI int php_session_register_serializer(const char *name,
265 	        zend_string *(*encode)(PS_SERIALIZER_ENCODE_ARGS),
266 	        int (*decode)(PS_SERIALIZER_DECODE_ARGS));
267 
268 PHPAPI void php_session_set_id(char *id);
269 PHPAPI int php_session_start(void);
270 PHPAPI int php_session_flush(int write);
271 
272 PHPAPI const ps_module *_php_find_ps_module(char *name);
273 PHPAPI const ps_serializer *_php_find_ps_serializer(char *name);
274 
275 PHPAPI int php_session_valid_key(const char *key);
276 PHPAPI int php_session_reset_id(void);
277 
278 #define PS_ADD_VARL(name) do {										\
279 	php_add_session_var(name);							\
280 } while (0)
281 
282 #define PS_ADD_VAR(name) PS_ADD_VARL(name)
283 
284 #define PS_DEL_VARL(name) do {										\
285 	if (!Z_ISNULL(PS(http_session_vars))) {							\
286 		zend_hash_del(Z_ARRVAL(PS(http_session_vars)), name);		\
287 	}																\
288 } while (0)
289 
290 
291 #define PS_ENCODE_VARS 												\
292 	zend_string *key;												\
293 	zend_ulong num_key;													\
294 	zval *struc;
295 
296 #define PS_ENCODE_LOOP(code) do {									\
297 	HashTable *_ht = Z_ARRVAL_P(Z_REFVAL(PS(http_session_vars)));	\
298 	ZEND_HASH_FOREACH_KEY(_ht, num_key, key) {						\
299 		if (key == NULL) {											\
300 			php_error_docref(NULL, E_NOTICE,						\
301 					"Skipping numeric key " ZEND_LONG_FMT, num_key);\
302 			continue;												\
303 		}															\
304 		if ((struc = php_get_session_var(key))) {					\
305 			code;		 											\
306 		} 															\
307 	} ZEND_HASH_FOREACH_END();										\
308 } while(0)
309 
310 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(ps)
311 
312 void php_session_auto_start(void *data);
313 
314 #define PS_CLASS_NAME "SessionHandler"
315 extern PHPAPI zend_class_entry *php_session_class_entry;
316 
317 #define PS_IFACE_NAME "SessionHandlerInterface"
318 extern PHPAPI zend_class_entry *php_session_iface_entry;
319 
320 #define PS_SID_IFACE_NAME "SessionIdInterface"
321 extern PHPAPI zend_class_entry *php_session_id_iface_entry;
322 
323 #define PS_UPDATE_TIMESTAMP_IFACE_NAME "SessionUpdateTimestampHandlerInterface"
324 extern PHPAPI zend_class_entry *php_session_update_timestamp_iface_entry;
325 
326 extern PHP_METHOD(SessionHandler, open);
327 extern PHP_METHOD(SessionHandler, close);
328 extern PHP_METHOD(SessionHandler, read);
329 extern PHP_METHOD(SessionHandler, write);
330 extern PHP_METHOD(SessionHandler, destroy);
331 extern PHP_METHOD(SessionHandler, gc);
332 extern PHP_METHOD(SessionHandler, create_sid);
333 extern PHP_METHOD(SessionHandler, validateId);
334 extern PHP_METHOD(SessionHandler, updateTimestamp);
335 
336 #endif
337