LuminaWebsite/includes/moderation.php
Maaxxs 9eb340684e Consilium: only accounts with consilium.view see it - normal legion members see nothing of it
Asked for: normal members must not see anything of the alliances - not the alliance member list, not their
calendar entries - and the alliance things out of the patch notes.

- consilium_can_view() is now consilium.view only (before: every legion member). Normal members get no menu entry,
  no Consilium page, no member-list tab, no Consilium raids (raids page, dashboard, calendar) and no share button
  or Consilium hints on the map (tactics_api.php: consilium flag and consilium_share need consilium.view).
- Calendar: Consilium entries only with consilium.view - calendar_can_view(), the iCal subscription, the "Für wen?"
  option and the moderation queue and its counter.
- member.php: profiles of allied members only for those who see the Consilium.
- Allies no longer counted in legion lists: Lumipunkte rank on the dashboard, premade invite candidates, class
  counts on the professions page.
- Map: normal members in a shared session see guests like other participants, without 🤝 and legion.
- Patch note about the Consilium removed; permission description updated (schema 70); admin setup hint and README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 02:14:44 +02:00

335 lines
13 KiB
PHP

<?php
declare(strict_types=1);
/**
* Moderation von Galeriebildern und Kommentaren.
*
* Wer darf direkt posten? Alles ist sofort sichtbar - ausser:
* - das Konto ist neu (erste MODERATION_TRUST_DAYS Tage seit Discord-Beitritt bzw. Kontoerstellung),
* - ein Kommentar enthaelt einen Link,
* - die Moderation hat "Alles manuell freigeben" eingeschaltet (z.B. bei einer Spam-Welle).
* Dann wartet der Inhalt in der Freigabe-Liste (moderation.php). Der Verfasser sieht ihn schon,
* alle anderen erst nach der Freigabe.
*
* Melden: Ab MODERATION_REPORT_THRESHOLD Meldungen verschiedener Mitglieder wird ein Inhalt
* automatisch ausgeblendet, bis die Moderation entscheidet.
*/
const MODERATION_TRUST_DAYS = 14;
const MODERATION_REPORT_THRESHOLD = 2;
// Inhaltsarten: Schluessel => [Tabelle, Beschriftung, Spalte mit dem Verfasser, Spalte mit dem Text]
const MODERATION_TYPES = [
'picture' => ['member_gallery', 'Bild', 'user_id', 'caption'],
'comment' => ['gallery_comments', 'Kommentar', 'author_id', 'body'],
'calendar' => ['calendar_entries', 'Termin', 'user_id', 'title'],
];
/** Darf das angemeldete Konto moderieren? */
function moderation_can(): bool
{
return user_has_permission('comments.moderate');
}
function moderation_all_manual(): bool
{
return setting_get('moderation_all') === '1';
}
/** Ist das angemeldete Konto schon lange genug dabei, um ohne Freigabe zu posten? */
function moderation_user_trusted(): bool
{
$user = current_user();
if (!$user) {
return false;
}
$stmt = db()->prepare('SELECT COALESCE(discord_joined_at, created_at) FROM users WHERE id = ?');
$stmt->execute([(int) $user['id']]);
$since = strtotime((string) $stmt->fetchColumn());
return $since !== false && $since < time() - MODERATION_TRUST_DAYS * 86400;
}
/** Enthaelt der Text einen Link (auch ohne http, z.B. www.… oder discord.gg/…)? */
function moderation_has_link(string $text): bool
{
// Links auf Orte unserer Karte sind harmlos
$text = map_links_strip($text);
return (bool) preg_match('~https?://|www\.|discord\.gg/|\b[a-z0-9-]+\.(com|de|net|org|io|gg|ru|xyz|ly|me|tk|top|shop)\b~i', $text);
}
/**
* Status fuer neuen Inhalt des angemeldeten Kontos.
*
* @return array{0: string, 1: ?string} [status, Grund]
*/
function moderation_initial_status(string $type, string $text = ''): array
{
if (moderation_can()) {
return ['visible', null];
}
$hits = moderation_word_hits($text);
if (moderation_word_action($hits) === 'hold') {
return ['pending', 'Schimpfwortfilter: ' . implode(', ', array_keys(array_filter($hits, static fn ($a) => $a === 'hold')))];
}
if (moderation_all_manual()) {
return ['pending', 'Alles wird gerade manuell freigegeben'];
}
if (!moderation_user_trusted()) {
return ['pending', 'Neues Mitglied (erste ' . MODERATION_TRUST_DAYS . ' Tage)'];
}
// Links (auch in Bildunterschriften) - beliebter Weg fuer Spam und Phishing
if ($text !== '' && moderation_has_link($text)) {
return ['pending', 'Enthält einen Link'];
}
return ['visible', null];
}
/**
* Offenes in der Moderation (fuer die Zahl in der Seitenleiste), gezaehlt wie auf moderation.php:
* [wartet auf Freigabe, gemeldet und noch sichtbar]. Interne Termine der Leitung zaehlen nur fuer
* Moderatoren, die sie auch sehen (calendar.leadership), Consilium-Termine nur mit consilium.view.
*
* @return array{0: int, 1: int}
*/
function moderation_open_counts(): array
{
$pending = 0;
$reported = 0;
foreach (MODERATION_TYPES as $type => [$table]) {
$where = $type !== 'calendar' ? '' : (user_has_permission('calendar.leadership') ? '' : "audience <> 'internal' AND ")
. (consilium_can_view() ? '' : "audience <> 'consilium' AND ");
try {
$pending += (int) db()->query("SELECT COUNT(*) FROM $table WHERE {$where}status = 'pending'")->fetchColumn();
$reported += (int) db()->query(
"SELECT COUNT(*) FROM $table t WHERE {$where}t.status = 'visible'
AND EXISTS (SELECT 1 FROM content_reports r WHERE r.content_type = " . db()->quote($type) . " AND r.content_id = t.id)"
)->fetchColumn();
} catch (PDOException $e) {
// Spalten/Tabellen noch nicht angelegt
}
}
return [$pending, $reported];
}
/**
* Inhalt melden. Ab MODERATION_REPORT_THRESHOLD Meldungen wird er ausgeblendet.
*
* @return string Meldung fuer den Nutzer
*/
function moderation_report(string $type, int $contentId, string $reason): string
{
$me = current_user();
[$table, $label, $owner] = MODERATION_TYPES[$type];
$stmt = db()->prepare("SELECT id, $owner AS owner_id, status FROM $table WHERE id = ?");
$stmt->execute([$contentId]);
$content = $stmt->fetch();
if (!$content) {
return 'Diesen Inhalt gibt es nicht (mehr).';
}
if ((int) $content['owner_id'] === (int) $me['id']) {
return 'Eigene Inhalte kannst du nicht melden - aber löschen.';
}
try {
db()->prepare('INSERT INTO content_reports (content_type, content_id, reporter_id, reason) VALUES (?, ?, ?, ?)')
->execute([$type, $contentId, (int) $me['id'], $reason !== '' ? $reason : null]);
} catch (PDOException $e) {
return 'Du hast diesen Inhalt schon gemeldet - danke, die Moderation schaut es sich an.';
}
$count = db()->prepare('SELECT COUNT(*) FROM content_reports WHERE content_type = ? AND content_id = ?');
$count->execute([$type, $contentId]);
$reports = (int) $count->fetchColumn();
$hidden = false;
if ($content['status'] === 'visible' && $reports >= MODERATION_REPORT_THRESHOLD) {
db()->prepare("UPDATE $table SET status = 'pending', moderation_note = ? WHERE id = ?")
->execute([$reports . ' Meldungen', $contentId]);
$hidden = true;
}
admin_log($hidden ? '🚩 ' . $label . ' nach Meldungen ausgeblendet' : '🚩 ' . $label . ' gemeldet', [
'Von' => admin_log_user((int) $content['owner_id']),
'Grund' => $reason,
'Meldungen' => (string) $reports,
'Prüfen' => 'Moderation → Freigabe-Liste',
], 'warning', 'members');
return $hidden
? 'Danke für die Meldung. Der Inhalt ist ausgeblendet, bis die Moderation entschieden hat.'
: 'Danke für die Meldung - die Moderation schaut es sich an.';
}
/** Meldungen zu einem Inhalt verwerfen (nach Freigabe oder Loeschen). */
function moderation_clear_reports(string $type, int $contentId): void
{
db()->prepare('DELETE FROM content_reports WHERE content_type = ? AND content_id = ?')->execute([$type, $contentId]);
}
/**
* Welche der Inhalte hat das angemeldete Konto schon gemeldet?
*
* @param array<int, int> $ids
* @return array<int, true>
*/
function moderation_my_reports(string $type, array $ids): array
{
$me = current_user();
if (!$me || empty($ids)) {
return [];
}
$placeholders = implode(',', array_fill(0, count($ids), '?'));
try {
$stmt = db()->prepare("SELECT content_id FROM content_reports WHERE content_type = ? AND reporter_id = ? AND content_id IN ($placeholders)");
$stmt->execute(array_merge([$type, (int) $me['id']], array_map('intval', $ids)));
return array_fill_keys(array_map('intval', $stmt->fetchAll(PDO::FETCH_COLUMN)), true);
} catch (PDOException $e) {
return [];
}
}
// ---- Schimpfwortfilter ----
// Was ein Filterwort bewirkt: Schluessel => Beschriftung
const MODERATION_WORD_ACTIONS = [
'flag' => 'Nur markieren',
'hold' => 'Zur Freigabe zurückhalten',
'block' => 'Blockieren',
];
// Startliste beim ersten Aufruf - danach pflegt die Moderation die Liste selbst (moderation.php).
const MODERATION_DEFAULT_WORDS = [
'hurensohn' => 'hold', 'hure' => 'hold', 'wichser' => 'hold', 'fotze' => 'hold', 'missgeburt' => 'hold',
'spast*' => 'hold', 'schwuchtel' => 'hold', 'heil hitler' => 'hold', 'fick*' => 'hold', 'arschloch' => 'hold',
'bastard' => 'hold', 'kys' => 'hold', 'kill yourself' => 'hold', 'bring dich um' => 'hold', 'behindert' => 'flag',
'nazi*' => 'flag', 'nitro' => 'flag', 'free' => 'flag', 'gratis' => 'flag', 'gewinnspiel' => 'flag',
'giveaway' => 'flag', 'kaufen' => 'flag', 'verkaufe*' => 'flag', 'cheap' => 'flag', 'boost*' => 'flag',
'steam gift' => 'flag', 'airdrop' => 'flag', 'crypto' => 'flag', 'krypto*' => 'flag',
'passwort' => 'flag', 'password' => 'flag', 'verify' => 'flag', 'verifizier*' => 'flag',
];
/**
* Die Filterwoerter (beim allerersten Aufruf mit der Startliste befuellt).
*
* @return array<string, string> Wort => Aktion
*/
function moderation_words(bool $reload = false): array
{
static $words = null;
if ($words !== null && !$reload) {
return $words;
}
try {
$words = db()->query('SELECT word, action FROM moderation_words ORDER BY word')->fetchAll(PDO::FETCH_KEY_PAIR);
if (!$words && setting_get('moderation_words_seeded') !== '1') {
$insert = db()->prepare('INSERT INTO moderation_words (word, action) VALUES (?, ?)');
foreach (MODERATION_DEFAULT_WORDS as $word => $action) {
$insert->execute([$word, $action]);
}
setting_set('moderation_words_seeded', '1');
$words = MODERATION_DEFAULT_WORDS;
ksort($words);
}
} catch (PDOException $e) {
$words = [];
}
return $words;
}
/** Text fuer den Vergleich: klein, Leetspeak aufgeloest (f1ck, @rsch), Buchstaben-Wiederholungen gekuerzt. */
function moderation_normalize(string $text): string
{
$text = mb_strtolower($text);
$text = strtr($text, ['0' => 'o', '1' => 'i', '3' => 'e', '4' => 'a', '5' => 's', '7' => 't', '@' => 'a', '$' => 's', '€' => 'e']);
return (string) preg_replace('/(\p{L})\1{2,}/u', '$1', $text); // fiiiick -> fick
}
/**
* Welche Filterwoerter kommen im Text vor? Ganze Woerter, mit * auch als Wortteil.
*
* @return array<string, string> Wort => Aktion
*/
function moderation_word_hits(string $text): array
{
if (trim($text) === '') {
return [];
}
$variants = array_unique([mb_strtolower($text), moderation_normalize($text)]);
$hits = [];
foreach (moderation_words() as $word => $action) {
$core = preg_quote(trim((string) $word, '*'), '/');
$core = str_replace(' ', '\s+', $core); // Leerzeichen im Filterwort: beliebig viel Abstand
$before = str_starts_with((string) $word, '*') ? '' : '(?<!\p{L})';
$after = str_ends_with((string) $word, '*') ? '' : '(?!\p{L})';
foreach ($variants as $variant) {
if (preg_match('/' . $before . $core . $after . '/u', $variant)) {
$hits[(string) $word] = $action;
break;
}
}
}
return $hits;
}
/** Strengste Aktion der Treffer (block > hold > flag), null ohne Treffer. */
function moderation_word_action(array $hits): ?string
{
foreach (['block', 'hold', 'flag'] as $action) {
if (in_array($action, $hits, true)) {
return $action;
}
}
return null;
}
/** Blockiert der Filter diesen Text? (Moderation ausgenommen) - dann gleich ins Log. */
function moderation_blocks(string $text, string $where): bool
{
$hits = moderation_word_hits($text);
if (moderation_can() || moderation_word_action($hits) !== 'block') {
return false;
}
admin_log('🚫 Beitrag vom Schimpfwortfilter blockiert', [
'Wo' => $where,
'Wörter' => implode(', ', array_keys(array_filter($hits, static fn ($a) => $a === 'block'))),
'Text' => mb_strimwidth($text, 0, 300, '…'),
], 'warning', 'members');
return true;
}
/**
* Markierungen fuer einen Kommentar: [Text, Stufe] mit Stufe "critical" oder "warning".
* Erwartet: status, body, report_count, author_since (Discord-Beitritt bzw. Kontoerstellung).
*
* @return array<int, array{0: string, 1: string}>
*/
function moderation_comment_flags(array $comment): array
{
$flags = [];
$body = (string) $comment['body'];
if (($comment['status'] ?? '') === 'pending') {
$flags[] = ['Wartet auf Freigabe', 'critical'];
}
$reports = (int) ($comment['report_count'] ?? 0);
if ($reports > 0) {
$flags[] = [$reports === 1 ? '1 Meldung' : $reports . ' Meldungen', 'critical'];
}
$hits = moderation_word_hits($body);
if ($hits) {
$flags[] = ['Filterwort: ' . implode(', ', array_slice(array_keys($hits), 0, 3)), 'critical'];
}
if (moderation_has_link($body)) {
$flags[] = ['Enthält Link', 'warning'];
}
$since = strtotime((string) ($comment['author_since'] ?? ''));
if ($since !== false && $since > time() - MODERATION_TRUST_DAYS * 86400) {
$flags[] = ['Neues Mitglied', 'warning'];
}
$letters = preg_replace('/[^\p{L}]/u', '', $body) ?? '';
if (mb_strlen($letters) >= 12 && mb_strtoupper($letters) === $letters) {
$flags[] = ['Nur Großbuchstaben', 'warning'];
}
if (preg_match('/(.)\1{7,}/u', $body)) {
$flags[] = ['Zeichen-Spam', 'warning'];
}
return $flags;
}