1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2016 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: Arpad Ray <arpad@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "php.h"
22 #include "php_session.h"
23
24 #define PS_SANITY_CHECK \
25 if (PS(default_mod) == NULL) { \
26 php_error_docref(NULL TSRMLS_CC, E_CORE_ERROR, "Cannot call default session handler"); \
27 RETURN_FALSE; \
28 }
29
30 #define PS_SANITY_CHECK_IS_OPEN \
31 PS_SANITY_CHECK; \
32 if (!PS(mod_user_is_open)) { \
33 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parent session handler is not open"); \
34 RETURN_FALSE; \
35 }
36
37 /* {{{ proto bool SessionHandler::open(string save_path, string session_name)
38 Wraps the old open handler */
PHP_METHOD(SessionHandler,open)39 PHP_METHOD(SessionHandler, open)
40 {
41 char *save_path = NULL, *session_name = NULL;
42 int save_path_len, session_name_len;
43
44 PS_SANITY_CHECK;
45
46 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &save_path, &save_path_len, &session_name, &session_name_len) == FAILURE) {
47 return;
48 }
49
50 PS(mod_user_is_open) = 1;
51 RETVAL_BOOL(SUCCESS == PS(default_mod)->s_open(&PS(mod_data), save_path, session_name TSRMLS_CC));
52 }
53 /* }}} */
54
55 /* {{{ proto bool SessionHandler::close()
56 Wraps the old close handler */
PHP_METHOD(SessionHandler,close)57 PHP_METHOD(SessionHandler, close)
58 {
59 PS_SANITY_CHECK_IS_OPEN;
60
61 // don't return on failure, since not closing the default handler
62 // could result in memory leaks or other nasties
63 zend_parse_parameters_none();
64
65 PS(mod_user_is_open) = 0;
66 RETVAL_BOOL(SUCCESS == PS(default_mod)->s_close(&PS(mod_data) TSRMLS_CC));
67 }
68 /* }}} */
69
70 /* {{{ proto bool SessionHandler::read(string id)
71 Wraps the old read handler */
PHP_METHOD(SessionHandler,read)72 PHP_METHOD(SessionHandler, read)
73 {
74 char *key, *val;
75 int key_len, val_len;
76
77 PS_SANITY_CHECK_IS_OPEN;
78
79 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
80 return;
81 }
82
83 if (PS(default_mod)->s_read(&PS(mod_data), key, &val, &val_len TSRMLS_CC) == FAILURE) {
84 RETVAL_FALSE;
85 return;
86 }
87
88 RETVAL_STRINGL(val, val_len, 1);
89 str_efree(val);
90 return;
91 }
92 /* }}} */
93
94 /* {{{ proto bool SessionHandler::write(string id, string data)
95 Wraps the old write handler */
PHP_METHOD(SessionHandler,write)96 PHP_METHOD(SessionHandler, write)
97 {
98 char *key, *val;
99 int key_len, val_len;
100
101 PS_SANITY_CHECK_IS_OPEN;
102
103 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, &val, &val_len) == FAILURE) {
104 return;
105 }
106
107 RETVAL_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, val_len TSRMLS_CC));
108 }
109 /* }}} */
110
111 /* {{{ proto bool SessionHandler::destroy(string id)
112 Wraps the old destroy handler */
PHP_METHOD(SessionHandler,destroy)113 PHP_METHOD(SessionHandler, destroy)
114 {
115 char *key;
116 int key_len;
117
118 PS_SANITY_CHECK_IS_OPEN;
119
120 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE) {
121 return;
122 }
123
124 RETVAL_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key TSRMLS_CC));
125 }
126 /* }}} */
127
128 /* {{{ proto bool SessionHandler::gc(int maxlifetime)
129 Wraps the old gc handler */
PHP_METHOD(SessionHandler,gc)130 PHP_METHOD(SessionHandler, gc)
131 {
132 long maxlifetime;
133 int nrdels;
134
135 PS_SANITY_CHECK_IS_OPEN;
136
137 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &maxlifetime) == FAILURE) {
138 return;
139 }
140
141 RETVAL_BOOL(SUCCESS == PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels TSRMLS_CC));
142 }
143 /* }}} */
144
145 /* {{{ proto char SessionHandler::create_sid()
146 Wraps the old create_sid handler */
PHP_METHOD(SessionHandler,create_sid)147 PHP_METHOD(SessionHandler, create_sid)
148 {
149 char *id;
150
151 PS_SANITY_CHECK;
152
153 if (zend_parse_parameters_none() == FAILURE) {
154 return;
155 }
156
157 id = PS(default_mod)->s_create_sid(&PS(mod_data), NULL TSRMLS_CC);
158
159 RETURN_STRING(id, 0);
160 }
161 /* }}} */
162