]> git.mxchange.org Git - friendica.git/blob - include/auth.php
a0244571b3fec5703c99a7d5832cc2c51f1f374e
[friendica.git] / include / auth.php
1 <?php
2
3
4 function nuke_session() {
5         unset($_SESSION['authenticated']);
6         unset($_SESSION['uid']);
7         unset($_SESSION['visitor_id']);
8         unset($_SESSION['administrator']);
9         unset($_SESSION['cid']);
10         unset($_SESSION['theme']);
11         unset($_SESSION['page_flags']);
12 }
13
14
15 // login/logout 
16
17
18
19
20 if((isset($_SESSION)) && (x($_SESSION,'authenticated')) && ((! (x($_POST,'auth-params'))) || ($_POST['auth-params'] !== 'login'))) {
21
22         if(((x($_POST,'auth-params')) && ($_POST['auth-params'] === 'logout')) || ($a->module === 'logout')) {
23         
24                 // process logout request
25
26                 nuke_session();
27                 notice( t('Logged out.') . EOL);
28                 goaway($a->get_baseurl());
29         }
30
31         if(x($_SESSION,'uid')) {
32
33                 // already logged in user returning
34
35                 $check = get_config('system','paranoia');
36                 // extra paranoia - if the IP changed, log them out
37                 if($check && ($_SESSION['addr'] != $_SERVER['REMOTE_ADDR'])) {
38                         nuke_session();
39                         goaway($a->get_baseurl());
40                 }
41
42                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
43                         intval($_SESSION['uid'])
44                 );
45
46                 if(! count($r)) {
47                         nuke_session();
48                         goaway($a->get_baseurl());
49                 }
50
51                 // initialise user environment
52
53                 $a->user = $r[0];
54                 $_SESSION['theme'] = $a->user['theme'];
55                 $_SESSION['page_flags'] = $a->user['page-flags'];
56
57                 if(strlen($a->user['timezone'])) {
58                         date_default_timezone_set($a->user['timezone']);
59                         $a->timezone = $a->user['timezone'];
60                 }
61
62                 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
63
64                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
65                         intval($_SESSION['uid']));
66                 if(count($r)) {
67                         $a->contact = $r[0];
68                         $a->cid = $r[0]['id'];
69                         $_SESSION['cid'] = $a->cid;
70
71                 }
72                 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
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
86                         $noid = get_config('system','no_openid');
87
88                         $openid_url = trim($_POST['openid_url']);
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($a->get_baseurl());
100                                 // NOTREACHED
101                         }
102
103                         // Otherwise it's probably an openid.
104
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() . '/openid'; 
111
112                         $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
113                                 dbesc($openid_url)
114                         );
115                         if(count($r)) { 
116                                 // existing account
117                                 goaway($openid->authUrl());
118                                 // NOTREACHED   
119                         }
120                         else {
121                                 if($a->config['register_policy'] == REGISTER_CLOSED) {
122                                         $a = get_app();
123                                         notice( t('Login failed.') . EOL);
124                                         goaway($a->get_baseurl());
125                                         // NOTREACHED
126                                 }
127                                 // new account
128                                 $_SESSION['register'] = 1;
129                                 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
130                                 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
131                                 goaway($openid->authUrl());
132                                 // NOTREACHED   
133                         }
134                 }
135         }
136         if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
137
138                 $record = null;
139
140                 $addon_auth = array(
141                         'username' => trim($_POST['openid_url']), 
142                         'password' => trim($_POST['password']),
143                         'authenticated' => 0,
144                         'user_record' => null
145                 );
146
147                 /**
148                  *
149                  * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
150                  * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
151                  * and later plugins should not interfere with an earlier one that succeeded.
152                  *
153                  */
154
155                 call_hooks('authenticate', $addon_auth);
156
157                 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
158                         $record = $addon_auth['user_record'];
159                 }
160                 else {
161
162                         // process normal login request
163
164                         $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) 
165                                 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
166                                 dbesc(trim($_POST['openid_url'])),
167                                 dbesc(trim($_POST['openid_url'])),
168                                 dbesc($encrypted)
169                         );
170                         if(count($r))
171                                 $record = $r[0];
172                 }
173
174                 if((! $record) || (! count($record))) {
175                         logger('authenticate: failed login attempt: ' . trim($_POST['openid_url'])); 
176                         notice( t('Login failed.') . EOL );
177                         goaway($a->get_baseurl());
178                 }
179
180                 $_SESSION['uid'] = $record['uid'];
181                 $_SESSION['theme'] = $record['theme'];
182                 $_SESSION['authenticated'] = 1;
183                 $_SESSION['page_flags'] = $record['page-flags'];
184                 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
185                 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
186
187                 notice( t("Welcome back ") . $record['username'] . EOL);
188                 $a->user = $record;
189
190                 if(strlen($a->user['timezone'])) {
191                         date_default_timezone_set($a->user['timezone']);
192                         $a->timezone = $a->user['timezone'];
193                 }
194
195                 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
196                         intval($_SESSION['uid']));
197                 if(count($r)) {
198                         $a->contact = $r[0];
199                         $a->cid = $r[0]['id'];
200                         $_SESSION['cid'] = $a->cid;
201                 }
202                 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1",
203                         dbesc(datetime_convert()),
204                         intval($_SESSION['uid'])
205                 );
206
207                 call_hooks('logged_in', $a->user);
208
209                 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
210                 if(($a->module !== 'home') && isset($_SESSION['return_url']))
211                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
212         }
213 }
214
215 // Returns an array of group id's this contact is a member of.
216 // This array will only contain group id's related to the uid of this
217 // DFRN contact. They are *not* neccessarily unique across the entire site. 
218
219
220 if(! function_exists('init_groups_visitor')) {
221 function init_groups_visitor($contact_id) {
222         $groups = array();
223         $r = q("SELECT `gid` FROM `group_member` 
224                 WHERE `contact-id` = %d ",
225                 intval($contact_id)
226         );
227         if(count($r)) {
228                 foreach($r as $rr)
229                         $groups[] = $rr['gid'];
230         }
231         return $groups;
232 }}
233
234