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);
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 $member_since = strtotime($a->user['register_date']);
67 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
68 $_SESSION['new_member'] = true;
70 $_SESSION['new_member'] = false;
72 if(strlen($a->user['timezone'])) {
73 date_default_timezone_set($a->user['timezone']);
74 $a->timezone = $a->user['timezone'];
77 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
79 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
80 dbesc($a->user['password']),
81 dbesc($a->user['email'])
86 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
87 intval($_SESSION['uid']));
90 $a->cid = $r[0]['id'];
91 $_SESSION['cid'] = $a->cid;
94 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
99 if(isset($_SESSION)) {
103 if((x($_POST,'password')) && strlen($_POST['password']))
104 $encrypted = hash('whirlpool',trim($_POST['password']));
106 if((x($_POST,'openid_url')) && strlen($_POST['openid_url'])) {
108 $noid = get_config('system','no_openid');
110 $openid_url = trim($_POST['openid_url']);
112 // validate_url alters the calling parameter
114 $temp_string = $openid_url;
116 // if it's an email address or doesn't resolve to a URL, fail.
118 if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
120 notice( t('Login failed.') . EOL);
121 goaway($a->get_baseurl());
125 // Otherwise it's probably an openid.
127 require_once('library/openid.php');
128 $openid = new LightOpenID;
129 $openid->identity = $openid_url;
130 $_SESSION['openid'] = $openid_url;
132 $openid->returnUrl = $a->get_baseurl() . '/openid';
134 $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
139 goaway($openid->authUrl());
143 if($a->config['register_policy'] == REGISTER_CLOSED) {
145 notice( t('Login failed.') . EOL);
146 goaway($a->get_baseurl());
150 $_SESSION['register'] = 1;
151 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
152 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
153 goaway($openid->authUrl());
158 if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
163 'username' => trim($_POST['openid_url']),
164 'password' => trim($_POST['password']),
165 'authenticated' => 0,
166 'user_record' => null
171 * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
172 * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
173 * and later plugins should not interfere with an earlier one that succeeded.
177 call_hooks('authenticate', $addon_auth);
179 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
180 $record = $addon_auth['user_record'];
184 // process normal login request
186 $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' )
187 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
188 dbesc(trim($_POST['openid_url'])),
189 dbesc(trim($_POST['openid_url'])),
196 if((! $record) || (! count($record))) {
197 logger('authenticate: failed login attempt: ' . trim($_POST['openid_url']));
198 notice( t('Login failed.') . EOL );
199 goaway($a->get_baseurl());
202 $_SESSION['uid'] = $record['uid'];
203 $_SESSION['theme'] = $record['theme'];
204 $_SESSION['authenticated'] = 1;
205 $_SESSION['page_flags'] = $record['page-flags'];
206 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
207 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
211 if($a->user['login_date'] === '0000-00-00 00:00:00') {
212 $_SESSION['return_url'] = 'profile_photo/new';
213 $a->module = 'profile_photo';
214 info( t("Welcome ") . $a->user['username'] . EOL);
215 info( t('Please upload a profile photo.') . EOL);
218 info( t("Welcome back ") . $a->user['username'] . EOL);
221 $member_since = strtotime($a->user['register_date']);
222 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
223 $_SESSION['new_member'] = true;
225 $_SESSION['new_member'] = false;
227 if(strlen($a->user['timezone'])) {
228 date_default_timezone_set($a->user['timezone']);
229 $a->timezone = $a->user['timezone'];
232 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
233 dbesc($a->user['password']),
234 dbesc($a->user['email'])
240 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
241 intval($_SESSION['uid']));
244 $a->cid = $r[0]['id'];
245 $_SESSION['cid'] = $a->cid;
250 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
251 dbesc(datetime_convert()),
253 intval($_SESSION['uid'])
256 call_hooks('logged_in', $a->user);
258 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
259 if(($a->module !== 'home') && isset($_SESSION['return_url']))
260 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
264 // Returns an array of group id's this contact is a member of.
265 // This array will only contain group id's related to the uid of this
266 // DFRN contact. They are *not* neccessarily unique across the entire site.
269 if(! function_exists('init_groups_visitor')) {
270 function init_groups_visitor($contact_id) {
272 $r = q("SELECT `gid` FROM `group_member`
273 WHERE `contact-id` = %d ",
278 $groups[] = $rr['gid'];