3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Entity;
8 * Get the path to a versioned file.
14 function versioned_asset($file = '')
16 static $version = null;
18 if (is_null($version)) {
19 $versionFile = base_path('version');
20 $version = trim(file_get_contents($versionFile));
24 if (config('app.env') === 'development') {
25 $additional = sha1_file(public_path($file));
28 $path = $file . '?version=' . urlencode($version) . $additional;
29 return baseUrl($path);
33 * Helper method to get the current User.
34 * Defaults to public 'Guest' user if not logged in.
35 * @return \BookStack\Auth\User
39 return auth()->user() ?: \BookStack\Auth\User::getDefault();
43 * Check if current user is a signed in user.
46 function signedInUser() : bool
48 return auth()->user() && !auth()->user()->isDefault();
52 * Check if the current user has general access.
55 function hasAppAccess() : bool
57 return !auth()->guest() || setting('app-public');
61 * Check if the current user has a permission.
62 * If an ownable element is passed in the jointPermissions are checked against
63 * that particular item.
64 * @param string $permission
65 * @param Ownable $ownable
68 function userCan(string $permission, Ownable $ownable = null)
70 if ($ownable === null) {
71 return user() && user()->can($permission);
74 // Check permission on ownable item
75 $permissionService = app(PermissionService::class);
76 return $permissionService->checkOwnableUserAccess($ownable, $permission);
80 * Check if the current user has the given permission
81 * on any item in the system.
82 * @param string $permission
83 * @param string|null $entityClass
86 function userCanOnAny(string $permission, string $entityClass = null)
88 $permissionService = app(PermissionService::class);
89 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
93 * Helper to access system settings.
95 * @param bool $default
96 * @return bool|string|\BookStack\Settings\SettingService
98 function setting($key = null, $default = false)
100 $settingService = resolve(\BookStack\Settings\SettingService::class);
102 return $settingService;
104 return $settingService->get($key, $default);
108 * Helper to create url's relative to the applications root path.
109 * @param string $path
110 * @param bool $forceAppDomain
113 function baseUrl($path, $forceAppDomain = false)
115 $isFullUrl = strpos($path, 'http') === 0;
116 if ($isFullUrl && !$forceAppDomain) {
120 $path = trim($path, '/');
121 $base = rtrim(config('app.url'), '/');
123 // Remove non-specified domain if forced and we have a domain
124 if ($isFullUrl && $forceAppDomain) {
125 if (!empty($base) && strpos($path, $base) === 0) {
126 $path = mb_substr($path, mb_strlen($base));
128 $explodedPath = explode('/', $path);
129 $path = implode('/', array_splice($explodedPath, 3));
133 // Return normal url path if not specified in config
134 if (config('app.url') === '') {
138 return $base . '/' . ltrim($path, '/');
142 * Get an instance of the redirector.
143 * Overrides the default laravel redirect helper.
144 * Ensures it redirects even when the app is in a subdirectory.
146 * @param string|null $to
148 * @param array $headers
149 * @param bool $secure
150 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
152 function redirect($to = null, $status = 302, $headers = [], $secure = null)
155 return app('redirect');
160 return app('redirect')->to($to, $status, $headers, $secure);
164 * Get a path to a theme resource.
165 * @param string $path
166 * @return string|boolean
168 function theme_path($path = '')
170 $theme = config('view.theme');
175 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
179 * Get fetch an SVG icon as a string.
180 * Checks for icons defined within a custom theme before defaulting back
181 * to the 'resources/assets/icons' folder.
183 * Returns an empty string if icon file not found.
185 * @param array $attrs
188 function icon($name, $attrs = [])
190 $attrs = array_merge([
191 'class' => 'svg-icon',
195 foreach ($attrs as $attrName => $attr) {
196 $attrString .= $attrName . '="' . $attr . '" ';
199 $iconPath = resource_path('assets/icons/' . $name . '.svg');
200 $themeIconPath = theme_path('icons/' . $name . '.svg');
201 if ($themeIconPath && file_exists($themeIconPath)) {
202 $iconPath = $themeIconPath;
203 } else if (!file_exists($iconPath)) {
207 $fileContents = file_get_contents($iconPath);
208 return str_replace('<svg', '<svg' . $attrString, $fileContents);
212 * Generate a url with multiple parameters for sorting purposes.
213 * Works out the logic to set the correct sorting direction
214 * Discards empty parameters and allows overriding.
217 * @param array $overrideData
220 function sortUrl($path, $data, $overrideData = [])
222 $queryStringSections = [];
223 $queryData = array_merge($data, $overrideData);
225 // Change sorting direction is already sorted on current attribute
226 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
227 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
229 $queryData['order'] = 'asc';
232 foreach ($queryData as $name => $value) {
233 $trimmedVal = trim($value);
234 if ($trimmedVal === '') {
237 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
240 if (count($queryStringSections) === 0) {
244 return baseUrl($path . '?' . implode('&', $queryStringSections));