129 lines
5.8 KiB
PHP
129 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* Gear-Planer, Katalog: Gegenstaende von aion2hub uebernehmen und abgleichen.
|
|
* Antwortet als JSON (Aufrufe aus assets/js/gear-admin.js), nur "delete" leitet zurueck.
|
|
*/
|
|
require_once __DIR__ . '/../includes/bootstrap.php';
|
|
require_once __DIR__ . '/../includes/gear.php';
|
|
require_permission('gear.manage');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
redirect('/admin/gear_items.php');
|
|
}
|
|
verify_csrf();
|
|
|
|
$userId = (int) current_user()['id'];
|
|
$action = (string) ($_POST['action'] ?? '');
|
|
|
|
/** JSON-Antwort und Ende. */
|
|
$respond = static function (array $data, int $status = 200): void {
|
|
http_response_code($status);
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
};
|
|
|
|
/**
|
|
* Hoechstens ein Abruf pro Sekunde (pro Sitzung) - aion2hub soll von uns nicht mehr merken als
|
|
* von jemandem, der sich durch die Seiten klickt.
|
|
*/
|
|
$politePause = static function (): void {
|
|
$last = (float) ($_SESSION['gear_source_last_fetch'] ?? 0);
|
|
$wait = 1.0 - (microtime(true) - $last);
|
|
if ($wait > 0) {
|
|
usleep((int) ($wait * 1000000));
|
|
}
|
|
$_SESSION['gear_source_last_fetch'] = microtime(true);
|
|
};
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'plan':
|
|
// Was steckt hinter der Eingabe? Ein einzelner Gegenstand oder Listen je Kategorie.
|
|
$input = trim((string) ($_POST['url'] ?? ''));
|
|
$single = gear_source_parse_id($input);
|
|
if ($single !== null) {
|
|
$respond(['ok' => true, 'ids' => [$single], 'known' => gear_item($single) ? [$single] : []]);
|
|
}
|
|
$respond(['ok' => true, 'lists' => gear_source_list_urls($input)]);
|
|
|
|
case 'list':
|
|
// Eine Seite einer Liste (Adresse aus "plan") -> enthaltene IDs und Seitenzahl
|
|
$url = trim((string) ($_POST['url'] ?? ''));
|
|
$page = max(1, (int) ($_POST['page'] ?? 1));
|
|
if (!in_array($url, gear_source_list_urls($url), true)) {
|
|
$respond(['ok' => false, 'error' => 'Ungültige Listen-Adresse.'], 400);
|
|
}
|
|
$politePause();
|
|
$result = gear_source_list_page(gear_source_fetch($url . '&page=' . $page));
|
|
$known = $result['ids']
|
|
? db()->query('SELECT id FROM gear_items WHERE id IN (' . implode(',', $result['ids']) . ')')->fetchAll(PDO::FETCH_COLUMN)
|
|
: [];
|
|
$respond(['ok' => true, 'known' => array_map('intval', $known)] + $result);
|
|
|
|
case 'import':
|
|
case 'check':
|
|
$id = (int) ($_POST['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
$respond(['ok' => false, 'error' => 'Ungültige ID.'], 400);
|
|
}
|
|
$politePause();
|
|
$parsed = gear_item_fetch($id);
|
|
$old = gear_item($id);
|
|
$diffs = gear_item_diff($old, $parsed);
|
|
// Abgleich ohne Uebernehmen: nur zeigen, was anders waere
|
|
$apply = $action === 'import' || !empty($_POST['apply']);
|
|
if ($apply) {
|
|
$result = gear_item_store($parsed, $userId);
|
|
} elseif ($old !== null) {
|
|
// Trotzdem als "geprueft" vermerken, wenn sich nichts geaendert hat
|
|
if (!$diffs) {
|
|
db()->prepare('UPDATE gear_items SET checked_at = NOW(), checked_by = ?, source_version = ? WHERE id = ?')
|
|
->execute([$userId, mb_substr($parsed['source_version'], 0, 120), $id]);
|
|
}
|
|
}
|
|
if (empty($_POST['batch']) && $apply) {
|
|
admin_log($old === null ? '🗡️ Gear-Planer: Gegenstand übernommen' : '🗡️ Gear-Planer: Gegenstand abgeglichen', [
|
|
'Gegenstand' => $parsed['name'] . ' (#' . $id . ')',
|
|
'Änderungen' => $old === null ? 'neu' : ($diffs ? implode(', ', array_column($diffs, 0)) : 'keine'),
|
|
], 'info', 'gear');
|
|
}
|
|
$respond([
|
|
'ok' => true,
|
|
'id' => $id,
|
|
'name' => $parsed['name'],
|
|
'new' => $old === null,
|
|
'applied' => $apply,
|
|
'diffs' => $old === null ? [] : $diffs,
|
|
]);
|
|
|
|
case 'batch_done':
|
|
// Eine Zusammenfassung pro Durchlauf statt eines Eintrags je Gegenstand
|
|
$summary = array_filter([
|
|
'Art' => (string) ($_POST['kind'] ?? '') === 'check' ? 'Abgleich' : 'Übernahme',
|
|
'Neu' => (int) ($_POST['new'] ?? 0) ?: null,
|
|
'Geändert' => (int) ($_POST['changed'] ?? 0) ?: null,
|
|
'Unverändert' => (int) ($_POST['same'] ?? 0) ?: null,
|
|
'Fehler' => (int) ($_POST['failed'] ?? 0) ?: null,
|
|
'Quelle' => mb_substr((string) ($_POST['source'] ?? ''), 0, 200) ?: null,
|
|
], static fn ($value): bool => $value !== null);
|
|
admin_log('🗡️ Gear-Planer: Gegenstände von aion2hub', $summary, 'info', 'gear');
|
|
$respond(['ok' => true]);
|
|
|
|
case 'delete':
|
|
$item = gear_item((int) ($_POST['id'] ?? 0));
|
|
if ($item) {
|
|
db()->prepare('DELETE FROM gear_items WHERE id = ?')->execute([$item['id']]);
|
|
if (!empty($item['icon']) && strpos((string) $item['icon'], '/') === false) {
|
|
@unlink(GEAR_ICON_DIR . $item['icon']);
|
|
}
|
|
admin_log('🗡️ Gear-Planer: Gegenstand gelöscht', ['Gegenstand' => $item['name'] . ' (#' . $item['id'] . ')'], 'danger', 'gear');
|
|
set_flash('success', '„' . $item['name'] . '“ gelöscht - in Sets ist der Slot jetzt leer.');
|
|
}
|
|
redirect('/admin/gear_items.php');
|
|
}
|
|
} catch (RuntimeException $e) {
|
|
$respond(['ok' => false, 'error' => $e->getMessage()], 422);
|
|
}
|
|
|
|
$respond(['ok' => false, 'error' => 'Unbekannte Aktion.'], 400);
|