File manager - Edit - /home/pack/api-secure/register_device.php
Back
<?php /** * PACK BEACON – SECURE API * Register FCM Device Token * Path: /api-secure/register_device.php * * ใช้สำหรับ: * - แอป Android / iOS / Web ลงทะเบียนอุปกรณ์รับการแจ้งเตือน */ require_once __DIR__ . "/config-secure.php"; require_once __DIR__ . "/auth-secure.php"; // รับเฉพาะ POST เท่านั้น require_method("POST"); // ตรวจ Token + Timestamp + Signature list($raw, $input) = require_secure_json(); /* ============================================================ รับค่าจากอุปกรณ์ ============================================================ */ $user_id = intval($input["user_id"] ?? 0); $platform = strtolower(trim($input["platform"] ?? "android")); // android / ios / web $fcm_token = trim($input["fcm_token"] ?? ""); /* ============================================================ ตรวจสอบความถูกต้องของข้อมูล ============================================================ */ if ($user_id <= 0 || $fcm_token == "") { api_json([ "status" => "error", "message" => "Missing required fields (user_id, fcm_token)" ]); } if (!in_array($platform, ["android", "ios", "web"])) { api_json([ "status" => "error", "message" => "Invalid platform" ]); } /* ============================================================ ตรวจสอบว่าผู้ใช้มีอยู่จริงหรือไม่ ============================================================ */ $stmt = $conn->prepare("SELECT id FROM users WHERE id = ?"); $stmt->bind_param("i", $user_id); $stmt->execute(); $user = $stmt->get_result()->fetch_assoc(); if (!$user) { api_json([ "status" => "error", "message" => "User not found" ]); } /* ============================================================ ลบ token ซ้ำ (กัน token เดียวซ้ำหลาย user/device) ============================================================ */ $stmt = $conn->prepare("DELETE FROM devices WHERE fcm_token = ?"); $stmt->bind_param("s", $fcm_token); $stmt->execute(); /* ============================================================ เช็คว่า user มี device ตัวอื่นแล้วหรือไม่ (1 user อาจมีหลาย device ก็ได้ เช่น มือถือ + tablet) ============================================================ */ $stmt = $conn->prepare(" SELECT id FROM devices WHERE user_id = ? AND platform = ? "); $stmt->bind_param("is", $user_id, $platform); $stmt->execute(); $exists = $stmt->get_result()->fetch_assoc(); /* ============================================================ ถ้ามี → อัปเดต token ใหม่ ถ้าไม่มี → เพิ่มใหม่ ============================================================ */ if ($exists) { $device_id = intval($exists["id"]); $stmt = $conn->prepare(" UPDATE devices SET fcm_token = ?, created_at = NOW() WHERE id = ? "); $stmt->bind_param("si", $fcm_token, $device_id); $stmt->execute(); api_json([ "status" => "updated", "message" => "Device token updated", "device_id" => $device_id ]); } else { $stmt = $conn->prepare(" INSERT INTO devices (user_id, platform, fcm_token) VALUES (?, ?, ?) "); $stmt->bind_param("iss", $user_id, $platform, $fcm_token); $stmt->execute(); api_json([ "status" => "registered", "message" => "Device registered successfully", "device_id" => $conn->insert_id ]); }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.4 |
proxy
|
phpinfo
|
Settings