]> git.mxchange.org Git - friendica.git/blob - include/auth.php
sorting out visitor sessions
[friendica.git] / include / auth.php
1 <?php
2
3 // login/logout 
4
5 if((x($_SESSION,'authenticated')) && (! ($_POST['auth-params'] == 'login'))) {
6         if($_POST['auth-params'] == 'logout' || $a->module == "logout") {
7                 unset($_SESSION['authenticated']);
8                 unset($_SESSION['uid']);
9                 unset($_SESSION['visitor_id']);
10                 unset($_SESSION['is_visitor']);
11                 unset($_SESSION['administrator']);
12                 unset($_SESSION['cid']);
13                 $_SESSION['sysmsg'] = "Logged out." . EOL;
14                 goaway($a->get_baseurl());
15         }
16         if(x($_SESSION,'uid')) {
17                 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
18                         intval($_SESSION['uid']));
19                 if($r === NULL || (! count($r))) {
20                         goaway($a->get_baseurl());
21                 }
22                 $a->user = $r[0];
23                 if(strlen($a->user['timezone']))
24                         date_default_timezone_set($a->user['timezone']);
25
26                 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
27                         intval($_SESSION['uid']));
28                 if(count($r)) {
29                         $a->cid = $r[0]['id'];
30                         $_SESSION['cid'] = $a->cid;
31                 }
32         }
33 }
34 else {
35         unset($_SESSION['authenticated']);
36         unset($_SESSION['uid']);
37         unset($_SESSION['visitor_id']);
38         unset($_SESSION['is_visitor']);
39         unset($_SESSION['administrator']);
40         unset($_SESSION['cid']);
41         $encrypted = hash('whirlpool',trim($_POST['password']));
42
43         if((x($_POST,'auth-params')) && $_POST['auth-params'] == 'login') {
44                 $r = q("SELECT * FROM `user` 
45                         WHERE `email` = '%s' AND `password` = '%s' LIMIT 1",
46                         dbesc(trim($_POST['login-name'])),
47                         dbesc($encrypted));
48                 if(($r === false) || (! count($r))) {
49                         $_SESSION['sysmsg'] = 'Login failed.' . EOL ;
50                         goaway($a->get_baseurl());
51                 }
52                 $_SESSION['uid'] = $r[0]['uid'];
53                 $_SESSION['admin'] = $r[0]['admin'];
54                 $_SESSION['authenticated'] = 1;
55                 if(x($r[0],'nickname'))
56                         $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $r[0]['nickname'];
57                 else
58                         $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $r[0]['uid'];
59
60                 $_SESSION['sysmsg'] = "Welcome back " . $r[0]['username'] . EOL;
61                 $a->user = $r[0];
62                 if(strlen($a->user['timezone']))
63                         date_default_timezone_set($a->user['timezone']);
64
65                 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
66                         intval($_SESSION['uid']));
67                 if(count($r)) {
68                         $a->cid = $r[0]['id'];
69                         $_SESSION['cid'] = $a->cid;
70                 }
71
72
73         }
74 }
75
76 // Returns an array of group id's this contact is a member of.
77 // This array will only contain group id's related to the uid of this
78 // DFRN contact. They are *not* neccessarily unique across the entire site. 
79
80
81 if(! function_exists('init_groups_visitor')) {
82 function init_groups_visitor($contact_id) {
83         $groups = array();
84         $r = q("SELECT `gid` FROM `group_member` 
85                 WHERE `contact-id` = %d ",
86                 intval($contact_id)
87         );
88         if(count($r)) {
89                 foreach($r as $rr)
90                         $groups[] = $rr['gid'];
91         }
92         return $groups;
93 }}
94
95