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