4 require_once('include/security.php');
5 require_once('include/datetime.php');
7 function nuke_session() {
8 if (get_config('system', 'disable_database_session')) {
13 new_cookie(0); // make sure cookie is deleted on browser close, as a security measure
15 unset($_SESSION['authenticated']);
16 unset($_SESSION['uid']);
17 unset($_SESSION['visitor_id']);
18 unset($_SESSION['administrator']);
19 unset($_SESSION['cid']);
20 unset($_SESSION['theme']);
21 unset($_SESSION['mobile-theme']);
22 unset($_SESSION['page_flags']);
23 unset($_SESSION['submanage']);
24 unset($_SESSION['my_url']);
25 unset($_SESSION['my_address']);
26 unset($_SESSION['addr']);
27 unset($_SESSION['return_url']);
37 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
39 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
41 // process logout request
42 call_hooks("logging_out");
44 info( t('Logged out.') . EOL);
48 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
49 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
50 intval($_SESSION['visitor_id'])
57 if(x($_SESSION,'uid')) {
59 // already logged in user returning
61 $check = get_config('system','paranoia');
62 // extra paranoia - if the IP changed, log them out
63 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
64 logger('Session address changed. Paranoid setting in effect, blocking session. '
65 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
70 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
71 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
72 intval($_SESSION['uid'])
80 // Make sure to refresh the last login time for the user if the user
81 // stays logged in for a long time, e.g. with "Remember Me"
82 $login_refresh = false;
83 if(! x($_SESSION['last_login_date'])) {
84 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
86 if( strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0 ) {
88 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
89 $login_refresh = true;
91 authenticate_success($r[0], false, false, $login_refresh);
96 if(isset($_SESSION)) {
100 if((x($_POST,'password')) && strlen($_POST['password']))
101 $encrypted = hash('whirlpool',trim($_POST['password']));
103 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
104 (x($_POST,'username')) && strlen($_POST['username'])) {
106 $noid = get_config('system','no_openid');
108 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
110 // validate_url alters the calling parameter
112 $temp_string = $openid_url;
114 // if it's an email address or doesn't resolve to a URL, fail.
116 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
118 notice( t('Login failed.') . EOL);
123 // Otherwise it's probably an openid.
126 require_once('library/openid.php');
127 $openid = new LightOpenID;
128 $openid->identity = $openid_url;
129 $_SESSION['openid'] = $openid_url;
131 $openid->returnUrl = $a->get_baseurl(true) . '/openid';
132 goaway($openid->authUrl());
133 } catch (Exception $e) {
134 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());
140 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
145 'username' => trim($_POST['username']),
146 'password' => trim($_POST['password']),
147 'authenticated' => 0,
148 'user_record' => null
153 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
154 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
155 * and later plugins should not interfere with an earlier one that succeeded.
159 call_hooks('authenticate', $addon_auth);
161 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
162 $record = $addon_auth['user_record'];
166 // process normal login request
168 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
169 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
170 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
171 dbesc(trim($_POST['username'])),
172 dbesc(trim($_POST['username'])),
179 if((! $record) || (! count($record))) {
180 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
181 notice( t('Login failed.') . EOL );
185 // If the user specified to remember the authentication, then change the cookie
186 // to expire after one year (the default is when the browser is closed).
187 // If the user did not specify to remember, change the cookie to expire when the
188 // browser is closed. The reason this is necessary is because if the user
189 // specifies to remember, then logs out and logs back in without specifying to
190 // remember, the old "remember" cookie may remain and prevent the session from
191 // expiring when the browser is closed.
193 // It seems like I should be able to test for the old cookie, but for some reason when
194 // I read the lifetime value from session_get_cookie_params(), I always get '0'
195 // (i.e. expire when the browser is closed), even when there's a time expiration
197 if($_POST['remember']) {
198 new_cookie(31449600); // one year
201 new_cookie(0); // 0 means delete on browser exit
204 // if we haven't failed up this point, log them in.
206 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
207 authenticate_success($record, true, true);
211 function new_cookie($time) {
212 if (!get_config('system', 'disable_database_session'))
213 $old_sid = session_id();
215 session_set_cookie_params($time);
217 if (!get_config('system', 'disable_database_session')) {
218 session_regenerate_id(false);
219 q("UPDATE session SET sid = '%s' WHERE sid = '%s'", dbesc(session_id()), dbesc($old_sid));