]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Use joint_permissions to determine is a user has an available page or chapter to...
[bookstack] / app / helpers.php
1 <?php
2
3 use BookStack\Ownable;
4
5 /**
6  * Get the path to a versioned file.
7  *
8  * @param  string $file
9  * @return string
10  * @throws Exception
11  */
12 function versioned_asset($file = '')
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 baseUrl($path);
28 }
29
30 /**
31  * Helper method to get the current User.
32  * Defaults to public 'Guest' user if not logged in.
33  * @return \BookStack\Auth\User
34  */
35 function user()
36 {
37     return auth()->user() ?: \BookStack\Auth\User::getDefault();
38 }
39
40 /**
41  * Check if current user is a signed in user.
42  * @return bool
43  */
44 function signedInUser()
45 {
46     return auth()->user() && !auth()->user()->isDefault();
47 }
48
49 /**
50  * Check if the current user has a permission.
51  * If an ownable element is passed in the jointPermissions are checked against
52  * that particular item.
53  * @param $permission
54  * @param Ownable $ownable
55  * @return mixed
56  */
57 function userCan($permission, Ownable $ownable = null)
58 {
59     if ($ownable === null) {
60         return user() && user()->can($permission);
61     }
62
63     // Check permission on ownable item
64     $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
65     return $permissionService->checkOwnableUserAccess($ownable, $permission);
66 }
67
68 /**
69  * Check if the current user has the ability to create a page for an existing object
70  * @return bool
71  */
72 function userCanCreatePage()
73 {
74     // Check for create page permissions
75     $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
76     return $permissionService->checkAvailableCreatePageAccess();
77 }
78
79 /**
80  * Helper to access system settings.
81  * @param $key
82  * @param bool $default
83  * @return bool|string|\BookStack\Settings\SettingService
84  */
85 function setting($key = null, $default = false)
86 {
87     $settingService = resolve(\BookStack\Settings\SettingService::class);
88     if (is_null($key)) {
89         return $settingService;
90     }
91     return $settingService->get($key, $default);
92 }
93
94 /**
95  * Helper to create url's relative to the applications root path.
96  * @param string $path
97  * @param bool $forceAppDomain
98  * @return string
99  */
100 function baseUrl($path, $forceAppDomain = false)
101 {
102     $isFullUrl = strpos($path, 'http') === 0;
103     if ($isFullUrl && !$forceAppDomain) {
104         return $path;
105     }
106
107     $path = trim($path, '/');
108     $base = rtrim(config('app.url'), '/');
109
110     // Remove non-specified domain if forced and we have a domain
111     if ($isFullUrl && $forceAppDomain) {
112         if (!empty($base) && strpos($path, $base) === 0) {
113             $path = trim(substr($path, strlen($base) - 1));
114         }
115         $explodedPath = explode('/', $path);
116         $path = implode('/', array_splice($explodedPath, 3));
117     }
118
119     // Return normal url path if not specified in config
120     if (config('app.url') === '') {
121         return url($path);
122     }
123
124     return $base . '/' . $path;
125 }
126
127 /**
128  * Get an instance of the redirector.
129  * Overrides the default laravel redirect helper.
130  * Ensures it redirects even when the app is in a subdirectory.
131  *
132  * @param  string|null  $to
133  * @param  int     $status
134  * @param  array   $headers
135  * @param  bool    $secure
136  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
137  */
138 function redirect($to = null, $status = 302, $headers = [], $secure = null)
139 {
140     if (is_null($to)) {
141         return app('redirect');
142     }
143
144     $to = baseUrl($to);
145
146     return app('redirect')->to($to, $status, $headers, $secure);
147 }
148
149 /**
150  * Get a path to a theme resource.
151  * @param string $path
152  * @return string|boolean
153  */
154 function theme_path($path = '')
155 {
156     $theme = config('view.theme');
157     if (!$theme) {
158         return false;
159     }
160
161     return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
162 }
163
164 /**
165  * Get fetch an SVG icon as a string.
166  * Checks for icons defined within a custom theme before defaulting back
167  * to the 'resources/assets/icons' folder.
168  *
169  * Returns an empty string if icon file not found.
170  * @param $name
171  * @param array $attrs
172  * @return mixed
173  */
174 function icon($name, $attrs = [])
175 {
176     $attrs = array_merge([
177         'class' => 'svg-icon',
178         'data-icon' => $name
179     ], $attrs);
180     $attrString = ' ';
181     foreach ($attrs as $attrName => $attr) {
182         $attrString .=  $attrName . '="' . $attr . '" ';
183     }
184
185     $iconPath = resource_path('assets/icons/' . $name . '.svg');
186     $themeIconPath = theme_path('icons/' . $name . '.svg');
187     if ($themeIconPath && file_exists($themeIconPath)) {
188         $iconPath = $themeIconPath;
189     } else if (!file_exists($iconPath)) {
190         return '';
191     }
192
193     $fileContents = file_get_contents($iconPath);
194     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
195 }
196
197 /**
198  * Generate a url with multiple parameters for sorting purposes.
199  * Works out the logic to set the correct sorting direction
200  * Discards empty parameters and allows overriding.
201  * @param $path
202  * @param array $data
203  * @param array $overrideData
204  * @return string
205  */
206 function sortUrl($path, $data, $overrideData = [])
207 {
208     $queryStringSections = [];
209     $queryData = array_merge($data, $overrideData);
210
211     // Change sorting direction is already sorted on current attribute
212     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
213         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
214     } else {
215         $queryData['order'] = 'asc';
216     }
217
218     foreach ($queryData as $name => $value) {
219         $trimmedVal = trim($value);
220         if ($trimmedVal === '') {
221             continue;
222         }
223         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
224     }
225
226     if (count($queryStringSections) === 0) {
227         return $path;
228     }
229
230     return baseUrl($path . '?' . implode('&', $queryStringSections));
231 }