load($request, $article, $release, $path); $user = $request->user(); $copy = DeliveryLog::newCopyId(); $shown = false; foreach ($guide['entries'] as $index => $entry) { $link = fn (string $action) => SignedLink::temporary('member.media', now()->addMinutes(10), [ 'article' => $article->id, 'release' => $release->id, 'version' => $entry['version']->id, 'action' => $action, 'reader' => $user->id, ]); $guide['entries'][$index]['url'] = $entry['allowed'] && $entry['class'] !== 'text' ? $link($entry['action']) : null; $guide['entries'][$index]['download_url'] = $entry['allowed'] && $entry['may_download'] && $entry['class'] !== 'text' ? $link('laden') : null; if ($entry['allowed'] && $entry['class'] === 'text' && $entry['grant'] !== null) { app(DeliveryLog::class)->write($entry['grant'], $article, $release, $entry['version'], 'ansehen', 'text', hash('sha256', (string) $entry['version']->body), 'k2', $copy, $located['version']->id); $shown = true; } } $pdfAllowed = app(ExercisePdf::class)->sections($guide) !== null; return view('member.exercise', [ 'guide' => $guide, 'article' => $article, 'release' => $release, 'path' => $path, 'reader' => $user->name, 'copy' => $shown ? $copy : null, 'pdfUrl' => $pdfAllowed ? SignedLink::temporary('member.exercise.pdf', now()->addMinutes(10), [ 'article' => $article->id, 'release' => $release->id, 'path' => $path, 'variante' => $guide['chosen'], 'reader' => $user->id, ]) : null, 'variantUrl' => fn (string $key): string => route('member.exercise', ['article' => $article, 'release' => $release, 'path' => $path, 'variante' => $key]), ]); } public function pdf(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): Response { abort_unless((int) $request->query('reader') === $request->user()->id, 403); [$guide, $located] = $this->load($request, $article, $release, $path); $pdf = app(ExercisePdf::class); $sections = $pdf->sections($guide); abort_if($sections === null, 403, 'Für dieses PDF fehlt die Berechtigung oder ein erforderlicher Bestandteil.'); $copy = DeliveryLog::newCopyId(); $variant = collect($guide['variants'])->firstWhere('key', $guide['chosen']); $content = $pdf->render( $guide, $sections, $request->user()->name, $copy, (string) ($variant['name'] ?? ''), route('member.exercise', ['article' => $article, 'release' => $release, 'path' => $path, 'variante' => $guide['chosen']]), ); $checksum = hash('sha256', $content); foreach ($sections as $section) { $grant = $section['download_grant'] ?? $section['grant']; if ($grant !== null) { app(DeliveryLog::class)->write($grant, $article, $release, $section['version'], 'laden', 'pdf', $checksum, 'k2', $copy, $located['version']->id); } } $name = str($guide['title'])->slug()->value() ?: 'uebung'; return response($content, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="'.$name.'.pdf"', 'X-License-Copy' => $copy, 'Cache-Control' => 'private, no-store', ]); } /** * @return array{0: array, 1: array{version: CompositionVersion, selection: list|null}} */ private function load(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): array { $guides = app(ExerciseGuide::class); abort_unless($guides->mayOpen($request->user(), $article, $release), 403); $located = $guides->locate($release, $path); abort_if($located === null, 404); $variant = $request->query('variante'); return [ $guides->build($request->user(), $article, $release, $located['version'], $located['selection'], is_string($variant) ? $variant : null), $located, ]; } }