]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
LDAP: Added TLS support
[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->getParent());
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->getParent());
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::add($page, 'page_delete', $page->book_id);
312
313         return redirect($parent->getUrl());
314     }
315
316     /**
317      * Remove the specified draft page from storage.
318      * @throws NotFoundException
319      * @throws NotifyException
320      * @throws Throwable
321      */
322     public function destroyDraft(string $bookSlug, int $pageId)
323     {
324         $page = $this->pageRepo->getById($pageId);
325         $book = $page->book;
326         $chapter = $page->chapter;
327         $this->checkOwnablePermission('page-update', $page);
328
329         $this->pageRepo->destroy($page);
330
331         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
332
333         if ($chapter && userCan('view', $chapter)) {
334             return redirect($chapter->getUrl());
335         }
336         return redirect($book->getUrl());
337     }
338
339     /**
340      * Show a listing of recently created pages.
341      */
342     public function showRecentlyUpdated()
343     {
344         $pages = Page::visible()->orderBy('updated_at', 'desc')
345             ->paginate(20)
346             ->setPath(url('/pages/recently-updated'));
347
348         return view('pages.detailed-listing', [
349             'title' => trans('entities.recently_updated_pages'),
350             'pages' => $pages
351         ]);
352     }
353
354     /**
355      * Show the view to choose a new parent to move a page into.
356      * @throws NotFoundException
357      */
358     public function showMove(string $bookSlug, string $pageSlug)
359     {
360         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
361         $this->checkOwnablePermission('page-update', $page);
362         $this->checkOwnablePermission('page-delete', $page);
363         return view('pages.move', [
364             'book' => $page->book,
365             'page' => $page
366         ]);
367     }
368
369     /**
370      * Does the action of moving the location of a page.
371      * @throws NotFoundException
372      * @throws Throwable
373      */
374     public function move(Request $request, string $bookSlug, string $pageSlug)
375     {
376         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
377         $this->checkOwnablePermission('page-update', $page);
378         $this->checkOwnablePermission('page-delete', $page);
379
380         $entitySelection = $request->get('entity_selection', null);
381         if ($entitySelection === null || $entitySelection === '') {
382             return redirect($page->getUrl());
383         }
384
385         try {
386             $parent = $this->pageRepo->move($page, $entitySelection);
387         } catch (Exception $exception) {
388             if ($exception instanceof  PermissionsException) {
389                 $this->showPermissionError();
390             }
391
392             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
393             return redirect()->back();
394         }
395
396         Activity::add($page, 'page_move', $page->book->id);
397         $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
398         return redirect($page->getUrl());
399     }
400
401     /**
402      * Show the view to copy a page.
403      * @throws NotFoundException
404      */
405     public function showCopy(string $bookSlug, string $pageSlug)
406     {
407         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
408         $this->checkOwnablePermission('page-view', $page);
409         session()->flashInput(['name' => $page->name]);
410         return view('pages.copy', [
411             'book' => $page->book,
412             'page' => $page
413         ]);
414     }
415
416
417     /**
418      * Create a copy of a page within the requested target destination.
419      * @throws NotFoundException
420      * @throws Throwable
421      */
422     public function copy(Request $request, string $bookSlug, string $pageSlug)
423     {
424         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
425         $this->checkOwnablePermission('page-view', $page);
426
427         $entitySelection = $request->get('entity_selection', null) ?? null;
428         $newName = $request->get('name', null);
429
430         try {
431             $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
432         } catch (Exception $exception) {
433             if ($exception instanceof  PermissionsException) {
434                 $this->showPermissionError();
435             }
436
437             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
438             return redirect()->back();
439         }
440
441         Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
442
443         $this->showSuccessNotification(trans('entities.pages_copy_success'));
444         return redirect($pageCopy->getUrl());
445     }
446
447     /**
448      * Show the Permissions view.
449      * @throws NotFoundException
450      */
451     public function showPermissions(string $bookSlug, string $pageSlug)
452     {
453         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
454         $this->checkOwnablePermission('restrictions-manage', $page);
455         return view('pages.permissions', [
456             'page'  => $page,
457         ]);
458     }
459
460     /**
461      * Set the permissions for this page.
462      * @throws NotFoundException
463      * @throws Throwable
464      */
465     public function permissions(Request $request, string $bookSlug, string $pageSlug)
466     {
467         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
468         $this->checkOwnablePermission('restrictions-manage', $page);
469
470         $restricted = $request->get('restricted') === 'true';
471         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
472         $this->pageRepo->updatePermissions($page, $restricted, $permissions);
473
474         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
475         return redirect($page->getUrl());
476     }
477 }