-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecovery.php
204 lines (147 loc) · 4.04 KB
/
recovery.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
<?php
if(!defined('MAIN_DIR')){define('MAIN_DIR',dirname('__FILENAME__'));}
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['recovery'])) {
// Validate required fields
$required_fields = array('email');
$errors = array_merge($errors, check_required_fields($required_fields));
// Sanitize data
$email = trim($mysqli->real_escape_string($_POST['email']));
if (empty($errors)) { // User-submitted data is valid, so continue
//-----------------------------------------------
// Check user credentials against the database
//-----------------------------------------------
$sql = "SELECT * FROM $DB_NAME.user
WHERE email = '{$email}' ";
$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_image_ids_edit',
'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>Password Recovery</h1>
<form action="recovery.php" method="post">
<input type="email"
id="email"
name="email"
placeholder="email"
value="">
<br>
<input type="submit"
name="login"
value="Submit">
</form>
<div id="recovery">
<hr>
<br>
<a href="recovery.php">Forgotten Username or Password?</a>
<br>
</div>
</div>
<script>
$('div#recovery').hide();
<?php
if ($status == 'badcombo'):
// Incorrect username/password combination entered ?>
input_error($('input#username, input#password'));
msg(['Incorrect username & password combination'], 'error');
$('div#recovery').show();
<?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');
$('div#recovery').show();
<?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");
?>