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