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