/** * Sets the login cookie and session based on the id_member and password passed. * * What it does: * - password should be already encrypted with the cookie salt. * - logs the user out if id_member is zero. * - sets the cookie and session to last the number of seconds specified by cookie_length. * - when logging out, if the globalCookies setting is enabled, attempts to clear the subdomain's cookie too. * * @package Authorization * @param int $cookie_length * @param int $id The id of the member * @param string $password = '' */ function setLoginCookie($cookie_length, $id, $password = '') { global $cookiename, $boardurl, $modSettings; // If changing state force them to re-address some permission caching. $_SESSION['mc']['time'] = 0; // Let's be sure it is an int to simplify the regexp used to validate the cookie $id = (int) $id; // The cookie may already exist, and have been set with different options. $cookie_state = (empty($modSettings['localCookies']) ? 0 : 1) | (empty($modSettings['globalCookies']) ? 0 : 2); if (isset($_COOKIE[$cookiename]) && preg_match('~^a:[34]:\\{i:0;i:\\d{1,8};i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\\d{1,14};(i:3;i:\\d;)?\\}$~', $_COOKIE[$cookiename]) === 1) { $array = @unserialize($_COOKIE[$cookiename]); // Out with the old, in with the new! if (isset($array[3]) && $array[3] != $cookie_state) { $cookie_url = url_parts($array[3] & 1 > 0, $array[3] & 2 > 0); elk_setcookie($cookiename, serialize(array(0, '', 0)), time() - 3600, $cookie_url[1], $cookie_url[0]); } } // Get the data and path to set it on. $data = serialize(empty($id) ? array(0, '', 0) : array($id, $password, time() + $cookie_length, $cookie_state)); $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])); // Set the cookie, $_COOKIE, and session variable. elk_setcookie($cookiename, $data, time() + $cookie_length, $cookie_url[1], $cookie_url[0]); // If subdomain-independent cookies are on, unset the subdomain-dependent cookie too. if (empty($id) && !empty($modSettings['globalCookies'])) { elk_setcookie($cookiename, $data, time() + $cookie_length, $cookie_url[1], ''); } // Any alias URLs? This is mainly for use with frames, etc. if (!empty($modSettings['forum_alias_urls'])) { $aliases = explode(',', $modSettings['forum_alias_urls']); $temp = $boardurl; foreach ($aliases as $alias) { // Fake the $boardurl so we can set a different cookie. $alias = strtr(trim($alias), array('http://' => '', 'https://' => '')); $boardurl = 'http://' . $alias; $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])); if ($cookie_url[0] == '') { $cookie_url[0] = strtok($alias, '/'); } elk_setcookie($cookiename, $data, time() + $cookie_length, $cookie_url[1], $cookie_url[0]); } $boardurl = $temp; } $_COOKIE[$cookiename] = $data; // Make sure the user logs in with a new session ID. if (!isset($_SESSION['login_' . $cookiename]) || $_SESSION['login_' . $cookiename] !== $data) { // We need to meddle with the session. require_once SOURCEDIR . '/Session.php'; // Backup the old session. $oldSessionData = $_SESSION; // Remove the old session data and file / db entry $_SESSION = array(); session_destroy(); // Recreate and restore the new session. loadSession(); // Get a new session id, and load it with the data session_regenerate_id(); $_SESSION = $oldSessionData; $_SESSION['login_' . $cookiename] = $data; } }
/** * Allow the user to vote. * It is called to register a vote in a poll. * Must be called with a topic and option specified. * Requires the poll_vote permission. * Upon successful completion of action will direct user back to topic. * Accessed via ?action=poll;sa=vote. * * @uses Post language file. */ public function action_vote() { global $topic, $user_info, $modSettings; require_once SUBSDIR . '/Poll.subs.php'; // Make sure you can vote. isAllowedTo('poll_vote'); loadLanguage('Post'); // Check if they have already voted, or voting is locked. $row = checkVote($topic); if (empty($row)) { fatal_lang_error('poll_error', false); } // If this is a guest can they vote? if ($user_info['is_guest']) { // Guest voting disabled? if (!$row['guest_vote']) { fatal_lang_error('guest_vote_disabled'); } elseif (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $row['id_poll'] . ',') !== false) { // ;id,timestamp,[vote,vote...]; etc $guestinfo = explode(';', $_COOKIE['guest_poll_vote']); // Find the poll we're after. foreach ($guestinfo as $i => $guestvoted) { $guestvoted = explode(',', $guestvoted); if ($guestvoted[0] == $row['id_poll']) { break; } } // Has the poll been reset since guest voted? if (isset($guestvoted[1]) && $row['reset_poll'] > $guestvoted[1]) { // Remove the poll info from the cookie to allow guest to vote again unset($guestinfo[$i]); if (!empty($guestinfo)) { $_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo); } else { unset($_COOKIE['guest_poll_vote']); } } else { fatal_lang_error('poll_error', false); } unset($guestinfo, $guestvoted, $i); } } // Is voting locked or has it expired? if (!empty($row['voting_locked']) || !empty($row['expire_time']) && time() > $row['expire_time']) { fatal_lang_error('poll_error', false); } // If they have already voted and aren't allowed to change their vote - hence they are outta here! if (!$user_info['is_guest'] && $row['selected'] != -1 && empty($row['change_vote'])) { fatal_lang_error('poll_error', false); } elseif (!empty($row['change_vote']) && !$user_info['is_guest'] && empty($_POST['options'])) { checkSession('request'); // Find out what they voted for before. $pollOptions = determineVote($user_info['id'], $row['id_poll']); // Just skip it if they had voted for nothing before. if (!empty($pollOptions)) { // Update the poll totals. decreaseVoteCounter($row['id_poll'], $pollOptions); // Delete off the log. removeVote($user_info['id'], $row['id_poll']); } // Redirect back to the topic so the user can vote again! if (empty($_POST['options'])) { redirectexit('topic=' . $topic . '.' . $_REQUEST['start']); } } checkSession('request'); // Make sure the option(s) are valid. if (empty($_POST['options'])) { fatal_lang_error('didnt_select_vote', false); } // Too many options checked! if (count($_REQUEST['options']) > $row['max_votes']) { fatal_lang_error('poll_too_many_votes', false, array($row['max_votes'])); } $pollOptions = array(); $inserts = array(); foreach ($_REQUEST['options'] as $id) { $id = (int) $id; $pollOptions[] = $id; $inserts[] = array($row['id_poll'], $user_info['id'], $id); } // Add their vote to the tally. addVote($inserts); increaseVoteCounter($row['id_poll'], $pollOptions); // If it's a guest don't let them vote again. if ($user_info['is_guest'] && count($pollOptions) > 0) { // Time is stored in case the poll is reset later, plus what they voted for. $_COOKIE['guest_poll_vote'] = empty($_COOKIE['guest_poll_vote']) ? '' : $_COOKIE['guest_poll_vote']; // ;id,timestamp,[vote,vote...]; etc $_COOKIE['guest_poll_vote'] .= ';' . $row['id_poll'] . ',' . time() . ',' . (count($pollOptions) > 1 ? implode(',', $pollOptions) : $pollOptions[0]); // Increase num guest voters count by 1 increaseGuestVote($row['id_poll']); require_once SUBSDIR . '/Auth.subs.php'; $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])); elk_setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], false, false); } // Maybe let a social networking mod log this, or something? call_integration_hook('integrate_poll_vote', array(&$row['id_poll'], &$pollOptions)); // Return to the post... redirectexit('topic=' . $topic . '.' . $_REQUEST['start']); }
/** * Apply restrictions for banned users. For example, disallow access. * * What it does: * - If the user is banned, it dies with an error. * - Caches this information for optimization purposes. * - Forces a recheck if force_check is true. * * @param bool $forceCheck = false */ function is_not_banned($forceCheck = false) { global $txt, $modSettings, $context, $user_info, $cookiename, $user_settings; $db = database(); // You cannot be banned if you are an admin - doesn't help if you log out. if ($user_info['is_admin']) { return; } // Only check the ban every so often. (to reduce load.) if ($forceCheck || !isset($_SESSION['ban']) || empty($modSettings['banLastUpdated']) || $_SESSION['ban']['last_checked'] < $modSettings['banLastUpdated'] || $_SESSION['ban']['id_member'] != $user_info['id'] || $_SESSION['ban']['ip'] != $user_info['ip'] || $_SESSION['ban']['ip2'] != $user_info['ip2'] || isset($user_info['email'], $_SESSION['ban']['email']) && $_SESSION['ban']['email'] != $user_info['email']) { // Innocent until proven guilty. (but we know you are! :P) $_SESSION['ban'] = array('last_checked' => time(), 'id_member' => $user_info['id'], 'ip' => $user_info['ip'], 'ip2' => $user_info['ip2'], 'email' => $user_info['email']); $ban_query = array(); $ban_query_vars = array('current_time' => time()); $flag_is_activated = false; // Check both IP addresses. foreach (array('ip', 'ip2') as $ip_number) { if ($ip_number == 'ip2' && $user_info['ip2'] == $user_info['ip']) { continue; } $ban_query[] = constructBanQueryIP($user_info[$ip_number]); // IP was valid, maybe there's also a hostname... if (empty($modSettings['disableHostnameLookup']) && $user_info[$ip_number] != 'unknown') { $hostname = host_from_ip($user_info[$ip_number]); if (strlen($hostname) > 0) { $ban_query[] = '({string:hostname} LIKE bi.hostname)'; $ban_query_vars['hostname'] = $hostname; } } } // Is their email address banned? if (strlen($user_info['email']) != 0) { $ban_query[] = '({string:email} LIKE bi.email_address)'; $ban_query_vars['email'] = $user_info['email']; } // How about this user? if (!$user_info['is_guest'] && !empty($user_info['id'])) { $ban_query[] = 'bi.id_member = {int:id_member}'; $ban_query_vars['id_member'] = $user_info['id']; } // Check the ban, if there's information. if (!empty($ban_query)) { $restrictions = array('cannot_access', 'cannot_login', 'cannot_post', 'cannot_register'); $request = $db->query('', ' SELECT bi.id_ban, bi.email_address, bi.id_member, bg.cannot_access, bg.cannot_register, bg.cannot_post, bg.cannot_login, bg.reason, IFNULL(bg.expire_time, 0) AS expire_time FROM {db_prefix}ban_items AS bi INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time})) WHERE (' . implode(' OR ', $ban_query) . ')', $ban_query_vars); // Store every type of ban that applies to you in your session. while ($row = $db->fetch_assoc($request)) { foreach ($restrictions as $restriction) { if (!empty($row[$restriction])) { $_SESSION['ban'][$restriction]['reason'] = $row['reason']; $_SESSION['ban'][$restriction]['ids'][] = $row['id_ban']; if (!isset($_SESSION['ban']['expire_time']) || $_SESSION['ban']['expire_time'] != 0 && ($row['expire_time'] == 0 || $row['expire_time'] > $_SESSION['ban']['expire_time'])) { $_SESSION['ban']['expire_time'] = $row['expire_time']; } if (!$user_info['is_guest'] && $restriction == 'cannot_access' && ($row['id_member'] == $user_info['id'] || $row['email_address'] == $user_info['email'])) { $flag_is_activated = true; } } } } $db->free_result($request); } // Mark the cannot_access and cannot_post bans as being 'hit'. if (isset($_SESSION['ban']['cannot_access']) || isset($_SESSION['ban']['cannot_post']) || isset($_SESSION['ban']['cannot_login'])) { log_ban(array_merge(isset($_SESSION['ban']['cannot_access']) ? $_SESSION['ban']['cannot_access']['ids'] : array(), isset($_SESSION['ban']['cannot_post']) ? $_SESSION['ban']['cannot_post']['ids'] : array(), isset($_SESSION['ban']['cannot_login']) ? $_SESSION['ban']['cannot_login']['ids'] : array())); } // If for whatever reason the is_activated flag seems wrong, do a little work to clear it up. if ($user_info['id'] && ($user_settings['is_activated'] >= 10 && !$flag_is_activated || $user_settings['is_activated'] < 10 && $flag_is_activated)) { require_once SUBSDIR . '/Bans.subs.php'; updateBanMembers(); } } // Hey, I know you! You're ehm... if (!isset($_SESSION['ban']['cannot_access']) && !empty($_COOKIE[$cookiename . '_'])) { $bans = explode(',', $_COOKIE[$cookiename . '_']); foreach ($bans as $key => $value) { $bans[$key] = (int) $value; } $request = $db->query('', ' SELECT bi.id_ban, bg.reason FROM {db_prefix}ban_items AS bi INNER JOIN {db_prefix}ban_groups AS bg ON (bg.id_ban_group = bi.id_ban_group) WHERE bi.id_ban IN ({array_int:ban_list}) AND (bg.expire_time IS NULL OR bg.expire_time > {int:current_time}) AND bg.cannot_access = {int:cannot_access} LIMIT ' . count($bans), array('cannot_access' => 1, 'ban_list' => $bans, 'current_time' => time())); while ($row = $db->fetch_assoc($request)) { $_SESSION['ban']['cannot_access']['ids'][] = $row['id_ban']; $_SESSION['ban']['cannot_access']['reason'] = $row['reason']; } $db->free_result($request); // My mistake. Next time better. if (!isset($_SESSION['ban']['cannot_access'])) { require_once SUBSDIR . '/Auth.subs.php'; $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])); elk_setcookie($cookiename . '_', '', time() - 3600, $cookie_url[1], $cookie_url[0], false, false); } } // If you're fully banned, it's end of the story for you. if (isset($_SESSION['ban']['cannot_access'])) { require_once SUBSDIR . '/Auth.subs.php'; // We don't wanna see you! if (!$user_info['is_guest']) { require_once CONTROLLERDIR . '/Auth.controller.php'; $controller = new Auth_Controller(); $controller->action_logout(true, false); } // 'Log' the user out. Can't have any funny business... (save the name!) $old_name = isset($user_info['name']) && $user_info['name'] != '' ? $user_info['name'] : $txt['guest_title']; $user_info['name'] = ''; $user_info['username'] = ''; $user_info['is_guest'] = true; $user_info['is_admin'] = false; $user_info['permissions'] = array(); $user_info['id'] = 0; $context['user'] = array('id' => 0, 'username' => '', 'name' => $txt['guest_title'], 'is_guest' => true, 'is_logged' => false, 'is_admin' => false, 'is_mod' => false, 'can_mod' => false, 'language' => $user_info['language']); // A goodbye present. $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])); elk_setcookie($cookiename . '_', implode(',', $_SESSION['ban']['cannot_access']['ids']), time() + 3153600, $cookie_url[1], $cookie_url[0], false, false); // Don't scare anyone, now. $_GET['action'] = ''; $_GET['board'] = ''; $_GET['topic'] = ''; writeLog(true); // You banned, sucka! fatal_error(sprintf($txt['your_ban'], $old_name) . (empty($_SESSION['ban']['cannot_access']['reason']) ? '' : '<br />' . $_SESSION['ban']['cannot_access']['reason']) . '<br />' . (!empty($_SESSION['ban']['expire_time']) ? sprintf($txt['your_ban_expires'], standardTime($_SESSION['ban']['expire_time'], false)) : $txt['your_ban_expires_never']), 'user'); // If we get here, something's gone wrong.... but let's try anyway. trigger_error('Hacking attempt...', E_USER_ERROR); } elseif (isset($_SESSION['ban']['cannot_login']) && !$user_info['is_guest']) { // We don't wanna see you! $db->query('', ' DELETE FROM {db_prefix}log_online WHERE id_member = {int:current_member}', array('current_member' => $user_info['id'])); // 'Log' the user out. Can't have any funny business... (save the name!) $old_name = isset($user_info['name']) && $user_info['name'] != '' ? $user_info['name'] : $txt['guest_title']; $user_info['name'] = ''; $user_info['username'] = ''; $user_info['is_guest'] = true; $user_info['is_admin'] = false; $user_info['permissions'] = array(); $user_info['id'] = 0; $context['user'] = array('id' => 0, 'username' => '', 'name' => $txt['guest_title'], 'is_guest' => true, 'is_logged' => false, 'is_admin' => false, 'is_mod' => false, 'can_mod' => false, 'language' => $user_info['language']); // Wipe 'n Clean(r) erases all traces. $_GET['action'] = ''; $_GET['board'] = ''; $_GET['topic'] = ''; writeLog(true); // Log them out require_once CONTROLLERDIR . '/Auth.controller.php'; $controller = new Auth_Controller(); $controller->action_logout(true, false); // Tell them thanks fatal_error(sprintf($txt['your_ban'], $old_name) . (empty($_SESSION['ban']['cannot_login']['reason']) ? '' : '<br />' . $_SESSION['ban']['cannot_login']['reason']) . '<br />' . (!empty($_SESSION['ban']['expire_time']) ? sprintf($txt['your_ban_expires'], standardTime($_SESSION['ban']['expire_time'], false)) : $txt['your_ban_expires_never']) . '<br />' . $txt['ban_continue_browse'], 'user'); } // Fix up the banning permissions. if (isset($user_info['permissions'])) { banPermissions(); } }