]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Updated issue template and added TinyMCE autolinking
[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      */
149     public function show($bookSlug, $pageSlug)
150     {
151         try {
152             $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
153         } catch (NotFoundException $e) {
154             $page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
155             if ($page === null) abort(404);
156             return redirect($page->getUrl());
157         }
158
159         $this->checkOwnablePermission('page-view', $page);
160
161         $sidebarTree = $this->entityRepo->getBookChildren($page->book);
162         $pageNav = $this->entityRepo->getPageNav($page);
163         
164         Views::add($page);
165         $this->setPageTitle($page->getShortName());
166         return view('pages/show', ['page' => $page, 'book' => $page->book,
167                                    'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
168     }
169
170     /**
171      * Get page from an ajax request.
172      * @param int $pageId
173      * @return \Illuminate\Http\JsonResponse
174      */
175     public function getPageAjax($pageId)
176     {
177         $page = $this->entityRepo->getById('page', $pageId);
178         return response()->json($page);
179     }
180
181     /**
182      * Show the form for editing the specified page.
183      * @param string $bookSlug
184      * @param string $pageSlug
185      * @return Response
186      */
187     public function edit($bookSlug, $pageSlug)
188     {
189         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
190         $this->checkOwnablePermission('page-update', $page);
191         $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
192         $page->isDraft = false;
193
194         // Check for active editing
195         $warnings = [];
196         if ($this->entityRepo->isPageEditingActive($page, 60)) {
197             $warnings[] = $this->entityRepo->getPageEditingActiveMessage($page, 60);
198         }
199
200         // Check for a current draft version for this user
201         if ($this->entityRepo->hasUserGotPageDraft($page, $this->currentUser->id)) {
202             $draft = $this->entityRepo->getUserPageDraft($page, $this->currentUser->id);
203             $page->name = $draft->name;
204             $page->html = $draft->html;
205             $page->markdown = $draft->markdown;
206             $page->isDraft = true;
207             $warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
208         }
209
210         if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
211
212         $draftsEnabled = $this->signedIn;
213         return view('pages/edit', [
214             'page' => $page,
215             'book' => $page->book,
216             'current' => $page,
217             'draftsEnabled' => $draftsEnabled
218         ]);
219     }
220
221     /**
222      * Update the specified page in storage.
223      * @param  Request $request
224      * @param  string $bookSlug
225      * @param  string $pageSlug
226      * @return Response
227      */
228     public function update(Request $request, $bookSlug, $pageSlug)
229     {
230         $this->validate($request, [
231             'name' => 'required|string|max:255'
232         ]);
233         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
234         $this->checkOwnablePermission('page-update', $page);
235         $this->entityRepo->updatePage($page, $page->book->id, $request->all());
236         Activity::add($page, 'page_update', $page->book->id);
237         return redirect($page->getUrl());
238     }
239
240     /**
241      * Save a draft update as a revision.
242      * @param Request $request
243      * @param int $pageId
244      * @return \Illuminate\Http\JsonResponse
245      */
246     public function saveDraft(Request $request, $pageId)
247     {
248         $page = $this->entityRepo->getById('page', $pageId, true);
249         $this->checkOwnablePermission('page-update', $page);
250
251         if (!$this->signedIn) {
252             return response()->json([
253                 'status' => 'error',
254                 'message' => trans('errors.guests_cannot_save_drafts'),
255             ], 500);
256         }
257
258         $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
259
260         $updateTime = $draft->updated_at->timestamp;
261         $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
262         return response()->json([
263             'status'    => 'success',
264             'message'   => trans('entities.pages_edit_draft_save_at'),
265             'timestamp' => $utcUpdateTimestamp
266         ]);
267     }
268
269     /**
270      * Redirect from a special link url which
271      * uses the page id rather than the name.
272      * @param int $pageId
273      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
274      */
275     public function redirectFromLink($pageId)
276     {
277         $page = $this->entityRepo->getById('page', $pageId);
278         return redirect($page->getUrl());
279     }
280
281     /**
282      * Show the deletion page for the specified page.
283      * @param string $bookSlug
284      * @param string $pageSlug
285      * @return \Illuminate\View\View
286      */
287     public function showDelete($bookSlug, $pageSlug)
288     {
289         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
290         $this->checkOwnablePermission('page-delete', $page);
291         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
292         return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
293     }
294
295
296     /**
297      * Show the deletion page for the specified page.
298      * @param string $bookSlug
299      * @param int $pageId
300      * @return \Illuminate\View\View
301      * @throws NotFoundException
302      */
303     public function showDeleteDraft($bookSlug, $pageId)
304     {
305         $page = $this->entityRepo->getById('page', $pageId, true);
306         $this->checkOwnablePermission('page-update', $page);
307         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
308         return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
309     }
310
311     /**
312      * Remove the specified page from storage.
313      * @param string $bookSlug
314      * @param string $pageSlug
315      * @return Response
316      * @internal param int $id
317      */
318     public function destroy($bookSlug, $pageSlug)
319     {
320         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
321         $book = $page->book;
322         $this->checkOwnablePermission('page-delete', $page);
323         Activity::addMessage('page_delete', $book->id, $page->name);
324         session()->flash('success', trans('entities.pages_delete_success'));
325         $this->entityRepo->destroyPage($page);
326         return redirect($book->getUrl());
327     }
328
329     /**
330      * Remove the specified draft page from storage.
331      * @param string $bookSlug
332      * @param int $pageId
333      * @return Response
334      * @throws NotFoundException
335      */
336     public function destroyDraft($bookSlug, $pageId)
337     {
338         $page = $this->entityRepo->getById('page', $pageId, true);
339         $book = $page->book;
340         $this->checkOwnablePermission('page-update', $page);
341         session()->flash('success', trans('entities.pages_delete_draft_success'));
342         $this->entityRepo->destroyPage($page);
343         return redirect($book->getUrl());
344     }
345
346     /**
347      * Shows the last revisions for this page.
348      * @param string $bookSlug
349      * @param string $pageSlug
350      * @return \Illuminate\View\View
351      */
352     public function showRevisions($bookSlug, $pageSlug)
353     {
354         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
355         $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
356         return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
357     }
358
359     /**
360      * Shows a preview of a single revision
361      * @param string $bookSlug
362      * @param string $pageSlug
363      * @param int $revisionId
364      * @return \Illuminate\View\View
365      */
366     public function showRevision($bookSlug, $pageSlug, $revisionId)
367     {
368         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
369         $revision = $this->entityRepo->getById('page_revision', $revisionId, false);
370
371         $page->fill($revision->toArray());
372         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
373         
374         return view('pages/revision', [
375             'page' => $page,
376             'book' => $page->book,
377         ]);
378     }
379
380     /**
381      * Shows the changes of a single revision
382      * @param string $bookSlug
383      * @param string $pageSlug
384      * @param int $revisionId
385      * @return \Illuminate\View\View
386      */
387     public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
388     {
389         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
390         $revision = $this->entityRepo->getById('page_revision', $revisionId);
391
392         $prev = $revision->getPrevious();
393         $prevContent = ($prev === null) ? '' : $prev->html;
394         $diff = (new Htmldiff)->diff($prevContent, $revision->html);
395
396         $page->fill($revision->toArray());
397         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
398
399         return view('pages/revision', [
400             'page' => $page,
401             'book' => $page->book,
402             'diff' => $diff,
403         ]);
404     }
405
406     /**
407      * Restores a page using the content of the specified revision.
408      * @param string $bookSlug
409      * @param string $pageSlug
410      * @param int $revisionId
411      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
412      */
413     public function restoreRevision($bookSlug, $pageSlug, $revisionId)
414     {
415         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
416         $this->checkOwnablePermission('page-update', $page);
417         $page = $this->entityRepo->restorePageRevision($page, $page->book, $revisionId);
418         Activity::add($page, 'page_restore', $page->book->id);
419         return redirect($page->getUrl());
420     }
421
422     /**
423      * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
424      * https://github.com/barryvdh/laravel-dompdf
425      * @param string $bookSlug
426      * @param string $pageSlug
427      * @return \Illuminate\Http\Response
428      */
429     public function exportPdf($bookSlug, $pageSlug)
430     {
431         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
432         $pdfContent = $this->exportService->pageToPdf($page);
433         return response()->make($pdfContent, 200, [
434             'Content-Type'        => 'application/octet-stream',
435             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
436         ]);
437     }
438
439     /**
440      * Export a page to a self-contained HTML file.
441      * @param string $bookSlug
442      * @param string $pageSlug
443      * @return \Illuminate\Http\Response
444      */
445     public function exportHtml($bookSlug, $pageSlug)
446     {
447         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
448         $containedHtml = $this->exportService->pageToContainedHtml($page);
449         return response()->make($containedHtml, 200, [
450             'Content-Type'        => 'application/octet-stream',
451             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
452         ]);
453     }
454
455     /**
456      * Export a page to a simple plaintext .txt file.
457      * @param string $bookSlug
458      * @param string $pageSlug
459      * @return \Illuminate\Http\Response
460      */
461     public function exportPlainText($bookSlug, $pageSlug)
462     {
463         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
464         $containedHtml = $this->exportService->pageToPlainText($page);
465         return response()->make($containedHtml, 200, [
466             'Content-Type'        => 'application/octet-stream',
467             'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
468         ]);
469     }
470
471     /**
472      * Show a listing of recently created pages
473      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
474      */
475     public function showRecentlyCreated()
476     {
477         $pages = $this->entityRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
478         return view('pages/detailed-listing', [
479             'title' => trans('entities.recently_created_pages'),
480             'pages' => $pages
481         ]);
482     }
483
484     /**
485      * Show a listing of recently created pages
486      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
487      */
488     public function showRecentlyUpdated()
489     {
490         $pages = $this->entityRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
491         return view('pages/detailed-listing', [
492             'title' => trans('entities.recently_updated_pages'),
493             'pages' => $pages
494         ]);
495     }
496
497     /**
498      * Show the Restrictions view.
499      * @param string $bookSlug
500      * @param string $pageSlug
501      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
502      */
503     public function showRestrict($bookSlug, $pageSlug)
504     {
505         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
506         $this->checkOwnablePermission('restrictions-manage', $page);
507         $roles = $this->userRepo->getRestrictableRoles();
508         return view('pages/restrictions', [
509             'page'  => $page,
510             'roles' => $roles
511         ]);
512     }
513
514     /**
515      * Show the view to choose a new parent to move a page into.
516      * @param string $bookSlug
517      * @param string $pageSlug
518      * @return mixed
519      * @throws NotFoundException
520      */
521     public function showMove($bookSlug, $pageSlug)
522     {
523         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
524         $this->checkOwnablePermission('page-update', $page);
525         return view('pages/move', [
526             'book' => $page->book,
527             'page' => $page
528         ]);
529     }
530
531     /**
532      * Does the action of moving the location of a page
533      * @param string $bookSlug
534      * @param string $pageSlug
535      * @param Request $request
536      * @return mixed
537      * @throws NotFoundException
538      */
539     public function move($bookSlug, $pageSlug, Request $request)
540     {
541         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
542         $this->checkOwnablePermission('page-update', $page);
543
544         $entitySelection = $request->get('entity_selection', null);
545         if ($entitySelection === null || $entitySelection === '') {
546             return redirect($page->getUrl());
547         }
548
549         $stringExploded = explode(':', $entitySelection);
550         $entityType = $stringExploded[0];
551         $entityId = intval($stringExploded[1]);
552
553
554         try {
555             $parent = $this->entityRepo->getById($entityType, $entityId);
556         } catch (\Exception $e) {
557             session()->flash(trans('entities.selected_book_chapter_not_found'));
558             return redirect()->back();
559         }
560
561         $this->entityRepo->changePageParent($page, $parent);
562         Activity::add($page, 'page_move', $page->book->id);
563         session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
564
565         return redirect($page->getUrl());
566     }
567
568     /**
569      * Set the permissions for this page.
570      * @param string $bookSlug
571      * @param string $pageSlug
572      * @param Request $request
573      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
574      */
575     public function restrict($bookSlug, $pageSlug, Request $request)
576     {
577         $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
578         $this->checkOwnablePermission('restrictions-manage', $page);
579         $this->entityRepo->updateEntityPermissionsFromRequest($request, $page);
580         session()->flash('success', trans('entities.pages_permissions_success'));
581         return redirect($page->getUrl());
582     }
583
584 }