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