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 session()->flash('success', trans('entities.revision_delete_success'));
488 return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
492 * Exports a page to a PDF.
493 * https://github.com/barryvdh/laravel-dompdf
494 * @param string $bookSlug
495 * @param string $pageSlug
496 * @return \Illuminate\Http\Response
498 public function exportPdf($bookSlug, $pageSlug)
500 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
501 $page->html = $this->entityRepo->renderPage($page);
502 $pdfContent = $this->exportService->pageToPdf($page);
503 return response()->make($pdfContent, 200, [
504 'Content-Type' => 'application/octet-stream',
505 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
510 * Export a page to a self-contained HTML file.
511 * @param string $bookSlug
512 * @param string $pageSlug
513 * @return \Illuminate\Http\Response
515 public function exportHtml($bookSlug, $pageSlug)
517 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
518 $page->html = $this->entityRepo->renderPage($page);
519 $containedHtml = $this->exportService->pageToContainedHtml($page);
520 return response()->make($containedHtml, 200, [
521 'Content-Type' => 'application/octet-stream',
522 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
527 * Export a page to a simple plaintext .txt file.
528 * @param string $bookSlug
529 * @param string $pageSlug
530 * @return \Illuminate\Http\Response
532 public function exportPlainText($bookSlug, $pageSlug)
534 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
535 $containedHtml = $this->exportService->pageToPlainText($page);
536 return response()->make($containedHtml, 200, [
537 'Content-Type' => 'application/octet-stream',
538 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
543 * Show a listing of recently created pages
544 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
546 public function showRecentlyCreated()
548 $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
549 return view('pages/detailed-listing', [
550 'title' => trans('entities.recently_created_pages'),
556 * Show a listing of recently created pages
557 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
559 public function showRecentlyUpdated()
561 $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
562 return view('pages/detailed-listing', [
563 'title' => trans('entities.recently_updated_pages'),
569 * Show the Restrictions view.
570 * @param string $bookSlug
571 * @param string $pageSlug
572 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
574 public function showRestrict($bookSlug, $pageSlug)
576 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
577 $this->checkOwnablePermission('restrictions-manage', $page);
578 $roles = $this->userRepo->getRestrictableRoles();
579 return view('pages/restrictions', [
586 * Show the view to choose a new parent to move a page into.
587 * @param string $bookSlug
588 * @param string $pageSlug
590 * @throws NotFoundException
592 public function showMove($bookSlug, $pageSlug)
594 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
595 $this->checkOwnablePermission('page-update', $page);
596 return view('pages/move', [
597 'book' => $page->book,
603 * Does the action of moving the location of a page
604 * @param string $bookSlug
605 * @param string $pageSlug
606 * @param Request $request
608 * @throws NotFoundException
610 public function move($bookSlug, $pageSlug, Request $request)
612 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
613 $this->checkOwnablePermission('page-update', $page);
615 $entitySelection = $request->get('entity_selection', null);
616 if ($entitySelection === null || $entitySelection === '') {
617 return redirect($page->getUrl());
620 $stringExploded = explode(':', $entitySelection);
621 $entityType = $stringExploded[0];
622 $entityId = intval($stringExploded[1]);
626 $parent = $this->entityRepo->getById($entityType, $entityId);
627 } catch (\Exception $e) {
628 session()->flash(trans('entities.selected_book_chapter_not_found'));
629 return redirect()->back();
632 $this->checkOwnablePermission('page-create', $parent);
634 $this->entityRepo->changePageParent($page, $parent);
635 Activity::add($page, 'page_move', $page->book->id);
636 session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
638 return redirect($page->getUrl());
642 * Show the view to copy a page.
643 * @param string $bookSlug
644 * @param string $pageSlug
646 * @throws NotFoundException
648 public function showCopy($bookSlug, $pageSlug)
650 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
651 $this->checkOwnablePermission('page-update', $page);
652 session()->flashInput(['name' => $page->name]);
653 return view('pages/copy', [
654 'book' => $page->book,
660 * Create a copy of a page within the requested target destination.
661 * @param string $bookSlug
662 * @param string $pageSlug
663 * @param Request $request
665 * @throws NotFoundException
667 public function copy($bookSlug, $pageSlug, Request $request)
669 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
670 $this->checkOwnablePermission('page-update', $page);
672 $entitySelection = $request->get('entity_selection', null);
673 if ($entitySelection === null || $entitySelection === '') {
674 $parent = $page->chapter ? $page->chapter : $page->book;
676 $stringExploded = explode(':', $entitySelection);
677 $entityType = $stringExploded[0];
678 $entityId = intval($stringExploded[1]);
681 $parent = $this->entityRepo->getById($entityType, $entityId);
682 } catch (\Exception $e) {
683 session()->flash(trans('entities.selected_book_chapter_not_found'));
684 return redirect()->back();
688 $this->checkOwnablePermission('page-create', $parent);
690 $pageCopy = $this->entityRepo->copyPage($page, $parent, $request->get('name', ''));
692 Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
693 session()->flash('success', trans('entities.pages_copy_success'));
695 return redirect($pageCopy->getUrl());
699 * Set the permissions for this page.
700 * @param string $bookSlug
701 * @param string $pageSlug
702 * @param Request $request
703 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
704 * @throws NotFoundException
706 public function restrict($bookSlug, $pageSlug, Request $request)
708 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
709 $this->checkOwnablePermission('restrictions-manage', $page);
710 $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
711 session()->flash('success', trans('entities.pages_permissions_success'));
712 return redirect($page->getUrl());