6 * Get the path to a versioned file.
12 function versioned_asset($file = '')
14 static $version = null;
16 if (is_null($version)) {
17 $versionFile = base_path('version');
18 $version = trim(file_get_contents($versionFile));
22 if (config('app.env') === 'development') {
23 $additional = sha1_file(public_path($file));
26 $path = $file . '?version=' . urlencode($version) . $additional;
27 return baseUrl($path);
31 * Helper method to get the current User.
32 * Defaults to public 'Guest' user if not logged in.
33 * @return \BookStack\User
37 return auth()->user() ?: \BookStack\User::getDefault();
41 * Check if the current user has a permission.
42 * If an ownable element is passed in the jointPermissions are checked against
43 * that particular item.
45 * @param Ownable $ownable
48 function userCan($permission, Ownable $ownable = null)
50 if ($ownable === null) {
51 return user() && user()->can($permission);
54 // Check permission on ownable item
55 $permissionService = app(\BookStack\Services\PermissionService::class);
56 return $permissionService->checkOwnableUserAccess($ownable, $permission);
60 * Helper to access system settings.
62 * @param bool $default
65 function setting($key, $default = false)
67 $settingService = app(\BookStack\Services\SettingService::class);
68 return $settingService->get($key, $default);
72 * Helper to create url's relative to the applications root path.
74 * @param bool $forceAppDomain
77 function baseUrl($path, $forceAppDomain = false)
79 $isFullUrl = strpos($path, 'http') === 0;
80 if ($isFullUrl && !$forceAppDomain) return $path;
81 $path = trim($path, '/');
83 // Remove non-specified domain if forced and we have a domain
84 if ($isFullUrl && $forceAppDomain) {
85 $explodedPath = explode('/', $path);
86 $path = implode('/', array_splice($explodedPath, 3));
89 // Return normal url path if not specified in config
90 if (config('app.url') === '') {
94 return rtrim(config('app.url'), '/') . '/' . $path;
98 * Get an instance of the redirector.
99 * Overrides the default laravel redirect helper.
100 * Ensures it redirects even when the app is in a subdirectory.
102 * @param string|null $to
104 * @param array $headers
105 * @param bool $secure
106 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
108 function redirect($to = null, $status = 302, $headers = [], $secure = null)
111 return app('redirect');
116 return app('redirect')->to($to, $status, $headers, $secure);
120 * Generate a url with multiple parameters for sorting purposes.
121 * Works out the logic to set the correct sorting direction
122 * Discards empty parameters and allows overriding.
125 * @param array $overrideData
128 function sortUrl($path, $data, $overrideData = [])
130 $queryStringSections = [];
131 $queryData = array_merge($data, $overrideData);
133 // Change sorting direction is already sorted on current attribute
134 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
135 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
137 $queryData['order'] = 'asc';
140 foreach ($queryData as $name => $value) {
141 $trimmedVal = trim($value);
142 if ($trimmedVal === '') continue;
143 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
146 if (count($queryStringSections) === 0) return $path;
148 return baseUrl($path . '?' . implode('&', $queryStringSections));