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 * Check if the current user has a permission.
41 * If an ownable element is passed in the jointPermissions are checked against
42 * that particular item.
44 * @param Ownable $ownable
47 function userCan($permission, Ownable $ownable = null)
49 if ($ownable === null) {
50 return auth()->user() && auth()->user()->can($permission);
53 // Check permission on ownable item
54 $permissionService = app(\BookStack\Services\PermissionService::class);
55 return $permissionService->checkOwnableUserAccess($ownable, $permission);
59 * Helper to access system settings.
61 * @param bool $default
64 function setting($key, $default = false)
66 $settingService = app(\BookStack\Services\SettingService::class);
67 return $settingService->get($key, $default);
71 * Helper to create url's relative to the applications root path.
73 * @param bool $forceAppDomain
76 function baseUrl($path, $forceAppDomain = false)
78 $isFullUrl = strpos($path, 'http') === 0;
79 if ($isFullUrl && !$forceAppDomain) return $path;
80 $path = trim($path, '/');
82 // Remove non-specified domain if forced and we have a domain
83 if ($isFullUrl && $forceAppDomain) {
84 $explodedPath = explode('/', $path);
85 $path = implode('/', array_splice($explodedPath, 3));
88 // Return normal url path if not specified in config
89 if (config('app.url') === '') {
93 return rtrim(config('app.url'), '/') . '/' . $path;
97 * Get an instance of the redirector.
98 * Overrides the default laravel redirect helper.
99 * Ensures it redirects even when the app is in a subdirectory.
101 * @param string|null $to
103 * @param array $headers
104 * @param bool $secure
105 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
107 function redirect($to = null, $status = 302, $headers = [], $secure = null)
110 return app('redirect');
115 return app('redirect')->to($to, $status, $headers, $secure);
119 * Generate a url with multiple parameters for sorting purposes.
120 * Works out the logic to set the correct sorting direction
121 * Discards empty parameters and allows overriding.
124 * @param array $overrideData
127 function sortUrl($path, $data, $overrideData = [])
129 $queryStringSections = [];
130 $queryData = array_merge($data, $overrideData);
132 // Change sorting direction is already sorted on current attribute
133 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
134 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
136 $queryData['order'] = 'asc';
139 foreach ($queryData as $name => $value) {
140 $trimmedVal = trim($value);
141 if ($trimmedVal === '') continue;
142 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
145 if (count($queryStringSections) === 0) return $path;
147 return baseUrl($path . '?' . implode('&', $queryStringSections));