]> git.mxchange.org Git - friendica.git/blob - include/auth.php
string update
[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                    (x($_POST,'username')) && strlen($_POST['username'])) {
109
110                         $noid = get_config('system','no_openid');
111
112                         $openid_url = trim(  (strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
113
114                         // validate_url alters the calling parameter
115
116                         $temp_string = $openid_url;
117
118                         // if it's an email address or doesn't resolve to a URL, fail.
119
120                         if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
121                                 $a = get_app();
122                                 notice( t('Login failed.') . EOL);
123                                 goaway(z_root());
124                                 // NOTREACHED
125                         }
126
127                         // Otherwise it's probably an openid.
128
129                         require_once('library/openid.php');
130                         $openid = new LightOpenID;
131                         $openid->identity = $openid_url;
132                         $_SESSION['openid'] = $openid_url;
133                         $a = get_app();
134                         $openid->returnUrl = $a->get_baseurl() . '/openid'; 
135
136                         $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
137                                 dbesc($openid_url)
138                         );
139                         if(count($r)) { 
140                                 // existing account
141                                 goaway($openid->authUrl());
142                                 // NOTREACHED   
143                         }
144                         else {
145                                 if($a->config['register_policy'] == REGISTER_CLOSED) {
146                                         $a = get_app();
147                                         notice( t('Login failed.') . EOL);
148                                         goaway(z_root());
149                                         // NOTREACHED
150                                 }
151                                 // new account
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());
156                                 // NOTREACHED   
157                         }
158                 }
159         }
160         if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
161
162                 $record = null;
163
164                 $addon_auth = array(
165                         'username' => trim($_POST['username']), 
166                         'password' => trim($_POST['password']),
167                         'authenticated' => 0,
168                         'user_record' => null
169                 );
170
171                 /**
172                  *
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.
176                  *
177                  */
178
179                 call_hooks('authenticate', $addon_auth);
180
181                 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
182                         $record = $addon_auth['user_record'];
183                 }
184                 else {
185
186                         // process normal login request
187
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'])),
193                                 dbesc($encrypted)
194                         );
195                         if(count($r))
196                                 $record = $r[0];
197                 }
198
199                 if((! $record) || (! count($record))) {
200                         logger('authenticate: failed login attempt: ' . trim($_POST['username'])); 
201                         notice( t('Login failed.') . EOL );
202                         goaway(z_root());
203                 }
204
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'];
211
212                 $a->user = $record;
213
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);
219                 }
220                 else
221                         info( t("Welcome back ") . $a->user['username'] . EOL);
222
223
224                 $member_since = strtotime($a->user['register_date']);
225                 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
226                         $_SESSION['new_member'] = true;
227                 else
228                         $_SESSION['new_member'] = false;
229
230                 if(strlen($a->user['timezone'])) {
231                         date_default_timezone_set($a->user['timezone']);
232                         $a->timezone = $a->user['timezone'];
233                 }
234
235                 $r = q("SELECT `uid`,`username` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
236                         dbesc($a->user['password']),
237                         dbesc($a->user['email'])
238                 );
239                 if(count($r))
240                         $a->identities = $r;
241
242
243                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
244                         intval($_SESSION['uid']));
245                 if(count($r)) {
246                         $a->contact = $r[0];
247                         $a->cid = $r[0]['id'];
248                         $_SESSION['cid'] = $a->cid;
249                 }
250
251                 $l = get_language();
252
253                 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
254                         dbesc(datetime_convert()),
255                         dbesc($l),
256                         intval($_SESSION['uid'])
257                 );
258
259                 call_hooks('logged_in', $a->user);
260
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']);
264         }
265 }
266
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. 
270
271
272 if(! function_exists('init_groups_visitor')) {
273 function init_groups_visitor($contact_id) {
274         $groups = array();
275         $r = q("SELECT `gid` FROM `group_member` 
276                 WHERE `contact-id` = %d ",
277                 intval($contact_id)
278         );
279         if(count($r)) {
280                 foreach($r as $rr)
281                         $groups[] = $rr['gid'];
282         }
283         return $groups;
284 }}
285
286