]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Fixed entities wrongly visible on 404
[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\User
34  */
35 function user()
36 {
37     return auth()->user() ?: \BookStack\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\Services\PermissionService::class);
65     return $permissionService->checkOwnableUserAccess($ownable, $permission);
66 }
67
68 /**
69  * Helper to access system settings.
70  * @param $key
71  * @param bool $default
72  * @return bool|string|\BookStack\Services\SettingService
73  */
74 function setting($key = null, $default = false)
75 {
76     $settingService = app(\BookStack\Services\SettingService::class);
77     if (is_null($key)) return $settingService;
78     return $settingService->get($key, $default);
79 }
80
81 /**
82  * Helper to create url's relative to the applications root path.
83  * @param string $path
84  * @param bool $forceAppDomain
85  * @return string
86  */
87 function baseUrl($path, $forceAppDomain = false)
88 {
89     $isFullUrl = strpos($path, 'http') === 0;
90     if ($isFullUrl && !$forceAppDomain) return $path;
91     $path = trim($path, '/');
92
93     // Remove non-specified domain if forced and we have a domain
94     if ($isFullUrl && $forceAppDomain) {
95         $explodedPath = explode('/', $path);
96         $path = implode('/', array_splice($explodedPath, 3));
97     }
98
99     // Return normal url path if not specified in config
100     if (config('app.url') === '') {
101         return url($path);
102     }
103
104     return rtrim(config('app.url'), '/') . '/' . $path;
105 }
106
107 /**
108  * Get an instance of the redirector.
109  * Overrides the default laravel redirect helper.
110  * Ensures it redirects even when the app is in a subdirectory.
111  *
112  * @param  string|null  $to
113  * @param  int     $status
114  * @param  array   $headers
115  * @param  bool    $secure
116  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
117  */
118 function redirect($to = null, $status = 302, $headers = [], $secure = null)
119 {
120     if (is_null($to)) {
121         return app('redirect');
122     }
123
124     $to = baseUrl($to);
125
126     return app('redirect')->to($to, $status, $headers, $secure);
127 }
128
129 /**
130  * Generate a url with multiple parameters for sorting purposes.
131  * Works out the logic to set the correct sorting direction
132  * Discards empty parameters and allows overriding.
133  * @param $path
134  * @param array $data
135  * @param array $overrideData
136  * @return string
137  */
138 function sortUrl($path, $data, $overrideData = [])
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     } else {
147         $queryData['order'] = 'asc';
148     }
149
150     foreach ($queryData as $name => $value) {
151         $trimmedVal = trim($value);
152         if ($trimmedVal === '') continue;
153         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
154     }
155
156     if (count($queryStringSections) === 0) return $path;
157
158     return baseUrl($path . '?' . implode('&', $queryStringSections));
159 }