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