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') {
137 'name' => trim($_POST['openid_url']),
138 'password' => trim($_POST['password']),
144 * A plugin indicates successful login by setting 'authenticated' to non-zero value
145 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
146 * and later plugins should not interfere with an earlier one that succeeded.
150 call_hooks('authenticate', $addon_auth);
152 if(! $addon_auth['authenticated']) {
153 // process login request
155 $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
156 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
157 dbesc(trim($_POST['openid_url'])),
158 dbesc(trim($_POST['openid_url'])),
161 if(($r === false) || (! count($r))) {
162 notice( t('Login failed.') . EOL );
163 goaway($a->get_baseurl());
167 $_SESSION['uid'] = $r[0]['uid'];
168 $_SESSION['theme'] = $r[0]['theme'];
169 $_SESSION['authenticated'] = 1;
170 $_SESSION['page_flags'] = $r[0]['page-flags'];
171 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $r[0]['nickname'];
172 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
174 notice( t("Welcome back ") . $r[0]['username'] . EOL);
176 if(strlen($a->user['timezone']))
177 date_default_timezone_set($a->user['timezone']);
179 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
180 intval($_SESSION['uid']));
183 $a->cid = $r[0]['id'];
184 $_SESSION['cid'] = $a->cid;
186 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1",
187 dbesc(datetime_convert()),
188 intval($_SESSION['uid'])
191 call_hooks('logged_in', $a->user);
193 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
194 if(($a->module !== 'home') && isset($_SESSION['return_url']))
195 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
199 // Returns an array of group id's this contact is a member of.
200 // This array will only contain group id's related to the uid of this
201 // DFRN contact. They are *not* neccessarily unique across the entire site.
204 if(! function_exists('init_groups_visitor')) {
205 function init_groups_visitor($contact_id) {
207 $r = q("SELECT `gid` FROM `group_member`
208 WHERE `contact-id` = %d ",
213 $groups[] = $rr['gid'];