69 lines
No EOL
2.3 KiB
PHP
69 lines
No EOL
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Module G3: Outbound DM Broadcasting Post Router
|
|
* Target Path: htdocs/api/v1/direct_v2/broadcast.php
|
|
*/
|
|
|
|
require_once __DIR__ . '/../../../config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
die(json_encode(["status" => "fail", "message" => "Method not allowed."]));
|
|
}
|
|
|
|
$sessionCookie = $_COOKIE['sessionid'] ?? '';
|
|
if (empty($sessionCookie) || strpos($sessionCookie, 'nostolga_sess_') !== 0) {
|
|
http_response_code(401);
|
|
die(json_encode(["status" => "fail", "message" => "Unauthorized session context."]));
|
|
}
|
|
$userId = (int)str_replace('nostolga_sess_', '', $sessionCookie);
|
|
|
|
// Gather values pushed from the app UI form
|
|
$text = $_POST['text'] ?? '';
|
|
$recipientJson = $_POST['recipients'] ?? ''; // App packages recipient IDs inside an explicit JSON format array string
|
|
|
|
// Parse target out of the JSON parameter payload
|
|
$recipientId = 0;
|
|
if (!empty($recipientJson)) {
|
|
$recipientsArray = json_decode($recipientJson, true);
|
|
// Grab the first ID listed inside the array
|
|
$recipientId = (int)($recipientsArray[0] ?? 0);
|
|
}
|
|
|
|
// Fallback: Check standard direct target params if the array wrapper was bypassed
|
|
if ($recipientId === 0) {
|
|
$recipientId = (int)($_POST['recipient_users'] ?? 0);
|
|
}
|
|
|
|
if (empty($text) || $recipientId === 0) {
|
|
http_response_code(400);
|
|
die(json_encode(["status" => "fail", "message" => "Required payload message configurations missing."]));
|
|
}
|
|
|
|
$db = getDB();
|
|
|
|
try {
|
|
$timestamp = time();
|
|
|
|
// Write message into table matrix ledger
|
|
$stmt = $db->prepare("INSERT INTO direct_messages (sender_id, recipient_id, message_text, created_at) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$userId, $recipientId, $text, $timestamp]);
|
|
$newMsgId = $db->lastInsertId();
|
|
|
|
// Mirror the broadcast output structure format so it updates instantly in the chat UI screen
|
|
http_response_code(200);
|
|
echo json_encode([
|
|
"payload" => [
|
|
"item_id" => (string)$newMsgId,
|
|
"timestamp" => $timestamp * 1000000,
|
|
"item_type" => "text",
|
|
"text" => $text
|
|
],
|
|
"status" => "ok"
|
|
]);
|
|
exit;
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
die(json_encode(["status" => "fail", "message" => "Data broadcast recording sequence failed."]));
|
|
} |