]> BookStack Code Mirror - bookstack/blob - app/App/helpers.php
Themes: Added route to serve public theme files
[bookstack] / app / App / helpers.php
1 <?php
2
3 use BookStack\App\Model;
4 use BookStack\Facades\Theme;
5 use BookStack\Permissions\PermissionApplicator;
6 use BookStack\Settings\SettingService;
7 use BookStack\Users\Models\User;
8
9 /**
10  * Get the path to a versioned file.
11  *
12  * @throws Exception
13  */
14 function versioned_asset(string $file = ''): string
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
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  */
37 function user(): User
38 {
39     return auth()->user() ?: User::getGuest();
40 }
41
42 /**
43  * Check if the current user has a permission. If an ownable element
44  * is passed in the jointPermissions are checked against that particular item.
45  */
46 function userCan(string $permission, Model $ownable = null): bool
47 {
48     if ($ownable === null) {
49         return user()->can($permission);
50     }
51
52     // Check permission on ownable item
53     $permissions = app()->make(PermissionApplicator::class);
54
55     return $permissions->checkOwnableUserAccess($ownable, $permission);
56 }
57
58 /**
59  * Check if the current user can perform the given action on any items in the system.
60  * Can be provided the class name of an entity to filter ability to that specific entity type.
61  */
62 function userCanOnAny(string $action, string $entityClass = ''): bool
63 {
64     $permissions = app()->make(PermissionApplicator::class);
65
66     return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
67 }
68
69 /**
70  * Helper to access system settings.
71  *
72  * @return mixed|SettingService
73  */
74 function setting(string $key = null, $default = null)
75 {
76     $settingService = app()->make(SettingService::class);
77
78     if (is_null($key)) {
79         return $settingService;
80     }
81
82     return $settingService->get($key, $default);
83 }
84
85 /**
86  * Get a path to a theme resource.
87  * Returns null if a theme is not configured and
88  * therefore a full path is not available for use.
89  */
90 function theme_path(string $path = ''): ?string
91 {
92     $theme = Theme::getTheme();
93     if (!$theme) {
94         return null;
95     }
96
97     return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
98 }
99
100 /**
101  * Generate a URL with multiple parameters for sorting purposes.
102  * Works out the logic to set the correct sorting direction
103  * Discards empty parameters and allows overriding.
104  */
105 function sortUrl(string $path, array $data, array $overrideData = []): string
106 {
107     $queryStringSections = [];
108     $queryData = array_merge($data, $overrideData);
109
110     // Change sorting direction is already sorted on current attribute
111     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
112         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
113     } elseif (isset($overrideData['sort'])) {
114         $queryData['order'] = 'asc';
115     }
116
117     foreach ($queryData as $name => $value) {
118         $trimmedVal = trim($value);
119         if ($trimmedVal === '') {
120             continue;
121         }
122         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
123     }
124
125     if (count($queryStringSections) === 0) {
126         return url($path);
127     }
128
129     return url($path . '?' . implode('&', $queryStringSections));
130 }