]> git.mxchange.org Git - friendica.git/blob - include/auth.php
Merge remote-tracking branch 'upstream/master'
[friendica.git] / include / auth.php
1 <?php
2
3
4 require_once('include/security.php');
5 require_once('include/datetime.php');
6
7 function nuke_session() {
8         unset($_SESSION['authenticated']);
9         unset($_SESSION['uid']);
10         unset($_SESSION['visitor_id']);
11         unset($_SESSION['administrator']);
12         unset($_SESSION['cid']);
13         unset($_SESSION['theme']);
14         unset($_SESSION['mobile-theme']);
15         unset($_SESSION['page_flags']);
16         unset($_SESSION['submanage']);
17         unset($_SESSION['my_url']);
18         unset($_SESSION['my_address']);
19         unset($_SESSION['addr']);
20         unset($_SESSION['return_url']);
21 }
22
23
24 // login/logout 
25
26
27
28
29 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
30
31         if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
32         
33                 // process logout request
34                 call_hooks("logging_out");
35                 nuke_session();
36                 info( t('Logged out.') . EOL);
37                 goaway(z_root());
38         }
39
40         if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
41                 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
42                         intval($_SESSION['visitor_id'])
43                 );
44                 if(count($r)) {
45                         $a->contact = $r[0];
46                 }
47         }
48
49         if(x($_SESSION,'uid')) {
50
51                 // already logged in user returning
52
53                 $check = get_config('system','paranoia');
54                 // extra paranoia - if the IP changed, log them out
55                 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
56                         logger('Session address changed. Paranoid setting in effect, blocking session. ' 
57                                 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
58                         nuke_session();
59                         goaway(z_root());
60                 }
61
62                 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey` 
63                 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
64                         intval($_SESSION['uid'])
65                 );
66
67                 if(! count($r)) {
68                         nuke_session();
69                         goaway(z_root());
70                 }
71
72                 // Make sure to refresh the last login time for the user if the user
73                 // stays logged in for a long time, e.g. with "Remember Me"
74                 $login_refresh = false;
75                 if(! x($_SESSION['last_login_date'])) {
76                         $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
77                 }
78                 if( strcmp(datetime_convert('UTC','UTC','now - 12 hours'), $_SESSION['last_login_date']) > 0 ) {
79
80                         $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
81                         $login_refresh = true;
82                 }
83                 authenticate_success($r[0], false, false, $login_refresh);
84         }
85 }
86 else {
87
88         if(isset($_SESSION)) {
89                 nuke_session();
90         }
91
92         if((x($_POST,'password')) && strlen($_POST['password']))
93                 $encrypted = hash('whirlpool',trim($_POST['password']));
94         else {
95                 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
96                    (x($_POST,'username')) && strlen($_POST['username'])) {
97
98                         $noid = get_config('system','no_openid');
99
100                         $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
101
102                         // validate_url alters the calling parameter
103
104                         $temp_string = $openid_url;
105
106                         // if it's an email address or doesn't resolve to a URL, fail.
107
108                         if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
109                                 $a = get_app();
110                                 notice( t('Login failed.') . EOL);
111                                 goaway(z_root());
112                                 // NOTREACHED
113                         }
114
115                         // Otherwise it's probably an openid.
116
117                         try {
118                         require_once('library/openid.php');
119                         $openid = new LightOpenID;
120                         $openid->identity = $openid_url;
121                         $_SESSION['openid'] = $openid_url;
122                         $a = get_app();
123                         $openid->returnUrl = $a->get_baseurl(true) . '/openid'; 
124                         goaway($openid->authUrl());
125                         } catch (Exception $e) {
126                             notice( t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.').'<br /><br >'. t('The error message was:').' '.$e->getMessage());
127                         }
128                         // NOTREACHED
129                 }
130         }
131
132         if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
133
134                 $record = null;
135
136                 $addon_auth = array(
137                         'username' => trim($_POST['username']), 
138                         'password' => trim($_POST['password']),
139                         'authenticated' => 0,
140                         'user_record' => null
141                 );
142
143                 /**
144                  *
145                  * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
146                  * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
147                  * and later plugins should not interfere with an earlier one that succeeded.
148                  *
149                  */
150
151                 call_hooks('authenticate', $addon_auth);
152
153                 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
154                         $record = $addon_auth['user_record'];
155                 }
156                 else {
157
158                         // process normal login request
159
160                         $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`  
161                                 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) 
162                                 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
163                                 dbesc(trim($_POST['username'])),
164                                 dbesc(trim($_POST['username'])),
165                                 dbesc($encrypted)
166                         );
167                         if(count($r))
168                                 $record = $r[0];
169                 }
170
171                 if((! $record) || (! count($record))) {
172                         logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']); 
173                         notice( t('Login failed.') . EOL );
174                         goaway(z_root());
175                 }
176
177                 // If the user specified to remember the authentication, then change the cookie
178                 // to expire after one year (the default is when the browser is closed).
179                 // If the user did not specify to remember, change the cookie to expire when the
180                 // browser is closed. The reason this is necessary is because if the user
181                 // specifies to remember, then logs out and logs back in without specifying to
182                 // remember, the old "remember" cookie may remain and prevent the session from
183                 // expiring when the browser is closed.
184                 //
185                 // It seems like I should be able to test for the old cookie, but for some reason when
186                 // I read the lifetime value from session_get_cookie_params(), I always get '0'
187                 // (i.e. expire when the browser is closed), even when there's a time expiration
188                 // on the cookie
189                 if($_POST['remember']) {
190                         $old_sid = session_id();
191                         session_set_cookie_params('31449600'); // one year
192                         session_regenerate_id(false);
193
194                         q("UPDATE session SET sid = '%s' WHERE sid = '%s'", dbesc(session_id()), dbesc($old_sid));
195                 }
196                 else {
197                         $old_sid = session_id();
198                         session_set_cookie_params('0');
199                         session_regenerate_id(false);
200
201                         q("UPDATE session SET sid = '%s' WHERE sid = '%s'", dbesc(session_id()), dbesc($old_sid));
202                 }
203
204                 // if we haven't failed up this point, log them in.
205
206                 $_SESSION['last_login_date'] = datetime_convert('UTC','UTC');
207                 authenticate_success($record, true, true);
208         }
209 }
210
211