4 require_once('include/security.php');
5 require_once('include/datetime.php');
7 function nuke_session() {
8 new_cookie(0); // make sure cookie is deleted on browser close, as a security measure
10 unset($_SESSION['authenticated']);
11 unset($_SESSION['uid']);
12 unset($_SESSION['visitor_id']);
13 unset($_SESSION['administrator']);
14 unset($_SESSION['cid']);
15 unset($_SESSION['theme']);
16 unset($_SESSION['mobile-theme']);
17 unset($_SESSION['page_flags']);
18 unset($_SESSION['submanage']);
19 unset($_SESSION['my_url']);
20 unset($_SESSION['my_address']);
21 unset($_SESSION['addr']);
22 unset($_SESSION['return_url']);
31 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
33 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
35 // process logout request
36 call_hooks("logging_out");
38 info( t('Logged out.') . EOL);
42 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
43 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
44 intval($_SESSION['visitor_id'])
51 if(x($_SESSION,'uid')) {
53 // already logged in user returning
55 $check = get_config('system','paranoia');
56 // extra paranoia - if the IP changed, log them out
57 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
58 logger('Session address changed. Paranoid setting in effect, blocking session. '
59 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
64 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
65 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
66 intval($_SESSION['uid'])
74 // Make sure to refresh the last login time for the user if the user
75 // stays logged in for a long time, e.g. with "Remember Me"
76 $login_refresh = false;
77 if(! x($_SESSION['last_login_date'])) {
78 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
80 if( strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0 ) {
82 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
83 $login_refresh = true;
85 authenticate_success($r[0], false, false, $login_refresh);
90 if(isset($_SESSION)) {
94 if((x($_POST,'password')) && strlen($_POST['password']))
95 $encrypted = hash('whirlpool',trim($_POST['password']));
97 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
98 (x($_POST,'username')) && strlen($_POST['username'])) {
100 $noid = get_config('system','no_openid');
102 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
104 // validate_url alters the calling parameter
106 $temp_string = $openid_url;
108 // if it's an email address or doesn't resolve to a URL, fail.
110 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
112 notice( t('Login failed.') . EOL);
117 // Otherwise it's probably an openid.
120 require_once('library/openid.php');
121 $openid = new LightOpenID;
122 $openid->identity = $openid_url;
123 $_SESSION['openid'] = $openid_url;
125 $openid->returnUrl = $a->get_baseurl(true) . '/openid';
126 goaway($openid->authUrl());
127 } catch (Exception $e) {
128 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());
134 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
139 'username' => trim($_POST['username']),
140 'password' => trim($_POST['password']),
141 'authenticated' => 0,
142 'user_record' => null
147 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
148 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
149 * and later plugins should not interfere with an earlier one that succeeded.
153 call_hooks('authenticate', $addon_auth);
155 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
156 $record = $addon_auth['user_record'];
160 // process normal login request
162 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
163 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
164 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
165 dbesc(trim($_POST['username'])),
166 dbesc(trim($_POST['username'])),
173 if((! $record) || (! count($record))) {
174 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
175 notice( t('Login failed.') . EOL );
179 // If the user specified to remember the authentication, then change the cookie
180 // to expire after one year (the default is when the browser is closed).
181 // If the user did not specify to remember, change the cookie to expire when the
182 // browser is closed. The reason this is necessary is because if the user
183 // specifies to remember, then logs out and logs back in without specifying to
184 // remember, the old "remember" cookie may remain and prevent the session from
185 // expiring when the browser is closed.
187 // It seems like I should be able to test for the old cookie, but for some reason when
188 // I read the lifetime value from session_get_cookie_params(), I always get '0'
189 // (i.e. expire when the browser is closed), even when there's a time expiration
191 if($_POST['remember']) {
192 new_cookie(31449600); // one year
195 new_cookie(0); // 0 means delete on browser exit
198 // if we haven't failed up this point, log them in.
200 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
201 authenticate_success($record, true, true);
205 function new_cookie($time) {
206 $old_sid = session_id();
207 session_set_cookie_params("$time");
208 session_regenerate_id(false);
210 q("UPDATE session SET sid = '%s' WHERE sid = '%s'", dbesc(session_id()), dbesc($old_sid));