-
Notifications
You must be signed in to change notification settings - Fork 59
/
login.php
49 lines (41 loc) · 1.49 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
<?php
session_start();
set_error_handler(function() {
throw new Exception('Failed to open authentification file in <code>' . FILE_PASS . '</code>');
});
require 'config.php';
require 'lib/password.php';
// logout
if (isset($_GET['logout'])) {
unset($_SESSION['authentificated']);
session_destroy();
}
// check identification
else if (isset($_POST['username']) && isset($_POST['password']) && !empty($_POST['username']) && !empty($_POST['password'])) {
try {
//*
$db = json_decode(file_get_contents(FILE_PASS));
$username = $db->{'user'};
$password = $db->{'password'};
if (password_needs_rehash($password, PASSWORD_BCRYPT)) {
$password = password_hash($password, PASSWORD_BCRYPT);
$db->{'password'} = $password;
if (is_writable(FILE_PASS)) {
$handler = fopen(FILE_PASS, 'w');
fwrite($handler, json_encode($db));
fclose($handler);
} else {
$_SESSION['message'] = "Database file \"" . FILE_PASS . "\" is not writable, please check the file owner and rights.";
}
}
if ($_POST['username'] == $username && password_verify($_POST['password'], $password))
$_SESSION['authentificated'] = true;
else
$_SESSION['message'] = 'Incorrect username or password.';
} catch (Exception $e) {
$_SESSION['message'] = $e->getMessage();
}
}
header('Location: ' . INDEX);
exit();
?>