|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Implementation of PHP's session API. |
|
5
|
|
|
* What it does: |
|
6
|
|
|
* - it handles the session data in the database (more scalable.) |
|
7
|
|
|
* - it uses the databaseSession_lifetime setting for garbage collection. |
|
8
|
|
|
* - the custom session handler is set by loadSession(). |
|
9
|
|
|
* |
|
10
|
|
|
* Simple Machines Forum (SMF) |
|
11
|
|
|
* |
|
12
|
|
|
* @package SMF |
|
13
|
|
|
* @author Simple Machines https://www.simplemachines.org |
|
14
|
|
|
* @copyright 2020 Simple Machines and individual contributors |
|
15
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
|
16
|
|
|
* |
|
17
|
|
|
* @version 2.1 RC3 |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
if (!defined('SMF')) |
|
21
|
|
|
die('No direct access...'); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Attempt to start the session, unless it already has been. |
|
25
|
|
|
*/ |
|
26
|
|
|
function loadSession() |
|
27
|
|
|
{ |
|
28
|
|
|
global $modSettings, $boardurl, $sc, $smcFunc, $cache_enable; |
|
29
|
|
|
|
|
30
|
|
|
// Attempt to change a few PHP settings. |
|
31
|
|
|
@ini_set('session.use_cookies', true); |
|
|
|
|
|
|
32
|
|
|
@ini_set('session.use_only_cookies', false); |
|
|
|
|
|
|
33
|
|
|
@ini_set('url_rewriter.tags', ''); |
|
34
|
|
|
@ini_set('session.use_trans_sid', false); |
|
35
|
|
|
@ini_set('arg_separator.output', '&'); |
|
36
|
|
|
|
|
37
|
|
|
// Allows mods to change/add PHP settings |
|
38
|
|
|
call_integration_hook('integrate_load_session'); |
|
39
|
|
|
|
|
40
|
|
|
if (!empty($modSettings['globalCookies'])) |
|
41
|
|
|
{ |
|
42
|
|
|
$parsed_url = parse_url($boardurl); |
|
43
|
|
|
|
|
44
|
|
|
if (preg_match('~^\d{1,3}(\.\d{1,3}){3}$~', $parsed_url['host']) == 0 && preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $parsed_url['host'], $parts) == 1) |
|
45
|
|
|
@ini_set('session.cookie_domain', '.' . $parts[1]); |
|
46
|
|
|
} |
|
47
|
|
|
// @todo Set the session cookie path? |
|
48
|
|
|
|
|
49
|
|
|
// If it's already been started... probably best to skip this. |
|
50
|
|
|
if ((ini_get('session.auto_start') == 1 && !empty($modSettings['databaseSession_enable'])) || session_id() == '') |
|
51
|
|
|
{ |
|
52
|
|
|
// Attempt to end the already-started session. |
|
53
|
|
|
if (ini_get('session.auto_start') == 1) |
|
54
|
|
|
session_write_close(); |
|
55
|
|
|
|
|
56
|
|
|
// This is here to stop people from using bad junky PHPSESSIDs. |
|
57
|
|
|
if (isset($_REQUEST[session_name()]) && preg_match('~^[A-Za-z0-9,-]{16,64}$~', $_REQUEST[session_name()]) == 0 && !isset($_COOKIE[session_name()])) |
|
58
|
|
|
{ |
|
59
|
|
|
$session_id = md5(md5('smf_sess_' . time()) . $smcFunc['random_int']()); |
|
60
|
|
|
$_REQUEST[session_name()] = $session_id; |
|
61
|
|
|
$_GET[session_name()] = $session_id; |
|
62
|
|
|
$_POST[session_name()] = $session_id; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Use database sessions? (they don't work in 4.1.x!) |
|
66
|
|
|
if (!empty($modSettings['databaseSession_enable'])) |
|
67
|
|
|
{ |
|
68
|
|
|
@ini_set('session.serialize_handler', 'php_serialize'); |
|
69
|
|
|
if (ini_get('session.serialize_handler') != 'php_serialize') |
|
70
|
|
|
@ini_set('session.serialize_handler', 'php'); |
|
71
|
|
|
session_set_save_handler('sessionOpen', 'sessionClose', 'sessionRead', 'sessionWrite', 'sessionDestroy', 'sessionGC'); |
|
72
|
|
|
@ini_set('session.gc_probability', '1'); |
|
73
|
|
|
} |
|
74
|
|
|
elseif (ini_get('session.gc_maxlifetime') <= 1440 && !empty($modSettings['databaseSession_lifetime'])) |
|
75
|
|
|
@ini_set('session.gc_maxlifetime', max($modSettings['databaseSession_lifetime'], 60)); |
|
76
|
|
|
|
|
77
|
|
|
// Use cache setting sessions? |
|
78
|
|
|
if (empty($modSettings['databaseSession_enable']) && !empty($cache_enable) && php_sapi_name() != 'cli') |
|
79
|
|
|
call_integration_hook('integrate_session_handlers'); |
|
80
|
|
|
|
|
81
|
|
|
session_start(); |
|
82
|
|
|
|
|
83
|
|
|
// Change it so the cache settings are a little looser than default. |
|
84
|
|
|
if (!empty($modSettings['databaseSession_loose'])) |
|
85
|
|
|
header('cache-control: private'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Set the randomly generated code. |
|
89
|
|
|
if (!isset($_SESSION['session_var'])) |
|
90
|
|
|
{ |
|
91
|
|
|
$_SESSION['session_value'] = md5(session_id() . $smcFunc['random_int']()); |
|
92
|
|
|
$_SESSION['session_var'] = substr(preg_replace('~^\d+~', '', sha1($smcFunc['random_int']() . session_id() . $smcFunc['random_int']())), 0, $smcFunc['random_int'](7, 12)); |
|
93
|
|
|
} |
|
94
|
|
|
$sc = $_SESSION['session_value']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Implementation of sessionOpen() replacing the standard open handler. |
|
99
|
|
|
* It simply returns true. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $save_path The path to save the session to |
|
102
|
|
|
* @param string $session_name The name of the session |
|
103
|
|
|
* @return boolean Always returns true |
|
104
|
|
|
*/ |
|
105
|
|
|
function sessionOpen($save_path, $session_name) |
|
106
|
|
|
{ |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Implementation of sessionClose() replacing the standard close handler. |
|
112
|
|
|
* It simply returns true. |
|
113
|
|
|
* |
|
114
|
|
|
* @return boolean Always returns true |
|
115
|
|
|
*/ |
|
116
|
|
|
function sessionClose() |
|
117
|
|
|
{ |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Implementation of sessionRead() replacing the standard read handler. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $session_id The session ID |
|
125
|
|
|
* @return string The session data |
|
126
|
|
|
*/ |
|
127
|
|
|
function sessionRead($session_id) |
|
128
|
|
|
{ |
|
129
|
|
|
global $smcFunc; |
|
130
|
|
|
|
|
131
|
|
|
if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
|
132
|
|
|
return ''; |
|
133
|
|
|
|
|
134
|
|
|
// Look for it in the database. |
|
135
|
|
|
$result = $smcFunc['db_query']('', ' |
|
136
|
|
|
SELECT data |
|
137
|
|
|
FROM {db_prefix}sessions |
|
138
|
|
|
WHERE session_id = {string:session_id} |
|
139
|
|
|
LIMIT 1', |
|
140
|
|
|
array( |
|
141
|
|
|
'session_id' => $session_id, |
|
142
|
|
|
) |
|
143
|
|
|
); |
|
144
|
|
|
list ($sess_data) = $smcFunc['db_fetch_row']($result); |
|
145
|
|
|
$smcFunc['db_free_result']($result); |
|
146
|
|
|
|
|
147
|
|
|
return $sess_data != null ? $sess_data : ''; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Implementation of sessionWrite() replacing the standard write handler. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $session_id The session ID |
|
154
|
|
|
* @param string $data The data to write to the session |
|
155
|
|
|
* @return boolean Whether the info was successfully written |
|
156
|
|
|
*/ |
|
157
|
|
|
function sessionWrite($session_id, $data) |
|
158
|
|
|
{ |
|
159
|
|
|
global $smcFunc, $db_connection, $db_server, $db_name, $db_user, $db_passwd; |
|
160
|
|
|
global $db_prefix, $db_persist, $db_port, $db_mb4; |
|
161
|
|
|
|
|
162
|
|
|
if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
|
163
|
|
|
return false; |
|
164
|
|
|
|
|
165
|
|
|
// php < 7.0 need this |
|
166
|
|
|
if (empty($db_connection)) |
|
167
|
|
|
{ |
|
168
|
|
|
$db_options = array(); |
|
169
|
|
|
|
|
170
|
|
|
// Add in the port if needed |
|
171
|
|
|
if (!empty($db_port)) |
|
172
|
|
|
$db_options['port'] = $db_port; |
|
173
|
|
|
|
|
174
|
|
|
if (!empty($db_mb4)) |
|
175
|
|
|
$db_options['db_mb4'] = $db_mb4; |
|
176
|
|
|
|
|
177
|
|
|
$options = array_merge($db_options, array('persist' => $db_persist, 'dont_select_db' => SMF == 'SSI')); |
|
178
|
|
|
|
|
179
|
|
|
$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// First try to update an existing row... |
|
183
|
|
|
$smcFunc['db_query']('', ' |
|
184
|
|
|
UPDATE {db_prefix}sessions |
|
185
|
|
|
SET data = {string:data}, last_update = {int:last_update} |
|
186
|
|
|
WHERE session_id = {string:session_id}', |
|
187
|
|
|
array( |
|
188
|
|
|
'last_update' => time(), |
|
189
|
|
|
'data' => $data, |
|
190
|
|
|
'session_id' => $session_id, |
|
191
|
|
|
) |
|
192
|
|
|
); |
|
193
|
|
|
|
|
194
|
|
|
// If that didn't work, try inserting a new one. |
|
195
|
|
|
if ($smcFunc['db_affected_rows']() == 0) |
|
196
|
|
|
$smcFunc['db_insert']('ignore', |
|
197
|
|
|
'{db_prefix}sessions', |
|
198
|
|
|
array('session_id' => 'string', 'data' => 'string', 'last_update' => 'int'), |
|
199
|
|
|
array($session_id, $data, time()), |
|
200
|
|
|
array('session_id') |
|
201
|
|
|
); |
|
202
|
|
|
|
|
203
|
|
|
return ($smcFunc['db_affected_rows']() == 0 ? false : true); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Implementation of sessionDestroy() replacing the standard destroy handler. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $session_id The session ID |
|
210
|
|
|
* @return boolean Whether the session was successfully destroyed |
|
211
|
|
|
*/ |
|
212
|
|
|
function sessionDestroy($session_id) |
|
213
|
|
|
{ |
|
214
|
|
|
global $smcFunc; |
|
215
|
|
|
|
|
216
|
|
|
if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
|
217
|
|
|
return false; |
|
218
|
|
|
|
|
219
|
|
|
// Just delete the row... |
|
220
|
|
|
$smcFunc['db_query']('', ' |
|
221
|
|
|
DELETE FROM {db_prefix}sessions |
|
222
|
|
|
WHERE session_id = {string:session_id}', |
|
223
|
|
|
array( |
|
224
|
|
|
'session_id' => $session_id, |
|
225
|
|
|
) |
|
226
|
|
|
); |
|
227
|
|
|
|
|
228
|
|
|
return true; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Implementation of sessionGC() replacing the standard gc handler. |
|
233
|
|
|
* Callback for garbage collection. |
|
234
|
|
|
* |
|
235
|
|
|
* @param int $max_lifetime The maximum lifetime (in seconds) - prevents deleting of sessions older than this |
|
236
|
|
|
* @return boolean Whether the option was successful |
|
237
|
|
|
*/ |
|
238
|
|
|
function sessionGC($max_lifetime) |
|
239
|
|
|
{ |
|
240
|
|
|
global $modSettings, $smcFunc; |
|
241
|
|
|
|
|
242
|
|
|
// Just set to the default or lower? Ignore it for a higher value. (hopefully) |
|
243
|
|
|
if (!empty($modSettings['databaseSession_lifetime']) && ($max_lifetime <= 1440 || $modSettings['databaseSession_lifetime'] > $max_lifetime)) |
|
244
|
|
|
$max_lifetime = max($modSettings['databaseSession_lifetime'], 60); |
|
245
|
|
|
|
|
246
|
|
|
// Clean up after yerself ;). |
|
247
|
|
|
$smcFunc['db_query']('', ' |
|
248
|
|
|
DELETE FROM {db_prefix}sessions |
|
249
|
|
|
WHERE last_update < {int:last_update}', |
|
250
|
|
|
array( |
|
251
|
|
|
'last_update' => time() - $max_lifetime, |
|
252
|
|
|
) |
|
253
|
|
|
); |
|
254
|
|
|
|
|
255
|
|
|
return ($smcFunc['db_affected_rows']() == 0 ? false : true); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
?> |