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 current user is a signed in user.
44 function signedInUser()
46 return auth()->user() && !auth()->user()->isDefault();
50 * Check if the current user has a permission.
51 * If an ownable element is passed in the jointPermissions are checked against
52 * that particular item.
54 * @param Ownable $ownable
57 function userCan($permission, Ownable $ownable = null)
59 if ($ownable === null) {
60 return user() && user()->can($permission);
63 // Check permission on ownable item
64 $permissionService = app(\BookStack\Services\PermissionService::class);
65 return $permissionService->checkOwnableUserAccess($ownable, $permission);
69 * Helper to access system settings.
71 * @param bool $default
72 * @return bool|string|\BookStack\Services\SettingService
74 function setting($key = null, $default = false)
76 $settingService = app(\BookStack\Services\SettingService::class);
77 if (is_null($key)) return $settingService;
78 return $settingService->get($key, $default);
82 * Helper to create url's relative to the applications root path.
84 * @param bool $forceAppDomain
87 function baseUrl($path, $forceAppDomain = false)
89 $isFullUrl = strpos($path, 'http') === 0;
90 if ($isFullUrl && !$forceAppDomain) return $path;
91 $path = trim($path, '/');
93 // Remove non-specified domain if forced and we have a domain
94 if ($isFullUrl && $forceAppDomain) {
95 $explodedPath = explode('/', $path);
96 $path = implode('/', array_splice($explodedPath, 3));
99 // Return normal url path if not specified in config
100 if (config('app.url') === '') {
104 return rtrim(config('app.url'), '/') . '/' . $path;
108 * Get an instance of the redirector.
109 * Overrides the default laravel redirect helper.
110 * Ensures it redirects even when the app is in a subdirectory.
112 * @param string|null $to
114 * @param array $headers
115 * @param bool $secure
116 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
118 function redirect($to = null, $status = 302, $headers = [], $secure = null)
121 return app('redirect');
126 return app('redirect')->to($to, $status, $headers, $secure);
130 * Generate a url with multiple parameters for sorting purposes.
131 * Works out the logic to set the correct sorting direction
132 * Discards empty parameters and allows overriding.
135 * @param array $overrideData
138 function sortUrl($path, $data, $overrideData = [])
140 $queryStringSections = [];
141 $queryData = array_merge($data, $overrideData);
143 // Change sorting direction is already sorted on current attribute
144 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
145 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
147 $queryData['order'] = 'asc';
150 foreach ($queryData as $name => $value) {
151 $trimmedVal = trim($value);
152 if ($trimmedVal === '') continue;
153 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
156 if (count($queryStringSections) === 0) return $path;
158 return baseUrl($path . '?' . implode('&', $queryStringSections));