55 lines
2.1 KiB
PHP
55 lines
2.1 KiB
PHP
<?php
|
|
include 'config.php';
|
|
|
|
// Handle the file upload
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file']) && isset($_POST['key']) && $_POST['key'] === $uploadKey) {
|
|
$fileName = basename($_FILES['file']['name']);
|
|
$uploadFilePath = $uploadDir . $fileName;
|
|
|
|
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath)) {
|
|
$dbIdentifier = $_POST['db_identifier'];
|
|
$dbVersion = $_POST['db_version'];
|
|
$size = filesize($uploadFilePath);
|
|
|
|
// Convert db_identifier to lowercase
|
|
$dbIdentifierLower = strtolower($dbIdentifier);
|
|
|
|
// Check the latest version in the database for the given identifier
|
|
$stmt = $main_db->prepare("SELECT db_version FROM archive_logs WHERE db_identifier = ? ORDER BY created DESC LIMIT 1");
|
|
$stmt->bind_param("s", $dbIdentifierLower);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$latest = $result->fetch_assoc();
|
|
$latestVersion = $latest ? $latest['db_version'] : null;
|
|
$stmt->close();
|
|
$db_changed = false;
|
|
|
|
// If the version has changed, protect the latest entry
|
|
if ($latestVersion && $dbVersion !== $latestVersion) {
|
|
$stmt = $main_db->prepare("UPDATE archive_logs SET protected = 1 WHERE db_identifier = ? AND db_version = ?");
|
|
$stmt->bind_param("ss", $dbIdentifierLower, $latestVersion);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
$db_changed = true;
|
|
}
|
|
|
|
// Insert the new archive log entry
|
|
$protected = $db_changed ? 1 : 0;
|
|
$stmt = $main_db->prepare("INSERT INTO archive_logs (archive, size, created, db_identifier, db_version, protected) VALUES (?, ?, NOW(), ?, ?, $protected)");
|
|
$stmt->bind_param("sisss", $fileName, $size, $dbIdentifierLower, $dbVersion);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
|
|
echo 'File uploaded successfully.';
|
|
|
|
// Clean up old archives
|
|
cleanOldArchives($main_db, $uploadDir, $maxAgeDate);
|
|
} else {
|
|
echo 'File upload failed.';
|
|
}
|
|
} else {
|
|
echo 'Invalid request or key.';
|
|
}
|
|
|
|
?>
|