Upload files to "/"
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
# Fix Tugas_SIG Tiara untuk Coolify
|
||||
|
||||
Upload/replace file-file ini ke repo root `Tugas_SIG`.
|
||||
|
||||
File penting:
|
||||
- `koneksi.php` root: koneksi database pakai Environment Variables + fallback database.
|
||||
- `install_database.php`: installer sementara database.
|
||||
- `Dockerfile`: PHP Apache port 80.
|
||||
- `Tugas2/koneksi.php`, `Tugas3/koneksi.php`, `Tugas4/koneksi.php`, `Tugas1/koneksi.php`: proxy supaya include('koneksi.php') tetap jalan dari subfolder.
|
||||
|
||||
Setelah import database berhasil, hapus `install_database.php`, commit, push, lalu redeploy.
|
||||
+49
-16
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
// install_database.php - installer sementara database Tugas_SIG Tiara
|
||||
// install_database.php - installer sementara database Tugas SIG Tiara
|
||||
// HAPUS file ini setelah database berhasil diimport.
|
||||
|
||||
set_time_limit(300);
|
||||
@@ -7,34 +7,67 @@ set_time_limit(300);
|
||||
$host = getenv('DB_HOST') ?: 'localhost';
|
||||
$user = getenv('DB_USER') ?: 'root';
|
||||
$pass = getenv('DB_PASS') ?: (getenv('DB_PASSWORD') ?: '');
|
||||
$db = getenv('DB_NAME') ?: 'db_poverty_mapping';
|
||||
$port = (int)(getenv('DB_PORT') ?: 3306);
|
||||
$requestedDb = getenv('DB_NAME') ?: 'db_poverty_mapping';
|
||||
|
||||
echo "<h2>Install Database Tugas SIG Tiara</h2>";
|
||||
echo "<p>Target koneksi: <b>" . htmlspecialchars($host) . ":" . htmlspecialchars((string)$port) . "</b>, database: <b>" . htmlspecialchars($db) . "</b>, user: <b>" . htmlspecialchars($user) . "</b></p>";
|
||||
$dbCandidates = array_values(array_unique(array_filter([
|
||||
$requestedDb,
|
||||
getenv('MYSQL_DATABASE') ?: null,
|
||||
'default',
|
||||
'db_poverty_mapping'
|
||||
])));
|
||||
|
||||
$conn = new mysqli($host, $user, $pass, $db, $port);
|
||||
mysqli_report(MYSQLI_REPORT_OFF);
|
||||
|
||||
$conn = new mysqli($host, $user, $pass, '', $port);
|
||||
|
||||
if ($conn->connect_error) {
|
||||
die("<p style='color:red'>Koneksi gagal: " . htmlspecialchars($conn->connect_error) . "</p>");
|
||||
die("<h2>Install Database Tugas SIG Tiara</h2><p style='color:red'>Koneksi server database gagal: " . htmlspecialchars($conn->connect_error) . "</p>");
|
||||
}
|
||||
|
||||
$sqlFile = __DIR__ . "/database.sql";
|
||||
$selectedDb = null;
|
||||
$selectErrors = [];
|
||||
|
||||
foreach ($dbCandidates as $candidate) {
|
||||
if (!$candidate) continue;
|
||||
if (@$conn->select_db($candidate)) {
|
||||
$selectedDb = $candidate;
|
||||
break;
|
||||
}
|
||||
$selectErrors[] = $candidate . ' => ' . $conn->error;
|
||||
}
|
||||
|
||||
echo "<h2>Install Database Tugas SIG Tiara</h2>";
|
||||
echo "<p>Target server: <b>" . htmlspecialchars($host) . ":" . htmlspecialchars((string)$port) . "</b></p>";
|
||||
echo "<p>User: <b>" . htmlspecialchars($user) . "</b></p>";
|
||||
echo "<p>DB_NAME dari env: <b>" . htmlspecialchars($requestedDb) . "</b></p>";
|
||||
|
||||
if (!$selectedDb) {
|
||||
echo "<p style='color:red'><b>Gagal memilih database.</b></p>";
|
||||
echo "<p>Database yang dicoba:</p><pre>" . htmlspecialchars(implode("\n", $selectErrors)) . "</pre>";
|
||||
echo "<p>Solusi: di Coolify bagian <b>Production Environment Variables</b>, ubah <code>DB_NAME</code> menjadi nama database yang ada di ujung <b>MySQL URL internal</b>. Kalau URL internal berakhir dengan <code>/default</code>, isi <code>DB_NAME=default</code>.</p>";
|
||||
exit;
|
||||
}
|
||||
|
||||
echo "<p>Database yang dipakai: <b>" . htmlspecialchars($selectedDb) . "</b></p>";
|
||||
|
||||
$sqlFile = __DIR__ . '/database.sql';
|
||||
|
||||
if (!file_exists($sqlFile)) {
|
||||
die("<p style='color:red'>File SQL tidak ditemukan: " . htmlspecialchars($sqlFile) . "</p>");
|
||||
}
|
||||
|
||||
$sql = file_get_contents($sqlFile);
|
||||
|
||||
// Coolify normal user biasanya tidak butuh/ tidak punya izin CREATE DATABASE.
|
||||
// Database sudah dibuat dari field Initial Database, jadi baris CREATE DATABASE dan USE diabaikan.
|
||||
$sql = preg_replace('/CREATE\\s+DATABASE\\s+IF\\s+NOT\\s+EXISTS\\s+`?[^`\\s;]+`?.*?;/is', '', $sql);
|
||||
$sql = preg_replace('/USE\\s+`?[^`\\s;]+`?\\s*;/is', '', $sql);
|
||||
// Di Coolify, database sudah dibuat oleh resource database.
|
||||
// Jadi dump tidak boleh memaksa CREATE DATABASE / USE database lain.
|
||||
$sql = preg_replace('/CREATE\s+DATABASE\s+IF\s+NOT\s+EXISTS\s+`?[^`\s;]+`?.*?;/is', '', $sql);
|
||||
$sql = preg_replace('/CREATE\s+DATABASE\s+`?[^`\s;]+`?.*?;/is', '', $sql);
|
||||
$sql = preg_replace('/USE\s+`?[^`\s;]+`?\s*;/is', '', $sql);
|
||||
|
||||
// Jika dump hasil copy menjadi komentar seperti "-- 1.\nTABEL ... CREATE TABLE",
|
||||
// buang label teks agar SQL tidak error.
|
||||
$sql = preg_replace('/--\\s*\\d+\\.\\s*TABEL[^\\n]*\\n/is', '', $sql);
|
||||
$sql = preg_replace('/\\bTABEL\\s+(PENGGUNA|RUMAH|BENCANA|DATA)[^;]*?(CREATE\\s+TABLE)/is', '$2', $sql);
|
||||
// Supaya import ulang tidak langsung gagal karena tabel sudah ada.
|
||||
$sql = preg_replace('/CREATE\s+TABLE\s+`/i', 'CREATE TABLE IF NOT EXISTS `', $sql);
|
||||
$sql = preg_replace('/CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+IF\s+NOT\s+EXISTS\s+`/i', 'CREATE TABLE IF NOT EXISTS `', $sql);
|
||||
|
||||
if ($conn->multi_query($sql)) {
|
||||
do {
|
||||
@@ -46,7 +79,7 @@ if ($conn->multi_query($sql)) {
|
||||
if ($conn->errno) {
|
||||
echo "<p style='color:red'>Ada error saat import: " . htmlspecialchars($conn->error) . "</p>";
|
||||
} else {
|
||||
echo "<p style='color:green;font-weight:bold'>Database berhasil diimport.</p>";
|
||||
echo "<p style='color:green;font-weight:bold'>Database berhasil diimport ke database <code>" . htmlspecialchars($selectedDb) . "</code>.</p>";
|
||||
echo "<p>Setelah ini, hapus file <code>install_database.php</code> dari repo lalu redeploy.</p>";
|
||||
}
|
||||
} else {
|
||||
|
||||
+68
-22
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
// koneksi.php - versi deploy Coolify untuk Tugas_SIG Tiara
|
||||
// koneksi.php - versi aman untuk XAMPP dan Coolify
|
||||
// Bisa membaca DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS dari Environment Variables.
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
@@ -7,50 +9,94 @@ if (session_status() === PHP_SESSION_NONE) {
|
||||
$host = getenv('DB_HOST') ?: 'localhost';
|
||||
$user = getenv('DB_USER') ?: 'root';
|
||||
$pass = getenv('DB_PASS') ?: (getenv('DB_PASSWORD') ?: '');
|
||||
$db = getenv('DB_NAME') ?: 'db_poverty_mapping';
|
||||
$port = (int)(getenv('DB_PORT') ?: 3306);
|
||||
|
||||
$conn = new mysqli($host, $user, $pass, $db, $port);
|
||||
// DB_NAME dari Coolify harusnya sesuai Initial Database.
|
||||
// Kalau salah, kode ini akan mencoba fallback ke database umum seperti default.
|
||||
$requestedDb = getenv('DB_NAME') ?: 'db_poverty_mapping';
|
||||
$dbCandidates = array_values(array_unique(array_filter([
|
||||
$requestedDb,
|
||||
getenv('MYSQL_DATABASE') ?: null,
|
||||
'default',
|
||||
'db_poverty_mapping'
|
||||
])));
|
||||
|
||||
// Koneksi tanpa memilih database dulu, supaya tidak langsung fatal kalau DB_NAME salah.
|
||||
mysqli_report(MYSQLI_REPORT_OFF);
|
||||
$conn = new mysqli($host, $user, $pass, '', $port);
|
||||
|
||||
if ($conn->connect_error) {
|
||||
http_response_code(500);
|
||||
die(json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Koneksi database gagal: ' . $conn->connect_error
|
||||
'message' => 'Koneksi server database gagal: ' . $conn->connect_error,
|
||||
'target' => $host . ':' . $port,
|
||||
'user' => $user
|
||||
]));
|
||||
}
|
||||
|
||||
$selectedDb = null;
|
||||
foreach ($dbCandidates as $candidate) {
|
||||
if ($candidate && @$conn->select_db($candidate)) {
|
||||
$selectedDb = $candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$selectedDb) {
|
||||
http_response_code(500);
|
||||
die(json_encode([
|
||||
'status' => 'error',
|
||||
'message' => 'Tidak bisa memilih database. Cek DB_NAME di Production Environment Variables Coolify. DB_NAME harus sama dengan Initial Database / nama database di MySQL URL internal.',
|
||||
'dicoba' => $dbCandidates,
|
||||
'target' => $host . ':' . $port,
|
||||
'user' => $user
|
||||
]));
|
||||
}
|
||||
|
||||
$conn->set_charset('utf8mb4');
|
||||
|
||||
function getRedirectPath($targetFile) {
|
||||
$currentDir = basename(dirname($_SERVER['SCRIPT_FILENAME']));
|
||||
if (stripos($currentDir, 'Tugas') !== false || stripos($currentDir, 'tugas') !== false || $currentDir === 'includes') {
|
||||
return '../' . $targetFile;
|
||||
}
|
||||
return $targetFile;
|
||||
// Alias agar kompatibel kalau file lain memakai nama variable berbeda.
|
||||
$koneksi = $conn;
|
||||
$mysqli = $conn;
|
||||
|
||||
// Constants agar kompatibel kalau kode lama memakai DB_HOST, DB_USER, dst.
|
||||
if (!defined('DB_HOST')) define('DB_HOST', $host);
|
||||
if (!defined('DB_USER')) define('DB_USER', $user);
|
||||
if (!defined('DB_PASS')) define('DB_PASS', $pass);
|
||||
if (!defined('DB_NAME')) define('DB_NAME', $selectedDb);
|
||||
if (!defined('DB_PORT')) define('DB_PORT', $port);
|
||||
|
||||
function bersih($str, $conn) {
|
||||
return $conn->real_escape_string(htmlspecialchars(strip_tags(trim((string)$str))));
|
||||
}
|
||||
|
||||
function cekLogin() {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header('Location: ' . getRedirectPath('login.php'));
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function cekRole($role_wajib) {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
if (!isset($_SESSION['role']) || $_SESSION['role'] !== $role_wajib) {
|
||||
header('Location: ' . getRedirectPath('dashboard.php?error=unauthorized'));
|
||||
function cekRole(...$roles) {
|
||||
if (!in_array($_SESSION['role'] ?? '', $roles)) {
|
||||
header('Location: index.php?error=akses_ditolak');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function bersih($str, $conn) {
|
||||
return $conn->real_escape_string(htmlspecialchars(strip_tags(trim($str))));
|
||||
function rupiah($angka) {
|
||||
return 'Rp ' . number_format((float)$angka, 0, ',', '.');
|
||||
}
|
||||
|
||||
function hitungUsia($tgl) {
|
||||
if (!$tgl || $tgl === '0000-00-00') return '-';
|
||||
try {
|
||||
$lahir = new DateTime($tgl);
|
||||
$sekarang = new DateTime();
|
||||
return $lahir->diff($sekarang)->y . ' th';
|
||||
} catch (Exception $e) {
|
||||
return '-';
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user