1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Repos\UserRepo;
6 use BookStack\Services\ExportService;
8 use Illuminate\Http\Request;
9 use BookStack\Http\Requests;
10 use BookStack\Repos\BookRepo;
11 use BookStack\Repos\ChapterRepo;
12 use BookStack\Repos\PageRepo;
13 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 use GatherContent\Htmldiff\Htmldiff;
17 class PageController extends Controller
22 protected $chapterRepo;
23 protected $exportService;
27 * PageController constructor.
28 * @param PageRepo $pageRepo
29 * @param BookRepo $bookRepo
30 * @param ChapterRepo $chapterRepo
31 * @param ExportService $exportService
32 * @param UserRepo $userRepo
34 public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo)
36 $this->pageRepo = $pageRepo;
37 $this->bookRepo = $bookRepo;
38 $this->chapterRepo = $chapterRepo;
39 $this->exportService = $exportService;
40 $this->userRepo = $userRepo;
41 parent::__construct();
45 * Show the form for creating a new page.
46 * @param string $bookSlug
47 * @param bool $chapterSlug
49 * @internal param bool $pageSlug
51 public function create($bookSlug, $chapterSlug = false)
53 $book = $this->bookRepo->getBySlug($bookSlug);
54 $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
55 $parent = $chapter ? $chapter : $book;
56 $this->checkOwnablePermission('page-create', $parent);
57 $this->setPageTitle('Create New Page');
59 $draft = $this->pageRepo->getDraftPage($book, $chapter);
60 return redirect($draft->getUrl());
64 * Show form to continue editing a draft page.
65 * @param string $bookSlug
67 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
69 public function editDraft($bookSlug, $pageId)
71 $book = $this->bookRepo->getBySlug($bookSlug);
72 $draft = $this->pageRepo->getById($pageId, true);
73 $this->checkOwnablePermission('page-create', $book);
74 $this->setPageTitle('Edit Page Draft');
76 return view('pages/edit', ['page' => $draft, 'book' => $book, 'isDraft' => true]);
80 * Store a new page by changing a draft into a page.
81 * @param Request $request
82 * @param string $bookSlug
85 public function store(Request $request, $bookSlug, $pageId)
87 $this->validate($request, [
88 'name' => 'required|string|max:255'
91 $input = $request->all();
92 $book = $this->bookRepo->getBySlug($bookSlug);
94 $draftPage = $this->pageRepo->getById($pageId, true);
96 $chapterId = intval($draftPage->chapter_id);
97 $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
98 $this->checkOwnablePermission('page-create', $parent);
100 if ($parent->isA('chapter')) {
101 $input['priority'] = $this->chapterRepo->getNewPriority($parent);
103 $input['priority'] = $this->bookRepo->getNewPriority($parent);
106 $page = $this->pageRepo->publishDraft($draftPage, $input);
108 Activity::add($page, 'page_create', $book->id);
109 return redirect($page->getUrl());
113 * Display the specified page.
114 * If the page is not found via the slug the
115 * revisions are searched for a match.
116 * @param string $bookSlug
117 * @param string $pageSlug
120 public function show($bookSlug, $pageSlug)
122 $book = $this->bookRepo->getBySlug($bookSlug);
125 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
126 } catch (NotFoundException $e) {
127 $page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
128 if ($page === null) abort(404);
129 return redirect($page->getUrl());
132 $this->checkOwnablePermission('page-view', $page);
134 $sidebarTree = $this->bookRepo->getChildren($book);
135 $pageNav = $this->pageRepo->getPageNav($page);
138 $this->setPageTitle($page->getShortName());
139 return view('pages/show', ['page' => $page, 'book' => $book,
140 'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
144 * Get page from an ajax request.
146 * @return \Illuminate\Http\JsonResponse
148 public function getPageAjax($pageId)
150 $page = $this->pageRepo->getById($pageId);
151 return response()->json($page);
155 * Show the form for editing the specified page.
156 * @param string $bookSlug
157 * @param string $pageSlug
160 public function edit($bookSlug, $pageSlug)
162 $book = $this->bookRepo->getBySlug($bookSlug);
163 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
164 $this->checkOwnablePermission('page-update', $page);
165 $this->setPageTitle('Editing Page ' . $page->getShortName());
166 $page->isDraft = false;
168 // Check for active editing
170 if ($this->pageRepo->isPageEditingActive($page, 60)) {
171 $warnings[] = $this->pageRepo->getPageEditingActiveMessage($page, 60);
174 // Check for a current draft version for this user
175 if ($this->pageRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
176 $draft = $this->pageRepo->getUserPageDraft($page, $this->currentUser->id);
177 $page->name = $draft->name;
178 $page->html = $draft->html;
179 $page->markdown = $draft->markdown;
180 $page->isDraft = true;
181 $warnings [] = $this->pageRepo->getUserPageDraftMessage($draft);
184 if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
186 return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]);
190 * Update the specified page in storage.
191 * @param Request $request
192 * @param string $bookSlug
193 * @param string $pageSlug
196 public function update(Request $request, $bookSlug, $pageSlug)
198 $this->validate($request, [
199 'name' => 'required|string|max:255'
201 $book = $this->bookRepo->getBySlug($bookSlug);
202 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
203 $this->checkOwnablePermission('page-update', $page);
204 $this->pageRepo->updatePage($page, $book->id, $request->all());
205 Activity::add($page, 'page_update', $book->id);
206 return redirect($page->getUrl());
210 * Save a draft update as a revision.
211 * @param Request $request
213 * @return \Illuminate\Http\JsonResponse
215 public function saveDraft(Request $request, $pageId)
217 $page = $this->pageRepo->getById($pageId, true);
218 $this->checkOwnablePermission('page-update', $page);
220 $draft = $this->pageRepo->updateDraftPage($page, $request->only(['name', 'html', 'markdown']));
222 $draft = $this->pageRepo->saveUpdateDraft($page, $request->only(['name', 'html', 'markdown']));
225 $updateTime = $draft->updated_at->timestamp;
226 $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
227 return response()->json([
228 'status' => 'success',
229 'message' => 'Draft saved at ',
230 'timestamp' => $utcUpdateTimestamp
235 * Redirect from a special link url which
236 * uses the page id rather than the name.
238 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
240 public function redirectFromLink($pageId)
242 $page = $this->pageRepo->getById($pageId);
243 return redirect($page->getUrl());
247 * Show the deletion page for the specified page.
248 * @param string $bookSlug
249 * @param string $pageSlug
250 * @return \Illuminate\View\View
252 public function showDelete($bookSlug, $pageSlug)
254 $book = $this->bookRepo->getBySlug($bookSlug);
255 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
256 $this->checkOwnablePermission('page-delete', $page);
257 $this->setPageTitle('Delete Page ' . $page->getShortName());
258 return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
263 * Show the deletion page for the specified page.
264 * @param string $bookSlug
266 * @return \Illuminate\View\View
267 * @throws NotFoundException
269 public function showDeleteDraft($bookSlug, $pageId)
271 $book = $this->bookRepo->getBySlug($bookSlug);
272 $page = $this->pageRepo->getById($pageId, true);
273 $this->checkOwnablePermission('page-update', $page);
274 $this->setPageTitle('Delete Draft Page ' . $page->getShortName());
275 return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
279 * Remove the specified page from storage.
280 * @param string $bookSlug
281 * @param string $pageSlug
283 * @internal param int $id
285 public function destroy($bookSlug, $pageSlug)
287 $book = $this->bookRepo->getBySlug($bookSlug);
288 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
289 $this->checkOwnablePermission('page-delete', $page);
290 Activity::addMessage('page_delete', $book->id, $page->name);
291 session()->flash('success', 'Page deleted');
292 $this->pageRepo->destroy($page);
293 return redirect($book->getUrl());
297 * Remove the specified draft page from storage.
298 * @param string $bookSlug
301 * @throws NotFoundException
303 public function destroyDraft($bookSlug, $pageId)
305 $book = $this->bookRepo->getBySlug($bookSlug);
306 $page = $this->pageRepo->getById($pageId, true);
307 $this->checkOwnablePermission('page-update', $page);
308 session()->flash('success', 'Draft deleted');
309 $this->pageRepo->destroy($page);
310 return redirect($book->getUrl());
314 * Shows the last revisions for this page.
315 * @param string $bookSlug
316 * @param string $pageSlug
317 * @return \Illuminate\View\View
319 public function showRevisions($bookSlug, $pageSlug)
321 $book = $this->bookRepo->getBySlug($bookSlug);
322 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
323 $this->setPageTitle('Revisions For ' . $page->getShortName());
324 return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]);
328 * Shows a preview of a single revision
329 * @param string $bookSlug
330 * @param string $pageSlug
331 * @param int $revisionId
332 * @return \Illuminate\View\View
334 public function showRevision($bookSlug, $pageSlug, $revisionId)
336 $book = $this->bookRepo->getBySlug($bookSlug);
337 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
338 $revision = $this->pageRepo->getRevisionById($revisionId);
340 $next = $revision->getNext() ?: $page;
341 $diff = (new Htmldiff)->diff($revision->html, $next->html);
343 $page->fill($revision->toArray());
344 $this->setPageTitle('Page Revision For ' . $page->getShortName());
346 return view('pages/revision', [
354 * Restores a page using the content of the specified revision.
355 * @param string $bookSlug
356 * @param string $pageSlug
357 * @param int $revisionId
358 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
360 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
362 $book = $this->bookRepo->getBySlug($bookSlug);
363 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
364 $this->checkOwnablePermission('page-update', $page);
365 $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
366 Activity::add($page, 'page_restore', $book->id);
367 return redirect($page->getUrl());
371 * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
372 * https://github.com/barryvdh/laravel-dompdf
373 * @param string $bookSlug
374 * @param string $pageSlug
375 * @return \Illuminate\Http\Response
377 public function exportPdf($bookSlug, $pageSlug)
379 $book = $this->bookRepo->getBySlug($bookSlug);
380 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
381 $pdfContent = $this->exportService->pageToPdf($page);
382 return response()->make($pdfContent, 200, [
383 'Content-Type' => 'application/octet-stream',
384 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
389 * Export a page to a self-contained HTML file.
390 * @param string $bookSlug
391 * @param string $pageSlug
392 * @return \Illuminate\Http\Response
394 public function exportHtml($bookSlug, $pageSlug)
396 $book = $this->bookRepo->getBySlug($bookSlug);
397 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
398 $containedHtml = $this->exportService->pageToContainedHtml($page);
399 return response()->make($containedHtml, 200, [
400 'Content-Type' => 'application/octet-stream',
401 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
406 * Export a page to a simple plaintext .txt file.
407 * @param string $bookSlug
408 * @param string $pageSlug
409 * @return \Illuminate\Http\Response
411 public function exportPlainText($bookSlug, $pageSlug)
413 $book = $this->bookRepo->getBySlug($bookSlug);
414 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
415 $containedHtml = $this->exportService->pageToPlainText($page);
416 return response()->make($containedHtml, 200, [
417 'Content-Type' => 'application/octet-stream',
418 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
423 * Show a listing of recently created pages
424 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
426 public function showRecentlyCreated()
428 $pages = $this->pageRepo->getRecentlyCreatedPaginated(20)->setPath(baseUrl('/pages/recently-created'));
429 return view('pages/detailed-listing', [
430 'title' => 'Recently Created Pages',
436 * Show a listing of recently created pages
437 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
439 public function showRecentlyUpdated()
441 $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20)->setPath(baseUrl('/pages/recently-updated'));
442 return view('pages/detailed-listing', [
443 'title' => 'Recently Updated Pages',
449 * Show the Restrictions view.
450 * @param string $bookSlug
451 * @param string $pageSlug
452 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
454 public function showRestrict($bookSlug, $pageSlug)
456 $book = $this->bookRepo->getBySlug($bookSlug);
457 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
458 $this->checkOwnablePermission('restrictions-manage', $page);
459 $roles = $this->userRepo->getRestrictableRoles();
460 return view('pages/restrictions', [
467 * Show the view to choose a new parent to move a page into.
468 * @param string $bookSlug
469 * @param string $pageSlug
471 * @throws NotFoundException
473 public function showMove($bookSlug, $pageSlug)
475 $book = $this->bookRepo->getBySlug($bookSlug);
476 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
477 $this->checkOwnablePermission('page-update', $page);
478 return view('pages/move', [
485 * Does the action of moving the location of a page
486 * @param string $bookSlug
487 * @param string $pageSlug
488 * @param Request $request
490 * @throws NotFoundException
492 public function move($bookSlug, $pageSlug, Request $request)
494 $book = $this->bookRepo->getBySlug($bookSlug);
495 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
496 $this->checkOwnablePermission('page-update', $page);
498 $entitySelection = $request->get('entity_selection', null);
499 if ($entitySelection === null || $entitySelection === '') {
500 return redirect($page->getUrl());
503 $stringExploded = explode(':', $entitySelection);
504 $entityType = $stringExploded[0];
505 $entityId = intval($stringExploded[1]);
509 if ($entityType == 'chapter') {
510 $parent = $this->chapterRepo->getById($entityId);
511 } else if ($entityType == 'book') {
512 $parent = $this->bookRepo->getById($entityId);
515 if ($parent === false || $parent === null) {
516 session()->flash('The selected Book or Chapter was not found');
517 return redirect()->back();
520 $this->pageRepo->changePageParent($page, $parent);
521 Activity::add($page, 'page_move', $page->book->id);
522 session()->flash('success', sprintf('Page moved to "%s"', $parent->name));
524 return redirect($page->getUrl());
528 * Set the permissions for this page.
529 * @param string $bookSlug
530 * @param string $pageSlug
531 * @param Request $request
532 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
534 public function restrict($bookSlug, $pageSlug, Request $request)
536 $book = $this->bookRepo->getBySlug($bookSlug);
537 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
538 $this->checkOwnablePermission('restrictions-manage', $page);
539 $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
540 session()->flash('success', 'Page Permissions Updated');
541 return redirect($page->getUrl());