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\User
37 return auth()->user() ?: \BookStack\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\Services\PermissionService::class);
65 return $permissionService->checkOwnableUserAccess($ownable, $permission);
69 * Helper to access system settings.
71 * @param bool $default
72 * @return bool|string|\BookStack\Services\SettingService
74 function setting($key = null, $default = false)
76 $settingService = resolve(\BookStack\Services\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) {
95 $path = trim($path, '/');
97 // Remove non-specified domain if forced and we have a domain
98 if ($isFullUrl && $forceAppDomain) {
99 $explodedPath = explode('/', $path);
100 $path = implode('/', array_splice($explodedPath, 3));
103 // Return normal url path if not specified in config
104 if (config('app.url') === '') {
108 return rtrim(config('app.url'), '/') . '/' . $path;
112 * Get an instance of the redirector.
113 * Overrides the default laravel redirect helper.
114 * Ensures it redirects even when the app is in a subdirectory.
116 * @param string|null $to
118 * @param array $headers
119 * @param bool $secure
120 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
122 function redirect($to = null, $status = 302, $headers = [], $secure = null)
125 return app('redirect');
130 return app('redirect')->to($to, $status, $headers, $secure);
134 * Get a path to a theme resource.
135 * @param string $path
136 * @return string|boolean
138 function theme_path($path = '')
140 $theme = config('view.theme');
145 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
149 * Get fetch an SVG icon as a string.
150 * Checks for icons defined within a custom theme before defaulting back
151 * to the 'resources/assets/icons' folder.
153 * Returns an empty string if icon file not found.
155 * @param array $attrs
158 function icon($name, $attrs = [])
160 $attrs = array_merge([
161 'class' => 'svg-icon',
165 foreach ($attrs as $attrName => $attr) {
166 $attrString .= $attrName . '="' . $attr . '" ';
169 $iconPath = resource_path('assets/icons/' . $name . '.svg');
170 $themeIconPath = theme_path('icons/' . $name . '.svg');
171 if ($themeIconPath && file_exists($themeIconPath)) {
172 $iconPath = $themeIconPath;
173 } else if (!file_exists($iconPath)) {
177 $fileContents = file_get_contents($iconPath);
178 return str_replace('<svg', '<svg' . $attrString, $fileContents);
182 * Generate a url with multiple parameters for sorting purposes.
183 * Works out the logic to set the correct sorting direction
184 * Discards empty parameters and allows overriding.
187 * @param array $overrideData
190 function sortUrl($path, $data, $overrideData = [])
192 $queryStringSections = [];
193 $queryData = array_merge($data, $overrideData);
195 // Change sorting direction is already sorted on current attribute
196 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
197 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
199 $queryData['order'] = 'asc';
202 foreach ($queryData as $name => $value) {
203 $trimmedVal = trim($value);
204 if ($trimmedVal === '') {
207 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
210 if (count($queryStringSections) === 0) {
214 return baseUrl($path . '?' . implode('&', $queryStringSections));