3 use BookStack\Auth\Permissions\PermissionService;
7 * Get the path to a versioned file.
13 function versioned_asset($file = '')
15 static $version = null;
17 if (is_null($version)) {
18 $versionFile = base_path('version');
19 $version = trim(file_get_contents($versionFile));
23 if (config('app.env') === 'development') {
24 $additional = sha1_file(public_path($file));
27 $path = $file . '?version=' . urlencode($version) . $additional;
28 return baseUrl($path);
32 * Helper method to get the current User.
33 * Defaults to public 'Guest' user if not logged in.
34 * @return \BookStack\Auth\User
38 return auth()->user() ?: \BookStack\Auth\User::getDefault();
42 * Check if current user is a signed in user.
45 function signedInUser()
47 return auth()->user() && !auth()->user()->isDefault();
51 * Check if the current user has a permission.
52 * If an ownable element is passed in the jointPermissions are checked against
53 * that particular item.
54 * @param string $permission
55 * @param Ownable $ownable
58 function userCan(string $permission, Ownable $ownable = null)
60 if ($ownable === null) {
61 return user() && user()->can($permission);
64 // Check permission on ownable item
65 $permissionService = app(PermissionService::class);
66 return $permissionService->checkOwnableUserAccess($ownable, $permission);
70 * Check if the current user has the given permission
71 * on any item in the system.
72 * @param string $permission
75 function userCanOnAny(string $permission)
77 $permissionService = app(PermissionService::class);
78 return $permissionService->checkUserHasPermissionOnAnything($permission);
82 * Helper to access system settings.
84 * @param bool $default
85 * @return bool|string|\BookStack\Settings\SettingService
87 function setting($key = null, $default = false)
89 $settingService = resolve(\BookStack\Settings\SettingService::class);
91 return $settingService;
93 return $settingService->get($key, $default);
97 * Helper to create url's relative to the applications root path.
99 * @param bool $forceAppDomain
102 function baseUrl($path, $forceAppDomain = false)
104 $isFullUrl = strpos($path, 'http') === 0;
105 if ($isFullUrl && !$forceAppDomain) {
109 $path = trim($path, '/');
110 $base = rtrim(config('app.url'), '/');
112 // Remove non-specified domain if forced and we have a domain
113 if ($isFullUrl && $forceAppDomain) {
114 if (!empty($base) && strpos($path, $base) === 0) {
115 $path = trim(substr($path, strlen($base) - 1));
117 $explodedPath = explode('/', $path);
118 $path = implode('/', array_splice($explodedPath, 3));
121 // Return normal url path if not specified in config
122 if (config('app.url') === '') {
126 return $base . '/' . $path;
130 * Get an instance of the redirector.
131 * Overrides the default laravel redirect helper.
132 * Ensures it redirects even when the app is in a subdirectory.
134 * @param string|null $to
136 * @param array $headers
137 * @param bool $secure
138 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
140 function redirect($to = null, $status = 302, $headers = [], $secure = null)
143 return app('redirect');
148 return app('redirect')->to($to, $status, $headers, $secure);
152 * Get a path to a theme resource.
153 * @param string $path
154 * @return string|boolean
156 function theme_path($path = '')
158 $theme = config('view.theme');
163 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
167 * Get fetch an SVG icon as a string.
168 * Checks for icons defined within a custom theme before defaulting back
169 * to the 'resources/assets/icons' folder.
171 * Returns an empty string if icon file not found.
173 * @param array $attrs
176 function icon($name, $attrs = [])
178 $attrs = array_merge([
179 'class' => 'svg-icon',
183 foreach ($attrs as $attrName => $attr) {
184 $attrString .= $attrName . '="' . $attr . '" ';
187 $iconPath = resource_path('assets/icons/' . $name . '.svg');
188 $themeIconPath = theme_path('icons/' . $name . '.svg');
189 if ($themeIconPath && file_exists($themeIconPath)) {
190 $iconPath = $themeIconPath;
191 } else if (!file_exists($iconPath)) {
195 $fileContents = file_get_contents($iconPath);
196 return str_replace('<svg', '<svg' . $attrString, $fileContents);
200 * Generate a url with multiple parameters for sorting purposes.
201 * Works out the logic to set the correct sorting direction
202 * Discards empty parameters and allows overriding.
205 * @param array $overrideData
208 function sortUrl($path, $data, $overrideData = [])
210 $queryStringSections = [];
211 $queryData = array_merge($data, $overrideData);
213 // Change sorting direction is already sorted on current attribute
214 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
215 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
217 $queryData['order'] = 'asc';
220 foreach ($queryData as $name => $value) {
221 $trimmedVal = trim($value);
222 if ($trimmedVal === '') {
225 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
228 if (count($queryStringSections) === 0) {
232 return baseUrl($path . '?' . implode('&', $queryStringSections));