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);
133 function icon($name, $attrs = [])
135 $iconPath = resource_path('assets/icons/' . $name . '.svg');
137 foreach ($attrs as $attrName => $attr) {
138 $attrString .= $attrName . '="' . $attr . '" ';
140 $fileContents = file_get_contents($iconPath);
141 return str_replace('<svg', '<svg' . $attrString, $fileContents);
145 * Generate a url with multiple parameters for sorting purposes.
146 * Works out the logic to set the correct sorting direction
147 * Discards empty parameters and allows overriding.
150 * @param array $overrideData
153 function sortUrl($path, $data, $overrideData = [])
155 $queryStringSections = [];
156 $queryData = array_merge($data, $overrideData);
158 // Change sorting direction is already sorted on current attribute
159 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
160 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
162 $queryData['order'] = 'asc';
165 foreach ($queryData as $name => $value) {
166 $trimmedVal = trim($value);
167 if ($trimmedVal === '') {
170 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
173 if (count($queryStringSections) === 0) {
177 return baseUrl($path . '?' . implode('&', $queryStringSections));