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