2015-12-16 18:09:44 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-01 21:11:49 +02:00
|
|
|
use BookStack\Ownable;
|
|
|
|
|
2016-03-05 19:09:21 +01:00
|
|
|
if (!function_exists('versioned_asset')) {
|
2015-12-16 18:09:44 +01:00
|
|
|
/**
|
|
|
|
* Get the path to a versioned file.
|
|
|
|
*
|
2016-03-05 19:09:21 +01:00
|
|
|
* @param string $file
|
2015-12-16 18:09:44 +01:00
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function versioned_asset($file)
|
|
|
|
{
|
|
|
|
static $manifest = null;
|
|
|
|
|
|
|
|
if (is_null($manifest)) {
|
|
|
|
$manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($manifest[$file])) {
|
|
|
|
return '/' . $manifest[$file];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(public_path($file))) {
|
|
|
|
return '/' . $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
|
|
|
|
}
|
2016-02-27 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user has a permission.
|
2016-05-01 22:20:50 +02:00
|
|
|
* If an ownable element is passed in the jointPermissions are checked against
|
2016-02-27 20:24:42 +01:00
|
|
|
* that particular item.
|
|
|
|
* @param $permission
|
2016-07-01 21:11:49 +02:00
|
|
|
* @param Ownable $ownable
|
2016-02-27 20:24:42 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-07-01 21:11:49 +02:00
|
|
|
function userCan($permission, Ownable $ownable = null)
|
2016-02-27 20:24:42 +01:00
|
|
|
{
|
|
|
|
if ($ownable === null) {
|
|
|
|
return auth()->user() && auth()->user()->can($permission);
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:31:21 +01:00
|
|
|
// Check permission on ownable item
|
2016-07-01 21:11:49 +02:00
|
|
|
$permissionService = app(\BookStack\Services\PermissionService::class);
|
|
|
|
return $permissionService->checkOwnableUserAccess($ownable, $permission);
|
2016-03-06 13:55:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to access system settings.
|
|
|
|
* @param $key
|
|
|
|
* @param bool $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function setting($key, $default = false)
|
|
|
|
{
|
|
|
|
$settingService = app('BookStack\Services\SettingService');
|
|
|
|
return $settingService->get($key, $default);
|
|
|
|
}
|
2016-05-22 11:44:31 +02: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.
|
|
|
|
* @param $path
|
|
|
|
* @param array $data
|
|
|
|
* @param array $overrideData
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function sortUrl($path, $data, $overrideData = [])
|
|
|
|
{
|
|
|
|
$queryStringSections = [];
|
|
|
|
$queryData = array_merge($data, $overrideData);
|
|
|
|
|
|
|
|
// Change sorting direction is already sorted on current attribute
|
|
|
|
if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
|
|
|
|
$queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
|
|
|
|
} else {
|
|
|
|
$queryData['order'] = 'asc';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($queryData as $name => $value) {
|
|
|
|
$trimmedVal = trim($value);
|
|
|
|
if ($trimmedVal === '') continue;
|
|
|
|
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($queryStringSections) === 0) return $path;
|
|
|
|
|
|
|
|
return $path . '?' . implode('&', $queryStringSections);
|
|
|
|
}
|