Files
backPRO/install.php
2026-02-15 11:37:27 +01:00

80 lines
2.3 KiB
PHP

<?php
/**
* BackPRO - Installation script
* Run this once after deploying to set up the database and create admin user.
* DELETE THIS FILE after installation!
*/
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
\App\Core\Config::load(__DIR__);
echo "=== BackPRO Installer ===\n\n";
// Check if running from CLI or web
$isCli = php_sapi_name() === 'cli';
$nl = $isCli ? "\n" : "<br>";
try {
$db = \App\Core\Database::getInstance();
echo "Połączenie z bazą danych: OK{$nl}";
} catch (\Exception $e) {
echo "BŁĄD połączenia z bazą: " . $e->getMessage() . $nl;
echo "Sprawdź konfigurację w pliku .env{$nl}";
exit(1);
}
// Run all migrations in order
$migrationFiles = glob(__DIR__ . '/migrations/*.sql');
sort($migrationFiles);
foreach ($migrationFiles as $migrationFile) {
$migrationName = basename($migrationFile);
$sql = file_get_contents($migrationFile);
$sqlClean = preg_replace('/--.*$/m', '', $sql);
$statements = array_filter(
array_map('trim', explode(';', $sqlClean)),
fn($s) => !empty($s)
);
foreach ($statements as $statement) {
try {
$db->exec($statement);
} catch (\PDOException $e) {
if (!str_contains($e->getMessage(), 'already exists') && !str_contains($e->getMessage(), 'Duplicate')) {
echo "SQL Warning ({$migrationName}): " . $e->getMessage() . $nl;
}
}
}
echo "Migracja {$migrationName}: OK{$nl}";
}
// Create admin user
$username = 'admin';
$password = 'admin123'; // Change this!
$hash = password_hash($password, PASSWORD_BCRYPT);
try {
$stmt = $db->prepare("SELECT id FROM users WHERE username = :u");
$stmt->execute(['u' => $username]);
if ($stmt->fetch()) {
echo "Użytkownik '{$username}' już istnieje.{$nl}";
} else {
$stmt = $db->prepare("INSERT INTO users (username, password_hash) VALUES (:u, :p)");
$stmt->execute(['u' => $username, 'p' => $hash]);
echo "Utworzono użytkownika: {$username} / {$password}{$nl}";
echo "WAŻNE: Zmień hasło po pierwszym logowaniu!{$nl}";
}
} catch (\PDOException $e) {
echo "Błąd tworzenia użytkownika: " . $e->getMessage() . $nl;
}
echo "{$nl}=== Instalacja zakończona ==={$nl}";
echo "USUŃ PLIK install.php po zakończeniu instalacji!{$nl}";