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