]> BookStack Code Mirror - bookstack/blob - app/helpers.php
add missing icon, fix name conventions
[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 = resolve(\BookStack\Services\SettingService::class);
77     if (is_null($key)) {
78         return $settingService;
79     }
80     return $settingService->get($key, $default);
81 }
82
83 /**
84  * Helper to create url's relative to the applications root path.
85  * @param string $path
86  * @param bool $forceAppDomain
87  * @return string
88  */
89 function baseUrl($path, $forceAppDomain = false)
90 {
91     $isFullUrl = strpos($path, 'http') === 0;
92     if ($isFullUrl && !$forceAppDomain) {
93         return $path;
94     }
95     $path = trim($path, '/');
96
97     // Remove non-specified domain if forced and we have a domain
98     if ($isFullUrl && $forceAppDomain) {
99         $explodedPath = explode('/', $path);
100         $path = implode('/', array_splice($explodedPath, 3));
101     }
102
103     // Return normal url path if not specified in config
104     if (config('app.url') === '') {
105         return url($path);
106     }
107
108     return rtrim(config('app.url'), '/') . '/' . $path;
109 }
110
111 /**
112  * Get an instance of the redirector.
113  * Overrides the default laravel redirect helper.
114  * Ensures it redirects even when the app is in a subdirectory.
115  *
116  * @param  string|null  $to
117  * @param  int     $status
118  * @param  array   $headers
119  * @param  bool    $secure
120  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
121  */
122 function redirect($to = null, $status = 302, $headers = [], $secure = null)
123 {
124     if (is_null($to)) {
125         return app('redirect');
126     }
127
128     $to = baseUrl($to);
129
130     return app('redirect')->to($to, $status, $headers, $secure);
131 }
132
133 function icon($name, $attrs = [])
134 {
135     $iconPath = resource_path('assets/icons/' . $name . '.svg');
136     $attrString = ' ';
137     foreach ($attrs as $attrName => $attr) {
138         $attrString .=  $attrName . '="' . $attr . '" ';
139     }
140     $fileContents = file_get_contents($iconPath);
141     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
142 }
143
144 /**
145  * Generate a url with multiple parameters for sorting purposes.
146  * Works out the logic to set the correct sorting direction
147  * Discards empty parameters and allows overriding.
148  * @param $path
149  * @param array $data
150  * @param array $overrideData
151  * @return string
152  */
153 function sortUrl($path, $data, $overrideData = [])
154 {
155     $queryStringSections = [];
156     $queryData = array_merge($data, $overrideData);
157
158     // Change sorting direction is already sorted on current attribute
159     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
160         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
161     } else {
162         $queryData['order'] = 'asc';
163     }
164
165     foreach ($queryData as $name => $value) {
166         $trimmedVal = trim($value);
167         if ($trimmedVal === '') {
168             continue;
169         }
170         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
171     }
172
173     if (count($queryStringSections) === 0) {
174         return $path;
175     }
176
177     return baseUrl($path . '?' . implode('&', $queryStringSections));
178 }