mirror of
https://github.com/michalcz10/USB-RAID-Array.git
synced 2026-11-11 16:09:03 +00:00
revise backend
This commit is contained in:
+46
-78
@@ -1,83 +1,51 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if (isset($_POST['uname'], $_POST['pswd'])) {
|
||||
require_once __DIR__ . '/../config/config.php';
|
||||
// $username = "UNAME";
|
||||
// $password = "PSWD";
|
||||
// $db = "usbraidlogin";
|
||||
|
||||
$uname = htmlspecialchars($_POST['uname']);
|
||||
$pswd = htmlspecialchars($_POST['pswd']);
|
||||
|
||||
$conn = getDatabaseConnection();
|
||||
// charset already set in getDatabaseConnection()
|
||||
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM users WHERE uname=?";
|
||||
$stmt = $conn->prepare($sql);
|
||||
if (!$stmt) {
|
||||
$_SESSION['login_error'] = "Database error. Please try again.";
|
||||
$conn->close();
|
||||
header("Location: ../index.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
$stmt->bind_param("s", $uname);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result && $result->num_rows > 0) {
|
||||
$login_successful = false;
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$hash = $row["pswd"];
|
||||
$admin = (bool)$row["admin"];
|
||||
$defPath = $row["defPath"];
|
||||
$delPer = $row["delPer"];
|
||||
$downPer = $row["downPer"];
|
||||
$upPer = $row["upPer"];
|
||||
|
||||
if (password_verify($pswd, $hash)) {
|
||||
// Login successful - store info in session
|
||||
$_SESSION['uname'] = $uname;
|
||||
$_SESSION['admin'] = $admin;
|
||||
$_SESSION['defPath'] = $defPath;
|
||||
$_SESSION['downPer'] = $downPer;
|
||||
$_SESSION['delPer'] = $delPer;
|
||||
$_SESSION['upPer'] = $upPer;
|
||||
$login_successful = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$result->free_result();
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
|
||||
if ($login_successful) {
|
||||
header("Location: ftp/index.php");
|
||||
exit();
|
||||
} else {
|
||||
// If password verification fails
|
||||
$_SESSION['login_error'] = "Invalid username or password.";
|
||||
header("Location: ../index.php");
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
// If no user is found
|
||||
$result->free_result();
|
||||
$stmt->close();
|
||||
$conn->close();
|
||||
$_SESSION['login_error'] = "Invalid username or password.";
|
||||
header("Location: ../index.php");
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
// If form fields are not set
|
||||
$_SESSION['login_error'] = "Please fill in all fields.";
|
||||
header("Location: ../index.php");
|
||||
exit();
|
||||
if (!isset($_POST['uname'], $_POST['pswd'])
|
||||
|| !is_string($_POST['uname']) || !is_string($_POST['pswd'])) {
|
||||
$_SESSION['login_error'] = 'Please fill in all fields.';
|
||||
header('Location: ../index.php');
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
$username = trim($_POST['uname']);
|
||||
$password = $_POST['pswd'];
|
||||
$loginSuccessful = false;
|
||||
$database = getDatabaseConnection();
|
||||
|
||||
try {
|
||||
$statement = $database->prepare('SELECT uname, pswd, admin, defPath, downPer, delPer, upPer FROM users WHERE uname = ?');
|
||||
$statement->bind_param('s', $username);
|
||||
$statement->execute();
|
||||
$result = $statement->get_result();
|
||||
|
||||
while ($user = $result->fetch_assoc()) {
|
||||
if (!password_verify($password, $user['pswd'])) {
|
||||
continue;
|
||||
}
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['uname'] = $user['uname'];
|
||||
$_SESSION['admin'] = (bool) $user['admin'];
|
||||
$_SESSION['defPath'] = $user['defPath'];
|
||||
foreach (['downPer', 'delPer', 'upPer'] as $permission) {
|
||||
$_SESSION[$permission] = (bool) $user[$permission];
|
||||
}
|
||||
unset($_SESSION['login_error']);
|
||||
$loginSuccessful = true;
|
||||
break;
|
||||
}
|
||||
$result->free();
|
||||
$statement->close();
|
||||
if (!$loginSuccessful) {
|
||||
$_SESSION['login_error'] = 'Invalid username or password.';
|
||||
}
|
||||
} catch (mysqli_sql_exception $exception) {
|
||||
error_log($exception->getMessage());
|
||||
$_SESSION['login_error'] = 'Database error. Please try again.';
|
||||
} finally {
|
||||
$database->close();
|
||||
}
|
||||
|
||||
header('Location: ' . ($loginSuccessful ? 'ftp/index.php' : '../index.php'));
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user