This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
login.php
325 lines (263 loc) · 12.2 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?PHP
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@include_once("inc/config.php");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// If no DB info and install file exists then go there...
if(!$server_success){
if(file_exists('install.php')){
header("Location: install.php");
die();
} else {
$settings['cms']['site_name'] = 'Connection Error';
$settings['cms']['site_url'] = 'http://getdirectus.com/';
$alert[] = 'error_server';
}
} else {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// If DB connection is good then continue
require_once("inc/setup.php");
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reset password
if($_POST['reset']){
if($_POST["password_reset"] != $_POST["password_confirm"]){
$alert[] = "passwords_match";
}
if(strlen($_POST["password_reset"]) < 3){
$alert[] = "password_format";
}
if(count($alert) == 0){
$sth = $dbh->prepare("SELECT * FROM `directus_users` WHERE `reset_token` = :reset_token AND `reset_expiration` > NOW() ");
$sth->bindParam(':reset_token', $_POST['reset']);
$sth->execute();
if($row = $sth->fetch()){
// Hash the new password
$hasher = new PasswordHash(8, FALSE);
$new_password = $hasher->HashPassword($_POST["password_reset"]);
// Save new password and reset token and expiration
$sth2 = $dbh->prepare("UPDATE `directus_users` SET `password` = :password, `reset_token` = '', `reset_expiration` = NOW() WHERE `id` = :id ");
$sth2->bindParam(':password', $new_password);
$sth2->bindParam(':id', $row["id"]);
if($sth2->execute()){
$_SESSION['cms_user_id'] = $row["id"];
} else {
$alert[] = "general_error";
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check if user is already logged in
// With cookie?
check_user_cookie();
if(!isset($_SESSION['cms_user_id'])) {
// If they attempt to log in
if($_POST['attempt']) {
// Check if email is filled in
if($_POST['login_email']){
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check if email exists with parameterized PDO
$sth = $dbh->prepare("SELECT * FROM `directus_users` WHERE `email` = :email ");
$sth->bindParam(':email', $_POST['login_email']);
$sth->execute();
if($row = $sth->fetch()){
// Ensure this is an active user (not inactive)
if($row['active'] == '1'){
// Send password if forgotten, else check password
if($_POST["forgot"] == "true"){
// Update reset token and expiration here
$reset_token = nonce();
$sth2 = $dbh->prepare("UPDATE `directus_users` SET `reset_token` = :reset_token, `reset_expiration` = NOW() + INTERVAL 30 MINUTE WHERE `id` = :id ");
$sth2->bindParam(':reset_token', $reset_token);
$sth2->bindParam(':id', $row["id"]);
$sth2->execute();
$body = "Hello ".$row["first_name"]." ".$row["last_name"].",\n<br>\n<br>Below is a link to reset your Directus user password for \"" . $settings['cms']['site_name'] . "\". This link will work for the next 30 minutes, after that you will need to request a new password reset.\n<br>\n<br>\n<br>" . CMS_INSTALL_PATH . "login.php?reset-token=".urlencode($reset_token);
$sent = send_email("Directus Password Reset", $body, $to = $row["email"], $bcc = false, $from = false);
if($sent) {
$_SESSION['alert'] = "password_sent";
} else {
$alert[] = "email_fail_forgot_password_".$_POST['login_email'];
}
} else {
if($_POST['login_password']){
// Check that the password is correct
$hasher = new PasswordHash(8, FALSE);
if($hasher->CheckPassword($_POST['login_password'], $row["password"])){
// Check to ensure that user is only online once (USE IP ADDRESS)
$_SESSION['cms_user_id'] = $row["id"];
// If user would like to "Remember Me" then store token
if($_POST['remember_me'] == 'true'){
// Generate a new user token
$token = nonce();
$sth2 = $dbh->prepare("UPDATE `directus_users` SET `token` = :token WHERE `id` = :id ");
$sth2->bindParam(':token', $token);
$sth2->bindParam(':id', $row["id"]);
$sth2->execute();
setcookie("token", $token, time() + (60*$settings['cms']['cookie_life']), CMS_PATH);
}
// Redirect to where you last were or the homepage
if(file_exists('install.php')){
$_SESSION['alert'] = (unlink('install.php'))? "installer_removed" : "installer_remove_manual";
header("Location: ".CMS_INSTALL_PATH."settings.php");
die();
} elseif($_COOKIE['cms_redirect']){
// The installer should no longer be present
if(file_exists('install.php')){
$_SESSION['alert'] = "remove_install";
}
// Redirect to previous page if one exists
$redirect = $_COOKIE['cms_redirect'];
setrawcookie("cms_redirect", "", time() - 3600, CMS_PATH);
header("Location: $redirect");
die();
} else {
// The installer should no longer be present
if(file_exists('install.php')){
$_SESSION['alert'] = "remove_install";
}
// If this is the users first time logging in then present with help info
if($row["last_login"] == '0000-00-00 00:00:00'){
$_SESSION['alert'] = "virgin";
}
if($row["last_page"] != ""){
header("Location: ".CMS_INSTALL_PATH.$row["last_page"]);
} else {
header("Location: ".CMS_INSTALL_PATH."tables.php");
}
die();
}
} else{
$alert[] = "password_fail_".$_POST['login_email'].' / '.$_POST['login_password']; // Could be same error message bad user/password for safer error handling
}
} else {
$alert[] = "password_required";
}
}
} else {
$alert[] = "login_user_disabled_".$_POST['login_email']; // Could be same error message bad user/password for safer error handling
}
} else {
$alert[] = "email_fail_".$_POST['login_email']; // Could be same error message bad user/password for safer error handling
}
} else{
$alert[] = "email_required";
}
}
} else {
// Redirect if already logged in
header("Location: ".CMS_INSTALL_PATH."tables.php");
die();
}
if(isset($_COOKIE['cms_alert'])){
$_SESSION['alert'] = $_COOKIE['cms_alert'];
setcookie("cms_alert", "", time() - 3600, CMS_PATH);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check for password reset
$reset_password = false;
if($_GET['reset-token']){
// Check reset-token
$sth = $dbh->prepare("SELECT * FROM `directus_users` WHERE `reset_token` = :reset_token AND `reset_expiration` > NOW() ");
$sth->bindParam(':reset_token', $_GET['reset-token']);
$sth->execute();
if($row = $sth->fetch()){
$reset_password = true;
} else {
$alert[] = "reset_expired";
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?PHP echo $settings['cms']['site_name']; ?> - Login to Directus</title>
<link rel="shortcut icon" href="<?PHP echo $directus_path;?>media/site/favicon.ico">
<link rel="stylesheet" href="<?PHP echo $directus_path;?>inc/css/directus.css<?PHP echo '?'.time(); ?>" type="text/css" media="screen" title="" charset="utf-8">
<link rel="stylesheet" href="<?PHP echo $directus_path;?>inc/css/cms_colors/<?PHP echo $settings['cms']['cms_color'];?>.css<?PHP echo '?'.time(); ?>" type="text/css" media="screen" title="" charset="utf-8">
<script type="text/javascript" src="<?PHP echo $directus_path;?>inc/js/jquery.js"></script>
<script type="text/javascript" src="<?PHP echo $directus_path;?>inc/js/jquery-ui.js"></script>
<script type="text/javascript" src="<?PHP echo $directus_path;?>inc/js/directus.js" id="base_path" base_path="<?PHP echo $directus_path;?>"></script>
<script>
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if(browser == 'Microsoft Internet Explorer'){
alert('You are using IE :(\n\nChrome/FireFox/Safari are the recommended browsers for Directus.\n\nYou can continue, but things may not be as pretty.');
} else if(browser != 'Netscape') {
alert('Chrome/FireFox/Safari are the recommended browsers for Directus.\n\nThank you.');
}
<?PHP
if(count($alert) > 0) {
?>
$(document).ready(function(){
// Shake if alert
$("#shake").effect("shake", { times: 2 }, 150);
});
<?PHP
}
?>
</script>
</head>
<body>
<div id="alert_container">
<?PHP
// Add URL alert to existing array of alerts
if($_SESSION['alert']){
$alert[] = $_SESSION['alert'];
// Clear session so alert doesn't repeat
unset($_SESSION['alert']);
}
if(count($alert) > 0){
require_once(BASE_PATH . "inc/alert.php");
}
?>
</div>
<div id="toolbar">
<div class="container clearfix">
<div id="site_title">
<span class="title"><?PHP echo $settings['cms']['site_name']; ?></span>
<a class="badge view_site" href="<?PHP echo $settings['cms']['site_url']; ?>" target="_blank"><?PHP echo ($server_success)?'View Site':'Troubleshoot';?></a>
</div>
</div>
</div>
<?PHP if($server_success){ ?>
<div id="login">
<div id="shake">
<?PHP
if($reset_password){
?>
<form id="login_form" name="login_form" action="login.php?reset-token=<?PHP echo urlencode($_GET['reset-token']); ?>" method="post" enctype="multipart/form-data">
<input id="reset" name="reset" type="hidden" value="<?PHP echo $_GET['reset-token']; ?>" />
<label class="primary" for="password_reset">New Password</label><br>
<input id="password_reset" name="password_reset" class="title" type="password" maxlength="50"><br>
<label class="primary" for="password_confirm">Confirm Password</label><br>
<input id="password_confirm" name="password_confirm" class="title" type="password" maxlength="50"><br>
<input class="button big color login" type="submit" value="Save">
</form>
<?PHP
} else {
?>
<form id="login_form" name="login_form" action="login.php" method="post" enctype="multipart/form-data">
<input id="attempt" name="attempt" type="hidden" value="true" />
<input id="forgot" name="forgot" type="hidden" value="false" />
<label class="primary" for="login_email">Email</label><br>
<input id="login_email" name="login_email" class="title" type="text" maxlength="50"><br>
<label class="primary" for="login_password">Password</label><br>
<input id="login_password" name="login_password" class="title" type="password" maxlength="50"><br>
<input type="checkbox" id="remember_me" name="remember_me" value="true" /> <label for="remember_me">Remember Me</label><br>
<input class="button big color login" type="submit" value="Login">
<a href="#" id="forgot_password">Forgot your password?</a>
</form>
<?PHP
}
?>
</div>
<div id="login_footer">
Powered by <a href="http://getdirectus.com">Directus (v<?PHP echo $settings['cms']['version']; ?>)</a>
</div>
</div>
<?PHP } ?>
</body>
</html>