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