148 lines
5.7 KiB
PHP
148 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Core\Database;
|
|
|
|
require_once dirname(__DIR__) . '/app/bootstrap.php';
|
|
|
|
$isLocal = app_env('APP_ENV', 'production') === 'local';
|
|
$expectedToken = app_env('INSTALL_TOKEN', '');
|
|
$actualToken = $_GET['token'] ?? '';
|
|
$canRun = $isLocal && $expectedToken !== '' && hash_equals($expectedToken, (string) $actualToken);
|
|
$result = null;
|
|
$error = null;
|
|
$stats = install_stats();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!$canRun) {
|
|
http_response_code(403);
|
|
$error = 'Доступ запрещен. Проверьте APP_ENV и INSTALL_TOKEN.';
|
|
} else {
|
|
try {
|
|
$pdo = Database::pdo();
|
|
run_sql_file($pdo, base_path('database/schema.sql'));
|
|
run_sql_file($pdo, base_path('database/seed.sql'));
|
|
$stats = install_stats();
|
|
$result = 'База данных установлена. Таблицы и стартовые данные созданы.';
|
|
} catch (Throwable $exception) {
|
|
http_response_code(500);
|
|
$error = app_env('APP_DEBUG', 'false') === 'true'
|
|
? $exception->getMessage()
|
|
: 'Ошибка установки базы данных.';
|
|
}
|
|
}
|
|
}
|
|
|
|
function install_stats(): array
|
|
{
|
|
try {
|
|
$pdo = Database::pdo();
|
|
$tables = $pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
|
|
$categoryCount = in_array('categories', $tables, true)
|
|
? (int) $pdo->query('SELECT COUNT(*) FROM categories')->fetchColumn()
|
|
: 0;
|
|
$firstCategories = in_array('categories', $tables, true)
|
|
? $pdo->query('SELECT name FROM categories WHERE parent_id IS NULL ORDER BY sort_order LIMIT 12')->fetchAll(PDO::FETCH_COLUMN)
|
|
: [];
|
|
|
|
return [
|
|
'ok' => true,
|
|
'database' => app_env('DB_DATABASE', ''),
|
|
'tables_count' => count($tables),
|
|
'category_count' => $categoryCount,
|
|
'first_categories' => $firstCategories,
|
|
];
|
|
} catch (Throwable $exception) {
|
|
return [
|
|
'ok' => false,
|
|
'error' => $exception->getMessage(),
|
|
];
|
|
}
|
|
}
|
|
|
|
function run_sql_file(PDO $pdo, string $path): void
|
|
{
|
|
if (!is_file($path)) {
|
|
throw new RuntimeException('SQL file not found: ' . $path);
|
|
}
|
|
|
|
$sql = file_get_contents($path);
|
|
|
|
if ($sql === false) {
|
|
throw new RuntimeException('Cannot read SQL file: ' . $path);
|
|
}
|
|
|
|
$pdo->exec($sql);
|
|
}
|
|
?>
|
|
<!doctype html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Установка базы Рыбсток</title>
|
|
<link rel="stylesheet" href="<?= e(versioned_asset('/assets/css/app.css')) ?>">
|
|
</head>
|
|
<body>
|
|
<main class="page">
|
|
<section class="start-screen">
|
|
<p class="eyebrow">Шаг 2</p>
|
|
<h1>Установка базы</h1>
|
|
<p>Эта страница применяет `database/schema.sql` и `database/seed.sql`. Таблицы будут пересозданы.</p>
|
|
|
|
<div class="status-box">
|
|
<span>Версия install.php:</span>
|
|
<strong>2026-06-04-02</strong>
|
|
</div>
|
|
|
|
<div class="status-box">
|
|
<span>Файл:</span>
|
|
<strong><?= htmlspecialchars(__FILE__, ENT_QUOTES, 'UTF-8') ?></strong>
|
|
</div>
|
|
|
|
<?php if ($stats['ok'] ?? false): ?>
|
|
<div class="status-box">
|
|
<span>База:</span>
|
|
<strong><?= htmlspecialchars((string) $stats['database'], ENT_QUOTES, 'UTF-8') ?></strong>
|
|
</div>
|
|
<div class="status-box">
|
|
<span>Таблиц:</span>
|
|
<strong><?= htmlspecialchars((string) $stats['tables_count'], ENT_QUOTES, 'UTF-8') ?></strong>
|
|
</div>
|
|
<div class="status-box">
|
|
<span>Категорий:</span>
|
|
<strong><?= htmlspecialchars((string) $stats['category_count'], ENT_QUOTES, 'UTF-8') ?></strong>
|
|
</div>
|
|
<?php if (!empty($stats['first_categories'])): ?>
|
|
<div class="status-box">
|
|
<span>Верхние категории:</span>
|
|
<strong><?= htmlspecialchars(implode(', ', $stats['first_categories']), ENT_QUOTES, 'UTF-8') ?></strong>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<div class="status-box danger"><?= htmlspecialchars((string) ($stats['error'] ?? 'Нет статистики'), ENT_QUOTES, 'UTF-8') ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!$isLocal): ?>
|
|
<div class="status-box danger">Установка доступна только при APP_ENV=local.</div>
|
|
<?php elseif (!$canRun): ?>
|
|
<div class="status-box danger">Добавьте токен в адрес страницы: <strong>?token=...</strong></div>
|
|
<?php else: ?>
|
|
<form method="post">
|
|
<button class="primary-button" type="submit">Создать таблицы и стартовые данные</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($result !== null): ?>
|
|
<div class="status-box success"><?= htmlspecialchars($result, ENT_QUOTES, 'UTF-8') ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error !== null): ?>
|
|
<div class="status-box danger"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div>
|
|
<?php endif; ?>
|
|
</section>
|
|
</main>
|
|
</body>
|
|
</html>
|