1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Repos\UserRepo;
7 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
42 public function create($bookSlug, $chapterSlug = null)
44 $book = $this->entityRepo->getBySlug('book', $bookSlug);
45 $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
46 $parent = $chapter ? $chapter : $book;
47 $this->checkOwnablePermission('page-create', $parent);
49 // Redirect to draft edit screen if signed in
50 if ($this->signedIn) {
51 $draft = $this->entityRepo->getDraftPage($book, $chapter);
52 return redirect($draft->getUrl());
55 // Otherwise show edit view
56 $this->setPageTitle(trans('entities.pages_new'));
57 return view('pages/guest-create', ['parent' => $parent]);
61 * Create a new page as a guest user.
62 * @param Request $request
63 * @param string $bookSlug
64 * @param string|null $chapterSlug
66 * @throws NotFoundException
68 public function createAsGuest(Request $request, $bookSlug, $chapterSlug = null)
70 $this->validate($request, [
71 'name' => 'required|string|max:255'
74 $book = $this->entityRepo->getBySlug('book', $bookSlug);
75 $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
76 $parent = $chapter ? $chapter : $book;
77 $this->checkOwnablePermission('page-create', $parent);
79 $page = $this->entityRepo->getDraftPage($book, $chapter);
80 $this->entityRepo->publishPageDraft($page, [
81 'name' => $request->get('name'),
84 return redirect($page->getUrl('/edit'));
88 * Show form to continue editing a draft page.
89 * @param string $bookSlug
91 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
93 public function editDraft($bookSlug, $pageId)
95 $draft = $this->entityRepo->getById('page', $pageId, true);
96 $this->checkOwnablePermission('page-create', $draft->book);
97 $this->setPageTitle(trans('entities.pages_edit_draft'));
99 $draftsEnabled = $this->signedIn;
100 return view('pages/edit', [
102 'book' => $draft->book,
104 'draftsEnabled' => $draftsEnabled
109 * Store a new page by changing a draft into a page.
110 * @param Request $request
111 * @param string $bookSlug
115 public function store(Request $request, $bookSlug, $pageId)
117 $this->validate($request, [
118 'name' => 'required|string|max:255'
121 $input = $request->all();
122 $book = $this->entityRepo->getBySlug('book', $bookSlug);
124 $draftPage = $this->entityRepo->getById('page', $pageId, true);
126 $chapterId = intval($draftPage->chapter_id);
127 $parent = $chapterId !== 0 ? $this->entityRepo->getById('chapter', $chapterId) : $book;
128 $this->checkOwnablePermission('page-create', $parent);
130 if ($parent->isA('chapter')) {
131 $input['priority'] = $this->entityRepo->getNewChapterPriority($parent);
133 $input['priority'] = $this->entityRepo->getNewBookPriority($parent);
136 $page = $this->entityRepo->publishPageDraft($draftPage, $input);
138 Activity::add($page, 'page_create', $book->id);
139 return redirect($page->getUrl());
143 * Display the specified page.
144 * If the page is not found via the slug the revisions are searched for a match.
145 * @param string $bookSlug
146 * @param string $pageSlug
148 * @throws NotFoundException
150 public function show($bookSlug, $pageSlug)
153 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
154 } catch (NotFoundException $e) {
155 $page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
156 if ($page === null) {
159 return redirect($page->getUrl());
162 $this->checkOwnablePermission('page-view', $page);
164 $page->html = $this->entityRepo->renderPage($page);
165 $sidebarTree = $this->entityRepo->getBookChildren($page->book);
166 $pageNav = $this->entityRepo->getPageNav($page->html);
168 // check if the comment's are enabled
169 $commentsEnabled = !setting('app-disable-comments');
170 if ($commentsEnabled) {
171 $page->load(['comments.createdBy']);
175 $this->setPageTitle($page->getShortName());
176 return view('pages/show', [
177 'page' => $page,'book' => $page->book,
179 'sidebarTree' => $sidebarTree,
180 'commentsEnabled' => $commentsEnabled,
181 'pageNav' => $pageNav
186 * Get page from an ajax request.
188 * @return \Illuminate\Http\JsonResponse
190 public function getPageAjax($pageId)
192 $page = $this->entityRepo->getById('page', $pageId);
193 return response()->json($page);
197 * Show the form for editing the specified page.
198 * @param string $bookSlug
199 * @param string $pageSlug
202 public function edit($bookSlug, $pageSlug)
204 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
205 $this->checkOwnablePermission('page-update', $page);
206 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
207 $page->isDraft = false;
209 // Check for active editing
211 if ($this->entityRepo->isPageEditingActive($page, 60)) {
212 $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
215 // Check for a current draft version for this user
216 if ($this->entityRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
217 $draft = $this->entityRepo->getUserPageDraft($page, $this->currentUser->id);
218 $page->name = $draft->name;
219 $page->html = $draft->html;
220 $page->markdown = $draft->markdown;
221 $page->isDraft = true;
222 $warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
225 if (count($warnings) > 0) {
226 session()->flash('warning', implode("\n", $warnings));
229 $draftsEnabled = $this->signedIn;
230 return view('pages/edit', [
232 'book' => $page->book,
234 'draftsEnabled' => $draftsEnabled
239 * Update the specified page in storage.
240 * @param Request $request
241 * @param string $bookSlug
242 * @param string $pageSlug
245 public function update(Request $request, $bookSlug, $pageSlug)
247 $this->validate($request, [
248 'name' => 'required|string|max:255'
250 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
251 $this->checkOwnablePermission('page-update', $page);
252 $this->entityRepo->updatePage($page, $page->book->id, $request->all());
253 Activity::add($page, 'page_update', $page->book->id);
254 return redirect($page->getUrl());
258 * Save a draft update as a revision.
259 * @param Request $request
261 * @return \Illuminate\Http\JsonResponse
263 public function saveDraft(Request $request, $pageId)
265 $page = $this->entityRepo->getById('page', $pageId, true);
266 $this->checkOwnablePermission('page-update', $page);
268 if (!$this->signedIn) {
269 return response()->json([
271 'message' => trans('errors.guests_cannot_save_drafts'),
275 $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
277 $updateTime = $draft->updated_at->timestamp;
278 return response()->json([
279 'status' => 'success',
280 'message' => trans('entities.pages_edit_draft_save_at'),
281 'timestamp' => $updateTime
286 * Redirect from a special link url which
287 * uses the page id rather than the name.
289 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
291 public function redirectFromLink($pageId)
293 $page = $this->entityRepo->getById('page', $pageId);
294 return redirect($page->getUrl());
298 * Show the deletion page for the specified page.
299 * @param string $bookSlug
300 * @param string $pageSlug
301 * @return \Illuminate\View\View
303 public function showDelete($bookSlug, $pageSlug)
305 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
306 $this->checkOwnablePermission('page-delete', $page);
307 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
308 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
313 * Show the deletion page for the specified page.
314 * @param string $bookSlug
316 * @return \Illuminate\View\View
317 * @throws NotFoundException
319 public function showDeleteDraft($bookSlug, $pageId)
321 $page = $this->entityRepo->getById('page', $pageId, true);
322 $this->checkOwnablePermission('page-update', $page);
323 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
324 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
328 * Remove the specified page from storage.
329 * @param string $bookSlug
330 * @param string $pageSlug
332 * @internal param int $id
334 public function destroy($bookSlug, $pageSlug)
336 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
338 $this->checkOwnablePermission('page-delete', $page);
339 $this->entityRepo->destroyPage($page);
341 Activity::addMessage('page_delete', $book->id, $page->name);
342 session()->flash('success', trans('entities.pages_delete_success'));
343 return redirect($book->getUrl());
347 * Remove the specified draft page from storage.
348 * @param string $bookSlug
351 * @throws NotFoundException
353 public function destroyDraft($bookSlug, $pageId)
355 $page = $this->entityRepo->getById('page', $pageId, true);
357 $this->checkOwnablePermission('page-update', $page);
358 session()->flash('success', trans('entities.pages_delete_draft_success'));
359 $this->entityRepo->destroyPage($page);
360 return redirect($book->getUrl());
364 * Shows the last revisions for this page.
365 * @param string $bookSlug
366 * @param string $pageSlug
367 * @return \Illuminate\View\View
369 public function showRevisions($bookSlug, $pageSlug)
371 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
372 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
373 return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
377 * Shows a preview of a single revision
378 * @param string $bookSlug
379 * @param string $pageSlug
380 * @param int $revisionId
381 * @return \Illuminate\View\View
383 public function showRevision($bookSlug, $pageSlug, $revisionId)
385 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
386 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
387 if ($revision === null) {
391 $page->fill($revision->toArray());
392 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
394 return view('pages/revision', [
396 'book' => $page->book,
397 'revision' => $revision
402 * Shows the changes of a single revision
403 * @param string $bookSlug
404 * @param string $pageSlug
405 * @param int $revisionId
406 * @return \Illuminate\View\View
408 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
410 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
411 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
412 if ($revision === null) {
416 $prev = $revision->getPrevious();
417 $prevContent = ($prev === null) ? '' : $prev->html;
418 $diff = (new Htmldiff)->diff($prevContent, $revision->html);
420 $page->fill($revision->toArray());
421 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
423 return view('pages/revision', [
425 'book' => $page->book,
427 'revision' => $revision
432 * Restores a page using the content of the specified revision.
433 * @param string $bookSlug
434 * @param string $pageSlug
435 * @param int $revisionId
436 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
438 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
440 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
441 $this->checkOwnablePermission('page-update', $page);
442 $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
443 Activity::add($page, 'page_restore', $page->book->id);
444 return redirect($page->getUrl());
448 * Exports a page to a PDF.
449 * https://github.com/barryvdh/laravel-dompdf
450 * @param string $bookSlug
451 * @param string $pageSlug
452 * @return \Illuminate\Http\Response
454 public function exportPdf($bookSlug, $pageSlug)
456 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
457 $page->html = $this->entityRepo->renderPage($page);
458 $pdfContent = $this->exportService->pageToPdf($page);
459 return response()->make($pdfContent, 200, [
460 'Content-Type' => 'application/octet-stream',
461 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
466 * Export a page to a self-contained HTML file.
467 * @param string $bookSlug
468 * @param string $pageSlug
469 * @return \Illuminate\Http\Response
471 public function exportHtml($bookSlug, $pageSlug)
473 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
474 $page->html = $this->entityRepo->renderPage($page);
475 $containedHtml = $this->exportService->pageToContainedHtml($page);
476 return response()->make($containedHtml, 200, [
477 'Content-Type' => 'application/octet-stream',
478 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
483 * Export a page to a simple plaintext .txt file.
484 * @param string $bookSlug
485 * @param string $pageSlug
486 * @return \Illuminate\Http\Response
488 public function exportPlainText($bookSlug, $pageSlug)
490 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
491 $containedHtml = $this->exportService->pageToPlainText($page);
492 return response()->make($containedHtml, 200, [
493 'Content-Type' => 'application/octet-stream',
494 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
499 * Show a listing of recently created pages
500 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
502 public function showRecentlyCreated()
504 $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
505 return view('pages/detailed-listing', [
506 'title' => trans('entities.recently_created_pages'),
512 * Show a listing of recently created pages
513 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
515 public function showRecentlyUpdated()
517 $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
518 return view('pages/detailed-listing', [
519 'title' => trans('entities.recently_updated_pages'),
525 * Show the Restrictions view.
526 * @param string $bookSlug
527 * @param string $pageSlug
528 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
530 public function showRestrict($bookSlug, $pageSlug)
532 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
533 $this->checkOwnablePermission('restrictions-manage', $page);
534 $roles = $this->userRepo->getRestrictableRoles();
535 return view('pages/restrictions', [
542 * Show the view to choose a new parent to move a page into.
543 * @param string $bookSlug
544 * @param string $pageSlug
546 * @throws NotFoundException
548 public function showMove($bookSlug, $pageSlug)
550 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
551 $this->checkOwnablePermission('page-update', $page);
552 return view('pages/move', [
553 'book' => $page->book,
559 * Does the action of moving the location of a page
560 * @param string $bookSlug
561 * @param string $pageSlug
562 * @param Request $request
564 * @throws NotFoundException
566 public function move($bookSlug, $pageSlug, Request $request)
568 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
569 $this->checkOwnablePermission('page-update', $page);
571 $entitySelection = $request->get('entity_selection', null);
572 if ($entitySelection === null || $entitySelection === '') {
573 return redirect($page->getUrl());
576 $stringExploded = explode(':', $entitySelection);
577 $entityType = $stringExploded[0];
578 $entityId = intval($stringExploded[1]);
582 $parent = $this->entityRepo->getById($entityType, $entityId);
583 } catch (\Exception $e) {
584 session()->flash(trans('entities.selected_book_chapter_not_found'));
585 return redirect()->back();
588 $this->entityRepo->changePageParent($page, $parent);
589 Activity::add($page, 'page_move', $page->book->id);
590 session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
592 return redirect($page->getUrl());
596 * Set the permissions for this page.
597 * @param string $bookSlug
598 * @param string $pageSlug
599 * @param Request $request
600 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
602 public function restrict($bookSlug, $pageSlug, Request $request)
604 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
605 $this->checkOwnablePermission('restrictions-manage', $page);
606 $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
607 session()->flash('success', trans('entities.pages_permissions_success'));
608 return redirect($page->getUrl());