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 * Check if the current user has the ability to create a page for an existing object
72 function userCanCreatePage()
74 // Check for create page permissions
75 $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
76 return $permissionService->checkAvailableCreatePageAccess();
80 * Helper to access system settings.
82 * @param bool $default
83 * @return bool|string|\BookStack\Settings\SettingService
85 function setting($key = null, $default = false)
87 $settingService = resolve(\BookStack\Settings\SettingService::class);
89 return $settingService;
91 return $settingService->get($key, $default);
95 * Helper to create url's relative to the applications root path.
97 * @param bool $forceAppDomain
100 function baseUrl($path, $forceAppDomain = false)
102 $isFullUrl = strpos($path, 'http') === 0;
103 if ($isFullUrl && !$forceAppDomain) {
107 $path = trim($path, '/');
108 $base = rtrim(config('app.url'), '/');
110 // Remove non-specified domain if forced and we have a domain
111 if ($isFullUrl && $forceAppDomain) {
112 if (!empty($base) && strpos($path, $base) === 0) {
113 $path = trim(substr($path, strlen($base) - 1));
115 $explodedPath = explode('/', $path);
116 $path = implode('/', array_splice($explodedPath, 3));
119 // Return normal url path if not specified in config
120 if (config('app.url') === '') {
124 return $base . '/' . $path;
128 * Get an instance of the redirector.
129 * Overrides the default laravel redirect helper.
130 * Ensures it redirects even when the app is in a subdirectory.
132 * @param string|null $to
134 * @param array $headers
135 * @param bool $secure
136 * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
138 function redirect($to = null, $status = 302, $headers = [], $secure = null)
141 return app('redirect');
146 return app('redirect')->to($to, $status, $headers, $secure);
150 * Get a path to a theme resource.
151 * @param string $path
152 * @return string|boolean
154 function theme_path($path = '')
156 $theme = config('view.theme');
161 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
165 * Get fetch an SVG icon as a string.
166 * Checks for icons defined within a custom theme before defaulting back
167 * to the 'resources/assets/icons' folder.
169 * Returns an empty string if icon file not found.
171 * @param array $attrs
174 function icon($name, $attrs = [])
176 $attrs = array_merge([
177 'class' => 'svg-icon',
181 foreach ($attrs as $attrName => $attr) {
182 $attrString .= $attrName . '="' . $attr . '" ';
185 $iconPath = resource_path('assets/icons/' . $name . '.svg');
186 $themeIconPath = theme_path('icons/' . $name . '.svg');
187 if ($themeIconPath && file_exists($themeIconPath)) {
188 $iconPath = $themeIconPath;
189 } else if (!file_exists($iconPath)) {
193 $fileContents = file_get_contents($iconPath);
194 return str_replace('<svg', '<svg' . $attrString, $fileContents);
198 * Generate a url with multiple parameters for sorting purposes.
199 * Works out the logic to set the correct sorting direction
200 * Discards empty parameters and allows overriding.
203 * @param array $overrideData
206 function sortUrl($path, $data, $overrideData = [])
208 $queryStringSections = [];
209 $queryData = array_merge($data, $overrideData);
211 // Change sorting direction is already sorted on current attribute
212 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
213 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
215 $queryData['order'] = 'asc';
218 foreach ($queryData as $name => $value) {
219 $trimmedVal = trim($value);
220 if ($trimmedVal === '') {
223 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
226 if (count($queryStringSections) === 0) {
230 return baseUrl($path . '?' . implode('&', $queryStringSections));