155 lines
7.5 KiB
PHP
155 lines
7.5 KiB
PHP
<?php
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Content-Type: application/json; charset=UTF-8");
|
|
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE");
|
|
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
// ตั้งค่าการเชื่อมต่อฐานข้อมูล MySQL
|
|
$host = "localhost";
|
|
$db_name = "it_asset";
|
|
$username = "root";
|
|
$password = "@Samui@10742";
|
|
|
|
try {
|
|
$conn = new PDO("mysql:host=$host;dbname=$db_name;charset=utf8", $username, $password);
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $exception) {
|
|
echo json_encode(["error" => "เชื่อมต่อฐานข้อมูลไม่ได้: " . $exception->getMessage()]);
|
|
exit;
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
// ดึงข้อมูล (GET)
|
|
if ($method === 'GET') {
|
|
try {
|
|
$stmt = $conn->prepare("SELECT * FROM assets ORDER BY last_seen DESC");
|
|
$stmt->execute();
|
|
$assets = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode($assets);
|
|
} catch (PDOException $e) {
|
|
echo json_encode(["error" => "ดึงข้อมูลผิดพลาด: " . $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// รับและอัปเดตข้อมูล (POST)
|
|
if ($method === 'POST') {
|
|
$data = json_decode(file_get_contents("php://input"));
|
|
|
|
if (!empty($data->hostname)) {
|
|
// เตรียมข้อมูล
|
|
$id = isset($data->id) ? $data->id : null;
|
|
$asset_tag = !empty($data->asset_tag) ? $data->asset_tag : null;
|
|
$hostname = $data->hostname;
|
|
$os_version = isset($data->os_version) ? $data->os_version : null;
|
|
$cpu_model = isset($data->cpu_model) ? $data->cpu_model : null;
|
|
$ram_gb = isset($data->ram_gb) ? $data->ram_gb : null;
|
|
$storage_gb = isset($data->storage_gb) ? $data->storage_gb : null;
|
|
$ip_address = isset($data->ip_address) ? $data->ip_address : null;
|
|
$mac_address = isset($data->mac_address) ? $data->mac_address : null;
|
|
$status = !empty($data->status) ? $data->status : 'Active';
|
|
$is_auto_updated = isset($data->is_auto_updated) ? $data->is_auto_updated : 1;
|
|
|
|
try {
|
|
if ($id) {
|
|
// อัปเดตข้อมูลทั้งหมดผ่าน ID (แก้ไขข้อผิดพลาดแล้ว)
|
|
$sql = "UPDATE assets SET
|
|
asset_tag = :asset_tag,
|
|
hostname = :hostname,
|
|
os_version = :os_version,
|
|
cpu_model = :cpu_model,
|
|
ram_gb = :ram_gb,
|
|
storage_gb = :storage_gb,
|
|
ip_address = :ip_address,
|
|
mac_address = :mac_address,
|
|
status = :status,
|
|
is_auto_updated = :is_auto_updated,
|
|
last_seen = NOW()
|
|
WHERE id = :id";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->execute([
|
|
':asset_tag' => $asset_tag,
|
|
':hostname' => $hostname,
|
|
':os_version' => $os_version,
|
|
':cpu_model' => $cpu_model,
|
|
':ram_gb' => $ram_gb,
|
|
':storage_gb' => $storage_gb,
|
|
':ip_address' => $ip_address,
|
|
':mac_address' => $mac_address,
|
|
':status' => $status,
|
|
':is_auto_updated' => $is_auto_updated,
|
|
':id' => $id
|
|
]);
|
|
} else {
|
|
// ตรวจสอบเครื่องที่มีอยู่แล้ว (กรณี Agent/เพิ่มใหม่)
|
|
$check = $conn->prepare("SELECT id FROM assets WHERE hostname = :hostname LIMIT 1");
|
|
$check->execute([':hostname' => $hostname]);
|
|
$exists = $check->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($exists) {
|
|
$sql = "UPDATE assets SET
|
|
asset_tag = COALESCE(:asset_tag, asset_tag),
|
|
os_version = COALESCE(:os_version, os_version),
|
|
cpu_model = COALESCE(:cpu_model, cpu_model),
|
|
ram_gb = COALESCE(:ram_gb, ram_gb),
|
|
storage_gb = COALESCE(:storage_gb, storage_gb),
|
|
ip_address = COALESCE(:ip_address, ip_address),
|
|
mac_address = COALESCE(:mac_address, mac_address),
|
|
status = COALESCE(:status, status),
|
|
is_auto_updated = :is_auto_updated,
|
|
last_seen = NOW()
|
|
WHERE id = :id";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->execute([
|
|
':asset_tag' => $asset_tag,
|
|
':os_version' => $os_version,
|
|
':cpu_model' => $cpu_model,
|
|
':ram_gb' => $ram_gb,
|
|
':storage_gb' => $storage_gb,
|
|
':ip_address' => $ip_address,
|
|
':mac_address' => $mac_address,
|
|
':status' => $status,
|
|
':is_auto_updated' => $is_auto_updated,
|
|
':id' => $exists['id']
|
|
]);
|
|
} else {
|
|
$sql = "INSERT INTO assets (asset_tag, hostname, os_version, cpu_model, ram_gb, storage_gb, ip_address, mac_address, status, is_auto_updated)
|
|
VALUES (:asset_tag, :hostname, :os_version, :cpu_model, :ram_gb, :storage_gb, :ip_address, :mac_address, :status, :is_auto_updated)";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->execute([
|
|
':asset_tag' => $asset_tag,
|
|
':hostname' => $hostname,
|
|
':os_version' => $os_version,
|
|
':cpu_model' => $cpu_model,
|
|
':ram_gb' => $ram_gb,
|
|
':storage_gb' => $storage_gb,
|
|
':ip_address' => $ip_address,
|
|
':mac_address' => $mac_address,
|
|
':status' => $status,
|
|
':is_auto_updated' => $is_auto_updated
|
|
]);
|
|
}
|
|
}
|
|
|
|
echo json_encode(["success" => true, "message" => "บันทึกข้อมูลสำเร็จ"]);
|
|
} catch (PDOException $e) {
|
|
if ($e->getCode() == 23000) {
|
|
echo json_encode(["error" => "รหัสทรัพย์สิน (Asset Tag) หรือ MAC Address นี้ถูกใช้ในระบบไปแล้ว"]);
|
|
} else {
|
|
echo json_encode(["error" => "เกิดข้อผิดพลาดที่ฐานข้อมูล: " . $e->getMessage()]);
|
|
}
|
|
}
|
|
} else {
|
|
echo json_encode(["error" => "ข้อมูลไม่ครบถ้วน กรุณาระบุชื่อเครื่อง"]);
|
|
}
|
|
}
|