]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
add missing @param in method comment
[bookstack] / app / Http / Controllers / PageController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Repos\UserRepo;
7 use BookStack\Services\ExportService;
8 use Illuminate\Http\Request;
9 use Illuminate\Http\Response;
10 use Views;
11 use GatherContent\Htmldiff\Htmldiff;
12
13 class PageController extends Controller
14 {
15
16     protected $entityRepo;
17     protected $exportService;
18     protected $userRepo;
19
20     /**
21      * PageController constructor.
22      * @param EntityRepo $entityRepo
23      * @param ExportService $exportService
24      * @param UserRepo $userRepo
25      */
26     public function __construct(EntityRepo $entityRepo, ExportService $exportService, UserRepo $userRepo)
27     {
28         $this->entityRepo = $entityRepo;
29         $this->exportService = $exportService;
30         $this->userRepo = $userRepo;
31         parent::__construct();
32     }
33
34     /**
35      * Show the form for creating a new page.
36      * @param string $bookSlug
37      * @param string $chapterSlug
38      * @return Response
39      * @internal param bool $pageSlug
40      * @throws NotFoundException
41      */
42     public function create($bookSlug, $chapterSlug = null)
43     {
44         if ($chapterSlug !== null) {
45             $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
46             $book = $chapter->book;
47         } else {
48             $chapter = null;
49             $book = $this->entityRepo->getBySlug('book', $bookSlug);
50         }
51
52         $parent = $chapter ? $chapter : $book;
53         $this->checkOwnablePermission('page-create', $parent);
54
55         // Redirect to draft edit screen if signed in
56         if ($this->signedIn) {
57             $draft = $this->entityRepo->getDraftPage($book, $chapter);
58             return redirect($draft->getUrl());
59         }
60
61         // Otherwise show the edit view if they're a guest
62         $this->setPageTitle(trans('entities.pages_new'));
63         return view('pages/guest-create', ['parent' => $parent]);
64     }
65
66     /**
67      * Create a new page as a guest user.
68      * @param Request $request
69      * @param string $bookSlug
70      * @param string|null $chapterSlug
71      * @return mixed
72      * @throws NotFoundException
73      */
74     public function createAsGuest(Request $request, $bookSlug, $chapterSlug = null)
75     {
76         $this->validate($request, [
77             'name' => 'required|string|max:255'
78         ]);
79
80         if ($chapterSlug !== null) {
81             $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
82             $book = $chapter->book;
83         } else {
84             $chapter = null;
85             $book = $this->entityRepo->getBySlug('book', $bookSlug);
86         }
87
88         $parent = $chapter ? $chapter : $book;
89         $this->checkOwnablePermission('page-create', $parent);
90
91         $page = $this->entityRepo->getDraftPage($book, $chapter);
92         $this->entityRepo->publishPageDraft($page, [
93             'name' => $request->get('name'),
94             'html' => ''
95         ]);
96         return redirect($page->getUrl('/edit'));
97     }
98
99     /**
100      * Show form to continue editing a draft page.
101      * @param string $bookSlug
102      * @param int $pageId
103      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
104      */
105     public function editDraft($bookSlug, $pageId)
106     {
107         $draft = $this->entityRepo->getById('page', $pageId, true);
108         $this->checkOwnablePermission('page-create', $draft->parent);
109         $this->setPageTitle(trans('entities.pages_edit_draft'));
110
111         $draftsEnabled = $this->signedIn;
112         return view('pages/edit', [
113             'page' => $draft,
114             'book' => $draft->book,
115             'isDraft' => true,
116             'draftsEnabled' => $draftsEnabled
117         ]);
118     }
119
120     /**
121      * Store a new page by changing a draft into a page.
122      * @param  Request $request
123      * @param  string $bookSlug
124      * @param  int $pageId
125      * @return Response
126      */
127     public function store(Request $request, $bookSlug, $pageId)
128     {
129         $this->validate($request, [
130             'name' => 'required|string|max:255'
131         ]);
132
133         $input = $request->all();
134         $draftPage = $this->entityRepo->getById('page', $pageId, true);
135         $book = $draftPage->book;
136
137         $parent = $draftPage->parent;
138         $this->checkOwnablePermission('page-create', $parent);
139
140         if ($parent->isA('chapter')) {
141             $input['priority'] = $this->entityRepo->getNewChapterPriority($parent);
142         } else {
143             $input['priority'] = $this->entityRepo->getNewBookPriority($parent);
144         }
145
146         $page = $this->entityRepo->publishPageDraft($draftPage, $input);
147
148         Activity::add($page, 'page_create', $book->id);
149         return redirect($page->getUrl());
150     }
151
152     /**
153      * Display the specified page.
154      * If the page is not found via the slug the revisions are searched for a match.
155      * @param string $bookSlug
156      * @param string $pageSlug
157      * @return Response
158      * @throws NotFoundException
159      */
160     public function show($bookSlug, $pageSlug)
161     {
162         try {
163             $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
164         } catch (NotFoundException $e) {
165             $page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
166             if ($page === null) {
167                 throw $e;
168             }
169             return redirect($page->getUrl());
170         }
171
172         $this->checkOwnablePermission('page-view', $page);
173
174         $page->html = $this->entityRepo->renderPage($page);
175         $sidebarTree = $this->entityRepo->getBookChildren($page->book);
176         $pageNav = $this->entityRepo->getPageNav($page->html);
177
178         // check if the comment's are enabled
179         $commentsEnabled = !setting('app-disable-comments');
180         if ($commentsEnabled) {
181             $page->load(['comments.createdBy']);
182         }
183
184         Views::add($page);
185         $this->setPageTitle($page->getShortName());
186         return view('pages/show', [
187             'page' => $page,'book' => $page->book,
188             'current' => $page,
189             'sidebarTree' => $sidebarTree,
190             'commentsEnabled' => $commentsEnabled,
191             'pageNav' => $pageNav
192         ]);
193     }
194
195     /**
196      * Get page from an ajax request.
197      * @param int $pageId
198      * @return \Illuminate\Http\JsonResponse
199      */
200     public function getPageAjax($pageId)
201     {
202         $page = $this->entityRepo->getById('page', $pageId);
203         return response()->json($page);
204     }
205
206     /**
207      * Show the form for editing the specified page.
208      * @param string $bookSlug
209      * @param string $pageSlug
210      * @return Response
211      */
212     public function edit($bookSlug, $pageSlug)
213     {
214         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
215         $this->checkOwnablePermission('page-update', $page);
216         $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
217         $page->isDraft = false;
218
219         // Check for active editing
220         $warnings = [];
221         if ($this->entityRepo->isPageEditingActive($page, 60)) {
222             $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
223         }
224
225         // Check for a current draft version for this user
226         if ($this->entityRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
227             $draft = $this->entityRepo->getUserPageDraft($page, $this->currentUser->id);
228             $page->name = $draft->name;
229             $page->html = $draft->html;
230             $page->markdown = $draft->markdown;
231             $page->isDraft = true;
232             $warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
233         }
234
235         if (count($warnings) > 0) {
236             session()->flash('warning', implode("\n", $warnings));
237         }
238
239         $draftsEnabled = $this->signedIn;
240         return view('pages/edit', [
241             'page' => $page,
242             'book' => $page->book,
243             'current' => $page,
244             'draftsEnabled' => $draftsEnabled
245         ]);
246     }
247
248     /**
249      * Update the specified page in storage.
250      * @param  Request $request
251      * @param  string $bookSlug
252      * @param  string $pageSlug
253      * @return Response
254      */
255     public function update(Request $request, $bookSlug, $pageSlug)
256     {
257         $this->validate($request, [
258             'name' => 'required|string|max:255'
259         ]);
260         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
261         $this->checkOwnablePermission('page-update', $page);
262         $this->entityRepo->updatePage($page, $page->book->id, $request->all());
263         Activity::add($page, 'page_update', $page->book->id);
264         return redirect($page->getUrl());
265     }
266
267     /**
268      * Save a draft update as a revision.
269      * @param Request $request
270      * @param int $pageId
271      * @return \Illuminate\Http\JsonResponse
272      */
273     public function saveDraft(Request $request, $pageId)
274     {
275         $page = $this->entityRepo->getById('page', $pageId, true);
276         $this->checkOwnablePermission('page-update', $page);
277
278         if (!$this->signedIn) {
279             return response()->json([
280                 'status' => 'error',
281                 'message' => trans('errors.guests_cannot_save_drafts'),
282             ], 500);
283         }
284
285         $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
286
287         $updateTime = $draft->updated_at->timestamp;
288         return response()->json([
289             'status'    => 'success',
290             'message'   => trans('entities.pages_edit_draft_save_at'),
291             'timestamp' => $updateTime
292         ]);
293     }
294
295     /**
296      * Redirect from a special link url which
297      * uses the page id rather than the name.
298      * @param int $pageId
299      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
300      */
301     public function redirectFromLink($pageId)
302     {
303         $page = $this->entityRepo->getById('page', $pageId);
304         return redirect($page->getUrl());
305     }
306
307     /**
308      * Show the deletion page for the specified page.
309      * @param string $bookSlug
310      * @param string $pageSlug
311      * @return \Illuminate\View\View
312      */
313     public function showDelete($bookSlug, $pageSlug)
314     {
315         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
316         $this->checkOwnablePermission('page-delete', $page);
317         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
318         return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
319     }
320
321
322     /**
323      * Show the deletion page for the specified page.
324      * @param string $bookSlug
325      * @param int $pageId
326      * @return \Illuminate\View\View
327      * @throws NotFoundException
328      */
329     public function showDeleteDraft($bookSlug, $pageId)
330     {
331         $page = $this->entityRepo->getById('page', $pageId, true);
332         $this->checkOwnablePermission('page-update', $page);
333         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
334         return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
335     }
336
337     /**
338      * Remove the specified page from storage.
339      * @param string $bookSlug
340      * @param string $pageSlug
341      * @return Response
342      * @internal param int $id
343      */
344     public function destroy($bookSlug, $pageSlug)
345     {
346         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
347         $book = $page->book;
348         $this->checkOwnablePermission('page-delete', $page);
349         $this->entityRepo->destroyPage($page);
350
351         Activity::addMessage('page_delete', $book->id, $page->name);
352         session()->flash('success', trans('entities.pages_delete_success'));
353         return redirect($book->getUrl());
354     }
355
356     /**
357      * Remove the specified draft page from storage.
358      * @param string $bookSlug
359      * @param int $pageId
360      * @return Response
361      * @throws NotFoundException
362      */
363     public function destroyDraft($bookSlug, $pageId)
364     {
365         $page = $this->entityRepo->getById('page', $pageId, true);
366         $book = $page->book;
367         $this->checkOwnablePermission('page-update', $page);
368         session()->flash('success', trans('entities.pages_delete_draft_success'));
369         $this->entityRepo->destroyPage($page);
370         return redirect($book->getUrl());
371     }
372
373     /**
374      * Shows the last revisions for this page.
375      * @param string $bookSlug
376      * @param string $pageSlug
377      * @return \Illuminate\View\View
378      */
379     public function showRevisions($bookSlug, $pageSlug)
380     {
381         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
382         $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
383         return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
384     }
385
386     /**
387      * Shows a preview of a single revision
388      * @param string $bookSlug
389      * @param string $pageSlug
390      * @param int $revisionId
391      * @return \Illuminate\View\View
392      */
393     public function showRevision($bookSlug, $pageSlug, $revisionId)
394     {
395         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
396         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
397         if ($revision === null) {
398             abort(404);
399         }
400
401         $page->fill($revision->toArray());
402         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
403
404         return view('pages/revision', [
405             'page' => $page,
406             'book' => $page->book,
407             'revision' => $revision
408         ]);
409     }
410
411     /**
412      * Shows the changes of a single revision
413      * @param string $bookSlug
414      * @param string $pageSlug
415      * @param int $revisionId
416      * @return \Illuminate\View\View
417      */
418     public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
419     {
420         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
421         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
422         if ($revision === null) {
423             abort(404);
424         }
425
426         $prev = $revision->getPrevious();
427         $prevContent = ($prev === null) ? '' : $prev->html;
428         $diff = (new Htmldiff)->diff($prevContent, $revision->html);
429
430         $page->fill($revision->toArray());
431         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
432
433         return view('pages/revision', [
434             'page' => $page,
435             'book' => $page->book,
436             'diff' => $diff,
437             'revision' => $revision
438         ]);
439     }
440
441     /**
442      * Restores a page using the content of the specified revision.
443      * @param string $bookSlug
444      * @param string $pageSlug
445      * @param int $revisionId
446      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
447      */
448     public function restoreRevision($bookSlug, $pageSlug, $revisionId)
449     {
450         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
451         $this->checkOwnablePermission('page-update', $page);
452         $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
453         Activity::add($page, 'page_restore', $page->book->id);
454         return redirect($page->getUrl());
455     }
456
457     /**
458      * Exports a page to a PDF.
459      * https://github.com/barryvdh/laravel-dompdf
460      * @param string $bookSlug
461      * @param string $pageSlug
462      * @return \Illuminate\Http\Response
463      */
464     public function exportPdf($bookSlug, $pageSlug)
465     {
466         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
467         $page->html = $this->entityRepo->renderPage($page);
468         $pdfContent = $this->exportService->pageToPdf($page);
469         return response()->make($pdfContent, 200, [
470             'Content-Type'        => 'application/octet-stream',
471             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
472         ]);
473     }
474
475     /**
476      * Export a page to a self-contained HTML file.
477      * @param string $bookSlug
478      * @param string $pageSlug
479      * @return \Illuminate\Http\Response
480      */
481     public function exportHtml($bookSlug, $pageSlug)
482     {
483         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
484         $page->html = $this->entityRepo->renderPage($page);
485         $containedHtml = $this->exportService->pageToContainedHtml($page);
486         return response()->make($containedHtml, 200, [
487             'Content-Type'        => 'application/octet-stream',
488             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
489         ]);
490     }
491
492     /**
493      * Export a page to a simple plaintext .txt file.
494      * @param string $bookSlug
495      * @param string $pageSlug
496      * @return \Illuminate\Http\Response
497      */
498     public function exportPlainText($bookSlug, $pageSlug)
499     {
500         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
501         $containedHtml = $this->exportService->pageToPlainText($page);
502         return response()->make($containedHtml, 200, [
503             'Content-Type'        => 'application/octet-stream',
504             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
505         ]);
506     }
507
508     /**
509      * Show a listing of recently created pages
510      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
511      */
512     public function showRecentlyCreated()
513     {
514         $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
515         return view('pages/detailed-listing', [
516             'title' => trans('entities.recently_created_pages'),
517             'pages' => $pages
518         ]);
519     }
520
521     /**
522      * Show a listing of recently created pages
523      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
524      */
525     public function showRecentlyUpdated()
526     {
527         $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
528         return view('pages/detailed-listing', [
529             'title' => trans('entities.recently_updated_pages'),
530             'pages' => $pages
531         ]);
532     }
533
534     /**
535      * Show the Restrictions view.
536      * @param string $bookSlug
537      * @param string $pageSlug
538      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
539      */
540     public function showRestrict($bookSlug, $pageSlug)
541     {
542         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
543         $this->checkOwnablePermission('restrictions-manage', $page);
544         $roles = $this->userRepo->getRestrictableRoles();
545         return view('pages/restrictions', [
546             'page'  => $page,
547             'roles' => $roles
548         ]);
549     }
550
551     /**
552      * Show the view to choose a new parent to move a page into.
553      * @param string $bookSlug
554      * @param string $pageSlug
555      * @return mixed
556      * @throws NotFoundException
557      */
558     public function showMove($bookSlug, $pageSlug)
559     {
560         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
561         $this->checkOwnablePermission('page-update', $page);
562         return view('pages/move', [
563             'book' => $page->book,
564             'page' => $page
565         ]);
566     }
567
568     /**
569      * Does the action of moving the location of a page
570      * @param string $bookSlug
571      * @param string $pageSlug
572      * @param Request $request
573      * @return mixed
574      * @throws NotFoundException
575      */
576     public function move($bookSlug, $pageSlug, Request $request)
577     {
578         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
579         $this->checkOwnablePermission('page-update', $page);
580
581         $entitySelection = $request->get('entity_selection', null);
582         if ($entitySelection === null || $entitySelection === '') {
583             return redirect($page->getUrl());
584         }
585
586         $stringExploded = explode(':', $entitySelection);
587         $entityType = $stringExploded[0];
588         $entityId = intval($stringExploded[1]);
589
590
591         try {
592             $parent = $this->entityRepo->getById($entityType, $entityId);
593         } catch (\Exception $e) {
594             session()->flash(trans('entities.selected_book_chapter_not_found'));
595             return redirect()->back();
596         }
597
598         $this->checkOwnablePermission('page-create', $parent);
599
600         $this->entityRepo->changePageParent($page, $parent);
601         Activity::add($page, 'page_move', $page->book->id);
602         session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
603
604         return redirect($page->getUrl());
605     }
606
607     /**
608      * Show the view to copy a page.
609      * @param string $bookSlug
610      * @param string $pageSlug
611      * @return mixed
612      * @throws NotFoundException
613      */
614     public function showCopy($bookSlug, $pageSlug)
615     {
616         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
617         $this->checkOwnablePermission('page-update', $page);
618         session()->flashInput(['name' => $page->name]);
619         return view('pages/copy', [
620             'book' => $page->book,
621             'page' => $page
622         ]);
623     }
624
625     /**
626      * Create a copy of a page within the requested target destination.
627      * @param string $bookSlug
628      * @param string $pageSlug
629      * @param Request $request
630      * @return mixed
631      * @throws NotFoundException
632      */
633     public function copy($bookSlug, $pageSlug, Request $request)
634     {
635         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
636         $this->checkOwnablePermission('page-update', $page);
637
638         $entitySelection = $request->get('entity_selection', null);
639         if ($entitySelection === null || $entitySelection === '') {
640             $parent = $page->chapter ? $page->chapter : $page->book;
641         } else {
642             $stringExploded = explode(':', $entitySelection);
643             $entityType = $stringExploded[0];
644             $entityId = intval($stringExploded[1]);
645
646             try {
647                 $parent = $this->entityRepo->getById($entityType, $entityId);
648             } catch (\Exception $e) {
649                 session()->flash(trans('entities.selected_book_chapter_not_found'));
650                 return redirect()->back();
651             }
652         }
653
654         $this->checkOwnablePermission('page-create', $parent);
655
656         $pageCopy = $this->entityRepo->copyPage($page, $parent, $request->get('name', ''));
657
658         Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
659         session()->flash('success', trans('entities.pages_copy_success'));
660
661         return redirect($pageCopy->getUrl());
662     }
663
664     /**
665      * Set the permissions for this page.
666      * @param string $bookSlug
667      * @param string $pageSlug
668      * @param Request $request
669      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
670      * @throws NotFoundException
671      */
672     public function restrict($bookSlug, $pageSlug, Request $request)
673     {
674         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
675         $this->checkOwnablePermission('restrictions-manage', $page);
676         $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
677         session()->flash('success', trans('entities.pages_permissions_success'));
678         return redirect($page->getUrl());
679     }
680 }