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