1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Exceptions\BadRequestException;
6 use BookStack\Repos\EntityRepo;
7 use BookStack\Repos\UserRepo;
8 use BookStack\Services\ExportService;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
12 use GatherContent\Htmldiff\Htmldiff;
14 class PageController extends Controller
17 protected $entityRepo;
18 protected $exportService;
22 * PageController constructor.
23 * @param EntityRepo $entityRepo
24 * @param ExportService $exportService
25 * @param UserRepo $userRepo
27 public function __construct(EntityRepo $entityRepo, ExportService $exportService, UserRepo $userRepo)
29 $this->entityRepo = $entityRepo;
30 $this->exportService = $exportService;
31 $this->userRepo = $userRepo;
32 parent::__construct();
36 * Show the form for creating a new page.
37 * @param string $bookSlug
38 * @param string $chapterSlug
40 * @internal param bool $pageSlug
41 * @throws NotFoundException
43 public function create($bookSlug, $chapterSlug = null)
45 if ($chapterSlug !== null) {
46 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
47 $book = $chapter->book;
50 $book = $this->entityRepo->getBySlug('book', $bookSlug);
53 $parent = $chapter ? $chapter : $book;
54 $this->checkOwnablePermission('page-create', $parent);
56 // Redirect to draft edit screen if signed in
57 if ($this->signedIn) {
58 $draft = $this->entityRepo->getDraftPage($book, $chapter);
59 return redirect($draft->getUrl());
62 // Otherwise show the edit view if they're a guest
63 $this->setPageTitle(trans('entities.pages_new'));
64 return view('pages/guest-create', ['parent' => $parent]);
68 * Create a new page as a guest user.
69 * @param Request $request
70 * @param string $bookSlug
71 * @param string|null $chapterSlug
73 * @throws NotFoundException
75 public function createAsGuest(Request $request, $bookSlug, $chapterSlug = null)
77 $this->validate($request, [
78 'name' => 'required|string|max:255'
81 if ($chapterSlug !== null) {
82 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
83 $book = $chapter->book;
86 $book = $this->entityRepo->getBySlug('book', $bookSlug);
89 $parent = $chapter ? $chapter : $book;
90 $this->checkOwnablePermission('page-create', $parent);
92 $page = $this->entityRepo->getDraftPage($book, $chapter);
93 $this->entityRepo->publishPageDraft($page, [
94 'name' => $request->get('name'),
97 return redirect($page->getUrl('/edit'));
101 * Show form to continue editing a draft page.
102 * @param string $bookSlug
104 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
106 public function editDraft($bookSlug, $pageId)
108 $draft = $this->entityRepo->getById('page', $pageId, true);
109 $this->checkOwnablePermission('page-create', $draft->parent);
110 $this->setPageTitle(trans('entities.pages_edit_draft'));
112 $draftsEnabled = $this->signedIn;
113 return view('pages/edit', [
115 'book' => $draft->book,
117 'draftsEnabled' => $draftsEnabled
122 * Store a new page by changing a draft into a page.
123 * @param Request $request
124 * @param string $bookSlug
128 public function store(Request $request, $bookSlug, $pageId)
130 $this->validate($request, [
131 'name' => 'required|string|max:255'
134 $input = $request->all();
135 $draftPage = $this->entityRepo->getById('page', $pageId, true);
136 $book = $draftPage->book;
138 $parent = $draftPage->parent;
139 $this->checkOwnablePermission('page-create', $parent);
141 if ($parent->isA('chapter')) {
142 $input['priority'] = $this->entityRepo->getNewChapterPriority($parent);
144 $input['priority'] = $this->entityRepo->getNewBookPriority($parent);
147 $page = $this->entityRepo->publishPageDraft($draftPage, $input);
149 Activity::add($page, 'page_create', $book->id);
150 return redirect($page->getUrl());
154 * Display the specified page.
155 * If the page is not found via the slug the revisions are searched for a match.
156 * @param string $bookSlug
157 * @param string $pageSlug
159 * @throws NotFoundException
161 public function show($bookSlug, $pageSlug)
164 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
165 } catch (NotFoundException $e) {
166 $page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
167 if ($page === null) {
170 return redirect($page->getUrl());
173 $this->checkOwnablePermission('page-view', $page);
175 $page->html = $this->entityRepo->renderPage($page);
176 $sidebarTree = $this->entityRepo->getBookChildren($page->book);
177 $pageNav = $this->entityRepo->getPageNav($page->html);
179 // check if the comment's are enabled
180 $commentsEnabled = !setting('app-disable-comments');
181 if ($commentsEnabled) {
182 $page->load(['comments.createdBy']);
186 $this->setPageTitle($page->getShortName());
187 return view('pages/show', [
188 'page' => $page,'book' => $page->book,
190 'sidebarTree' => $sidebarTree,
191 'commentsEnabled' => $commentsEnabled,
192 'pageNav' => $pageNav
197 * Get page from an ajax request.
199 * @return \Illuminate\Http\JsonResponse
201 public function getPageAjax($pageId)
203 $page = $this->entityRepo->getById('page', $pageId);
204 return response()->json($page);
208 * Show the form for editing the specified page.
209 * @param string $bookSlug
210 * @param string $pageSlug
213 public function edit($bookSlug, $pageSlug)
215 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
216 $this->checkOwnablePermission('page-update', $page);
217 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
218 $page->isDraft = false;
220 // Check for active editing
222 if ($this->entityRepo->isPageEditingActive($page, 60)) {
223 $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
226 // Check for a current draft version for this user
227 if ($this->entityRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
228 $draft = $this->entityRepo->getUserPageDraft($page, $this->currentUser->id);
229 $page->name = $draft->name;
230 $page->html = $draft->html;
231 $page->markdown = $draft->markdown;
232 $page->isDraft = true;
233 $warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
236 if (count($warnings) > 0) {
237 session()->flash('warning', implode("\n", $warnings));
240 $draftsEnabled = $this->signedIn;
241 return view('pages/edit', [
243 'book' => $page->book,
245 'draftsEnabled' => $draftsEnabled
250 * Update the specified page in storage.
251 * @param Request $request
252 * @param string $bookSlug
253 * @param string $pageSlug
256 public function update(Request $request, $bookSlug, $pageSlug)
258 $this->validate($request, [
259 'name' => 'required|string|max:255'
261 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
262 $this->checkOwnablePermission('page-update', $page);
263 $this->entityRepo->updatePage($page, $page->book->id, $request->all());
264 Activity::add($page, 'page_update', $page->book->id);
265 return redirect($page->getUrl());
269 * Save a draft update as a revision.
270 * @param Request $request
272 * @return \Illuminate\Http\JsonResponse
274 public function saveDraft(Request $request, $pageId)
276 $page = $this->entityRepo->getById('page', $pageId, true);
277 $this->checkOwnablePermission('page-update', $page);
279 if (!$this->signedIn) {
280 return response()->json([
282 'message' => trans('errors.guests_cannot_save_drafts'),
286 $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
288 $updateTime = $draft->updated_at->timestamp;
289 return response()->json([
290 'status' => 'success',
291 'message' => trans('entities.pages_edit_draft_save_at'),
292 'timestamp' => $updateTime
297 * Redirect from a special link url which
298 * uses the page id rather than the name.
300 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
302 public function redirectFromLink($pageId)
304 $page = $this->entityRepo->getById('page', $pageId);
305 return redirect($page->getUrl());
309 * Show the deletion page for the specified page.
310 * @param string $bookSlug
311 * @param string $pageSlug
312 * @return \Illuminate\View\View
314 public function showDelete($bookSlug, $pageSlug)
316 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
317 $this->checkOwnablePermission('page-delete', $page);
318 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
319 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
324 * Show the deletion page for the specified page.
325 * @param string $bookSlug
327 * @return \Illuminate\View\View
328 * @throws NotFoundException
330 public function showDeleteDraft($bookSlug, $pageId)
332 $page = $this->entityRepo->getById('page', $pageId, true);
333 $this->checkOwnablePermission('page-update', $page);
334 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
335 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
339 * Remove the specified page from storage.
340 * @param string $bookSlug
341 * @param string $pageSlug
343 * @internal param int $id
345 public function destroy($bookSlug, $pageSlug)
347 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
349 $this->checkOwnablePermission('page-delete', $page);
350 $this->entityRepo->destroyPage($page);
352 Activity::addMessage('page_delete', $book->id, $page->name);
353 session()->flash('success', trans('entities.pages_delete_success'));
354 return redirect($book->getUrl());
358 * Remove the specified draft page from storage.
359 * @param string $bookSlug
362 * @throws NotFoundException
364 public function destroyDraft($bookSlug, $pageId)
366 $page = $this->entityRepo->getById('page', $pageId, true);
368 $this->checkOwnablePermission('page-update', $page);
369 session()->flash('success', trans('entities.pages_delete_draft_success'));
370 $this->entityRepo->destroyPage($page);
371 return redirect($book->getUrl());
375 * Shows the last revisions for this page.
376 * @param string $bookSlug
377 * @param string $pageSlug
378 * @return \Illuminate\View\View
380 public function showRevisions($bookSlug, $pageSlug)
382 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
383 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
384 return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
388 * Shows a preview of a single revision
389 * @param string $bookSlug
390 * @param string $pageSlug
391 * @param int $revisionId
392 * @return \Illuminate\View\View
394 public function showRevision($bookSlug, $pageSlug, $revisionId)
396 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
397 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
398 if ($revision === null) {
402 $page->fill($revision->toArray());
403 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
405 return view('pages/revision', [
407 'book' => $page->book,
408 'revision' => $revision
413 * Shows the changes of a single revision
414 * @param string $bookSlug
415 * @param string $pageSlug
416 * @param int $revisionId
417 * @return \Illuminate\View\View
419 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
421 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
422 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
423 if ($revision === null) {
427 $prev = $revision->getPrevious();
428 $prevContent = ($prev === null) ? '' : $prev->html;
429 $diff = (new Htmldiff)->diff($prevContent, $revision->html);
431 $page->fill($revision->toArray());
432 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
434 return view('pages/revision', [
436 'book' => $page->book,
438 'revision' => $revision
443 * Restores a page using the content of the specified revision.
444 * @param string $bookSlug
445 * @param string $pageSlug
446 * @param int $revisionId
447 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
449 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
451 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
452 $this->checkOwnablePermission('page-update', $page);
453 $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
454 Activity::add($page, 'page_restore', $page->book->id);
455 return redirect($page->getUrl());
460 * Deletes a revision using the id of the specified revision.
461 * @param string $bookSlug
462 * @param string $pageSlug
463 * @param int $revisionId
464 * @throws NotFoundException
465 * @throws BadRequestException
466 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
468 public function destroyRevision($bookSlug, $pageSlug, $revId)
470 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
471 $this->checkOwnablePermission('page-update', $page);
473 $revision = $page->revisions()->where('id', '=', $revId)->first();
474 if ($revision === null) {
475 throw new NotFoundException("Revision #{$revId} not found");
478 // Get the current revision for the page
479 $current = $revision->getCurrent();
481 // Check if its the latest revision, cannot delete latest revision.
482 if (intval($current->id) === intval($revId)) {
483 throw new BadRequestException("Cannot delete the current revision #{$revId}");
487 return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
491 * Exports a page to a PDF.
492 * https://github.com/barryvdh/laravel-dompdf
493 * @param string $bookSlug
494 * @param string $pageSlug
495 * @return \Illuminate\Http\Response
497 public function exportPdf($bookSlug, $pageSlug)
499 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
500 $page->html = $this->entityRepo->renderPage($page);
501 $pdfContent = $this->exportService->pageToPdf($page);
502 return response()->make($pdfContent, 200, [
503 'Content-Type' => 'application/octet-stream',
504 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
509 * Export a page to a self-contained HTML file.
510 * @param string $bookSlug
511 * @param string $pageSlug
512 * @return \Illuminate\Http\Response
514 public function exportHtml($bookSlug, $pageSlug)
516 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
517 $page->html = $this->entityRepo->renderPage($page);
518 $containedHtml = $this->exportService->pageToContainedHtml($page);
519 return response()->make($containedHtml, 200, [
520 'Content-Type' => 'application/octet-stream',
521 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
526 * Export a page to a simple plaintext .txt file.
527 * @param string $bookSlug
528 * @param string $pageSlug
529 * @return \Illuminate\Http\Response
531 public function exportPlainText($bookSlug, $pageSlug)
533 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
534 $containedHtml = $this->exportService->pageToPlainText($page);
535 return response()->make($containedHtml, 200, [
536 'Content-Type' => 'application/octet-stream',
537 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
542 * Show a listing of recently created pages
543 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
545 public function showRecentlyCreated()
547 $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
548 return view('pages/detailed-listing', [
549 'title' => trans('entities.recently_created_pages'),
555 * Show a listing of recently created pages
556 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
558 public function showRecentlyUpdated()
560 $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
561 return view('pages/detailed-listing', [
562 'title' => trans('entities.recently_updated_pages'),
568 * Show the Restrictions view.
569 * @param string $bookSlug
570 * @param string $pageSlug
571 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
573 public function showRestrict($bookSlug, $pageSlug)
575 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
576 $this->checkOwnablePermission('restrictions-manage', $page);
577 $roles = $this->userRepo->getRestrictableRoles();
578 return view('pages/restrictions', [
585 * Show the view to choose a new parent to move a page into.
586 * @param string $bookSlug
587 * @param string $pageSlug
589 * @throws NotFoundException
591 public function showMove($bookSlug, $pageSlug)
593 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
594 $this->checkOwnablePermission('page-update', $page);
595 return view('pages/move', [
596 'book' => $page->book,
602 * Does the action of moving the location of a page
603 * @param string $bookSlug
604 * @param string $pageSlug
605 * @param Request $request
607 * @throws NotFoundException
609 public function move($bookSlug, $pageSlug, Request $request)
611 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
612 $this->checkOwnablePermission('page-update', $page);
614 $entitySelection = $request->get('entity_selection', null);
615 if ($entitySelection === null || $entitySelection === '') {
616 return redirect($page->getUrl());
619 $stringExploded = explode(':', $entitySelection);
620 $entityType = $stringExploded[0];
621 $entityId = intval($stringExploded[1]);
625 $parent = $this->entityRepo->getById($entityType, $entityId);
626 } catch (\Exception $e) {
627 session()->flash(trans('entities.selected_book_chapter_not_found'));
628 return redirect()->back();
631 $this->checkOwnablePermission('page-create', $parent);
633 $this->entityRepo->changePageParent($page, $parent);
634 Activity::add($page, 'page_move', $page->book->id);
635 session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
637 return redirect($page->getUrl());
641 * Show the view to copy a page.
642 * @param string $bookSlug
643 * @param string $pageSlug
645 * @throws NotFoundException
647 public function showCopy($bookSlug, $pageSlug)
649 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
650 $this->checkOwnablePermission('page-update', $page);
651 session()->flashInput(['name' => $page->name]);
652 return view('pages/copy', [
653 'book' => $page->book,
659 * Create a copy of a page within the requested target destination.
660 * @param string $bookSlug
661 * @param string $pageSlug
662 * @param Request $request
664 * @throws NotFoundException
666 public function copy($bookSlug, $pageSlug, Request $request)
668 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
669 $this->checkOwnablePermission('page-update', $page);
671 $entitySelection = $request->get('entity_selection', null);
672 if ($entitySelection === null || $entitySelection === '') {
673 $parent = $page->chapter ? $page->chapter : $page->book;
675 $stringExploded = explode(':', $entitySelection);
676 $entityType = $stringExploded[0];
677 $entityId = intval($stringExploded[1]);
680 $parent = $this->entityRepo->getById($entityType, $entityId);
681 } catch (\Exception $e) {
682 session()->flash(trans('entities.selected_book_chapter_not_found'));
683 return redirect()->back();
687 $this->checkOwnablePermission('page-create', $parent);
689 $pageCopy = $this->entityRepo->copyPage($page, $parent, $request->get('name', ''));
691 Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
692 session()->flash('success', trans('entities.pages_copy_success'));
694 return redirect($pageCopy->getUrl());
698 * Set the permissions for this page.
699 * @param string $bookSlug
700 * @param string $pageSlug
701 * @param Request $request
702 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
703 * @throws NotFoundException
705 public function restrict($bookSlug, $pageSlug, Request $request)
707 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
708 $this->checkOwnablePermission('restrictions-manage', $page);
709 $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
710 session()->flash('success', trans('entities.pages_permissions_success'));
711 return redirect($page->getUrl());