nostolgagram/NostolgaGram/api/v1/media/configure.php
2026-06-24 21:33:55 +01:00

62 lines
No EOL
2 KiB
PHP

<?php
/**
* Module E2: Post Caption Configurator & Database Committer
* Target Path: htdocs/api/v1/media/configure.php
*/
require_once __DIR__ . '/../../../config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
die(json_encode(["status" => "fail", "message" => "Method not allowed."]));
}
// Parse Session Identity
$sessionCookie = $_COOKIE['sessionid'] ?? '';
if (empty($sessionCookie) || strpos($sessionCookie, 'nostolga_sess_') !== 0) {
http_response_code(401);
die(json_encode(["status" => "fail", "message" => "Session verification required."]));
}
$userId = (int)str_replace('nostolga_sess_', '', $sessionCookie);
$db = getDB();
// Gather payload configurations
$uploadId = $_POST['upload_id'] ?? '';
$caption = $_POST['caption_text'] ?? '';
// Grab the secure URL string cached from our upload script step
$cachedUrlCookie = "pending_media_" . $uploadId;
$mediaUrl = $_COOKIE[$cachedUrlCookie] ?? '';
if (empty($mediaUrl)) {
http_response_code(400);
die(json_encode(["status" => "fail", "message" => "Missing media resource locator. Upload session expired."]));
}
try {
// Write post metadata to the newly generated MySQL table
$timestamp = time();
$insertStmt = $db->prepare("INSERT INTO posts (user_id, media_url, caption, created_at) VALUES (?, ?, ?, ?)");
$insertStmt->execute([$userId, $mediaUrl, $caption, $timestamp]);
// Clear temporary cache token
setcookie($cachedUrlCookie, '', time() - 3600, "/");
} catch (PDOException $e) {
http_response_code(500);
die(json_encode(["status" => "fail", "message" => "Failed to write post records to data matrix."]));
}
// Respond back using the expected 2014 object model format so the app exits the upload screens seamlessly
http_response_code(200);
echo json_encode([
"media" => [
"pk" => (int)$db->lastInsertId(),
"media_type" => 1,
"code" => "post_conf_" . $uploadId,
"status" => "ok"
],
"status" => "ok"
]);
exit;