2015-12-16 18:09:44 +01:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\App\Model;
|
|
|
|
use BookStack\Permissions\PermissionApplicator;
|
2019-08-04 15:26:39 +02:00
|
|
|
use BookStack\Settings\SettingService;
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\Users\Models\User;
|
2016-07-01 21:11:49 +02:00
|
|
|
|
2016-08-27 12:27:23 +02:00
|
|
|
/**
|
|
|
|
* Get the path to a versioned file.
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2016-09-03 13:08:58 +02:00
|
|
|
* @throws Exception
|
2016-08-27 12:27:23 +02:00
|
|
|
*/
|
2019-09-15 19:29:51 +02:00
|
|
|
function versioned_asset(string $file = ''): string
|
2016-08-27 12:27:23 +02:00
|
|
|
{
|
2016-10-30 18:44:00 +01:00
|
|
|
static $version = null;
|
|
|
|
|
|
|
|
if (is_null($version)) {
|
|
|
|
$versionFile = base_path('version');
|
|
|
|
$version = trim(file_get_contents($versionFile));
|
2016-08-27 12:27:23 +02:00
|
|
|
}
|
2015-12-16 18:09:44 +01:00
|
|
|
|
2016-10-30 18:44:00 +01:00
|
|
|
$additional = '';
|
|
|
|
if (config('app.env') === 'development') {
|
|
|
|
$additional = sha1_file(public_path($file));
|
2016-08-27 12:27:23 +02:00
|
|
|
}
|
2015-12-16 18:09:44 +01:00
|
|
|
|
2016-10-30 18:44:00 +01:00
|
|
|
$path = $file . '?version=' . urlencode($version) . $additional;
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2019-08-04 15:26:39 +02:00
|
|
|
return url($path);
|
2016-02-27 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:43:46 +02:00
|
|
|
/**
|
|
|
|
* Helper method to get the current User.
|
|
|
|
* Defaults to public 'Guest' user if not logged in.
|
|
|
|
*/
|
2019-09-15 19:29:51 +02:00
|
|
|
function user(): User
|
2016-09-29 13:43:46 +02:00
|
|
|
{
|
2023-09-16 14:18:35 +02:00
|
|
|
return auth()->user() ?: User::getGuest();
|
2019-02-03 18:34:15 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 20:24:42 +01:00
|
|
|
/**
|
2020-11-01 00:05:48 +01:00
|
|
|
* Check if the current user has a permission. If an ownable element
|
|
|
|
* is passed in the jointPermissions are checked against that particular item.
|
2016-02-27 20:24:42 +01:00
|
|
|
*/
|
2020-12-30 19:25:35 +01:00
|
|
|
function userCan(string $permission, Model $ownable = null): bool
|
2016-02-27 20:24:42 +01:00
|
|
|
{
|
|
|
|
if ($ownable === null) {
|
2023-09-16 14:18:35 +02:00
|
|
|
return user()->can($permission);
|
2016-02-27 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:31:21 +01:00
|
|
|
// Check permission on ownable item
|
2023-09-16 19:25:08 +02:00
|
|
|
$permissions = app()->make(PermissionApplicator::class);
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2022-07-12 21:15:41 +02:00
|
|
|
return $permissions->checkOwnableUserAccess($ownable, $permission);
|
2016-03-06 13:55:08 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:55:28 +01:00
|
|
|
/**
|
2022-07-16 14:17:08 +02:00
|
|
|
* Check if the current user can perform the given action on any items in the system.
|
|
|
|
* Can be provided the class name of an entity to filter ability to that specific entity type.
|
2019-01-02 06:55:28 +01:00
|
|
|
*/
|
2022-07-16 14:17:08 +02:00
|
|
|
function userCanOnAny(string $action, string $entityClass = ''): bool
|
2019-01-02 06:55:28 +01:00
|
|
|
{
|
2023-09-16 19:25:08 +02:00
|
|
|
$permissions = app()->make(PermissionApplicator::class);
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2022-07-16 14:17:08 +02:00
|
|
|
return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
|
2019-01-02 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:55:08 +01:00
|
|
|
/**
|
|
|
|
* Helper to access system settings.
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2021-02-11 00:21:49 +01:00
|
|
|
* @return mixed|SettingService
|
2016-03-06 13:55:08 +01:00
|
|
|
*/
|
2021-02-11 00:21:49 +01:00
|
|
|
function setting(string $key = null, $default = null)
|
2016-03-06 13:55:08 +01:00
|
|
|
{
|
2023-09-16 19:25:08 +02:00
|
|
|
$settingService = app()->make(SettingService::class);
|
2020-11-01 00:05:48 +01:00
|
|
|
|
2018-01-28 17:58:52 +01:00
|
|
|
if (is_null($key)) {
|
|
|
|
return $settingService;
|
|
|
|
}
|
2020-11-01 00:05:48 +01:00
|
|
|
|
2016-03-06 13:55:08 +01:00
|
|
|
return $settingService->get($key, $default);
|
|
|
|
}
|
2016-05-22 11:44:31 +02:00
|
|
|
|
2018-02-17 13:36:24 +01:00
|
|
|
/**
|
|
|
|
* Get a path to a theme resource.
|
2021-07-03 12:53:46 +02:00
|
|
|
* Returns null if a theme is not configured and
|
|
|
|
* therefore a full path is not available for use.
|
2018-02-17 13:36:24 +01:00
|
|
|
*/
|
2021-07-03 12:53:46 +02:00
|
|
|
function theme_path(string $path = ''): ?string
|
2018-02-17 13:36:24 +01:00
|
|
|
{
|
|
|
|
$theme = config('view.theme');
|
2020-11-01 00:05:48 +01:00
|
|
|
|
2018-02-17 13:36:24 +01:00
|
|
|
if (!$theme) {
|
2021-07-03 12:53:46 +02:00
|
|
|
return null;
|
2018-02-17 13:36:24 +01:00
|
|
|
}
|
|
|
|
|
2021-06-26 17:23:15 +02:00
|
|
|
return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
|
2018-02-17 13:36:24 +01:00
|
|
|
}
|
|
|
|
|
2016-05-22 11:44:31 +02:00
|
|
|
/**
|
2023-04-27 14:47:49 +02:00
|
|
|
* Generate a URL with multiple parameters for sorting purposes.
|
2016-05-22 11:44:31 +02:00
|
|
|
* Works out the logic to set the correct sorting direction
|
|
|
|
* Discards empty parameters and allows overriding.
|
|
|
|
*/
|
2019-09-15 19:29:51 +02:00
|
|
|
function sortUrl(string $path, array $data, array $overrideData = []): string
|
2016-05-22 11:44:31 +02:00
|
|
|
{
|
|
|
|
$queryStringSections = [];
|
|
|
|
$queryData = array_merge($data, $overrideData);
|
2016-10-30 18:44:00 +01:00
|
|
|
|
2016-05-22 11:44:31 +02:00
|
|
|
// Change sorting direction is already sorted on current attribute
|
|
|
|
if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
|
|
|
|
$queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
|
2020-09-19 13:06:45 +02:00
|
|
|
} elseif (isset($overrideData['sort'])) {
|
2016-05-22 11:44:31 +02:00
|
|
|
$queryData['order'] = 'asc';
|
|
|
|
}
|
2016-10-30 18:44:00 +01:00
|
|
|
|
2016-05-22 11:44:31 +02:00
|
|
|
foreach ($queryData as $name => $value) {
|
|
|
|
$trimmedVal = trim($value);
|
2018-01-28 17:58:52 +01:00
|
|
|
if ($trimmedVal === '') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-22 11:44:31 +02:00
|
|
|
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
|
|
|
}
|
|
|
|
|
2018-01-28 17:58:52 +01:00
|
|
|
if (count($queryStringSections) === 0) {
|
2023-04-27 14:47:49 +02:00
|
|
|
return url($path);
|
2018-01-28 17:58:52 +01:00
|
|
|
}
|
2016-05-22 11:44:31 +02:00
|
|
|
|
2019-08-04 15:26:39 +02:00
|
|
|
return url($path . '?' . implode('&', $queryStringSections));
|
2018-01-28 17:58:52 +01:00
|
|
|
}
|