Harden Project 01 public endpoints

This commit is contained in:
Andrie
2026-06-11 23:15:42 +07:00
parent a90748d9c1
commit 6f4488c1b8
39 changed files with 1183 additions and 612 deletions
+28 -35
View File
@@ -1,54 +1,47 @@
<?php
require_once '../../auth/helper.php';
require_once '../../includes/validation.php';
require_admin_post();
require_once '../../koneksi.php';
require_admin_json();
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['status' => 'error', 'message' => 'Method tidak valid.']); exit;
}
$nama = clean_string($_POST['nama'] ?? '', 150);
$deskripsi = clean_string($_POST['deskripsi'] ?? '', 500);
$attrKey = clean_string($_POST['attribute_key'] ?? '', 100);
$palette = validate_enum($_POST['palette'] ?? '', ['blue', 'orange', 'green', 'purple']) ?? 'blue';
$nama = trim($_POST['nama'] ?? '');
$deskripsi = trim($_POST['deskripsi'] ?? '');
$attr_key = trim($_POST['attribute_key'] ?? '');
$palette = in_array($_POST['palette'] ?? '', ['blue', 'orange', 'green', 'purple'])
? $_POST['palette'] : 'blue';
if (!$nama || !$attr_key) {
echo json_encode(['status' => 'error', 'message' => 'Nama layer dan atribut wajib diisi.']); exit;
if ($nama === '' || $attrKey === '') {
json_error('Nama layer dan atribut wajib diisi.', 400);
}
if (empty($_FILES['geojson']['tmp_name']) || $_FILES['geojson']['error'] !== UPLOAD_ERR_OK) {
echo json_encode(['status' => 'error', 'message' => 'File GeoJSON tidak ditemukan atau gagal diupload.']); exit;
json_error('File GeoJSON tidak ditemukan atau gagal diupload.', 400);
}
if ($_FILES['geojson']['size'] > 10 * 1024 * 1024) {
echo json_encode(['status' => 'error', 'message' => 'File terlalu besar (maksimum 10 MB).']); exit;
if ($_FILES['geojson']['size'] > MAX_GEOJSON_BYTES) {
json_error('File terlalu besar.', 400);
}
$raw = file_get_contents($_FILES['geojson']['tmp_name']);
if ($raw === false) {
echo json_encode(['status' => 'error', 'message' => 'Gagal membaca file.']); exit;
json_error('Gagal membaca file.', 400);
}
$gj = json_decode($raw, true);
if (!$gj || !isset($gj['type']) || $gj['type'] !== 'FeatureCollection') {
echo json_encode(['status' => 'error', 'message' => 'File bukan GeoJSON FeatureCollection yang valid.']); exit;
}
if (empty($gj['features'])) {
echo json_encode(['status' => 'error', 'message' => 'GeoJSON tidak memiliki fitur/data.']); exit;
}
$first_props = $gj['features'][0]['properties'] ?? [];
if (!array_key_exists($attr_key, $first_props)) {
$available = implode(', ', array_keys($first_props));
echo json_encode(['status' => 'error', 'message' => "Atribut '{$attr_key}' tidak ditemukan. Tersedia: {$available}"]); exit;
$geojson = decode_geojson($raw);
if ($geojson === null || !validate_feature_collection($geojson, $attrKey)) {
json_error('GeoJSON tidak valid atau atribut choropleth tidak lengkap.', 400);
}
$stmt = $conn->prepare("INSERT INTO choropleth_layers (nama, deskripsi, geojson, attribute_key, palette) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param('sssss', $nama, $deskripsi, $raw, $attr_key, $palette);
$stmt->execute();
$id = $conn->insert_id;
$stmt->close();
$conn->close();
if (!$stmt) {
error_log('Project 01 choropleth upload prepare failed: ' . $conn->error);
json_error('Gagal menyimpan layer.', 500);
}
echo json_encode(['status' => 'success', 'id' => $id, 'nama' => $nama]);
$stmt->bind_param('sssss', $nama, $deskripsi, $raw, $attrKey, $palette);
if (!$stmt->execute()) {
error_log('Project 01 choropleth upload failed: ' . $stmt->error);
json_error('Gagal menyimpan layer.', 500);
}
json_success(['id' => $conn->insert_id, 'nama' => $nama]);