PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/includes/sessions.php

https://bitbucket.org/publicwhip/publicwhip-v1
PHP | 663 lines | 442 code | 94 blank | 127 comment | 94 complexity | affa500c54d1710b2a8d353c6988e923 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause
  1. <?php
  2. /***************************************************************************
  3. * sessions.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : support@phpbb.com
  8. *
  9. * $Id: sessions.php,v 1.7 2007/06/05 00:28:37 frabcus Exp $
  10. *
  11. *
  12. ***************************************************************************/
  13. /***************************************************************************
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. ***************************************************************************/
  21. // Added by FAI
  22. $thepath = $phpbb_root_path . "../";
  23. require_once "$thepath/common.inc";
  24. require_once "$thepath/config.php";
  25. require_once "$thepath/account/user.inc";
  26. require_once "$thepath/database.inc";
  27. // End added by FAI
  28. //
  29. // Adds/updates a new session to the database for the given userid.
  30. // Returns the new session ID on success.
  31. //
  32. function session_begin($user_id, $user_ip, $page_id, $auto_create = 0, $enable_autologin = 0, $admin = 0)
  33. {
  34. global $db, $board_config;
  35. global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  36. $cookiename = $board_config['cookie_name'];
  37. $cookiepath = $board_config['cookie_path'];
  38. $cookiedomain = $board_config['cookie_domain'];
  39. $cookiesecure = $board_config['cookie_secure'];
  40. if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
  41. {
  42. $session_id = isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
  43. $sessiondata = isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
  44. $sessionmethod = SESSION_METHOD_COOKIE;
  45. }
  46. else
  47. {
  48. $sessiondata = array();
  49. $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
  50. $sessionmethod = SESSION_METHOD_GET;
  51. }
  52. //
  53. if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
  54. {
  55. $session_id = '';
  56. }
  57. $page_id = (int) $page_id;
  58. $last_visit = 0;
  59. $current_time = time();
  60. //
  61. // Are auto-logins allowed?
  62. // If allow_autologin is not set or is true then they are
  63. // (same behaviour as old 2.0.x session code)
  64. //
  65. if (isset($board_config['allow_autologin']) && !$board_config['allow_autologin'])
  66. {
  67. $enable_autologin = $sessiondata['autologinid'] = false;
  68. }
  69. //
  70. // First off attempt to join with the autologin value if we have one
  71. // If not, just use the user_id value
  72. //
  73. $userdata = array();
  74. if ($user_id != ANONYMOUS)
  75. {
  76. if (isset($sessiondata['autologinid']) && (string) $sessiondata['autologinid'] != '' && $user_id)
  77. {
  78. $sql = 'SELECT u.*
  79. FROM ' . USERS_TABLE . ' u, ' . SESSIONS_KEYS_TABLE . ' k
  80. WHERE u.user_id = ' . (int) $user_id . "
  81. AND u.user_active = 1
  82. AND k.user_id = u.user_id
  83. AND k.key_id = '" . md5($sessiondata['autologinid']) . "'";
  84. if (!($result = $db->sql_query($sql)))
  85. {
  86. message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
  87. }
  88. $userdata = $db->sql_fetchrow($result);
  89. $db->sql_freeresult($result);
  90. $enable_autologin = $login = 1;
  91. }
  92. else if (!$auto_create)
  93. {
  94. $sessiondata['autologinid'] = '';
  95. $sessiondata['userid'] = $user_id;
  96. $sql = 'SELECT *
  97. FROM ' . USERS_TABLE . '
  98. WHERE user_id = ' . (int) $user_id . '
  99. AND user_active = 1';
  100. if (!($result = $db->sql_query($sql)))
  101. {
  102. message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
  103. }
  104. $userdata = $db->sql_fetchrow($result);
  105. $db->sql_freeresult($result);
  106. $login = 1;
  107. }
  108. }
  109. //
  110. // At this point either $userdata should be populated or
  111. // one of the below is true
  112. // * Key didn't match one in the DB
  113. // * User does not exist
  114. // * User is inactive
  115. //
  116. if (!sizeof($userdata) || !is_array($userdata) || !$userdata)
  117. {
  118. $sessiondata['autologinid'] = '';
  119. $sessiondata['userid'] = $user_id = ANONYMOUS;
  120. $enable_autologin = $login = 0;
  121. $sql = 'SELECT *
  122. FROM ' . USERS_TABLE . '
  123. WHERE user_id = ' . (int) $user_id;
  124. if (!($result = $db->sql_query($sql)))
  125. {
  126. message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
  127. }
  128. $userdata = $db->sql_fetchrow($result);
  129. $db->sql_freeresult($result);
  130. }
  131. //
  132. // Initial ban check against user id, IP and email address
  133. //
  134. preg_match('/(..)(..)(..)(..)/', $user_ip, $user_ip_parts);
  135. $sql = "SELECT ban_ip, ban_userid, ban_email
  136. FROM " . BANLIST_TABLE . "
  137. WHERE ban_ip IN ('" . $user_ip_parts[1] . $user_ip_parts[2] . $user_ip_parts[3] . $user_ip_parts[4] . "', '" . $user_ip_parts[1] . $user_ip_parts[2] . $user_ip_parts[3] . "ff', '" . $user_ip_parts[1] . $user_ip_parts[2] . "ffff', '" . $user_ip_parts[1] . "ffffff')
  138. OR ban_userid = $user_id";
  139. if ( $user_id != ANONYMOUS )
  140. {
  141. $sql .= " OR ban_email LIKE '" . str_replace("\'", "''", $userdata['user_email']) . "'
  142. OR ban_email LIKE '" . substr(str_replace("\'", "''", $userdata['user_email']), strpos(str_replace("\'", "''", $userdata['user_email']), "@")) . "'";
  143. }
  144. if ( !($result = $db->sql_query($sql)) )
  145. {
  146. message_die(CRITICAL_ERROR, 'Could not obtain ban information', '', __LINE__, __FILE__, $sql);
  147. }
  148. if ( $ban_info = $db->sql_fetchrow($result) )
  149. {
  150. if ( $ban_info['ban_ip'] || $ban_info['ban_userid'] || $ban_info['ban_email'] )
  151. {
  152. message_die(CRITICAL_MESSAGE, 'You_been_banned');
  153. }
  154. }
  155. //
  156. // Create or update the session
  157. //
  158. $sql = "UPDATE " . SESSIONS_TABLE . "
  159. SET session_user_id = $user_id, session_start = $current_time, session_time = $current_time, session_page = $page_id, session_logged_in = $login, session_admin = $admin
  160. WHERE session_id = '" . $session_id . "'
  161. AND session_ip = '$user_ip'";
  162. if ( !$db->sql_query($sql) || !$db->sql_affectedrows() )
  163. {
  164. $session_id = md5(dss_rand());
  165. $sql = "INSERT INTO " . SESSIONS_TABLE . "
  166. (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_admin)
  167. VALUES ('$session_id', $user_id, $current_time, $current_time, '$user_ip', $page_id, $login, $admin)";
  168. if ( !$db->sql_query($sql) )
  169. {
  170. message_die(CRITICAL_ERROR, 'Error creating new session', '', __LINE__, __FILE__, $sql);
  171. }
  172. }
  173. if ( $user_id != ANONYMOUS )
  174. {
  175. $last_visit = ( $userdata['user_session_time'] > 0 ) ? $userdata['user_session_time'] : $current_time;
  176. if (!$admin)
  177. {
  178. $sql = "UPDATE " . USERS_TABLE . "
  179. SET user_session_time = $current_time, user_session_page = $page_id, user_lastvisit = $last_visit
  180. WHERE user_id = $user_id";
  181. if ( !$db->sql_query($sql) )
  182. {
  183. message_die(CRITICAL_ERROR, 'Error updating last visit time', '', __LINE__, __FILE__, $sql);
  184. }
  185. }
  186. $userdata['user_lastvisit'] = $last_visit;
  187. //
  188. // Regenerate the auto-login key
  189. //
  190. if ($enable_autologin)
  191. {
  192. $auto_login_key = dss_rand() . dss_rand();
  193. if (isset($sessiondata['autologinid']) && (string) $sessiondata['autologinid'] != '')
  194. {
  195. $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . "
  196. SET last_ip = '$user_ip', key_id = '" . md5($auto_login_key) . "', last_login = $current_time
  197. WHERE key_id = '" . md5($sessiondata['autologinid']) . "'";
  198. }
  199. else
  200. {
  201. $sql = 'INSERT INTO ' . SESSIONS_KEYS_TABLE . "(key_id, user_id, last_ip, last_login)
  202. VALUES ('" . md5($auto_login_key) . "', $user_id, '$user_ip', $current_time)";
  203. }
  204. if ( !$db->sql_query($sql) )
  205. {
  206. message_die(CRITICAL_ERROR, 'Error updating session key', '', __LINE__, __FILE__, $sql);
  207. }
  208. $sessiondata['autologinid'] = $auto_login_key;
  209. unset($auto_login_key);
  210. }
  211. else
  212. {
  213. $sessiondata['autologinid'] = '';
  214. }
  215. // $sessiondata['autologinid'] = (!$admin) ? (( $enable_autologin && $sessionmethod == SESSION_METHOD_COOKIE ) ? $auto_login_key : '') : $sessiondata['autologinid'];
  216. $sessiondata['userid'] = $user_id;
  217. }
  218. $userdata['session_id'] = $session_id;
  219. $userdata['session_ip'] = $user_ip;
  220. $userdata['session_user_id'] = $user_id;
  221. $userdata['session_logged_in'] = $login;
  222. $userdata['session_page'] = $page_id;
  223. $userdata['session_start'] = $current_time;
  224. $userdata['session_time'] = $current_time;
  225. $userdata['session_admin'] = $admin;
  226. $userdata['session_key'] = $sessiondata['autologinid'];
  227. setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  228. setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
  229. $SID = 'sid=' . $session_id;
  230. return $userdata;
  231. }
  232. //
  233. // Checks for a given user session, tidies session table and updates user
  234. // sessions at each page refresh
  235. //
  236. function session_pagestart($user_ip, $thispage_id)
  237. {
  238. global $db, $lang, $board_config;
  239. global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  240. $cookiename = $board_config['cookie_name'];
  241. $cookiepath = $board_config['cookie_path'];
  242. $cookiedomain = $board_config['cookie_domain'];
  243. $cookiesecure = $board_config['cookie_secure'];
  244. $current_time = time();
  245. unset($userdata);
  246. if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) )
  247. {
  248. $sessiondata = isset( $HTTP_COOKIE_VARS[$cookiename . '_data'] ) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array();
  249. $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : '';
  250. $sessionmethod = SESSION_METHOD_COOKIE;
  251. }
  252. else
  253. {
  254. $sessiondata = array();
  255. $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : '';
  256. $sessionmethod = SESSION_METHOD_GET;
  257. }
  258. //
  259. if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
  260. {
  261. $session_id = '';
  262. }
  263. $thispage_id = (int) $thispage_id;
  264. //
  265. // Does a session exist?
  266. //
  267. if ( !empty($session_id) )
  268. {
  269. //
  270. // session_id exists so go ahead and attempt to grab all
  271. // data in preparation
  272. //
  273. $sql = "SELECT u.*, s.*
  274. FROM " . SESSIONS_TABLE . " s, " . USERS_TABLE . " u
  275. WHERE s.session_id = '$session_id'
  276. AND u.user_id = s.session_user_id";
  277. if ( !($result = $db->sql_query($sql)) )
  278. {
  279. message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql);
  280. }
  281. $userdata = $db->sql_fetchrow($result);
  282. //
  283. // Did the session exist in the DB?
  284. //
  285. if ( isset($userdata['user_id']) )
  286. {
  287. // Added by FAI
  288. // Find Public Whip user id
  289. $logged_into_pw = user_isloggedin();
  290. global $user_name;
  291. // Look it up in PHPBB user account list
  292. $result = $db->sql_query("select user_id from phpbb_users where username = '"
  293. . mysql_escape_string($user_name) . "'");
  294. if ($result) {
  295. $row = $db->sql_fetchrow($result);
  296. $user_id_for_phpbb = $row['user_id'];
  297. }
  298. $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&amp;', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : str_replace("/forum/", "", $_SERVER['REQUEST_URI']);
  299. if ($logged_into_pw) {
  300. // If user id is wrong, log into it
  301. if ($user_id_for_phpbb && $user_id_for_phpbb != $userdata['user_id']) {
  302. if ($userdata['user_id'] > 0) {
  303. session_end($userdata['session_id'], $userdata['user_id']);
  304. redirect(append_sid($url, true));
  305. }
  306. $session_id = session_begin($user_id_for_phpbb, $user_ip, PAGE_INDEX, FALSE, TRUE);
  307. redirect(append_sid($url, true));
  308. exit;
  309. }
  310. // Otherwise make new account
  311. if (!$user_id_for_phpbb) {
  312. // Log out first if logged in
  313. if ($userdata['user_id'] > 0) {
  314. session_end($userdata['session_id'], $userdata['user_id']);
  315. redirect(append_sid($url, true));
  316. exit;
  317. }
  318. $sql = "SELECT MAX(user_id) AS total FROM " . USERS_TABLE;
  319. if ( !($result = $db->sql_query($sql)) ) {
  320. message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
  321. }
  322. if ( !($row = $db->sql_fetchrow($result)) ) {
  323. message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
  324. }
  325. $user_id = $row['total'] + 1;
  326. $notifyreply = 1;
  327. $sql = "INSERT INTO " . USERS_TABLE . "
  328. VALUES ('" . mysql_escape_string($user_id) . "',1,'" . mysql_escape_string($user_name) . "','NEVER***',0,0,0," . time() . ",0,0,0.00,1,'english','D M d, Y g:i a',0,0,0,NULL,0,1,1,1,1,1,1,1,1,1,1,0,'',0,'" . mysql_escape_string(user_getemail()). "','','','','','','','','','','','',NULL,0,0)";
  329. /* VALUES ($user_id, '" . str_replace("\'", "''", $user_name) . "', " . time() . ", '" . str_replace("\'", "''", "NOT VALID ***") . "', '" . str_replace("\'", "''", user_getemail()) . "', '" . str_replace("\'", "''", $icq) . "', '" . str_replace("\'", "''", $website) . "', '" . str_replace("\'", "''", $occupation) . "', '" . str_replace("\'", "''", $location) . "', '" . str_replace("\'", "''", $interests) . "', '" . str_replace("\'", "''", $signature) . "', '$signature_bbcode_uid', $avatar_sql, $viewemail, '" . str_replace("\'", "''", str_replace(' ', '+', $aim)) . "', '" . str_replace("\'", "''", $yim) . "', '" . str_replace("\'", "''", $msn) . "', $attachsig, $allowsmilies, $allowhtml, $allowbbcode, $allowviewonline, $notifyreply, $notifypm, $popup_pm, $user_timezone, '" . str_replace("\'", "''", $user_dateformat) . "', '" . str_replace("\'", "''", $user_lang) . "', $user_style, 0, 1, ";
  330. */
  331. if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
  332. {
  333. message_die(GENERAL_ERROR, 'Could not insert data into users table', '', __LINE__, __FILE__, $sql);
  334. }
  335. $sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user, group_moderator)
  336. VALUES ('', 'Personal User', 1, 0)";
  337. if ( !($result = $db->sql_query($sql)) ) {
  338. message_die(GENERAL_ERROR, 'Could not insert data into groups table', '', __LINE__, __FILE__, $sql);
  339. }
  340. $group_id = $db->sql_nextid();
  341. $sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
  342. VALUES ($user_id, $group_id, 0)";
  343. if( !($result = $db->sql_query($sql, END_TRANSACTION)) ) {
  344. message_die(GENERAL_ERROR, 'Could not insert data into user_group table', '', __LINE__, __FILE__, $sql);
  345. }
  346. $session_id = session_begin($user_id, $user_ip, PAGE_INDEX, FALSE, TRUE);
  347. redirect(append_sid($url, true));
  348. exit;
  349. }
  350. } else {
  351. if ($userdata['user_id'] > 0) {
  352. session_end($userdata['session_id'], $userdata['user_id']);
  353. redirect(append_sid($url, true));
  354. exit;
  355. }
  356. }
  357. // End added by FAI
  358. //
  359. // Do not check IP assuming equivalence, if IPv4 we'll check only first 24
  360. // bits ... I've been told (by vHiker) this should alleviate problems with
  361. // load balanced et al proxies while retaining some reliance on IP security.
  362. //
  363. $ip_check_s = substr($userdata['session_ip'], 0, 6);
  364. $ip_check_u = substr($user_ip, 0, 6);
  365. if ($ip_check_s == $ip_check_u)
  366. {
  367. $SID = ($sessionmethod == SESSION_METHOD_GET || defined('IN_ADMIN')) ? 'sid=' . $session_id : '';
  368. //
  369. // Only update session DB a minute or so after last update
  370. //
  371. if ( $current_time - $userdata['session_time'] > 60 )
  372. {
  373. // A little trick to reset session_admin on session re-usage
  374. $update_admin = (!defined('IN_ADMIN') && $current_time - $userdata['session_time'] > ($board_config['session_length']+60)) ? ', session_admin = 0' : '';
  375. $sql = "UPDATE " . SESSIONS_TABLE . "
  376. SET session_time = $current_time, session_page = $thispage_id$update_admin
  377. WHERE session_id = '" . $userdata['session_id'] . "'";
  378. if ( !$db->sql_query($sql) )
  379. {
  380. message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
  381. }
  382. if ( $userdata['user_id'] != ANONYMOUS )
  383. {
  384. $sql = "UPDATE " . USERS_TABLE . "
  385. SET user_session_time = $current_time, user_session_page = $thispage_id
  386. WHERE user_id = " . $userdata['user_id'];
  387. if ( !$db->sql_query($sql) )
  388. {
  389. message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql);
  390. }
  391. }
  392. session_clean($userdata['session_id']);
  393. setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  394. setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure);
  395. }
  396. // Add the session_key to the userdata array if it is set
  397. if ( isset($sessiondata['autologinid']) && $sessiondata['autologinid'] != '' )
  398. {
  399. $userdata['session_key'] = $sessiondata['autologinid'];
  400. }
  401. return $userdata;
  402. }
  403. }
  404. }
  405. //
  406. // If we reach here then no (valid) session exists. So we'll create a new one,
  407. // using the cookie user_id if available to pull basic user prefs.
  408. //
  409. $user_id = ( isset($sessiondata['userid']) ) ? intval($sessiondata['userid']) : ANONYMOUS;
  410. if ( !($userdata = session_begin($user_id, $user_ip, $thispage_id, TRUE)) )
  411. {
  412. message_die(CRITICAL_ERROR, 'Error creating user session', '', __LINE__, __FILE__, $sql);
  413. }
  414. return $userdata;
  415. }
  416. /**
  417. * Terminates the specified session
  418. * It will delete the entry in the sessions table for this session,
  419. * remove the corresponding auto-login key and reset the cookies
  420. */
  421. function session_end($session_id, $user_id)
  422. {
  423. global $db, $lang, $board_config, $userdata;
  424. global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID;
  425. $cookiename = $board_config['cookie_name'];
  426. $cookiepath = $board_config['cookie_path'];
  427. $cookiedomain = $board_config['cookie_domain'];
  428. $cookiesecure = $board_config['cookie_secure'];
  429. $current_time = time();
  430. if (!preg_match('/^[A-Za-z0-9]*$/', $session_id))
  431. {
  432. return;
  433. }
  434. //
  435. // Delete existing session
  436. //
  437. $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
  438. WHERE session_id = '$session_id'
  439. AND session_user_id = $user_id";
  440. if ( !$db->sql_query($sql) )
  441. {
  442. message_die(CRITICAL_ERROR, 'Error removing user session', '', __LINE__, __FILE__, $sql);
  443. }
  444. //
  445. // Remove this auto-login entry (if applicable)
  446. //
  447. if ( isset($userdata['session_key']) && $userdata['session_key'] != '' )
  448. {
  449. $autologin_key = md5($userdata['session_key']);
  450. $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
  451. WHERE user_id = ' . (int) $user_id . "
  452. AND key_id = '$autologin_key'";
  453. if ( !$db->sql_query($sql) )
  454. {
  455. message_die(CRITICAL_ERROR, 'Error removing auto-login key', '', __LINE__, __FILE__, $sql);
  456. }
  457. }
  458. //
  459. // We expect that message_die will be called after this function,
  460. // but just in case it isn't, reset $userdata to the details for a guest
  461. //
  462. $sql = 'SELECT *
  463. FROM ' . USERS_TABLE . '
  464. WHERE user_id = ' . ANONYMOUS;
  465. if ( !($result = $db->sql_query($sql)) )
  466. {
  467. message_die(CRITICAL_ERROR, 'Error obtaining user details', '', __LINE__, __FILE__, $sql);
  468. }
  469. if ( !($userdata = $db->sql_fetchrow($result)) )
  470. {
  471. message_die(CRITICAL_ERROR, 'Error obtaining user details', '', __LINE__, __FILE__, $sql);
  472. }
  473. $db->sql_freeresult($result);
  474. setcookie($cookiename . '_data', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  475. setcookie($cookiename . '_sid', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  476. return true;
  477. }
  478. /**
  479. * Removes expired sessions and auto-login keys from the database
  480. */
  481. function session_clean($session_id)
  482. {
  483. global $board_config, $db;
  484. //
  485. // Delete expired sessions
  486. //
  487. $sql = 'DELETE FROM ' . SESSIONS_TABLE . '
  488. WHERE session_time < ' . (time() - (int) $board_config['session_length']) . "
  489. AND session_id <> '$session_id'";
  490. if ( !$db->sql_query($sql) )
  491. {
  492. message_die(CRITICAL_ERROR, 'Error clearing sessions table', '', __LINE__, __FILE__, $sql);
  493. }
  494. //
  495. // Delete expired auto-login keys
  496. // If max_autologin_time is not set then keys will never be deleted
  497. // (same behaviour as old 2.0.x session code)
  498. //
  499. if (!empty($board_config['max_autologin_time']) && $board_config['max_autologin_time'] > 0)
  500. {
  501. $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
  502. WHERE last_login < ' . (time() - (86400 * (int) $board_config['max_autologin_time']));
  503. $db->sql_query($sql);
  504. }
  505. return true;
  506. }
  507. /**
  508. * Reset all login keys for the specified user
  509. * Called on password changes
  510. */
  511. function session_reset_keys($user_id, $user_ip)
  512. {
  513. global $db, $userdata, $board_config;
  514. $key_sql = ($user_id == $userdata['user_id'] && !empty($userdata['session_key'])) ? "AND key_id != '" . md5($userdata['session_key']) . "'" : '';
  515. $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . '
  516. WHERE user_id = ' . (int) $user_id . "
  517. $key_sql";
  518. if ( !$db->sql_query($sql) )
  519. {
  520. message_die(CRITICAL_ERROR, 'Error removing auto-login keys', '', __LINE__, __FILE__, $sql);
  521. }
  522. $where_sql = 'session_user_id = ' . (int) $user_id;
  523. $where_sql .= ($user_id == $userdata['user_id']) ? " AND session_id <> '" . $userdata['session_id'] . "'" : '';
  524. $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
  525. WHERE $where_sql";
  526. if ( !$db->sql_query($sql) )
  527. {
  528. message_die(CRITICAL_ERROR, 'Error removing user session(s)', '', __LINE__, __FILE__, $sql);
  529. }
  530. if ( !empty($key_sql) )
  531. {
  532. $auto_login_key = dss_rand() . dss_rand();
  533. $current_time = time();
  534. $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . "
  535. SET last_ip = '$user_ip', key_id = '" . md5($auto_login_key) . "', last_login = $current_time
  536. WHERE key_id = '" . md5($userdata['session_key']) . "'";
  537. if ( !$db->sql_query($sql) )
  538. {
  539. message_die(CRITICAL_ERROR, 'Error updating session key', '', __LINE__, __FILE__, $sql);
  540. }
  541. // And now rebuild the cookie
  542. $sessiondata['userid'] = $user_id;
  543. $sessiondata['autologinid'] = $auto_login_key;
  544. $cookiename = $board_config['cookie_name'];
  545. $cookiepath = $board_config['cookie_path'];
  546. $cookiedomain = $board_config['cookie_domain'];
  547. $cookiesecure = $board_config['cookie_secure'];
  548. setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure);
  549. $userdata['session_key'] = $auto_login_key;
  550. unset($sessiondata);
  551. unset($auto_login_key);
  552. }
  553. }
  554. //
  555. // Append $SID to a url. Borrowed from phplib and modified. This is an
  556. // extra routine utilised by the session code above and acts as a wrapper
  557. // around every single URL and form action. If you replace the session
  558. // code you must include this routine, even if it's empty.
  559. //
  560. function append_sid($url, $non_html_amp = false)
  561. {
  562. global $SID;
  563. if ( !empty($SID) && !preg_match('#sid=#', $url) )
  564. {
  565. $url .= ( ( strpos($url, '?') !== false ) ? ( ( $non_html_amp ) ? '&' : '&amp;' ) : '?' ) . $SID;
  566. }
  567. return $url;
  568. }
  569. ?>