]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Merge pull request #1072 from CliffyPrime/german_update
[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()
45 {
46     return auth()->user() && !auth()->user()->isDefault();
47 }
48
49 /**
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.
53  * @param $permission
54  * @param Ownable $ownable
55  * @return mixed
56  */
57 function userCan($permission, Ownable $ownable = null)
58 {
59     if ($ownable === null) {
60         return user() && user()->can($permission);
61     }
62
63     // Check permission on ownable item
64     $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
65     return $permissionService->checkOwnableUserAccess($ownable, $permission);
66 }
67
68 /**
69  * Helper to access system settings.
70  * @param $key
71  * @param bool $default
72  * @return bool|string|\BookStack\Settings\SettingService
73  */
74 function setting($key = null, $default = false)
75 {
76     $settingService = resolve(\BookStack\Settings\SettingService::class);
77     if (is_null($key)) {
78         return $settingService;
79     }
80     return $settingService->get($key, $default);
81 }
82
83 /**
84  * Helper to create url's relative to the applications root path.
85  * @param string $path
86  * @param bool $forceAppDomain
87  * @return string
88  */
89 function baseUrl($path, $forceAppDomain = false)
90 {
91     $isFullUrl = strpos($path, 'http') === 0;
92     if ($isFullUrl && !$forceAppDomain) {
93         return $path;
94     }
95
96     $path = trim($path, '/');
97     $base = rtrim(config('app.url'), '/');
98
99     // Remove non-specified domain if forced and we have a domain
100     if ($isFullUrl && $forceAppDomain) {
101         if (!empty($base) && strpos($path, $base) === 0) {
102             $path = trim(substr($path, strlen($base) - 1));
103         }
104         $explodedPath = explode('/', $path);
105         $path = implode('/', array_splice($explodedPath, 3));
106     }
107
108     // Return normal url path if not specified in config
109     if (config('app.url') === '') {
110         return url($path);
111     }
112
113     return $base . '/' . $path;
114 }
115
116 /**
117  * Get an instance of the redirector.
118  * Overrides the default laravel redirect helper.
119  * Ensures it redirects even when the app is in a subdirectory.
120  *
121  * @param  string|null  $to
122  * @param  int     $status
123  * @param  array   $headers
124  * @param  bool    $secure
125  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
126  */
127 function redirect($to = null, $status = 302, $headers = [], $secure = null)
128 {
129     if (is_null($to)) {
130         return app('redirect');
131     }
132
133     $to = baseUrl($to);
134
135     return app('redirect')->to($to, $status, $headers, $secure);
136 }
137
138 /**
139  * Get a path to a theme resource.
140  * @param string $path
141  * @return string|boolean
142  */
143 function theme_path($path = '')
144 {
145     $theme = config('view.theme');
146     if (!$theme) {
147         return false;
148     }
149
150     return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
151 }
152
153 /**
154  * Get fetch an SVG icon as a string.
155  * Checks for icons defined within a custom theme before defaulting back
156  * to the 'resources/assets/icons' folder.
157  *
158  * Returns an empty string if icon file not found.
159  * @param $name
160  * @param array $attrs
161  * @return mixed
162  */
163 function icon($name, $attrs = [])
164 {
165     $attrs = array_merge([
166         'class' => 'svg-icon',
167         'data-icon' => $name
168     ], $attrs);
169     $attrString = ' ';
170     foreach ($attrs as $attrName => $attr) {
171         $attrString .=  $attrName . '="' . $attr . '" ';
172     }
173
174     $iconPath = resource_path('assets/icons/' . $name . '.svg');
175     $themeIconPath = theme_path('icons/' . $name . '.svg');
176     if ($themeIconPath && file_exists($themeIconPath)) {
177         $iconPath = $themeIconPath;
178     } else if (!file_exists($iconPath)) {
179         return '';
180     }
181
182     $fileContents = file_get_contents($iconPath);
183     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
184 }
185
186 /**
187  * Generate a url with multiple parameters for sorting purposes.
188  * Works out the logic to set the correct sorting direction
189  * Discards empty parameters and allows overriding.
190  * @param $path
191  * @param array $data
192  * @param array $overrideData
193  * @return string
194  */
195 function sortUrl($path, $data, $overrideData = [])
196 {
197     $queryStringSections = [];
198     $queryData = array_merge($data, $overrideData);
199
200     // Change sorting direction is already sorted on current attribute
201     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
202         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
203     } else {
204         $queryData['order'] = 'asc';
205     }
206
207     foreach ($queryData as $name => $value) {
208         $trimmedVal = trim($value);
209         if ($trimmedVal === '') {
210             continue;
211         }
212         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
213     }
214
215     if (count($queryStringSections) === 0) {
216         return $path;
217     }
218
219     return baseUrl($path . '?' . implode('&', $queryStringSections));
220 }