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 = trim(substr($path, strlen($base) - 1));
128 $explodedPath = explode('/', $path);
129 $path = implode('/', array_splice($explodedPath, 3));
132 // Return normal url path if not specified in config
133 if (config('app.url') === '') {
137 return $base . '/' . $path;
141 * Get an instance of the redirector.
142 * Overrides the default laravel redirect helper.
143 * Ensures it redirects even when the app is in a subdirectory.
145 * @param string|null $to
147 * @param array $headers
148 * @param bool $secure
149 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
151 function redirect($to = null, $status = 302, $headers = [], $secure = null)
154 return app('redirect');
159 return app('redirect')->to($to, $status, $headers, $secure);
163 * Get a path to a theme resource.
164 * @param string $path
165 * @return string|boolean
167 function theme_path($path = '')
169 $theme = config('view.theme');
174 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
178 * Get fetch an SVG icon as a string.
179 * Checks for icons defined within a custom theme before defaulting back
180 * to the 'resources/assets/icons' folder.
182 * Returns an empty string if icon file not found.
184 * @param array $attrs
187 function icon($name, $attrs = [])
189 $attrs = array_merge([
190 'class' => 'svg-icon',
194 foreach ($attrs as $attrName => $attr) {
195 $attrString .= $attrName . '="' . $attr . '" ';
198 $iconPath = resource_path('assets/icons/' . $name . '.svg');
199 $themeIconPath = theme_path('icons/' . $name . '.svg');
200 if ($themeIconPath && file_exists($themeIconPath)) {
201 $iconPath = $themeIconPath;
202 } else if (!file_exists($iconPath)) {
206 $fileContents = file_get_contents($iconPath);
207 return str_replace('<svg', '<svg' . $attrString, $fileContents);
211 * Generate a url with multiple parameters for sorting purposes.
212 * Works out the logic to set the correct sorting direction
213 * Discards empty parameters and allows overriding.
216 * @param array $overrideData
219 function sortUrl($path, $data, $overrideData = [])
221 $queryStringSections = [];
222 $queryData = array_merge($data, $overrideData);
224 // Change sorting direction is already sorted on current attribute
225 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
226 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
228 $queryData['order'] = 'asc';
231 foreach ($queryData as $name => $value) {
232 $trimmedVal = trim($value);
233 if ($trimmedVal === '') {
236 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
239 if (count($queryStringSections) === 0) {
243 return baseUrl($path . '?' . implode('&', $queryStringSections));