]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Apply fixes from StyleCI
[bookstack] / app / helpers.php
1 <?php
2
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Auth\User;
5 use BookStack\Model;
6 use BookStack\Settings\SettingService;
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::getDefault();
39 }
40
41 /**
42  * Check if current user is a signed in user.
43  */
44 function signedInUser(): bool
45 {
46     return auth()->user() && !auth()->user()->isDefault();
47 }
48
49 /**
50  * Check if the current user has general access.
51  */
52 function hasAppAccess(): bool
53 {
54     return !auth()->guest() || setting('app-public');
55 }
56
57 /**
58  * Check if the current user has a permission. If an ownable element
59  * is passed in the jointPermissions are checked against that particular item.
60  */
61 function userCan(string $permission, Model $ownable = null): bool
62 {
63     if ($ownable === null) {
64         return user() && user()->can($permission);
65     }
66
67     // Check permission on ownable item
68     $permissionService = app(PermissionService::class);
69
70     return $permissionService->checkOwnableUserAccess($ownable, $permission);
71 }
72
73 /**
74  * Check if the current user has the given permission
75  * on any item in the system.
76  */
77 function userCanOnAny(string $permission, string $entityClass = null): bool
78 {
79     $permissionService = app(PermissionService::class);
80
81     return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
82 }
83
84 /**
85  * Helper to access system settings.
86  *
87  * @return mixed|SettingService
88  */
89 function setting(string $key = null, $default = null)
90 {
91     $settingService = resolve(SettingService::class);
92
93     if (is_null($key)) {
94         return $settingService;
95     }
96
97     return $settingService->get($key, $default);
98 }
99
100 /**
101  * Get a path to a theme resource.
102  */
103 function theme_path(string $path = ''): string
104 {
105     $theme = config('view.theme');
106
107     if (!$theme) {
108         return '';
109     }
110
111     return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
112 }
113
114 /**
115  * Get fetch an SVG icon as a string.
116  * Checks for icons defined within a custom theme before defaulting back
117  * to the 'resources/assets/icons' folder.
118  *
119  * Returns an empty string if icon file not found.
120  */
121 function icon(string $name, array $attrs = []): string
122 {
123     $attrs = array_merge([
124         'class'     => 'svg-icon',
125         'data-icon' => $name,
126         'role'      => 'presentation',
127     ], $attrs);
128     $attrString = ' ';
129     foreach ($attrs as $attrName => $attr) {
130         $attrString .= $attrName . '="' . $attr . '" ';
131     }
132
133     $iconPath = resource_path('icons/' . $name . '.svg');
134     $themeIconPath = theme_path('icons/' . $name . '.svg');
135
136     if ($themeIconPath && file_exists($themeIconPath)) {
137         $iconPath = $themeIconPath;
138     } elseif (!file_exists($iconPath)) {
139         return '';
140     }
141
142     $fileContents = file_get_contents($iconPath);
143
144     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
145 }
146
147 /**
148  * Generate a url with multiple parameters for sorting purposes.
149  * Works out the logic to set the correct sorting direction
150  * Discards empty parameters and allows overriding.
151  */
152 function sortUrl(string $path, array $data, array $overrideData = []): string
153 {
154     $queryStringSections = [];
155     $queryData = array_merge($data, $overrideData);
156
157     // Change sorting direction is already sorted on current attribute
158     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
159         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
160     } elseif (isset($overrideData['sort'])) {
161         $queryData['order'] = 'asc';
162     }
163
164     foreach ($queryData as $name => $value) {
165         $trimmedVal = trim($value);
166         if ($trimmedVal === '') {
167             continue;
168         }
169         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
170     }
171
172     if (count($queryStringSections) === 0) {
173         return $path;
174     }
175
176     return url($path . '?' . implode('&', $queryStringSections));
177 }