This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.php
208 lines (151 loc) · 4.39 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
<?php
$urlpatch = (strpos($_SERVER['DOCUMENT_ROOT'], 'xampp') == true)?'/dimli':'';
if(!defined('MAIN_DIR')){define('MAIN_DIR',$_SERVER['DOCUMENT_ROOT'].$urlpatch);}
require_once(MAIN_DIR.'/_php/_config/session.php');
require_once(MAIN_DIR.'/_php/_config/connection.php');
require_once(MAIN_DIR.'/_php/_config/functions.php');
$errors = array();
$status = false;
// If user is already logged in, redirect to the homepage
if (logged_in()) {
header('Location: index.php');
exit;
}
// The below script runs when a user attempts to log in
if (isset($_POST['login'])) {
// Validate required fields
$required_fields = array('username','password');
$errors = array_merge($errors, check_required_fields($required_fields));
// Validate field lengths
$fields_with_lengths = array('username' => 15, 'password' => 15);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));
// Sanitize data
$username = trim($mysqli->real_escape_string($_POST['username']));
$password = crypt(trim($mysqli->real_escape_string($_POST['password'])), SALT);
if (empty($errors)) { // User-submitted data is valid, so continue
//-----------------------------------------------
// Check user credentials against the database
//-----------------------------------------------
$sql = "SELECT * FROM dimli.user
WHERE username = '{$username}'
AND crypted_password = '{$password}' ";
$result = db_query($mysqli, $sql);
if ($result->num_rows === 1) {
// One, and ONLY one, match was found
$found_user = $result->fetch_assoc();
//----------------------------------------------
// Load user info, preferences and privileges
// and save them to the session
//----------------------------------------------
$_SESSION['user_id'] = $found_user['id'];
foreach (array(
'username',
'first_name',
'last_name',
'display_name',
'pref_lantern_view',
'pref_user_type',
'priv_digitize',
'priv_edit',
'priv_exportImages',
'priv_deliver',
'priv_catalog',
'priv_approve',
'priv_users_read',
'priv_users_create',
'priv_users_delete',
'priv_orders_read',
'priv_orders_create',
'priv_orders_confirmCreation',
'priv_orders_download',
'priv_orders_delete',
'priv_csv_import',
'priv_csv_export',
'priv_images_delete',
'priv_images_flag4Export'
) as $type)
{
$_SESSION[$type] = $found_user[$type];
}
// Redirect the user to the homepage
header('Location: index.php');
exit();
} else { // Username/password combination not found
$status = 'badcombo';
}
} else { // Errors occurred
$status = 'invalidentry';
}
} else { // Login form has not yet been submitted.
$username = '';
$password = '';
}
##############################################################
################### BEGIN CLIENT SIDE ####################
##############################################################
require("_php/header.php"); ?>
<div id="message_wrapper">
<div id="message_text"></div>
</div>
<div class="module">
<h1>Log in</h1>
<form action="login.php" method="post">
<input type="text"
id="username"
name="username"
placeholder="username"
value="<?php echo htmlentities($username); ?>"
maxlength="15">
<br>
<input type="password"
id="password"
name="password"
placeholder="password"
value=""
maxlength="15">
<br>
<input type="submit"
name="login"
value="Submit">
</form>
</div>
<script>
<?php
if ($status == 'badcombo'):
// Incorrect username/password combination entered ?>
input_error($('input#username, input#password'));
msg(['Incorrect username & password combination'], 'error');
<?php
elseif ($status == 'invalidentry'):
if (in_array('username', $errors)): ?>
input_error($('input#username'));
<?php
endif;
if (in_array('password', $errors)): ?>
input_error($('input#password'));
<?php
endif; ?>
msg(['Both username and password must be between','six and fifteen charcters in length'], 'error');
<?php
endif; ?>
$(document).ready(
function()
{
if ($('div#message_text').text() == '') {
$('input#username').focus();
}
});
$('input').focus(
function()
{
$(this).next('span.helper').fadeIn(50);
})
.blur(
function()
{
$(this).next('span.helper').fadeOut(50);
});
</script>
<?php
require("_php/footer.php");
?>