|
From: Jon O. <jon...@us...> - 2007-09-09 16:51:39
|
Update of /cvsroot/mxbb/core/includes/sessions/internal In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv10517/internal Added Files: constants.php login.php session.php Log Message: Ok, massive update for 2.9.x. --- NEW FILE: login.php --- <?php /** * * @package mxBB Portal Core * @version $Id: login.php,v 1.1 2007/09/09 16:51:31 jonohlsson Exp $ * @copyright (c) 2002-2006 mxBB Project Team * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v2 * @link http://www.mxbb.net * */ if ( !defined('IN_PORTAL') ) { die("Hacking attempt"); } if( ( isset($HTTP_POST_VARS['login']) || isset($HTTP_GET_VARS['login']) ) && (!$userdata['session_logged_in'] || isset($HTTP_POST_VARS['admin'])) ) { $username = isset($HTTP_POST_VARS['username']) ? phpBB2::phpbb_clean_username($HTTP_POST_VARS['username']) : ''; $password = isset($HTTP_POST_VARS['password']) ? $HTTP_POST_VARS['password'] : ''; $sql = "SELECT user_id, username, user_password, user_active, user_level, user_login_tries, user_last_login_try FROM " . USERS_TABLE . " WHERE username = '" . str_replace("\\'", "''", $username) . "'"; if ( !($result = $db->sql_query($sql) ) ) { mx_message_die(GENERAL_ERROR, 'Error in obtaining userdata', '', __LINE__, __FILE__, $sql); } if( $row = $db->sql_fetchrow($result) ) { if( $row['user_level'] != ADMIN && $board_config['board_disable'] ) { mx_redirect(mx_append_sid("index.$phpEx", false)); } else { // If the last login is more than x minutes ago, then reset the login tries/time if ($row['user_last_login_try'] && $board_config['login_reset_time'] && $row['user_last_login_try'] < (time() - ($board_config['login_reset_time'] * 60))) { $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_login_tries = 0, user_last_login_try = 0 WHERE user_id = ' . $row['user_id']); $row['user_last_login_try'] = $row['user_login_tries'] = 0; } // Check to see if user is allowed to login again... if his tries are exceeded if ($row['user_last_login_try'] && $board_config['login_reset_time'] && $board_config['max_login_attempts'] && $row['user_last_login_try'] >= (time() - ($board_config['login_reset_time'] * 60)) && $row['user_login_tries'] >= $board_config['max_login_attempts'] && $userdata['user_level'] != ADMIN) { mx_message_die(GENERAL_MESSAGE, sprintf($lang['Login_attempts_exceeded'], $board_config['max_login_attempts'], $board_config['login_reset_time'])); } if( md5($password) == $row['user_password'] && $row['user_active'] ) { $autologin = ( isset($HTTP_POST_VARS['autologin']) ) ? TRUE : 0; $admin = (isset($HTTP_POST_VARS['admin'])) ? 1 : 0; $session_id = $mx_user->mx_session_create($row['user_id'], $user_ip, PAGE_INDEX, FALSE, $autologin, $admin); // Reset login tries $db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_login_tries = 0, user_last_login_try = 0 WHERE user_id = ' . $row['user_id']); if( $session_id ) { $fromurl = ( !empty($HTTP_REFERER) ) ? str_replace('&', '&', htmlspecialchars($HTTP_REFERER)) : "index.$phpEx"; $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : $fromurl; mx_redirect(mx_append_sid($url, false, false, $session_id)); } else { mx_message_die(CRITICAL_ERROR, "Couldn't start session : login", "", __LINE__, __FILE__); } } // Only store a failed login attempt for an active user - inactive users can't login even with a correct password elseif( $row['user_active'] ) { // Save login tries and last login if ($row['user_id'] != ANONYMOUS) { $sql = 'UPDATE ' . USERS_TABLE . ' SET user_login_tries = user_login_tries + 1, user_last_login_try = ' . time() . ' WHERE user_id = ' . $row['user_id']; $db->sql_query($sql); } $redirect = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : ''; $redirect = str_replace('?', '&', $redirect); if (strstr(urldecode($redirect), "\n") || strstr(urldecode($redirect), "\r")) { mx_message_die(GENERAL_ERROR, 'Tried to redirect to potentially insecure url.'); } $template->assign_vars(array( 'META' => "<meta http-equiv=\"refresh\" content=\"3;url=login.$phpEx?redirect=$redirect\">") ); $message = $lang['Error_login'] . '<br /><br />' . sprintf($lang['Click_return_login'], "<a href=\"login.$phpEx?redirect=$redirect\">", '</a>') . '<br /><br />' . sprintf($lang['Click_return_index'], '<a href="' . mx_append_sid("index.$phpEx") . '">', '</a>'); mx_message_die(GENERAL_MESSAGE, $message); } } } else { $redirect = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : ""; $redirect = str_replace("?", "&", $redirect); if (strstr(urldecode($redirect), "\n") || strstr(urldecode($redirect), "\r")) { mx_message_die(GENERAL_ERROR, 'Tried to redirect to potentially insecure url.'); } $template->assign_vars(array( 'META' => "<meta http-equiv=\"refresh\" content=\"3;url=login.$phpEx?redirect=$redirect\">") ); $message = $lang['Error_login'] . '<br /><br />' . sprintf($lang['Click_return_login'], "<a href=\"login.$phpEx?redirect=$redirect\">", '</a>') . '<br /><br />' . sprintf($lang['Click_return_index'], '<a href="' . mx_append_sid("index.$phpEx") . '">', '</a>'); mx_message_die(GENERAL_MESSAGE, $message); } } else if( ( isset($HTTP_GET_VARS['logout']) || isset($HTTP_POST_VARS['logout']) ) && $userdata['session_logged_in'] ) { // session id check if ($sid == '' || $sid != $userdata['session_id']) { mx_message_die(GENERAL_ERROR, 'Invalid_session'); } if( $userdata['session_logged_in'] ) { $mx_user->mx_session_kill($userdata['session_id'], $userdata['user_id']); } if (!empty($HTTP_POST_VARS['redirect']) || !empty($HTTP_GET_VARS['redirect'])) { $fromurl = ( !empty($HTTP_REFERER) ) ? str_replace('&', '&', htmlspecialchars($HTTP_REFERER)) : "index.$phpEx"; $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : $fromurl; mx_redirect(mx_append_sid($url, false, false, $session_id)); } else { mx_redirect(mx_append_sid("index.$phpEx", false)); } } else { $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'])) : "index.$phpEx"; mx_redirect(mx_append_sid($url, false)); } ?> --- NEW FILE: constants.php --- <?php /*************************************************************************** * constants.php * ------------------- * begin : Saturday', Feb 13', 2001 * copyright : ('C) 2001 The phpBB Group * email : su...@ph... * * $Id: constants.php,v 1.1 2007/09/09 16:51:31 jonohlsson Exp $ * * ***************************************************************************/ /*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License', or * ('at your option) any later version. * ***************************************************************************/ if ( !defined('IN_PHPBB') ) { die("Hacking attempt"); } // Debug Level //define('DEBUG', 1); // Debugging on define('DEBUG', 1); // Debugging off // User Levels <- Do not change the values of USER or ADMIN define('DELETED', -1); define('ANONYMOUS', -1); define('USER', 0); define('ADMIN', 1); define('MOD', 2); // User related define('USER_ACTIVATION_NONE', 0); define('USER_ACTIVATION_SELF', 1); define('USER_ACTIVATION_ADMIN', 2); define('USER_AVATAR_NONE', 0); define('USER_AVATAR_UPLOAD', 1); define('USER_AVATAR_REMOTE', 2); define('USER_AVATAR_GALLERY', 3); // Group settings define('GROUP_OPEN', 0); define('GROUP_CLOSED', 1); define('GROUP_HIDDEN', 2); // Forum state define('FORUM_UNLOCKED', 0); define('FORUM_LOCKED', 1); // Topic status define('TOPIC_UNLOCKED', 0); define('TOPIC_LOCKED', 1); define('TOPIC_MOVED', 2); define('TOPIC_WATCH_NOTIFIED', 1); define('TOPIC_WATCH_UN_NOTIFIED', 0); // Topic types define('POST_NORMAL', 0); define('POST_STICKY', 1); define('POST_ANNOUNCE', 2); define('POST_GLOBAL_ANNOUNCE', 3); // SQL codes define('BEGIN_TRANSACTION', 1); define('END_TRANSACTION', 2); // Error codes define('GENERAL_MESSAGE', 200); define('GENERAL_ERROR', 202); define('CRITICAL_MESSAGE', 203); define('CRITICAL_ERROR', 204); // Private messaging define('PRIVMSGS_READ_MAIL', 0); define('PRIVMSGS_NEW_MAIL', 1); define('PRIVMSGS_SENT_MAIL', 2); define('PRIVMSGS_SAVED_IN_MAIL', 3); define('PRIVMSGS_SAVED_OUT_MAIL', 4); define('PRIVMSGS_UNREAD_MAIL', 5); // URL PARAMETERS define('POST_TOPIC_URL', 't'); define('POST_CAT_URL', 'c'); define('POST_FORUM_URL', 'f'); define('POST_USERS_URL', 'u'); define('POST_POST_URL', 'p'); define('POST_GROUPS_URL', 'g'); // Session parameters define('SESSION_METHOD_COOKIE', 100); define('SESSION_METHOD_GET', 101); // Page numbers for session handling define('PAGE_INDEX', 0); define('PAGE_LOGIN', -1); define('PAGE_SEARCH', -2); define('PAGE_REGISTER', -3); define('PAGE_PROFILE', -4); define('PAGE_VIEWONLINE', -6); define('PAGE_VIEWMEMBERS', -7); define('PAGE_FAQ', -8); define('PAGE_POSTING', -9); define('PAGE_PRIVMSGS', -10); define('PAGE_GROUPCP', -11); define('PAGE_TOPIC_OFFSET', 5000); // Auth settings define('AUTH_LIST_ALL', 0); define('AUTH_ALL', 0); define('AUTH_REG', 1); define('AUTH_ACL', 2); define('AUTH_MOD', 3); define('AUTH_ADMIN', 5); define('AUTH_VIEW', 1); define('AUTH_READ', 2); define('AUTH_POST', 3); define('AUTH_REPLY', 4); define('AUTH_EDIT', 5); define('AUTH_DELETE', 6); define('AUTH_ANNOUNCE', 7); define('AUTH_STICKY', 8); define('AUTH_POLLCREATE', 9); define('AUTH_VOTE', 10); define('AUTH_ATTACH', 11); // Table names //define('CONFIRM_TABLE', $table_prefix.'confirm'); //define('AUTH_ACCESS_TABLE', $table_prefix.'auth_access'); //define('BANLIST_TABLE', $table_prefix.'banlist'); //define('CATEGORIES_TABLE', $table_prefix.'categories'); //define('CONFIG_TABLE', $table_prefix.'config'); //define('DISALLOW_TABLE', $table_prefix.'disallow'); //define('FORUMS_TABLE', $table_prefix.'forums'); define('GROUPS_TABLE', $mx_table_prefix.'groups'); //define('POSTS_TABLE', $table_prefix.'posts'); //define('POSTS_TEXT_TABLE', $table_prefix.'posts_text'); //define('PRIVMSGS_TABLE', $table_prefix.'privmsgs'); //define('PRIVMSGS_TEXT_TABLE', $table_prefix.'privmsgs_text'); //define('PRIVMSGS_IGNORE_TABLE', $table_prefix.'privmsgs_ignore'); //define('PRUNE_TABLE', $table_prefix.'forum_prune'); //define('RANKS_TABLE', $table_prefix.'ranks'); //define('SEARCH_TABLE', $table_prefix.'search_results'); //define('SEARCH_WORD_TABLE', $table_prefix.'search_wordlist'); //define('SEARCH_MATCH_TABLE', $table_prefix.'search_wordmatch'); define('SESSIONS_TABLE', $mx_table_prefix.'sessions'); define('SESSIONS_KEYS_TABLE', $mx_table_prefix.'sessions_keys'); define('SMILIES_TABLE', $table_prefix.'smilies'); //define('THEMES_TABLE', $table_prefix.'themes'); //define('THEMES_NAME_TABLE', $table_prefix.'themes_name'); //define('TOPICS_TABLE', $table_prefix.'topics'); //define('TOPICS_WATCH_TABLE', $table_prefix.'topics_watch'); define('USER_GROUP_TABLE', $mx_table_prefix.'user_group'); define('USERS_TABLE', $mx_table_prefix.'users'); define('WORDS_TABLE', $table_prefix.'words'); //define('VOTE_DESC_TABLE', $table_prefix.'vote_desc'); //define('VOTE_RESULTS_TABLE', $table_prefix.'vote_results'); //define('VOTE_USERS_TABLE', $table_prefix.'vote_voters'); ?> --- NEW FILE: session.php --- <?php /*************************************************************************** * sessions.php * ------------------- * begin : Saturday, Feb 13, 2001 * copyright : (C) 2001 The phpBB Group * email : su...@ph... * * $Id: session.php,v 1.1 2007/09/09 16:51:31 jonohlsson Exp $ * * ***************************************************************************/ /*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * ***************************************************************************/ /** * Modifications: * * Class wrapper * mx_dss_rand */ /** * Session class * @package mxBB3 */ class session { var $cookie_data = array(); var $page = array(); var $data = array(); var $browser = ''; var $forwarded_for = ''; var $host = ''; var $session_id = ''; var $ip = ''; var $load = 0; var $time_now = 0; var $update_session_page = true; // // Adds/updates a new session to the database for the given userid. // Returns the new session ID on success. // function session_begin($user_id, $user_ip, $page_id, $auto_create = 0, $enable_autologin = 0, $admin = 0) { global $db, $board_config; global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID; $cookiename = $board_config['cookie_name']; $cookiepath = $board_config['cookie_path']; $cookiedomain = $board_config['cookie_domain']; $cookiesecure = $board_config['cookie_secure']; if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ) { $session_id = isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : ''; $sessiondata = isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array(); $sessionmethod = SESSION_METHOD_COOKIE; } else { $sessiondata = array(); $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : ''; $sessionmethod = SESSION_METHOD_GET; } // if (!preg_match('/^[A-Za-z0-9]*$/', $session_id)) { $session_id = ''; } $page_id = (int) $page_id; $last_visit = 0; $current_time = time(); // // Are auto-logins allowed? // If allow_autologin is not set or is true then they are // (same behaviour as old 2.0.x session code) // if (isset($board_config['allow_autologin']) && !$board_config['allow_autologin']) { $enable_autologin = $sessiondata['autologinid'] = false; } // // First off attempt to join with the autologin value if we have one // If not, just use the user_id value // $userdata = array(); if ($user_id != ANONYMOUS) { if (isset($sessiondata['autologinid']) && (string) $sessiondata['autologinid'] != '' && $user_id) { $sql = 'SELECT u.* FROM ' . USERS_TABLE . ' u, ' . SESSIONS_KEYS_TABLE . ' k WHERE u.user_id = ' . (int) $user_id . " AND u.user_active = 1 AND k.user_id = u.user_id AND k.key_id = '" . md5($sessiondata['autologinid']) . "'"; if (!($result = $db->sql_query($sql))) { mx_message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql); } $userdata = $db->sql_fetchrow($result); $db->sql_freeresult($result); $enable_autologin = $login = 1; } else if (!$auto_create) { $sessiondata['autologinid'] = ''; $sessiondata['userid'] = $user_id; $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . (int) $user_id . ' AND user_active = 1'; if (!($result = $db->sql_query($sql))) { mx_message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql); } $userdata = $db->sql_fetchrow($result); $db->sql_freeresult($result); $login = 1; } } // // At this point either $userdata should be populated or // one of the below is true // * Key didn't match one in the DB // * User does not exist // * User is inactive // if (!sizeof($userdata) || !is_array($userdata) || !$userdata) { $sessiondata['autologinid'] = ''; $sessiondata['userid'] = $user_id = ANONYMOUS; $enable_autologin = $login = 0; $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . (int) $user_id; if (!($result = $db->sql_query($sql))) { mx_message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql); } $userdata = $db->sql_fetchrow($result); $db->sql_freeresult($result); } // // Initial ban check against user id, IP and email address // /* preg_match('/(..)(..)(..)(..)/', $user_ip, $user_ip_parts); $sql = "SELECT ban_ip, ban_userid, ban_email FROM " . BANLIST_TABLE . " 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') OR ban_userid = $user_id"; if ( $user_id != ANONYMOUS ) { $sql .= " OR ban_email LIKE '" . str_replace("\'", "''", $userdata['user_email']) . "' OR ban_email LIKE '" . substr(str_replace("\'", "''", $userdata['user_email']), strpos(str_replace("\'", "''", $userdata['user_email']), "@")) . "'"; } if ( !($result = $db->sql_query($sql)) ) { mx_message_die(CRITICAL_ERROR, 'Could not obtain ban information', '', __LINE__, __FILE__, $sql); } if ( $ban_info = $db->sql_fetchrow($result) ) { if ( $ban_info['ban_ip'] || $ban_info['ban_userid'] || $ban_info['ban_email'] ) { mx_message_die(CRITICAL_MESSAGE, 'You_been_banned'); } } */ // // Create or update the session // $sql = "UPDATE " . SESSIONS_TABLE . " 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 WHERE session_id = '" . $session_id . "' AND session_ip = '$user_ip'"; if ( !$db->sql_query($sql) || !$db->sql_affectedrows() ) { $session_id = md5(mx_dss_rand()); $sql = "INSERT INTO " . SESSIONS_TABLE . " (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_admin) VALUES ('$session_id', $user_id, $current_time, $current_time, '$user_ip', $page_id, $login, $admin)"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error creating new session', '', __LINE__, __FILE__, $sql); } } if ( $user_id != ANONYMOUS ) { $last_visit = ( $userdata['user_session_time'] > 0 ) ? $userdata['user_session_time'] : $current_time; if (!$admin) { $sql = "UPDATE " . USERS_TABLE . " SET user_session_time = $current_time, user_session_page = $page_id, user_lastvisit = $last_visit WHERE user_id = $user_id"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error updating last visit time', '', __LINE__, __FILE__, $sql); } } $userdata['user_lastvisit'] = $last_visit; // // Regenerate the auto-login key // if ($enable_autologin) { $auto_login_key = mx_dss_rand() . mx_dss_rand(); if (isset($sessiondata['autologinid']) && (string) $sessiondata['autologinid'] != '') { $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . " SET last_ip = '$user_ip', key_id = '" . md5($auto_login_key) . "', last_login = $current_time WHERE key_id = '" . md5($sessiondata['autologinid']) . "'"; } else { $sql = 'INSERT INTO ' . SESSIONS_KEYS_TABLE . "(key_id, user_id, last_ip, last_login) VALUES ('" . md5($auto_login_key) . "', $user_id, '$user_ip', $current_time)"; } if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error updating session key', '', __LINE__, __FILE__, $sql); } $sessiondata['autologinid'] = $auto_login_key; unset($auto_login_key); } else { $sessiondata['autologinid'] = ''; } // $sessiondata['autologinid'] = (!$admin) ? (( $enable_autologin && $sessionmethod == SESSION_METHOD_COOKIE ) ? $auto_login_key : '') : $sessiondata['autologinid']; $sessiondata['userid'] = $user_id; } $userdata['session_id'] = $session_id; $userdata['session_ip'] = $user_ip; $userdata['session_user_id'] = $user_id; $userdata['session_logged_in'] = $login; $userdata['session_page'] = $page_id; $userdata['session_start'] = $current_time; $userdata['session_time'] = $current_time; $userdata['session_admin'] = $admin; $userdata['session_key'] = $sessiondata['autologinid']; setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure); setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure); $SID = 'sid=' . $session_id; return $userdata; } // // Checks for a given user session, tidies session table and updates user // sessions at each page refresh // function session_pagestart($user_ip, $thispage_id) { global $db, $lang, $board_config; global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID; $cookiename = $board_config['cookie_name']; $cookiepath = $board_config['cookie_path']; $cookiedomain = $board_config['cookie_domain']; $cookiesecure = $board_config['cookie_secure']; $current_time = time(); unset($userdata); if ( isset($HTTP_COOKIE_VARS[$cookiename . '_sid']) || isset($HTTP_COOKIE_VARS[$cookiename . '_data']) ) { $sessiondata = isset( $HTTP_COOKIE_VARS[$cookiename . '_data'] ) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookiename . '_data'])) : array(); $session_id = isset( $HTTP_COOKIE_VARS[$cookiename . '_sid'] ) ? $HTTP_COOKIE_VARS[$cookiename . '_sid'] : ''; $sessionmethod = SESSION_METHOD_COOKIE; } else { $sessiondata = array(); $session_id = ( isset($HTTP_GET_VARS['sid']) ) ? $HTTP_GET_VARS['sid'] : ''; $sessionmethod = SESSION_METHOD_GET; } // if (!preg_match('/^[A-Za-z0-9]*$/', $session_id)) { $session_id = ''; } $thispage_id = (int) $thispage_id; // // Does a session exist? // if ( !empty($session_id) ) { // // session_id exists so go ahead and attempt to grab all // data in preparation // $sql = "SELECT u.*, s.* FROM " . SESSIONS_TABLE . " s, " . USERS_TABLE . " u WHERE s.session_id = '$session_id' AND u.user_id = s.session_user_id"; if ( !($result = $db->sql_query($sql)) ) { mx_message_die(CRITICAL_ERROR, 'Error doing DB query userdata row fetch', '', __LINE__, __FILE__, $sql); } $userdata = $db->sql_fetchrow($result); // // Did the session exist in the DB? // if ( isset($userdata['user_id']) ) { // // Do not check IP assuming equivalence, if IPv4 we'll check only first 24 // bits ... I've been told (by vHiker) this should alleviate problems with // load balanced et al proxies while retaining some reliance on IP security. // $ip_check_s = substr($userdata['session_ip'], 0, 6); $ip_check_u = substr($user_ip, 0, 6); if ($ip_check_s == $ip_check_u) { $SID = ($sessionmethod == SESSION_METHOD_GET || defined('IN_ADMIN')) ? 'sid=' . $session_id : ''; // // Only update session DB a minute or so after last update // if ( $current_time - $userdata['session_time'] > 60 ) { // A little trick to reset session_admin on session re-usage $update_admin = (!defined('IN_ADMIN') && $current_time - $userdata['session_time'] > ($board_config['session_length']+60)) ? ', session_admin = 0' : ''; $sql = "UPDATE " . SESSIONS_TABLE . " SET session_time = $current_time, session_page = $thispage_id$update_admin WHERE session_id = '" . $userdata['session_id'] . "'"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql); } if ( $userdata['user_id'] != ANONYMOUS ) { $sql = "UPDATE " . USERS_TABLE . " SET user_session_time = $current_time, user_session_page = $thispage_id WHERE user_id = " . $userdata['user_id']; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error updating sessions table', '', __LINE__, __FILE__, $sql); } } $this->session_clean($userdata['session_id']); setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure); setcookie($cookiename . '_sid', $session_id, 0, $cookiepath, $cookiedomain, $cookiesecure); } // Add the session_key to the userdata array if it is set if ( isset($sessiondata['autologinid']) && $sessiondata['autologinid'] != '' ) { $userdata['session_key'] = $sessiondata['autologinid']; } return $userdata; } } } // // If we reach here then no (valid) session exists. So we'll create a new one, // using the cookie user_id if available to pull basic user prefs. // $user_id = ( isset($sessiondata['userid']) ) ? intval($sessiondata['userid']) : ANONYMOUS; if ( !($userdata = $this->session_begin($user_id, $user_ip, $thispage_id, TRUE)) ) { mx_message_die(CRITICAL_ERROR, 'Error creating user session', '', __LINE__, __FILE__, $sql); } return $userdata; } /** * Terminates the specified session * It will delete the entry in the sessions table for this session, * remove the corresponding auto-login key and reset the cookies */ function session_end($session_id, $user_id) { global $db, $lang, $board_config, $userdata; global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $SID; $cookiename = $board_config['cookie_name']; $cookiepath = $board_config['cookie_path']; $cookiedomain = $board_config['cookie_domain']; $cookiesecure = $board_config['cookie_secure']; $current_time = time(); if (!preg_match('/^[A-Za-z0-9]*$/', $session_id)) { return; } // // Delete existing session // $sql = 'DELETE FROM ' . SESSIONS_TABLE . " WHERE session_id = '$session_id' AND session_user_id = $user_id"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error removing user session', '', __LINE__, __FILE__, $sql); } // // Remove this auto-login entry (if applicable) // if ( isset($userdata['session_key']) && $userdata['session_key'] != '' ) { $autologin_key = md5($userdata['session_key']); $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . ' WHERE user_id = ' . (int) $user_id . " AND key_id = '$autologin_key'"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error removing auto-login key', '', __LINE__, __FILE__, $sql); } } // // We expect that message_die will be called after this function, // but just in case it isn't, reset $userdata to the details for a guest // $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS; if ( !($result = $db->sql_query($sql)) ) { mx_message_die(CRITICAL_ERROR, 'Error obtaining user details', '', __LINE__, __FILE__, $sql); } if ( !($userdata = $db->sql_fetchrow($result)) ) { mx_message_die(CRITICAL_ERROR, 'Error obtaining user details', '', __LINE__, __FILE__, $sql); } $db->sql_freeresult($result); setcookie($cookiename . '_data', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure); setcookie($cookiename . '_sid', '', $current_time - 31536000, $cookiepath, $cookiedomain, $cookiesecure); return true; } /** * Removes expired sessions and auto-login keys from the database */ function session_clean($session_id) { global $board_config, $db; // // Delete expired sessions // $sql = 'DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_time < ' . (time() - (int) $board_config['session_length']) . " AND session_id <> '$session_id'"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error clearing sessions table', '', __LINE__, __FILE__, $sql); } // // Delete expired auto-login keys // If max_autologin_time is not set then keys will never be deleted // (same behaviour as old 2.0.x session code) // if (!empty($board_config['max_autologin_time']) && $board_config['max_autologin_time'] > 0) { $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . ' WHERE last_login < ' . (time() - (86400 * (int) $board_config['max_autologin_time'])); $db->sql_query($sql); } return true; } /** * Reset all login keys for the specified user * Called on password changes */ function session_reset_keys($user_id, $user_ip) { global $db, $userdata, $board_config; $key_sql = ($user_id == $userdata['user_id'] && !empty($userdata['session_key'])) ? "AND key_id != '" . md5($userdata['session_key']) . "'" : ''; $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . ' WHERE user_id = ' . (int) $user_id . " $key_sql"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error removing auto-login keys', '', __LINE__, __FILE__, $sql); } $where_sql = 'session_user_id = ' . (int) $user_id; $where_sql .= ($user_id == $userdata['user_id']) ? " AND session_id <> '" . $userdata['session_id'] . "'" : ''; $sql = 'DELETE FROM ' . SESSIONS_TABLE . " WHERE $where_sql"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error removing user session(s)', '', __LINE__, __FILE__, $sql); } if ( !empty($key_sql) ) { $auto_login_key = mx_dss_rand() . mx_dss_rand(); $current_time = time(); $sql = 'UPDATE ' . SESSIONS_KEYS_TABLE . " SET last_ip = '$user_ip', key_id = '" . md5($auto_login_key) . "', last_login = $current_time WHERE key_id = '" . md5($userdata['session_key']) . "'"; if ( !$db->sql_query($sql) ) { mx_message_die(CRITICAL_ERROR, 'Error updating session key', '', __LINE__, __FILE__, $sql); } // And now rebuild the cookie $sessiondata['userid'] = $user_id; $sessiondata['autologinid'] = $auto_login_key; $cookiename = $board_config['cookie_name']; $cookiepath = $board_config['cookie_path']; $cookiedomain = $board_config['cookie_domain']; $cookiesecure = $board_config['cookie_secure']; setcookie($cookiename . '_data', serialize($sessiondata), $current_time + 31536000, $cookiepath, $cookiedomain, $cookiesecure); $userdata['session_key'] = $auto_login_key; unset($sessiondata); unset($auto_login_key); } } } // // Append $SID to a url. Borrowed from phplib and modified. This is an // extra routine utilised by the session code above and acts as a wrapper // around every single URL and form action. If you replace the session // code you must include this routine, even if it's empty. // function append_sid($url, $non_html_amp = false) { global $SID; if ( !empty($SID) && !preg_match('#sid=#', $url) ) { $url .= ( ( strpos($url, '?') !== false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID; } return $url; } ?> |