qna/admin/fedi.php
2025-03-18 09:43:57 -04:00

58 lines
1.4 KiB
PHP

<?php
$query = "
SELECT * FROM data
WHERE id = {$id};
";
$queryResponse = pg_query($db, $query);
$question = pg_fetch_array($queryResponse);
if (strlen($question["fedipostid"]) == 0) {
$method = "POST";
$route = $fediAPIRoute;
} else {
$method = "PUT";
$route = $fediAPIRoute . "/" . $question["fedipostid"];
}
$cw = $fediCWFormat;
$msg = $fediMessageFormat;
if ($question["iscwed"] == "t") {
$cw = str_replace("%c", $question["cw"], $fediCWFormatWithCW);
$msg = str_replace("%c", $question["cw"], $fediMessageFormatWithCW);
}
$url = "{$pageProto}://" . $_SERVER["HTTP_HOST"] . $pagePath . "/fetch.php?id={$id}";
foreach (array(
"%q" => $question["text"],
"%r" => $question["responsetext"],
"%u" => $url
) as $replace => $text) {
$cw = str_replace($replace, $text, $cw);
$msg = str_replace($replace, $text, $msg);
}
$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" => $msg,
"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);
?>