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\Auth\User
37 return auth()->user() ?: \BookStack\Auth\User::getDefault();
41 * Check if current user is a signed in user.
44 function signedInUser() : bool
46 return auth()->user() && !auth()->user()->isDefault();
50 * Check if the current user has general access.
53 function hasAppAccess() : bool {
54 return !auth()->guest() || setting('app-public');
58 * Check if the current user has a permission.
59 * If an ownable element is passed in the jointPermissions are checked against
60 * that particular item.
62 * @param Ownable $ownable
65 function userCan($permission, Ownable $ownable = null)
67 if ($ownable === null) {
68 return user() && user()->can($permission);
71 // Check permission on ownable item
72 $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
73 return $permissionService->checkOwnableUserAccess($ownable, $permission);
77 * Helper to access system settings.
79 * @param bool $default
80 * @return bool|string|\BookStack\Settings\SettingService
82 function setting($key = null, $default = false)
84 $settingService = resolve(\BookStack\Settings\SettingService::class);
86 return $settingService;
88 return $settingService->get($key, $default);
92 * Helper to create url's relative to the applications root path.
94 * @param bool $forceAppDomain
97 function baseUrl($path, $forceAppDomain = false)
99 $isFullUrl = strpos($path, 'http') === 0;
100 if ($isFullUrl && !$forceAppDomain) {
104 $path = trim($path, '/');
105 $base = rtrim(config('app.url'), '/');
107 // Remove non-specified domain if forced and we have a domain
108 if ($isFullUrl && $forceAppDomain) {
109 if (!empty($base) && strpos($path, $base) === 0) {
110 $path = trim(substr($path, strlen($base) - 1));
112 $explodedPath = explode('/', $path);
113 $path = implode('/', array_splice($explodedPath, 3));
116 // Return normal url path if not specified in config
117 if (config('app.url') === '') {
121 return $base . '/' . $path;
125 * Get an instance of the redirector.
126 * Overrides the default laravel redirect helper.
127 * Ensures it redirects even when the app is in a subdirectory.
129 * @param string|null $to
131 * @param array $headers
132 * @param bool $secure
133 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
135 function redirect($to = null, $status = 302, $headers = [], $secure = null)
138 return app('redirect');
143 return app('redirect')->to($to, $status, $headers, $secure);
147 * Get a path to a theme resource.
148 * @param string $path
149 * @return string|boolean
151 function theme_path($path = '')
153 $theme = config('view.theme');
158 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
162 * Get fetch an SVG icon as a string.
163 * Checks for icons defined within a custom theme before defaulting back
164 * to the 'resources/assets/icons' folder.
166 * Returns an empty string if icon file not found.
168 * @param array $attrs
171 function icon($name, $attrs = [])
173 $attrs = array_merge([
174 'class' => 'svg-icon',
178 foreach ($attrs as $attrName => $attr) {
179 $attrString .= $attrName . '="' . $attr . '" ';
182 $iconPath = resource_path('assets/icons/' . $name . '.svg');
183 $themeIconPath = theme_path('icons/' . $name . '.svg');
184 if ($themeIconPath && file_exists($themeIconPath)) {
185 $iconPath = $themeIconPath;
186 } else if (!file_exists($iconPath)) {
190 $fileContents = file_get_contents($iconPath);
191 return str_replace('<svg', '<svg' . $attrString, $fileContents);
195 * Generate a url with multiple parameters for sorting purposes.
196 * Works out the logic to set the correct sorting direction
197 * Discards empty parameters and allows overriding.
200 * @param array $overrideData
203 function sortUrl($path, $data, $overrideData = [])
205 $queryStringSections = [];
206 $queryData = array_merge($data, $overrideData);
208 // Change sorting direction is already sorted on current attribute
209 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
210 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
212 $queryData['order'] = 'asc';
215 foreach ($queryData as $name => $value) {
216 $trimmedVal = trim($value);
217 if ($trimmedVal === '') {
220 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
223 if (count($queryStringSections) === 0) {
227 return baseUrl($path . '?' . implode('&', $queryStringSections));