87 lines
2.9 KiB
PHP
87 lines
2.9 KiB
PHP
<?php
|
|
// ============================================================
|
|
// api/config.php — Konfigurasi Database & Helper Functions
|
|
// GeoSosial — Sistem Informasi Sosial Kota Pontianak
|
|
// ============================================================
|
|
|
|
// Pastikan PHP errors tidak merusak output JSON
|
|
error_reporting(0);
|
|
ini_set('display_errors', '0');
|
|
ini_set('display_startup_errors', '0');
|
|
// Mulai output buffering untuk tangkap output tak terduga
|
|
ob_start();
|
|
|
|
define('DB_HOST', getenv('DB_HOST') ?: 'db');
|
|
define('DB_PORT', getenv('DB_PORT') ?: '3306');
|
|
define('DB_NAME', getenv('DB_NAME_GEOSOSIAL') ?: 'geososial');
|
|
define('DB_USER', getenv('DB_USER') ?: 'root');
|
|
define('DB_PASS', getenv('DB_PASS') ?: 'root');
|
|
define('DB_CHARSET', 'utf8mb4');
|
|
|
|
// CORS Headers
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); }
|
|
|
|
// Session
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_name('geososial_session');
|
|
session_start();
|
|
}
|
|
|
|
// PDO Connection
|
|
function getDB(): PDO {
|
|
static $pdo = null;
|
|
if ($pdo === null) {
|
|
$dsn = "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
|
|
try {
|
|
$pdo = new PDO($dsn, DB_USER, DB_PASS, [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
]);
|
|
} catch (PDOException $e) {
|
|
jsonError('Koneksi database gagal: ' . $e->getMessage(), 500);
|
|
}
|
|
}
|
|
return $pdo;
|
|
}
|
|
|
|
// Response helpers — bersihkan output buffer dulu agar tidak ada PHP warning yang merusak JSON
|
|
function jsonOk($data = null, string $msg = 'OK'): void {
|
|
if (ob_get_level()) ob_clean();
|
|
echo json_encode(['success' => true, 'message' => $msg, 'data' => $data], JSON_UNESCAPED_UNICODE);
|
|
exit();
|
|
}
|
|
function jsonError(string $msg, int $code = 400): void {
|
|
if (ob_get_level()) ob_clean();
|
|
http_response_code($code);
|
|
echo json_encode(['success' => false, 'message' => $msg], JSON_UNESCAPED_UNICODE);
|
|
exit();
|
|
}
|
|
|
|
// Auth check
|
|
function requireAuth(): array {
|
|
if (empty($_SESSION['user'])) {
|
|
jsonError('Tidak terautentikasi. Silakan login.', 401);
|
|
}
|
|
return $_SESSION['user'];
|
|
}
|
|
function requireEdit(): array {
|
|
$user = requireAuth();
|
|
if (!$user['can_edit']) jsonError('Anda tidak memiliki hak akses edit.', 403);
|
|
return $user;
|
|
}
|
|
|
|
// Input helper
|
|
function body(): array {
|
|
$raw = file_get_contents('php://input');
|
|
return json_decode($raw, true) ?? [];
|
|
}
|
|
function getParam(string $key, $default = null) {
|
|
return $_GET[$key] ?? $default;
|
|
}
|