Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,121 @@
<?php
require_once '../includes/auth.php';
require_once '../includes/db.php';
require_once '../includes/api_helper.php';
require_once '../includes/logger.php';
require_once '../includes/notifier.php';
function sendJson($statusCode, $data) {
http_response_code($statusCode);
header('Content-Type: application/json');
echo json_encode($data);
exit();
}
// Check if user is admin
if (!isAdmin()) {
sendJson(403, ['error' => 'Permission denied. Admins only.']);
}
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
try {
// Auto-create table if not exists
try {
$pdo->query("SELECT 1 FROM settings LIMIT 1");
} catch (PDOException $e) {
$pdo->exec("
CREATE TABLE IF NOT EXISTS settings (
setting_key VARCHAR(50) PRIMARY KEY,
setting_value TEXT NULL,
description VARCHAR(255) NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
$defaultSettings = [
['line_notify_token', '', 'LINE Notify Token สำหรับส่งการแจ้งเตือน'],
['telegram_bot_token', '', 'Telegram Bot Token ที่ได้จาก BotFather'],
['telegram_chat_id', '', 'Telegram Chat ID หรือ Group ID ที่ต้องการให้บอทส่งข้อความ'],
['notify_events', '["vehicle","driver","schedule","maintenance"]', 'เหตุการณ์ที่ต้องการให้แจ้งเตือน (JSON array)']
];
$stmt = $pdo->prepare("INSERT IGNORE INTO settings (setting_key, setting_value, description) VALUES (?, ?, ?)");
foreach ($defaultSettings as $setting) {
$stmt->execute($setting);
}
}
$stmt = $pdo->query("SELECT setting_key, setting_value FROM settings");
$settingsData = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
sendJson(200, ['settings' => $settingsData]);
} catch (PDOException $e) {
sendJson(500, ['error' => 'Database error: ' . $e->getMessage()]);
}
} elseif ($method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
$action = $data['action'] ?? '';
if ($action === 'save') {
$settings = $data['settings'] ?? [];
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = ?");
foreach ($settings as $key => $value) {
// Ensure the setting key exists to avoid SQL errors if someone injects keys
$checkStmt = $pdo->prepare("SELECT setting_key FROM settings WHERE setting_key = ?");
$checkStmt->execute([$key]);
if ($checkStmt->fetch()) {
$stmt->execute([$value, $key]);
} else {
$insertStmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?)");
$insertStmt->execute([$key, $value]);
}
}
$pdo->commit();
logActivity("Updated Notification Settings", 'settings', null);
sendJson(200, ['success' => true]);
} catch (PDOException $e) {
$pdo->rollBack();
sendJson(500, ['error' => 'Database error: ' . $e->getMessage()]);
}
}
elseif ($action === 'test_line') {
$token = $data['token'] ?? '';
if (empty($token)) {
sendJson(400, ['error' => 'LINE Notify Token is required']);
}
$message = "✅ ทดสอบระบบแจ้งเตือนผ่าน LINE Notify\nจากระบบจัดการข้อมูลยานพาหนะ";
$result = sendLineNotify($token, $message);
if ($result === true) {
sendJson(200, ['success' => true]);
} else {
sendJson(400, ['error' => 'เกิดข้อผิดพลาดในการส่ง LINE Notify: ' . $result]);
}
}
elseif ($action === 'test_telegram') {
$token = $data['token'] ?? '';
$chat_id = $data['chat_id'] ?? '';
if (empty($token) || empty($chat_id)) {
sendJson(400, ['error' => 'Telegram Bot Token and Chat ID are required']);
}
$message = "✅ ทดสอบระบบแจ้งเตือนผ่าน Telegram\nจากระบบจัดการข้อมูลยานพาหนะ";
$result = sendTelegramMessage($token, $chat_id, $message);
if ($result === true) {
sendJson(200, ['success' => true]);
} else {
sendJson(400, ['error' => 'เกิดข้อผิดพลาดในการส่ง Telegram: ' . $result]);
}
}
else {
sendJson(400, ['error' => 'Invalid action']);
}
} else {
sendJson(405, ['error' => 'Method Not Allowed']);
}