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