]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Merge pull request #1 from ssddanbrown/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     // Don't require css and JS assets for testing
15     if (config('app.env') === 'testing') return '';
16
17     static $manifest = null;
18     $manifestPath = 'build/manifest.json';
19
20     if (is_null($manifest) && file_exists($manifestPath)) {
21         $manifest = json_decode(file_get_contents(public_path($manifestPath)), true);
22     } else if (!file_exists($manifestPath)) {
23         if (config('app.env') !== 'production') {
24             $path = public_path($manifestPath);
25             $error = "No {$path} file found, Ensure you have built the css/js assets using gulp.";
26         } else {
27             $error = "No {$manifestPath} file found, Ensure you are using the release version of BookStack";
28         }
29         throw new \Exception($error);
30     }
31
32     if (isset($manifest[$file])) {
33         return baseUrl($manifest[$file]);
34     }
35
36     throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
37 }
38
39 /**
40  * Check if the current user has a permission.
41  * If an ownable element is passed in the jointPermissions are checked against
42  * that particular item.
43  * @param $permission
44  * @param Ownable $ownable
45  * @return mixed
46  */
47 function userCan($permission, Ownable $ownable = null)
48 {
49     if ($ownable === null) {
50         return auth()->user() && auth()->user()->can($permission);
51     }
52
53     // Check permission on ownable item
54     $permissionService = app(\BookStack\Services\PermissionService::class);
55     return $permissionService->checkOwnableUserAccess($ownable, $permission);
56 }
57
58 /**
59  * Helper to access system settings.
60  * @param $key
61  * @param bool $default
62  * @return mixed
63  */
64 function setting($key, $default = false)
65 {
66     $settingService = app(\BookStack\Services\SettingService::class);
67     return $settingService->get($key, $default);
68 }
69
70 /**
71  * Helper to create url's relative to the applications root path.
72  * @param string $path
73  * @param bool $forceAppDomain
74  * @return string
75  */
76 function baseUrl($path, $forceAppDomain = false)
77 {
78     $isFullUrl = strpos($path, 'http') === 0;
79     if ($isFullUrl && !$forceAppDomain) return $path;
80     $path = trim($path, '/');
81
82     // Remove non-specified domain if forced and we have a domain
83     if ($isFullUrl && $forceAppDomain) {
84         $explodedPath = explode('/', $path);
85         $path = implode('/', array_splice($explodedPath, 3));
86     }
87
88     // Return normal url path if not specified in config
89     if (config('app.url') === '') {
90         return url($path);
91     }
92
93     return rtrim(config('app.url'), '/') . '/' . $path;
94 }
95
96 /**
97  * Get an instance of the redirector.
98  * Overrides the default laravel redirect helper.
99  * Ensures it redirects even when the app is in a subdirectory.
100  *
101  * @param  string|null  $to
102  * @param  int     $status
103  * @param  array   $headers
104  * @param  bool    $secure
105  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
106  */
107 function redirect($to = null, $status = 302, $headers = [], $secure = null)
108 {
109     if (is_null($to)) {
110         return app('redirect');
111     }
112
113     $to = baseUrl($to);
114
115     return app('redirect')->to($to, $status, $headers, $secure);
116 }
117
118 /**
119  * Generate a url with multiple parameters for sorting purposes.
120  * Works out the logic to set the correct sorting direction
121  * Discards empty parameters and allows overriding.
122  * @param $path
123  * @param array $data
124  * @param array $overrideData
125  * @return string
126  */
127 function sortUrl($path, $data, $overrideData = [])
128 {
129     $queryStringSections = [];
130     $queryData = array_merge($data, $overrideData);
131     
132     // Change sorting direction is already sorted on current attribute
133     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
134         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
135     } else {
136         $queryData['order'] = 'asc';
137     }
138     
139     foreach ($queryData as $name => $value) {
140         $trimmedVal = trim($value);
141         if ($trimmedVal === '') continue;
142         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
143     }
144
145     if (count($queryStringSections) === 0) return $path;
146
147     return baseUrl($path . '?' . implode('&', $queryStringSections));
148 }