Dash-Da-Dash-DX-2023-Fan-Pa.../update_db.php

264 lines
11 KiB
PHP
Raw Permalink Normal View History

<?php
// Path to the SQLite database file
$db_path = 'db/ddd_db.sqlite';
$config_path = 'config.cfg';
// Load password from configuration file
$password = '';
if (file_exists($config_path)) {
$config = parse_ini_file($config_path);
if (isset($config['sqlite_password'])) {
$password = $config['sqlite_password'];
}
}
// Function to display an alert message
function displayAlert($message, $type) {
echo '<div class="alert alert-' . $type . '">' . $message . '</div>';
}
// Check if the 'db' directory exists, if not, create it
if (!is_dir('db')) {
if (!mkdir('db', 0777, true)) {
die('Failed to create directories...');
}
}
// Handle password submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login_password'])) {
if ($_POST['login_password'] === $password) {
session_start();
$_SESSION['logged_in'] = true;
} else {
displayAlert('Invalid password.', 'danger');
}
}
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Score</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true): ?>
<h1 class="mt-5">Enter Password to Access</h1>
<form action="" method="post" class="mt-4">
<div class="form-group">
<label for="login_password">Password:</label>
<input type="password" class="form-control" id="login_password" name="login_password" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<?php else: ?>
<h1 class="mt-5">Add a New Score</h1>
<?php
// Process form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
if ($_POST['action'] == 'add' && isset($_POST['name']) && isset($_POST['score']) && isset($_POST['mode'])) {
if (file_exists($db_path)) {
try {
// Open the SQLite3 database file
$db = new SQLite3($db_path);
// Prepare the insert query
$insertQuery = $db->prepare('INSERT INTO scores (Name, Score, Mode, Bosses) VALUES (:name, :score, :mode, :bosses)');
$insertQuery->bindValue(':name', $_POST['name'], SQLITE3_TEXT);
$insertQuery->bindValue(':score', $_POST['score'], SQLITE3_INTEGER);
$insertQuery->bindValue(':mode', $_POST['mode'], SQLITE3_TEXT);
$insertQuery->bindValue(':bosses', $_POST['bosses'], SQLITE3_TEXT);
// Execute the query
if ($insertQuery->execute()) {
displayAlert('Score added successfully.', 'success');
} else {
displayAlert('Error adding score: ' . $db->lastErrorMsg(), 'danger');
}
// Close the database connection
$db->close();
} catch (Exception $e) {
displayAlert('Caught exception: ' . $e->getMessage(), 'danger');
} catch (Error $e) {
displayAlert('Caught error: ' . $e->getMessage(), 'danger');
}
} else {
displayAlert('Database does not exist.', 'warning');
}
}
if ($_POST['action'] == 'delete' && isset($_POST['delete_id'])) {
if (file_exists($db_path)) {
try {
// Open the SQLite3 database file
$db = new SQLite3($db_path);
// Prepare the delete query
$deleteQuery = $db->prepare('DELETE FROM scores WHERE ID = :id');
$deleteQuery->bindValue(':id', $_POST['delete_id'], SQLITE3_INTEGER);
// Execute the query
if ($deleteQuery->execute()) {
displayAlert('Score deleted successfully.', 'success');
} else {
displayAlert('Error deleting score: ' . $db->lastErrorMsg(), 'danger');
}
// Close the database connection
$db->close();
} catch (Exception $e) {
displayAlert('Caught exception: ' . $e->getMessage(), 'danger');
} catch (Error $e) {
displayAlert('Caught error: ' . $e->getMessage(), 'danger');
}
} else {
displayAlert('Database does not exist.', 'warning');
}
}
}
?>
<form action="" method="post" class="mt-4">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="score">Score:</label>
<input type="number" class="form-control" id="score" name="score" required>
</div>
<div class="form-group">
<label for="mode">Mode:</label>
<input type="text" class="form-control" id="mode" name="mode" required>
</div>
<div class="form-group">
<label for="bosses">Bosses:</label>
<input type="text" class="form-control" id="bosses" name="bosses">
</div>
<input type="hidden" name="action" value="add">
<button type="submit" class="btn btn-primary">Add Score</button>
</form>
<h2 class="mt-5">Current Scores (DX)</h2>
<table class="table table-bordered mt-3">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Score</th>
<th>Mode</th>
<th>Bosses</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
if (file_exists($db_path)) {
try {
// Open the SQLite3 database file
$db = new SQLite3($db_path);
// Query to fetch current scores for DX mode ordered by highest score
$result = $db->query("SELECT * FROM scores WHERE Mode = 'DX' ORDER BY Score DESC");
// Display the rows
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo "<tr>
<td>{$row['ID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Score']}</td>
<td>{$row['Mode']}</td>
<td>{$row['Bosses']}</td>
<td>
<form action='' method='post' style='display:inline-block;'>
<input type='hidden' name='delete_id' value='{$row['ID']}'>
<input type='hidden' name='action' value='delete'>
<button type='submit' class='btn btn-danger btn-sm'>Delete</button>
</form>
</td>
</tr>";
}
// Close the database connection
$db->close();
} catch (Exception $e) {
displayAlert('Caught exception: ' . $e->getMessage(), 'danger');
} catch (Error $e) {
displayAlert('Caught error: ' . $e->getMessage(), 'danger');
}
} else {
echo '<tr><td colspan="6">No scores found. Database does not exist.</td></tr>';
}
?>
</tbody>
</table>
<h2 class="mt-5">Current Scores (EX)</h2>
<table class="table table-bordered mt-3">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Score</th>
<th>Mode</th>
<th>Bosses</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
if (file_exists($db_path)) {
try {
// Open the SQLite3 database file
$db = new SQLite3($db_path);
// Query to fetch current scores for EX mode ordered by highest score
$result = $db->query("SELECT * FROM scores WHERE Mode = 'EX' ORDER BY Score DESC");
// Display the rows
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo "<tr>
<td>{$row['ID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Score']}</td>
<td>{$row['Mode']}</td>
<td>{$row['Bosses']}</td>
<td>
<form action='' method='post' style='display:inline-block;'>
<input type='hidden' name='delete_id' value='{$row['ID']}'>
<input type='hidden' name='action' value='delete'>
<button type='submit' class='btn btn-danger btn-sm'>Delete</button>
</form>
</td>
</tr>";
}
// Close the database connection
$db->close();
} catch (Exception $e) {
displayAlert('Caught exception: ' . $e->getMessage(), 'danger');
} catch (Error $e) {
displayAlert('Caught error: ' . $e->getMessage(), 'danger');
}
} else {
echo '<tr><td colspan="6">No scores found. Database does not exist.</td></tr>';
}
?>
</tbody>
</table>
<?php endif; ?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>