CI Feature / test (push) Successful in 2m34s
Eigener Eintrag Übungen unter Inhalte (Bestand, Anlegen, gemeinsamer Aufbau mit Varianten, Verwendung und Variantenauswahl im Modul und Programm), Verwendungsnachweis über verschachtelte und ältere Stände, Auswirkungsbericht einer Mediensperre, gesperrte Bestandteile verhindern die Angebotsaktivierung, Vorschaubilder gesperrter Medien werden nicht mehr ausgeliefert, Rückfalltest gegen die Daten des neuen Stands. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
526 lines
21 KiB
PHP
526 lines
21 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Member;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use App\Domain\Catalog\EditorialVocabulary;
|
|
use App\Domain\Catalog\IdentityDimension;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Catalog\Pillar;
|
|
use App\Domain\Catalog\PillarFilterCompat;
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Domain\Media\DeliveryLog;
|
|
use App\Domain\Media\FilmPlayback;
|
|
use App\Domain\Media\HlsPlaylist;
|
|
use App\Domain\Media\MediaRendition;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\CourseOutline;
|
|
use App\Http\LibraryCatalog;
|
|
use App\Http\MemberLicenses;
|
|
use App\Http\SignedLink;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\View\View;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class LibraryController extends Controller
|
|
{
|
|
/** Inhaltsarten der Bibliothek (Schlüssel der Adresse und des Filters). */
|
|
public const TYPES = [
|
|
'programm' => 'Programme',
|
|
'modul' => 'Module',
|
|
'film' => 'Filme',
|
|
'material' => 'Materialien',
|
|
];
|
|
|
|
public function index(Request $request, ?string $sicht = null): View
|
|
{
|
|
$catalog = app(LibraryCatalog::class)->index($request->user());
|
|
$items = [...$catalog['programs'], ...$catalog['modules'], ...$catalog['materials']];
|
|
$untouched = $items === [];
|
|
$query = trim((string) $request->query('q', ''));
|
|
$nutzung = (string) $request->query('nutzung', '');
|
|
$sortierung = $request->query('sortierung') === 'titel' ? 'titel' : 'standard';
|
|
$art = match ($sicht) {
|
|
'programme' => 'programm',
|
|
'module' => 'modul',
|
|
default => (string) $request->query('art', ''),
|
|
};
|
|
$art = array_key_exists($art, self::TYPES) ? $art : '';
|
|
$dimensionIds = array_values(array_filter(array_map(intval(...), (array) $request->query('dimension', []))));
|
|
$saeuleIds = array_values(array_filter(array_map(intval(...), (array) $request->query('saeule', []))));
|
|
$dimensions = app(IdentityDimension::class);
|
|
|
|
// Alte Adressen: Säulen waren ein Wert der Einordnung. Mit belastbarer Verknüpfung gilt der Filter der Säule.
|
|
foreach ($dimensionIds as $index => $valueId) {
|
|
if (is_int($pillarId = PillarFilterCompat::pillarOfLegacyValue($valueId))) {
|
|
$saeuleIds[] = $pillarId;
|
|
unset($dimensionIds[$index]);
|
|
}
|
|
}
|
|
|
|
$dimensionIds = array_values($dimensionIds);
|
|
$saeuleIds = array_values(array_unique($saeuleIds));
|
|
$pillarOptions = $this->visiblePillars($items, $saeuleIds);
|
|
|
|
// Gemeinsame Filter für alle Inhaltsarten. Die Zahl je Art zeigt, was die übrigen Filter übrig lassen.
|
|
$matching = array_values(array_filter($items, function (array $card) use ($query, $nutzung, $dimensionIds, $saeuleIds, $dimensions): bool {
|
|
return ($query === '' || $this->matches($card, $query))
|
|
&& ($dimensionIds === [] || $dimensions->matches($card['subject'], $dimensionIds))
|
|
&& ($saeuleIds === [] || $this->hasPillar($card['subject'], $saeuleIds))
|
|
&& (! in_array($nutzung, ['nutzbar', 'teilweise', 'nicht enthalten'], true) || ($card['coverage'] ?? '') === $nutzung);
|
|
}));
|
|
$counts = array_fill_keys(array_keys(self::TYPES), 0);
|
|
|
|
foreach ($matching as $card) {
|
|
$counts[$card['type']]++;
|
|
}
|
|
|
|
$shown = $art === '' ? $matching : array_values(array_filter($matching, fn (array $card): bool => $card['type'] === $art));
|
|
|
|
if ($sortierung === 'titel') {
|
|
usort($shown, fn (array $left, array $right): int => strcoll(mb_strtolower($left['title']), mb_strtolower($right['title'])));
|
|
}
|
|
|
|
$shelves = [];
|
|
|
|
foreach (self::TYPES as $type => $label) {
|
|
$cards = array_values(array_filter($shown, fn (array $card): bool => $card['type'] === $type));
|
|
|
|
if ($cards !== []) {
|
|
$shelves[$type] = ['label' => $label, 'cards' => $cards];
|
|
}
|
|
}
|
|
|
|
$filterActive = $query !== '' || $nutzung !== '' || $dimensionIds !== [] || $saeuleIds !== [] || $art !== '';
|
|
|
|
return view('member.library', [
|
|
'shelves' => $shelves,
|
|
'total' => count($shown),
|
|
'counts' => $counts,
|
|
'allCount' => count($matching),
|
|
'art' => $art,
|
|
'types' => self::TYPES,
|
|
'query' => $query,
|
|
'sicht' => $sicht,
|
|
'nutzung' => $nutzung,
|
|
'sortierung' => $sortierung,
|
|
'dimensionIds' => $dimensionIds,
|
|
'saeuleIds' => $saeuleIds,
|
|
'filterPillars' => $pillarOptions,
|
|
'filterDimensions' => app(EditorialVocabulary::class)->memberFilterDimensions(array_map(intval(...), $dimensionIds)),
|
|
'libraryEmpty' => $untouched,
|
|
'filterActive' => $filterActive,
|
|
'filterCount' => count($dimensionIds) + count($saeuleIds) + ($nutzung !== '' ? 1 : 0),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Direkte Säulenzuordnung des Objekts selbst; die Säulen enthaltener Medien zählen hier nicht.
|
|
*
|
|
* @param list<int> $pillarIds
|
|
*/
|
|
private function hasPillar(Model $subject, array $pillarIds): bool
|
|
{
|
|
return $subject->pillars()->whereIn('pillars.id', $pillarIds)->exists();
|
|
}
|
|
|
|
/**
|
|
* Säulen für den Filter: nur solche, die ein sichtbares Objekt der Bibliothek tatsächlich trägt, und eine bereits
|
|
* gewählte. So erscheinen neue oder interne Säulen ohne Zuordnung nicht in der Bibliothek.
|
|
*
|
|
* @param list<array<string, mixed>> $items
|
|
* @param list<int> $selected
|
|
* @return Collection<int, Pillar>
|
|
*/
|
|
private function visiblePillars(array $items, array $selected)
|
|
{
|
|
$used = [];
|
|
|
|
foreach ($items as $card) {
|
|
foreach ($card['subject']->pillars()->pluck('pillars.id') as $id) {
|
|
$used[(int) $id] = true;
|
|
}
|
|
}
|
|
|
|
return Pillar::query()->whereIn('id', array_values(array_unique([...array_keys($used), ...$selected])))
|
|
->orderBy('sort_order')->orderBy('name')->get();
|
|
}
|
|
|
|
public function programs(Request $request): View
|
|
{
|
|
return $this->index($request, 'programme');
|
|
}
|
|
|
|
public function modules(Request $request): View
|
|
{
|
|
return $this->index($request, 'module');
|
|
}
|
|
|
|
public function program(Request $request, Composition $composition): View
|
|
{
|
|
return view('member.program', app(LibraryCatalog::class)->program($request->user(), $composition));
|
|
}
|
|
|
|
/**
|
|
* Film oder Material im Zusammenhang seines Programms oder Moduls. Die Seite zeigt nur Stellen, die in der
|
|
* angezeigten Fassung des Programms stehen; die Wiedergabe- und Downloadadressen kommen aus DecideAccess.
|
|
*/
|
|
public function content(Request $request, Composition $composition, int $version): View|Response
|
|
{
|
|
$data = app(LibraryCatalog::class)->content($request->user(), $composition, $version);
|
|
abort_if($data === null, 404);
|
|
|
|
return response()->view('member.content', $data, $data['allowed'] ? 200 : 403);
|
|
}
|
|
|
|
/** Die frühere Seite „Materialien“ ist ein Filter der Bibliothek (eine Navigation statt zwei). */
|
|
public function materials(): RedirectResponse
|
|
{
|
|
return redirect()->route('member.library', ['art' => 'material']);
|
|
}
|
|
|
|
public function grants(Request $request): View
|
|
{
|
|
$entries = app(MemberLicenses::class)->overview($request->user());
|
|
$filter = in_array($request->query('status'), ['aktiv', 'inaktiv'], true) ? (string) $request->query('status') : 'alle';
|
|
|
|
return view('member.grants', [
|
|
'entries' => match ($filter) {
|
|
'aktiv' => $entries->where('active', true)->values(),
|
|
'inaktiv' => $entries->where('active', false)->values(),
|
|
default => $entries,
|
|
},
|
|
'filter' => $filter,
|
|
'counts' => [
|
|
'aktiv' => $entries->where('active', true)->count(),
|
|
'inaktiv' => $entries->where('active', false)->count(),
|
|
'alle' => $entries->count(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/** Eine einzelne Berechtigung. Nur eigene (oder die der eigenen Organisation mit Bestellrecht); sonst nicht gefunden. */
|
|
public function grant(Request $request, Grant $grant): View
|
|
{
|
|
abort_unless(
|
|
Grant::query()->usableBy($request->user())->whereKey($grant->id)->exists(),
|
|
404,
|
|
);
|
|
|
|
return view('member.grant', app(MemberLicenses::class)->detail($request->user(), $grant));
|
|
}
|
|
|
|
public function show(Request $request, LicenseArticle $article): View
|
|
{
|
|
$release = $article->currentRelease()->with('contents.elementVersion.element.pillars')->first();
|
|
$entries = [];
|
|
|
|
if ($release !== null) {
|
|
foreach ($release->contents as $content) {
|
|
$version = $content->elementVersion;
|
|
$class = $version->element->output_class;
|
|
$metadata = is_array($version->metadata) ? $version->metadata : [];
|
|
$action = $class === 'film' || $class === 'audio' ? 'streamen' : 'ansehen';
|
|
$decision = app(DecideAccess::class)($request->user(), $article, $release, $version, $action);
|
|
$download = app(DecideAccess::class)($request->user(), $article, $release, $version, 'laden');
|
|
$file = $metadata['file'] ?? null;
|
|
$entries[] = [
|
|
'title' => app(LibraryCatalog::class)->text($version->element)['title'],
|
|
'class' => $class,
|
|
'content' => $content,
|
|
'version' => $version,
|
|
'role' => $content->role,
|
|
'action' => $action,
|
|
'allowed' => $decision->allowed,
|
|
'reason' => $decision->reason,
|
|
'decision' => is_numeric($metadata['decision_seconds'] ?? null)
|
|
? (int) $metadata['decision_seconds']
|
|
: null,
|
|
'playable' => $decision->allowed && is_string($file) && $file !== '' && ($class === 'film' || $class === 'audio'),
|
|
'playback' => app(FilmPlayback::class)->mode($version->id),
|
|
'download_url' => $download->allowed
|
|
? SignedLink::temporary('member.media', now()->addMinutes(10), [
|
|
'article' => $article->id,
|
|
'release' => $release->id,
|
|
'version' => $version->id,
|
|
'action' => 'laden',
|
|
'reader' => $request->user()->id,
|
|
])
|
|
: null,
|
|
'pillars' => $version->element->pillars->pluck('name')->all(),
|
|
'url' => $decision->allowed
|
|
? SignedLink::temporary('member.media', now()->addMinutes(10), [
|
|
'article' => $article->id,
|
|
'release' => $release->id,
|
|
'version' => $version->id,
|
|
'action' => $action,
|
|
'reader' => $request->user()->id,
|
|
])
|
|
: null,
|
|
];
|
|
}
|
|
}
|
|
|
|
return view('member.article', [
|
|
'article' => $article,
|
|
'release' => $release,
|
|
'entries' => $entries,
|
|
'title' => app(CourseOutline::class)->title($article),
|
|
'kind' => app(CourseOutline::class)->kindLabel($article),
|
|
]);
|
|
}
|
|
|
|
public function media(
|
|
Request $request,
|
|
LicenseArticle $article,
|
|
ArticleRelease $release,
|
|
ContentElementVersion $version,
|
|
string $action,
|
|
): View|StreamedResponse|Response {
|
|
abort_unless(in_array($action, ['ansehen', 'streamen', 'laden'], true), 404);
|
|
abort_unless((int) $request->query('reader') === $request->user()->id, 403);
|
|
|
|
$decision = app(DecideAccess::class)($request->user(), $article, $release, $version, $action);
|
|
abort_unless($decision->allowed && $decision->grant !== null, 403, $decision->reason);
|
|
|
|
$version->loadMissing('element');
|
|
$master = $action === 'streamen' ? MediaRendition::masterPath($version->id) : null;
|
|
|
|
if (is_string($master)) {
|
|
return $this->playlist($request, $article, $release, $version, $decision->grant, $master);
|
|
}
|
|
|
|
$file = $version->metadata['file'] ?? null;
|
|
|
|
if (! is_string($file) || $file === '' || str_contains($file, '..')) {
|
|
$delivery = app(DeliveryLog::class)->write(
|
|
$decision->grant,
|
|
$article,
|
|
$release,
|
|
$version,
|
|
$action,
|
|
'text',
|
|
hash('sha256', (string) $version->body),
|
|
'k2',
|
|
);
|
|
|
|
return view('member.read', [
|
|
'article' => $article,
|
|
'version' => $version,
|
|
'license' => $decision->grant->rights_snapshot['purpose'] ?? null,
|
|
'reader' => $request->user()->name,
|
|
'copy' => $delivery->copy_id,
|
|
]);
|
|
}
|
|
|
|
return $this->fileResponse($decision->grant, $article, $release, $version, $action, $file, 'bestand');
|
|
}
|
|
|
|
public function rendition(
|
|
Request $request,
|
|
LicenseArticle $article,
|
|
ArticleRelease $release,
|
|
ContentElementVersion $version,
|
|
string $datei,
|
|
): Response|StreamedResponse {
|
|
abort_unless((int) $request->query('reader') === $request->user()->id, 403);
|
|
abort_unless($this->renditionBelongsTo($version->id, $datei), 404);
|
|
|
|
$decision = app(DecideAccess::class)($request->user(), $article, $release, $version, 'streamen');
|
|
abort_unless($decision->allowed && $decision->grant !== null, 403, $decision->reason);
|
|
|
|
if (str_ends_with($datei, '.m3u8')) {
|
|
return $this->playlist(
|
|
$request,
|
|
$article,
|
|
$release,
|
|
$version,
|
|
$decision->grant,
|
|
$datei,
|
|
str_ends_with($datei, 'master.m3u8'),
|
|
);
|
|
}
|
|
|
|
$disk = Storage::disk('media_derived');
|
|
abort_unless($disk->exists($datei), 404);
|
|
|
|
return $disk->response($datei, basename($datei), ['Content-Disposition' => 'inline']);
|
|
}
|
|
|
|
public function thumbnail(Request $request, string $kind, int $subject): StreamedResponse
|
|
{
|
|
abort_unless(in_array($kind, ['komposition', 'element'], true), 404);
|
|
$model = $kind === 'komposition'
|
|
? Composition::query()->findOrFail($subject)
|
|
: ContentElement::query()->findOrFail($subject);
|
|
$path = $model->thumbnail_path;
|
|
abort_unless(is_string($path) && str_starts_with($path, 'vorschau/') && ! str_contains($path, '..'), 404);
|
|
abort_unless($this->maySeeThumbnail($request, $kind, $subject), 403);
|
|
$disk = Storage::disk('media_derived');
|
|
abort_unless($disk->exists($path), 404);
|
|
|
|
return $disk->response($path);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $card
|
|
*/
|
|
private function matches(array $card, string $query): bool
|
|
{
|
|
$haystack = mb_strtolower(($card['title'] ?? '').' '.($card['summary'] ?? '').' '.($card['subject']->search_terms ?? ''));
|
|
|
|
return str_contains($haystack, mb_strtolower($query));
|
|
}
|
|
|
|
private function playlist(
|
|
Request $request,
|
|
LicenseArticle $article,
|
|
ArticleRelease $release,
|
|
ContentElementVersion $version,
|
|
Grant $grant,
|
|
string $path,
|
|
bool $record = true,
|
|
): Response {
|
|
abort_unless($this->renditionBelongsTo($version->id, $path), 404);
|
|
$disk = Storage::disk('media_derived');
|
|
abort_unless($disk->exists($path), 404);
|
|
$rewritten = app(HlsPlaylist::class)->rewrite(
|
|
$disk->get($path),
|
|
$path,
|
|
fn (string $target): string => SignedLink::temporary('member.rendition', now()->addMinutes(10), [
|
|
'article' => $article->id,
|
|
'release' => $release->id,
|
|
'version' => $version->id,
|
|
'datei' => $target,
|
|
'reader' => $request->user()->id,
|
|
]),
|
|
);
|
|
$copy = '';
|
|
|
|
if ($record) {
|
|
$delivery = app(DeliveryLog::class)->write(
|
|
$grant,
|
|
$article,
|
|
$release,
|
|
$version,
|
|
'streamen',
|
|
'hls',
|
|
hash('sha256', $rewritten),
|
|
'k0',
|
|
);
|
|
$copy = $delivery->copy_id;
|
|
}
|
|
|
|
return response($rewritten, 200, [
|
|
'Content-Type' => 'application/vnd.apple.mpegurl',
|
|
'X-License-Copy' => $copy,
|
|
]);
|
|
}
|
|
|
|
private function fileResponse(
|
|
Grant $grant,
|
|
LicenseArticle $article,
|
|
ArticleRelease $release,
|
|
ContentElementVersion $version,
|
|
string $action,
|
|
string $file,
|
|
string $format,
|
|
): StreamedResponse {
|
|
$disk = Storage::disk('media_derived');
|
|
abort_unless($disk->exists($file) && ! str_contains($file, '..'), 404);
|
|
$delivery = app(DeliveryLog::class)->write(
|
|
$grant,
|
|
$article,
|
|
$release,
|
|
$version,
|
|
$action,
|
|
$format,
|
|
hash_file('sha256', $disk->path($file)) ?: null,
|
|
'k0',
|
|
);
|
|
|
|
// Dateiname aus dem Anzeigetitel statt aus der Adresse („laden.mp4“); Laravel setzt Content-Disposition mit
|
|
// ASCII-Rückfall und UTF-8-Name.
|
|
return $disk->response(
|
|
$file,
|
|
$this->downloadName($version, $file),
|
|
['X-License-Copy' => $delivery->copy_id],
|
|
$action === 'laden' ? 'attachment' : 'inline',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Dateiname für Ansicht und Download: Anzeigetitel des Mediums (Mitgliedertext, sonst Arbeitstitel) mit der
|
|
* Endung der gespeicherten Datei. Zeichen, die in Dateinamen nicht erlaubt sind, entfallen.
|
|
*/
|
|
private function downloadName(ContentElementVersion $version, string $file): string
|
|
{
|
|
$element = $version->element;
|
|
$title = $element === null ? '' : app(LibraryCatalog::class)->text($element)['title'];
|
|
$title = (string) preg_replace('/[\x00-\x1F\x7F\/\\\\:*?"<>|]+/u', ' ', $title);
|
|
$title = trim((string) preg_replace('/\s+/u', ' ', $title));
|
|
$title = rtrim(mb_substr($title, 0, 120), '. ');
|
|
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
|
|
|
if ($title === '') {
|
|
return basename($file);
|
|
}
|
|
|
|
return $extension === '' ? $title : $title.'.'.$extension;
|
|
}
|
|
|
|
private function renditionBelongsTo(int $versionId, string $path): bool
|
|
{
|
|
$prefix = 'ableitungen/'.$versionId;
|
|
|
|
return ($path === $prefix || str_starts_with($path, $prefix.'/')) && ! str_contains($path, '..');
|
|
}
|
|
|
|
private function maySeeThumbnail(Request $request, string $kind, int $subject): bool
|
|
{
|
|
$grants = $this->grantQuery($request)->with('article.currentRelease.contents.elementVersion', 'article.currentRelease.rootCompositionVersion')->get();
|
|
|
|
foreach ($grants as $grant) {
|
|
$release = $grant->article->currentRelease;
|
|
|
|
if ($release === null) {
|
|
continue;
|
|
}
|
|
|
|
if ($kind === 'komposition' && $release->rootCompositionVersion?->composition_id === $subject) {
|
|
return true;
|
|
}
|
|
|
|
// Ein gesperrtes Medium zeigt auch kein Vorschaubild mehr (Sperre gilt für Ausgaben und Ableitungen).
|
|
if ($kind === 'element' && $release->contents->contains(
|
|
fn ($content): bool => $content->elementVersion?->content_element_id === $subject
|
|
&& $content->elementVersion->status !== ContentVersionStatus::Gesperrt,
|
|
)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function grantQuery(Request $request)
|
|
{
|
|
return Grant::query()
|
|
->where('status', GrantStatus::Aktiv)
|
|
->usableBy($request->user())
|
|
->with('article.currentRelease')
|
|
->orderBy('id');
|
|
}
|
|
}
|