2015-12-16 18:09:44 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-01 21:11:49 +02:00
|
|
|
use BookStack\Ownable;
|
|
|
|
|
2016-08-27 12:27:23 +02:00
|
|
|
/**
|
|
|
|
* Get the path to a versioned file.
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
2016-09-03 13:08:58 +02:00
|
|
|
* @throws Exception
|
2016-08-27 12:27:23 +02:00
|
|
|
*/
|
2016-09-03 13:08:58 +02:00
|
|
|
function versioned_asset($file = '')
|
2016-08-27 12:27:23 +02:00
|
|
|
{
|
2016-09-03 13:08:58 +02:00
|
|
|
// Don't require css and JS assets for testing
|
|
|
|
if (config('app.env') === 'testing') return '';
|
|
|
|
|
2016-08-27 12:27:23 +02:00
|
|
|
static $manifest = null;
|
2016-09-03 13:08:58 +02:00
|
|
|
$manifestPath = 'build/manifest.json';
|
2015-12-16 18:09:44 +01:00
|
|
|
|
2016-09-03 13:08:58 +02:00
|
|
|
if (is_null($manifest) && file_exists($manifestPath)) {
|
|
|
|
$manifest = json_decode(file_get_contents(public_path($manifestPath)), true);
|
|
|
|
} else if (!file_exists($manifestPath)) {
|
|
|
|
if (config('app.env') !== 'production') {
|
|
|
|
$path = public_path($manifestPath);
|
|
|
|
$error = "No {$path} file found, Ensure you have built the css/js assets using gulp.";
|
|
|
|
} else {
|
|
|
|
$error = "No {$manifestPath} file found, Ensure you are using the release version of BookStack";
|
|
|
|
}
|
|
|
|
throw new \Exception($error);
|
2016-08-27 12:27:23 +02:00
|
|
|
}
|
2015-12-16 18:09:44 +01:00
|
|
|
|
2016-08-27 12:27:23 +02:00
|
|
|
if (isset($manifest[$file])) {
|
|
|
|
return baseUrl($manifest[$file]);
|
|
|
|
}
|
2015-12-16 18:09:44 +01:00
|
|
|
|
2016-08-27 12:27:23 +02:00
|
|
|
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
|
|
|
|
2016-08-13 18:56:20 +02:00
|
|
|
/**
|
|
|
|
* Helper to create url's relative to the applications root path.
|
2016-08-21 15:49:40 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param bool $forceAppDomain
|
2016-08-13 18:56:20 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-21 15:49:40 +02:00
|
|
|
function baseUrl($path, $forceAppDomain = false)
|
2016-08-13 18:56:20 +02:00
|
|
|
{
|
2016-08-21 15:49:40 +02:00
|
|
|
$isFullUrl = strpos($path, 'http') === 0;
|
|
|
|
if ($isFullUrl && !$forceAppDomain) return $path;
|
2016-08-13 18:56:20 +02:00
|
|
|
$path = trim($path, '/');
|
2016-08-21 15:49:40 +02:00
|
|
|
|
|
|
|
if ($isFullUrl && $forceAppDomain) {
|
|
|
|
$explodedPath = explode('/', $path);
|
|
|
|
$path = implode('/', array_splice($explodedPath, 3));
|
|
|
|
}
|
|
|
|
|
2016-08-13 18:56:20 +02:00
|
|
|
return rtrim(config('app.url'), '/') . '/' . $path;
|
|
|
|
}
|
|
|
|
|
2016-08-14 13:29:35 +02: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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-08-14 13:29:35 +02:00
|
|
|
return baseUrl($path . '?' . implode('&', $queryStringSections));
|
2016-05-22 11:44:31 +02:00
|
|
|
}
|