| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/bpkd/admin/api/ |
Upload File : |
<?php
/**
* API Sync PPID Rekap Konten
* Untuk diakses dari Google Apps Script
*
* Endpoint: POST /admin/api/ppid_sync.php
* Header: X-API-Key
* Body: JSON
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, X-API-Key');
// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// ============================================================
// LOAD DEPENDENCIES
// ============================================================
require_once dirname(__DIR__) . '/config/database.php';
require_once dirname(__DIR__, 2) . '/includes/config.php';
require_once dirname(__DIR__) . '/lib/PPIDService.php';
$config = include dirname(__DIR__) . '/config/ppid.php';
// ============================================================
// AUTHENTIKASI API KEY
// ============================================================
$headers = getallheaders();
$apiKey = $headers['X-API-Key']
?? $headers['x-api-key']
?? $_GET['api_key']
?? $_POST['api_key']
?? '';
// Ambil API key dari config (tambahkan 'gas_api_key' di config/ppid.php)
$validApiKey = $config['gas_api_key'] ?? 'bpkd_gas_2026_secret_key_abc123';
if ($apiKey !== $validApiKey) {
http_response_code(401);
echo json_encode([
'status' => 'error',
'code' => 401,
'message' => 'Unauthorized. API Key tidak valid.'
]);
exit();
}
// ============================================================
// METHOD CHECK
// ============================================================
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'status' => 'error',
'code' => 405,
'message' => 'Method tidak diizinkan. Gunakan POST.'
]);
exit();
}
// ============================================================
// PARSE INPUT
// ============================================================
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
// Fallback ke form-urlencoded jika bukan JSON
if (!$input) {
$input = $_POST;
}
if (empty($input)) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'code' => 400,
'message' => 'Body request kosong atau tidak valid'
]);
exit();
}
// ============================================================
// VALIDASI INPUT
// ============================================================
$judul = trim($input['judul'] ?? '');
$tipe = trim($input['tipe'] ?? '');
$kategori = trim($input['kategori'] ?? '');
$sumber = trim($input['sumber'] ?? '');
$link = trim($input['link'] ?? '');
$egovNewsId = (int)($input['egov_news_id'] ?? 0);
$errors = [];
if (empty($judul)) {
$errors['judul'] = 'Judul wajib diisi';
}
if (empty($tipe)) {
$errors['tipe'] = 'Tipe wajib diisi';
} elseif (!in_array($tipe, ['berita', 'konten'])) {
$errors['tipe'] = 'Tipe harus "berita" atau "konten"';
}
if (empty($kategori)) {
$errors['kategori'] = 'Kategori wajib diisi';
} else {
// Validasi multi-kategori (pisah dengan koma)
$allowedKategori = ['website', 'fb', 'facebook', 'ig', 'instagram', 'tiktok', 'twitter/x', 'twitter', 'youtube'];
$kategoriArr = array_map('trim', explode(',', $kategori));
foreach ($kategoriArr as $kat) {
$katLower = strtolower($kat);
if (!in_array($katLower, $allowedKategori)) {
$errors['kategori'] = "Kategori tidak valid: $kat";
break;
}
}
}
if (empty($sumber)) {
$errors['sumber'] = 'Sumber wajib diisi';
} elseif (!in_array($sumber, ['internal', 'eksternal', 'repost'])) {
$errors['sumber'] = 'Sumber harus "internal", "eksternal", atau "repost"';
}
if (!empty($errors)) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'code' => 400,
'message' => 'Validasi gagal',
'errors' => $errors
]);
exit();
}
// ============================================================
// NORMALISASI KATEGORI
// ============================================================
// Mapping nama panjang → kode PPID
$kategoriMap = [
'facebook' => 'fb',
'instagram' => 'ig',
'twitter' => 'twitter/x',
];
$kategoriArr = array_map('trim', explode(',', $kategori));
$kategoriNormalized = [];
foreach ($kategoriArr as $kat) {
$katLower = strtolower($kat);
$kategoriNormalized[] = $kategoriMap[$katLower] ?? $katLower;
}
$kategoriFinal = implode(',', $kategoriNormalized);
// ============================================================
// PROSES SYNC KE PPID
// ============================================================
try {
$ppid = new PPIDService($config);
$result = $ppid->saveRekapKonten(
$judul,
$tipe,
$kategoriFinal,
$sumber,
$link,
$egovNewsId > 0 ? $egovNewsId : null
);
if (isset($result['error']) && $result['error'] === false) {
// ✅ SUKSES
http_response_code(200);
echo json_encode([
'status' => 'success',
'code' => 200,
'message' => 'Konten berhasil disinkronkan ke PPID',
'data' => [
'judul' => $judul,
'tipe' => $tipe,
'kategori' => $kategoriFinal,
'sumber' => $sumber,
'link' => $link,
'egov_news_id' => $egovNewsId > 0 ? $egovNewsId : null,
'opd_name' => $config['opd_name']
]
]);
} else {
// ❌ GAGAL DARI PPID
http_response_code(500);
echo json_encode([
'status' => 'error',
'code' => 500,
'message' => 'Gagal sync ke PPID: ' . ($result['message'] ?? 'Unknown error'),
'debug' => $result
]);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'code' => 500,
'message' => 'Exception: ' . $e->getMessage()
]);
}