4 require_once('include/security.php');
6 function nuke_session() {
7 unset($_SESSION['authenticated']);
8 unset($_SESSION['uid']);
9 unset($_SESSION['visitor_id']);
10 unset($_SESSION['administrator']);
11 unset($_SESSION['cid']);
12 unset($_SESSION['theme']);
13 unset($_SESSION['mobile-theme']);
14 unset($_SESSION['page_flags']);
15 unset($_SESSION['submanage']);
16 unset($_SESSION['my_url']);
17 unset($_SESSION['my_address']);
18 unset($_SESSION['addr']);
19 unset($_SESSION['return_url']);
28 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
30 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
32 // process logout request
33 call_hooks("logging_out");
35 info( t('Logged out.') . EOL);
39 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
40 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
41 intval($_SESSION['visitor_id'])
48 if(x($_SESSION,'uid')) {
50 // already logged in user returning
52 $check = get_config('system','paranoia');
53 // extra paranoia - if the IP changed, log them out
54 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
55 logger('Session address changed. Paranoid setting in effect, blocking session. '
56 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
61 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
62 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
63 intval($_SESSION['uid'])
71 authenticate_success($r[0]);
76 if(isset($_SESSION)) {
80 if((x($_POST,'password')) && strlen($_POST['password']))
81 $encrypted = hash('whirlpool',trim($_POST['password']));
83 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
84 (x($_POST,'username')) && strlen($_POST['username'])) {
86 $noid = get_config('system','no_openid');
88 $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
90 // validate_url alters the calling parameter
92 $temp_string = $openid_url;
94 // if it's an email address or doesn't resolve to a URL, fail.
96 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
98 notice( t('Login failed.') . EOL);
103 // Otherwise it's probably an openid.
106 require_once('library/openid.php');
107 $openid = new LightOpenID;
108 $openid->identity = $openid_url;
109 $_SESSION['openid'] = $openid_url;
111 $openid->returnUrl = $a->get_baseurl(true) . '/openid';
112 goaway($openid->authUrl());
113 } catch (Exception $e) {
114 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());
120 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
125 'username' => trim($_POST['username']),
126 'password' => trim($_POST['password']),
127 'authenticated' => 0,
128 'user_record' => null
133 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
134 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
135 * and later plugins should not interfere with an earlier one that succeeded.
139 call_hooks('authenticate', $addon_auth);
141 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
142 $record = $addon_auth['user_record'];
146 // process normal login request
148 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
149 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
150 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
151 dbesc(trim($_POST['username'])),
152 dbesc(trim($_POST['username'])),
159 if((! $record) || (! count($record))) {
160 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
161 notice( t('Login failed.') . EOL );
165 // if we haven't failed up this point, log them in.
167 authenticate_success($record, true, true);
171 // Returns an array of group id's this contact is a member of.
172 // This array will only contain group id's related to the uid of this
173 // DFRN contact. They are *not* neccessarily unique across the entire site.
176 if(! function_exists('init_groups_visitor')) {
177 function init_groups_visitor($contact_id) {
179 $r = q("SELECT `gid` FROM `group_member`
180 WHERE `contact-id` = %d ",
185 $groups[] = $rr['gid'];