/** * Logs the user in by setting the session cookie * * Be sure you've already authenticated the username/password * using smfapi_authenticate() or some other means because * this function WILL set the correct session cookie for the * user you specify and they WILL be logged in * * @param string $username (or int member id or string email. We're not picky) * @param int $cookieLength length to set the cookie for (in minutes) * @return bool whether the login cookie was set or not * @since 0.1.0 */ function smfapi_login($username = '', $cookieLength = 525600) { global $scripturl, $user_info, $user_settings, $smcFunc; global $cookiename, $maintenance, $modSettings, $sc, $sourcedir; if (1 == $maintenance || '' == $username) { return false; } $user_data = smfapi_getUserData($username); if (!$user_data) { return false; } // cookie set, session too smfapi_setLoginCookie(60 * $cookieLength, $user_data['id_member'], sha1($user_data['passwd'] . $user_data['password_salt'])); // you've logged in, haven't you? smfapi_updateMemberData($user_data['id_member'], array('last_login' => time(), 'member_ip' => $user_info['ip'])); // get rid of the online entry for that old guest.... $smcFunc['db_query']('', ' DELETE FROM {db_prefix}log_online WHERE session = {string:session}', array('session' => 'ip' . $user_info['ip'])); smfapi_loadUserSettings(); return true; }
/** * Will log out a user * * Takes a username, email or member id and logs that user out. If it can't find * a match it will look for the currently logged user if any. Best to leave this * function's arguments empty. If logoutis failing, make sure your SMF cookie is * being set on path '/', otherwise we can't delete it. Also, try leaving the * function parameter empty to let the script auto-detect the currently logged in * user for you. * * @param string $username user's member name (or int member id or string email) * @return bool whether logout was successful or not * @since 0.1.2 */ function smfapi_logout($username = '') { global $user_info, $smcFunc; if ($user_info['is_guest']) { smfapi_loadUserSettings(); } if ('' == $username) { if ($user_info['is_guest']) { return false; } else { $username = $user_info['username']; } } $user_data = smfapi_getUserData($username); if (!$user_data || empty($user_data)) { // no user by that name return false; } // delete them from log_online $smcFunc['db_query']('', ' DELETE FROM {db_prefix}log_online WHERE id_member = {int:current_member}', array('current_member' => $user_data['id_member'])); if (isset($_SESSION['pack_ftp'])) { $_SESSION['pack_ftp'] = null; } // they cannot be open ID verified any longer. if (isset($_SESSION['openid'])) { unset($_SESSION['openid']); } // it won't be first login anymore. if (isset($_SESSION['openid'])) { unset($_SESSION['first_login']); } // destroy the cookie smfapi_setLoginCookie(-3600, 0); return true; }