-
Notifications
You must be signed in to change notification settings - Fork 174
/
login.php
138 lines (118 loc) · 3.34 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
<?php
if (!defined('APP_STARTED')) {
die('Forbidden!');
}
/**
* This class is only instantiated if the ACCESS_USER and ACCESS_PASSWORD constants are defined
*
* @todo Add log for the login attempts and limit as soon as possible.
*/
class Login
{
/**
* Constructor
*/
public function __construct()
{
session_start();
if ($_GET['action'] === 'logout') {
$this->doLogout();
header("location: " . BASE_URL);
exit;
}
}
/**
* Check if the user is logged
* @return boolean
*/
private function isLogged()
{
if ($_SESSION['ACCESS_USER'] !== ACCESS_USER || $_SESSION['ACCESS_PASSWORD'] !== ACCESS_PASSWORD) {
return false;
}
return true;
}
/**
* Do the login
* @param string $ip IP address
* @param string $username Username
* @param string $password Password
* @return boolean
*/
private function doLogin($ip, $username, $password)
{
// Check the access to this function, using logs and ip
//--> to be implemented
// Check credentials
if ($username !== ACCESS_USER || $password !== ACCESS_PASSWORD) {
return false;
}
$_SESSION['ACCESS_USER'] = $username;
$_SESSION['ACCESS_PASSWORD'] = $password;
return true;
}
/**
* Logout from the password protected area
* @return boolean Always true
*/
private function doLogout()
{
$_SESSION['ACCESS_USER'] = '';
$_SESSION['ACCESS_PASSWORD'] = '';
return true;
}
/**
* Get the IP address of the visitor
* @return string
*/
private function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
return $_SERVER['HTTP_CLIENT_IP'];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $_SERVER['REMOTE_ADDR'];
}
/**
* Render the login page, if the authentication is not performed
*/
public function dispatch()
{
// Stop if user is aready logged in (exception from negative first)
if ($this->isLogged()) {
return true;
}
$ip = $this->getRealIpAddr();
$username = isset($_POST['username']) ? htmlspecialchars($_POST['username']) : null;
$password = isset($_POST['password']) ? htmlspecialchars($_POST['password']) : null;
if (isset($_POST['login'])) {
if (!$username || !$password) {
$error = 'Please complete both fields.';
} else {
if (!$this->doLogin($ip, $username, $password)) {
$error = "Wrong password.";
} else {
return true;
}
}
}
// Show the login layout and stop
$layout = __DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'login.php';
include($layout);
exit;
}
/**
* Singleton
* @return Login
*/
public static function instance()
{
static $instance;
if (!($instance instanceof self)) {
$instance = new self();
}
return $instance;
}
}