]> BookStack Code Mirror - bookstack/blob - app/helpers.php
Page Attachments - Improved UI, Now initially complete
[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  * Helper method to get the current User.
41  * Defaults to public 'Guest' user if not logged in.
42  * @return \BookStack\User
43  */
44 function user()
45 {
46     return auth()->user() ?: \BookStack\User::getDefault();
47 }
48
49 /**
50  * Check if the current user has a permission.
51  * If an ownable element is passed in the jointPermissions are checked against
52  * that particular item.
53  * @param $permission
54  * @param Ownable $ownable
55  * @return mixed
56  */
57 function userCan($permission, Ownable $ownable = null)
58 {
59     if ($ownable === null) {
60         return user() && user()->can($permission);
61     }
62
63     // Check permission on ownable item
64     $permissionService = app(\BookStack\Services\PermissionService::class);
65     return $permissionService->checkOwnableUserAccess($ownable, $permission);
66 }
67
68 /**
69  * Helper to access system settings.
70  * @param $key
71  * @param bool $default
72  * @return mixed
73  */
74 function setting($key, $default = false)
75 {
76     $settingService = app(\BookStack\Services\SettingService::class);
77     return $settingService->get($key, $default);
78 }
79
80 /**
81  * Helper to create url's relative to the applications root path.
82  * @param string $path
83  * @param bool $forceAppDomain
84  * @return string
85  */
86 function baseUrl($path, $forceAppDomain = false)
87 {
88     $isFullUrl = strpos($path, 'http') === 0;
89     if ($isFullUrl && !$forceAppDomain) return $path;
90     $path = trim($path, '/');
91
92     // Remove non-specified domain if forced and we have a domain
93     if ($isFullUrl && $forceAppDomain) {
94         $explodedPath = explode('/', $path);
95         $path = implode('/', array_splice($explodedPath, 3));
96     }
97
98     // Return normal url path if not specified in config
99     if (config('app.url') === '') {
100         return url($path);
101     }
102
103     return rtrim(config('app.url'), '/') . '/' . $path;
104 }
105
106 /**
107  * Get an instance of the redirector.
108  * Overrides the default laravel redirect helper.
109  * Ensures it redirects even when the app is in a subdirectory.
110  *
111  * @param  string|null  $to
112  * @param  int     $status
113  * @param  array   $headers
114  * @param  bool    $secure
115  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
116  */
117 function redirect($to = null, $status = 302, $headers = [], $secure = null)
118 {
119     if (is_null($to)) {
120         return app('redirect');
121     }
122
123     $to = baseUrl($to);
124
125     return app('redirect')->to($to, $status, $headers, $secure);
126 }
127
128 /**
129  * Generate a url with multiple parameters for sorting purposes.
130  * Works out the logic to set the correct sorting direction
131  * Discards empty parameters and allows overriding.
132  * @param $path
133  * @param array $data
134  * @param array $overrideData
135  * @return string
136  */
137 function sortUrl($path, $data, $overrideData = [])
138 {
139     $queryStringSections = [];
140     $queryData = array_merge($data, $overrideData);
141     
142     // Change sorting direction is already sorted on current attribute
143     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
144         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
145     } else {
146         $queryData['order'] = 'asc';
147     }
148     
149     foreach ($queryData as $name => $value) {
150         $trimmedVal = trim($value);
151         if ($trimmedVal === '') continue;
152         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
153     }
154
155     if (count($queryStringSections) === 0) return $path;
156
157     return baseUrl($path . '?' . implode('&', $queryStringSections));
158 }