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 2022 Simple Machines and individual contributors |
||||
15 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||||
16 | * |
||||
17 | * @version 2.1.0 |
||||
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); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
32 | @ini_set('session.use_only_cookies', false); |
||||
0 ignored issues
–
show
false of type false is incompatible with the type string expected by parameter $value of ini_set() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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_iri($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 | // If an insert fails due to a dupe, replace the existing session... |
||||
183 | $session_update = $smcFunc['db_insert']('replace', |
||||
0 ignored issues
–
show
|
|||||
184 | '{db_prefix}sessions', |
||||
185 | array('session_id' => 'string', 'data' => 'string', 'last_update' => 'int'), |
||||
186 | array($session_id, $data, time()), |
||||
187 | array('session_id') |
||||
188 | ); |
||||
189 | |||||
190 | return ($smcFunc['db_affected_rows']() == 0 ? false : true); |
||||
191 | } |
||||
192 | |||||
193 | /** |
||||
194 | * Implementation of sessionDestroy() replacing the standard destroy handler. |
||||
195 | * |
||||
196 | * @param string $session_id The session ID |
||||
197 | * @return boolean Whether the session was successfully destroyed |
||||
198 | */ |
||||
199 | function sessionDestroy($session_id) |
||||
200 | { |
||||
201 | global $smcFunc; |
||||
202 | |||||
203 | if (preg_match('~^[A-Za-z0-9,-]{16,64}$~', $session_id) == 0) |
||||
204 | return false; |
||||
205 | |||||
206 | // Just delete the row... |
||||
207 | $smcFunc['db_query']('', ' |
||||
208 | DELETE FROM {db_prefix}sessions |
||||
209 | WHERE session_id = {string:session_id}', |
||||
210 | array( |
||||
211 | 'session_id' => $session_id, |
||||
212 | ) |
||||
213 | ); |
||||
214 | |||||
215 | return true; |
||||
216 | } |
||||
217 | |||||
218 | /** |
||||
219 | * Implementation of sessionGC() replacing the standard gc handler. |
||||
220 | * Callback for garbage collection. |
||||
221 | * |
||||
222 | * @param int $max_lifetime The maximum lifetime (in seconds) - prevents deleting of sessions older than this |
||||
223 | * @return boolean Whether the option was successful |
||||
224 | */ |
||||
225 | function sessionGC($max_lifetime) |
||||
226 | { |
||||
227 | global $modSettings, $smcFunc; |
||||
228 | |||||
229 | // Just set to the default or lower? Ignore it for a higher value. (hopefully) |
||||
230 | if (!empty($modSettings['databaseSession_lifetime']) && ($max_lifetime <= 1440 || $modSettings['databaseSession_lifetime'] > $max_lifetime)) |
||||
231 | $max_lifetime = max($modSettings['databaseSession_lifetime'], 60); |
||||
232 | |||||
233 | // Clean up after yerself ;). |
||||
234 | $session_update = $smcFunc['db_query']('', ' |
||||
0 ignored issues
–
show
|
|||||
235 | DELETE FROM {db_prefix}sessions |
||||
236 | WHERE last_update < {int:last_update}', |
||||
237 | array( |
||||
238 | 'last_update' => time() - $max_lifetime, |
||||
239 | ) |
||||
240 | ); |
||||
241 | |||||
242 | return ($smcFunc['db_affected_rows']() == 0 ? false : true); |
||||
243 | } |
||||
244 | |||||
245 | ?> |