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;
16 class PageController extends Controller
21 protected $chapterRepo;
22 protected $exportService;
26 * PageController constructor.
27 * @param PageRepo $pageRepo
28 * @param BookRepo $bookRepo
29 * @param ChapterRepo $chapterRepo
30 * @param ExportService $exportService
31 * @param UserRepo $userRepo
33 public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo)
35 $this->pageRepo = $pageRepo;
36 $this->bookRepo = $bookRepo;
37 $this->chapterRepo = $chapterRepo;
38 $this->exportService = $exportService;
39 $this->userRepo = $userRepo;
40 parent::__construct();
44 * Show the form for creating a new page.
45 * @param string $bookSlug
46 * @param bool $chapterSlug
48 * @internal param bool $pageSlug
50 public function create($bookSlug, $chapterSlug = false)
52 $book = $this->bookRepo->getBySlug($bookSlug);
53 $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
54 $parent = $chapter ? $chapter : $book;
55 $this->checkOwnablePermission('page-create', $parent);
56 $this->setPageTitle('Create New Page');
58 $draft = $this->pageRepo->getDraftPage($book, $chapter);
59 return redirect($draft->getUrl());
63 * Show form to continue editing a draft page.
64 * @param string $bookSlug
66 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
68 public function editDraft($bookSlug, $pageId)
70 $book = $this->bookRepo->getBySlug($bookSlug);
71 $draft = $this->pageRepo->getById($pageId, true);
72 $this->checkOwnablePermission('page-create', $book);
73 $this->setPageTitle('Edit Page Draft');
75 return view('pages/edit', ['page' => $draft, 'book' => $book, 'isDraft' => true]);
79 * Store a new page by changing a draft into a page.
80 * @param Request $request
81 * @param string $bookSlug
84 public function store(Request $request, $bookSlug, $pageId)
86 $this->validate($request, [
87 'name' => 'required|string|max:255'
90 $input = $request->all();
91 $book = $this->bookRepo->getBySlug($bookSlug);
93 $draftPage = $this->pageRepo->getById($pageId, true);
95 $chapterId = intval($draftPage->chapter_id);
96 $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
97 $this->checkOwnablePermission('page-create', $parent);
99 if ($parent->isA('chapter')) {
100 $input['priority'] = $this->chapterRepo->getNewPriority($parent);
102 $input['priority'] = $this->bookRepo->getNewPriority($parent);
105 $page = $this->pageRepo->publishDraft($draftPage, $input);
107 Activity::add($page, 'page_create', $book->id);
108 return redirect($page->getUrl());
112 * Display the specified page.
113 * If the page is not found via the slug the
114 * revisions are searched for a match.
115 * @param string $bookSlug
116 * @param string $pageSlug
119 public function show($bookSlug, $pageSlug)
121 $book = $this->bookRepo->getBySlug($bookSlug);
124 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
125 } catch (NotFoundException $e) {
126 $page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
127 if ($page === null) abort(404);
128 return redirect($page->getUrl());
131 $this->checkOwnablePermission('page-view', $page);
133 $sidebarTree = $this->bookRepo->getChildren($book);
134 $pageNav = $this->pageRepo->getPageNav($page);
137 $this->setPageTitle($page->getShortName());
138 return view('pages/show', ['page' => $page, 'book' => $book,
139 'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
143 * Get page from an ajax request.
145 * @return \Illuminate\Http\JsonResponse
147 public function getPageAjax($pageId)
149 $page = $this->pageRepo->getById($pageId);
150 return response()->json($page);
154 * Show the form for editing the specified page.
155 * @param string $bookSlug
156 * @param string $pageSlug
159 public function edit($bookSlug, $pageSlug)
161 $book = $this->bookRepo->getBySlug($bookSlug);
162 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
163 $this->checkOwnablePermission('page-update', $page);
164 $this->setPageTitle('Editing Page ' . $page->getShortName());
165 $page->isDraft = false;
167 // Check for active editing
169 if ($this->pageRepo->isPageEditingActive($page, 60)) {
170 $warnings[] = $this->pageRepo->getPageEditingActiveMessage($page, 60);
173 // Check for a current draft version for this user
174 if ($this->pageRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
175 $draft = $this->pageRepo->getUserPageDraft($page, $this->currentUser->id);
176 $page->name = $draft->name;
177 $page->html = $draft->html;
178 $page->markdown = $draft->markdown;
179 $page->isDraft = true;
180 $warnings [] = $this->pageRepo->getUserPageDraftMessage($draft);
183 if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
185 return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]);
189 * Update the specified page in storage.
190 * @param Request $request
191 * @param string $bookSlug
192 * @param string $pageSlug
195 public function update(Request $request, $bookSlug, $pageSlug)
197 $this->validate($request, [
198 'name' => 'required|string|max:255'
200 $book = $this->bookRepo->getBySlug($bookSlug);
201 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
202 $this->checkOwnablePermission('page-update', $page);
203 $this->pageRepo->updatePage($page, $book->id, $request->all());
204 Activity::add($page, 'page_update', $book->id);
205 return redirect($page->getUrl());
209 * Save a draft update as a revision.
210 * @param Request $request
212 * @return \Illuminate\Http\JsonResponse
214 public function saveDraft(Request $request, $pageId)
216 $page = $this->pageRepo->getById($pageId, true);
217 $this->checkOwnablePermission('page-update', $page);
219 $draft = $this->pageRepo->updateDraftPage($page, $request->only(['name', 'html', 'markdown']));
221 $draft = $this->pageRepo->saveUpdateDraft($page, $request->only(['name', 'html', 'markdown']));
224 $updateTime = $draft->updated_at->timestamp;
225 $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
226 return response()->json([
227 'status' => 'success',
228 'message' => 'Draft saved at ',
229 'timestamp' => $utcUpdateTimestamp
234 * Redirect from a special link url which
235 * uses the page id rather than the name.
237 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
239 public function redirectFromLink($pageId)
241 $page = $this->pageRepo->getById($pageId);
242 return redirect($page->getUrl());
246 * Show the deletion page for the specified page.
247 * @param string $bookSlug
248 * @param string $pageSlug
249 * @return \Illuminate\View\View
251 public function showDelete($bookSlug, $pageSlug)
253 $book = $this->bookRepo->getBySlug($bookSlug);
254 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
255 $this->checkOwnablePermission('page-delete', $page);
256 $this->setPageTitle('Delete Page ' . $page->getShortName());
257 return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
262 * Show the deletion page for the specified page.
263 * @param string $bookSlug
265 * @return \Illuminate\View\View
266 * @throws NotFoundException
268 public function showDeleteDraft($bookSlug, $pageId)
270 $book = $this->bookRepo->getBySlug($bookSlug);
271 $page = $this->pageRepo->getById($pageId, true);
272 $this->checkOwnablePermission('page-update', $page);
273 $this->setPageTitle('Delete Draft Page ' . $page->getShortName());
274 return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
278 * Remove the specified page from storage.
279 * @param string $bookSlug
280 * @param string $pageSlug
282 * @internal param int $id
284 public function destroy($bookSlug, $pageSlug)
286 $book = $this->bookRepo->getBySlug($bookSlug);
287 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
288 $this->checkOwnablePermission('page-delete', $page);
289 Activity::addMessage('page_delete', $book->id, $page->name);
290 session()->flash('success', 'Page deleted');
291 $this->pageRepo->destroy($page);
292 return redirect($book->getUrl());
296 * Remove the specified draft page from storage.
297 * @param string $bookSlug
300 * @throws NotFoundException
302 public function destroyDraft($bookSlug, $pageId)
304 $book = $this->bookRepo->getBySlug($bookSlug);
305 $page = $this->pageRepo->getById($pageId, true);
306 $this->checkOwnablePermission('page-update', $page);
307 session()->flash('success', 'Draft deleted');
308 $this->pageRepo->destroy($page);
309 return redirect($book->getUrl());
313 * Shows the last revisions for this page.
314 * @param string $bookSlug
315 * @param string $pageSlug
316 * @return \Illuminate\View\View
318 public function showRevisions($bookSlug, $pageSlug)
320 $book = $this->bookRepo->getBySlug($bookSlug);
321 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
322 $this->setPageTitle('Revisions For ' . $page->getShortName());
323 return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]);
327 * Shows a preview of a single revision
328 * @param string $bookSlug
329 * @param string $pageSlug
330 * @param int $revisionId
331 * @return \Illuminate\View\View
333 public function showRevision($bookSlug, $pageSlug, $revisionId)
335 $book = $this->bookRepo->getBySlug($bookSlug);
336 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
337 $revision = $this->pageRepo->getRevisionById($revisionId);
338 $page->fill($revision->toArray());
339 $this->setPageTitle('Page Revision For ' . $page->getShortName());
340 return view('pages/revision', ['page' => $page, 'book' => $book]);
344 * Restores a page using the content of the specified revision.
345 * @param string $bookSlug
346 * @param string $pageSlug
347 * @param int $revisionId
348 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
350 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
352 $book = $this->bookRepo->getBySlug($bookSlug);
353 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
354 $this->checkOwnablePermission('page-update', $page);
355 $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
356 Activity::add($page, 'page_restore', $book->id);
357 return redirect($page->getUrl());
361 * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
362 * https://github.com/barryvdh/laravel-dompdf
363 * @param string $bookSlug
364 * @param string $pageSlug
365 * @return \Illuminate\Http\Response
367 public function exportPdf($bookSlug, $pageSlug)
369 $book = $this->bookRepo->getBySlug($bookSlug);
370 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
371 $pdfContent = $this->exportService->pageToPdf($page);
372 return response()->make($pdfContent, 200, [
373 'Content-Type' => 'application/octet-stream',
374 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
379 * Export a page to a self-contained HTML file.
380 * @param string $bookSlug
381 * @param string $pageSlug
382 * @return \Illuminate\Http\Response
384 public function exportHtml($bookSlug, $pageSlug)
386 $book = $this->bookRepo->getBySlug($bookSlug);
387 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
388 $containedHtml = $this->exportService->pageToContainedHtml($page);
389 return response()->make($containedHtml, 200, [
390 'Content-Type' => 'application/octet-stream',
391 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
396 * Export a page to a simple plaintext .txt file.
397 * @param string $bookSlug
398 * @param string $pageSlug
399 * @return \Illuminate\Http\Response
401 public function exportPlainText($bookSlug, $pageSlug)
403 $book = $this->bookRepo->getBySlug($bookSlug);
404 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
405 $containedHtml = $this->exportService->pageToPlainText($page);
406 return response()->make($containedHtml, 200, [
407 'Content-Type' => 'application/octet-stream',
408 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
413 * Show a listing of recently created pages
414 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
416 public function showRecentlyCreated()
418 $pages = $this->pageRepo->getRecentlyCreatedPaginated(20)->setPath(baseUrl('/pages/recently-created'));
419 return view('pages/detailed-listing', [
420 'title' => 'Recently Created Pages',
426 * Show a listing of recently created pages
427 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
429 public function showRecentlyUpdated()
431 $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20)->setPath(baseUrl('/pages/recently-updated'));
432 return view('pages/detailed-listing', [
433 'title' => 'Recently Updated Pages',
439 * Show the Restrictions view.
440 * @param string $bookSlug
441 * @param string $pageSlug
442 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
444 public function showRestrict($bookSlug, $pageSlug)
446 $book = $this->bookRepo->getBySlug($bookSlug);
447 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
448 $this->checkOwnablePermission('restrictions-manage', $page);
449 $roles = $this->userRepo->getRestrictableRoles();
450 return view('pages/restrictions', [
457 * Show the view to choose a new parent to move a page into.
458 * @param string $bookSlug
459 * @param string $pageSlug
461 * @throws NotFoundException
463 public function showMove($bookSlug, $pageSlug)
465 $book = $this->bookRepo->getBySlug($bookSlug);
466 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
467 $this->checkOwnablePermission('page-update', $page);
468 return view('pages/move', [
475 * Does the action of moving the location of a page
476 * @param string $bookSlug
477 * @param string $pageSlug
478 * @param Request $request
480 * @throws NotFoundException
482 public function move($bookSlug, $pageSlug, Request $request)
484 $book = $this->bookRepo->getBySlug($bookSlug);
485 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
486 $this->checkOwnablePermission('page-update', $page);
488 $entitySelection = $request->get('entity_selection', null);
489 if ($entitySelection === null || $entitySelection === '') {
490 return redirect($page->getUrl());
493 $stringExploded = explode(':', $entitySelection);
494 $entityType = $stringExploded[0];
495 $entityId = intval($stringExploded[1]);
499 if ($entityType == 'chapter') {
500 $parent = $this->chapterRepo->getById($entityId);
501 } else if ($entityType == 'book') {
502 $parent = $this->bookRepo->getById($entityId);
505 if ($parent === false || $parent === null) {
506 session()->flash('The selected Book or Chapter was not found');
507 return redirect()->back();
510 $this->pageRepo->changePageParent($page, $parent);
511 Activity::add($page, 'page_move', $page->book->id);
512 session()->flash('success', sprintf('Page moved to "%s"', $parent->name));
514 return redirect($page->getUrl());
518 * Set the permissions for this page.
519 * @param string $bookSlug
520 * @param string $pageSlug
521 * @param Request $request
522 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
524 public function restrict($bookSlug, $pageSlug, Request $request)
526 $book = $this->bookRepo->getBySlug($bookSlug);
527 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
528 $this->checkOwnablePermission('restrictions-manage', $page);
529 $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
530 session()->flash('success', 'Page Permissions Updated');
531 return redirect($page->getUrl());