214 lines
10 KiB
PHP
214 lines
10 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/site.php';
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
function installer_source_label(string $source): string
|
|
{
|
|
return match ($source) {
|
|
'env' => 'environment server',
|
|
'file' => 'file installer lokal',
|
|
default => 'belum ada',
|
|
};
|
|
}
|
|
|
|
$status = site_installation_status();
|
|
$nextPath = safe_install_next_path((string)($_REQUEST['next'] ?? '/'));
|
|
$formData = [
|
|
'db_host' => DB_HOST !== '' ? DB_HOST : 'localhost',
|
|
'db_port' => DB_PORT !== '' ? DB_PORT : '3306',
|
|
'db_name' => DB_NAME,
|
|
'db_user' => DB_USER,
|
|
'db_pass' => DB_PASS,
|
|
];
|
|
$errors = [];
|
|
$flash = null;
|
|
|
|
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
|
|
$formData = [
|
|
'db_host' => trim((string)($_POST['db_host'] ?? 'localhost')),
|
|
'db_port' => trim((string)($_POST['db_port'] ?? '3306')),
|
|
'db_name' => trim((string)($_POST['db_name'] ?? '')),
|
|
'db_user' => trim((string)($_POST['db_user'] ?? '')),
|
|
'db_pass' => (string)($_POST['db_pass'] ?? ''),
|
|
];
|
|
$nextPath = safe_install_next_path((string)($_POST['next'] ?? '/'));
|
|
|
|
if ($formData['db_host'] === '') {
|
|
$formData['db_host'] = 'localhost';
|
|
}
|
|
if ($formData['db_port'] === '') {
|
|
$formData['db_port'] = '3306';
|
|
}
|
|
|
|
if ($formData['db_name'] === '') {
|
|
$errors['db_name'] = 'Nama database wajib diisi.';
|
|
}
|
|
if ($formData['db_user'] === '') {
|
|
$errors['db_user'] = 'Username database wajib diisi.';
|
|
}
|
|
if (!ctype_digit($formData['db_port']) || (int)$formData['db_port'] < 1 || (int)$formData['db_port'] > 65535) {
|
|
$errors['db_port'] = 'Port harus berupa angka 1 sampai 65535.';
|
|
}
|
|
|
|
if ($errors === []) {
|
|
$settings = [
|
|
'DB_HOST' => $formData['db_host'],
|
|
'DB_PORT' => $formData['db_port'],
|
|
'DB_NAME' => $formData['db_name'],
|
|
'DB_USER' => $formData['db_user'],
|
|
'DB_PASS' => $formData['db_pass'],
|
|
];
|
|
|
|
try {
|
|
$connectionError = db_connection_error($settings);
|
|
if ($connectionError !== null && db_error_is_missing_database($connectionError)) {
|
|
db_create_database_if_missing($settings);
|
|
$connectionError = db_connection_error($settings);
|
|
}
|
|
|
|
if ($connectionError !== null) {
|
|
throw new RuntimeException($connectionError);
|
|
}
|
|
|
|
db_save_local_settings($settings);
|
|
header('Location: ' . $nextPath);
|
|
exit;
|
|
} catch (Throwable $exception) {
|
|
$flash = [
|
|
'type' => 'danger',
|
|
'title' => 'Koneksi database gagal.',
|
|
'message' => $exception->getMessage(),
|
|
];
|
|
}
|
|
} else {
|
|
$flash = [
|
|
'type' => 'warning',
|
|
'title' => 'Form belum lengkap.',
|
|
'message' => 'Periksa lagi field yang diberi tanda merah.',
|
|
];
|
|
}
|
|
}
|
|
|
|
render_page_start([
|
|
'title' => 'Installer Database',
|
|
'description' => 'Form instalasi cepat untuk menghubungkan aplikasi ke database MySQL.',
|
|
'canonical' => canonical_for('install.php'),
|
|
'robots' => 'noindex,nofollow',
|
|
'keywords' => 'installer database, setup mysql, setup php app',
|
|
'body_class' => 'install-page',
|
|
]);
|
|
?>
|
|
<main id="main-content" class="page-wrap py-5">
|
|
<div class="container">
|
|
<section class="hero-panel mb-4">
|
|
<div class="row g-4 align-items-center">
|
|
<div class="col-lg-7">
|
|
<span class="eyebrow">Auto installer</span>
|
|
<h1 class="hero-title mt-3 mb-3">Pindah hosting? Tinggal isi data database, lalu website jalan lagi.</h1>
|
|
<p class="hero-copy mb-0">Kalau script ini dipindah ke server baru, halaman ini akan muncul otomatis saat koneksi database belum siap. Anda hanya perlu memasukkan host, port, nama database, username, dan password MySQL.</p>
|
|
</div>
|
|
<div class="col-lg-5">
|
|
<div class="panel-card h-100">
|
|
<div class="card-label">Yang perlu disiapkan</div>
|
|
<ul class="mb-0 ps-3 small">
|
|
<li>Host database, seringnya <code>localhost</code>.</li>
|
|
<li>Port MySQL, default <code>3306</code>.</li>
|
|
<li>Nama database dari hosting/cPanel.</li>
|
|
<li>Username dan password database.</li>
|
|
</ul>
|
|
<div class="alert alert-info mt-3 mb-0 small">Jika database yang Anda tulis belum ada, installer akan mencoba membuatnya otomatis jika akun database punya izin.</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-4">
|
|
<section class="panel-card h-100">
|
|
<div class="card-label">Status saat ini</div>
|
|
<?php if (!empty($status['ready'])): ?>
|
|
<div class="alert alert-success mb-3">Aplikasi sudah terhubung ke database.</div>
|
|
<p class="section-copy mb-2">Sumber konfigurasi aktif: <strong><?= e(installer_source_label((string)$status['source'])) ?></strong>.</p>
|
|
<p class="section-copy mb-0">Kalau Anda hanya ingin memakai website, tidak perlu mengisi ulang form di kanan.</p>
|
|
<?php else: ?>
|
|
<div class="alert alert-warning mb-3">
|
|
<strong><?= e((string)$status['message']) ?></strong>
|
|
<?php if (!empty($status['details'])): ?>
|
|
<div class="small mt-2"><?= e((string)$status['details']) ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<p class="section-copy mb-0">Begitu data database benar, aplikasi akan membuat tabel blog otomatis saat Anda masuk ke halaman utama.</p>
|
|
<?php endif; ?>
|
|
</section>
|
|
</div>
|
|
|
|
<div class="col-lg-8">
|
|
<section class="panel-card">
|
|
<div class="d-flex flex-column flex-md-row justify-content-between align-items-start gap-3 mb-4">
|
|
<div>
|
|
<span class="section-kicker">Form koneksi</span>
|
|
<h2 class="section-title mb-1">Isi data MySQL / MariaDB</h2>
|
|
<p class="section-copy mb-0">Data ini biasanya ada di menu Database pada panel hosting atau PHPMyAdmin.</p>
|
|
</div>
|
|
<a class="btn btn-outline-dark btn-sm" href="https://www.phpmyadmin.net/" target="_blank" rel="noopener noreferrer">Apa itu PHPMyAdmin?</a>
|
|
</div>
|
|
|
|
<?php if ($flash !== null): ?>
|
|
<div class="alert alert-<?= e((string)$flash['type']) ?>">
|
|
<strong><?= e((string)$flash['title']) ?></strong>
|
|
<div class="small mt-1"><?= e((string)$flash['message']) ?></div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!empty($status['ready'])): ?>
|
|
<div class="d-flex flex-wrap gap-2">
|
|
<a class="btn btn-dark" href="<?= e($nextPath) ?>">Masuk ke website</a>
|
|
<a class="btn btn-outline-dark" href="index.php">Ke beranda</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<form method="post" novalidate>
|
|
<input type="hidden" name="next" value="<?= e($nextPath) ?>">
|
|
<div class="row g-3">
|
|
<div class="col-md-8">
|
|
<label class="form-label" for="db_host">Host database</label>
|
|
<input class="form-control <?= isset($errors['db_host']) ? 'is-invalid' : '' ?>" type="text" id="db_host" name="db_host" value="<?= e($formData['db_host']) ?>" placeholder="localhost" autocomplete="off">
|
|
<?php if (isset($errors['db_host'])): ?><div class="invalid-feedback"><?= e($errors['db_host']) ?></div><?php endif; ?>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label class="form-label" for="db_port">Port</label>
|
|
<input class="form-control <?= isset($errors['db_port']) ? 'is-invalid' : '' ?>" type="text" id="db_port" name="db_port" value="<?= e($formData['db_port']) ?>" placeholder="3306" inputmode="numeric" autocomplete="off">
|
|
<?php if (isset($errors['db_port'])): ?><div class="invalid-feedback"><?= e($errors['db_port']) ?></div><?php endif; ?>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label" for="db_name">Nama database</label>
|
|
<input class="form-control <?= isset($errors['db_name']) ? 'is-invalid' : '' ?>" type="text" id="db_name" name="db_name" value="<?= e($formData['db_name']) ?>" placeholder="nama_database" autocomplete="off">
|
|
<?php if (isset($errors['db_name'])): ?><div class="invalid-feedback"><?= e($errors['db_name']) ?></div><?php endif; ?>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label" for="db_user">Username database</label>
|
|
<input class="form-control <?= isset($errors['db_user']) ? 'is-invalid' : '' ?>" type="text" id="db_user" name="db_user" value="<?= e($formData['db_user']) ?>" placeholder="username_db" autocomplete="off">
|
|
<?php if (isset($errors['db_user'])): ?><div class="invalid-feedback"><?= e($errors['db_user']) ?></div><?php endif; ?>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label" for="db_pass">Password database</label>
|
|
<input class="form-control" type="password" id="db_pass" name="db_pass" value="<?= e($formData['db_pass']) ?>" placeholder="Kosongkan jika memang tidak ada password" autocomplete="new-password">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="alert alert-secondary mt-4 mb-0 small">Setelah berhasil disimpan, website akan memakai file konfigurasi lokal di folder <code>db/</code>. Jadi kalau script dipindah ke server lain, cukup buka web dan isi form ini lagi bila koneksi lama tidak cocok.</div>
|
|
|
|
<div class="d-flex flex-wrap gap-2 mt-4">
|
|
<button class="btn btn-dark btn-cta" type="submit">Simpan & masuk ke website</button>
|
|
<a class="btn btn-outline-dark btn-cta" href="/">Kembali</a>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
</section>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<?php render_page_end(); ?>
|