forked from GibbonEdu/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
276 lines (244 loc) · 15 KB
/
login.php
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/*
Gibbon, Flexible & Open School System
Copyright (C) 2010, Ross Parker
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Gibbon\Comms\NotificationEvent;
// Gibbon system-wide include
require_once './gibbon.php';
setCurrentSchoolYear($guid, $connection2);
//The current/actual school year info, just in case we are working in a different year
$_SESSION[$guid]['gibbonSchoolYearIDCurrent'] = $_SESSION[$guid]['gibbonSchoolYearID'];
$_SESSION[$guid]['gibbonSchoolYearNameCurrent'] = $_SESSION[$guid]['gibbonSchoolYearName'];
$_SESSION[$guid]['gibbonSchoolYearSequenceNumberCurrent'] = $_SESSION[$guid]['gibbonSchoolYearSequenceNumber'];
$_SESSION[$guid]['pageLoads'] = null;
$URL = './index.php';
// Sanitize the whole $_POST array
$validator = new \Gibbon\Data\Validator();
$_POST = $validator->sanitize($_POST);
//Get and store POST variables from calling page
$username = isset($_POST['username'])? $_POST['username'] : '';
$password = isset($_POST['password'])? $_POST['password'] : '';
if (empty($username) or empty($password)) {
$URL .= '?loginReturn=fail0b';
header("Location: {$URL}");
exit;
}
//VALIDATE LOGIN INFORMATION
else {
try {
$data = array('username' => $username);
$sql = "SELECT gibbonPerson.*, futureYearsLogin, pastYearsLogin FROM gibbonPerson LEFT JOIN gibbonRole ON (gibbonPerson.gibbonRoleIDPrimary=gibbonRole.gibbonRoleID) WHERE ((username=:username OR (LOCATE('@', :username)>0 AND email=:username) ) AND (status='Full'))";
$result = $connection2->prepare($sql);
$result->execute($data);
} catch (PDOException $e) {
}
//Test to see if username exists and is unique
if ($result->rowCount() != 1) {
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, null, 'Login - Failed', array('username' => $username, 'reason' => 'Username does not exist'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail1';
header("Location: {$URL}");
exit;
} else {
$row = $result->fetch();
// Insufficient privileges to login
if ($row['canLogin'] != 'Y') {
$URL .= '?loginReturn=fail2';
header("Location: {$URL}");
exit;
}
// Get primary role info
$data = array('gibbonRoleIDPrimary' => $row['gibbonRoleIDPrimary']);
$sql = "SELECT * FROM gibbonRole WHERE gibbonRoleID=:gibbonRoleIDPrimary";
$role = $pdo->selectOne($sql, $data);
// Login not allowed for this role
if (!empty($role['canLoginRole']) && $role['canLoginRole'] != 'Y') {
$URL .= '?loginReturn=fail9';
header("Location: {$URL}");
exit;
}
// Set the username explicity, to handle logging in with email
$username = $row['username'];
//Check fail count, reject & alert if 3rd time
if ($row['failCount'] >= 3) {
try {
$dataSecure = array('lastFailIPAddress' => $_SERVER['REMOTE_ADDR'], 'lastFailTimestamp' => date('Y-m-d H:i:s'), 'failCount' => ($row['failCount'] + 1), 'username' => $username);
$sqlSecure = 'UPDATE gibbonPerson SET lastFailIPAddress=:lastFailIPAddress, lastFailTimestamp=:lastFailTimestamp, failCount=:failCount WHERE (username=:username)';
$resultSecure = $connection2->prepare($sqlSecure);
$resultSecure->execute($dataSecure);
} catch (PDOException $e) {
}
if ($row['failCount'] == 3) {
// Raise a new notification event
$event = new NotificationEvent('User Admin', 'Login - Failed');
$event->addRecipient($_SESSION[$guid]['organisationAdministrator']);
$event->setNotificationText(sprintf(__('Someone failed to login to account "%1$s" 3 times in a row.'), $username));
$event->setActionLink('/index.php?q=/modules/User Admin/user_manage.php&search='.$username);
$event->sendNotifications($pdo, $gibbon->session);
}
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Failed', array('username' => $username, 'reason' => 'Too many failed logins'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail6';
header("Location: {$URL}");
exit;
} else {
$passwordTest = false;
//If strong password exists
$salt = $row['passwordStrongSalt'];
$passwordStrong = $row['passwordStrong'];
if ($passwordStrong != '' and $salt != '') {
if (hash('sha256', $row['passwordStrongSalt'].$password) == $row['passwordStrong']) {
$passwordTest = true;
}
}
//If only weak password exists
elseif ($row['password'] != '') {
if ($row['password'] == md5($password)) {
$passwordTest = true;
//Migrate to strong password
$salt = getSalt();
$passwordStrong = hash('sha256', $salt.$password);
try {
$dataSecure = array('passwordStrong' => $passwordStrong, 'passwordStrongSalt' => $salt, 'username' => $username);
$sqlSecure = "UPDATE gibbonPerson SET password='', passwordStrong=:passwordStrong, passwordStrongSalt=:passwordStrongSalt WHERE (username=:username)";
$resultSecure = $connection2->prepare($sqlSecure);
$resultSecure->execute($dataSecure);
} catch (PDOException $e) {
$passwordTest = false;
}
}
}
//Test to see if password matches username
if ($passwordTest != true) {
//FAIL PASSWORD
try {
$dataSecure = array('lastFailIPAddress' => $_SERVER['REMOTE_ADDR'], 'lastFailTimestamp' => date('Y-m-d H:i:s'), 'failCount' => ($row['failCount'] + 1), 'username' => $username);
$sqlSecure = 'UPDATE gibbonPerson SET lastFailIPAddress=:lastFailIPAddress, lastFailTimestamp=:lastFailTimestamp, failCount=:failCount WHERE (username=:username)';
$resultSecure = $connection2->prepare($sqlSecure);
$resultSecure->execute($dataSecure);
} catch (PDOException $e) {
$passwordTest = false;
}
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Failed', array('username' => $username, 'reason' => 'Incorrect password'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail1';
header("Location: {$URL}");
exit;
} else {
if ($row['gibbonRoleIDPrimary'] == '' or count(getRoleList($row['gibbonRoleIDAll'], $connection2)) == 0) {
//FAILED TO SET ROLES
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Failed', array('username' => $username, 'reason' => 'Failed to set role(s)'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail2';
header("Location: {$URL}");
exit;
} else {
//Allow for non-current school years to be specified
if ($_POST['gibbonSchoolYearID'] != $_SESSION[$guid]['gibbonSchoolYearID']) {
if ($row['futureYearsLogin'] != 'Y' and $row['pastYearsLogin'] != 'Y') { //NOT ALLOWED DUE TO CONTROLS ON ROLE, KICK OUT!
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Failed', array('username' => $username, 'reason' => 'Not permitted to access non-current school year'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail9';
header("Location: {$URL}");
exit();
} else {
//Get details on requested school year
try {
$dataYear = array('gibbonSchoolYearID' => $_POST['gibbonSchoolYearID']);
$sqlYear = 'SELECT * FROM gibbonSchoolYear WHERE gibbonSchoolYearID=:gibbonSchoolYearID';
$resultYear = $connection2->prepare($sqlYear);
$resultYear->execute($dataYear);
} catch (PDOException $e) {
}
//Check number of rows returned.
//If it is not 1, show error
if (!($resultYear->rowCount() == 1)) {
die(__('Configuration Error: there is a problem accessing the current Academic Year from the database.'));
}
//Else get year details
else {
$rowYear = $resultYear->fetch();
if ($row['futureYearsLogin'] != 'Y' and $_SESSION[$guid]['gibbonSchoolYearSequenceNumber'] < $rowYear['sequenceNumber']) { //POSSIBLY NOT ALLOWED DUE TO CONTROLS ON ROLE, CHECK YEAR
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Failed', array('username' => $username, 'reason' => 'Not permitted to access non-current school year'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail9';
header("Location: {$URL}");
exit();
} elseif ($row['pastYearsLogin'] != 'Y' and $_SESSION[$guid]['gibbonSchoolYearSequenceNumber'] > $rowYear['sequenceNumber']) { //POSSIBLY NOT ALLOWED DUE TO CONTROLS ON ROLE, CHECK YEAR
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Failed', array('username' => $username, 'reason' => 'Not permitted to access non-current school year'), $_SERVER['REMOTE_ADDR']);
$URL .= '?loginReturn=fail9';
header("Location: {$URL}");
exit();
} else { //ALLOWED
$_SESSION[$guid]['gibbonSchoolYearID'] = $rowYear['gibbonSchoolYearID'];
$_SESSION[$guid]['gibbonSchoolYearName'] = $rowYear['name'];
$_SESSION[$guid]['gibbonSchoolYearSequenceNumber'] = $rowYear['sequenceNumber'];
}
}
}
}
//USER EXISTS, SET SESSION VARIABLES
$gibbon->session->createUserSession($username, $row);
// Set these from local values
$gibbon->session->set('passwordStrong', $passwordStrong);
$gibbon->session->set('passwordStrongSalt', $salt);
$gibbon->session->set('googleAPIAccessToken', null);
//Allow for non-system default language to be specified from login form
if (@$_POST['gibboni18nID'] != $_SESSION[$guid]['i18n']['gibboni18nID']) {
try {
$dataLanguage = array('gibboni18nID' => $_POST['gibboni18nID']);
$sqlLanguage = 'SELECT * FROM gibboni18n WHERE gibboni18nID=:gibboni18nID';
$resultLanguage = $connection2->prepare($sqlLanguage);
$resultLanguage->execute($dataLanguage);
} catch (PDOException $e) {
}
if ($resultLanguage->rowCount() == 1) {
$rowLanguage = $resultLanguage->fetch();
setLanguageSession($guid, $rowLanguage, false);
}
} else {
//If no language specified, get user preference if it exists
if (!is_null($_SESSION[$guid]['gibboni18nIDPersonal'])) {
try {
$dataLanguage = array('gibboni18nID' => $_SESSION[$guid]['gibboni18nIDPersonal']);
$sqlLanguage = "SELECT * FROM gibboni18n WHERE active='Y' AND gibboni18nID=:gibboni18nID";
$resultLanguage = $connection2->prepare($sqlLanguage);
$resultLanguage->execute($dataLanguage);
} catch (PDOException $e) {
}
if ($resultLanguage->rowCount() == 1) {
$rowLanguage = $resultLanguage->fetch();
setLanguageSession($guid, $rowLanguage, false);
}
}
}
//Make best effort to set IP address and other details, but no need to error check etc.
try {
$data = array('lastIPAddress' => $_SERVER['REMOTE_ADDR'], 'lastTimestamp' => date('Y-m-d H:i:s'), 'failCount' => 0, 'username' => $username);
$sql = 'UPDATE gibbonPerson SET lastIPAddress=:lastIPAddress, lastTimestamp=:lastTimestamp, failCount=:failCount WHERE username=:username';
$result = $connection2->prepare($sql);
$result->execute($data);
} catch (PDOException $e) {
}
if (isset($_GET['q'])) {
if ($_GET['q'] == '/publicRegistration.php') {
$URL = './index.php';
} else {
$URL = './index.php?q='.$_GET['q'];
}
} else {
$URL = './index.php';
}
setLog($connection2, $_SESSION[$guid]['gibbonSchoolYearIDCurrent'], null, $row['gibbonPersonID'], 'Login - Success', array('username' => $username), $_SERVER['REMOTE_ADDR']);
header("Location: {$URL}");
exit;
}
}
}
}
}