]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/PageController.php
Pages: Redirect user to view if they can't edit
[bookstack] / app / Entities / Controllers / PageController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\CommentTree;
7 use BookStack\Activity\Tools\UserEntityWatchOptions;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Chapter;
10 use BookStack\Entities\Queries\EntityQueries;
11 use BookStack\Entities\Queries\PageQueries;
12 use BookStack\Entities\Repos\PageRepo;
13 use BookStack\Entities\Tools\BookContents;
14 use BookStack\Entities\Tools\Cloner;
15 use BookStack\Entities\Tools\NextPreviousContentLocator;
16 use BookStack\Entities\Tools\PageContent;
17 use BookStack\Entities\Tools\PageEditActivity;
18 use BookStack\Entities\Tools\PageEditorData;
19 use BookStack\Exceptions\NotFoundException;
20 use BookStack\Exceptions\NotifyException;
21 use BookStack\Exceptions\PermissionsException;
22 use BookStack\Http\Controller;
23 use BookStack\References\ReferenceFetcher;
24 use Exception;
25 use Illuminate\Database\Eloquent\Relations\BelongsTo;
26 use Illuminate\Http\Request;
27 use Illuminate\Validation\ValidationException;
28 use Throwable;
29
30 class PageController extends Controller
31 {
32     public function __construct(
33         protected PageRepo $pageRepo,
34         protected PageQueries $queries,
35         protected EntityQueries $entityQueries,
36         protected ReferenceFetcher $referenceFetcher
37     ) {
38     }
39
40     /**
41      * Show the form for creating a new page.
42      *
43      * @throws Throwable
44      */
45     public function create(string $bookSlug, ?string $chapterSlug = null)
46     {
47         if ($chapterSlug) {
48             $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
49         } else {
50             $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
51         }
52
53         $this->checkOwnablePermission('page-create', $parent);
54
55         // Redirect to draft edit screen if signed in
56         if ($this->isSignedIn()) {
57             $draft = $this->pageRepo->getNewDraftPage($parent);
58
59             return redirect($draft->getUrl());
60         }
61
62         // Otherwise show the edit view if they're a guest
63         $this->setPageTitle(trans('entities.pages_new'));
64
65         return view('pages.guest-create', ['parent' => $parent]);
66     }
67
68     /**
69      * Create a new page as a guest user.
70      *
71      * @throws ValidationException
72      */
73     public function createAsGuest(Request $request, string $bookSlug, ?string $chapterSlug = null)
74     {
75         $this->validate($request, [
76             'name' => ['required', 'string', 'max:255'],
77         ]);
78
79         if ($chapterSlug) {
80             $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
81         } else {
82             $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
83         }
84
85         $this->checkOwnablePermission('page-create', $parent);
86
87         $page = $this->pageRepo->getNewDraftPage($parent);
88         $this->pageRepo->publishDraft($page, [
89             'name' => $request->get('name'),
90         ]);
91
92         return redirect($page->getUrl('/edit'));
93     }
94
95     /**
96      * Show form to continue editing a draft page.
97      *
98      * @throws NotFoundException
99      */
100     public function editDraft(Request $request, string $bookSlug, int $pageId)
101     {
102         $draft = $this->queries->findVisibleByIdOrFail($pageId);
103         $this->checkOwnablePermission('page-create', $draft->getParent());
104
105         $editorData = new PageEditorData($draft, $this->entityQueries, $request->query('editor', ''));
106         $this->setPageTitle(trans('entities.pages_edit_draft'));
107
108         return view('pages.edit', $editorData->getViewData());
109     }
110
111     /**
112      * Store a new page by changing a draft into a page.
113      *
114      * @throws NotFoundException
115      * @throws ValidationException
116      */
117     public function store(Request $request, string $bookSlug, int $pageId)
118     {
119         $this->validate($request, [
120             'name' => ['required', 'string', 'max:255'],
121         ]);
122         $draftPage = $this->queries->findVisibleByIdOrFail($pageId);
123         $this->checkOwnablePermission('page-create', $draftPage->getParent());
124
125         $page = $this->pageRepo->publishDraft($draftPage, $request->all());
126
127         return redirect($page->getUrl());
128     }
129
130     /**
131      * Display the specified page.
132      * If the page is not found via the slug the revisions are searched for a match.
133      *
134      * @throws NotFoundException
135      */
136     public function show(string $bookSlug, string $pageSlug)
137     {
138         try {
139             $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
140         } catch (NotFoundException $e) {
141             $revision = $this->entityQueries->revisions->findLatestVersionBySlugs($bookSlug, $pageSlug);
142             $page = $revision->page ?? null;
143
144             if (is_null($page)) {
145                 throw $e;
146             }
147
148             return redirect($page->getUrl());
149         }
150
151         $this->checkOwnablePermission('page-view', $page);
152
153         $pageContent = (new PageContent($page));
154         $page->html = $pageContent->render();
155         $pageNav = $pageContent->getNavigation($page->html);
156
157         $sidebarTree = (new BookContents($page->book))->getTree();
158         $commentTree = (new CommentTree($page));
159         $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
160
161         View::incrementFor($page);
162         $this->setPageTitle($page->getShortName());
163
164         return view('pages.show', [
165             'page'            => $page,
166             'book'            => $page->book,
167             'current'         => $page,
168             'sidebarTree'     => $sidebarTree,
169             'commentTree'     => $commentTree,
170             'pageNav'         => $pageNav,
171             'watchOptions'    => new UserEntityWatchOptions(user(), $page),
172             'next'            => $nextPreviousLocator->getNext(),
173             'previous'        => $nextPreviousLocator->getPrevious(),
174             'referenceCount'  => $this->referenceFetcher->getReferenceCountToEntity($page),
175         ]);
176     }
177
178     /**
179      * Get page from an ajax request.
180      *
181      * @throws NotFoundException
182      */
183     public function getPageAjax(int $pageId)
184     {
185         $page = $this->queries->findVisibleByIdOrFail($pageId);
186         $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
187         $page->makeHidden(['book']);
188
189         return response()->json($page);
190     }
191
192     /**
193      * Show the form for editing the specified page.
194      *
195      * @throws NotFoundException
196      */
197     public function edit(Request $request, string $bookSlug, string $pageSlug)
198     {
199         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
200         $this->checkOwnablePermission('page-update', $page, $page->getUrl());
201
202         $editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
203         if ($editorData->getWarnings()) {
204             $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
205         }
206
207         $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
208
209         return view('pages.edit', $editorData->getViewData());
210     }
211
212     /**
213      * Update the specified page in storage.
214      *
215      * @throws ValidationException
216      * @throws NotFoundException
217      */
218     public function update(Request $request, string $bookSlug, string $pageSlug)
219     {
220         $this->validate($request, [
221             'name' => ['required', 'string', 'max:255'],
222         ]);
223         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
224         $this->checkOwnablePermission('page-update', $page);
225
226         $this->pageRepo->update($page, $request->all());
227
228         return redirect($page->getUrl());
229     }
230
231     /**
232      * Save a draft update as a revision.
233      *
234      * @throws NotFoundException
235      */
236     public function saveDraft(Request $request, int $pageId)
237     {
238         $page = $this->queries->findVisibleByIdOrFail($pageId);
239         $this->checkOwnablePermission('page-update', $page);
240
241         if (!$this->isSignedIn()) {
242             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
243         }
244
245         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
246         $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
247
248         return response()->json([
249             'status'    => 'success',
250             'message'   => trans('entities.pages_edit_draft_save_at'),
251             'warning'   => implode("\n", $warnings),
252             'timestamp' => $draft->updated_at->timestamp,
253         ]);
254     }
255
256     /**
257      * Redirect from a special link url which uses the page id rather than the name.
258      *
259      * @throws NotFoundException
260      */
261     public function redirectFromLink(int $pageId)
262     {
263         $page = $this->queries->findVisibleByIdOrFail($pageId);
264
265         return redirect($page->getUrl());
266     }
267
268     /**
269      * Show the deletion page for the specified page.
270      *
271      * @throws NotFoundException
272      */
273     public function showDelete(string $bookSlug, string $pageSlug)
274     {
275         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
276         $this->checkOwnablePermission('page-delete', $page);
277         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
278         $usedAsTemplate =
279             $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
280             $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
281
282         return view('pages.delete', [
283             'book'    => $page->book,
284             'page'    => $page,
285             'current' => $page,
286             'usedAsTemplate' => $usedAsTemplate,
287         ]);
288     }
289
290     /**
291      * Show the deletion page for the specified page.
292      *
293      * @throws NotFoundException
294      */
295     public function showDeleteDraft(string $bookSlug, int $pageId)
296     {
297         $page = $this->queries->findVisibleByIdOrFail($pageId);
298         $this->checkOwnablePermission('page-update', $page);
299         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
300         $usedAsTemplate =
301             $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
302             $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
303
304         return view('pages.delete', [
305             'book'    => $page->book,
306             'page'    => $page,
307             'current' => $page,
308             'usedAsTemplate' => $usedAsTemplate,
309         ]);
310     }
311
312     /**
313      * Remove the specified page from storage.
314      *
315      * @throws NotFoundException
316      * @throws Throwable
317      */
318     public function destroy(string $bookSlug, string $pageSlug)
319     {
320         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
321         $this->checkOwnablePermission('page-delete', $page);
322         $parent = $page->getParent();
323
324         $this->pageRepo->destroy($page);
325
326         return redirect($parent->getUrl());
327     }
328
329     /**
330      * Remove the specified draft page from storage.
331      *
332      * @throws NotFoundException
333      * @throws Throwable
334      */
335     public function destroyDraft(string $bookSlug, int $pageId)
336     {
337         $page = $this->queries->findVisibleByIdOrFail($pageId);
338         $book = $page->book;
339         $chapter = $page->chapter;
340         $this->checkOwnablePermission('page-update', $page);
341
342         $this->pageRepo->destroy($page);
343
344         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
345
346         if ($chapter && userCan('view', $chapter)) {
347             return redirect($chapter->getUrl());
348         }
349
350         return redirect($book->getUrl());
351     }
352
353     /**
354      * Show a listing of recently created pages.
355      */
356     public function showRecentlyUpdated()
357     {
358         $visibleBelongsScope = function (BelongsTo $query) {
359             $query->scopes('visible');
360         };
361
362         $pages = $this->queries->visibleForList()
363             ->addSelect('updated_by')
364             ->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
365             ->orderBy('updated_at', 'desc')
366             ->paginate(20)
367             ->setPath(url('/pages/recently-updated'));
368
369         $this->setPageTitle(trans('entities.recently_updated_pages'));
370
371         return view('common.detailed-listing-paginated', [
372             'title'         => trans('entities.recently_updated_pages'),
373             'entities'      => $pages,
374             'showUpdatedBy' => true,
375             'showPath'      => true,
376         ]);
377     }
378
379     /**
380      * Show the view to choose a new parent to move a page into.
381      *
382      * @throws NotFoundException
383      */
384     public function showMove(string $bookSlug, string $pageSlug)
385     {
386         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
387         $this->checkOwnablePermission('page-update', $page);
388         $this->checkOwnablePermission('page-delete', $page);
389
390         return view('pages.move', [
391             'book' => $page->book,
392             'page' => $page,
393         ]);
394     }
395
396     /**
397      * Does the action of moving the location of a page.
398      *
399      * @throws NotFoundException
400      * @throws Throwable
401      */
402     public function move(Request $request, string $bookSlug, string $pageSlug)
403     {
404         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
405         $this->checkOwnablePermission('page-update', $page);
406         $this->checkOwnablePermission('page-delete', $page);
407
408         $entitySelection = $request->get('entity_selection', null);
409         if ($entitySelection === null || $entitySelection === '') {
410             return redirect($page->getUrl());
411         }
412
413         try {
414             $this->pageRepo->move($page, $entitySelection);
415         } catch (PermissionsException $exception) {
416             $this->showPermissionError();
417         } catch (Exception $exception) {
418             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
419
420             return redirect($page->getUrl('/move'));
421         }
422
423         return redirect($page->getUrl());
424     }
425
426     /**
427      * Show the view to copy a page.
428      *
429      * @throws NotFoundException
430      */
431     public function showCopy(string $bookSlug, string $pageSlug)
432     {
433         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
434         $this->checkOwnablePermission('page-view', $page);
435         session()->flashInput(['name' => $page->name]);
436
437         return view('pages.copy', [
438             'book' => $page->book,
439             'page' => $page,
440         ]);
441     }
442
443     /**
444      * Create a copy of a page within the requested target destination.
445      *
446      * @throws NotFoundException
447      * @throws Throwable
448      */
449     public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
450     {
451         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
452         $this->checkOwnablePermission('page-view', $page);
453
454         $entitySelection = $request->get('entity_selection') ?: null;
455         $newParent = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $page->getParent();
456
457         if (!$newParent instanceof Book && !$newParent instanceof Chapter) {
458             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
459
460             return redirect($page->getUrl('/copy'));
461         }
462
463         $this->checkOwnablePermission('page-create', $newParent);
464
465         $newName = $request->get('name') ?: $page->name;
466         $pageCopy = $cloner->clonePage($page, $newParent, $newName);
467         $this->showSuccessNotification(trans('entities.pages_copy_success'));
468
469         return redirect($pageCopy->getUrl());
470     }
471 }