]> BookStack Code Mirror - bookstack/blob - app/App/helpers.php
Guest control: Cleaned methods involved in fetching/handling
[bookstack] / app / App / helpers.php
1 <?php
2
3 use BookStack\App\Model;
4 use BookStack\Permissions\PermissionApplicator;
5 use BookStack\Settings\SettingService;
6 use BookStack\Users\Models\User;
7
8 /**
9  * Get the path to a versioned file.
10  *
11  * @throws Exception
12  */
13 function versioned_asset(string $file = ''): string
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
29     return url($path);
30 }
31
32 /**
33  * Helper method to get the current User.
34  * Defaults to public 'Guest' user if not logged in.
35  */
36 function user(): User
37 {
38     return auth()->user() ?: User::getGuest();
39 }
40
41 /**
42  * Check if the current user has a permission. If an ownable element
43  * is passed in the jointPermissions are checked against that particular item.
44  */
45 function userCan(string $permission, Model $ownable = null): bool
46 {
47     if ($ownable === null) {
48         return user()->can($permission);
49     }
50
51     // Check permission on ownable item
52     $permissions = app(PermissionApplicator::class);
53
54     return $permissions->checkOwnableUserAccess($ownable, $permission);
55 }
56
57 /**
58  * Check if the current user can perform the given action on any items in the system.
59  * Can be provided the class name of an entity to filter ability to that specific entity type.
60  */
61 function userCanOnAny(string $action, string $entityClass = ''): bool
62 {
63     $permissions = app(PermissionApplicator::class);
64
65     return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
66 }
67
68 /**
69  * Helper to access system settings.
70  *
71  * @return mixed|SettingService
72  */
73 function setting(string $key = null, $default = null)
74 {
75     $settingService = resolve(SettingService::class);
76
77     if (is_null($key)) {
78         return $settingService;
79     }
80
81     return $settingService->get($key, $default);
82 }
83
84 /**
85  * Get a path to a theme resource.
86  * Returns null if a theme is not configured and
87  * therefore a full path is not available for use.
88  */
89 function theme_path(string $path = ''): ?string
90 {
91     $theme = config('view.theme');
92
93     if (!$theme) {
94         return null;
95     }
96
97     return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
98 }
99
100 /**
101  * Get fetch an SVG icon as a string.
102  * Checks for icons defined within a custom theme before defaulting back
103  * to the 'resources/assets/icons' folder.
104  *
105  * Returns an empty string if icon file not found.
106  */
107 function icon(string $name, array $attrs = []): string
108 {
109     $attrs = array_merge([
110         'class'     => 'svg-icon',
111         'data-icon' => $name,
112         'role'      => 'presentation',
113     ], $attrs);
114     $attrString = ' ';
115     foreach ($attrs as $attrName => $attr) {
116         $attrString .= $attrName . '="' . $attr . '" ';
117     }
118
119     $iconPath = resource_path('icons/' . $name . '.svg');
120     $themeIconPath = theme_path('icons/' . $name . '.svg');
121
122     if ($themeIconPath && file_exists($themeIconPath)) {
123         $iconPath = $themeIconPath;
124     } elseif (!file_exists($iconPath)) {
125         return '';
126     }
127
128     $fileContents = file_get_contents($iconPath);
129
130     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
131 }
132
133 /**
134  * Generate a URL with multiple parameters for sorting purposes.
135  * Works out the logic to set the correct sorting direction
136  * Discards empty parameters and allows overriding.
137  */
138 function sortUrl(string $path, array $data, array $overrideData = []): string
139 {
140     $queryStringSections = [];
141     $queryData = array_merge($data, $overrideData);
142
143     // Change sorting direction is already sorted on current attribute
144     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
145         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
146     } elseif (isset($overrideData['sort'])) {
147         $queryData['order'] = 'asc';
148     }
149
150     foreach ($queryData as $name => $value) {
151         $trimmedVal = trim($value);
152         if ($trimmedVal === '') {
153             continue;
154         }
155         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
156     }
157
158     if (count($queryStringSections) === 0) {
159         return url($path);
160     }
161
162     return url($path . '?' . implode('&', $queryStringSections));
163 }