mastodonapi.php aktualisiert

This commit is contained in:
simonpipe 2025-08-11 17:43:34 +02:00
parent 2c6033ede1
commit c4cba211f0

View file

@ -1,8 +1,8 @@
<?php
// MastodonAPI extension (public-only), https://codeberg.org/simonpipe/yellow-mastodonapi
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
class YellowMastodonAPI {
const VERSION = "0.2";
const VERSION = "0.21";
public $yellow;
// Initialisierung
@ -19,8 +19,9 @@ class YellowMastodonAPI {
// Webhook-Request
public function onRequest($scheme, $address, $base, $location, $fileName) {
$triggerPath = $this->yellow->system->get("mastodonapiTriggerPath");
if ($location == $triggerPath) {
$triggerPath = rtrim($this->yellow->system->get("mastodonapiTriggerPath"), "/");
$locationClean = rtrim($location, "/");
if ($locationClean === $triggerPath) {
$postsCreated = $this->processMastodonFeed();
if ($postsCreated !== false) {
$this->sendResponse("Mastodon API feed processed. Created " . $postsCreated . " posts.", 200);
@ -45,7 +46,7 @@ class YellowMastodonAPI {
}
}
// API-Logik
// API-Logik ohne Access Token
private function processMastodonFeed() {
$instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl");
$accountId = $this->yellow->system->get("mastodonapiAccountId");
@ -60,21 +61,27 @@ class YellowMastodonAPI {
}
$apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=20";
$context = stream_context_create([
'http' => [
'method' => "GET",
'header' => "User-Agent: YellowMastodonAPI\r\n",
'timeout' => 30
],
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true
]
]);
$json = @file_get_contents($apiUrl, false, $context);
// 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: Could not fetch API data from $apiUrl");
error_log("ERROR: MastodonAPI: Curl error: " . curl_error($ch));
curl_close($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
error_log("ERROR: MastodonAPI: HTTP status $httpCode from $apiUrl");
return false;
}
@ -90,9 +97,9 @@ class YellowMastodonAPI {
foreach ($posts as $post) {
$contentHtml = $post['content'];
$createdAt = $post['created_at'];
$tags = array_column($post['tags'], 'name');
$tags = array_map('strtolower', array_column($post['tags'], 'name'));
if (!empty($hashtag) && !in_array(strtolower($hashtag), array_map('strtolower', $tags))) {
if (!empty($hashtag) && !in_array(strtolower($hashtag), $tags)) {
continue;
}
@ -103,7 +110,6 @@ 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;