LuminaWebsite/includes/raids_api.php
Maaxxs 620ef68015 Add a Raids page fed by the guild's external raids API
New raids.php shows upcoming raids (date, requirements, gearscore,
groups and their signups) pulled from https://api.maaxxs-coding.de/api/raids
via includes/raids_api.php (cURL + X-API-Key header), gated behind a
new raids.view permission so it fits the existing permission-group
model. Fresh installs seed raids.view onto Mitglied automatically;
existing installs need to add it once via the Berechtigungsgruppen UI
(documented in the README). API/network failures show a friendly
error instead of crashing the page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-18 12:44:17 +02:00

52 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
/**
* @return array<int, array<string, mixed>>
* @throws RuntimeException
*/
function fetch_raids(): array
{
$configPath = __DIR__ . '/../config.php';
if (!file_exists($configPath)) {
throw new RuntimeException('Konfigurationsdatei fehlt.');
}
$config = require $configPath;
$apiUrl = $config['raids_api']['url'] ?? '';
$apiKey = $config['raids_api']['key'] ?? '';
if ($apiUrl === '' || $apiKey === '') {
throw new RuntimeException('Raid-API ist nicht konfiguriert (config.php: raids_api.url / raids_api.key fehlt).');
}
if (!function_exists('curl_init')) {
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
}
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: ' . $apiKey],
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($response === false) {
throw new RuntimeException('Verbindung zur Raid-API fehlgeschlagen: ' . $curlError);
}
if ($httpCode !== 200) {
throw new RuntimeException('Raid-API antwortete mit Statuscode ' . $httpCode . '.');
}
$data = json_decode($response, true);
if (!is_array($data) || ($data['status'] ?? '') !== 'success' || !isset($data['raids']) || !is_array($data['raids'])) {
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
}
return $data['raids'];
}