6 * Get the path to a versioned file.
11 * @throws \InvalidArgumentException
13 function versioned_asset($file)
15 static $manifest = null;
17 if (is_null($manifest)) {
18 $manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
21 if (isset($manifest[$file])) {
22 return baseUrl($manifest[$file]);
25 if (file_exists(public_path($file))) {
26 return baseUrl($file);
29 throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
33 * Check if the current user has a permission.
34 * If an ownable element is passed in the jointPermissions are checked against
35 * that particular item.
37 * @param Ownable $ownable
40 function userCan($permission, Ownable $ownable = null)
42 if ($ownable === null) {
43 return auth()->user() && auth()->user()->can($permission);
46 // Check permission on ownable item
47 $permissionService = app(\BookStack\Services\PermissionService::class);
48 return $permissionService->checkOwnableUserAccess($ownable, $permission);
52 * Helper to access system settings.
54 * @param bool $default
57 function setting($key, $default = false)
59 $settingService = app('BookStack\Services\SettingService');
60 return $settingService->get($key, $default);
64 * Helper to create url's relative to the applications root path.
66 * @param bool $forceAppDomain
69 function baseUrl($path, $forceAppDomain = false)
71 $isFullUrl = strpos($path, 'http') === 0;
72 if ($isFullUrl && !$forceAppDomain) return $path;
73 $path = trim($path, '/');
75 if ($isFullUrl && $forceAppDomain) {
76 $explodedPath = explode('/', $path);
77 $path = implode('/', array_splice($explodedPath, 3));
80 return rtrim(config('app.url'), '/') . '/' . $path;
84 * Get an instance of the redirector.
85 * Overrides the default laravel redirect helper.
86 * Ensures it redirects even when the app is in a subdirectory.
88 * @param string|null $to
90 * @param array $headers
92 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
94 function redirect($to = null, $status = 302, $headers = [], $secure = null)
97 return app('redirect');
102 return app('redirect')->to($to, $status, $headers, $secure);
106 * Generate a url with multiple parameters for sorting purposes.
107 * Works out the logic to set the correct sorting direction
108 * Discards empty parameters and allows overriding.
111 * @param array $overrideData
114 function sortUrl($path, $data, $overrideData = [])
116 $queryStringSections = [];
117 $queryData = array_merge($data, $overrideData);
119 // Change sorting direction is already sorted on current attribute
120 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
121 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
123 $queryData['order'] = 'asc';
126 foreach ($queryData as $name => $value) {
127 $trimmedVal = trim($value);
128 if ($trimmedVal === '') continue;
129 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
132 if (count($queryStringSections) === 0) return $path;
134 return baseUrl($path . '?' . implode('&', $queryStringSections));