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