db_archive/config.php.template
2024-07-18 17:31:32 -07:00

114 lines
3.2 KiB
Plaintext

<?php
$dbHost = "";
$dbPort = "3311";
$dbUser = "";
$dbPass = "";
$dbName = "db_archives";
$uploadKey = 'your_secure_upload_key';
$uploadDir = './uploads/';
$maxAgeDays = 30;
$main_db = new mysqli("$dbHost:$dbPort", $dbUser, $dbPass, $dbName);
$currentDate = new DateTime();
$maxAgeInterval = new DateInterval('P' . $maxAgeDays . 'D');
$maxAgeDate = $currentDate->sub($maxAgeInterval)->format('Y-m-d H:i:s');
if ($main_db->connect_error) {
die("Connection failed: " . $main_db->connect_error);
}
$fullNames = [
'Classic' => 'Classic EverQuest',
'Kunark' => 'Ruins of Kunark',
'Velious' => 'Scars of Velious',
'Luclin' => 'Shadows of Luclin',
'Planes' => 'Planes of Power',
// Add more expansions as needed
];
// Function to check if table exists and create it if it doesn't
function createArchiveLogsTable($db) {
$query = "CREATE TABLE IF NOT EXISTS archive_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
downloads INT DEFAULT 0,
archive VARCHAR(255),
size BIGINT,
created DATETIME,
db_identifier VARCHAR(50),
db_version varchar(255),
protected tinyint(1) NOT NULL DEFAULT 0
)";
if ($db->query($query) === FALSE) {
echo "Error creating table: " . $db->error . "<br>";
}
}
// Check and create the table
createArchiveLogsTable($main_db);
function formatSize($bytes) {
return number_format($bytes / 1048576, 2) . ' MB';
}
function timeElapsedString($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = [
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
];
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
function cleanOldArchives($db, $uploadDir, $maxAgeDate) {
// Get all non-protected archives older than the maximum age
$stmt = $db->prepare("SELECT id, archive, created FROM archive_logs WHERE created < ? AND protected = 0");
$stmt->bind_param("s", $maxAgeDate);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$archiveDate = new DateTime($row['created']);
$dayOfWeek = $archiveDate->format('N'); // 1 (for Monday) through 7 (for Sunday)
// Delete the archive if it's Monday-Saturday or if it's older than 30 days
if ($dayOfWeek != 7 || $archiveDate < $maxAgeDate) {
$archivePath = $uploadDir . $row['archive'];
// Delete the file
if (file_exists($archivePath)) {
unlink($archivePath);
}
// Delete the database entry
$deleteStmt = $db->prepare("DELETE FROM archive_logs WHERE id = ?");
$deleteStmt->bind_param("i", $row['id']);
$deleteStmt->execute();
$deleteStmt->close();
}
}
$stmt->close();
}
?>