4 require_once('include/security.php');
5 require_once('include/datetime.php');
7 function nuke_session() {
9 new_cookie(0); // make sure cookie is deleted on browser close, as a security measure
16 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
18 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
20 // process logout request
21 call_hooks("logging_out");
24 info( t('Logged out.') . EOL);
28 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
29 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
30 intval($_SESSION['visitor_id'])
37 if(x($_SESSION,'uid')) {
39 // already logged in user returning
41 $check = get_config('system','paranoia');
42 // extra paranoia - if the IP changed, log them out
43 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
44 logger('Session address changed. Paranoid setting in effect, blocking session. '
45 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
50 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
51 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
52 intval($_SESSION['uid'])
60 // Make sure to refresh the last login time for the user if the user
61 // stays logged in for a long time, e.g. with "Remember Me"
62 $login_refresh = false;
63 if(! x($_SESSION['last_login_date'])) {
64 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
66 if( strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0 ) {
68 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
69 $login_refresh = true;
71 authenticate_success($r[0], false, false, $login_refresh);
75 if(isset($_SESSION)) {
79 if((x($_POST,'password')) && strlen($_POST['password']))
80 $encrypted = hash('whirlpool',trim($_POST['password']));
82 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
83 (x($_POST,'username')) && strlen($_POST['username'])) {
85 $noid = get_config('system','no_openid');
87 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
89 // validate_url alters the calling parameter
91 $temp_string = $openid_url;
93 // if it's an email address or doesn't resolve to a URL, fail.
95 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
97 notice( t('Login failed.') . EOL);
102 // Otherwise it's probably an openid.
105 require_once('library/openid.php');
106 $openid = new LightOpenID;
107 $openid->identity = $openid_url;
108 $_SESSION['openid'] = $openid_url;
110 $openid->returnUrl = $a->get_baseurl(true) . '/openid';
111 goaway($openid->authUrl());
112 } catch (Exception $e) {
113 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());
119 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
124 'username' => trim($_POST['username']),
125 'password' => trim($_POST['password']),
126 'authenticated' => 0,
127 'user_record' => null
132 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
133 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
134 * and later plugins should not interfere with an earlier one that succeeded.
138 call_hooks('authenticate', $addon_auth);
140 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
141 $record = $addon_auth['user_record'];
145 // process normal login request
147 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
148 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
149 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
150 dbesc(trim($_POST['username'])),
151 dbesc(trim($_POST['username'])),
158 if((! $record) || (! count($record))) {
159 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
160 notice( t('Login failed.') . EOL );
164 // If the user specified to remember the authentication, then change the cookie
165 // to expire after one year (the default is when the browser is closed).
166 // If the user did not specify to remember, change the cookie to expire when the
167 // browser is closed. The reason this is necessary is because if the user
168 // specifies to remember, then logs out and logs back in without specifying to
169 // remember, the old "remember" cookie may remain and prevent the session from
170 // expiring when the browser is closed.
172 // It seems like I should be able to test for the old cookie, but for some reason when
173 // I read the lifetime value from session_get_cookie_params(), I always get '0'
174 // (i.e. expire when the browser is closed), even when there's a time expiration
176 if($_POST['remember']) {
177 new_cookie(31449600); // one year
180 new_cookie(0); // 0 means delete on browser exit
183 // if we haven't failed up this point, log them in.
185 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
186 authenticate_success($record, true, true);
190 function new_cookie($time) {
193 $time = $time + time();
195 $params = session_get_cookie_params();
196 setcookie(session_name(), session_id(), $time, $params['path'], $params['domain'], $params['secure'], isset($params['httponly']));