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 info( t('Logged out.') . EOL);
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'])) {
51 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
52 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
53 intval($_SESSION['uid'])
61 // initialise user environment
64 $_SESSION['theme'] = $a->user['theme'];
65 $_SESSION['page_flags'] = $a->user['page-flags'];
67 $member_since = strtotime($a->user['register_date']);
68 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
69 $_SESSION['new_member'] = true;
71 $_SESSION['new_member'] = false;
73 if(strlen($a->user['timezone'])) {
74 date_default_timezone_set($a->user['timezone']);
75 $a->timezone = $a->user['timezone'];
78 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
80 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
81 dbesc($a->user['password']),
82 dbesc($a->user['email'])
87 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
88 intval($_SESSION['uid']));
91 $a->cid = $r[0]['id'];
92 $_SESSION['cid'] = $a->cid;
95 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
100 if(isset($_SESSION)) {
104 if((x($_POST,'password')) && strlen($_POST['password']))
105 $encrypted = hash('whirlpool',trim($_POST['password']));
107 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
108 (x($_POST,'username')) && strlen($_POST['username'])) {
110 $noid = get_config('system','no_openid');
112 $openid_url = trim( (strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
114 // validate_url alters the calling parameter
116 $temp_string = $openid_url;
118 // if it's an email address or doesn't resolve to a URL, fail.
120 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
122 notice( t('Login failed.') . EOL);
127 // Otherwise it's probably an openid.
129 require_once('library/openid.php');
130 $openid = new LightOpenID;
131 $openid->identity = $openid_url;
132 $_SESSION['openid'] = $openid_url;
134 $openid->returnUrl = $a->get_baseurl() . '/openid';
136 $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
141 goaway($openid->authUrl());
145 if($a->config['register_policy'] == REGISTER_CLOSED) {
147 notice( t('Login failed.') . EOL);
152 $_SESSION['register'] = 1;
153 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
154 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
155 goaway($openid->authUrl());
160 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
165 'username' => trim($_POST['username']),
166 'password' => trim($_POST['password']),
167 'authenticated' => 0,
168 'user_record' => null
173 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
174 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
175 * and later plugins should not interfere with an earlier one that succeeded.
179 call_hooks('authenticate', $addon_auth);
181 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
182 $record = $addon_auth['user_record'];
186 // process normal login request
188 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
189 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
190 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
191 dbesc(trim($_POST['username'])),
192 dbesc(trim($_POST['username'])),
199 if((! $record) || (! count($record))) {
200 logger('authenticate: failed login attempt: ' . trim($_POST['username']));
201 notice( t('Login failed.') . EOL );
205 $_SESSION['uid'] = $record['uid'];
206 $_SESSION['theme'] = $record['theme'];
207 $_SESSION['authenticated'] = 1;
208 $_SESSION['page_flags'] = $record['page-flags'];
209 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
210 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
214 if($a->user['login_date'] === '0000-00-00 00:00:00') {
215 $_SESSION['return_url'] = 'profile_photo/new';
216 $a->module = 'profile_photo';
217 info( t("Welcome ") . $a->user['username'] . EOL);
218 info( t('Please upload a profile photo.') . EOL);
221 info( t("Welcome back ") . $a->user['username'] . EOL);
224 $member_since = strtotime($a->user['register_date']);
225 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
226 $_SESSION['new_member'] = true;
228 $_SESSION['new_member'] = false;
230 if(strlen($a->user['timezone'])) {
231 date_default_timezone_set($a->user['timezone']);
232 $a->timezone = $a->user['timezone'];
235 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
236 dbesc($a->user['password']),
237 dbesc($a->user['email'])
243 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
244 intval($_SESSION['uid']));
247 $a->cid = $r[0]['id'];
248 $_SESSION['cid'] = $a->cid;
253 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
254 dbesc(datetime_convert()),
256 intval($_SESSION['uid'])
259 call_hooks('logged_in', $a->user);
261 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
262 if(($a->module !== 'home') && isset($_SESSION['return_url']))
263 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
267 // Returns an array of group id's this contact is a member of.
268 // This array will only contain group id's related to the uid of this
269 // DFRN contact. They are *not* neccessarily unique across the entire site.
272 if(! function_exists('init_groups_visitor')) {
273 function init_groups_visitor($contact_id) {
275 $r = q("SELECT `gid` FROM `group_member`
276 WHERE `contact-id` = %d ",
281 $groups[] = $rr['gid'];