]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Brazilian Portuguese Localization
[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 /**
121  * Generate a url with multiple parameters for sorting purposes.
122  * Works out the logic to set the correct sorting direction
123  * Discards empty parameters and allows overriding.
124  * @param $path
125  * @param array $data
126  * @param array $overrideData
127  * @return string
128  */
129 function sortUrl($path, $data, $overrideData = [])
130 {
131     $queryStringSections = [];
132     $queryData = array_merge($data, $overrideData);
133
134     // Change sorting direction is already sorted on current attribute
135     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
136         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
137     } else {
138         $queryData['order'] = 'asc';
139     }
140
141     foreach ($queryData as $name => $value) {
142         $trimmedVal = trim($value);
143         if ($trimmedVal === '') continue;
144         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
145     }
146
147     if (count($queryStringSections) === 0) return $path;
148
149     return baseUrl($path . '?' . implode('&', $queryStringSections));
150 }