]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Fixed issue where restricted page content in plaintext export
[bookstack] / app / Http / Controllers / PageController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Entities\Managers\BookContents;
5 use BookStack\Entities\Managers\PageContent;
6 use BookStack\Entities\Managers\PageEditActivity;
7 use BookStack\Entities\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Exceptions\NotifyException;
11 use BookStack\Exceptions\PermissionsException;
12 use Exception;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
15 use Throwable;
16 use Views;
17
18 class PageController extends Controller
19 {
20
21     protected $pageRepo;
22
23     /**
24      * PageController constructor.
25      */
26     public function __construct(PageRepo $pageRepo)
27     {
28         $this->pageRepo = $pageRepo;
29         parent::__construct();
30     }
31
32     /**
33      * Show the form for creating a new page.
34      * @throws Throwable
35      */
36     public function create(string $bookSlug, string $chapterSlug = null)
37     {
38         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
39         $this->checkOwnablePermission('page-create', $parent);
40
41         // Redirect to draft edit screen if signed in
42         if ($this->isSignedIn()) {
43             $draft = $this->pageRepo->getNewDraftPage($parent);
44             return redirect($draft->getUrl());
45         }
46
47         // Otherwise show the edit view if they're a guest
48         $this->setPageTitle(trans('entities.pages_new'));
49         return view('pages.guest-create', ['parent' => $parent]);
50     }
51
52     /**
53      * Create a new page as a guest user.
54      * @throws ValidationException
55      */
56     public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
57     {
58         $this->validate($request, [
59             'name' => 'required|string|max:255'
60         ]);
61
62         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
63         $this->checkOwnablePermission('page-create', $parent);
64
65         $page = $this->pageRepo->getNewDraftPage($parent);
66         $this->pageRepo->publishDraft($page, [
67             'name' => $request->get('name'),
68             'html' => ''
69         ]);
70
71         return redirect($page->getUrl('/edit'));
72     }
73
74     /**
75      * Show form to continue editing a draft page.
76      * @throws NotFoundException
77      */
78     public function editDraft(string $bookSlug, int $pageId)
79     {
80         $draft = $this->pageRepo->getById($pageId);
81         $this->checkOwnablePermission('page-create', $draft->parent());
82         $this->setPageTitle(trans('entities.pages_edit_draft'));
83
84         $draftsEnabled = $this->isSignedIn();
85         $templates = $this->pageRepo->getTemplates(10);
86
87         return view('pages.edit', [
88             'page' => $draft,
89             'book' => $draft->book,
90             'isDraft' => true,
91             'draftsEnabled' => $draftsEnabled,
92             'templates' => $templates,
93         ]);
94     }
95
96     /**
97      * Store a new page by changing a draft into a page.
98      * @throws NotFoundException
99      * @throws ValidationException
100      */
101     public function store(Request $request, string $bookSlug, int $pageId)
102     {
103         $this->validate($request, [
104             'name' => 'required|string|max:255'
105         ]);
106         $draftPage = $this->pageRepo->getById($pageId);
107         $this->checkOwnablePermission('page-create', $draftPage->parent());
108
109         $page = $this->pageRepo->publishDraft($draftPage, $request->all());
110         Activity::add($page, 'page_create', $draftPage->book->id);
111
112         return redirect($page->getUrl());
113     }
114
115     /**
116      * Display the specified page.
117      * If the page is not found via the slug the revisions are searched for a match.
118      * @throws NotFoundException
119      */
120     public function show(string $bookSlug, string $pageSlug)
121     {
122         try {
123             $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
124         } catch (NotFoundException $e) {
125             $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
126
127             if ($page === null) {
128                 throw $e;
129             }
130
131             return redirect($page->getUrl());
132         }
133
134         $this->checkOwnablePermission('page-view', $page);
135
136         $pageContent = (new PageContent($page));
137         $page->html = $pageContent->render();
138         $sidebarTree = (new BookContents($page->book))->getTree();
139         $pageNav = $pageContent->getNavigation($page->html);
140
141         // Check if page comments are enabled
142         $commentsEnabled = !setting('app-disable-comments');
143         if ($commentsEnabled) {
144             $page->load(['comments.createdBy']);
145         }
146
147         Views::add($page);
148         $this->setPageTitle($page->getShortName());
149         return view('pages.show', [
150             'page' => $page,
151             'book' => $page->book,
152             'current' => $page,
153             'sidebarTree' => $sidebarTree,
154             'commentsEnabled' => $commentsEnabled,
155             'pageNav' => $pageNav
156         ]);
157     }
158
159     /**
160      * Get page from an ajax request.
161      * @throws NotFoundException
162      */
163     public function getPageAjax(int $pageId)
164     {
165         $page = $this->pageRepo->getById($pageId);
166         $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
167         $page->addHidden(['book']);
168         return response()->json($page);
169     }
170
171     /**
172      * Show the form for editing the specified page.
173      * @throws NotFoundException
174      */
175     public function edit(string $bookSlug, string $pageSlug)
176     {
177         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
178         $this->checkOwnablePermission('page-update', $page);
179
180         $page->isDraft = false;
181         $editActivity = new PageEditActivity($page);
182
183         // Check for active editing
184         $warnings = [];
185         if ($editActivity->hasActiveEditing()) {
186             $warnings[] = $editActivity->activeEditingMessage();
187         }
188
189         // Check for a current draft version for this user
190         $userDraft = $this->pageRepo->getUserDraft($page);
191         if ($userDraft !== null) {
192             $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
193             $page->isDraft = true;
194             $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
195         }
196
197         if (count($warnings) > 0) {
198             $this->showWarningNotification(implode("\n", $warnings));
199         }
200
201         $templates = $this->pageRepo->getTemplates(10);
202         $draftsEnabled = $this->isSignedIn();
203         $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
204         return view('pages.edit', [
205             'page' => $page,
206             'book' => $page->book,
207             'current' => $page,
208             'draftsEnabled' => $draftsEnabled,
209             'templates' => $templates,
210         ]);
211     }
212
213     /**
214      * Update the specified page in storage.
215      * @throws ValidationException
216      * @throws NotFoundException
217      */
218     public function update(Request $request, string $bookSlug, string $pageSlug)
219     {
220         $this->validate($request, [
221             'name' => 'required|string|max:255'
222         ]);
223         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
224         $this->checkOwnablePermission('page-update', $page);
225
226         $this->pageRepo->update($page, $request->all());
227         Activity::add($page, 'page_update', $page->book->id);
228
229         return redirect($page->getUrl());
230     }
231
232     /**
233      * Save a draft update as a revision.
234      * @throws NotFoundException
235      */
236     public function saveDraft(Request $request, int $pageId)
237     {
238         $page = $this->pageRepo->getById($pageId);
239         $this->checkOwnablePermission('page-update', $page);
240
241         if (!$this->isSignedIn()) {
242             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
243         }
244
245         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
246
247         $updateTime = $draft->updated_at->timestamp;
248         return response()->json([
249             'status'    => 'success',
250             'message'   => trans('entities.pages_edit_draft_save_at'),
251             'timestamp' => $updateTime
252         ]);
253     }
254
255     /**
256      * Redirect from a special link url which uses the page id rather than the name.
257      * @throws NotFoundException
258      */
259     public function redirectFromLink(int $pageId)
260     {
261         $page = $this->pageRepo->getById($pageId);
262         return redirect($page->getUrl());
263     }
264
265     /**
266      * Show the deletion page for the specified page.
267      * @throws NotFoundException
268      */
269     public function showDelete(string $bookSlug, string $pageSlug)
270     {
271         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
272         $this->checkOwnablePermission('page-delete', $page);
273         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
274         return view('pages.delete', [
275             'book' => $page->book,
276             'page' => $page,
277             'current' => $page
278         ]);
279     }
280
281     /**
282      * Show the deletion page for the specified page.
283      * @throws NotFoundException
284      */
285     public function showDeleteDraft(string $bookSlug, int $pageId)
286     {
287         $page = $this->pageRepo->getById($pageId);
288         $this->checkOwnablePermission('page-update', $page);
289         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
290         return view('pages.delete', [
291             'book' => $page->book,
292             'page' => $page,
293             'current' => $page
294         ]);
295     }
296
297     /**
298      * Remove the specified page from storage.
299      * @throws NotFoundException
300      * @throws Throwable
301      * @throws NotifyException
302      */
303     public function destroy(string $bookSlug, string $pageSlug)
304     {
305         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
306         $this->checkOwnablePermission('page-delete', $page);
307
308         $book = $page->book;
309         $parent = $page->chapter ?? $book;
310         $this->pageRepo->destroy($page);
311         Activity::addMessage('page_delete', $page->name, $book->id);
312
313         $this->showSuccessNotification(trans('entities.pages_delete_success'));
314         return redirect($parent->getUrl());
315     }
316
317     /**
318      * Remove the specified draft page from storage.
319      * @throws NotFoundException
320      * @throws NotifyException
321      * @throws Throwable
322      */
323     public function destroyDraft(string $bookSlug, int $pageId)
324     {
325         $page = $this->pageRepo->getById($pageId);
326         $book = $page->book;
327         $chapter = $page->chapter;
328         $this->checkOwnablePermission('page-update', $page);
329
330         $this->pageRepo->destroy($page);
331
332         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
333
334         if ($chapter && userCan('view', $chapter)) {
335             return redirect($chapter->getUrl());
336         }
337         return redirect($book->getUrl());
338     }
339
340     /**
341      * Show a listing of recently created pages.
342      */
343     public function showRecentlyUpdated()
344     {
345         $pages = Page::visible()->orderBy('updated_at', 'desc')
346             ->paginate(20)
347             ->setPath(url('/pages/recently-updated'));
348
349         return view('pages.detailed-listing', [
350             'title' => trans('entities.recently_updated_pages'),
351             'pages' => $pages
352         ]);
353     }
354
355     /**
356      * Show the view to choose a new parent to move a page into.
357      * @throws NotFoundException
358      */
359     public function showMove(string $bookSlug, string $pageSlug)
360     {
361         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
362         $this->checkOwnablePermission('page-update', $page);
363         $this->checkOwnablePermission('page-delete', $page);
364         return view('pages.move', [
365             'book' => $page->book,
366             'page' => $page
367         ]);
368     }
369
370     /**
371      * Does the action of moving the location of a page.
372      * @throws NotFoundException
373      * @throws Throwable
374      */
375     public function move(Request $request, string $bookSlug, string $pageSlug)
376     {
377         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
378         $this->checkOwnablePermission('page-update', $page);
379         $this->checkOwnablePermission('page-delete', $page);
380
381         $entitySelection = $request->get('entity_selection', null);
382         if ($entitySelection === null || $entitySelection === '') {
383             return redirect($page->getUrl());
384         }
385
386         try {
387             $parent = $this->pageRepo->move($page, $entitySelection);
388         } catch (Exception $exception) {
389             if ($exception instanceof  PermissionsException) {
390                 $this->showPermissionError();
391             }
392
393             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
394             return redirect()->back();
395         }
396
397         Activity::add($page, 'page_move', $page->book->id);
398         $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
399         return redirect($page->getUrl());
400     }
401
402     /**
403      * Show the view to copy a page.
404      * @throws NotFoundException
405      */
406     public function showCopy(string $bookSlug, string $pageSlug)
407     {
408         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
409         $this->checkOwnablePermission('page-view', $page);
410         session()->flashInput(['name' => $page->name]);
411         return view('pages.copy', [
412             'book' => $page->book,
413             'page' => $page
414         ]);
415     }
416
417
418     /**
419      * Create a copy of a page within the requested target destination.
420      * @throws NotFoundException
421      * @throws Throwable
422      */
423     public function copy(Request $request, string $bookSlug, string $pageSlug)
424     {
425         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
426         $this->checkOwnablePermission('page-view', $page);
427
428         $entitySelection = $request->get('entity_selection', null) ?? null;
429         $newName = $request->get('name', null);
430
431         try {
432             $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
433         } catch (Exception $exception) {
434             if ($exception instanceof  PermissionsException) {
435                 $this->showPermissionError();
436             }
437
438             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
439             return redirect()->back();
440         }
441
442         Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
443
444         $this->showSuccessNotification(trans('entities.pages_copy_success'));
445         return redirect($pageCopy->getUrl());
446     }
447
448     /**
449      * Show the Permissions view.
450      * @throws NotFoundException
451      */
452     public function showPermissions(string $bookSlug, string $pageSlug)
453     {
454         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
455         $this->checkOwnablePermission('restrictions-manage', $page);
456         return view('pages.permissions', [
457             'page'  => $page,
458         ]);
459     }
460
461     /**
462      * Set the permissions for this page.
463      * @throws NotFoundException
464      * @throws Throwable
465      */
466     public function permissions(Request $request, string $bookSlug, string $pageSlug)
467     {
468         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
469         $this->checkOwnablePermission('restrictions-manage', $page);
470
471         $restricted = $request->get('restricted') === 'true';
472         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
473         $this->pageRepo->updatePermissions($page, $restricted, $permissions);
474
475         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
476         return redirect($page->getUrl());
477     }
478 }