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()
48 return auth()->user() && !auth()->user()->isDefault();
52 * Check if the current user has a permission.
53 * If an ownable element is passed in the jointPermissions are checked against
54 * that particular item.
55 * @param string $permission
56 * @param Ownable $ownable
59 function userCan(string $permission, Ownable $ownable = null)
61 if ($ownable === null) {
62 return user() && user()->can($permission);
65 // Check permission on ownable item
66 $permissionService = app(PermissionService::class);
67 return $permissionService->checkOwnableUserAccess($ownable, $permission);
71 * Check if the current user has the given permission
72 * on any item in the system.
73 * @param string $permission
74 * @param string|null $entityClass
77 function userCanOnAny(string $permission, string $entityClass = null)
79 $permissionService = app(PermissionService::class);
80 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
84 * Helper to access system settings.
86 * @param bool $default
87 * @return bool|string|\BookStack\Settings\SettingService
89 function setting($key = null, $default = false)
91 $settingService = resolve(\BookStack\Settings\SettingService::class);
93 return $settingService;
95 return $settingService->get($key, $default);
99 * Helper to create url's relative to the applications root path.
100 * @param string $path
101 * @param bool $forceAppDomain
104 function baseUrl($path, $forceAppDomain = false)
106 $isFullUrl = strpos($path, 'http') === 0;
107 if ($isFullUrl && !$forceAppDomain) {
111 $path = trim($path, '/');
112 $base = rtrim(config('app.url'), '/');
114 // Remove non-specified domain if forced and we have a domain
115 if ($isFullUrl && $forceAppDomain) {
116 if (!empty($base) && strpos($path, $base) === 0) {
117 $path = trim(substr($path, strlen($base) - 1));
119 $explodedPath = explode('/', $path);
120 $path = implode('/', array_splice($explodedPath, 3));
123 // Return normal url path if not specified in config
124 if (config('app.url') === '') {
128 return $base . '/' . $path;
132 * Get an instance of the redirector.
133 * Overrides the default laravel redirect helper.
134 * Ensures it redirects even when the app is in a subdirectory.
136 * @param string|null $to
138 * @param array $headers
139 * @param bool $secure
140 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
142 function redirect($to = null, $status = 302, $headers = [], $secure = null)
145 return app('redirect');
150 return app('redirect')->to($to, $status, $headers, $secure);
154 * Get a path to a theme resource.
155 * @param string $path
156 * @return string|boolean
158 function theme_path($path = '')
160 $theme = config('view.theme');
165 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
169 * Get fetch an SVG icon as a string.
170 * Checks for icons defined within a custom theme before defaulting back
171 * to the 'resources/assets/icons' folder.
173 * Returns an empty string if icon file not found.
175 * @param array $attrs
178 function icon($name, $attrs = [])
180 $attrs = array_merge([
181 'class' => 'svg-icon',
185 foreach ($attrs as $attrName => $attr) {
186 $attrString .= $attrName . '="' . $attr . '" ';
189 $iconPath = resource_path('assets/icons/' . $name . '.svg');
190 $themeIconPath = theme_path('icons/' . $name . '.svg');
191 if ($themeIconPath && file_exists($themeIconPath)) {
192 $iconPath = $themeIconPath;
193 } else if (!file_exists($iconPath)) {
197 $fileContents = file_get_contents($iconPath);
198 return str_replace('<svg', '<svg' . $attrString, $fileContents);
202 * Generate a url with multiple parameters for sorting purposes.
203 * Works out the logic to set the correct sorting direction
204 * Discards empty parameters and allows overriding.
207 * @param array $overrideData
210 function sortUrl($path, $data, $overrideData = [])
212 $queryStringSections = [];
213 $queryData = array_merge($data, $overrideData);
215 // Change sorting direction is already sorted on current attribute
216 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
217 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
219 $queryData['order'] = 'asc';
222 foreach ($queryData as $name => $value) {
223 $trimmedVal = trim($value);
224 if ($trimmedVal === '') {
227 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
230 if (count($queryStringSections) === 0) {
234 return baseUrl($path . '?' . implode('&', $queryStringSections));