]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Corrected the keys for okta auth
[bookstack] / app / Http / Controllers / PageController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Exceptions\NotFoundException;
5 use BookStack\Repos\EntityRepo;
6 use BookStack\Repos\UserRepo;
7 use BookStack\Services\ExportService;
8 use Carbon\Carbon;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
11 use Views;
12 use GatherContent\Htmldiff\Htmldiff;
13
14 class PageController extends Controller
15 {
16
17     protected $entityRepo;
18     protected $exportService;
19     protected $userRepo;
20
21     /**
22      * PageController constructor.
23      * @param EntityRepo $entityRepo
24      * @param ExportService $exportService
25      * @param UserRepo $userRepo
26      */
27     public function __construct(EntityRepo $entityRepo, ExportService $exportService, UserRepo $userRepo)
28     {
29         $this->entityRepo = $entityRepo;
30         $this->exportService = $exportService;
31         $this->userRepo = $userRepo;
32         parent::__construct();
33     }
34
35     /**
36      * Show the form for creating a new page.
37      * @param string $bookSlug
38      * @param string $chapterSlug
39      * @return Response
40      * @internal param bool $pageSlug
41      */
42     public function create($bookSlug, $chapterSlug = null)
43     {
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);
48
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());
53         }
54
55         // Otherwise show edit view
56         $this->setPageTitle(trans('entities.pages_new'));
57         return view('pages/guest-create', ['parent' => $parent]);
58     }
59
60     /**
61      * Create a new page as a guest user.
62      * @param Request $request
63      * @param string $bookSlug
64      * @param string|null $chapterSlug
65      * @return mixed
66      * @throws NotFoundException
67      */
68     public function createAsGuest(Request $request, $bookSlug, $chapterSlug = null)
69     {
70         $this->validate($request, [
71             'name' => 'required|string|max:255'
72         ]);
73
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);
78
79         $page = $this->entityRepo->getDraftPage($book, $chapter);
80         $this->entityRepo->publishPageDraft($page, [
81             'name' => $request->get('name'),
82             'html' => ''
83         ]);
84         return redirect($page->getUrl('/edit'));
85     }
86
87     /**
88      * Show form to continue editing a draft page.
89      * @param string $bookSlug
90      * @param int $pageId
91      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
92      */
93     public function editDraft($bookSlug, $pageId)
94     {
95         $draft = $this->entityRepo->getById('page', $pageId, true);
96         $this->checkOwnablePermission('page-create', $draft->book);
97         $this->setPageTitle(trans('entities.pages_edit_draft'));
98
99         $draftsEnabled = $this->signedIn;
100         return view('pages/edit', [
101             'page' => $draft,
102             'book' => $draft->book,
103             'isDraft' => true,
104             'draftsEnabled' => $draftsEnabled
105         ]);
106     }
107
108     /**
109      * Store a new page by changing a draft into a page.
110      * @param  Request $request
111      * @param  string $bookSlug
112      * @param  int $pageId
113      * @return Response
114      */
115     public function store(Request $request, $bookSlug, $pageId)
116     {
117         $this->validate($request, [
118             'name' => 'required|string|max:255'
119         ]);
120
121         $input = $request->all();
122         $book = $this->entityRepo->getBySlug('book', $bookSlug);
123
124         $draftPage = $this->entityRepo->getById('page', $pageId, true);
125
126         $chapterId = intval($draftPage->chapter_id);
127         $parent = $chapterId !== 0 ? $this->entityRepo->getById('chapter', $chapterId) : $book;
128         $this->checkOwnablePermission('page-create', $parent);
129
130         if ($parent->isA('chapter')) {
131             $input['priority'] = $this->entityRepo->getNewChapterPriority($parent);
132         } else {
133             $input['priority'] = $this->entityRepo->getNewBookPriority($parent);
134         }
135
136         $page = $this->entityRepo->publishPageDraft($draftPage, $input);
137
138         Activity::add($page, 'page_create', $book->id);
139         return redirect($page->getUrl());
140     }
141
142     /**
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
147      * @return Response
148      * @throws NotFoundException
149      */
150     public function show($bookSlug, $pageSlug)
151     {
152         try {
153             $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
154         } catch (NotFoundException $e) {
155             $page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
156             if ($page === null) {
157                 throw $e;
158             }
159             return redirect($page->getUrl());
160         }
161
162         $this->checkOwnablePermission('page-view', $page);
163
164         $page->html = $this->entityRepo->renderPage($page);
165         $sidebarTree = $this->entityRepo->getBookChildren($page->book);
166         $pageNav = $this->entityRepo->getPageNav($page->html);
167
168         // check if the comment's are enabled
169         $commentsEnabled = !setting('app-disable-comments');
170         if ($commentsEnabled) {
171             $page->load(['comments.createdBy']);
172         }
173
174         Views::add($page);
175         $this->setPageTitle($page->getShortName());
176         return view('pages/show', [
177             'page' => $page,'book' => $page->book,
178             'current' => $page,
179             'sidebarTree' => $sidebarTree,
180             'commentsEnabled' => $commentsEnabled,
181             'pageNav' => $pageNav
182         ]);
183     }
184
185     /**
186      * Get page from an ajax request.
187      * @param int $pageId
188      * @return \Illuminate\Http\JsonResponse
189      */
190     public function getPageAjax($pageId)
191     {
192         $page = $this->entityRepo->getById('page', $pageId);
193         return response()->json($page);
194     }
195
196     /**
197      * Show the form for editing the specified page.
198      * @param string $bookSlug
199      * @param string $pageSlug
200      * @return Response
201      */
202     public function edit($bookSlug, $pageSlug)
203     {
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;
208
209         // Check for active editing
210         $warnings = [];
211         if ($this->entityRepo->isPageEditingActive($page, 60)) {
212             $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
213         }
214
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);
223         }
224
225         if (count($warnings) > 0) {
226             session()->flash('warning', implode("\n", $warnings));
227         }
228
229         $draftsEnabled = $this->signedIn;
230         return view('pages/edit', [
231             'page' => $page,
232             'book' => $page->book,
233             'current' => $page,
234             'draftsEnabled' => $draftsEnabled
235         ]);
236     }
237
238     /**
239      * Update the specified page in storage.
240      * @param  Request $request
241      * @param  string $bookSlug
242      * @param  string $pageSlug
243      * @return Response
244      */
245     public function update(Request $request, $bookSlug, $pageSlug)
246     {
247         $this->validate($request, [
248             'name' => 'required|string|max:255'
249         ]);
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());
255     }
256
257     /**
258      * Save a draft update as a revision.
259      * @param Request $request
260      * @param int $pageId
261      * @return \Illuminate\Http\JsonResponse
262      */
263     public function saveDraft(Request $request, $pageId)
264     {
265         $page = $this->entityRepo->getById('page', $pageId, true);
266         $this->checkOwnablePermission('page-update', $page);
267
268         if (!$this->signedIn) {
269             return response()->json([
270                 'status' => 'error',
271                 'message' => trans('errors.guests_cannot_save_drafts'),
272             ], 500);
273         }
274
275         $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
276
277         $updateTime = $draft->updated_at->timestamp;
278         $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
279         return response()->json([
280             'status'    => 'success',
281             'message'   => trans('entities.pages_edit_draft_save_at'),
282             'timestamp' => $utcUpdateTimestamp
283         ]);
284     }
285
286     /**
287      * Redirect from a special link url which
288      * uses the page id rather than the name.
289      * @param int $pageId
290      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
291      */
292     public function redirectFromLink($pageId)
293     {
294         $page = $this->entityRepo->getById('page', $pageId);
295         return redirect($page->getUrl());
296     }
297
298     /**
299      * Show the deletion page for the specified page.
300      * @param string $bookSlug
301      * @param string $pageSlug
302      * @return \Illuminate\View\View
303      */
304     public function showDelete($bookSlug, $pageSlug)
305     {
306         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
307         $this->checkOwnablePermission('page-delete', $page);
308         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
309         return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
310     }
311
312
313     /**
314      * Show the deletion page for the specified page.
315      * @param string $bookSlug
316      * @param int $pageId
317      * @return \Illuminate\View\View
318      * @throws NotFoundException
319      */
320     public function showDeleteDraft($bookSlug, $pageId)
321     {
322         $page = $this->entityRepo->getById('page', $pageId, true);
323         $this->checkOwnablePermission('page-update', $page);
324         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
325         return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
326     }
327
328     /**
329      * Remove the specified page from storage.
330      * @param string $bookSlug
331      * @param string $pageSlug
332      * @return Response
333      * @internal param int $id
334      */
335     public function destroy($bookSlug, $pageSlug)
336     {
337         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
338         $book = $page->book;
339         $this->checkOwnablePermission('page-delete', $page);
340         $this->entityRepo->destroyPage($page);
341
342         Activity::addMessage('page_delete', $book->id, $page->name);
343         session()->flash('success', trans('entities.pages_delete_success'));
344         return redirect($book->getUrl());
345     }
346
347     /**
348      * Remove the specified draft page from storage.
349      * @param string $bookSlug
350      * @param int $pageId
351      * @return Response
352      * @throws NotFoundException
353      */
354     public function destroyDraft($bookSlug, $pageId)
355     {
356         $page = $this->entityRepo->getById('page', $pageId, true);
357         $book = $page->book;
358         $this->checkOwnablePermission('page-update', $page);
359         session()->flash('success', trans('entities.pages_delete_draft_success'));
360         $this->entityRepo->destroyPage($page);
361         return redirect($book->getUrl());
362     }
363
364     /**
365      * Shows the last revisions for this page.
366      * @param string $bookSlug
367      * @param string $pageSlug
368      * @return \Illuminate\View\View
369      */
370     public function showRevisions($bookSlug, $pageSlug)
371     {
372         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
373         $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
374         return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
375     }
376
377     /**
378      * Shows a preview of a single revision
379      * @param string $bookSlug
380      * @param string $pageSlug
381      * @param int $revisionId
382      * @return \Illuminate\View\View
383      */
384     public function showRevision($bookSlug, $pageSlug, $revisionId)
385     {
386         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
387         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
388         if ($revision === null) {
389             abort(404);
390         }
391
392         $page->fill($revision->toArray());
393         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
394
395         return view('pages/revision', [
396             'page' => $page,
397             'book' => $page->book,
398             'revision' => $revision
399         ]);
400     }
401
402     /**
403      * Shows the changes of a single revision
404      * @param string $bookSlug
405      * @param string $pageSlug
406      * @param int $revisionId
407      * @return \Illuminate\View\View
408      */
409     public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
410     {
411         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
412         $revision = $page->revisions()->where('id', '=', $revisionId)->first();
413         if ($revision === null) {
414             abort(404);
415         }
416
417         $prev = $revision->getPrevious();
418         $prevContent = ($prev === null) ? '' : $prev->html;
419         $diff = (new Htmldiff)->diff($prevContent, $revision->html);
420
421         $page->fill($revision->toArray());
422         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
423
424         return view('pages/revision', [
425             'page' => $page,
426             'book' => $page->book,
427             'diff' => $diff,
428             'revision' => $revision
429         ]);
430     }
431
432     /**
433      * Restores a page using the content of the specified revision.
434      * @param string $bookSlug
435      * @param string $pageSlug
436      * @param int $revisionId
437      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
438      */
439     public function restoreRevision($bookSlug, $pageSlug, $revisionId)
440     {
441         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
442         $this->checkOwnablePermission('page-update', $page);
443         $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
444         Activity::add($page, 'page_restore', $page->book->id);
445         return redirect($page->getUrl());
446     }
447
448     /**
449      * Exports a page to a PDF.
450      * https://github.com/barryvdh/laravel-dompdf
451      * @param string $bookSlug
452      * @param string $pageSlug
453      * @return \Illuminate\Http\Response
454      */
455     public function exportPdf($bookSlug, $pageSlug)
456     {
457         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
458         $page->html = $this->entityRepo->renderPage($page);
459         $pdfContent = $this->exportService->pageToPdf($page);
460         return response()->make($pdfContent, 200, [
461             'Content-Type'        => 'application/octet-stream',
462             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
463         ]);
464     }
465
466     /**
467      * Export a page to a self-contained HTML file.
468      * @param string $bookSlug
469      * @param string $pageSlug
470      * @return \Illuminate\Http\Response
471      */
472     public function exportHtml($bookSlug, $pageSlug)
473     {
474         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
475         $page->html = $this->entityRepo->renderPage($page);
476         $containedHtml = $this->exportService->pageToContainedHtml($page);
477         return response()->make($containedHtml, 200, [
478             'Content-Type'        => 'application/octet-stream',
479             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
480         ]);
481     }
482
483     /**
484      * Export a page to a simple plaintext .txt file.
485      * @param string $bookSlug
486      * @param string $pageSlug
487      * @return \Illuminate\Http\Response
488      */
489     public function exportPlainText($bookSlug, $pageSlug)
490     {
491         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
492         $containedHtml = $this->exportService->pageToPlainText($page);
493         return response()->make($containedHtml, 200, [
494             'Content-Type'        => 'application/octet-stream',
495             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
496         ]);
497     }
498
499     /**
500      * Show a listing of recently created pages
501      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
502      */
503     public function showRecentlyCreated()
504     {
505         $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
506         return view('pages/detailed-listing', [
507             'title' => trans('entities.recently_created_pages'),
508             'pages' => $pages
509         ]);
510     }
511
512     /**
513      * Show a listing of recently created pages
514      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
515      */
516     public function showRecentlyUpdated()
517     {
518         $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
519         return view('pages/detailed-listing', [
520             'title' => trans('entities.recently_updated_pages'),
521             'pages' => $pages
522         ]);
523     }
524
525     /**
526      * Show the Restrictions view.
527      * @param string $bookSlug
528      * @param string $pageSlug
529      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
530      */
531     public function showRestrict($bookSlug, $pageSlug)
532     {
533         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
534         $this->checkOwnablePermission('restrictions-manage', $page);
535         $roles = $this->userRepo->getRestrictableRoles();
536         return view('pages/restrictions', [
537             'page'  => $page,
538             'roles' => $roles
539         ]);
540     }
541
542     /**
543      * Show the view to choose a new parent to move a page into.
544      * @param string $bookSlug
545      * @param string $pageSlug
546      * @return mixed
547      * @throws NotFoundException
548      */
549     public function showMove($bookSlug, $pageSlug)
550     {
551         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
552         $this->checkOwnablePermission('page-update', $page);
553         return view('pages/move', [
554             'book' => $page->book,
555             'page' => $page
556         ]);
557     }
558
559     /**
560      * Does the action of moving the location of a page
561      * @param string $bookSlug
562      * @param string $pageSlug
563      * @param Request $request
564      * @return mixed
565      * @throws NotFoundException
566      */
567     public function move($bookSlug, $pageSlug, Request $request)
568     {
569         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
570         $this->checkOwnablePermission('page-update', $page);
571
572         $entitySelection = $request->get('entity_selection', null);
573         if ($entitySelection === null || $entitySelection === '') {
574             return redirect($page->getUrl());
575         }
576
577         $stringExploded = explode(':', $entitySelection);
578         $entityType = $stringExploded[0];
579         $entityId = intval($stringExploded[1]);
580
581
582         try {
583             $parent = $this->entityRepo->getById($entityType, $entityId);
584         } catch (\Exception $e) {
585             session()->flash(trans('entities.selected_book_chapter_not_found'));
586             return redirect()->back();
587         }
588
589         $this->entityRepo->changePageParent($page, $parent);
590         Activity::add($page, 'page_move', $page->book->id);
591         session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
592
593         return redirect($page->getUrl());
594     }
595
596     /**
597      * Set the permissions for this page.
598      * @param string $bookSlug
599      * @param string $pageSlug
600      * @param Request $request
601      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
602      */
603     public function restrict($bookSlug, $pageSlug, Request $request)
604     {
605         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
606         $this->checkOwnablePermission('restrictions-manage', $page);
607         $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
608         session()->flash('success', trans('entities.pages_permissions_success'));
609         return redirect($page->getUrl());
610     }
611 }