mirror of
https://github.com/fadlee/bigdump.git
synced 2025-10-25 13:46:53 +02:00
I've converted the original `bigdump.php` script into an object-oriented application with a clear separation of concerns. Key changes include: - A new directory structure (`src`, `public`, `templates`, `config`). - Object-oriented code with classes for `Configuration`, `Database`, `FileHandler`, and `Dumper`. - Separation of HTML, CSS, and JavaScript from the PHP logic. - Improved security by mitigating XSS and file path traversal risks. - A new `README.md` with updated instructions. - Unit tests for the core classes (written but not run due to environment constraints).
148 lines
4.7 KiB
PHP
148 lines
4.7 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../autoloader.php';
|
|
|
|
|
|
use BigDump\Configuration;
|
|
use BigDump\Database;
|
|
use BigDump\FileHandler;
|
|
use BigDump\Dumper;
|
|
|
|
// Basic error handling
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
session_start();
|
|
|
|
// Load configuration
|
|
$config = new Configuration();
|
|
|
|
// Instantiate core classes
|
|
try {
|
|
$db = new Database($config);
|
|
$fileHandler = new FileHandler($config);
|
|
$dumper = new Dumper($config, $db, $fileHandler);
|
|
} catch (\Exception $e) {
|
|
die("Error during initialization: " . $e->getMessage());
|
|
}
|
|
|
|
|
|
// Simple router
|
|
$action = $_GET['action'] ?? 'start';
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'upload':
|
|
if (isset($_FILES['dumpfile'])) {
|
|
$fileHandler->handleUpload($_FILES['dumpfile']);
|
|
$_SESSION['success'] = "File uploaded successfully.";
|
|
}
|
|
header('Location: .');
|
|
exit;
|
|
|
|
case 'delete':
|
|
if (isset($_GET['file'])) {
|
|
$fileHandler->deleteFile($_GET['file']);
|
|
$_SESSION['success'] = "File deleted successfully.";
|
|
}
|
|
header('Location: .');
|
|
exit;
|
|
|
|
case 'import':
|
|
$filename = $_GET['file'] ?? '';
|
|
if (empty($filename)) {
|
|
throw new \Exception("No file specified for import.");
|
|
}
|
|
|
|
$start_line = (int)($_GET['start_line'] ?? 1);
|
|
$foffset = (int)($_GET['foffset'] ?? 0);
|
|
$totalqueries = (int)($_GET['totalqueries'] ?? 0);
|
|
$delimiter = $_GET['delimiter'] ?? null;
|
|
|
|
if ($config->ajax) {
|
|
// AJAX request
|
|
if (isset($_GET['ajax_request'])) {
|
|
header('Content-Type: application/json');
|
|
$dumper->prepare($filename, $start_line, $foffset, $totalqueries, $delimiter);
|
|
$result = $dumper->run();
|
|
echo json_encode($result);
|
|
exit;
|
|
}
|
|
|
|
// Initial page for AJAX import
|
|
$content = renderTemplate('import.php', [
|
|
'filename' => $filename,
|
|
'start_line' => $start_line,
|
|
'delaypersession' => $config->delaypersession,
|
|
]);
|
|
echo renderTemplate('layout.php', ['content' => $content]);
|
|
|
|
} else {
|
|
// No-JS fallback
|
|
$dumper->prepare($filename, $start_line, $foffset, $totalqueries, $delimiter);
|
|
$result = $dumper->run();
|
|
|
|
if ($result['finished']) {
|
|
$_SESSION['success'] = 'Import finished successfully!';
|
|
header('Location: .');
|
|
exit;
|
|
} else {
|
|
// Redirect to continue
|
|
$query_params = http_build_query([
|
|
'action' => 'import',
|
|
'file' => $filename,
|
|
'start_line' => $result['linenumber'],
|
|
'foffset' => $result['foffset'],
|
|
'totalqueries' => $result['totalqueries'],
|
|
'delimiter' => $result['delimiter'],
|
|
]);
|
|
// To prevent browser caching the redirect
|
|
header("Location: ?{$query_params}", true, 302);
|
|
exit;
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'start':
|
|
default:
|
|
$files = $fileHandler->getAvailableDumps();
|
|
$db_connection_charset = '';
|
|
try {
|
|
if (!empty($config->db_name)) {
|
|
$db->connect();
|
|
$db_connection_charset = $db->getVariable('character_set_connection');
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Ignore db connection errors on start page
|
|
}
|
|
|
|
$error = $_SESSION['error'] ?? null;
|
|
$success = $_SESSION['success'] ?? null;
|
|
unset($_SESSION['error'], $_SESSION['success']);
|
|
|
|
$content = renderTemplate('start.php', [
|
|
'files' => $files,
|
|
'db_connection_charset' => $db_connection_charset,
|
|
'error' => $error,
|
|
'success' => $success,
|
|
]);
|
|
echo renderTemplate('layout.php', ['content' => $content]);
|
|
break;
|
|
}
|
|
} catch (\Exception $e) {
|
|
$_SESSION['error'] = 'An error occurred: ' . $e->getMessage();
|
|
//header('Location: .');
|
|
//exit;
|
|
echo "An error occurred: " . $e->getMessage();
|
|
}
|
|
|
|
|
|
function renderTemplate(string $templateName, array $data = []): string
|
|
{
|
|
$templatePath = __DIR__ . '/../templates/' . $templateName;
|
|
ob_start();
|
|
extract($data);
|
|
require $templatePath;
|
|
return ob_get_clean();
|
|
}
|