]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Added experimental breadcrumb traversal
[bookstack] / app / helpers.php
1 <?php
2
3 use BookStack\Ownable;
4
5 /**
6  * Get the path to a versioned file.
7  *
8  * @param  string $file
9  * @return string
10  * @throws Exception
11  */
12 function versioned_asset($file = '')
13 {
14     static $version = null;
15
16     if (is_null($version)) {
17         $versionFile = base_path('version');
18         $version = trim(file_get_contents($versionFile));
19     }
20
21     $additional = '';
22     if (config('app.env') === 'development') {
23         $additional = sha1_file(public_path($file));
24     }
25
26     $path = $file . '?version=' . urlencode($version) . $additional;
27     return baseUrl($path);
28 }
29
30 /**
31  * Helper method to get the current User.
32  * Defaults to public 'Guest' user if not logged in.
33  * @return \BookStack\Auth\User
34  */
35 function user()
36 {
37     return auth()->user() ?: \BookStack\Auth\User::getDefault();
38 }
39
40 /**
41  * Check if current user is a signed in user.
42  * @return bool
43  */
44 function signedInUser() : bool
45 {
46     return auth()->user() && !auth()->user()->isDefault();
47 }
48
49 /**
50  * Check if the current user has general access.
51  * @return bool
52  */
53 function hasAppAccess() : bool {
54     return !auth()->guest() || setting('app-public');
55 }
56
57 /**
58  * Check if the current user has a permission.
59  * If an ownable element is passed in the jointPermissions are checked against
60  * that particular item.
61  * @param $permission
62  * @param Ownable $ownable
63  * @return mixed
64  */
65 function userCan($permission, Ownable $ownable = null)
66 {
67     if ($ownable === null) {
68         return user() && user()->can($permission);
69     }
70
71     // Check permission on ownable item
72     $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
73     return $permissionService->checkOwnableUserAccess($ownable, $permission);
74 }
75
76 /**
77  * Helper to access system settings.
78  * @param $key
79  * @param bool $default
80  * @return bool|string|\BookStack\Settings\SettingService
81  */
82 function setting($key = null, $default = false)
83 {
84     $settingService = resolve(\BookStack\Settings\SettingService::class);
85     if (is_null($key)) {
86         return $settingService;
87     }
88     return $settingService->get($key, $default);
89 }
90
91 /**
92  * Helper to create url's relative to the applications root path.
93  * @param string $path
94  * @param bool $forceAppDomain
95  * @return string
96  */
97 function baseUrl($path, $forceAppDomain = false)
98 {
99     $isFullUrl = strpos($path, 'http') === 0;
100     if ($isFullUrl && !$forceAppDomain) {
101         return $path;
102     }
103
104     $path = trim($path, '/');
105     $base = rtrim(config('app.url'), '/');
106
107     // Remove non-specified domain if forced and we have a domain
108     if ($isFullUrl && $forceAppDomain) {
109         if (!empty($base) && strpos($path, $base) === 0) {
110             $path = trim(substr($path, strlen($base) - 1));
111         }
112         $explodedPath = explode('/', $path);
113         $path = implode('/', array_splice($explodedPath, 3));
114     }
115
116     // Return normal url path if not specified in config
117     if (config('app.url') === '') {
118         return url($path);
119     }
120
121     return $base . '/' . $path;
122 }
123
124 /**
125  * Get an instance of the redirector.
126  * Overrides the default laravel redirect helper.
127  * Ensures it redirects even when the app is in a subdirectory.
128  *
129  * @param  string|null  $to
130  * @param  int     $status
131  * @param  array   $headers
132  * @param  bool    $secure
133  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
134  */
135 function redirect($to = null, $status = 302, $headers = [], $secure = null)
136 {
137     if (is_null($to)) {
138         return app('redirect');
139     }
140
141     $to = baseUrl($to);
142
143     return app('redirect')->to($to, $status, $headers, $secure);
144 }
145
146 /**
147  * Get a path to a theme resource.
148  * @param string $path
149  * @return string|boolean
150  */
151 function theme_path($path = '')
152 {
153     $theme = config('view.theme');
154     if (!$theme) {
155         return false;
156     }
157
158     return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
159 }
160
161 /**
162  * Get fetch an SVG icon as a string.
163  * Checks for icons defined within a custom theme before defaulting back
164  * to the 'resources/assets/icons' folder.
165  *
166  * Returns an empty string if icon file not found.
167  * @param $name
168  * @param array $attrs
169  * @return mixed
170  */
171 function icon($name, $attrs = [])
172 {
173     $attrs = array_merge([
174         'class' => 'svg-icon',
175         'data-icon' => $name
176     ], $attrs);
177     $attrString = ' ';
178     foreach ($attrs as $attrName => $attr) {
179         $attrString .=  $attrName . '="' . $attr . '" ';
180     }
181
182     $iconPath = resource_path('assets/icons/' . $name . '.svg');
183     $themeIconPath = theme_path('icons/' . $name . '.svg');
184     if ($themeIconPath && file_exists($themeIconPath)) {
185         $iconPath = $themeIconPath;
186     } else if (!file_exists($iconPath)) {
187         return '';
188     }
189
190     $fileContents = file_get_contents($iconPath);
191     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
192 }
193
194 /**
195  * Generate a url with multiple parameters for sorting purposes.
196  * Works out the logic to set the correct sorting direction
197  * Discards empty parameters and allows overriding.
198  * @param $path
199  * @param array $data
200  * @param array $overrideData
201  * @return string
202  */
203 function sortUrl($path, $data, $overrideData = [])
204 {
205     $queryStringSections = [];
206     $queryData = array_merge($data, $overrideData);
207
208     // Change sorting direction is already sorted on current attribute
209     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
210         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
211     } else {
212         $queryData['order'] = 'asc';
213     }
214
215     foreach ($queryData as $name => $value) {
216         $trimmedVal = trim($value);
217         if ($trimmedVal === '') {
218             continue;
219         }
220         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
221     }
222
223     if (count($queryStringSections) === 0) {
224         return $path;
225     }
226
227     return baseUrl($path . '?' . implode('&', $queryStringSections));
228 }