4 function nuke_session() {
5 unset($_SESSION['authenticated']);
6 unset($_SESSION['uid']);
7 unset($_SESSION['visitor_id']);
8 unset($_SESSION['administrator']);
9 unset($_SESSION['cid']);
10 unset($_SESSION['theme']);
11 unset($_SESSION['page_flags']);
20 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
22 if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
24 // process logout request
27 notice( t('Logged out.') . EOL);
28 goaway($a->get_baseurl());
31 if(x($_SESSION,'uid')) {
33 // already logged in user returning
35 $check = get_config('system','paranoia');
36 // extra paranoia - if the IP changed, log them out
37 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
39 goaway($a->get_baseurl());
42 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
43 intval($_SESSION['uid'])
48 goaway($a->get_baseurl());
51 // initialise user environment
54 $_SESSION['theme'] = $a->user['theme'];
55 $_SESSION['page_flags'] = $a->user['page-flags'];
56 if(strlen($a->user['timezone']))
57 date_default_timezone_set($a->user['timezone']);
59 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
61 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
62 intval($_SESSION['uid']));
65 $a->cid = $r[0]['id'];
66 $_SESSION['cid'] = $a->cid;
69 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
74 if(isset($_SESSION)) {
78 if((x($_POST,'password')) && strlen($_POST['password']))
79 $encrypted = hash('whirlpool',trim($_POST['password']));
81 if((x($_POST,'openid_url')) && strlen($_POST['openid_url'])) {
83 $noid = get_config('system','no_openid');
85 $openid_url = trim($_POST['openid_url']);
87 // validate_url alters the calling parameter
89 $temp_string = $openid_url;
91 // if it's an email address or doesn't resolve to a URL, fail.
93 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
95 notice( t('Login failed.') . EOL);
96 goaway($a->get_baseurl());
100 // Otherwise it's probably an openid.
102 require_once('library/openid.php');
103 $openid = new LightOpenID;
104 $openid->identity = $openid_url;
105 $_SESSION['openid'] = $openid_url;
107 $openid->returnUrl = $a->get_baseurl() . '/openid';
109 $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
114 goaway($openid->authUrl());
118 if($a->config['register_policy'] == REGISTER_CLOSED) {
120 notice( t('Login failed.') . EOL);
121 goaway($a->get_baseurl());
125 $_SESSION['register'] = 1;
126 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
127 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
128 goaway($openid->authUrl());
133 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
138 'username' => trim($_POST['openid_url']),
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 * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
162 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
163 dbesc(trim($_POST['openid_url'])),
164 dbesc(trim($_POST['openid_url'])),
171 if((! $record) || (! count($record))) {
172 logger('authenticate: failed login attempt: ' . trim($_POST['openid_url']));
173 notice( t('Login failed.') . EOL );
174 goaway($a->get_baseurl());
177 $_SESSION['uid'] = $record['uid'];
178 $_SESSION['theme'] = $record['theme'];
179 $_SESSION['authenticated'] = 1;
180 $_SESSION['page_flags'] = $record['page-flags'];
181 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
182 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
184 notice( t("Welcome back ") . $record['username'] . EOL);
186 if(strlen($a->user['timezone']))
187 date_default_timezone_set($a->user['timezone']);
189 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
190 intval($_SESSION['uid']));
193 $a->cid = $r[0]['id'];
194 $_SESSION['cid'] = $a->cid;
196 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1",
197 dbesc(datetime_convert()),
198 intval($_SESSION['uid'])
201 call_hooks('logged_in', $a->user);
203 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
204 if(($a->module !== 'home') && isset($_SESSION['return_url']))
205 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
209 // Returns an array of group id's this contact is a member of.
210 // This array will only contain group id's related to the uid of this
211 // DFRN contact. They are *not* neccessarily unique across the entire site.
214 if(! function_exists('init_groups_visitor')) {
215 function init_groups_visitor($contact_id) {
217 $r = q("SELECT `gid` FROM `group_member`
218 WHERE `contact-id` = %d ",
223 $groups[] = $rr['gid'];