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);
164 $page->load(['comments.createdBy']);
167 $this->setPageTitle($page->getShortName());
168 return view('pages/show', [
169 'page' => $page,'book' => $page->book,
170 'current' => $page, 'sidebarTree' => $sidebarTree,
171 'pageNav' => $pageNav]);
175 * Get page from an ajax request.
177 * @return \Illuminate\Http\JsonResponse
179 public function getPageAjax($pageId)
181 $page = $this->entityRepo->getById('page', $pageId);
182 return response()->json($page);
186 * Show the form for editing the specified page.
187 * @param string $bookSlug
188 * @param string $pageSlug
191 public function edit($bookSlug, $pageSlug)
193 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
194 $this->checkOwnablePermission('page-update', $page);
195 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
196 $page->isDraft = false;
198 // Check for active editing
200 if ($this->entityRepo->isPageEditingActive($page, 60)) {
201 $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
204 // Check for a current draft version for this user
205 if ($this->entityRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
206 $draft = $this->entityRepo->getUserPageDraft($page, $this->currentUser->id);
207 $page->name = $draft->name;
208 $page->html = $draft->html;
209 $page->markdown = $draft->markdown;
210 $page->isDraft = true;
211 $warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
214 if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
216 $draftsEnabled = $this->signedIn;
217 return view('pages/edit', [
219 'book' => $page->book,
221 'draftsEnabled' => $draftsEnabled
226 * Update the specified page in storage.
227 * @param Request $request
228 * @param string $bookSlug
229 * @param string $pageSlug
232 public function update(Request $request, $bookSlug, $pageSlug)
234 $this->validate($request, [
235 'name' => 'required|string|max:255'
237 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
238 $this->checkOwnablePermission('page-update', $page);
239 $this->entityRepo->updatePage($page, $page->book->id, $request->all());
240 Activity::add($page, 'page_update', $page->book->id);
241 return redirect($page->getUrl());
245 * Save a draft update as a revision.
246 * @param Request $request
248 * @return \Illuminate\Http\JsonResponse
250 public function saveDraft(Request $request, $pageId)
252 $page = $this->entityRepo->getById('page', $pageId, true);
253 $this->checkOwnablePermission('page-update', $page);
255 if (!$this->signedIn) {
256 return response()->json([
258 'message' => trans('errors.guests_cannot_save_drafts'),
262 $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
264 $updateTime = $draft->updated_at->timestamp;
265 $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
266 return response()->json([
267 'status' => 'success',
268 'message' => trans('entities.pages_edit_draft_save_at'),
269 'timestamp' => $utcUpdateTimestamp
274 * Redirect from a special link url which
275 * uses the page id rather than the name.
277 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
279 public function redirectFromLink($pageId)
281 $page = $this->entityRepo->getById('page', $pageId);
282 return redirect($page->getUrl());
286 * Show the deletion page for the specified page.
287 * @param string $bookSlug
288 * @param string $pageSlug
289 * @return \Illuminate\View\View
291 public function showDelete($bookSlug, $pageSlug)
293 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
294 $this->checkOwnablePermission('page-delete', $page);
295 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
296 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
301 * Show the deletion page for the specified page.
302 * @param string $bookSlug
304 * @return \Illuminate\View\View
305 * @throws NotFoundException
307 public function showDeleteDraft($bookSlug, $pageId)
309 $page = $this->entityRepo->getById('page', $pageId, true);
310 $this->checkOwnablePermission('page-update', $page);
311 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
312 return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
316 * Remove the specified page from storage.
317 * @param string $bookSlug
318 * @param string $pageSlug
320 * @internal param int $id
322 public function destroy($bookSlug, $pageSlug)
324 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
326 $this->checkOwnablePermission('page-delete', $page);
327 $this->entityRepo->destroyPage($page);
329 Activity::addMessage('page_delete', $book->id, $page->name);
330 session()->flash('success', trans('entities.pages_delete_success'));
331 return redirect($book->getUrl());
335 * Remove the specified draft page from storage.
336 * @param string $bookSlug
339 * @throws NotFoundException
341 public function destroyDraft($bookSlug, $pageId)
343 $page = $this->entityRepo->getById('page', $pageId, true);
345 $this->checkOwnablePermission('page-update', $page);
346 session()->flash('success', trans('entities.pages_delete_draft_success'));
347 $this->entityRepo->destroyPage($page);
348 return redirect($book->getUrl());
352 * Shows the last revisions for this page.
353 * @param string $bookSlug
354 * @param string $pageSlug
355 * @return \Illuminate\View\View
357 public function showRevisions($bookSlug, $pageSlug)
359 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
360 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
361 return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
365 * Shows a preview of a single revision
366 * @param string $bookSlug
367 * @param string $pageSlug
368 * @param int $revisionId
369 * @return \Illuminate\View\View
371 public function showRevision($bookSlug, $pageSlug, $revisionId)
373 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
374 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
375 if ($revision === null) {
379 $page->fill($revision->toArray());
380 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
382 return view('pages/revision', [
384 'book' => $page->book,
385 'revision' => $revision
390 * Shows the changes of a single revision
391 * @param string $bookSlug
392 * @param string $pageSlug
393 * @param int $revisionId
394 * @return \Illuminate\View\View
396 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
398 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
399 $revision = $page->revisions()->where('id', '=', $revisionId)->first();
400 if ($revision === null) {
404 $prev = $revision->getPrevious();
405 $prevContent = ($prev === null) ? '' : $prev->html;
406 $diff = (new Htmldiff)->diff($prevContent, $revision->html);
408 $page->fill($revision->toArray());
409 $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
411 return view('pages/revision', [
413 'book' => $page->book,
415 'revision' => $revision
420 * Restores a page using the content of the specified revision.
421 * @param string $bookSlug
422 * @param string $pageSlug
423 * @param int $revisionId
424 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
426 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
428 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
429 $this->checkOwnablePermission('page-update', $page);
430 $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
431 Activity::add($page, 'page_restore', $page->book->id);
432 return redirect($page->getUrl());
436 * Exports a page to a PDF.
437 * https://github.com/barryvdh/laravel-dompdf
438 * @param string $bookSlug
439 * @param string $pageSlug
440 * @return \Illuminate\Http\Response
442 public function exportPdf($bookSlug, $pageSlug)
444 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
445 $page->html = $this->entityRepo->renderPage($page);
446 $pdfContent = $this->exportService->pageToPdf($page);
447 return response()->make($pdfContent, 200, [
448 'Content-Type' => 'application/octet-stream',
449 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
454 * Export a page to a self-contained HTML file.
455 * @param string $bookSlug
456 * @param string $pageSlug
457 * @return \Illuminate\Http\Response
459 public function exportHtml($bookSlug, $pageSlug)
461 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
462 $page->html = $this->entityRepo->renderPage($page);
463 $containedHtml = $this->exportService->pageToContainedHtml($page);
464 return response()->make($containedHtml, 200, [
465 'Content-Type' => 'application/octet-stream',
466 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
471 * Export a page to a simple plaintext .txt file.
472 * @param string $bookSlug
473 * @param string $pageSlug
474 * @return \Illuminate\Http\Response
476 public function exportPlainText($bookSlug, $pageSlug)
478 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
479 $containedHtml = $this->exportService->pageToPlainText($page);
480 return response()->make($containedHtml, 200, [
481 'Content-Type' => 'application/octet-stream',
482 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
487 * Show a listing of recently created pages
488 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
490 public function showRecentlyCreated()
492 $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
493 return view('pages/detailed-listing', [
494 'title' => trans('entities.recently_created_pages'),
500 * Show a listing of recently created pages
501 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
503 public function showRecentlyUpdated()
505 $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
506 return view('pages/detailed-listing', [
507 'title' => trans('entities.recently_updated_pages'),
513 * Show the Restrictions view.
514 * @param string $bookSlug
515 * @param string $pageSlug
516 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
518 public function showRestrict($bookSlug, $pageSlug)
520 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
521 $this->checkOwnablePermission('restrictions-manage', $page);
522 $roles = $this->userRepo->getRestrictableRoles();
523 return view('pages/restrictions', [
530 * Show the view to choose a new parent to move a page into.
531 * @param string $bookSlug
532 * @param string $pageSlug
534 * @throws NotFoundException
536 public function showMove($bookSlug, $pageSlug)
538 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
539 $this->checkOwnablePermission('page-update', $page);
540 return view('pages/move', [
541 'book' => $page->book,
547 * Does the action of moving the location of a page
548 * @param string $bookSlug
549 * @param string $pageSlug
550 * @param Request $request
552 * @throws NotFoundException
554 public function move($bookSlug, $pageSlug, Request $request)
556 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
557 $this->checkOwnablePermission('page-update', $page);
559 $entitySelection = $request->get('entity_selection', null);
560 if ($entitySelection === null || $entitySelection === '') {
561 return redirect($page->getUrl());
564 $stringExploded = explode(':', $entitySelection);
565 $entityType = $stringExploded[0];
566 $entityId = intval($stringExploded[1]);
570 $parent = $this->entityRepo->getById($entityType, $entityId);
571 } catch (\Exception $e) {
572 session()->flash(trans('entities.selected_book_chapter_not_found'));
573 return redirect()->back();
576 $this->entityRepo->changePageParent($page, $parent);
577 Activity::add($page, 'page_move', $page->book->id);
578 session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
580 return redirect($page->getUrl());
584 * Set the permissions for this page.
585 * @param string $bookSlug
586 * @param string $pageSlug
587 * @param Request $request
588 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
590 public function restrict($bookSlug, $pageSlug, Request $request)
592 $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
593 $this->checkOwnablePermission('restrictions-manage', $page);
594 $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
595 session()->flash('success', trans('entities.pages_permissions_success'));
596 return redirect($page->getUrl());