]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Improved Exception handling, Removed npm requirement for testing
[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');
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     if ($isFullUrl && $forceAppDomain) {
83         $explodedPath = explode('/', $path);
84         $path = implode('/', array_splice($explodedPath, 3));
85     }
86
87     return rtrim(config('app.url'), '/') . '/' . $path;
88 }
89
90 /**
91  * Get an instance of the redirector.
92  * Overrides the default laravel redirect helper.
93  * Ensures it redirects even when the app is in a subdirectory.
94  *
95  * @param  string|null  $to
96  * @param  int     $status
97  * @param  array   $headers
98  * @param  bool    $secure
99  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
100  */
101 function redirect($to = null, $status = 302, $headers = [], $secure = null)
102 {
103     if (is_null($to)) {
104         return app('redirect');
105     }
106
107     $to = baseUrl($to);
108
109     return app('redirect')->to($to, $status, $headers, $secure);
110 }
111
112 /**
113  * Generate a url with multiple parameters for sorting purposes.
114  * Works out the logic to set the correct sorting direction
115  * Discards empty parameters and allows overriding.
116  * @param $path
117  * @param array $data
118  * @param array $overrideData
119  * @return string
120  */
121 function sortUrl($path, $data, $overrideData = [])
122 {
123     $queryStringSections = [];
124     $queryData = array_merge($data, $overrideData);
125     
126     // Change sorting direction is already sorted on current attribute
127     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
128         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
129     } else {
130         $queryData['order'] = 'asc';
131     }
132     
133     foreach ($queryData as $name => $value) {
134         $trimmedVal = trim($value);
135         if ($trimmedVal === '') continue;
136         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
137     }
138
139     if (count($queryStringSections) === 0) return $path;
140
141     return baseUrl($path . '?' . implode('&', $queryStringSections));
142 }