update to use single html with dynamic div

This commit is contained in:
2025-04-16 09:31:36 +02:00
parent 0dcdfa7e76
commit 1d3febeec1
21 changed files with 794 additions and 179 deletions
+29
View File
@@ -0,0 +1,29 @@
<?php
include 'db_connection.php';
session_start();
if (!isset($_SESSION['username'])) {
echo json_encode(['success' => false, 'error' => 'Not logged in.']);
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = sanitize_input($_POST['name']);
$maps_link = sanitize_input($_POST['maps_link']);
$stmt = $db->prepare("INSERT INTO Restaurant (name, maps_link) VALUES (:name, :maps_link)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':maps_link', $maps_link);
if ($stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'Restaurant added successfully!']);
exit();
} else {
echo json_encode(['success' => false, 'error' => 'Error: ' . print_r($stmt->errorInfo(), true)]);
exit();
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
exit();
}
?>
+70
View File
@@ -0,0 +1,70 @@
<?php
include 'db_connection.php';
session_start();
if (!isset($_SESSION['username'])) {
header("Location: login.php"); // Redirect if not logged in
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fk_restaurant_id = sanitize_input($_POST['fk_restaurant_id']);
$bewertung_int = sanitize_input($_POST['bewertung_int']);
$bewertung_str = sanitize_input($_POST['bewertung_str']);
$fk_user_id = $_SESSION['user_id']; // Get user ID from session
// Validate the rating (0-10)
if ($bewertung_int < 0 || $bewertung_int > 10) {
$review_error = "Rating must be between 0 and 10.";
} else {
$sql = "INSERT INTO Bewertung (fk_user_id, fk_restaurant_id, bewertung, bewertung_str) VALUES ($fk_user_id, $fk_restaurant_id, $bewertung_int, '$bewertung_str')";
if ($conn->query($sql) === TRUE) {
$review_success = "Review added successfully!";
} else {
$review_error = "Error: " . $sql . "<br>" . $conn->error;
}
}
}
// Fetch restaurants for the dropdown
$restaurants_sql = "SELECT id, name FROM Restaurant";
$restaurants_result = $conn->query($restaurants_sql);
$restaurants = [];
if ($restaurants_result->num_rows > 0) {
while ($row = $restaurants_result->fetch_assoc()) {
$restaurants[$row['id']] = $row['name'];
}
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Review</title>
</head>
<body>
<h2>Add New Review</h2>
<?php if (isset($review_error)): ?>
<p style="color:red;"><?php echo $review_error; ?></p>
<?php endif; ?>
<?php if (isset($review_success)): ?>
<p style="color:green;"><?php echo $review_success; ?></p>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Restaurant:
<select name="fk_restaurant_id" required>
<option value="">Select Restaurant</option>
<?php foreach ($restaurants as $id => $name): ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select><br><br>
Rating (0-10): <input type="number" name="bewertung_int" min="0" max="10" required><br><br>
Review Text: <textarea name="bewertung_str"></textarea><br><br>
<input type="submit" value="Add Review">
</form>
<p><a href="index.html">Back to Dashboard</a></p>
</body>
</html>
+91
View File
@@ -0,0 +1,91 @@
<?php
$databaseFile = '../mydatabase.db';
try {
$db = new PDO("sqlite:" . $databaseFile);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Check if the User table exists
$result = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='User'");
$userTableExists = $result->fetchColumn();
if (!$userTableExists) {
// Initialize the database structure
$db->exec("
CREATE TABLE User (
id INTEGER PRIMARY KEY AUTOINCREMENT,
vorname TEXT NOT NULL,
nachname TEXT NOT NULL,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
active BOOLEAN DEFAULT 0
)
");
$db->exec("
CREATE TABLE Restaurant (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
maps_link TEXT
)
");
$db->exec("
CREATE TABLE Bewertung (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fk_user_id INTEGER NOT NULL,
fk_restaurant_id INTEGER NOT NULL,
bewertung INTEGER NOT NULL CHECK (bewertung BETWEEN 0 AND 10),
bewertung_str TEXT,
FOREIGN KEY (fk_user_id) REFERENCES User(id),
FOREIGN KEY (fk_restaurant_id) REFERENCES Restaurant(id)
)
");
echo "<p style='color:green;'>Database structure created.</p>";
// Check if any users exist
$checkUserStmt = $db->query("SELECT COUNT(*) FROM User");
$userCount = $checkUserStmt->fetchColumn();
if ($userCount == 0) {
// Create an initial admin account
$adminVorname = 'Admin';
$adminNachname = 'User';
$adminUsername = 'admin';
$adminPassword = 'admin123'; // IMPORTANT: Change this default password!
$hashedPassword = password_hash($adminPassword, PASSWORD_DEFAULT);
$adminActive = 2; // Let's use 2 to denote an admin user
$insertAdminStmt = $db->prepare("
INSERT INTO User (vorname, nachname, username, password, active)
VALUES (:vorname, :nachname, :username, :password, :active)
");
$insertAdminStmt->bindParam(':vorname', $adminVorname);
$insertAdminStmt->bindParam(':nachname', $adminNachname);
$insertAdminStmt->bindParam(':username', $adminUsername);
$insertAdminStmt->bindParam(':password', $hashedPassword);
$insertAdminStmt->bindParam(':active', $adminActive, PDO::PARAM_INT);
if ($insertAdminStmt->execute()) {
echo "<p style='color:green;'>Initial admin account created: Username='admin', Password='admin123' (Please change immediately!).</p>";
} else {
echo "<p style='color:red;'>Error creating initial admin account: " . print_r($insertAdminStmt->errorInfo(), true) . "</p>";
}
}
} else {
// echo "<p style='color:blue;'>Database already exists.</p>";
}
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
+22
View File
@@ -0,0 +1,22 @@
<?php
include 'db_connection.php';
$stmt = $db->prepare("SELECT id, name, maps_link FROM Restaurant");
$stmt->execute();
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($restaurants) {
$output = "<ul>";
foreach ($restaurants as $restaurant) {
$output .= "<li>Name: {$restaurant['name']}";
if ($restaurant['maps_link']) {
$output .= " - <a href='{$restaurant['maps_link']}' target='_blank'>View on Maps</a>";
}
$output .= "</li>";
}
$output .= "</ul>";
echo $output;
} else {
echo "<p>No restaurants available.</p>";
}
?>
+37
View File
@@ -0,0 +1,37 @@
<?php
include 'db_connection.php';
// Fetch restaurants and their median scores
$stmt = $db->prepare("
SELECT
r.id,
r.name,
ROUND(MEDIAN(b.bewertung)) AS median_score
FROM Restaurant r
LEFT JOIN Bewertung b ON r.id = b.fk_restaurant_id
GROUP BY r.id, r.name
ORDER BY median_score DESC NULLS LAST
");
$stmt->execute();
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
$tiers = [];
foreach ($restaurants as $restaurant) {
$score = $restaurant['median_score'];
if ($score >= 9) {
$tiers['S'][] = $restaurant;
} elseif ($score >= 7) {
$tiers['A'][] = $restaurant;
} elseif ($score >= 5) {
$tiers['B'][] = $restaurant;
} elseif ($score >= 3) {
$tiers['C'][] = $restaurant;
} elseif ($score >= 1) {
$tiers['D'][] = $restaurant;
} else {
$tiers['Unrated'][] = $restaurant; // For restaurants with no reviews
}
}
echo json_encode($tiers);
?>
+37
View File
@@ -0,0 +1,37 @@
<?php
include 'db_connection.php';
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = sanitize_input($_POST['username']);
$password = sanitize_input($_POST['password']);
$stmt = $db->prepare("SELECT id, username, password, active FROM User WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
if ($row['active'] == 1 && password_verify($password, $row['password'])) {
$_SESSION['username'] = $row['username'];
$_SESSION['user_id'] = $row['id'];
// Set cookie
setcookie('loggedIn', 'true', time() + 600, '/'); // Expires in 10 minutes
echo json_encode(['success' => true, 'message' => 'Login successful!']);
exit();
} else if ($row['active'] == 0) {
echo json_encode(['success' => false, 'error' => 'Account is not active. Please wait for admin approval.']);
exit();
} else {
echo json_encode(['success' => false, 'error' => 'Invalid username or password.']);
exit();
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid username or password.']);
exit();
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
exit();
}
?>
+7
View File
@@ -0,0 +1,7 @@
<?php
session_start();
session_destroy();
setcookie('loggedIn', '', time() - 3600, '/'); // Clear the cookie
echo json_encode(['success' => true, 'message' => 'Logged out successfully.']);
exit();
?>
+101
View File
@@ -0,0 +1,101 @@
<?php
include 'db_connection.php';
session_start();
if (!isset($_SESSION['username'])) {
// header("Location: ../login.php"); // Redirect to login
echo json_encode(['success' => false, 'error' => 'Not logged in.']);
exit();
}
// Check if the user is an admin (you'd likely have an 'admin' column in your User table)
$check_admin_stmt = $db->prepare("SELECT id FROM User WHERE username = :username AND active = 2"); // 2 for admin
$check_admin_stmt->bindParam(':username', $_SESSION['username']);
$check_admin_stmt->execute();
$is_admin = $check_admin_stmt->fetchColumn();
if (!$is_admin) {
echo json_encode(['success' => false, 'error' => 'Unauthorized access.']);
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST['action'] == 'process') {
$user_id = sanitize_input($_POST['user_id']);
$accept = sanitize_input($_POST['accept']);
$update_stmt = $db->prepare("UPDATE User SET active = :active WHERE id = :user_id");
$update_stmt->bindParam(':active', $accept, PDO::PARAM_BOOL);
$update_stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
if ($update_stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'User status updated.']);
exit();
} else {
echo json_encode(['success' => false, 'error' => 'Error updating user status: ' . print_r($update_stmt->errorInfo(), true)]);
exit();
}
} else if ($_POST['action'] == 'change_password') {
$old_password = sanitize_input($_POST['old_password']);
$new_password = sanitize_input($_POST['new_password']);
$confirm_new_password = sanitize_input($_POST['confirm_new_password']);
if ($new_password != $confirm_new_password) {
echo json_encode(['success' => false, 'error' => 'New passwords do not match.']);
exit();
}
$user_id = $_SESSION['user_id'];
$get_user_stmt = $db->prepare("SELECT password FROM User WHERE id = :user_id");
$get_user_stmt->bindParam(':user_id', $user_id);
$get_user_stmt->execute();
$row = $get_user_stmt->fetch(PDO::FETCH_ASSOC);
if($row){
if (password_verify($old_password, $row['password'])) {
$hashed_new_password = password_hash($new_password, PASSWORD_DEFAULT);
$update_password_stmt = $db->prepare("UPDATE User SET password = :new_password WHERE id = :user_id");
$update_password_stmt->bindParam(':new_password', $hashed_new_password);
$update_password_stmt->bindParam(':user_id', $user_id);
if ($update_password_stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'Password changed successfully.']);
exit();
} else {
echo json_encode(['success' => false, 'error' => 'Error changing password: ' . print_r($update_password_stmt->errorInfo(), true)]);
exit();
}
} else {
echo json_encode(['success' => false, 'error' => 'Incorrect old password.']);
exit();
}
}
else{
echo json_encode(['success' => false, 'error' => 'User not found.']);
exit();
}
}
} else if ($_SERVER["REQUEST_METHOD"] == "GET" && $_GET['action'] == 'get_requests') {
$get_requests_stmt = $db->prepare("SELECT id, vorname, nachname, username FROM User WHERE active = 0");
$get_requests_stmt->execute();
$requests = $get_requests_stmt->fetchAll(PDO::FETCH_ASSOC);
if ($requests) {
$output = "<ul>";
foreach ($requests as $request) {
$output .= "<li>{$request['vorname']} {$request['nachname']} ({$request['username']}) -
<button class='accept-btn' data-user-id='{$request['id']}'>Accept</button>
<button class='reject-btn' data-user-id='{$request['id']}'>Reject</button>
</li>";
}
$output .= "</ul>";
echo $output;
exit();
} else {
echo "<p>No pending registration requests.</p>";
exit();
}
}else {
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
exit();
}
?>
+35
View File
@@ -0,0 +1,35 @@
<?php
include 'db_connection.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$vorname = sanitize_input($_POST['vorname']);
$nachname = sanitize_input($_POST['nachname']);
$username = sanitize_input($_POST['username']);
// Check if the username already exists
$check_stmt = $db->prepare("SELECT username FROM User WHERE username = :username");
$check_stmt->bindParam(':username', $username);
$check_stmt->execute();
if ($check_stmt->fetchColumn()) {
echo json_encode(['success' => false, 'error' => 'Username already exists.']);
exit();
} else {
$insert_stmt = $db->prepare("INSERT INTO User (vorname, nachname, username) VALUES (:vorname, :nachname, :username)");
$insert_stmt->bindParam(':vorname', $vorname);
$insert_stmt->bindParam(':nachname', $nachname);
$insert_stmt->bindParam(':username', $username);
if ($insert_stmt->execute()) {
echo json_encode(['success' => true, 'message' => 'Registration request sent. Please wait for admin approval.']);
exit();
} else {
echo json_encode(['success' => false, 'error' => 'Error: ' . print_r($insert_stmt->errorInfo(), true)]);
exit();
}
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid request method.']);
exit();
}
?>