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 {
56 return !auth()->guest() || setting('app-public');
60 * Check if the current user has a permission.
61 * If an ownable element is passed in the jointPermissions are checked against
62 * that particular item.
63 * @param string $permission
64 * @param Ownable $ownable
67 function userCan(string $permission, Ownable $ownable = null)
69 if ($ownable === null) {
70 return user() && user()->can($permission);
73 // Check permission on ownable item
74 $permissionService = app(PermissionService::class);
75 return $permissionService->checkOwnableUserAccess($ownable, $permission);
79 * Check if the current user has the given permission
80 * on any item in the system.
81 * @param string $permission
82 * @param string|null $entityClass
85 function userCanOnAny(string $permission, string $entityClass = null)
87 $permissionService = app(PermissionService::class);
88 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
92 * Helper to access system settings.
94 * @param bool $default
95 * @return bool|string|\BookStack\Settings\SettingService
97 function setting($key = null, $default = false)
99 $settingService = resolve(\BookStack\Settings\SettingService::class);
101 return $settingService;
103 return $settingService->get($key, $default);
107 * Helper to create url's relative to the applications root path.
108 * @param string $path
109 * @param bool $forceAppDomain
112 function baseUrl($path, $forceAppDomain = false)
114 $isFullUrl = strpos($path, 'http') === 0;
115 if ($isFullUrl && !$forceAppDomain) {
119 $path = trim($path, '/');
120 $base = rtrim(config('app.url'), '/');
122 // Remove non-specified domain if forced and we have a domain
123 if ($isFullUrl && $forceAppDomain) {
124 if (!empty($base) && strpos($path, $base) === 0) {
125 $path = trim(substr($path, strlen($base) - 1));
127 $explodedPath = explode('/', $path);
128 $path = implode('/', array_splice($explodedPath, 3));
131 // Return normal url path if not specified in config
132 if (config('app.url') === '') {
136 return $base . '/' . $path;
140 * Get an instance of the redirector.
141 * Overrides the default laravel redirect helper.
142 * Ensures it redirects even when the app is in a subdirectory.
144 * @param string|null $to
146 * @param array $headers
147 * @param bool $secure
148 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
150 function redirect($to = null, $status = 302, $headers = [], $secure = null)
153 return app('redirect');
158 return app('redirect')->to($to, $status, $headers, $secure);
162 * Get a path to a theme resource.
163 * @param string $path
164 * @return string|boolean
166 function theme_path($path = '')
168 $theme = config('view.theme');
173 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
177 * Get fetch an SVG icon as a string.
178 * Checks for icons defined within a custom theme before defaulting back
179 * to the 'resources/assets/icons' folder.
181 * Returns an empty string if icon file not found.
183 * @param array $attrs
186 function icon($name, $attrs = [])
188 $attrs = array_merge([
189 'class' => 'svg-icon',
193 foreach ($attrs as $attrName => $attr) {
194 $attrString .= $attrName . '="' . $attr . '" ';
197 $iconPath = resource_path('assets/icons/' . $name . '.svg');
198 $themeIconPath = theme_path('icons/' . $name . '.svg');
199 if ($themeIconPath && file_exists($themeIconPath)) {
200 $iconPath = $themeIconPath;
201 } else if (!file_exists($iconPath)) {
205 $fileContents = file_get_contents($iconPath);
206 return str_replace('<svg', '<svg' . $attrString, $fileContents);
210 * Generate a url with multiple parameters for sorting purposes.
211 * Works out the logic to set the correct sorting direction
212 * Discards empty parameters and allows overriding.
215 * @param array $overrideData
218 function sortUrl($path, $data, $overrideData = [])
220 $queryStringSections = [];
221 $queryData = array_merge($data, $overrideData);
223 // Change sorting direction is already sorted on current attribute
224 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
225 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
227 $queryData['order'] = 'asc';
230 foreach ($queryData as $name => $value) {
231 $trimmedVal = trim($value);
232 if ($trimmedVal === '') {
235 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
238 if (count($queryStringSections) === 0) {
242 return baseUrl($path . '?' . implode('&', $queryStringSections));