-
Notifications
You must be signed in to change notification settings - Fork 1
/
security_functions.php
266 lines (231 loc) · 8.04 KB
/
security_functions.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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Function to sanitize input data
function sanitizeInput($input) {
global $conn; // Access the global $conn variable
// Remove HTML and PHP tags
$input = strip_tags($input);
// Prevent SQL injection
$input = mysqli_real_escape_string($conn, $input);
// Return sanitized input
return $input;
}
// Function to hash passwords
function hashPassword($password) {
// Use PHP's password_hash function to hash the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
return $hashedPassword;
}
// Function to verify hashed password
function verifyPassword($password, $hashedPassword) {
// Use PHP's password_verify function to verify the password
$passwordMatch = password_verify($password, $hashedPassword);
return $passwordMatch;
}
// Function to generate a random string (for CSRF tokens, etc.)
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
// Function to log security actions
function logSecurityAction($action, $details) {
global $conn;
// Capture additional information
$ip_address = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// Escape details to prevent SQL injection
$escaped_details = mysqli_real_escape_string($conn, $details);
// Insert data into the database
$sql = "INSERT INTO student_logger (action, details, ip_address, user_agent)
VALUES ('$action', '$escaped_details', '$ip_address', '$user_agent')";
if (mysqli_query($conn, $sql)) {
return true; // Success
} else {
return false; // Error
}
}
// Function to check login attempts
function checkLoginAttempts($collegeId) {
global $conn;
$query = "SELECT login_attempts FROM college_students WHERE college_id = '$collegeId'";
$result = mysqli_query($conn, $query);
if ($result && mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
return $row['login_attempts'];
} else {
return 0;
}
}
// Function to increment login attempts
function incrementLoginAttempts($collegeId) {
global $conn;
$query = "UPDATE college_students SET login_attempts = login_attempts + 1 WHERE college_id = '$collegeId'";
mysqli_query($conn, $query);
}
// Function to check login timer
function checkLoginTimer($collegeId) {
global $conn;
$query = "SELECT login_timer FROM college_students WHERE college_id = '$collegeId'";
$result = mysqli_query($conn, $query);
if ($result && mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
$loginTimer = strtotime($row['login_timer']);
$currentTimestamp = time();
if ($currentTimestamp - $loginTimer < 60) { // 60 seconds lock time
return true;
}
}
return false;
}
// Function to reset login timer
function resetLoginTimer($collegeId) {
global $conn;
$query = "UPDATE college_students SET login_timer = NULL WHERE college_id = '$collegeId'";
mysqli_query($conn, $query);
}
// Function to reset login attempts
function resetLoginAttempts($collegeId) {
global $conn;
$query = "UPDATE college_students SET login_attempts = 0 WHERE college_id = '$collegeId'";
mysqli_query($conn, $query);
}
// Function to update the status of a student
function updateStudentStatus($collegeId, $status) {
global $conn;
// Check if the provided status is valid
if (!in_array($status, array('Online', 'Offline'))) {
return false; // Invalid status
}
// Update the status in the database
$query = "UPDATE college_students SET status = '$status' WHERE college_id = '$collegeId'";
if (mysqli_query($conn, $query)) {
return true; // Success
} else {
return false; // Error
}
}
function fetchDataFromTable($tableName, $fromColumn, $searchValue, $selectColumns = '*') {
global $conn;
// Prepare the SQL statement
$stmt = $conn->prepare("SELECT $selectColumns FROM $tableName WHERE $fromColumn = ?");
if ($stmt === false) {
return false; // Error in preparing the statement
}
// Bind parameters and execute the statement
$stmt->bind_param("s", $searchValue);
$stmt->execute();
// Get the result
$result = $stmt->get_result();
if ($result === false) {
return false; // Error in getting the result
}
// Fetch data from the result set
$data = $result->fetch_assoc();
// Close the statement
$stmt->close();
return $data;
}
// Function to check session duration and validity
function validateSession() {
if (isset($_SESSION['start_time']) && isset($_SESSION['college_id'])) {
global $conn; // Ensure the database connection is available
$sessionDuration = 60 * 60; // 60 minutes
$currentTime = time();
$startTime = $_SESSION['start_time'];
$collegeId = $_SESSION['college_id'];
// If session has been active for more than 60 minutes, log out and update status to 'Offline'
if ($currentTime - $startTime >= $sessionDuration) {
// Update student status to 'Offline'
updateStudentStatus($collegeId, 'Offline');
// Destroy session
session_unset();
session_destroy();
// Redirect to login page with session expired message
showMessage("Login session expired. Please log in again.", "login.html");
exit(); // Stop further execution
}
}
}
// Function to show a message and redirect after 5 seconds
function showMessage($message, $redirectURL) {
$randomQuotes = array(
"The only way to do great work is to love what you do. – Steve Jobs",
"Success is not final, failure is not fatal: It is the courage to continue that counts. – Winston Churchill",
"Believe you can and you're halfway there. – Theodore Roosevelt",
"Your limitation—it's only your imagination.",
"Push yourself, because no one else is going to do it for you.",
"Great things never come from comfort zones.",
"Dream it. Wish it. Do it.",
"Success doesn’t just find you. You have to go out and get it.",
"The harder you work for something, the greater you’ll feel when you achieve it."
);
$randomQuote = $randomQuotes[array_rand($randomQuotes)];
echo <<<EOD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loading...</title>
<style>
/* Styles for full-page loader */
.loader-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
color: white;
font-family: Arial, sans-serif;
z-index: 9999;
}
.loader {
border: 8px solid #f3f3f3;
border-radius: 50%;
border-top: 8px solid #3498db;
width: 50px;
height: 50px;
animation: spin 2s linear infinite;
margin-bottom: 20px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.message {
font-size: 24px;
margin-bottom: 20px;
text-align: center;
}
.quote {
font-style: italic;
text-align: center;
}
</style>
</head>
<body>
<div class="loader-container">
<div class="loader"></div>
<div class="message">$message</div>
<div class="quote">$randomQuote</div>
</div>
<script>
setTimeout(function() {
window.location.href = '$redirectURL';
}, 5000); // Redirect after 5 seconds
</script>
</body>
</html>
EOD;
}
?>