]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
Added cache breaker to tinymce loading systems
[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
58         throw new NotifyException($message, '/', 403);
59     }
60
61     /**
62      * Checks that the current user has the given permission otherwise throw an exception.
63      */
64     protected function checkPermission(string $permission): void
65     {
66         if (!user() || !user()->can($permission)) {
67             $this->showPermissionError();
68         }
69     }
70
71     /**
72      * Check the current user's permissions against an ownable item otherwise throw an exception.
73      */
74     protected function checkOwnablePermission(string $permission, Model $ownable): void
75     {
76         if (!userCan($permission, $ownable)) {
77             $this->showPermissionError();
78         }
79     }
80
81     /**
82      * Check if a user has a permission or bypass the permission
83      * check if the given callback resolves true.
84      */
85     protected function checkPermissionOr(string $permission, callable $callback): void
86     {
87         if ($callback() !== true) {
88             $this->checkPermission($permission);
89         }
90     }
91
92     /**
93      * Check if the current user has a permission or bypass if the provided user
94      * id matches the current user.
95      */
96     protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
97     {
98         $this->checkPermissionOr($permission, function () use ($userId) {
99             return $userId === user()->id;
100         });
101     }
102
103     /**
104      * Send back a json error message.
105      */
106     protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
107     {
108         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
109     }
110
111     /**
112      * Create a response that forces a download in the browser.
113      */
114     protected function downloadResponse(string $content, string $fileName): Response
115     {
116         return response()->make($content, 200, [
117             'Content-Type'           => 'application/octet-stream',
118             'Content-Disposition'    => 'attachment; filename="' . $fileName . '"',
119             'X-Content-Type-Options' => 'nosniff',
120         ]);
121     }
122
123     /**
124      * Create a file download response that provides the file with a content-type
125      * correct for the file, in a way so the browser can show the content in browser.
126      */
127     protected function inlineDownloadResponse(string $content, string $fileName): Response
128     {
129         $mime = (new WebSafeMimeSniffer())->sniff($content);
130
131         return response()->make($content, 200, [
132             'Content-Type'           => $mime,
133             'Content-Disposition'    => 'inline; filename="' . $fileName . '"',
134             'X-Content-Type-Options' => 'nosniff',
135         ]);
136     }
137
138     /**
139      * Show a positive, successful notification to the user on next view load.
140      */
141     protected function showSuccessNotification(string $message): void
142     {
143         session()->flash('success', $message);
144     }
145
146     /**
147      * Show a warning notification to the user on next view load.
148      */
149     protected function showWarningNotification(string $message): void
150     {
151         session()->flash('warning', $message);
152     }
153
154     /**
155      * Show an error notification to the user on next view load.
156      */
157     protected function showErrorNotification(string $message): void
158     {
159         session()->flash('error', $message);
160     }
161
162     /**
163      * Log an activity in the system.
164      *
165      * @param string|Loggable $detail
166      */
167     protected function logActivity(string $type, $detail = ''): void
168     {
169         Activity::add($type, $detail);
170     }
171
172     /**
173      * Get the validation rules for image files.
174      */
175     protected function getImageValidationRules(): array
176     {
177         return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];
178     }
179 }