]> git.mxchange.org Git - friendica.git/blob - include/auth.php
Add sample external authentication plugin (ldap)
[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                 if(strlen($a->user['timezone']))
57                         date_default_timezone_set($a->user['timezone']);
58
59                 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
60
61                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
62                         intval($_SESSION['uid']));
63                 if(count($r)) {
64                         $a->contact = $r[0];
65                         $a->cid = $r[0]['id'];
66                         $_SESSION['cid'] = $a->cid;
67
68                 }
69                 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
70         }
71 }
72 else {
73
74         if(isset($_SESSION)) {
75                 nuke_session();
76         }
77
78         if((x($_POST,'password')) && strlen($_POST['password']))
79                 $encrypted = hash('whirlpool',trim($_POST['password']));
80         else {
81                 if((x($_POST,'openid_url')) && strlen($_POST['openid_url'])) {
82
83                         $noid = get_config('system','no_openid');
84
85                         $openid_url = trim($_POST['openid_url']);
86
87                         // validate_url alters the calling parameter
88
89                         $temp_string = $openid_url;
90
91                         // if it's an email address or doesn't resolve to a URL, fail.
92
93                         if(($noid) || (strpos($temp_string,'@')) || (! validate_url($temp_string))) {
94                                 $a = get_app();
95                                 notice( t('Login failed.') . EOL);
96                                 goaway($a->get_baseurl());
97                                 // NOTREACHED
98                         }
99
100                         // Otherwise it's probably an openid.
101
102                         require_once('library/openid.php');
103                         $openid = new LightOpenID;
104                         $openid->identity = $openid_url;
105                         $_SESSION['openid'] = $openid_url;
106                         $a = get_app();
107                         $openid->returnUrl = $a->get_baseurl() . '/openid'; 
108
109                         $r = q("SELECT `uid` FROM `user` WHERE `openid` = '%s' LIMIT 1",
110                                 dbesc($openid_url)
111                         );
112                         if(count($r)) { 
113                                 // existing account
114                                 goaway($openid->authUrl());
115                                 // NOTREACHED   
116                         }
117                         else {
118                                 if($a->config['register_policy'] == REGISTER_CLOSED) {
119                                         $a = get_app();
120                                         notice( t('Login failed.') . EOL);
121                                         goaway($a->get_baseurl());
122                                         // NOTREACHED
123                                 }
124                                 // new account
125                                 $_SESSION['register'] = 1;
126                                 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
127                                 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
128                                 goaway($openid->authUrl());
129                                 // NOTREACHED   
130                         }
131                 }
132         }
133         if((x($_POST,'auth-params')) && $_POST['auth-params'] === 'login') {
134
135                 $record = null;
136
137                 $addon_auth = array(
138                         'username' => trim($_POST['openid_url']), 
139                         'password' => trim($_POST['password']),
140                         'authenticated' => 0,
141                         'user_record' => null
142                 );
143
144                 /**
145                  *
146                  * A plugin indicates successful login by setting 'authenticated' to non-zero value and returning a user record
147                  * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
148                  * and later plugins should not interfere with an earlier one that succeeded.
149                  *
150                  */
151
152                 call_hooks('authenticate', $addon_auth);
153
154                 if(($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
155                         $record = $addon_auth['user_record'];
156                 }
157                 else {
158
159                         // process normal login request
160
161                         $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) 
162                                 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
163                                 dbesc(trim($_POST['openid_url'])),
164                                 dbesc(trim($_POST['openid_url'])),
165                                 dbesc($encrypted)
166                         );
167                         if(count($r))
168                                 $record = $r[0];
169                 }
170
171                 if((! $record) || (! count($record))) {
172                         logger('authenticate: failed login attempt: ' . trim($_POST['openid_url'])); 
173                         notice( t('Login failed.') . EOL );
174                         goaway($a->get_baseurl());
175                 }
176
177                 $_SESSION['uid'] = $record['uid'];
178                 $_SESSION['theme'] = $record['theme'];
179                 $_SESSION['authenticated'] = 1;
180                 $_SESSION['page_flags'] = $record['page-flags'];
181                 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
182                 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
183
184                 notice( t("Welcome back ") . $record['username'] . EOL);
185                 $a->user = $record;
186                 if(strlen($a->user['timezone']))
187                         date_default_timezone_set($a->user['timezone']);
188
189                 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
190                         intval($_SESSION['uid']));
191                 if(count($r)) {
192                         $a->contact = $r[0];
193                         $a->cid = $r[0]['id'];
194                         $_SESSION['cid'] = $a->cid;
195                 }
196                 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1",
197                         dbesc(datetime_convert()),
198                         intval($_SESSION['uid'])
199                 );
200
201                 call_hooks('logged_in', $a->user);
202
203                 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
204                 if(($a->module !== 'home') && isset($_SESSION['return_url']))
205                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
206         }
207 }
208
209 // Returns an array of group id's this contact is a member of.
210 // This array will only contain group id's related to the uid of this
211 // DFRN contact. They are *not* neccessarily unique across the entire site. 
212
213
214 if(! function_exists('init_groups_visitor')) {
215 function init_groups_visitor($contact_id) {
216         $groups = array();
217         $r = q("SELECT `gid` FROM `group_member` 
218                 WHERE `contact-id` = %d ",
219                 intval($contact_id)
220         );
221         if(count($r)) {
222                 foreach($r as $rr)
223                         $groups[] = $rr['gid'];
224         }
225         return $groups;
226 }}
227
228