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