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');
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 if ($isFullUrl && $forceAppDomain) {
83 $explodedPath = explode('/', $path);
84 $path = implode('/', array_splice($explodedPath, 3));
87 return rtrim(config('app.url'), '/') . '/' . $path;
91 * Get an instance of the redirector.
92 * Overrides the default laravel redirect helper.
93 * Ensures it redirects even when the app is in a subdirectory.
95 * @param string|null $to
97 * @param array $headers
99 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
101 function redirect($to = null, $status = 302, $headers = [], $secure = null)
104 return app('redirect');
109 return app('redirect')->to($to, $status, $headers, $secure);
113 * Generate a url with multiple parameters for sorting purposes.
114 * Works out the logic to set the correct sorting direction
115 * Discards empty parameters and allows overriding.
118 * @param array $overrideData
121 function sortUrl($path, $data, $overrideData = [])
123 $queryStringSections = [];
124 $queryData = array_merge($data, $overrideData);
126 // Change sorting direction is already sorted on current attribute
127 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
128 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
130 $queryData['order'] = 'asc';
133 foreach ($queryData as $name => $value) {
134 $trimmedVal = trim($value);
135 if ($trimmedVal === '') continue;
136 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
139 if (count($queryStringSections) === 0) return $path;
141 return baseUrl($path . '?' . implode('&', $queryStringSections));