mastodonapi.php aktualisiert

This commit is contained in:
simonpipe 2025-08-12 10:27:48 +02:00
parent 6f52cfa71d
commit 246adf20cf

View file

@ -2,7 +2,7 @@
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
class YellowMastodonAPI {
const VERSION = "0.2.1";
const VERSION = "0.2";
public $yellow;
// Initialisierung
@ -10,6 +10,7 @@ class YellowMastodonAPI {
$this->yellow = $yellow;
$this->yellow->system->setDefault("mastodonapiInstanceUrl", "https://example.social");
$this->yellow->system->setDefault("mastodonapiAccountId", "");
$this->yellow->system->setDefault("mastodonapiAccessToken", ""); // optional
$this->yellow->system->setDefault("mastodonapiHashtag", "");
$this->yellow->system->setDefault("mastodonapiAuthor", "YellowUser");
$this->yellow->system->setDefault("mastodonapiTag", "Fediverse");
@ -19,9 +20,8 @@ class YellowMastodonAPI {
// Webhook-Request
public function onRequest($scheme, $address, $base, $location, $fileName) {
$triggerPath = rtrim($this->yellow->system->get("mastodonapiTriggerPath"), "/");
$locationClean = rtrim($location, "/");
if ($locationClean === $triggerPath) {
$triggerPath = $this->yellow->system->get("mastodonapiTriggerPath");
if ($location == $triggerPath) {
$postsCreated = $this->processMastodonFeed();
if ($postsCreated !== false) {
$this->sendResponse("Mastodon API feed processed. Created " . $postsCreated . " posts.", 200);
@ -46,10 +46,11 @@ class YellowMastodonAPI {
}
}
// API-Logik ohne Access Token
// API-Logik
private function processMastodonFeed() {
$instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl");
$accountId = $this->yellow->system->get("mastodonapiAccountId");
$accessToken = $this->yellow->system->get("mastodonapiAccessToken"); // optional
$hashtag = ltrim($this->yellow->system->get("mastodonapiHashtag"), '#');
$defaultAuthor = $this->yellow->system->get("mastodonapiAuthor");
$defaultTag = $this->yellow->system->get("mastodonapiTag");
@ -62,26 +63,27 @@ class YellowMastodonAPI {
$apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=20";
// Abruf mit curl
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "YellowMastodonAPI/0.2-public");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$json = curl_exec($ch);
if ($json === false) {
error_log("ERROR: MastodonAPI: Curl error: " . curl_error($ch));
curl_close($ch);
return false;
// Header dynamisch aufbauen
$headers = "User-Agent: YellowMastodonAPI\r\n";
if (!empty($accessToken)) {
$headers .= "Authorization: Bearer " . $accessToken . "\r\n";
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$context = stream_context_create([
'http' => [
'method' => "GET",
'header' => $headers,
'timeout' => 30
],
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true
]
]);
if ($httpCode !== 200) {
error_log("ERROR: MastodonAPI: HTTP status $httpCode from $apiUrl");
$json = @file_get_contents($apiUrl, false, $context);
if ($json === false) {
error_log("ERROR: MastodonAPI: Could not fetch API data from $apiUrl");
return false;
}
@ -110,6 +112,7 @@ class YellowMastodonAPI {
$fullPath = $config->get("coreServerDocument") . $targetFolder . $fileName;
if (file_exists($fullPath)) continue;
if (!is_dir(dirname($fullPath))) mkdir(dirname($fullPath), 0755, true);
$authorName = $post['account']['display_name'] ?: $defaultAuthor;