2015-12-17 01:09:44 +08:00
|
|
|
<?php
|
|
|
|
|
2019-03-10 00:50:22 +08:00
|
|
|
use BookStack\Auth\Permissions\PermissionService;
|
2019-08-04 21:26:39 +08:00
|
|
|
use BookStack\Auth\User;
|
2016-07-02 03:11:49 +08:00
|
|
|
use BookStack\Ownable;
|
2019-08-04 21:26:39 +08:00
|
|
|
use BookStack\Settings\SettingService;
|
2016-07-02 03:11:49 +08:00
|
|
|
|
2016-08-27 18:27:23 +08:00
|
|
|
/**
|
|
|
|
* Get the path to a versioned file.
|
2016-09-03 19:08:58 +08:00
|
|
|
* @throws Exception
|
2016-08-27 18:27:23 +08:00
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function versioned_asset(string $file = ''): string
|
2016-08-27 18:27:23 +08:00
|
|
|
{
|
2016-10-31 01:44:00 +08:00
|
|
|
static $version = null;
|
|
|
|
|
|
|
|
if (is_null($version)) {
|
|
|
|
$versionFile = base_path('version');
|
|
|
|
$version = trim(file_get_contents($versionFile));
|
2016-08-27 18:27:23 +08:00
|
|
|
}
|
2015-12-17 01:09:44 +08:00
|
|
|
|
2016-10-31 01:44:00 +08:00
|
|
|
$additional = '';
|
|
|
|
if (config('app.env') === 'development') {
|
|
|
|
$additional = sha1_file(public_path($file));
|
2016-08-27 18:27:23 +08:00
|
|
|
}
|
2015-12-17 01:09:44 +08:00
|
|
|
|
2016-10-31 01:44:00 +08:00
|
|
|
$path = $file . '?version=' . urlencode($version) . $additional;
|
2019-08-04 21:26:39 +08:00
|
|
|
return url($path);
|
2016-02-28 03:24:42 +08:00
|
|
|
}
|
|
|
|
|
2016-09-29 19:43:46 +08:00
|
|
|
/**
|
|
|
|
* Helper method to get the current User.
|
|
|
|
* Defaults to public 'Guest' user if not logged in.
|
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function user(): User
|
2016-09-29 19:43:46 +08:00
|
|
|
{
|
2019-08-04 21:26:39 +08:00
|
|
|
return auth()->user() ?: User::getDefault();
|
2016-09-29 19:43:46 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 05:19:29 +08:00
|
|
|
/**
|
|
|
|
* Check if current user is a signed in user.
|
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function signedInUser(): bool
|
2017-02-06 05:19:29 +08:00
|
|
|
{
|
|
|
|
return auth()->user() && !auth()->user()->isDefault();
|
|
|
|
}
|
|
|
|
|
2019-02-04 01:34:15 +08:00
|
|
|
/**
|
|
|
|
* Check if the current user has general access.
|
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function hasAppAccess(): bool
|
2019-05-05 21:54:37 +08:00
|
|
|
{
|
2019-02-04 01:34:15 +08:00
|
|
|
return !auth()->guest() || setting('app-public');
|
|
|
|
}
|
|
|
|
|
2016-02-28 03:24:42 +08:00
|
|
|
/**
|
2020-11-01 07:05:48 +08: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-28 03:24:42 +08:00
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function userCan(string $permission, Ownable $ownable = null): bool
|
2016-02-28 03:24:42 +08:00
|
|
|
{
|
|
|
|
if ($ownable === null) {
|
2016-09-29 19:43:46 +08:00
|
|
|
return user() && user()->can($permission);
|
2016-02-28 03:24:42 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 04:31:21 +08:00
|
|
|
// Check permission on ownable item
|
2019-03-10 00:50:22 +08:00
|
|
|
$permissionService = app(PermissionService::class);
|
2016-07-02 03:11:49 +08:00
|
|
|
return $permissionService->checkOwnableUserAccess($ownable, $permission);
|
2016-03-06 20:55:08 +08:00
|
|
|
}
|
|
|
|
|
2019-01-02 13:55:28 +08:00
|
|
|
/**
|
2019-03-10 00:50:22 +08:00
|
|
|
* Check if the current user has the given permission
|
|
|
|
* on any item in the system.
|
2019-01-02 13:55:28 +08:00
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function userCanOnAny(string $permission, string $entityClass = null): bool
|
2019-01-02 13:55:28 +08:00
|
|
|
{
|
2019-03-10 00:50:22 +08:00
|
|
|
$permissionService = app(PermissionService::class);
|
2019-03-10 05:15:45 +08:00
|
|
|
return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
|
2019-01-02 13:55:28 +08:00
|
|
|
}
|
|
|
|
|
2016-03-06 20:55:08 +08:00
|
|
|
/**
|
|
|
|
* Helper to access system settings.
|
2019-08-04 21:26:39 +08:00
|
|
|
* @return bool|string|SettingService
|
2016-03-06 20:55:08 +08:00
|
|
|
*/
|
2019-09-16 06:28:23 +08:00
|
|
|
function setting(string $key = null, $default = false)
|
2016-03-06 20:55:08 +08:00
|
|
|
{
|
2019-08-04 21:26:39 +08:00
|
|
|
$settingService = resolve(SettingService::class);
|
2020-11-01 07:05:48 +08:00
|
|
|
|
2018-01-29 00:58:52 +08:00
|
|
|
if (is_null($key)) {
|
|
|
|
return $settingService;
|
|
|
|
}
|
2020-11-01 07:05:48 +08:00
|
|
|
|
2016-03-06 20:55:08 +08:00
|
|
|
return $settingService->get($key, $default);
|
|
|
|
}
|
2016-05-22 17:44:31 +08:00
|
|
|
|
2018-02-17 20:36:24 +08:00
|
|
|
/**
|
|
|
|
* Get a path to a theme resource.
|
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function theme_path(string $path = ''): string
|
2018-02-17 20:36:24 +08:00
|
|
|
{
|
|
|
|
$theme = config('view.theme');
|
2020-11-01 07:05:48 +08:00
|
|
|
|
2018-02-17 20:36:24 +08:00
|
|
|
if (!$theme) {
|
2019-08-04 21:26:39 +08:00
|
|
|
return '';
|
2018-02-17 20:36:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get fetch an SVG icon as a string.
|
|
|
|
* Checks for icons defined within a custom theme before defaulting back
|
|
|
|
* to the 'resources/assets/icons' folder.
|
2018-02-18 03:49:00 +08:00
|
|
|
*
|
|
|
|
* Returns an empty string if icon file not found.
|
2018-02-17 20:36:24 +08:00
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function icon(string $name, array $attrs = []): string
|
2018-01-29 00:58:52 +08:00
|
|
|
{
|
2018-02-18 03:49:00 +08:00
|
|
|
$attrs = array_merge([
|
2019-08-25 01:26:28 +08:00
|
|
|
'class' => 'svg-icon',
|
|
|
|
'data-icon' => $name,
|
|
|
|
'role' => 'presentation',
|
2018-02-18 03:49:00 +08:00
|
|
|
], $attrs);
|
2017-02-04 19:01:49 +08:00
|
|
|
$attrString = ' ';
|
|
|
|
foreach ($attrs as $attrName => $attr) {
|
|
|
|
$attrString .= $attrName . '="' . $attr . '" ';
|
|
|
|
}
|
2018-02-17 20:36:24 +08:00
|
|
|
|
2019-09-07 06:36:16 +08:00
|
|
|
$iconPath = resource_path('icons/' . $name . '.svg');
|
2018-02-17 20:36:24 +08:00
|
|
|
$themeIconPath = theme_path('icons/' . $name . '.svg');
|
2020-11-01 07:05:48 +08:00
|
|
|
|
2018-02-17 20:36:24 +08:00
|
|
|
if ($themeIconPath && file_exists($themeIconPath)) {
|
|
|
|
$iconPath = $themeIconPath;
|
2018-02-18 03:49:00 +08:00
|
|
|
} else if (!file_exists($iconPath)) {
|
|
|
|
return '';
|
2018-02-17 20:36:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-04 19:01:49 +08:00
|
|
|
$fileContents = file_get_contents($iconPath);
|
|
|
|
return str_replace('<svg', '<svg' . $attrString, $fileContents);
|
|
|
|
}
|
|
|
|
|
2016-05-22 17:44:31 +08:00
|
|
|
/**
|
|
|
|
* Generate a url with multiple parameters for sorting purposes.
|
|
|
|
* Works out the logic to set the correct sorting direction
|
|
|
|
* Discards empty parameters and allows overriding.
|
|
|
|
*/
|
2019-09-16 01:29:51 +08:00
|
|
|
function sortUrl(string $path, array $data, array $overrideData = []): string
|
2016-05-22 17:44:31 +08:00
|
|
|
{
|
|
|
|
$queryStringSections = [];
|
|
|
|
$queryData = array_merge($data, $overrideData);
|
2016-10-31 01:44:00 +08:00
|
|
|
|
2016-05-22 17:44:31 +08: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 19:06:45 +08:00
|
|
|
} elseif (isset($overrideData['sort'])) {
|
2016-05-22 17:44:31 +08:00
|
|
|
$queryData['order'] = 'asc';
|
|
|
|
}
|
2016-10-31 01:44:00 +08:00
|
|
|
|
2016-05-22 17:44:31 +08:00
|
|
|
foreach ($queryData as $name => $value) {
|
|
|
|
$trimmedVal = trim($value);
|
2018-01-29 00:58:52 +08:00
|
|
|
if ($trimmedVal === '') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-22 17:44:31 +08:00
|
|
|
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:58:52 +08:00
|
|
|
if (count($queryStringSections) === 0) {
|
|
|
|
return $path;
|
|
|
|
}
|
2016-05-22 17:44:31 +08:00
|
|
|
|
2019-08-04 21:26:39 +08:00
|
|
|
return url($path . '?' . implode('&', $queryStringSections));
|
2018-01-29 00:58:52 +08:00
|
|
|
}
|