127 lines
6.3 KiB
PHP
127 lines
6.3 KiB
PHP
<?php
|
|
/**
|
|
* Musik-Steuerung der Seite musik.php (assets/js/music.js) - leitet an die Musik-API des Bots weiter
|
|
* (includes/music.php). Antwortet immer mit JSON {ok, message|error, music}.
|
|
*
|
|
* GET ?action=state Zustand (Kanal, Titel, Warteschlange, Sprachkanaele, Playlist)
|
|
* POST action=join, channel_id Bot in einen Sprachkanal holen
|
|
* POST action=leave Bot aus dem Sprachkanal schicken
|
|
* POST action=play, query, channel_id Titel-ID, YouTube-/Spotify-Link, Suche oder leer = ganze Playlist (channel_id: Kanal, falls der Bot in keinem ist)
|
|
* POST action=control, control, value toggle, skip, stop, shuffle, loop, volume (0-100), remove/move/jump (Index), seek (Sekunde), clear
|
|
* POST action=upload, file, title Datei in die Playlist (music.manage)
|
|
* POST action=delete, track_id Titel aus der Playlist (music.manage)
|
|
* Steuern: music.control, Playlist verwalten: music.manage. POST mit CSRF-Token.
|
|
*/
|
|
require_once __DIR__ . '/includes/bootstrap.php';
|
|
require_once __DIR__ . '/includes/music.php';
|
|
require_once __DIR__ . '/includes/admin_log.php';
|
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
$reply = static function (bool $ok, string $text = '', array $extra = []): void {
|
|
echo json_encode(array_merge(['ok' => $ok, $ok ? 'message' : 'error' => $text], $extra), JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
};
|
|
|
|
if (!current_user()) {
|
|
http_response_code(401);
|
|
$reply(false, 'Bitte neu anmelden.');
|
|
}
|
|
if (!user_has_permission('music.control')) {
|
|
http_response_code(403);
|
|
$reply(false, 'Dir fehlt die Berechtigung dafür.');
|
|
}
|
|
|
|
$action = (string) ($_POST['action'] ?? $_GET['action'] ?? 'state');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
verify_csrf();
|
|
} elseif ($action !== 'state') {
|
|
http_response_code(405);
|
|
$reply(false, 'Nur per POST.');
|
|
}
|
|
// Die Bot-API kann etwas dauern (Playlists) - andere Seiten derselben Sitzung sollen nicht warten
|
|
session_write_close();
|
|
|
|
$guild = '/music/' . events_guild_id();
|
|
try {
|
|
switch ($action) {
|
|
case 'state':
|
|
$reply(true, '', ['music' => music_state()]);
|
|
|
|
case 'join':
|
|
$channel = (string) ($_POST['channel_id'] ?? '');
|
|
if (!ctype_digit($channel)) {
|
|
$reply(false, 'Bitte einen Sprachkanal wählen.');
|
|
}
|
|
$data = music_api_request('POST', $guild . '/join', ['channel_id' => (int) $channel]);
|
|
$reply(true, (string) ($data['message'] ?? ''), ['music' => $data['music'] ?? null]);
|
|
|
|
case 'leave':
|
|
$data = music_api_request('POST', $guild . '/leave', []);
|
|
$reply(true, (string) ($data['message'] ?? ''), ['music' => $data['music'] ?? null]);
|
|
|
|
case 'play':
|
|
// Ist der Bot noch in keinem Sprachkanal, holt ihn der gewaehlte Kanal gleich mit
|
|
$query = mb_substr(trim((string) ($_POST['query'] ?? '')), 0, MUSIC_QUERY_MAX);
|
|
$channel = (string) ($_POST['channel_id'] ?? '');
|
|
$user = current_user();
|
|
$data = music_api_request('POST', $guild . '/play', [
|
|
'query' => $query,
|
|
'user' => (string) ($user['display_name'] !== '' ? $user['display_name'] : $user['username']),
|
|
'channel_id' => ctype_digit($channel) ? (int) $channel : null,
|
|
]);
|
|
$reply(true, (string) ($data['message'] ?? ''), ['music' => $data['music'] ?? null]);
|
|
|
|
case 'control':
|
|
$control = (string) ($_POST['control'] ?? '');
|
|
if (!in_array($control, MUSIC_ACTIONS, true)) {
|
|
$reply(false, 'Unbekannte Aktion.');
|
|
}
|
|
$value = isset($_POST['value']) && is_numeric($_POST['value']) ? (int) $_POST['value'] : null;
|
|
$data = music_api_request('POST', $guild . '/control', ['action' => $control, 'value' => $value]);
|
|
$reply(true, (string) ($data['message'] ?? ''), ['music' => $data['music'] ?? null]);
|
|
|
|
case 'upload':
|
|
if (!user_has_permission('music.manage')) {
|
|
http_response_code(403);
|
|
$reply(false, 'Titel hochladen darf nur, wer die Playlist verwaltet.');
|
|
}
|
|
$file = $_FILES['file'] ?? null;
|
|
if (!is_array($file) || ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || !is_uploaded_file($file['tmp_name'])) {
|
|
$reply(false, ($file['error'] ?? 0) === UPLOAD_ERR_INI_SIZE || ($file['error'] ?? 0) === UPLOAD_ERR_FORM_SIZE
|
|
? 'Die Datei ist zu groß (höchstens 25 MB).' : 'Bitte eine Datei auswählen.');
|
|
}
|
|
$extension = strtolower(pathinfo((string) $file['name'], PATHINFO_EXTENSION));
|
|
if (!in_array($extension, MUSIC_UPLOAD_EXTENSIONS, true)) {
|
|
$reply(false, 'Bitte eine Audiodatei: ' . implode(', ', MUSIC_UPLOAD_EXTENSIONS) . '.');
|
|
}
|
|
if ((int) $file['size'] > MUSIC_UPLOAD_MAX) {
|
|
$reply(false, 'Die Datei ist größer als 25 MB.');
|
|
}
|
|
$title = mb_substr(trim((string) ($_POST['title'] ?? '')), 0, 100);
|
|
$name = mb_substr(basename((string) $file['name']), 0, 150);
|
|
$data = music_api_request('POST', '/music/tracks', null, [$file['tmp_name'], $name, $title]);
|
|
$track = (array) ($data['track'] ?? []);
|
|
admin_log('🎵 Musik: Titel hochgeladen', ['Titel' => (string) ($track['title'] ?? $name)], 'info', 'discord');
|
|
$reply(true, '„' . ($track['title'] ?? $name) . '“ ist in der Playlist.', ['music' => music_state()]);
|
|
|
|
case 'delete':
|
|
if (!user_has_permission('music.manage')) {
|
|
http_response_code(403);
|
|
$reply(false, 'Titel löschen darf nur, wer die Playlist verwaltet.');
|
|
}
|
|
$trackId = (string) ($_POST['track_id'] ?? '');
|
|
if (!ctype_digit($trackId)) {
|
|
$reply(false, 'Unbekannter Titel.');
|
|
}
|
|
music_api_request('DELETE', '/music/tracks/' . $trackId);
|
|
admin_log('🎵 Musik: Titel gelöscht', ['Titel-Nr.' => $trackId], 'danger', 'discord');
|
|
$reply(true, 'Titel gelöscht.', ['music' => music_state()]);
|
|
|
|
default:
|
|
http_response_code(400);
|
|
$reply(false, 'Unbekannte Aktion.');
|
|
}
|
|
} catch (RuntimeException $e) {
|
|
$reply(false, $e->getMessage());
|
|
}
|