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']);
22 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
24 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
26 // process logout request
29 info( t('Logged out.') . EOL);
33 if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
34 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
35 intval($_SESSION['visitor_id'])
42 if(x($_SESSION,'uid')) {
44 // already logged in user returning
46 $check = get_config('system','paranoia');
47 // extra paranoia - if the IP changed, log them out
48 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
53 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
54 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
55 intval($_SESSION['uid'])
63 authenticate_success($r[0]);
68 if(isset($_SESSION)) {
72 if((x($_POST,'password')) && strlen($_POST['password']))
73 $encrypted = hash('whirlpool',trim($_POST['password']));
75 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
76 (x($_POST,'username')) && strlen($_POST['username'])) {
78 $noid = get_config('system','no_openid');
80 $openid_url = trim( (strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
82 // validate_url alters the calling parameter
84 $temp_string = $openid_url;
86 // if it's an email address or doesn't resolve to a URL, fail.
88 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
90 notice( t('Login failed.') . EOL);
95 // Otherwise it's probably an openid.
97 require_once('library/openid.php');
98 $openid = new LightOpenID;
99 $openid->identity = $openid_url;
100 $_SESSION['openid'] = $openid_url;
102 $openid->returnUrl = $a->get_baseurl() . '/openid';
104 $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
109 goaway($openid->authUrl());
113 if($a->config['register_policy'] == REGISTER_CLOSED) {
115 notice( t('Login failed.') . EOL);
121 $_SESSION['register'] = 1;
122 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
123 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
124 goaway($openid->authUrl());
125 } catch (Exception $e) {
126 // if the OpenID is misspelled we land here
127 notice( t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.<br /><br />The error message was: ').$e->getMessage() );
133 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
138 'username' => trim($_POST['username']),
139 'password' => trim($_POST['password']),
140 'authenticated' => 0,
141 'user_record' => null
146 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
147 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
148 * and later plugins should not interfere with an earlier one that succeeded.
152 call_hooks('authenticate', $addon_auth);
154 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
155 $record = $addon_auth['user_record'];
159 // process normal login request
161 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
162 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
163 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
164 dbesc(trim($_POST['username'])),
165 dbesc(trim($_POST['username'])),
172 if((! $record) || (! count($record))) {
173 logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])));
174 notice( t('Login failed.') . EOL );
178 // if we haven't failed up this point, log them in.
180 authenticate_success($record, true, true);
184 // Returns an array of group id's this contact is a member of.
185 // This array will only contain group id's related to the uid of this
186 // DFRN contact. They are *not* neccessarily unique across the entire site.
189 if(! function_exists('init_groups_visitor')) {
190 function init_groups_visitor($contact_id) {
192 $r = q("SELECT `gid` FROM `group_member`
193 WHERE `contact-id` = %d ",
198 $groups[] = $rr['gid'];