Update mastodonapi.php

This commit is contained in:
simonpipe 2026-02-23 08:40:40 +01:00
parent 128570ddc1
commit 15f3da1b62

View file

@ -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
@ -148,30 +148,40 @@ class YellowMastodonapi {
?? ($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);