Initial commit of unified WebGIS projects
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$result = $conn->query("SELECT * FROM jalan");
|
||||
$data = [];
|
||||
|
||||
while($row = $result->fetch_assoc()){
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
echo json_encode($data);
|
||||
?>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$result = $conn->query("SELECT * FROM lokasi");
|
||||
|
||||
$data = [];
|
||||
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
echo json_encode($data);
|
||||
?>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$result = $conn->query("SELECT * FROM tanah");
|
||||
$data = [];
|
||||
|
||||
while($row = $result->fetch_assoc()){
|
||||
// log a short preview of geom for debugging
|
||||
@file_put_contents('/tmp/webgis_debug.log', date('c') . " get_tanah id=".$row['id']." geom_preview=".substr($row['geom'],0,400)."\n", FILE_APPEND);
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
echo json_encode($data);
|
||||
?>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require_api_key();
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = null;
|
||||
if($_SERVER['REQUEST_METHOD'] === 'DELETE'){
|
||||
$id = isset($input['id']) ? $input['id'] : null;
|
||||
} else {
|
||||
$id = isset($_POST['id']) ? $_POST['id'] : (isset($input['id']) ? $input['id'] : null);
|
||||
}
|
||||
|
||||
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||
|
||||
$stmt = $conn->prepare('DELETE FROM jalan WHERE id = ?');
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
$stmt->bind_param('s', $id);
|
||||
$res = $stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
if($res){ echo json_encode(['success'=>true,'msg'=>'hapus']); }
|
||||
else { http_response_code(500); echo json_encode(['success'=>false,'error'=>'delete_failed']); }
|
||||
?>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
// require API key for delete
|
||||
require_api_key();
|
||||
|
||||
// accept DELETE or POST with JSON body or form
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = null;
|
||||
if($_SERVER['REQUEST_METHOD'] === 'DELETE'){
|
||||
$id = isset($input['id']) ? $input['id'] : null;
|
||||
} else {
|
||||
$id = isset($_POST['id']) ? $_POST['id'] : (isset($input['id']) ? $input['id'] : null);
|
||||
}
|
||||
|
||||
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||
|
||||
$stmt = $conn->prepare('DELETE FROM lokasi WHERE id = ?');
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
$stmt->bind_param('s', $id);
|
||||
$res = $stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
if($res){ echo json_encode(['success'=>true,'message'=>'hapus']); }
|
||||
else { http_response_code(500); echo json_encode(['success'=>false,'error'=>'delete_failed']); }
|
||||
?>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require_api_key();
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$id = null;
|
||||
if($_SERVER['REQUEST_METHOD'] === 'DELETE'){
|
||||
$id = isset($input['id']) ? $input['id'] : null;
|
||||
} else {
|
||||
$id = isset($_POST['id']) ? $_POST['id'] : (isset($input['id']) ? $input['id'] : null);
|
||||
}
|
||||
|
||||
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||
|
||||
$stmt = $conn->prepare('DELETE FROM tanah WHERE id = ?');
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
$stmt->bind_param('s', $id);
|
||||
$res = $stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
if($res){ echo json_encode(['success'=>true,'msg'=>'hapus']); }
|
||||
else { http_response_code(500); echo json_encode(['success'=>false,'error'=>'delete_failed']); }
|
||||
?>
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
require_api_key();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
if(!$data){
|
||||
http_response_code(400);
|
||||
echo json_encode(["success"=>false,"error"=>"invalid_json"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$nama = isset($data['nama']) ? $data['nama'] : '';
|
||||
$status = isset($data['status']) ? $data['status'] : '';
|
||||
$panjang = isset($data['panjang']) ? floatval($data['panjang']) : 0;
|
||||
$geom = isset($data['geom']) ? json_encode($data['geom']) : null;
|
||||
|
||||
$stmt = $conn->prepare("INSERT INTO jalan (nama,status,panjang,geom) VALUES (?, ?, ?, ?)");
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
$stmt->bind_param('sdds', $nama, $status, $panjang, $geom);
|
||||
$res = $stmt->execute();
|
||||
|
||||
if($res){
|
||||
$insert_id = $stmt->insert_id;
|
||||
echo json_encode(["success"=>true,"id"=>$insert_id]);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(["success"=>false,"error"=>$stmt->error]);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// require API key for create operations
|
||||
require_api_key();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
if(!$data){
|
||||
http_response_code(400);
|
||||
echo json_encode(["success"=>false,"error"=>"invalid_json"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// generate safer unique id
|
||||
$id = substr(uniqid('', true), -8);
|
||||
$nama = isset($data['nama']) ? $data['nama'] : '';
|
||||
$no_telp = isset($data['no_telp']) ? $data['no_telp'] : '';
|
||||
$buka = isset($data['buka_24_jam']) ? intval($data['buka_24_jam']) : 0;
|
||||
$alamat = isset($data['alamat']) ? $data['alamat'] : '';
|
||||
$jenis = isset($data['jenis']) ? $data['jenis'] : '';
|
||||
$lat = isset($data['latitude']) ? $data['latitude'] : null;
|
||||
$lng = isset($data['longitude']) ? $data['longitude'] : null;
|
||||
|
||||
// basic validation
|
||||
if($lat !== null){
|
||||
$lat = floatval($lat);
|
||||
if($lat < -90 || $lat > 90){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'invalid_latitude']); exit; }
|
||||
}
|
||||
if($lng !== null){
|
||||
$lng = floatval($lng);
|
||||
if($lng < -180 || $lng > 180){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'invalid_longitude']); exit; }
|
||||
}
|
||||
|
||||
// prepared statement
|
||||
$stmt = $conn->prepare("INSERT INTO lokasi (id, nama, jenis, no_telp, buka_24_jam, alamat, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
$stmt->bind_param('ssssisdd', $id, $nama, $jenis, $no_telp, $buka, $alamat, $lat, $lng);
|
||||
$res = $stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
if($res){
|
||||
echo json_encode(["success"=>true, "id"=>$id]);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(["success"=>false, "error"=> $conn->error]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
require_api_key();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
if(!$data){
|
||||
http_response_code(400);
|
||||
echo json_encode(["success"=>false,"error"=>"invalid_json"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$nama = isset($data['nama']) ? $data['nama'] : '';
|
||||
$status = isset($data['status']) ? $data['status'] : '';
|
||||
$luas = isset($data['luas']) ? floatval($data['luas']) : 0;
|
||||
$geom = isset($data['geom']) ? json_encode($data['geom']) : null;
|
||||
|
||||
// prepared statement
|
||||
$stmt = $conn->prepare("INSERT INTO tanah (nama,status,luas,geom) VALUES (?, ?, ?, ?)");
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
$stmt->bind_param('sdis', $nama, $status, $luas, $geom);
|
||||
$res = $stmt->execute();
|
||||
|
||||
if($res){
|
||||
$insert_id = $stmt->insert_id;
|
||||
echo json_encode(["success"=>true,"id"=>$insert_id]);
|
||||
@file_put_contents('/tmp/webgis_debug.log', date('c') . " tambah_tanah OK id=".$insert_id." payload=".json_encode($data)."\n", FILE_APPEND);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(["success"=>false,"error"=>$stmt->error]);
|
||||
@file_put_contents('/tmp/webgis_debug.log', date('c') . " tambah_tanah ERR=".$stmt->error." payload=".json_encode($data)."\n", FILE_APPEND);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require_api_key();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
if(!$data){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'invalid_json']); exit; }
|
||||
|
||||
$id = isset($data['id']) ? $data['id'] : null;
|
||||
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||
|
||||
$sets = [];
|
||||
$types = '';
|
||||
$values = [];
|
||||
if(isset($data['nama'])){ $sets[]='nama = ?'; $types.='s'; $values[]=$data['nama']; }
|
||||
if(isset($data['status'])){ $sets[]='status = ?'; $types.='s'; $values[]=$data['status']; }
|
||||
if(isset($data['panjang'])){ $sets[]='panjang = ?'; $types.='d'; $values[]=$data['panjang']; }
|
||||
if(isset($data['geom'])){ $sets[]='geom = ?'; $types.='s'; $values[]=json_encode($data['geom']); }
|
||||
|
||||
if(count($sets) > 0){
|
||||
$sql = 'UPDATE jalan SET ' . implode(', ', $sets) . ' WHERE id = ?';
|
||||
$types .= 's';
|
||||
$values[] = $id;
|
||||
$stmt = $conn->prepare($sql);
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
|
||||
$bind_names[] = $types;
|
||||
for($i=0;$i<count($values);$i++){ $bind_name='p'.$i; $$bind_name = $values[$i]; $bind_names[] = &$$bind_name; }
|
||||
call_user_func_array([$stmt, 'bind_param'], $bind_names);
|
||||
$res = $stmt->execute();
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
echo json_encode(["msg"=>"ok"]);
|
||||
?>
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require_api_key();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
$id = isset($data['id']) ? $data['id'] : '';
|
||||
$nama = isset($data['nama']) ? $data['nama'] : null;
|
||||
$no_telp = isset($data['no_telp']) ? $data['no_telp'] : null;
|
||||
$buka = isset($data['buka_24_jam']) ? intval($data['buka_24_jam']) : null;
|
||||
$alamat = isset($data['alamat']) ? $data['alamat'] : null;
|
||||
$jenis = isset($data['jenis']) ? $data['jenis'] : null;
|
||||
|
||||
$sets = [];
|
||||
$types = '';
|
||||
$values = [];
|
||||
if($nama !== null){ $sets[] = 'nama = ?'; $types .= 's'; $values[] = $nama; }
|
||||
if($no_telp !== null){ $sets[] = 'no_telp = ?'; $types .= 's'; $values[] = $no_telp; }
|
||||
if($buka !== null){ $sets[] = 'buka_24_jam = ?'; $types .= 'i'; $values[] = $buka; }
|
||||
if($alamat !== null){ $sets[] = 'alamat = ?'; $types .= 's'; $values[] = $alamat; }
|
||||
if($jenis !== null){ $sets[] = 'jenis = ?'; $types .= 's'; $values[] = $jenis; }
|
||||
|
||||
if(empty($sets) || empty($id)){
|
||||
http_response_code(400);
|
||||
echo json_encode(['success'=>false,'error'=>'missing_id_or_fields']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = 'UPDATE lokasi SET ' . implode(', ', $sets) . ' WHERE id = ?';
|
||||
$types .= 's';
|
||||
$values[] = $id;
|
||||
|
||||
$stmt = $conn->prepare($sql);
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
|
||||
// bind params dynamically
|
||||
$bind_names[] = $types;
|
||||
for ($i=0; $i<count($values); $i++){
|
||||
$bind_name = 'bind' . $i;
|
||||
$$bind_name = $values[$i];
|
||||
$bind_names[] = &$$bind_name;
|
||||
}
|
||||
call_user_func_array([$stmt, 'bind_param'], $bind_names);
|
||||
$res = $stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
if($res){
|
||||
echo json_encode(["success"=>true]);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(["success"=>false, "error"=>$conn->error]);
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
include __DIR__ . '/../config/koneksi.php';
|
||||
include __DIR__ . '/../config/auth.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
require_api_key();
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
$id = isset($data['id']) ? $data['id'] : null;
|
||||
if(!$id){ http_response_code(400); echo json_encode(['success'=>false,'error'=>'missing_id']); exit; }
|
||||
|
||||
$sets = [];
|
||||
$types = '';
|
||||
$values = [];
|
||||
if(isset($data['nama'])){ $sets[]='nama = ?'; $types.='s'; $values[]=$data['nama']; }
|
||||
if(isset($data['status'])){ $sets[]='status = ?'; $types.='s'; $values[]=$data['status']; }
|
||||
if(isset($data['luas'])){ $sets[]='luas = ?'; $types.='d'; $values[]=$data['luas']; }
|
||||
if(isset($data['geom'])){ $sets[]='geom = ?'; $types.='s'; $values[]=json_encode($data['geom']); }
|
||||
|
||||
if(count($sets) > 0){
|
||||
$sql = 'UPDATE tanah SET ' . implode(', ', $sets) . ' WHERE id = ?';
|
||||
$types .= 's';
|
||||
$values[] = $id;
|
||||
$stmt = $conn->prepare($sql);
|
||||
if(!$stmt){ http_response_code(500); echo json_encode(['success'=>false,'error'=>$conn->error]); exit; }
|
||||
|
||||
$bind_names[] = $types;
|
||||
for($i=0;$i<count($values);$i++){ $bind_name='b'.$i; $$bind_name = $values[$i]; $bind_names[] = &$$bind_name; }
|
||||
call_user_func_array([$stmt, 'bind_param'], $bind_names);
|
||||
$res = $stmt->execute();
|
||||
if($res){
|
||||
@file_put_contents('/tmp/webgis_debug.log', date('c') . " update_tanah OK id=".$id." payload=".json_encode($data)."\n", FILE_APPEND);
|
||||
} else {
|
||||
@file_put_contents('/tmp/webgis_debug.log', date('c') . " update_tanah ERR=".$stmt->error." payload=".json_encode($data)."\n", FILE_APPEND);
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
echo json_encode(["msg"=>"ok"]);
|
||||
?>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
include_once __DIR__ . '/config.php';
|
||||
|
||||
function get_request_api_key(){
|
||||
// check header first
|
||||
$headers = null;
|
||||
if(function_exists('getallheaders')){
|
||||
$headers = getallheaders();
|
||||
}
|
||||
if($headers){
|
||||
foreach($headers as $k => $v){
|
||||
if(strtolower($k) === 'x-api-key') return $v;
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to header in $_SERVER
|
||||
if(isset($_SERVER['HTTP_X_API_KEY'])) return $_SERVER['HTTP_X_API_KEY'];
|
||||
|
||||
// fallback to JSON body or POST
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
if(isset($input['api_key'])) return $input['api_key'];
|
||||
if(isset($_POST['api_key'])) return $_POST['api_key'];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function require_api_key(){
|
||||
$key = get_request_api_key();
|
||||
if(!$key || $key !== API_KEY){
|
||||
http_response_code(401);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode(['success'=>false,'error'=>'unauthorized']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
// Load environment from project root .env if present, then read API_KEY
|
||||
function load_dotenv($path){
|
||||
if(!file_exists($path)) return;
|
||||
$lines = file($path, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
|
||||
foreach($lines as $line){
|
||||
if(trim($line)==='' || strpos(trim($line),'#')===0) continue;
|
||||
if(strpos($line,'=')===false) continue;
|
||||
list($k,$v) = explode('=', $line, 2);
|
||||
$k = trim($k); $v = trim($v);
|
||||
// strip optional quotes
|
||||
if((substr($v,0,1)==='"' && substr($v,-1)==='"') || (substr($v,0,1)==="'" && substr($v,-1)==="'")){
|
||||
$v = substr($v,1,-1);
|
||||
}
|
||||
putenv("$k=$v");
|
||||
$_ENV[$k] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
$envPath = realpath(__DIR__ . '/../../.env');
|
||||
if($envPath){ load_dotenv($envPath); }
|
||||
|
||||
$apiKey = getenv('API_KEY');
|
||||
if(!$apiKey) {
|
||||
// fallback (development) - but prefer .env or system env in production
|
||||
$apiKey = '8f3b7e6a9c4d2f1a6b8c9d0e4f7a1b2c';
|
||||
}
|
||||
define('API_KEY', $apiKey);
|
||||
?>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
include_once __DIR__ . '/config.php';
|
||||
|
||||
$dbHost = getenv('DB_HOST') ?: 'localhost';
|
||||
$dbPort = (int)(getenv('DB_PORT') ?: 3306);
|
||||
$dbUser = getenv('DB_USER') ?: 'root';
|
||||
$dbPass = getenv('DB_PASS') ?: 'ilham';
|
||||
$dbName = getenv('DB_NAME') ?: 'spbu_db';
|
||||
|
||||
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName, $dbPort);
|
||||
|
||||
if ($conn->connect_error) {
|
||||
die("Koneksi gagal: " . $conn->connect_error);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user