save proper parsing

This commit is contained in:
Flatlogic Bot 2025-09-18 12:31:58 +00:00
parent 7d516df61b
commit e200fc0f11
2 changed files with 68 additions and 17 deletions

View File

@ -1,30 +1,42 @@
<?php <?php
session_start(); session_start();
$password = 'flatlogic1863'; // Hardcoded password for demonstration
$password = 'flatlogic1863'; // Replace with a secure password management
$error = ''; $error = '';
$message = ''; $message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Handle login
if (isset($_POST['password']) && $_POST['password'] === $password) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
if ($_POST['password'] === $password) {
$_SESSION['loggedin'] = true; $_SESSION['loggedin'] = true;
} elseif (isset($_POST['cleanup']) && isset($_SESSION['loggedin']) && $_SESSION['loggedin']) { header('Location: results.php'); // Redirect to avoid form resubmission
exit;
} else {
$error = 'Invalid password';
}
}
// Handle cleanup
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cleanup'])) {
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']) {
require_once 'db/config.php'; require_once 'db/config.php';
$pdo = db(); $pdo = db();
$pdo->exec('TRUNCATE TABLE votes'); $pdo->exec('TRUNCATE TABLE votes');
$message = 'All votes have been cleared.'; $message = 'All votes have been cleared.';
} else { } else {
if (!isset($_SESSION['loggedin']) || !$_SESSION['loggedin']) { // Optional: handle unauthorized cleanup attempt
$error = 'Invalid password';
}
} }
} }
// Handle logout
if (isset($_GET['logout'])) { if (isset($_GET['logout'])) {
session_destroy(); session_destroy();
header('Location: results.php'); header('Location: results.php');
exit; exit;
} }
$is_loggedin = isset($_SESSION['loggedin']) && $_SESSION['loggedin'];
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -37,7 +49,7 @@ if (isset($_GET['logout'])) {
</head> </head>
<body> <body>
<div class="container mt-5"> <div class="container mt-5">
<?php if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']): ?> <?php if ($is_loggedin): ?>
<div class="d-flex justify-content-between align-items-center mb-4"> <div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="mb-0">Voting Results</h1> <h1 class="mb-0">Voting Results</h1>
<a href="results.php?logout=true" class="btn btn-secondary">Logout</a> <a href="results.php?logout=true" class="btn btn-secondary">Logout</a>
@ -57,9 +69,10 @@ if (isset($_GET['logout'])) {
$pdo = db(); $pdo = db();
$stmt = $pdo->query("SELECT website_number, COUNT(*) as votes FROM votes WHERE category = 'beauty' GROUP BY website_number ORDER BY votes DESC"); $stmt = $pdo->query("SELECT website_number, COUNT(*) as votes FROM votes WHERE category = 'beauty' GROUP BY website_number ORDER BY votes DESC");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($results)): ?> if (empty($results)):
<p>No votes yet.</p> echo "<p>No votes yet for this category.</p>";
<?php else: ?> else:
?>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
<?php foreach ($results as $row): ?> <?php foreach ($results as $row): ?>
<li class="list-group-item d-flex justify-content-between align-items-center"> <li class="list-group-item d-flex justify-content-between align-items-center">
@ -79,9 +92,10 @@ if (isset($_GET['logout'])) {
<?php <?php
$stmt = $pdo->query("SELECT website_number, COUNT(*) as votes FROM votes WHERE category = 'funny' GROUP BY website_number ORDER BY votes DESC"); $stmt = $pdo->query("SELECT website_number, COUNT(*) as votes FROM votes WHERE category = 'funny' GROUP BY website_number ORDER BY votes DESC");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($results)): ?> if (empty($results)):
<p>No votes yet.</p> echo "<p>No votes yet for this category.</p>";
<?php else: ?> else:
?>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
<?php foreach ($results as $row): ?> <?php foreach ($results as $row): ?>
<li class="list-group-item d-flex justify-content-between align-items-center"> <li class="list-group-item d-flex justify-content-between align-items-center">
@ -96,6 +110,42 @@ if (isset($_GET['logout'])) {
</div> </div>
</div> </div>
<div class="card mt-4">
<div class="card-body">
<h2 class="card-title">Raw Votes</h2>
<?php
$stmt = $pdo->query("SELECT id, voter_name, category, website_number, created_at FROM votes ORDER BY created_at DESC");
$raw_votes = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($raw_votes)):
echo "<p>No votes have been cast yet.</p>";
else:
?>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Voter Name</th>
<th>Category</th>
<th>Website Number</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
<?php foreach ($raw_votes as $vote): ?>
<tr>
<td><?php echo htmlspecialchars($vote['id']); ?></td>
<td><?php echo htmlspecialchars($vote['voter_name']); ?></td>
<td><?php echo htmlspecialchars($vote['category']); ?></td>
<td><?php echo htmlspecialchars($vote['website_number']); ?></td>
<td><?php echo htmlspecialchars($vote['created_at']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
<div class="mt-4"> <div class="mt-4">
<form method="POST" onsubmit="return confirm('Are you sure you want to delete all votes?');"> <form method="POST" onsubmit="return confirm('Are you sure you want to delete all votes?');">
<button type="submit" name="cleanup" value="true" class="btn btn-danger">Clear All Votes</button> <button type="submit" name="cleanup" value="true" class="btn btn-danger">Clear All Votes</button>
@ -108,6 +158,7 @@ if (isset($_GET['logout'])) {
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<h1 class="card-title text-center">View Results</h1> <h1 class="card-title text-center">View Results</h1>
<p class="text-center text-muted">You must be logged in to see the voting results.</p>
<form method="POST"> <form method="POST">
<div class="mb-3"> <div class="mb-3">
<label for="password" class="form-label">Password</label> <label for="password" class="form-label">Password</label>
@ -115,7 +166,7 @@ if (isset($_GET['logout'])) {
</div> </div>
<button type="submit" class="btn btn-primary w-100">Login</button> <button type="submit" class="btn btn-primary w-100">Login</button>
<?php if ($error): ?> <?php if ($error): ?>
<p class="text-danger mt-2"><?php echo $error; ?></p> <p class="text-danger mt-2 text-center"><?php echo $error; ?></p>
<?php endif; ?> <?php endif; ?>
</form> </form>
</div> </div>

View File

@ -30,7 +30,7 @@ if (!$data) {
$voterName = $data['voter_name'] ?? ''; $voterName = $data['voter_name'] ?? '';
$category = $data['category'] ?? ''; $category = $data['category'] ?? '';
$website_numbers_str = $data['website_numbers'] ?? ''; $website_numbers_str = $data['website_numbers'] ?? '';
$website_numbers = !empty($website_numbers_str) ? explode(',', $website_numbers_str) : []; $website_numbers = !empty($website_numbers_str) ? array_map('trim', explode(',', $website_numbers_str)) : [];
if (empty($voterName) || empty($category) || !in_array($category, ['beauty', 'funny'])) { if (empty($voterName) || empty($category) || !in_array($category, ['beauty', 'funny'])) {
echo json_encode(['success' => false, 'error' => 'Invalid data provided.']); echo json_encode(['success' => false, 'error' => 'Invalid data provided.']);