]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Controller.php
move zip export into exportservice
[bookstack] / app / Http / Controllers / Controller.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Ownable;
6 use Illuminate\Foundation\Bus\DispatchesJobs;
7 use Illuminate\Foundation\Validation\ValidatesRequests;
8 use Illuminate\Http\Exceptions\HttpResponseException;
9 use Illuminate\Http\Request;
10 use Illuminate\Routing\Controller as BaseController;
11
12 abstract class Controller extends BaseController
13 {
14     use DispatchesJobs, ValidatesRequests;
15
16     /**
17      * Controller constructor.
18      */
19     public function __construct()
20     {
21         //
22     }
23
24     /**
25      * Check if the current user is signed in.
26      */
27     protected function isSignedIn(): bool
28     {
29         return auth()->check();
30     }
31
32     /**
33      * Stops the application and shows a permission error if
34      * the application is in demo mode.
35      */
36     protected function preventAccessInDemoMode()
37     {
38         if (config('app.env') === 'demo') {
39             $this->showPermissionError();
40         }
41     }
42
43     /**
44      * Adds the page title into the view.
45      * @param $title
46      */
47     public function setPageTitle($title)
48     {
49         view()->share('pageTitle', $title);
50     }
51
52     /**
53      * On a permission error redirect to home and display.
54      * the error as a notification.
55      */
56     protected function showPermissionError()
57     {
58         if (request()->wantsJson()) {
59             $response = response()->json(['error' => trans('errors.permissionJson')], 403);
60         } else {
61             $response = redirect('/');
62             $this->showErrorNotification(trans('errors.permission'));
63         }
64
65         throw new HttpResponseException($response);
66     }
67
68     /**
69      * Checks for a permission.
70      * @param string $permissionName
71      * @return bool|\Illuminate\Http\RedirectResponse
72      */
73     protected function checkPermission($permissionName)
74     {
75         if (!user() || !user()->can($permissionName)) {
76             $this->showPermissionError();
77         }
78         return true;
79     }
80
81     /**
82      * Check the current user's permissions against an ownable item.
83      * @param $permission
84      * @param Ownable $ownable
85      * @return bool
86      */
87     protected function checkOwnablePermission($permission, Ownable $ownable)
88     {
89         if (userCan($permission, $ownable)) {
90             return true;
91         }
92         return $this->showPermissionError();
93     }
94
95     /**
96      * Check if a user has a permission or bypass if the callback is true.
97      * @param $permissionName
98      * @param $callback
99      * @return bool
100      */
101     protected function checkPermissionOr($permissionName, $callback)
102     {
103         $callbackResult = $callback();
104         if ($callbackResult === false) {
105             $this->checkPermission($permissionName);
106         }
107         return true;
108     }
109
110     /**
111      * Check if the current user has a permission or bypass if the provided user
112      * id matches the current user.
113      * @param string $permissionName
114      * @param int $userId
115      * @return bool
116      */
117     protected function checkPermissionOrCurrentUser(string $permissionName, int $userId)
118     {
119         return $this->checkPermissionOr($permissionName, function () use ($userId) {
120             return $userId === user()->id;
121         });
122     }
123
124     /**
125      * Send back a json error message.
126      * @param string $messageText
127      * @param int $statusCode
128      * @return mixed
129      */
130     protected function jsonError($messageText = "", $statusCode = 500)
131     {
132         return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
133     }
134
135     /**
136      * Create the response for when a request fails validation.
137      * @param  \Illuminate\Http\Request  $request
138      * @param  array  $errors
139      * @return \Symfony\Component\HttpFoundation\Response
140      */
141     protected function buildFailedValidationResponse(Request $request, array $errors)
142     {
143         if ($request->expectsJson()) {
144             return response()->json(['validation' => $errors], 422);
145         }
146
147         return redirect()->to($this->getRedirectUrl())
148             ->withInput($request->input())
149             ->withErrors($errors, $this->errorBag());
150     }
151
152     /**
153      * Create a response that forces a download in the browser.
154      * @param string $content
155      * @param string $fileName
156      * @return \Illuminate\Http\Response
157      */
158     protected function downloadResponse(string $content, string $fileName)
159     {
160         return response()->make($content, 200, [
161             'Content-Type'        => 'application/octet-stream',
162             'Content-Disposition' => 'attachment; filename="' . $fileName . '"'
163         ]);
164     }
165
166     /**
167      * Show a positive, successful notification to the user on next view load.
168      * @param string $message
169      */
170     protected function showSuccessNotification(string $message)
171     {
172         session()->flash('success', $message);
173     }
174
175     /**
176      * Show a warning notification to the user on next view load.
177      * @param string $message
178      */
179     protected function showWarningNotification(string $message)
180     {
181         session()->flash('warning', $message);
182     }
183
184     /**
185      * Show an error notification to the user on next view load.
186      * @param string $message
187      */
188     protected function showErrorNotification(string $message)
189     {
190         session()->flash('error', $message);
191     }
192
193     /**
194      * Get the validation rules for image files.
195      */
196     protected function getImageValidationRules(): string
197     {
198         return 'image_extension|no_double_extension|mimes:jpeg,png,gif,webp';
199     }
200 }