1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Repos\EntityRepo;
6 use BookStack\Entities\ExportService;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Exceptions\NotFoundException;
9 use GatherContent\Htmldiff\Htmldiff;
10 use Illuminate\Http\Request;
11 use Illuminate\Http\Response;
14 class PageController extends Controller
18 protected $exportService;
22 * PageController constructor.
23 * @param \BookStack\Entities\Repos\PageRepo $pageRepo
24 * @param \BookStack\Entities\ExportService $exportService
25 * @param UserRepo $userRepo
27 public function __construct(PageRepo $pageRepo, ExportService $exportService, UserRepo $userRepo)
29 $this->pageRepo = $pageRepo;
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->pageRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
47 $book = $chapter->book;
50 $book = $this->pageRepo->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->pageRepo->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->pageRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
83 $book = $chapter->book;
86 $book = $this->pageRepo->getBySlug('book', $bookSlug);
89 $parent = $chapter ? $chapter : $book;
90 $this->checkOwnablePermission('page-create', $parent);
92 $page = $this->pageRepo->getDraftPage($book, $chapter);
93 $this->pageRepo->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->pageRepo->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->pageRepo->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->pageRepo->getNewChapterPriority($parent);
144 $input['priority'] = $this->pageRepo->getNewBookPriority($parent);
147 $page = $this->pageRepo->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->pageRepo->getPageBySlug($pageSlug, $bookSlug);
165 } catch (NotFoundException $e) {
166 $page = $this->pageRepo->getPageByOldSlug($pageSlug, $bookSlug);
167 if ($page === null) {
170 return redirect($page->getUrl());
173 $this->checkOwnablePermission('page-view', $page);
175 $page->html = $this->pageRepo->renderPage($page);
176 $sidebarTree = $this->pageRepo->getBookChildren($page->book);
177 $pageNav = $this->pageRepo->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->pageRepo->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
212 * @throws NotFoundException
214 public function edit($bookSlug, $pageSlug)
216 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
217 $this->checkOwnablePermission('page-update', $page);
218 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
219 $page->isDraft = false;
221 // Check for active editing
223 if ($this->pageRepo->isPageEditingActive($page, 60)) {
224 $warnings[] = $this->pageRepo->getPageEditingActiveMessage($page, 60);
227 // Check for a current draft version for this user
228 $userPageDraft = $this->pageRepo->getUserPageDraft($page, $this->currentUser->id);
229 if ($userPageDraft !== null) {
230 $page->name = $userPageDraft->name;
231 $page->html = $userPageDraft->html;
232 $page->markdown = $userPageDraft->markdown;
233 $page->isDraft = true;
234 $warnings [] = $this->pageRepo->getUserPageDraftMessage($userPageDraft);
237 if (count($warnings) > 0) {
238 session()->flash('warning', implode("\n", $warnings));
241 $draftsEnabled = $this->signedIn;
242 return view('pages.edit', [
244 'book' => $page->book,
246 'draftsEnabled' => $draftsEnabled
251 * Update the specified page in storage.
252 * @param Request $request
253 * @param string $bookSlug
254 * @param string $pageSlug
257 public function update(Request $request, $bookSlug, $pageSlug)
259 $this->validate($request, [
260 'name' => 'required|string|max:255'
262 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
263 $this->checkOwnablePermission('page-update', $page);
264 $this->pageRepo->updatePage($page, $page->book->id, $request->all());
265 Activity::add($page, 'page_update', $page->book->id);
266 return redirect($page->getUrl());
270 * Save a draft update as a revision.
271 * @param Request $request
273 * @return \Illuminate\Http\JsonResponse
275 public function saveDraft(Request $request, $pageId)
277 $page = $this->pageRepo->getById('page', $pageId, true);
278 $this->checkOwnablePermission('page-update', $page);
280 if (!$this->signedIn) {
281 return response()->json([
283 'message' => trans('errors.guests_cannot_save_drafts'),
287 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
289 $updateTime = $draft->updated_at->timestamp;
290 return response()->json([
291 'status' => 'success',
292 'message' => trans('entities.pages_edit_draft_save_at'),
293 'timestamp' => $updateTime
298 * Redirect from a special link url which
299 * uses the page id rather than the name.
301 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
303 public function redirectFromLink($pageId)
305 $page = $this->pageRepo->getById('page', $pageId);
306 return redirect($page->getUrl());
310 * Show the deletion page for the specified page.
311 * @param string $bookSlug
312 * @param string $pageSlug
313 * @return \Illuminate\View\View
315 public function showDelete($bookSlug, $pageSlug)
317 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
318 $this->checkOwnablePermission('page-delete', $page);
319 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
320 return view('pages.delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
325 * Show the deletion page for the specified page.
326 * @param string $bookSlug
328 * @return \Illuminate\View\View
329 * @throws NotFoundException
331 public function showDeleteDraft($bookSlug, $pageId)
333 $page = $this->pageRepo->getById('page', $pageId, true);
334 $this->checkOwnablePermission('page-update', $page);
335 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
336 return view('pages.delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
340 * Remove the specified page from storage.
341 * @param string $bookSlug
342 * @param string $pageSlug
344 * @internal param int $id
346 public function destroy($bookSlug, $pageSlug)
348 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
350 $this->checkOwnablePermission('page-delete', $page);
351 $this->pageRepo->destroyPage($page);
353 Activity::addMessage('page_delete', $book->id, $page->name);
354 session()->flash('success', trans('entities.pages_delete_success'));
355 return redirect($book->getUrl());
359 * Remove the specified draft page from storage.
360 * @param string $bookSlug
363 * @throws NotFoundException
365 public function destroyDraft($bookSlug, $pageId)
367 $page = $this->pageRepo->getById('page', $pageId, true);
369 $this->checkOwnablePermission('page-update', $page);
370 session()->flash('success', trans('entities.pages_delete_draft_success'));
371 $this->pageRepo->destroyPage($page);
372 return redirect($book->getUrl());
376 * Shows the last revisions for this page.
377 * @param string $bookSlug
378 * @param string $pageSlug
379 * @return \Illuminate\View\View
380 * @throws NotFoundException
382 public function showRevisions($bookSlug, $pageSlug)
384 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
385 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
386 return view('pages.revisions', ['page' => $page, 'current' => $page]);
390 * Shows a preview of a single revision
391 * @param string $bookSlug
392 * @param string $pageSlug
393 * @param int $revisionId
394 * @return \Illuminate\View\View
396 public function showRevision($bookSlug, $pageSlug, $revisionId)
398 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
399 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
400 if ($revision === null) {
404 $page->fill($revision->toArray());
405 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
407 return view('pages.revision', [
409 'book' => $page->book,
411 'revision' => $revision
416 * Shows the changes of a single revision
417 * @param string $bookSlug
418 * @param string $pageSlug
419 * @param int $revisionId
420 * @return \Illuminate\View\View
422 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
424 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
425 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
426 if ($revision === null) {
430 $prev = $revision->getPrevious();
431 $prevContent = ($prev === null) ? '' : $prev->html;
432 $diff = (new Htmldiff)->diff($prevContent, $revision->html);
434 $page->fill($revision->toArray());
435 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
437 return view('pages.revision', [
439 'book' => $page->book,
441 'revision' => $revision
446 * Restores a page using the content of the specified revision.
447 * @param string $bookSlug
448 * @param string $pageSlug
449 * @param int $revisionId
450 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
452 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
454 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
455 $this->checkOwnablePermission('page-update', $page);
456 $page = $this->pageRepo->restorePageRevision($page, $page->book, $revisionId);
457 Activity::add($page, 'page_restore', $page->book->id);
458 return redirect($page->getUrl());
463 * Deletes a revision using the id of the specified revision.
464 * @param string $bookSlug
465 * @param string $pageSlug
467 * @throws NotFoundException
468 * @throws BadRequestException
469 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
471 public function destroyRevision($bookSlug, $pageSlug, $revId)
473 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
474 $this->checkOwnablePermission('page-delete', $page);
476 $revision = $page->revisions()->where('id', '=', $revId)->first();
477 if ($revision === null) {
478 throw new NotFoundException("Revision #{$revId} not found");
481 // Get the current revision for the page
482 $currentRevision = $page->getCurrentRevision();
484 // Check if its the latest revision, cannot delete latest revision.
485 if (intval($currentRevision->id) === intval($revId)) {
486 session()->flash('error', trans('entities.revision_cannot_delete_latest'));
487 return response()->view('pages.revisions', ['page' => $page, 'book' => $page->book, 'current' => $page], 400);
491 session()->flash('success', trans('entities.revision_delete_success'));
492 return view('pages.revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
496 * Exports a page to a PDF.
497 * https://github.com/barryvdh/laravel-dompdf
498 * @param string $bookSlug
499 * @param string $pageSlug
500 * @return \Illuminate\Http\Response
502 public function exportPdf($bookSlug, $pageSlug)
504 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
505 $page->html = $this->pageRepo->renderPage($page);
506 $pdfContent = $this->exportService->pageToPdf($page);
507 return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
511 * Export a page to a self-contained HTML file.
512 * @param string $bookSlug
513 * @param string $pageSlug
514 * @return \Illuminate\Http\Response
516 public function exportHtml($bookSlug, $pageSlug)
518 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
519 $page->html = $this->pageRepo->renderPage($page);
520 $containedHtml = $this->exportService->pageToContainedHtml($page);
521 return $this->downloadResponse($containedHtml, $pageSlug . '.html');
525 * Export a page to a simple plaintext .txt file.
526 * @param string $bookSlug
527 * @param string $pageSlug
528 * @return \Illuminate\Http\Response
530 public function exportPlainText($bookSlug, $pageSlug)
532 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
533 $pageText = $this->exportService->pageToPlainText($page);
534 return $this->downloadResponse($pageText, $pageSlug . '.txt');
538 * Show a listing of recently created pages
539 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
541 public function showRecentlyUpdated()
543 // TODO - Still exist?
544 $pages = $this->pageRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
545 return view('pages.detailed-listing', [
546 'title' => trans('entities.recently_updated_pages'),
552 * Show the view to choose a new parent to move a page into.
553 * @param string $bookSlug
554 * @param string $pageSlug
556 * @throws NotFoundException
558 public function showMove($bookSlug, $pageSlug)
560 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
561 $this->checkOwnablePermission('page-update', $page);
562 $this->checkOwnablePermission('page-delete', $page);
563 return view('pages.move', [
564 'book' => $page->book,
570 * Does the action of moving the location of a page
571 * @param string $bookSlug
572 * @param string $pageSlug
573 * @param Request $request
575 * @throws NotFoundException
577 public function move($bookSlug, $pageSlug, Request $request)
579 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
580 $this->checkOwnablePermission('page-update', $page);
581 $this->checkOwnablePermission('page-delete', $page);
583 $entitySelection = $request->get('entity_selection', null);
584 if ($entitySelection === null || $entitySelection === '') {
585 return redirect($page->getUrl());
588 $stringExploded = explode(':', $entitySelection);
589 $entityType = $stringExploded[0];
590 $entityId = intval($stringExploded[1]);
594 $parent = $this->pageRepo->getById($entityType, $entityId);
595 } catch (\Exception $e) {
596 session()->flash(trans('entities.selected_book_chapter_not_found'));
597 return redirect()->back();
600 $this->checkOwnablePermission('page-create', $parent);
602 $this->pageRepo->changePageParent($page, $parent);
603 Activity::add($page, 'page_move', $page->book->id);
604 session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
606 return redirect($page->getUrl());
610 * Show the view to copy a page.
611 * @param string $bookSlug
612 * @param string $pageSlug
614 * @throws NotFoundException
616 public function showCopy($bookSlug, $pageSlug)
618 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
619 $this->checkOwnablePermission('page-view', $page);
620 session()->flashInput(['name' => $page->name]);
621 return view('pages.copy', [
622 'book' => $page->book,
628 * Create a copy of a page within the requested target destination.
629 * @param string $bookSlug
630 * @param string $pageSlug
631 * @param Request $request
633 * @throws NotFoundException
635 public function copy($bookSlug, $pageSlug, Request $request)
637 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
638 $this->checkOwnablePermission('page-view', $page);
640 $entitySelection = $request->get('entity_selection', null);
641 if ($entitySelection === null || $entitySelection === '') {
642 $parent = $page->chapter ? $page->chapter : $page->book;
644 $stringExploded = explode(':', $entitySelection);
645 $entityType = $stringExploded[0];
646 $entityId = intval($stringExploded[1]);
649 $parent = $this->pageRepo->getById($entityType, $entityId);
650 } catch (\Exception $e) {
651 session()->flash(trans('entities.selected_book_chapter_not_found'));
652 return redirect()->back();
656 $this->checkOwnablePermission('page-create', $parent);
658 $pageCopy = $this->pageRepo->copyPage($page, $parent, $request->get('name', ''));
660 Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
661 session()->flash('success', trans('entities.pages_copy_success'));
663 return redirect($pageCopy->getUrl());
667 * Show the Permissions view.
668 * @param string $bookSlug
669 * @param string $pageSlug
670 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
671 * @throws NotFoundException
673 public function showPermissions($bookSlug, $pageSlug)
675 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
676 $this->checkOwnablePermission('restrictions-manage', $page);
677 $roles = $this->userRepo->getRestrictableRoles();
678 return view('pages.permissions', [
685 * Set the permissions for this page.
686 * @param string $bookSlug
687 * @param string $pageSlug
688 * @param Request $request
689 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
690 * @throws NotFoundException
693 public function permissions($bookSlug, $pageSlug, Request $request)
695 $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
696 $this->checkOwnablePermission('restrictions-manage', $page);
697 $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
698 session()->flash('success', trans('entities.pages_permissions_success'));
699 return redirect($page->getUrl());