57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
// koneksi.php - versi deploy Coolify untuk Tugas_SIG Tiara
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
$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);
|
|
|
|
if ($conn->connect_error) {
|
|
http_response_code(500);
|
|
die(json_encode([
|
|
'status' => 'error',
|
|
'message' => 'Koneksi database gagal: ' . $conn->connect_error
|
|
]));
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
function cekLogin() {
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: ' . getRedirectPath('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'));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
function bersih($str, $conn) {
|
|
return $conn->real_escape_string(htmlspecialchars(strip_tags(trim($str))));
|
|
}
|
|
?>
|