]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Merge pull request #8 from BookStackApp/master
[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)) 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 function icon($name, $attrs = []) {
130     $iconPath = resource_path('assets/icons/' . $name . '.svg');
131     $attrString = ' ';
132     foreach ($attrs as $attrName => $attr) {
133         $attrString .=  $attrName . '="' . $attr . '" ';
134     }
135     $fileContents = file_get_contents($iconPath);
136     return  str_replace('<svg', '<svg' . $attrString, $fileContents);
137 }
138
139 /**
140  * Generate a url with multiple parameters for sorting purposes.
141  * Works out the logic to set the correct sorting direction
142  * Discards empty parameters and allows overriding.
143  * @param $path
144  * @param array $data
145  * @param array $overrideData
146  * @return string
147  */
148 function sortUrl($path, $data, $overrideData = [])
149 {
150     $queryStringSections = [];
151     $queryData = array_merge($data, $overrideData);
152
153     // Change sorting direction is already sorted on current attribute
154     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
155         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
156     } else {
157         $queryData['order'] = 'asc';
158     }
159
160     foreach ($queryData as $name => $value) {
161         $trimmedVal = trim($value);
162         if ($trimmedVal === '') continue;
163         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
164     }
165
166     if (count($queryStringSections) === 0) return $path;
167
168     return baseUrl($path . '?' . implode('&', $queryStringSections));
169 }