]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Updated Spanish translation
[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\User
34  */
35 function user()
36 {
37     return auth()->user() ?: \BookStack\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\Services\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\Services\SettingService
73  */
74 function setting($key = null, $default = false)
75 {
76     $settingService = resolve(\BookStack\Services\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     $path = trim($path, '/');
96
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));
101     }
102
103     // Return normal url path if not specified in config
104     if (config('app.url') === '') {
105         return url($path);
106     }
107
108     return rtrim(config('app.url'), '/') . '/' . $path;
109 }
110
111 /**
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.
115  *
116  * @param  string|null  $to
117  * @param  int     $status
118  * @param  array   $headers
119  * @param  bool    $secure
120  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
121  */
122 function redirect($to = null, $status = 302, $headers = [], $secure = null)
123 {
124     if (is_null($to)) {
125         return app('redirect');
126     }
127
128     $to = baseUrl($to);
129
130     return app('redirect')->to($to, $status, $headers, $secure);
131 }
132
133 /**
134  * Get a path to a theme resource.
135  * @param string $path
136  * @return string|boolean
137  */
138 function theme_path($path = '')
139 {
140     $theme = config('view.theme');
141     if (!$theme) {
142         return false;
143     }
144
145     return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
146 }
147
148 /**
149  * Get fetch an SVG icon as a string.
150  * Checks for icons defined within a custom theme before defaulting back
151  * to the 'resources/assets/icons' folder.
152  *
153  * Returns an empty string if icon file not found.
154  * @param $name
155  * @param array $attrs
156  * @return mixed
157  */
158 function icon($name, $attrs = [])
159 {
160     $attrs = array_merge([
161         'class' => 'svg-icon',
162         'data-icon' => $name
163     ], $attrs);
164     $attrString = ' ';
165     foreach ($attrs as $attrName => $attr) {
166         $attrString .=  $attrName . '="' . $attr . '" ';
167     }
168
169     $iconPath = resource_path('assets/icons/' . $name . '.svg');
170     $themeIconPath = theme_path('icons/' . $name . '.svg');
171     if ($themeIconPath && file_exists($themeIconPath)) {
172         $iconPath = $themeIconPath;
173     } else if (!file_exists($iconPath)) {
174         return '';
175     }
176
177     $fileContents = file_get_contents($iconPath);
178     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
179 }
180
181 /**
182  * Generate a url with multiple parameters for sorting purposes.
183  * Works out the logic to set the correct sorting direction
184  * Discards empty parameters and allows overriding.
185  * @param $path
186  * @param array $data
187  * @param array $overrideData
188  * @return string
189  */
190 function sortUrl($path, $data, $overrideData = [])
191 {
192     $queryStringSections = [];
193     $queryData = array_merge($data, $overrideData);
194
195     // Change sorting direction is already sorted on current attribute
196     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
197         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
198     } else {
199         $queryData['order'] = 'asc';
200     }
201
202     foreach ($queryData as $name => $value) {
203         $trimmedVal = trim($value);
204         if ($trimmedVal === '') {
205             continue;
206         }
207         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
208     }
209
210     if (count($queryStringSections) === 0) {
211         return $path;
212     }
213
214     return baseUrl($path . '?' . implode('&', $queryStringSections));
215 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.