6 * Get the path to a versioned file.
12 function versioned_asset($file = '')
14 // Don't require css and JS assets for testing
15 if (config('app.env') === 'testing') return '';
17 static $manifest = null;
18 $manifestPath = 'build/manifest.json';
20 if (is_null($manifest) && file_exists($manifestPath)) {
21 $manifest = json_decode(file_get_contents(public_path($manifestPath)), true);
22 } else if (!file_exists($manifestPath)) {
23 if (config('app.env') !== 'production') {
24 $path = public_path($manifestPath);
25 $error = "No {$path} file found, Ensure you have built the css/js assets using gulp.";
27 $error = "No {$manifestPath} file found, Ensure you are using the release version of BookStack";
29 throw new \Exception($error);
32 if (isset($manifest[$file])) {
33 return baseUrl($manifest[$file]);
36 throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
40 * Helper method to get the current User.
41 * Defaults to public 'Guest' user if not logged in.
42 * @return \BookStack\User
46 return auth()->user() ?: \BookStack\User::getDefault();
50 * Check if the current user has a permission.
51 * If an ownable element is passed in the jointPermissions are checked against
52 * that particular item.
54 * @param Ownable $ownable
57 function userCan($permission, Ownable $ownable = null)
59 if ($ownable === null) {
60 return user() && user()->can($permission);
63 // Check permission on ownable item
64 $permissionService = app(\BookStack\Services\PermissionService::class);
65 return $permissionService->checkOwnableUserAccess($ownable, $permission);
69 * Helper to access system settings.
71 * @param bool $default
74 function setting($key, $default = false)
76 $settingService = app(\BookStack\Services\SettingService::class);
77 return $settingService->get($key, $default);
81 * Helper to create url's relative to the applications root path.
83 * @param bool $forceAppDomain
86 function baseUrl($path, $forceAppDomain = false)
88 $isFullUrl = strpos($path, 'http') === 0;
89 if ($isFullUrl && !$forceAppDomain) return $path;
90 $path = trim($path, '/');
92 // Remove non-specified domain if forced and we have a domain
93 if ($isFullUrl && $forceAppDomain) {
94 $explodedPath = explode('/', $path);
95 $path = implode('/', array_splice($explodedPath, 3));
98 // Return normal url path if not specified in config
99 if (config('app.url') === '') {
103 return rtrim(config('app.url'), '/') . '/' . $path;
107 * Get an instance of the redirector.
108 * Overrides the default laravel redirect helper.
109 * Ensures it redirects even when the app is in a subdirectory.
111 * @param string|null $to
113 * @param array $headers
114 * @param bool $secure
115 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
117 function redirect($to = null, $status = 302, $headers = [], $secure = null)
120 return app('redirect');
125 return app('redirect')->to($to, $status, $headers, $secure);
129 * Generate a url with multiple parameters for sorting purposes.
130 * Works out the logic to set the correct sorting direction
131 * Discards empty parameters and allows overriding.
134 * @param array $overrideData
137 function sortUrl($path, $data, $overrideData = [])
139 $queryStringSections = [];
140 $queryData = array_merge($data, $overrideData);
142 // Change sorting direction is already sorted on current attribute
143 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
144 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
146 $queryData['order'] = 'asc';
149 foreach ($queryData as $name => $value) {
150 $trimmedVal = trim($value);
151 if ($trimmedVal === '') continue;
152 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
155 if (count($queryStringSections) === 0) return $path;
157 return baseUrl($path . '?' . implode('&', $queryStringSections));