1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
<?php
/**
* Elgg session management
* Functions to manage logins
*
* @package Elgg
* @subpackage Core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
* @author Curverider Ltd
* @copyright Curverider Ltd 2008
* @link http://elgg.org/
*/
/**
* Returns whether or not the user is currently logged in
*
* @uses $_SESSION
* @return true|false
*/
function isloggedin() {
if ((isset($_SESSION['guid'])) && ($_SESSION['guid'] > 0))
return true;
return false;
}
/**
* Log in
*
* @param string $username
* @param string $password
* @param true|false $persistent
* @return true|false
*/
function login($username, $password, $persistent = false) {
global $CONFIG;
$dbpassword = md5($password);
if ($user = get_user_by_username($username)) {
if ($user->password == $dbpassword) {
$_SESSION['user'] = $user;
$_SESSION['guid'] = $user->getGUID();
$_SESSION['id'] = $_SESSION['guid'];
$_SESSION['username'] = $user->username;
$_SESSION['name'] = $user->name;
$code = (md5($user->name . $user->username . time() . rand()));
// update_data("update {$CONFIG->dbprefix}users set code = '".md5($code)."' where id = {$user->id}");
$user->code = md5($code);
$user->save();
//$code = md5($code); // This is a deliberate re-MD5-ing
$_SESSION['code'] = $code;
//if (!empty($persistent)) {
setcookie("elggperm", $code, (time()+(86400 * 30)),"/");
//}
// set_login_fields($user->id);
}
return true;
} else {
return false;
}
}
/**
* Log the current user out
*
* @return true|false
*/
function logout() {
global $CONFIG;
if (isset($_SESSION['user'])) {
$_SESSION['user']->code = "";
$_SESSION['user']->save();
}
unset($_SESSION['username']);
unset($_SESSION['name']);
unset($_SESSION['code']);
unset($_SESSION['guid']);
unset($_SESSION['id']);
unset($_SESSION['user']);
setcookie("elggperm", "", (time()-(86400 * 30)),"/");
return true;
}
/**
* Initialises the system session and potentially logs the user in
*
* This function looks for:
*
* 1. $_SESSION['id'] - if not present, we're logged out, and this is set to 0
* 2. The cookie 'elggperm' - if present, checks it for an authentication token, validates it, and potentially logs the user in
*
* @uses $_SESSION
* @param unknown_type $event
* @param unknown_type $object_type
* @param unknown_type $object
*/
function session_init($event, $object_type, $object) {
session_name('Elgg');
session_start();
if (empty($_SESSION['guid'])) {
if (isset($_COOKIE['elggperm'])) {
$code = $_COOKIE['elggperm'];
$code = md5($code);
$_SESSION['guid'] = 0;
$_SESSION['id'] = 0;
if ($user = get_user_by_code($code)) {
$_SESSION['user'] = $user;
$_SESSION['id'] = $user->getGUID();
$_SESSION['guid'] = $_SESSION['id'];
$_SESSION['code'] = $_COOKIE['elggperm'];
}
} else {
$_SESSION['id'] = 0;
$_SESSION['guid'] = 0;
}
} else {
if (!empty($_SESSION['code'])) {
$code = md5($_SESSION['code']);
if ($user = get_user_by_code($code)) {
$_SESSION['user'] = $user;
} else {
unset($_SESSION['user']);
$_SESSION['guid'] = 0;
$_SESSION['id'] = 0;
}
} else {
$_SESSION['guid'] = 0;
$_SESSION['id'] = 0;
}
}
if ($_SESSION['id'] > 0) {
// set_last_action($_SESSION['id']);
}
}
register_event_handler("init","system","session_init");
//register actions *************************************************************
register_action("login",true);
register_action("logout");
?>
|