]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Update passwords.php
[bookstack] / app / helpers.php
1 <?php
2
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Auth\User;
5 use BookStack\Ownable;
6 use BookStack\Settings\SettingService;
7
8 /**
9  * Get the path to a versioned file.
10  *
11  * @param  string $file
12  * @return string
13  * @throws Exception
14  */
15 function versioned_asset($file = '') : string
16 {
17     static $version = null;
18
19     if (is_null($version)) {
20         $versionFile = base_path('version');
21         $version = trim(file_get_contents($versionFile));
22     }
23
24     $additional = '';
25     if (config('app.env') === 'development') {
26         $additional = sha1_file(public_path($file));
27     }
28
29     $path = $file . '?version=' . urlencode($version) . $additional;
30     return url($path);
31 }
32
33 /**
34  * Helper method to get the current User.
35  * Defaults to public 'Guest' user if not logged in.
36  * @return User
37  */
38 function user() : User
39 {
40     return auth()->user() ?: User::getDefault();
41 }
42
43 /**
44  * Check if current user is a signed in user.
45  * @return bool
46  */
47 function signedInUser() : bool
48 {
49     return auth()->user() && !auth()->user()->isDefault();
50 }
51
52 /**
53  * Check if the current user has general access.
54  * @return bool
55  */
56 function hasAppAccess() : bool
57 {
58     return !auth()->guest() || setting('app-public');
59 }
60
61 /**
62  * Check if the current user has a permission.
63  * If an ownable element is passed in the jointPermissions are checked against
64  * that particular item.
65  * @param string $permission
66  * @param Ownable $ownable
67  * @return bool
68  */
69 function userCan(string $permission, Ownable $ownable = null) : bool
70 {
71     if ($ownable === null) {
72         return user() && user()->can($permission);
73     }
74
75     // Check permission on ownable item
76     $permissionService = app(PermissionService::class);
77     return $permissionService->checkOwnableUserAccess($ownable, $permission);
78 }
79
80 /**
81  * Check if the current user has the given permission
82  * on any item in the system.
83  * @param string $permission
84  * @param string|null $entityClass
85  * @return bool
86  */
87 function userCanOnAny(string $permission, string $entityClass = null) : bool
88 {
89     $permissionService = app(PermissionService::class);
90     return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
91 }
92
93 /**
94  * Helper to access system settings.
95  * @param $key
96  * @param bool $default
97  * @return bool|string|SettingService
98  */
99 function setting($key = null, $default = false)
100 {
101     $settingService = resolve(SettingService::class);
102     if (is_null($key)) {
103         return $settingService;
104     }
105     return $settingService->get($key, $default);
106 }
107
108 /**
109  * Get a path to a theme resource.
110  * @param string $path
111  * @return string
112  */
113 function theme_path($path = '') : string
114 {
115     $theme = config('view.theme');
116     if (!$theme) {
117         return '';
118     }
119
120     return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
121 }
122
123 /**
124  * Get fetch an SVG icon as a string.
125  * Checks for icons defined within a custom theme before defaulting back
126  * to the 'resources/assets/icons' folder.
127  *
128  * Returns an empty string if icon file not found.
129  * @param $name
130  * @param array $attrs
131  * @return mixed
132  */
133 function icon($name, $attrs = [])
134 {
135     $attrs = array_merge([
136         'class'     => 'svg-icon',
137         'data-icon' => $name,
138         'role'      => 'presentation',
139     ], $attrs);
140     $attrString = ' ';
141     foreach ($attrs as $attrName => $attr) {
142         $attrString .=  $attrName . '="' . $attr . '" ';
143     }
144
145     $iconPath = resource_path('assets/icons/' . $name . '.svg');
146     $themeIconPath = theme_path('icons/' . $name . '.svg');
147     if ($themeIconPath && file_exists($themeIconPath)) {
148         $iconPath = $themeIconPath;
149     } else if (!file_exists($iconPath)) {
150         return '';
151     }
152
153     $fileContents = file_get_contents($iconPath);
154     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
155 }
156
157 /**
158  * Generate a url with multiple parameters for sorting purposes.
159  * Works out the logic to set the correct sorting direction
160  * Discards empty parameters and allows overriding.
161  * @param $path
162  * @param array $data
163  * @param array $overrideData
164  * @return string
165  */
166 function sortUrl($path, $data, $overrideData = [])
167 {
168     $queryStringSections = [];
169     $queryData = array_merge($data, $overrideData);
170
171     // Change sorting direction is already sorted on current attribute
172     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
173         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
174     } else {
175         $queryData['order'] = 'asc';
176     }
177
178     foreach ($queryData as $name => $value) {
179         $trimmedVal = trim($value);
180         if ($trimmedVal === '') {
181             continue;
182         }
183         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
184     }
185
186     if (count($queryStringSections) === 0) {
187         return $path;
188     }
189
190     return url($path . '?' . implode('&', $queryStringSections));
191 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.