qna/admin/fedi.php
2025-03-12 08:31:53 -04:00

47 lines
1.2 KiB
PHP

<?php
$query = "
SELECT * FROM data
WHERE id = {$id};
";
$queryResponse = pg_query($db, $query);
$question = pg_fetch_array($queryResponse);
$url = "{$pageProto}://" . $_SERVER["HTTP_HOST"] . $pagePath . "/fetch.php?id={$id}";
if (strlen($question["fedipostid"]) == 0) {
$method = "POST";
$route = $fediAPIRoute;
} else {
$method = "PUT";
$route = $fediAPIRoute . "/" . $question["fedipostid"];
}
$cw = "anonymous question response (automated)";
if ($question["iscwed"] == "t") {
$cw .= "; cw: {$question['cw']}";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$fediAPIToken}"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $route);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
"status" => "question: {$question['text']}\n\nresponse: {$question['responsetext']}\n\nlink: {$url}",
"visibility" => $fediVisibility,
"spoiler_text" => $cw
)));
$resp = curl_exec($ch);
if (strlen($question["fedipostid"]) == 0) {
$jsonResp = json_decode($resp, true);
$fediID = $jsonResp["id"];
pg_update($db, "data", array("fedipostid" => $fediID), array("id" => $id));
}
curl_close($ch);
?>