]> git.mxchange.org Git - friendica.git/blob - include/auth.php
Merge https://github.com/friendica/friendica into pull
[friendica.git] / include / auth.php
1 <?php
2
3
4 require_once('include/security.php');
5
6 function nuke_session() {
7         unset($_SESSION['authenticated']);
8         unset($_SESSION['uid']);
9         unset($_SESSION['visitor_id']);
10         unset($_SESSION['administrator']);
11         unset($_SESSION['cid']);
12         unset($_SESSION['theme']);
13         unset($_SESSION['mobile-theme']);
14         unset($_SESSION['page_flags']);
15         unset($_SESSION['submanage']);
16         unset($_SESSION['my_url']);
17         unset($_SESSION['my_address']);
18         unset($_SESSION['addr']);
19         unset($_SESSION['return_url']);
20 }
21
22
23 // login/logout 
24
25
26
27
28 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
29
30         if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
31         
32                 // process logout request
33                 call_hooks("logging_out");
34                 nuke_session();
35                 info( t('Logged out.') . EOL);
36                 goaway(z_root());
37         }
38
39         if(x($_SESSION,'visitor_id') && (! x($_SESSION,'uid'))) {
40                 $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
41                         intval($_SESSION['visitor_id'])
42                 );
43                 if(count($r)) {
44                         $a->contact = $r[0];
45                 }
46         }
47
48         if(x($_SESSION,'uid')) {
49
50                 // already logged in user returning
51
52                 $check = get_config('system','paranoia');
53                 // extra paranoia - if the IP changed, log them out
54                 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
55                         logger('Session address changed. Paranoid setting in effect, blocking session. ' 
56                                 . $_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
57                         nuke_session();
58                         goaway(z_root());
59                 }
60
61                 $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey` 
62                 FROM `user` WHERE `uid` = %d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
63                         intval($_SESSION['uid'])
64                 );
65
66                 if(! count($r)) {
67                         nuke_session();
68                         goaway(z_root());
69                 }
70
71                 authenticate_success($r[0]);
72         }
73 }
74 else {
75
76         if(isset($_SESSION)) {
77                 nuke_session();
78         }
79
80         if((x($_POST,'password')) && strlen($_POST['password']))
81                 $encrypted = hash('whirlpool',trim($_POST['password']));
82         else {
83                 if((x($_POST,'openid_url')) && strlen($_POST['openid_url']) ||
84                    (x($_POST,'username')) && strlen($_POST['username'])) {
85
86                         $noid = get_config('system','no_openid');
87
88                         $openid_url = trim((strlen($_POST['openid_url'])?$_POST['openid_url']:$_POST['username']) );
89
90                         // validate_url alters the calling parameter
91
92                         $temp_string = $openid_url;
93
94                         // if it's an email address or doesn't resolve to a URL, fail.
95
96                         if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
97                                 $a = get_app();
98                                 notice( t('Login failed.') . EOL);
99                                 goaway(z_root());
100                                 // NOTREACHED
101                         }
102
103                         // Otherwise it's probably an openid.
104
105                         try {
106                         require_once('library/openid.php');
107                         $openid = new LightOpenID;
108                         $openid->identity = $openid_url;
109                         $_SESSION['openid'] = $openid_url;
110                         $a = get_app();
111                         $openid->returnUrl = $a->get_baseurl(true) . '/openid'; 
112                         goaway($openid->authUrl());
113                         } catch (Exception $e) {
114                             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());
115                         }
116                         // NOTREACHED
117                 }
118         }
119
120         if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
121
122                 $record = null;
123
124                 $addon_auth = array(
125                         'username' => trim($_POST['username']), 
126                         'password' => trim($_POST['password']),
127                         'authenticated' => 0,
128                         'user_record' => null
129                 );
130
131                 /**
132                  *
133                  * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
134                  * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
135                  * and later plugins should not interfere with an earlier one that succeeded.
136                  *
137                  */
138
139                 call_hooks('authenticate', $addon_auth);
140
141                 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
142                         $record = $addon_auth['user_record'];
143                 }
144                 else {
145
146                         // process normal login request
147
148                         $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`  
149                                 FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) 
150                                 AND `password` = '%s' AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
151                                 dbesc(trim($_POST['username'])),
152                                 dbesc(trim($_POST['username'])),
153                                 dbesc($encrypted)
154                         );
155                         if(count($r))
156                                 $record = $r[0];
157                 }
158
159                 if((! $record) || (! count($record))) {
160                         logger('authenticate: failed login attempt: ' . notags(trim($_POST['username'])) . ' from IP ' . $_SERVER['REMOTE_ADDR']); 
161                         notice( t('Login failed.') . EOL );
162                         goaway(z_root());
163                 }
164
165                 // if we haven't failed up this point, log them in.
166
167                 authenticate_success($record, true, true);
168         }
169 }
170
171