From 15f3da1b62530f0a20209310e6011a30881e5c06 Mon Sep 17 00:00:00 2001 From: simonpipe Date: Mon, 23 Feb 2026 08:40:40 +0100 Subject: [PATCH] Update mastodonapi.php --- mastodonapi.php | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/mastodonapi.php b/mastodonapi.php index e1a2e24..454de78 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.5"; + const VERSION = "0.9.6"; public $yellow; // access to API // Initialize extension @@ -147,39 +147,49 @@ class YellowMastodonapi { ?? $media['preview_url'] ?? ($media['meta']['original']['url'] ?? null) ?? ($media['meta']['small']['url'] ?? null); - - if (empty($imageUrl)) { - return false; - } - + + if (empty($imageUrl)) return false; + 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; - if (file_exists($fullPath)) return $fileName; - + + // Bestehende Datei nur akzeptieren, wenn sie nicht leer ist + if (file_exists($fullPath) && filesize($fullPath) > 0) return $fileName; + $ch = curl_init($imageUrl); - $headers = ["User-Agent: YellowMastodonAPI"]; + // 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"); if (!empty($accessToken)) { $headers[] = "Authorization: Bearer " . $accessToken; } + curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => $headers, - CURLOPT_SSL_VERIFYPEER => true, - CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_SSL_VERIFYPEER => false, // Erhöht die Kompatibilität massiv ]); + $imgData = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - + if ($imgData === false || $httpCode < 200 || $httpCode >= 300) { return false; } - + file_put_contents($fullPath, $imgData); return $fileName; }