4 require_once('include/security.php');
5 require_once('include/datetime.php');
7 function nuke_session() {
10 new_cookie(0); // make sure cookie is deleted on browser close, as a security measure
12 unset($_SESSION['authenticated']);
13 unset($_SESSION['uid']);
14 unset($_SESSION['visitor_id']);
15 unset($_SESSION['administrator']);
16 unset($_SESSION['cid']);
17 unset($_SESSION['theme']);
18 unset($_SESSION['mobile-theme']);
19 unset($_SESSION['page_flags']);
20 unset($_SESSION['submanage']);
21 unset($_SESSION['my_url']);
22 unset($_SESSION['my_address']);
23 unset($_SESSION['addr']);
24 unset($_SESSION['return_url']);
34 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
36 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
38 // process logout request
39 call_hooks("logging_out");
41 info( t('Logged out.') . EOL);
45 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
46 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
47 intval($_SESSION['visitor_id'])
54 if(x($_SESSION,'uid')) {
56 // already logged in user returning
58 $check = get_config('system','paranoia');
59 // extra paranoia - if the IP changed, log them out
60 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
61 logger('Session address changed. Paranoid setting in effect, blocking session. '
62 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
67 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
68 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
69 intval($_SESSION['uid'])
77 // Make sure to refresh the last login time for the user if the user
78 // stays logged in for a long time, e.g. with "Remember Me"
79 $login_refresh = false;
80 if(! x($_SESSION['last_login_date'])) {
81 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
83 if( strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0 ) {
85 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
86 $login_refresh = true;
88 authenticate_success($r[0], false, false, $login_refresh);
93 if(isset($_SESSION)) {
97 if((x($_POST,'password')) && strlen($_POST['password']))
98 $encrypted = hash('whirlpool',trim($_POST['password']));
100 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
101 (x($_POST,'username')) && strlen($_POST['username'])) {
103 $noid = get_config('system','no_openid');
105 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
107 // validate_url alters the calling parameter
109 $temp_string = $openid_url;
111 // if it's an email address or doesn't resolve to a URL, fail.
113 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
115 notice( t('Login failed.') . EOL);
120 // Otherwise it's probably an openid.
123 require_once('library/openid.php');
124 $openid = new LightOpenID;
125 $openid->identity = $openid_url;
126 $_SESSION['openid'] = $openid_url;
128 $openid->returnUrl = $a->get_baseurl(true) . '/openid';
129 goaway($openid->authUrl());
130 } catch (Exception $e) {
131 notice( t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.').'<br /><br >'. t('The error message was:').' '.$e->getMessage());
137 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
142 'username' => trim($_POST['username']),
143 'password' => trim($_POST['password']),
144 'authenticated' => 0,
145 'user_record' => null
150 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
151 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
152 * and later plugins should not interfere with an earlier one that succeeded.
156 call_hooks('authenticate', $addon_auth);
158 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
159 $record = $addon_auth['user_record'];
163 // process normal login request
165 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
166 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
167 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
168 dbesc(trim($_POST['username'])),
169 dbesc(trim($_POST['username'])),
176 if((! $record) || (! count($record))) {
177 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
178 notice( t('Login failed.') . EOL );
182 // If the user specified to remember the authentication, then change the cookie
183 // to expire after one year (the default is when the browser is closed).
184 // If the user did not specify to remember, change the cookie to expire when the
185 // browser is closed. The reason this is necessary is because if the user
186 // specifies to remember, then logs out and logs back in without specifying to
187 // remember, the old "remember" cookie may remain and prevent the session from
188 // expiring when the browser is closed.
190 // It seems like I should be able to test for the old cookie, but for some reason when
191 // I read the lifetime value from session_get_cookie_params(), I always get '0'
192 // (i.e. expire when the browser is closed), even when there's a time expiration
194 if($_POST['remember']) {
195 new_cookie(31449600); // one year
198 new_cookie(0); // 0 means delete on browser exit
201 // if we haven't failed up this point, log them in.
203 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
204 authenticate_success($record, true, true);
208 function new_cookie($time) {
211 $old_sid = session_id();
212 session_set_cookie_params($time);
213 //session_set_cookie_params($time, "/", $a->get_hostname());
214 session_regenerate_id(false);
216 q("UPDATE session SET sid = '%s' WHERE sid = '%s'", dbesc(session_id()), dbesc($old_sid));
218 logger("Session parameter lifetime: ".$time." - got: ".print_r(session_get_cookie_params(), true), LOGGER_DEBUG);