yellow = $yellow; $yellow->system->setDefault("mastodonapiInstance", "https://mastodon.social"); $yellow->system->setDefault("mastodonapiUserId", ""); $yellow->system->setDefault("mastodonapiToken", ""); $yellow->system->setDefault("mastodonapiHashtag", ""); // Nur Posts mit diesem Hashtag werden importiert $yellow->system->setDefault("mastodonapiTag", "Fediverse"); // Dieser Tag wird im Yellow CMS gesetzt $yellow->system->setDefault("mastodonapiAuthor", "YellowUser"); $yellow->system->setDefault("mastodonapiFolder", "content/1-blog/"); $yellow->system->setDefault("mastodonapiTriggerPath", "/mastodonapi-webhook-CHANGE_THIS_SECRET_KEY"); } public function onRequest($scheme, $address, $base, $location, $fileName) { $triggerPath = $this->yellow->system->get("mastodonapiTriggerPath"); if ($location == $triggerPath) { $postsCreated = $this->processMastodonAPI(); if ($postsCreated !== false) { $this->sendResponse("Mastodon API processed. Created $postsCreated posts.", 200); } else { $this->sendResponse("Error processing Mastodon API. Check logs.", 500); } return 1; } return 0; } public function onCommand($command, $text) { if ($command == "mastodonapi") { $postsCreated = $this->processMastodonAPI(); return $postsCreated !== false ? 0 : 500; } return 0; } private function processMastodonAPI() { $instance = rtrim($this->yellow->system->get("mastodonapiInstance"), "/"); $userId = $this->yellow->system->get("mastodonapiUserId"); $token = $this->yellow->system->get("mastodonapiToken"); $hashtag = $this->yellow->system->get("mastodonapiHashtag"); $tag = $this->yellow->system->get("mastodonapiTag"); $author = $this->yellow->system->get("mastodonapiAuthor"); $folder = $this->yellow->system->get("mastodonapiFolder"); if (empty($userId) || empty($token)) { error_log("ERROR: MastodonAPI: Missing user ID or token."); return false; } $url = "$instance/api/v1/accounts/$userId/statuses?exclude_replies=true&exclude_reblogs=true&limit=5"; $opts = [ "http" => [ "method" => "GET", "header" => "Authorization: Bearer $token\r\nUser-Agent: YellowCMS\r\n" ] ]; $context = stream_context_create($opts); $json = @file_get_contents($url, false, $context); if ($json === false) { error_log("ERROR: MastodonAPI: Failed to fetch API data."); return false; } $statuses = json_decode($json, true); if (!is_array($statuses)) { error_log("ERROR: MastodonAPI: Invalid JSON response."); return false; } $postsCreated = 0; foreach ($statuses as $status) { // Nur Posts mit dem gewünschten Hashtag importieren if (!empty($hashtag) && stripos($status["content"], "#$hashtag") === false) { continue; } $content = strip_tags($status["content"]); $createdAt = $status["created_at"]; $title = mb_substr(preg_replace("/\\s+/", " ", $content), 0, 50); $slug = strtolower(preg_replace("/[^a-z0-9]+/", "-", $title)); $datePrefix = date("Y-m-d", strtotime($createdAt)); $fileName = "$datePrefix-$slug.md"; $path = $this->yellow->system->get("coreServerDocument") . $folder . $fileName; if (file_exists($path)) continue; $markdown = << "$content" > — [@{$status["account"]["acct"]}]({$status["account"]["url"]}) MARKDOWN; if (file_put_contents($path, $markdown) !== false) { $postsCreated++; } } return $postsCreated; } private function sendResponse($message, $statusCode = 200) { if (!headers_sent()) { header("HTTP/1.1 $statusCode"); header("Content-Type: text/plain"); } echo $message; exit; } }