]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Merge branch 'tinymce' into development
[bookstack] / app / Http / Controllers / Controller.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Exceptions\NotifyException;
6 use BookStack\Facades\Activity;
7 use BookStack\Interfaces\Loggable;
8 use BookStack\Model;
9 use BookStack\Util\WebSafeMimeSniffer;
10 use Illuminate\Foundation\Bus\DispatchesJobs;
11 use Illuminate\Foundation\Validation\ValidatesRequests;
12 use Illuminate\Http\JsonResponse;
13 use Illuminate\Http\Response;
14 use Illuminate\Routing\Controller as BaseController;
15
16 abstract class Controller extends BaseController
17 {
18     use DispatchesJobs;
19     use ValidatesRequests;
20
21     /**
22      * Check if the current user is signed in.
23      */
24     protected function isSignedIn(): bool
25     {
26         return auth()->check();
27     }
28
29     /**
30      * Stops the application and shows a permission error if
31      * the application is in demo mode.
32      */
33     protected function preventAccessInDemoMode()
34     {
35         if (config('app.env') === 'demo') {
36             $this->showPermissionError();
37         }
38     }
39
40     /**
41      * Adds the page title into the view.
42      */
43     public function setPageTitle(string $title)
44     {
45         view()->share('pageTitle', $title);
46     }
47
48     /**
49      * On a permission error redirect to home and display.
50      * the error as a notification.
51      *
52      * @return never
53      */
54     protected function showPermissionError()
55     {
56         $message = request()->wantsJson() ? trans('errors.permissionJson') : trans('errors.permission');
57         throw new NotifyException($message, '/', 403);
58     }
59
60     /**
61      * Checks that the current user has the given permission otherwise throw an exception.
62      */
63     protected function checkPermission(string $permission): void
64     {
65         if (!user() || !user()->can($permission)) {
66             $this->showPermissionError();
67         }
68     }
69
70     /**
71      * Check the current user's permissions against an ownable item otherwise throw an exception.
72      */
73     protected function checkOwnablePermission(string $permission, Model $ownable): void
74     {
75         if (!userCan($permission, $ownable)) {
76             $this->showPermissionError();
77         }
78     }
79
80     /**
81      * Check if a user has a permission or bypass the permission
82      * check if the given callback resolves true.
83      */
84     protected function checkPermissionOr(string $permission, callable $callback): void
85     {
86         if ($callback() !== true) {
87             $this->checkPermission($permission);
88         }
89     }
90
91     /**
92      * Check if the current user has a permission or bypass if the provided user
93      * id matches the current user.
94      */
95     protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
96     {
97         $this->checkPermissionOr($permission, function () use ($userId) {
98             return $userId === user()->id;
99         });
100     }
101
102     /**
103      * Send back a json error message.
104      */
105     protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
106     {
107         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
108     }
109
110     /**
111      * Create a response that forces a download in the browser.
112      */
113     protected function downloadResponse(string $content, string $fileName): Response
114     {
115         return response()->make($content, 200, [
116             'Content-Type'           => 'application/octet-stream',
117             'Content-Disposition'    => 'attachment; filename="' . $fileName . '"',
118             'X-Content-Type-Options' => 'nosniff',
119         ]);
120     }
121
122     /**
123      * Create a file download response that provides the file with a content-type
124      * correct for the file, in a way so the browser can show the content in browser.
125      */
126     protected function inlineDownloadResponse(string $content, string $fileName): Response
127     {
128         $mime = (new WebSafeMimeSniffer())->sniff($content);
129
130         return response()->make($content, 200, [
131             'Content-Type'           => $mime,
132             'Content-Disposition'    => 'inline; filename="' . $fileName . '"',
133             'X-Content-Type-Options' => 'nosniff',
134         ]);
135     }
136
137     /**
138      * Show a positive, successful notification to the user on next view load.
139      */
140     protected function showSuccessNotification(string $message): void
141     {
142         session()->flash('success', $message);
143     }
144
145     /**
146      * Show a warning notification to the user on next view load.
147      */
148     protected function showWarningNotification(string $message): void
149     {
150         session()->flash('warning', $message);
151     }
152
153     /**
154      * Show an error notification to the user on next view load.
155      */
156     protected function showErrorNotification(string $message): void
157     {
158         session()->flash('error', $message);
159     }
160
161     /**
162      * Log an activity in the system.
163      *
164      * @param string|Loggable $detail
165      */
166     protected function logActivity(string $type, $detail = ''): void
167     {
168         Activity::add($type, $detail);
169     }
170
171     /**
172      * Get the validation rules for image files.
173      */
174     protected function getImageValidationRules(): array
175     {
176         return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];
177     }
178 }