2015-12-16 12:09:44 -05:00
|
|
|
<?php
|
|
|
|
|
2019-03-09 11:50:22 -05:00
|
|
|
use BookStack\Auth\Permissions\PermissionService;
|
2019-03-09 16:15:45 -05:00
|
|
|
use BookStack\Entities\Entity;
|
2016-07-01 15:11:49 -04:00
|
|
|
use BookStack\Ownable;
|
|
|
|
|
2016-08-27 06:27:23 -04:00
|
|
|
/**
|
|
|
|
* Get the path to a versioned file.
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
2016-09-03 07:08:58 -04:00
|
|
|
* @throws Exception
|
2016-08-27 06:27:23 -04:00
|
|
|
*/
|
2016-09-03 07:08:58 -04:00
|
|
|
function versioned_asset($file = '')
|
2016-08-27 06:27:23 -04:00
|
|
|
{
|
2016-10-30 13:44:00 -04:00
|
|
|
static $version = null;
|
|
|
|
|
|
|
|
if (is_null($version)) {
|
|
|
|
$versionFile = base_path('version');
|
|
|
|
$version = trim(file_get_contents($versionFile));
|
2016-08-27 06:27:23 -04:00
|
|
|
}
|
2015-12-16 12:09:44 -05:00
|
|
|
|
2016-10-30 13:44:00 -04:00
|
|
|
$additional = '';
|
|
|
|
if (config('app.env') === 'development') {
|
|
|
|
$additional = sha1_file(public_path($file));
|
2016-08-27 06:27:23 -04:00
|
|
|
}
|
2015-12-16 12:09:44 -05:00
|
|
|
|
2016-10-30 13:44:00 -04:00
|
|
|
$path = $file . '?version=' . urlencode($version) . $additional;
|
|
|
|
return baseUrl($path);
|
2016-02-27 14:24:42 -05:00
|
|
|
}
|
|
|
|
|
2016-09-29 07:43:46 -04:00
|
|
|
/**
|
|
|
|
* Helper method to get the current User.
|
|
|
|
* Defaults to public 'Guest' user if not logged in.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return \BookStack\Auth\User
|
2016-09-29 07:43:46 -04:00
|
|
|
*/
|
|
|
|
function user()
|
|
|
|
{
|
2018-09-25 07:30:50 -04:00
|
|
|
return auth()->user() ?: \BookStack\Auth\User::getDefault();
|
2016-09-29 07:43:46 -04:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:19:29 -05:00
|
|
|
/**
|
|
|
|
* Check if current user is a signed in user.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-02-03 12:34:15 -05:00
|
|
|
function signedInUser() : bool
|
2017-02-05 16:19:29 -05:00
|
|
|
{
|
|
|
|
return auth()->user() && !auth()->user()->isDefault();
|
|
|
|
}
|
|
|
|
|
2019-02-03 12:34:15 -05:00
|
|
|
/**
|
|
|
|
* Check if the current user has general access.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function hasAppAccess() : bool {
|
|
|
|
return !auth()->guest() || setting('app-public');
|
|
|
|
}
|
|
|
|
|
2016-02-27 14:24:42 -05:00
|
|
|
/**
|
|
|
|
* Check if the current user has a permission.
|
2016-05-01 16:20:50 -04:00
|
|
|
* If an ownable element is passed in the jointPermissions are checked against
|
2016-02-27 14:24:42 -05:00
|
|
|
* that particular item.
|
2019-03-09 11:50:22 -05:00
|
|
|
* @param string $permission
|
2016-07-01 15:11:49 -04:00
|
|
|
* @param Ownable $ownable
|
2016-02-27 14:24:42 -05:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-03-09 11:50:22 -05:00
|
|
|
function userCan(string $permission, Ownable $ownable = null)
|
2016-02-27 14:24:42 -05:00
|
|
|
{
|
|
|
|
if ($ownable === null) {
|
2016-09-29 07:43:46 -04:00
|
|
|
return user() && user()->can($permission);
|
2016-02-27 14:24:42 -05:00
|
|
|
}
|
|
|
|
|
2016-02-29 15:31:21 -05:00
|
|
|
// Check permission on ownable item
|
2019-03-09 11:50:22 -05:00
|
|
|
$permissionService = app(PermissionService::class);
|
2016-07-01 15:11:49 -04:00
|
|
|
return $permissionService->checkOwnableUserAccess($ownable, $permission);
|
2016-03-06 07:55:08 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 00:55:28 -05:00
|
|
|
/**
|
2019-03-09 11:50:22 -05:00
|
|
|
* Check if the current user has the given permission
|
|
|
|
* on any item in the system.
|
|
|
|
* @param string $permission
|
2019-03-09 16:15:45 -05:00
|
|
|
* @param string|null $entityClass
|
2019-01-02 00:55:28 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-03-09 16:15:45 -05:00
|
|
|
function userCanOnAny(string $permission, string $entityClass = null)
|
2019-01-02 00:55:28 -05:00
|
|
|
{
|
2019-03-09 11:50:22 -05:00
|
|
|
$permissionService = app(PermissionService::class);
|
2019-03-09 16:15:45 -05:00
|
|
|
return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
|
2019-01-02 00:55:28 -05:00
|
|
|
}
|
|
|
|
|
2016-03-06 07:55:08 -05:00
|
|
|
/**
|
|
|
|
* Helper to access system settings.
|
|
|
|
* @param $key
|
|
|
|
* @param bool $default
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return bool|string|\BookStack\Settings\SettingService
|
2016-03-06 07:55:08 -05:00
|
|
|
*/
|
2017-01-15 11:27:24 -05:00
|
|
|
function setting($key = null, $default = false)
|
2016-03-06 07:55:08 -05:00
|
|
|
{
|
2018-09-25 07:30:50 -04:00
|
|
|
$settingService = resolve(\BookStack\Settings\SettingService::class);
|
2018-01-28 11:58:52 -05:00
|
|
|
if (is_null($key)) {
|
|
|
|
return $settingService;
|
|
|
|
}
|
2016-03-06 07:55:08 -05:00
|
|
|
return $settingService->get($key, $default);
|
|
|
|
}
|
2016-05-22 05:44:31 -04:00
|
|
|
|
2016-08-13 12:56:20 -04:00
|
|
|
/**
|
|
|
|
* Helper to create url's relative to the applications root path.
|
2016-08-21 09:49:40 -04:00
|
|
|
* @param string $path
|
|
|
|
* @param bool $forceAppDomain
|
2016-08-13 12:56:20 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-21 09:49:40 -04:00
|
|
|
function baseUrl($path, $forceAppDomain = false)
|
2016-08-13 12:56:20 -04:00
|
|
|
{
|
2016-08-21 09:49:40 -04:00
|
|
|
$isFullUrl = strpos($path, 'http') === 0;
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($isFullUrl && !$forceAppDomain) {
|
|
|
|
return $path;
|
|
|
|
}
|
2018-11-04 10:18:27 -05:00
|
|
|
|
2016-08-13 12:56:20 -04:00
|
|
|
$path = trim($path, '/');
|
2018-11-09 16:29:30 -05:00
|
|
|
$base = rtrim(config('app.url'), '/');
|
2016-08-21 09:49:40 -04:00
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
// Remove non-specified domain if forced and we have a domain
|
2016-08-21 09:49:40 -04:00
|
|
|
if ($isFullUrl && $forceAppDomain) {
|
2018-11-09 16:29:30 -05:00
|
|
|
if (!empty($base) && strpos($path, $base) === 0) {
|
|
|
|
$path = trim(substr($path, strlen($base) - 1));
|
2018-11-04 10:18:27 -05:00
|
|
|
}
|
2016-08-21 09:49:40 -04:00
|
|
|
$explodedPath = explode('/', $path);
|
|
|
|
$path = implode('/', array_splice($explodedPath, 3));
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
// Return normal url path if not specified in config
|
|
|
|
if (config('app.url') === '') {
|
|
|
|
return url($path);
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:29:30 -05:00
|
|
|
return $base . '/' . $path;
|
2016-08-13 12:56:20 -04:00
|
|
|
}
|
|
|
|
|
2016-08-14 07:29:35 -04:00
|
|
|
/**
|
|
|
|
* Get an instance of the redirector.
|
|
|
|
* Overrides the default laravel redirect helper.
|
|
|
|
* Ensures it redirects even when the app is in a subdirectory.
|
|
|
|
*
|
|
|
|
* @param string|null $to
|
|
|
|
* @param int $status
|
|
|
|
* @param array $headers
|
|
|
|
* @param bool $secure
|
|
|
|
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
function redirect($to = null, $status = 302, $headers = [], $secure = null)
|
|
|
|
{
|
|
|
|
if (is_null($to)) {
|
|
|
|
return app('redirect');
|
|
|
|
}
|
|
|
|
|
|
|
|
$to = baseUrl($to);
|
|
|
|
|
|
|
|
return app('redirect')->to($to, $status, $headers, $secure);
|
|
|
|
}
|
|
|
|
|
2018-02-17 07:36:24 -05:00
|
|
|
/**
|
|
|
|
* Get a path to a theme resource.
|
|
|
|
* @param string $path
|
|
|
|
* @return string|boolean
|
|
|
|
*/
|
|
|
|
function theme_path($path = '')
|
|
|
|
{
|
|
|
|
$theme = config('view.theme');
|
|
|
|
if (!$theme) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 14:49:00 -05:00
|
|
|
*
|
|
|
|
* Returns an empty string if icon file not found.
|
2018-02-17 07:36:24 -05:00
|
|
|
* @param $name
|
|
|
|
* @param array $attrs
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-01-28 11:58:52 -05:00
|
|
|
function icon($name, $attrs = [])
|
|
|
|
{
|
2018-02-17 14:49:00 -05:00
|
|
|
$attrs = array_merge([
|
|
|
|
'class' => 'svg-icon',
|
|
|
|
'data-icon' => $name
|
|
|
|
], $attrs);
|
2017-02-04 06:01:49 -05:00
|
|
|
$attrString = ' ';
|
|
|
|
foreach ($attrs as $attrName => $attr) {
|
|
|
|
$attrString .= $attrName . '="' . $attr . '" ';
|
|
|
|
}
|
2018-02-17 07:36:24 -05:00
|
|
|
|
|
|
|
$iconPath = resource_path('assets/icons/' . $name . '.svg');
|
|
|
|
$themeIconPath = theme_path('icons/' . $name . '.svg');
|
|
|
|
if ($themeIconPath && file_exists($themeIconPath)) {
|
|
|
|
$iconPath = $themeIconPath;
|
2018-02-17 14:49:00 -05:00
|
|
|
} else if (!file_exists($iconPath)) {
|
|
|
|
return '';
|
2018-02-17 07:36:24 -05:00
|
|
|
}
|
|
|
|
|
2017-02-04 06:01:49 -05:00
|
|
|
$fileContents = file_get_contents($iconPath);
|
|
|
|
return str_replace('<svg', '<svg' . $attrString, $fileContents);
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:44:31 -04: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);
|
2016-10-30 13:44:00 -04:00
|
|
|
|
2016-05-22 05:44:31 -04: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';
|
|
|
|
} else {
|
|
|
|
$queryData['order'] = 'asc';
|
|
|
|
}
|
2016-10-30 13:44:00 -04:00
|
|
|
|
2016-05-22 05:44:31 -04:00
|
|
|
foreach ($queryData as $name => $value) {
|
|
|
|
$trimmedVal = trim($value);
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($trimmedVal === '') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-22 05:44:31 -04:00
|
|
|
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
|
|
|
}
|
|
|
|
|
2018-01-28 11:58:52 -05:00
|
|
|
if (count($queryStringSections) === 0) {
|
|
|
|
return $path;
|
|
|
|
}
|
2016-05-22 05:44:31 -04:00
|
|
|
|
2016-08-14 07:29:35 -04:00
|
|
|
return baseUrl($path . '?' . implode('&', $queryStringSections));
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|