-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
yaf_session.c
361 lines (296 loc) · 10.1 KB
/
yaf_session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
+----------------------------------------------------------------------+
| Yet Another Framework |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "Zend/zend_interfaces.h" /* for zend_ce_iterator, zend_ce_countable*/
#include "php_yaf.h"
#include "yaf_namespace.h"
#include "yaf_session.h"
#include "yaf_exception.h"
#if PHP_MAJOR_VERSION > 7
#include "yaf_session_arginfo.h"
#else
#include "yaf_session_legacy_arginfo.h"
#endif
zend_class_entry *yaf_session_ce;
static zend_object_handlers yaf_session_obj_handlers;
#if defined(HAVE_SPL) && PHP_VERSION_ID < 70200
extern PHPAPI zend_class_entry *spl_ce_Countable;
#endif
static inline void yaf_session_start(yaf_session_object *session) /* {{{ */ {
if (session->flags & YAF_SESSION_STARTED) {
return;
}
php_session_start();
session->flags |= YAF_SESSION_STARTED;
}
/* }}} */
static HashTable *yaf_session_get_properties(yaf_object *obj) /* {{{ */ {
zval rv;
HashTable *ht;
yaf_session_object *sess = php_yaf_session_fetch_object(yaf_strip_obj(obj));
if (!sess->properties) {
ALLOC_HASHTABLE(sess->properties);
zend_hash_init(sess->properties, 2, NULL, ZVAL_PTR_DTOR, 0);
YAF_ALLOW_VIOLATION(sess->properties);
}
ht = sess->properties;
ZVAL_BOOL(&rv, sess->flags & YAF_SESSION_STARTED);
zend_hash_str_update(ht, "started:protected", sizeof("started:protected") - 1, &rv);
if (sess->session) {
ZVAL_ARR(&rv, sess->session);
Z_ADDREF(rv);
} else {
ZVAL_NULL(&rv);
}
zend_hash_str_update(ht, "session:protected", sizeof("session:protected") - 1, &rv);
return ht;
}
/* }}} */
static void yaf_session_object_free(zend_object *object) /* {{{ */ {
yaf_session_object *sess = php_yaf_session_fetch_object(object);
if (sess->properties) {
if (GC_DELREF(sess->properties)) {
GC_REMOVE_FROM_BUFFER(sess->properties);
zend_array_destroy(sess->properties);
}
}
zend_object_std_dtor(object);
}
/* }}} */
zend_object_iterator *yaf_session_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */ {
yaf_iterator *iterator;
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(object);
if (by_ref) {
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
}
if (!sess->session) {
return NULL;
}
iterator = emalloc(sizeof(yaf_iterator));
zend_iterator_init(&iterator->intern);
iterator->intern.funcs = &yaf_iterator_funcs;
ZVAL_ARR(&iterator->intern.data, sess->session);
Z_ADDREF(iterator->intern.data);
ZVAL_UNDEF(&iterator->current);
return &iterator->intern;
}
/* }}} */
static yaf_session_t *yaf_session_instance() /* {{{ */ {
zval *pzval;
yaf_session_object *sess;
yaf_session_t *instance = &YAF_G(session);
if (IS_OBJECT == Z_TYPE_P(instance)) {
return instance;
}
sess = emalloc(sizeof(yaf_session_object) + zend_object_properties_size(yaf_session_ce));
zend_object_std_init(&sess->std, yaf_session_ce);
sess->std.handlers = &yaf_session_obj_handlers;
ZVAL_OBJ(&YAF_G(session), &sess->std);
sess->flags = 0;
yaf_session_start(sess);
if ((pzval = zend_hash_find(&EG(symbol_table), YAF_KNOWN_STR(YAF_VAR_SESSION))) == NULL ||
Z_TYPE_P(pzval) != IS_REFERENCE || Z_TYPE_P(Z_REFVAL_P(pzval)) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING, "Attempt to start session failed");
sess->session = NULL;
return &YAF_G(session);
}
sess->session = Z_ARRVAL_P(Z_REFVAL_P(pzval));
sess->properties = NULL;
return &YAF_G(session);
}
/* }}} */
/** {{{ proto private Yaf_Session::__construct(void)
*/
PHP_METHOD(yaf_session, __construct) {
}
/* }}} */
/** {{{ proto public Yaf_Session::getInstance(void)
*/
PHP_METHOD(yaf_session, getInstance) {
yaf_session_t *instance;
if ((instance = yaf_session_instance())) {
RETURN_ZVAL(instance, 1, 0);
} else {
RETURN_NULL();
}
}
/* }}} */
/** {{{ proto public Yaf_Session::count(void)
*/
PHP_METHOD(yaf_session, count) {
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
if (sess->session) {
RETURN_LONG(zend_hash_num_elements(sess->session));
}
}
/* }}} */
/** {{{ proto public static Yaf_Session::start()
*/
PHP_METHOD(yaf_session, start) {
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
yaf_session_start(sess);
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/** {{{ proto public static Yaf_Session::get($name)
*/
PHP_METHOD(yaf_session, get) {
zend_string *name = NULL;
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &name) == FAILURE) {
return;
}
if (EXPECTED(sess->session)) {
if (name == NULL) {
RETVAL_ARR(sess->session);
Z_ADDREF_P(return_value);
return;
} else {
zval *val;
if ((val = zend_hash_find(sess->session, name))) {
RETURN_ZVAL(val, 1, 0);
}
}
}
RETURN_NULL();
}
/* }}} */
/** {{{ proto public static Yaf_Session::has($name)
*/
PHP_METHOD(yaf_session, has) {
zend_string *name;
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S!", &name) == FAILURE) {
return;
}
if (EXPECTED(sess->session)) {
RETURN_BOOL(zend_hash_exists(sess->session, name));
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public static Yaf_Session::set($name, $value)
*/
PHP_METHOD(yaf_session, set) {
zval *value;
zend_string *name;
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &value) == FAILURE) {
return;
}
if (EXPECTED(sess->session)) {
if (zend_hash_update(sess->session, name, value)) {
Z_TRY_ADDREF_P(value);
RETURN_TRUE;
}
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public static Yaf_Session::del($name)
*/
PHP_METHOD(yaf_session, del) {
zend_string *name;
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return;
}
if (EXPECTED(sess->session)) {
if (zend_hash_del(sess->session, name)) {
RETURN_TRUE;
}
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public static Yaf_Session::clear()
*/
PHP_METHOD(yaf_session, clear) {
yaf_session_object *sess = Z_YAFSESSIONOBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if (EXPECTED(sess->session)) {
zend_hash_clean(sess->session);
RETURN_ZVAL(getThis(), 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ yaf_session_methods
*/
zend_function_entry yaf_session_methods[] = {
PHP_ME(yaf_session, __construct, arginfo_class_Yaf_Session___construct, ZEND_ACC_CTOR|ZEND_ACC_PRIVATE)
PHP_ME(yaf_session, getInstance, arginfo_class_Yaf_Session_getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(yaf_session, start, arginfo_class_Yaf_Session_start, ZEND_ACC_PUBLIC)
PHP_ME(yaf_session, get, arginfo_class_Yaf_Session_get, ZEND_ACC_PUBLIC)
PHP_ME(yaf_session, has, arginfo_class_Yaf_Session_has, ZEND_ACC_PUBLIC)
PHP_ME(yaf_session, set, arginfo_class_Yaf_Session_set, ZEND_ACC_PUBLIC)
PHP_ME(yaf_session, del, arginfo_class_Yaf_Session_del, ZEND_ACC_PUBLIC)
PHP_ME(yaf_session, count, arginfo_class_Yaf_Session_count, ZEND_ACC_PUBLIC)
PHP_ME(yaf_session, clear, arginfo_class_Yaf_Session_clear, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, offsetGet, get, arginfo_class_Yaf_Session_offsetGet, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, offsetSet, set, arginfo_class_Yaf_Session_offsetSet, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, offsetExists, has, arginfo_class_Yaf_Session_offsetExists, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, offsetUnSet, del, arginfo_class_Yaf_Session_offsetUnSet, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, __get, get, arginfo_class_Yaf_Session___get, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, __isset, has, arginfo_class_Yaf_Session___isset, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, __set, set, arginfo_class_Yaf_Session___set, ZEND_ACC_PUBLIC)
PHP_MALIAS(yaf_session, __unset, del, arginfo_class_Yaf_Session___unset, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ YAF_STARTUP_FUNCTION
*/
YAF_STARTUP_FUNCTION(session) {
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Session", "Yaf\\Session", yaf_session_methods);
yaf_session_ce = zend_register_internal_class_ex(&ce, NULL);
yaf_session_ce->get_iterator = yaf_session_get_iterator;
#if PHP_VERSION_ID < 80100
yaf_session_ce->ce_flags |= ZEND_ACC_FINAL;
yaf_session_ce->serialize = zend_class_serialize_deny;
yaf_session_ce->unserialize = zend_class_unserialize_deny;
#else
yaf_session_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NOT_SERIALIZABLE;
#endif
memcpy(&yaf_session_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
yaf_session_obj_handlers.offset = XtOffsetOf(yaf_session_object, std);
yaf_session_obj_handlers.free_obj = yaf_session_object_free;
yaf_session_obj_handlers.clone_obj = NULL;
yaf_session_obj_handlers.get_gc = yaf_fake_get_gc;
yaf_session_obj_handlers.get_properties = yaf_session_get_properties;
#if defined(HAVE_SPL) && PHP_VERSION_ID < 70200
zend_class_implements(yaf_session_ce, 3, zend_ce_iterator, zend_ce_arrayaccess, spl_ce_Countable);
#elif PHP_VERSION_ID >= 70200
zend_class_implements(yaf_session_ce, 3, zend_ce_iterator, zend_ce_arrayaccess, zend_ce_countable);
#else
zend_class_implements(yaf_session_ce, 2, zend_ce_iterator, zend_ce_arrayaccess);
#endif
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/