]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Finished refactor of entity repos
[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 the current user has a permission.
42  * If an ownable element is passed in the jointPermissions are checked against
43  * that particular item.
44  * @param $permission
45  * @param Ownable $ownable
46  * @return mixed
47  */
48 function userCan($permission, Ownable $ownable = null)
49 {
50     if ($ownable === null) {
51         return user() && user()->can($permission);
52     }
53
54     // Check permission on ownable item
55     $permissionService = app(\BookStack\Services\PermissionService::class);
56     return $permissionService->checkOwnableUserAccess($ownable, $permission);
57 }
58
59 /**
60  * Helper to access system settings.
61  * @param $key
62  * @param bool $default
63  * @return mixed
64  */
65 function setting($key, $default = false)
66 {
67     $settingService = app(\BookStack\Services\SettingService::class);
68     return $settingService->get($key, $default);
69 }
70
71 /**
72  * Helper to create url's relative to the applications root path.
73  * @param string $path
74  * @param bool $forceAppDomain
75  * @return string
76  */
77 function baseUrl($path, $forceAppDomain = false)
78 {
79     $isFullUrl = strpos($path, 'http') === 0;
80     if ($isFullUrl && !$forceAppDomain) return $path;
81     $path = trim($path, '/');
82
83     // Remove non-specified domain if forced and we have a domain
84     if ($isFullUrl && $forceAppDomain) {
85         $explodedPath = explode('/', $path);
86         $path = implode('/', array_splice($explodedPath, 3));
87     }
88
89     // Return normal url path if not specified in config
90     if (config('app.url') === '') {
91         return url($path);
92     }
93
94     return rtrim(config('app.url'), '/') . '/' . $path;
95 }
96
97 /**
98  * Get an instance of the redirector.
99  * Overrides the default laravel redirect helper.
100  * Ensures it redirects even when the app is in a subdirectory.
101  *
102  * @param  string|null  $to
103  * @param  int     $status
104  * @param  array   $headers
105  * @param  bool    $secure
106  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
107  */
108 function redirect($to = null, $status = 302, $headers = [], $secure = null)
109 {
110     if (is_null($to)) {
111         return app('redirect');
112     }
113
114     $to = baseUrl($to);
115
116     return app('redirect')->to($to, $status, $headers, $secure);
117 }
118
119 /**
120  * Generate a url with multiple parameters for sorting purposes.
121  * Works out the logic to set the correct sorting direction
122  * Discards empty parameters and allows overriding.
123  * @param $path
124  * @param array $data
125  * @param array $overrideData
126  * @return string
127  */
128 function sortUrl($path, $data, $overrideData = [])
129 {
130     $queryStringSections = [];
131     $queryData = array_merge($data, $overrideData);
132
133     // Change sorting direction is already sorted on current attribute
134     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
135         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
136     } else {
137         $queryData['order'] = 'asc';
138     }
139
140     foreach ($queryData as $name => $value) {
141         $trimmedVal = trim($value);
142         if ($trimmedVal === '') continue;
143         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
144     }
145
146     if (count($queryStringSections) === 0) return $path;
147
148     return baseUrl($path . '?' . implode('&', $queryStringSections));
149 }