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