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
149 public function show($bookSlug, $pageSlug)
152 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
153 } catch (NotFoundException $e) {
154 $page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
155 if ($page === null) abort(404);
156 return redirect($page->getUrl());
159 $this->checkOwnablePermission('page-view', $page);
161 $page->html = $this->entityRepo->renderPage($page);
162 $sidebarTree = $this->entityRepo->getBookChildren($page->book);
163 $pageNav = $this->entityRepo->getPageNav($page->html);
165 // check if the comment's are enabled
166 $areCommentsEnabled = !setting('app-disable-comments');
167 if ($areCommentsEnabled) {
168 $page->load(['comments.createdBy']);
172 $this->setPageTitle($page->getShortName());
173 return view('pages/show', [
174 'page' => $page,'book' => $page->book,
175 'current' => $page, 'sidebarTree' => $sidebarTree,
176 'commentsEnabled' => $areCommentsEnabled,
177 'pageNav' => $pageNav]);
181 * Get page from an ajax request.
183 * @return \Illuminate\Http\JsonResponse
185 public function getPageAjax($pageId)
187 $page = $this->entityRepo->getById('page', $pageId);
188 return response()->json($page);
192 * Show the form for editing the specified page.
193 * @param string $bookSlug
194 * @param string $pageSlug
197 public function edit($bookSlug, $pageSlug)
199 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
200 $this->checkOwnablePermission('page-update', $page);
201 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
202 $page->isDraft = false;
204 // Check for active editing
206 if ($this->entityRepo->isPageEditingActive($page, 60)) {
207 $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
210 // Check for a current draft version for this user
211 if ($this->entityRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
212 $draft = $this->entityRepo->getUserPageDraft($page, $this->currentUser->id);
213 $page->name = $draft->name;
214 $page->html = $draft->html;
215 $page->markdown = $draft->markdown;
216 $page->isDraft = true;
217 $warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
220 if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
222 $draftsEnabled = $this->signedIn;
223 return view('pages/edit', [
225 'book' => $page->book,
227 'draftsEnabled' => $draftsEnabled
232 * Update the specified page in storage.
233 * @param Request $request
234 * @param string $bookSlug
235 * @param string $pageSlug
238 public function update(Request $request, $bookSlug, $pageSlug)
240 $this->validate($request, [
241 'name' => 'required|string|max:255'
243 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
244 $this->checkOwnablePermission('page-update', $page);
245 $this->entityRepo->updatePage($page, $page->book->id, $request->all());
246 Activity::add($page, 'page_update', $page->book->id);
247 return redirect($page->getUrl());
251 * Save a draft update as a revision.
252 * @param Request $request
254 * @return \Illuminate\Http\JsonResponse
256 public function saveDraft(Request $request, $pageId)
258 $page = $this->entityRepo->getById('page', $pageId, true);
259 $this->checkOwnablePermission('page-update', $page);
261 if (!$this->signedIn) {
262 return response()->json([
264 'message' => trans('errors.guests_cannot_save_drafts'),
268 $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
270 $updateTime = $draft->updated_at->timestamp;
271 $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
272 return response()->json([
273 'status' => 'success',
274 'message' => trans('entities.pages_edit_draft_save_at'),
275 'timestamp' => $utcUpdateTimestamp
280 * Redirect from a special link url which
281 * uses the page id rather than the name.
283 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
285 public function redirectFromLink($pageId)
287 $page = $this->entityRepo->getById('page', $pageId);
288 return redirect($page->getUrl());
292 * Show the deletion page for the specified page.
293 * @param string $bookSlug
294 * @param string $pageSlug
295 * @return \Illuminate\View\View
297 public function showDelete($bookSlug, $pageSlug)
299 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
300 $this->checkOwnablePermission('page-delete', $page);
301 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
302 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
307 * Show the deletion page for the specified page.
308 * @param string $bookSlug
310 * @return \Illuminate\View\View
311 * @throws NotFoundException
313 public function showDeleteDraft($bookSlug, $pageId)
315 $page = $this->entityRepo->getById('page', $pageId, true);
316 $this->checkOwnablePermission('page-update', $page);
317 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
318 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
322 * Remove the specified page from storage.
323 * @param string $bookSlug
324 * @param string $pageSlug
326 * @internal param int $id
328 public function destroy($bookSlug, $pageSlug)
330 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
332 $this->checkOwnablePermission('page-delete', $page);
333 $this->entityRepo->destroyPage($page);
335 Activity::addMessage('page_delete', $book->id, $page->name);
336 session()->flash('success', trans('entities.pages_delete_success'));
337 return redirect($book->getUrl());
341 * Remove the specified draft page from storage.
342 * @param string $bookSlug
345 * @throws NotFoundException
347 public function destroyDraft($bookSlug, $pageId)
349 $page = $this->entityRepo->getById('page', $pageId, true);
351 $this->checkOwnablePermission('page-update', $page);
352 session()->flash('success', trans('entities.pages_delete_draft_success'));
353 $this->entityRepo->destroyPage($page);
354 return redirect($book->getUrl());
358 * Shows the last revisions for this page.
359 * @param string $bookSlug
360 * @param string $pageSlug
361 * @return \Illuminate\View\View
363 public function showRevisions($bookSlug, $pageSlug)
365 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
366 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
367 return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
371 * Shows a preview of a single revision
372 * @param string $bookSlug
373 * @param string $pageSlug
374 * @param int $revisionId
375 * @return \Illuminate\View\View
377 public function showRevision($bookSlug, $pageSlug, $revisionId)
379 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
380 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
381 if ($revision === null) {
385 $page->fill($revision->toArray());
386 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
388 return view('pages/revision', [
390 'book' => $page->book,
391 'revision' => $revision
396 * Shows the changes of a single revision
397 * @param string $bookSlug
398 * @param string $pageSlug
399 * @param int $revisionId
400 * @return \Illuminate\View\View
402 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
404 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
405 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
406 if ($revision === null) {
410 $prev = $revision->getPrevious();
411 $prevContent = ($prev === null) ? '' : $prev->html;
412 $diff = (new Htmldiff)->diff($prevContent, $revision->html);
414 $page->fill($revision->toArray());
415 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
417 return view('pages/revision', [
419 'book' => $page->book,
421 'revision' => $revision
426 * Restores a page using the content of the specified revision.
427 * @param string $bookSlug
428 * @param string $pageSlug
429 * @param int $revisionId
430 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
432 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
434 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
435 $this->checkOwnablePermission('page-update', $page);
436 $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
437 Activity::add($page, 'page_restore', $page->book->id);
438 return redirect($page->getUrl());
442 * Exports a page to a PDF.
443 * https://github.com/barryvdh/laravel-dompdf
444 * @param string $bookSlug
445 * @param string $pageSlug
446 * @return \Illuminate\Http\Response
448 public function exportPdf($bookSlug, $pageSlug)
450 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
451 $page->html = $this->entityRepo->renderPage($page);
452 $pdfContent = $this->exportService->pageToPdf($page);
453 return response()->make($pdfContent, 200, [
454 'Content-Type' => 'application/octet-stream',
455 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
460 * Export a page to a self-contained HTML file.
461 * @param string $bookSlug
462 * @param string $pageSlug
463 * @return \Illuminate\Http\Response
465 public function exportHtml($bookSlug, $pageSlug)
467 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
468 $page->html = $this->entityRepo->renderPage($page);
469 $containedHtml = $this->exportService->pageToContainedHtml($page);
470 return response()->make($containedHtml, 200, [
471 'Content-Type' => 'application/octet-stream',
472 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
477 * Export a page to a simple plaintext .txt file.
478 * @param string $bookSlug
479 * @param string $pageSlug
480 * @return \Illuminate\Http\Response
482 public function exportPlainText($bookSlug, $pageSlug)
484 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
485 $containedHtml = $this->exportService->pageToPlainText($page);
486 return response()->make($containedHtml, 200, [
487 'Content-Type' => 'application/octet-stream',
488 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
493 * Show a listing of recently created pages
494 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
496 public function showRecentlyCreated()
498 $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
499 return view('pages/detailed-listing', [
500 'title' => trans('entities.recently_created_pages'),
506 * Show a listing of recently created pages
507 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
509 public function showRecentlyUpdated()
511 $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
512 return view('pages/detailed-listing', [
513 'title' => trans('entities.recently_updated_pages'),
519 * Show the Restrictions view.
520 * @param string $bookSlug
521 * @param string $pageSlug
522 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
524 public function showRestrict($bookSlug, $pageSlug)
526 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
527 $this->checkOwnablePermission('restrictions-manage', $page);
528 $roles = $this->userRepo->getRestrictableRoles();
529 return view('pages/restrictions', [
536 * Show the view to choose a new parent to move a page into.
537 * @param string $bookSlug
538 * @param string $pageSlug
540 * @throws NotFoundException
542 public function showMove($bookSlug, $pageSlug)
544 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
545 $this->checkOwnablePermission('page-update', $page);
546 return view('pages/move', [
547 'book' => $page->book,
553 * Does the action of moving the location of a page
554 * @param string $bookSlug
555 * @param string $pageSlug
556 * @param Request $request
558 * @throws NotFoundException
560 public function move($bookSlug, $pageSlug, Request $request)
562 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
563 $this->checkOwnablePermission('page-update', $page);
565 $entitySelection = $request->get('entity_selection', null);
566 if ($entitySelection === null || $entitySelection === '') {
567 return redirect($page->getUrl());
570 $stringExploded = explode(':', $entitySelection);
571 $entityType = $stringExploded[0];
572 $entityId = intval($stringExploded[1]);
576 $parent = $this->entityRepo->getById($entityType, $entityId);
577 } catch (\Exception $e) {
578 session()->flash(trans('entities.selected_book_chapter_not_found'));
579 return redirect()->back();
582 $this->entityRepo->changePageParent($page, $parent);
583 Activity::add($page, 'page_move', $page->book->id);
584 session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
586 return redirect($page->getUrl());
590 * Set the permissions for this page.
591 * @param string $bookSlug
592 * @param string $pageSlug
593 * @param Request $request
594 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
596 public function restrict($bookSlug, $pageSlug, Request $request)
598 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
599 $this->checkOwnablePermission('restrictions-manage', $page);
600 $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
601 session()->flash('success', trans('entities.pages_permissions_success'));
602 return redirect($page->getUrl());