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,'visitor_id') && (! x($_SESSION,'uid'))) {
32 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
33 intval($_SESSION['visitor_id'])
40 if(x($_SESSION,'uid')) {
42 // already logged in user returning
44 $check = get_config('system','paranoia');
45 // extra paranoia - if the IP changed, log them out
46 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
48 goaway($a->get_baseurl());
51 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
52 intval($_SESSION['uid'])
57 goaway($a->get_baseurl());
60 // initialise user environment
63 $_SESSION['theme'] = $a->user['theme'];
64 $_SESSION['page_flags'] = $a->user['page-flags'];
66 if(strlen($a->user['timezone'])) {
67 date_default_timezone_set($a->user['timezone']);
68 $a->timezone = $a->user['timezone'];
71 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
73 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
74 dbesc($a->user['password']),
75 dbesc($a->user['email'])
80 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
81 intval($_SESSION['uid']));
84 $a->cid = $r[0]['id'];
85 $_SESSION['cid'] = $a->cid;
88 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
93 if(isset($_SESSION)) {
97 if((x($_POST,'password')) && strlen($_POST['password']))
98 $encrypted = hash('whirlpool',trim($_POST['password']));
100 if((x($_POST,'openid_url')) && strlen($_POST['openid_url'])) {
102 $noid = get_config('system','no_openid');
104 $openid_url = trim($_POST['openid_url']);
106 // validate_url alters the calling parameter
108 $temp_string = $openid_url;
110 // if it's an email address or doesn't resolve to a URL, fail.
112 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
114 notice( t('Login failed.') . EOL);
115 goaway($a->get_baseurl());
119 // Otherwise it's probably an openid.
121 require_once('library/openid.php');
122 $openid = new LightOpenID;
123 $openid->identity = $openid_url;
124 $_SESSION['openid'] = $openid_url;
126 $openid->returnUrl = $a->get_baseurl() . '/openid';
128 $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
133 goaway($openid->authUrl());
137 if($a->config['register_policy'] == REGISTER_CLOSED) {
139 notice( t('Login failed.') . EOL);
140 goaway($a->get_baseurl());
144 $_SESSION['register'] = 1;
145 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
146 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
147 goaway($openid->authUrl());
152 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
157 'username' => trim($_POST['openid_url']),
158 'password' => trim($_POST['password']),
159 'authenticated' => 0,
160 'user_record' => null
165 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
166 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
167 * and later plugins should not interfere with an earlier one that succeeded.
171 call_hooks('authenticate', $addon_auth);
173 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
174 $record = $addon_auth['user_record'];
178 // process normal login request
180 $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
181 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
182 dbesc(trim($_POST['openid_url'])),
183 dbesc(trim($_POST['openid_url'])),
190 if((! $record) || (! count($record))) {
191 logger('authenticate: failed login attempt: ' . trim($_POST['openid_url']));
192 notice( t('Login failed.') . EOL );
193 goaway($a->get_baseurl());
196 $_SESSION['uid'] = $record['uid'];
197 $_SESSION['theme'] = $record['theme'];
198 $_SESSION['authenticated'] = 1;
199 $_SESSION['page_flags'] = $record['page-flags'];
200 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
201 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
205 if($a->user['login_date'] === '0000-00-00 00:00:00') {
206 $_SESSION['return_url'] = 'profile_photo/new';
207 $a->module = 'profile_photo';
208 notice( t("Welcome ") . $a->user['username'] . EOL);
209 notice( t('Please upload a profile photo.') . EOL);
212 notice( t("Welcome back ") . $a->user['username'] . EOL);
214 if(strlen($a->user['timezone'])) {
215 date_default_timezone_set($a->user['timezone']);
216 $a->timezone = $a->user['timezone'];
219 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
220 dbesc($a->user['password']),
221 dbesc($a->user['email'])
227 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
228 intval($_SESSION['uid']));
231 $a->cid = $r[0]['id'];
232 $_SESSION['cid'] = $a->cid;
237 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
238 dbesc(datetime_convert()),
240 intval($_SESSION['uid'])
243 call_hooks('logged_in', $a->user);
245 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
246 if(($a->module !== 'home') && isset($_SESSION['return_url']))
247 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
251 // Returns an array of group id's this contact is a member of.
252 // This array will only contain group id's related to the uid of this
253 // DFRN contact. They are *not* neccessarily unique across the entire site.
256 if(! function_exists('init_groups_visitor')) {
257 function init_groups_visitor($contact_id) {
259 $r = q("SELECT `gid` FROM `group_member`
260 WHERE `contact-id` = %d ",
265 $groups[] = $rr['gid'];