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['page_flags']);
14 unset($_SESSION['submanage']);
15 unset($_SESSION['my_url']);
16 unset($_SESSION['my_address']);
17 unset($_SESSION['addr']);
18 unset($_SESSION['return_url']);
19 unset($_SESSION['theme']);
20 unset($_SESSION['page_flags']);
29 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
31 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
33 // process logout request
34 call_hooks("logging_out");
36 info( t('Logged out.') . EOL);
40 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
41 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
42 intval($_SESSION['visitor_id'])
49 if(x($_SESSION,'uid')) {
51 // already logged in user returning
53 $check = get_config('system','paranoia');
54 // extra paranoia - if the IP changed, log them out
55 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
60 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
61 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
62 intval($_SESSION['uid'])
70 authenticate_success($r[0]);
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 `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 we haven't failed up this point, log them in.
166 authenticate_success($record, true, true);
170 // Returns an array of group id's this contact is a member of.
171 // This array will only contain group id's related to the uid of this
172 // DFRN contact. They are *not* neccessarily unique across the entire site.
175 if(! function_exists('init_groups_visitor')) {
176 function init_groups_visitor($contact_id) {
178 $r = q("SELECT `gid` FROM `group_member`
179 WHERE `contact-id` = %d ",
184 $groups[] = $rr['gid'];