]> git.mxchange.org Git - friendica.git/blob - include/auth.php
fix typo
[friendica.git] / include / auth.php
1 <?php
2
3
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']);
12 }
13
14
15 // login/logout 
16
17
18
19
20 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
21
22         if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
23         
24                 // process logout request
25
26                 nuke_session();
27                 info( t('Logged out.') . EOL);
28                 goaway(z_root());
29         }
30
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'])
34                 );
35                 if(count($r)) {
36                         $a->contact = $r[0];
37                 }
38         }
39
40         if(x($_SESSION,'uid')) {
41
42                 // already logged in user returning
43
44                 $check = get_config('system','paranoia');
45                 // extra paranoia - if the IP changed, log them out
46                 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
47                         nuke_session();
48                         goaway(z_root());
49                 }
50
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'])
54                 );
55
56                 if(! count($r)) {
57                         nuke_session();
58                         goaway(z_root());
59                 }
60
61                 // initialise user environment
62
63                 $a->user = $r[0];
64                 $_SESSION['theme'] = $a->user['theme'];
65                 $_SESSION['page_flags'] = $a->user['page-flags'];
66
67                 $member_since = strtotime($a->user['register_date']);
68                 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
69                         $_SESSION['new_member'] = true;
70                 else
71                         $_SESSION['new_member'] = false;
72
73                 if(strlen($a->user['timezone'])) {
74                         date_default_timezone_set($a->user['timezone']);
75                         $a->timezone = $a->user['timezone'];
76                 }
77
78                 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
79
80                 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
81                         dbesc($a->user['password']),
82                         dbesc($a->user['email'])
83                 );
84                 if(count($r))
85                         $a->identities = $r;
86
87                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
88                         intval($_SESSION['uid']));
89                 if(count($r)) {
90                         $a->contact = $r[0];
91                         $a->cid = $r[0]['id'];
92                         $_SESSION['cid'] = $a->cid;
93
94                 }
95                 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
96         }
97 }
98 else {
99
100         if(isset($_SESSION)) {
101                 nuke_session();
102         }
103
104         if((x($_POST,'password')) && strlen($_POST['password']))
105                 $encrypted = hash('whirlpool',trim($_POST['password']));
106         else {
107                 if((x($_POST,'openid_url')) && strlen($_POST['openid_url'])) {
108
109                         $noid = get_config('system','no_openid');
110
111                         $openid_url = trim($_POST['openid_url']);
112
113                         // validate_url alters the calling parameter
114
115                         $temp_string = $openid_url;
116
117                         // if it's an email address or doesn't resolve to a URL, fail.
118
119                         if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
120                                 $a = get_app();
121                                 notice( t('Login failed.') . EOL);
122                                 goaway(z_root());
123                                 // NOTREACHED
124                         }
125
126                         // Otherwise it's probably an openid.
127
128                         require_once('library/openid.php');
129                         $openid = new LightOpenID;
130                         $openid->identity = $openid_url;
131                         $_SESSION['openid'] = $openid_url;
132                         $a = get_app();
133                         $openid->returnUrl = $a->get_baseurl() . '/openid'; 
134
135                         $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
136                                 dbesc($openid_url)
137                         );
138                         if(count($r)) { 
139                                 // existing account
140                                 goaway($openid->authUrl());
141                                 // NOTREACHED   
142                         }
143                         else {
144                                 if($a->config['register_policy'] == REGISTER_CLOSED) {
145                                         $a = get_app();
146                                         notice( t('Login failed.') . EOL);
147                                         goaway(z_root());
148                                         // NOTREACHED
149                                 }
150                                 // new account
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());
155                                 // NOTREACHED   
156                         }
157                 }
158         }
159         if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
160
161                 $record = null;
162
163                 $addon_auth = array(
164                         'username' => trim($_POST['openid_url']), 
165                         'password' => trim($_POST['password']),
166                         'authenticated' => 0,
167                         'user_record' => null
168                 );
169
170                 /**
171                  *
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.
175                  *
176                  */
177
178                 call_hooks('authenticate', $addon_auth);
179
180                 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
181                         $record = $addon_auth['user_record'];
182                 }
183                 else {
184
185                         // process normal login request
186
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'])),
192                                 dbesc($encrypted)
193                         );
194                         if(count($r))
195                                 $record = $r[0];
196                 }
197
198                 if((! $record) || (! count($record))) {
199                         logger('authenticate: failed login attempt: ' . trim($_POST['openid_url'])); 
200                         notice( t('Login failed.') . EOL );
201                         goaway(z_root());
202                 }
203
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'];
210
211                 $a->user = $record;
212
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);
218                 }
219                 else
220                         info( t("Welcome back ") . $a->user['username'] . EOL);
221
222
223                 $member_since = strtotime($a->user['register_date']);
224                 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
225                         $_SESSION['new_member'] = true;
226                 else
227                         $_SESSION['new_member'] = false;
228
229                 if(strlen($a->user['timezone'])) {
230                         date_default_timezone_set($a->user['timezone']);
231                         $a->timezone = $a->user['timezone'];
232                 }
233
234                 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
235                         dbesc($a->user['password']),
236                         dbesc($a->user['email'])
237                 );
238                 if(count($r))
239                         $a->identities = $r;
240
241
242                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
243                         intval($_SESSION['uid']));
244                 if(count($r)) {
245                         $a->contact = $r[0];
246                         $a->cid = $r[0]['id'];
247                         $_SESSION['cid'] = $a->cid;
248                 }
249
250                 $l = get_language();
251
252                 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
253                         dbesc(datetime_convert()),
254                         dbesc($l),
255                         intval($_SESSION['uid'])
256                 );
257
258                 call_hooks('logged_in', $a->user);
259
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']);
263         }
264 }
265
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. 
269
270
271 if(! function_exists('init_groups_visitor')) {
272 function init_groups_visitor($contact_id) {
273         $groups = array();
274         $r = q("SELECT `gid` FROM `group_member` 
275                 WHERE `contact-id` = %d ",
276                 intval($contact_id)
277         );
278         if(count($r)) {
279                 foreach($r as $rr)
280                         $groups[] = $rr['gid'];
281         }
282         return $groups;
283 }}
284
285