xref: /PHP-7.0/ext/session/mod_user_class.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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, 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, 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 	size_t save_path_len, session_name_len;
43 
44 	PS_SANITY_CHECK;
45 
46 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "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));
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)));
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 	zend_string *val;
75 	zend_string *key;
76 
77 	PS_SANITY_CHECK_IS_OPEN;
78 
79 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
80 		return;
81 	}
82 
83 	if (PS(default_mod)->s_read(&PS(mod_data), key, &val, PS(gc_maxlifetime)) == FAILURE) {
84 		RETURN_FALSE;
85 	}
86 
87 	RETURN_STR(val);
88 }
89 /* }}} */
90 
91 /* {{{ proto bool SessionHandler::write(string id, string data)
92    Wraps the old write handler */
PHP_METHOD(SessionHandler,write)93 PHP_METHOD(SessionHandler, write)
94 {
95 	zend_string *key, *val;
96 
97 	PS_SANITY_CHECK_IS_OPEN;
98 
99 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
100 		return;
101 	}
102 
103 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
104 }
105 /* }}} */
106 
107 /* {{{ proto bool SessionHandler::destroy(string id)
108    Wraps the old destroy handler */
PHP_METHOD(SessionHandler,destroy)109 PHP_METHOD(SessionHandler, destroy)
110 {
111 	zend_string *key;
112 
113 	PS_SANITY_CHECK_IS_OPEN;
114 
115 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
116 		return;
117 	}
118 
119 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_destroy(&PS(mod_data), key));
120 }
121 /* }}} */
122 
123 /* {{{ proto bool SessionHandler::gc(int maxlifetime)
124    Wraps the old gc handler */
PHP_METHOD(SessionHandler,gc)125 PHP_METHOD(SessionHandler, gc)
126 {
127 	zend_long maxlifetime;
128 	int nrdels;
129 
130 	PS_SANITY_CHECK_IS_OPEN;
131 
132 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
133 		return;
134 	}
135 
136 	RETURN_BOOL(SUCCESS == PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels));
137 }
138 /* }}} */
139 
140 /* {{{ proto char SessionHandler::create_sid()
141    Wraps the old create_sid handler */
PHP_METHOD(SessionHandler,create_sid)142 PHP_METHOD(SessionHandler, create_sid)
143 {
144 	zend_string *id;
145 
146 	PS_SANITY_CHECK;
147 
148 	if (zend_parse_parameters_none() == FAILURE) {
149 	    return;
150 	}
151 
152 	id = PS(default_mod)->s_create_sid(&PS(mod_data));
153 
154 	RETURN_STR(id);
155 }
156 /* }}} */
157 
158 /* {{{ proto char SessionUpdateTimestampHandler::validateId(string id)
159    Simply return TRUE */
PHP_METHOD(SessionHandler,validateId)160 PHP_METHOD(SessionHandler, validateId)
161 {
162 	zend_string *key;
163 
164 	PS_SANITY_CHECK_IS_OPEN;
165 
166 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &key) == FAILURE) {
167 		return;
168 	}
169 
170 	/* Legacy save handler may not support validate_sid API. Return TRUE. */
171 	RETURN_TRUE;
172 }
173 /* }}} */
174 
175 /* {{{ proto bool SessionUpdateTimestampHandler::updateTimestamp(string id, string data)
176    Simply call update_timestamp */
PHP_METHOD(SessionHandler,updateTimestamp)177 PHP_METHOD(SessionHandler, updateTimestamp)
178 {
179 	zend_string *key, *val;
180 
181 	PS_SANITY_CHECK_IS_OPEN;
182 
183 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
184 		return;
185 	}
186 
187 	/* Legacy save handler may not support update_timestamp API. Just write. */
188 	RETVAL_BOOL(SUCCESS == PS(default_mod)->s_write(&PS(mod_data), key, val, PS(gc_maxlifetime)));
189 }
190 /* }}} */
191