6 * Get the path to a versioned file.
12 function versioned_asset($file = '')
14 static $version = null;
16 if (is_null($version)) {
17 $versionFile = base_path('version');
18 $version = trim(file_get_contents($versionFile));
22 if (config('app.env') === 'development') {
23 $additional = sha1_file(public_path($file));
26 $path = $file . '?version=' . urlencode($version) . $additional;
27 return baseUrl($path);
31 * Helper method to get the current User.
32 * Defaults to public 'Guest' user if not logged in.
33 * @return \BookStack\Auth\User
37 return auth()->user() ?: \BookStack\Auth\User::getDefault();
41 * Check if current user is a signed in user.
44 function signedInUser()
46 return auth()->user() && !auth()->user()->isDefault();
50 * Check if the current user has a permission.
51 * If an ownable element is passed in the jointPermissions are checked against
52 * that particular item.
54 * @param Ownable $ownable
57 function userCan($permission, Ownable $ownable = null)
59 if ($ownable === null) {
60 return user() && user()->can($permission);
63 // Check permission on ownable item
64 $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
65 return $permissionService->checkOwnableUserAccess($ownable, $permission);
69 * Helper to access system settings.
71 * @param bool $default
72 * @return bool|string|\BookStack\Settings\SettingService
74 function setting($key = null, $default = false)
76 $settingService = resolve(\BookStack\Settings\SettingService::class);
78 return $settingService;
80 return $settingService->get($key, $default);
84 * Helper to create url's relative to the applications root path.
86 * @param bool $forceAppDomain
89 function baseUrl($path, $forceAppDomain = false)
91 $isFullUrl = strpos($path, 'http') === 0;
92 if ($isFullUrl && !$forceAppDomain) {
96 $path = trim($path, '/');
97 $trimBase = rtrim(config('app.url'), '/');
99 // Remove non-specified domain if forced and we have a domain
100 if ($isFullUrl && $forceAppDomain) {
101 if (strpos($path, $trimBase) === 0) {
102 $path = trim(substr($path, strlen($trimBase) - 1));
104 $explodedPath = explode('/', $path);
105 $path = implode('/', array_splice($explodedPath, 3));
108 // Return normal url path if not specified in config
109 if (config('app.url') === '') {
113 return $trimBase . '/' . $path;
117 * Get an instance of the redirector.
118 * Overrides the default laravel redirect helper.
119 * Ensures it redirects even when the app is in a subdirectory.
121 * @param string|null $to
123 * @param array $headers
124 * @param bool $secure
125 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
127 function redirect($to = null, $status = 302, $headers = [], $secure = null)
130 return app('redirect');
135 return app('redirect')->to($to, $status, $headers, $secure);
139 * Get a path to a theme resource.
140 * @param string $path
141 * @return string|boolean
143 function theme_path($path = '')
145 $theme = config('view.theme');
150 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
154 * Get fetch an SVG icon as a string.
155 * Checks for icons defined within a custom theme before defaulting back
156 * to the 'resources/assets/icons' folder.
158 * Returns an empty string if icon file not found.
160 * @param array $attrs
163 function icon($name, $attrs = [])
165 $attrs = array_merge([
166 'class' => 'svg-icon',
170 foreach ($attrs as $attrName => $attr) {
171 $attrString .= $attrName . '="' . $attr . '" ';
174 $iconPath = resource_path('assets/icons/' . $name . '.svg');
175 $themeIconPath = theme_path('icons/' . $name . '.svg');
176 if ($themeIconPath && file_exists($themeIconPath)) {
177 $iconPath = $themeIconPath;
178 } else if (!file_exists($iconPath)) {
182 $fileContents = file_get_contents($iconPath);
183 return str_replace('<svg', '<svg' . $attrString, $fileContents);
187 * Generate a url with multiple parameters for sorting purposes.
188 * Works out the logic to set the correct sorting direction
189 * Discards empty parameters and allows overriding.
192 * @param array $overrideData
195 function sortUrl($path, $data, $overrideData = [])
197 $queryStringSections = [];
198 $queryData = array_merge($data, $overrideData);
200 // Change sorting direction is already sorted on current attribute
201 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
202 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
204 $queryData['order'] = 'asc';
207 foreach ($queryData as $name => $value) {
208 $trimmedVal = trim($value);
209 if ($trimmedVal === '') {
212 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
215 if (count($queryStringSections) === 0) {
219 return baseUrl($path . '?' . implode('&', $queryStringSections));