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
63 * @return bool|string|\BookStack\Services\SettingService
65 function setting($key = null, $default = false)
67 $settingService = app(\BookStack\Services\SettingService::class);
68 if (is_null($key)) return $settingService;
69 return $settingService->get($key, $default);
73 * Helper to create url's relative to the applications root path.
75 * @param bool $forceAppDomain
78 function baseUrl($path, $forceAppDomain = false)
80 $isFullUrl = strpos($path, 'http') === 0;
81 if ($isFullUrl && !$forceAppDomain) return $path;
82 $path = trim($path, '/');
84 // Remove non-specified domain if forced and we have a domain
85 if ($isFullUrl && $forceAppDomain) {
86 $explodedPath = explode('/', $path);
87 $path = implode('/', array_splice($explodedPath, 3));
90 // Return normal url path if not specified in config
91 if (config('app.url') === '') {
95 return rtrim(config('app.url'), '/') . '/' . $path;
99 * Get an instance of the redirector.
100 * Overrides the default laravel redirect helper.
101 * Ensures it redirects even when the app is in a subdirectory.
103 * @param string|null $to
105 * @param array $headers
106 * @param bool $secure
107 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
109 function redirect($to = null, $status = 302, $headers = [], $secure = null)
112 return app('redirect');
117 return app('redirect')->to($to, $status, $headers, $secure);
121 * Generate a url with multiple parameters for sorting purposes.
122 * Works out the logic to set the correct sorting direction
123 * Discards empty parameters and allows overriding.
126 * @param array $overrideData
129 function sortUrl($path, $data, $overrideData = [])
131 $queryStringSections = [];
132 $queryData = array_merge($data, $overrideData);
134 // Change sorting direction is already sorted on current attribute
135 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
136 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
138 $queryData['order'] = 'asc';
141 foreach ($queryData as $name => $value) {
142 $trimmedVal = trim($value);
143 if ($trimmedVal === '') continue;
144 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
147 if (count($queryStringSections) === 0) return $path;
149 return baseUrl($path . '?' . implode('&', $queryStringSections));