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