]> git.mxchange.org Git - friendica.git/blob - include/auth.php
Merge branch 'master' of git://github.com/friendika/friendika
[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
136                 $addon_auth = array(
137                         'name' => trim($_POST['openid_url']), 
138                         'password' => trim($_POST['password']),
139                         'authenticated' => 0
140                 );
141
142                 /**
143                  *
144                  * A plugin indicates successful login by setting 'authenticated' to non-zero value
145                  * Plugins should never set 'authenticated' except to indicate success - as hooks may be chained
146                  * and later plugins should not interfere with an earlier one that succeeded.
147                  *
148                  */
149
150                 call_hooks('authenticate', $addon_auth);
151
152                 if(! $addon_auth['authenticated']) {
153                         // process login request
154
155                         $r = q("SELECT * FROM `user` WHERE ( `email` = '%s' OR `nickname` = '%s' ) 
156                                 AND `password` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
157                                 dbesc(trim($_POST['openid_url'])),
158                                 dbesc(trim($_POST['openid_url'])),
159                                 dbesc($encrypted)
160                         );
161                         if(($r === false) || (! count($r))) {
162                                 notice( t('Login failed.') . EOL );
163                                 goaway($a->get_baseurl());
164                         }
165                 }
166
167                 $_SESSION['uid'] = $r[0]['uid'];
168                 $_SESSION['theme'] = $r[0]['theme'];
169                 $_SESSION['authenticated'] = 1;
170                 $_SESSION['page_flags'] = $r[0]['page-flags'];
171                 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $r[0]['nickname'];
172                 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
173
174                 notice( t("Welcome back ") . $r[0]['username'] . EOL);
175                 $a->user = $r[0];
176                 if(strlen($a->user['timezone']))
177                         date_default_timezone_set($a->user['timezone']);
178
179                 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
180                         intval($_SESSION['uid']));
181                 if(count($r)) {
182                         $a->contact = $r[0];
183                         $a->cid = $r[0]['id'];
184                         $_SESSION['cid'] = $a->cid;
185                 }
186                 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1",
187                         dbesc(datetime_convert()),
188                         intval($_SESSION['uid'])
189                 );
190
191                 call_hooks('logged_in', $a->user);
192
193                 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
194                 if(($a->module !== 'home') && isset($_SESSION['return_url']))
195                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
196         }
197 }
198
199 // Returns an array of group id's this contact is a member of.
200 // This array will only contain group id's related to the uid of this
201 // DFRN contact. They are *not* neccessarily unique across the entire site. 
202
203
204 if(! function_exists('init_groups_visitor')) {
205 function init_groups_visitor($contact_id) {
206         $groups = array();
207         $r = q("SELECT `gid` FROM `group_member` 
208                 WHERE `contact-id` = %d ",
209                 intval($contact_id)
210         );
211         if(count($r)) {
212                 foreach($r as $rr)
213                         $groups[] = $rr['gid'];
214         }
215         return $groups;
216 }}
217
218