From 6ad7d5a6ead241564d8cd5cb2f63b1a065b621c6 Mon Sep 17 00:00:00 2001 From: simonpipe Date: Wed, 27 May 2026 07:54:37 +0200 Subject: [PATCH] Update mastodonapi.php --- mastodonapi.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/mastodonapi.php b/mastodonapi.php index 454de78..169b33e 100644 --- a/mastodonapi.php +++ b/mastodonapi.php @@ -2,7 +2,7 @@ // Mastodonapi extension, https://codeberg.org/simonpipe/yellow-mastodonapi class YellowMastodonapi { - const VERSION = "0.9.6"; + const VERSION = "0.9.7"; public $yellow; // access to API // Initialize extension @@ -34,7 +34,7 @@ class YellowMastodonapi { echo "Mastodon Import gestartet...\n"; $count = $this->updateMastodonPosts(); if ( $count===false ) { - echo "Fehler: API nicht erreichbar oder falsche ID.\n"; + echo "Fehler: API nicht erreichbar, falsche ID oder Zugriff verweigert.\n"; return 500; } echo "Erfolg: " . $count . " neue Posts verarbeitet.\n"; @@ -75,25 +75,32 @@ class YellowMastodonapi { $statusSetting = $this->yellow->system->get("mastodonapiStatus"); $maxAgeHours = intval($this->yellow->system->get("mastodonapiMaxAge")); $limit = intval($this->yellow->system->get("mastodonapiLimit")); + if ( empty($instanceUrl) || empty($accountId) ) return false; + $apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=" . $limit; - $headers = array("User-Agent: YellowMastodonapi/" . self::VERSION); + + // FIX: Browser-ähnlicher User-Agent, da Snac strikt blockiert + $headers = array("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) YellowMastodonapi/" . self::VERSION); if ( !empty($accessToken) ) $headers[] = "Authorization: Bearer " . $accessToken; + $ch = curl_init($apiUrl); curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => true, - CURLOPT_SSL_VERIFYHOST => 2 + CURLOPT_SSL_VERIFYPEER => false, // FIX: Kompatibilität für GoToSocial/Snac erhöht + CURLOPT_SSL_VERIFYHOST => 0 )); $json = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); + if ( $json===false || $httpCode < 200 || $httpCode >= 300 ) return false; $posts = json_decode($json, true); if ( !is_array($posts) ) return false; + $postsCreated = 0; foreach ( $posts as $post ) { if ( !empty($post["reblog"]) || ( !empty($post["reblogged"]) && $post["reblogged"]===true ) ) continue; @@ -105,6 +112,7 @@ class YellowMastodonapi { } $tags = array_column($post["tags"] ?? array(), "name"); if ( !empty($hashtag) && !in_array(strtolower($hashtag), array_map("strtolower", $tags)) ) continue; + $title = $this->generateTitle($contentHtml); $titleSlug = $this->generateTitleSlug($title); $fileName = date("Y-m-d", strtotime($createdAt)) . "-" . $titleSlug . ".md"; @@ -152,21 +160,17 @@ class YellowMastodonapi { if (!is_dir($dirPath)) mkdir($dirPath, 0755, true); - // Dateiname etwas robuster ermitteln $fileName = basename(parse_url($imageUrl, PHP_URL_PATH)); - // Fallback falls Dateiname keine Endung hat (Mastodon-spezifisch) if (!strpos($fileName, '.')) { $fileName .= ".jpg"; } $fullPath = $dirPath . $fileName; - // Bestehende Datei nur akzeptieren, wenn sie nicht leer ist if (file_exists($fullPath) && filesize($fullPath) > 0) return $fileName; $ch = curl_init($imageUrl); - // Browser-ähnlicher User-Agent hilft gegen CDNs $headers = ["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) YellowMastodonAPI"]; $accessToken = $this->yellow->system->get("mastodonapiAccessToken"); @@ -179,7 +183,8 @@ class YellowMastodonapi { CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => false, // Erhöht die Kompatibilität massiv + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => 0 ]); $imgData = curl_exec($ch);