5 if (!function_exists('versioned_asset')) {
7 * Get the path to a versioned file.
12 * @throws \InvalidArgumentException
14 function versioned_asset($file)
16 static $manifest = null;
18 if (is_null($manifest)) {
19 $manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
22 if (isset($manifest[$file])) {
23 return baseUrl($manifest[$file]);
26 if (file_exists(public_path($file))) {
27 return baseUrl($file);
30 throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
35 * Check if the current user has a permission.
36 * If an ownable element is passed in the jointPermissions are checked against
37 * that particular item.
39 * @param Ownable $ownable
42 function userCan($permission, Ownable $ownable = null)
44 if ($ownable === null) {
45 return auth()->user() && auth()->user()->can($permission);
48 // Check permission on ownable item
49 $permissionService = app(\BookStack\Services\PermissionService::class);
50 return $permissionService->checkOwnableUserAccess($ownable, $permission);
54 * Helper to access system settings.
56 * @param bool $default
59 function setting($key, $default = false)
61 $settingService = app('BookStack\Services\SettingService');
62 return $settingService->get($key, $default);
66 * Helper to create url's relative to the applications root path.
68 * @param bool $forceAppDomain
71 function baseUrl($path, $forceAppDomain = false)
73 $isFullUrl = strpos($path, 'http') === 0;
74 if ($isFullUrl && !$forceAppDomain) return $path;
75 $path = trim($path, '/');
77 if ($isFullUrl && $forceAppDomain) {
78 $explodedPath = explode('/', $path);
79 $path = implode('/', array_splice($explodedPath, 3));
82 return rtrim(config('app.url'), '/') . '/' . $path;
86 * Get an instance of the redirector.
87 * Overrides the default laravel redirect helper.
88 * Ensures it redirects even when the app is in a subdirectory.
90 * @param string|null $to
92 * @param array $headers
94 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
96 function redirect($to = null, $status = 302, $headers = [], $secure = null)
99 return app('redirect');
104 return app('redirect')->to($to, $status, $headers, $secure);
108 * Generate a url with multiple parameters for sorting purposes.
109 * Works out the logic to set the correct sorting direction
110 * Discards empty parameters and allows overriding.
113 * @param array $overrideData
116 function sortUrl($path, $data, $overrideData = [])
118 $queryStringSections = [];
119 $queryData = array_merge($data, $overrideData);
121 // Change sorting direction is already sorted on current attribute
122 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
123 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
125 $queryData['order'] = 'asc';
128 foreach ($queryData as $name => $value) {
129 $trimmedVal = trim($value);
130 if ($trimmedVal === '') continue;
131 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
134 if (count($queryStringSections) === 0) return $path;
136 return baseUrl($path . '?' . implode('&', $queryStringSections));