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'])) {
109 $noid = get_config('system','no_openid');
111 $openid_url = trim($_POST['openid_url']);
113 // validate_url alters the calling parameter
115 $temp_string = $openid_url;
117 // if it's an email address or doesn't resolve to a URL, fail.
119 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
121 notice( t('Login failed.') . EOL);
126 // Otherwise it's probably an openid.
128 require_once('library/openid.php');
129 $openid = new LightOpenID;
130 $openid->identity = $openid_url;
131 $_SESSION['openid'] = $openid_url;
133 $openid->returnUrl = $a->get_baseurl() . '/openid';
135 $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
140 goaway($openid->authUrl());
144 if($a->config['register_policy'] == REGISTER_CLOSED) {
146 notice( t('Login failed.') . EOL);
151 $_SESSION['register'] = 1;
152 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
153 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
154 goaway($openid->authUrl());
159 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
164 'username' => trim($_POST['openid_url']),
165 'password' => trim($_POST['password']),
166 'authenticated' => 0,
167 'user_record' => null
172 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
173 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
174 * and later plugins should not interfere with an earlier one that succeeded.
178 call_hooks('authenticate', $addon_auth);
180 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
181 $record = $addon_auth['user_record'];
185 // process normal login request
187 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
188 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
189 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
190 dbesc(trim($_POST['openid_url'])),
191 dbesc(trim($_POST['openid_url'])),
198 if((! $record) || (! count($record))) {
199 logger('authenticate: failed login attempt: ' . trim($_POST['openid_url']));
200 notice( t('Login failed.') . EOL );
204 $_SESSION['uid'] = $record['uid'];
205 $_SESSION['theme'] = $record['theme'];
206 $_SESSION['authenticated'] = 1;
207 $_SESSION['page_flags'] = $record['page-flags'];
208 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
209 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
213 if($a->user['login_date'] === '0000-00-00 00:00:00') {
214 $_SESSION['return_url'] = 'profile_photo/new';
215 $a->module = 'profile_photo';
216 info( t("Welcome ") . $a->user['username'] . EOL);
217 info( t('Please upload a profile photo.') . EOL);
220 info( t("Welcome back ") . $a->user['username'] . EOL);
223 $member_since = strtotime($a->user['register_date']);
224 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
225 $_SESSION['new_member'] = true;
227 $_SESSION['new_member'] = false;
229 if(strlen($a->user['timezone'])) {
230 date_default_timezone_set($a->user['timezone']);
231 $a->timezone = $a->user['timezone'];
234 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
235 dbesc($a->user['password']),
236 dbesc($a->user['email'])
242 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
243 intval($_SESSION['uid']));
246 $a->cid = $r[0]['id'];
247 $_SESSION['cid'] = $a->cid;
252 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
253 dbesc(datetime_convert()),
255 intval($_SESSION['uid'])
258 call_hooks('logged_in', $a->user);
260 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
261 if(($a->module !== 'home') && isset($_SESSION['return_url']))
262 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
266 // Returns an array of group id's this contact is a member of.
267 // This array will only contain group id's related to the uid of this
268 // DFRN contact. They are *not* neccessarily unique across the entire site.
271 if(! function_exists('init_groups_visitor')) {
272 function init_groups_visitor($contact_id) {
274 $r = q("SELECT `gid` FROM `group_member`
275 WHERE `contact-id` = %d ",
280 $groups[] = $rr['gid'];