init
This commit is contained in:
324
kirby/src/Panel/Assets.php
Normal file
324
kirby/src/Panel/Assets.php
Normal file
@@ -0,0 +1,324 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Url;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Filesystem\Asset;
|
||||
use Kirby\Filesystem\Dir;
|
||||
use Kirby\Filesystem\F;
|
||||
use Kirby\Toolkit\A;
|
||||
|
||||
/**
|
||||
* The Assets class collects all js, css, icons and other
|
||||
* files for the Panel. It pushes them into the media folder
|
||||
* on demand and also makes sure to create proper asset URLs
|
||||
* depending on dev mode
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class Assets
|
||||
{
|
||||
protected bool $dev;
|
||||
protected App $kirby;
|
||||
protected string $nonce;
|
||||
protected Plugins $plugins;
|
||||
protected string $url;
|
||||
protected bool $vite;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->kirby = App::instance();
|
||||
$this->nonce = $this->kirby->nonce();
|
||||
$this->plugins = new Plugins();
|
||||
|
||||
$vite = $this->kirby->roots()->panel() . '/.vite-running';
|
||||
$this->vite = is_file($vite) === true;
|
||||
|
||||
// get the assets from the Vite dev server in dev mode;
|
||||
// dev mode = explicitly enabled in the config AND Vite is running
|
||||
$dev = $this->kirby->option('panel.dev', false);
|
||||
$this->dev = $dev !== false && $this->vite === true;
|
||||
|
||||
// get the base URL
|
||||
$this->url = $this->url();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all CSS files
|
||||
*/
|
||||
public function css(): array
|
||||
{
|
||||
$css = [
|
||||
'index' => $this->url . '/css/style.min.css',
|
||||
'plugins' => $this->plugins->url('css'),
|
||||
...$this->custom('panel.css')
|
||||
];
|
||||
|
||||
// during dev mode we do not need to load
|
||||
// the general stylesheet (as styling will be inlined)
|
||||
if ($this->dev === true) {
|
||||
$css['index'] = null;
|
||||
}
|
||||
|
||||
return array_filter($css);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for a custom asset file from the
|
||||
* config (e.g. panel.css or panel.js)
|
||||
*/
|
||||
public function custom(string $option): array
|
||||
{
|
||||
$customs = [];
|
||||
|
||||
if ($assets = $this->kirby->option($option)) {
|
||||
$assets = A::wrap($assets);
|
||||
|
||||
foreach ($assets as $index => $path) {
|
||||
if (Url::isAbsolute($path) === true) {
|
||||
$customs['custom-' . $index] = $path;
|
||||
continue;
|
||||
}
|
||||
|
||||
$asset = new Asset($path);
|
||||
|
||||
if ($asset->exists() === true) {
|
||||
$customs['custom-' . $index] = $asset->url() . '?' . $asset->modified();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $customs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array with all assets
|
||||
* that need to be loaded for the panel (js, css, icons)
|
||||
*/
|
||||
public function external(): array
|
||||
{
|
||||
return [
|
||||
'css' => $this->css(),
|
||||
'icons' => $this->favicons(),
|
||||
// loader for plugins' index.dev.mjs files – inlined,
|
||||
// so we provide the code instead of the asset URL
|
||||
'plugin-imports' => $this->plugins->read('mjs'),
|
||||
'js' => $this->js()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of favicon icons
|
||||
* based on config option
|
||||
*
|
||||
* @todo Deprecate `url` option in v5, use `href` option instead
|
||||
* @todo Deprecate `rel` usage as array key in v5, use `rel` option instead
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function favicons(): array
|
||||
{
|
||||
$icons = $this->kirby->option('panel.favicon', [
|
||||
[
|
||||
'rel' => 'apple-touch-icon',
|
||||
'type' => 'image/png',
|
||||
'href' => $this->url . '/apple-touch-icon.png'
|
||||
],
|
||||
[
|
||||
'rel' => 'alternate icon',
|
||||
'type' => 'image/png',
|
||||
'href' => $this->url . '/favicon.png'
|
||||
],
|
||||
[
|
||||
'rel' => 'shortcut icon',
|
||||
'type' => 'image/svg+xml',
|
||||
'href' => $this->url . '/favicon.svg'
|
||||
],
|
||||
[
|
||||
'rel' => 'apple-touch-icon',
|
||||
'type' => 'image/png',
|
||||
'href' => $this->url . '/apple-touch-icon-dark.png',
|
||||
'media' => '(prefers-color-scheme: dark)'
|
||||
],
|
||||
[
|
||||
'rel' => 'alternate icon',
|
||||
'type' => 'image/png',
|
||||
'href' => $this->url . '/favicon-dark.png',
|
||||
'media' => '(prefers-color-scheme: dark)'
|
||||
]
|
||||
]);
|
||||
|
||||
if (is_array($icons) === true) {
|
||||
// normalize options
|
||||
foreach ($icons as $rel => &$icon) {
|
||||
// TODO: remove this backward compatibility check in v6
|
||||
if (isset($icon['url']) === true) {
|
||||
$icon['href'] = $icon['url'];
|
||||
unset($icon['url']);
|
||||
}
|
||||
|
||||
// TODO: remove this backward compatibility check in v6
|
||||
if (is_string($rel) === true && isset($icon['rel']) === false) {
|
||||
$icon['rel'] = $rel;
|
||||
}
|
||||
|
||||
$icon['href'] = Url::to($icon['href']);
|
||||
$icon['nonce'] = $this->nonce;
|
||||
}
|
||||
|
||||
return array_values($icons);
|
||||
}
|
||||
|
||||
// make sure to convert favicon string to array
|
||||
if (is_string($icons) === true) {
|
||||
return [
|
||||
[
|
||||
'rel' => 'shortcut icon',
|
||||
'type' => F::mime($icons),
|
||||
'href' => Url::to($icons),
|
||||
'nonce' => $this->nonce
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Invalid panel.favicon option');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the SVG icon sprite
|
||||
* This will be injected in the
|
||||
* initial HTML document for the Panel
|
||||
*/
|
||||
public function icons(): string
|
||||
{
|
||||
$dir = $this->kirby->root('panel') . '/';
|
||||
$dir .= $this->dev ? 'public' : 'dist';
|
||||
$icons = F::read($dir . '/img/icons.svg');
|
||||
$icons = preg_replace('/<!--(.|\s)*?-->/', '', $icons);
|
||||
return $icons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all js files
|
||||
*/
|
||||
public function js(): array
|
||||
{
|
||||
$js = [
|
||||
'vue' => [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $this->url . '/js/vue.min.js'
|
||||
],
|
||||
'vendor' => [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $this->url . '/js/vendor.min.js',
|
||||
'type' => 'module'
|
||||
],
|
||||
'pluginloader' => [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $this->url . '/js/plugins.js',
|
||||
'type' => 'module'
|
||||
],
|
||||
'plugins' => [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $this->plugins->url('js'),
|
||||
'defer' => true
|
||||
],
|
||||
...A::map($this->custom('panel.js'), fn ($src) => [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $src,
|
||||
'type' => 'module'
|
||||
]),
|
||||
'index' => [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $this->url . '/js/index.min.js',
|
||||
'type' => 'module'
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
// during dev mode, add vite client and adapt
|
||||
// path to `index.js` - vendor does not need
|
||||
// to be loaded in dev mode
|
||||
if ($this->dev === true) {
|
||||
// load the non-minified index.js, remove vendor script and
|
||||
// development version of Vue
|
||||
$js['vendor']['src'] = null;
|
||||
$js['index']['src'] = $this->url . '/src/index.js';
|
||||
$js['vue']['src'] = $this->url . '/node_modules/vue/dist/vue.js';
|
||||
|
||||
// add vite dev client
|
||||
$js['vite'] = [
|
||||
'nonce' => $this->nonce,
|
||||
'src' => $this->url . '/@vite/client',
|
||||
'type' => 'module'
|
||||
];
|
||||
}
|
||||
|
||||
return array_filter($js, fn ($js) => empty($js['src']) === false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Links all dist files in the media folder
|
||||
* and returns the link to the requested asset
|
||||
*
|
||||
* @throws \Kirby\Exception\Exception If Panel assets could not be moved to the public directory
|
||||
*/
|
||||
public function link(): bool
|
||||
{
|
||||
$mediaRoot = $this->kirby->root('media') . '/panel';
|
||||
$panelRoot = $this->kirby->root('panel') . '/dist';
|
||||
$versionHash = $this->kirby->versionHash();
|
||||
$versionRoot = $mediaRoot . '/' . $versionHash;
|
||||
|
||||
// check if the version already exists
|
||||
if (is_dir($versionRoot) === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// delete the panel folder and all previous versions
|
||||
Dir::remove($mediaRoot);
|
||||
|
||||
// recreate the panel folder
|
||||
Dir::make($mediaRoot, true);
|
||||
|
||||
// copy assets to the dist folder
|
||||
if (Dir::copy($panelRoot, $versionRoot) !== true) {
|
||||
throw new Exception('Panel assets could not be linked');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the base URL for all assets depending on dev mode
|
||||
*/
|
||||
public function url(): string
|
||||
{
|
||||
// vite is not running, use production assets
|
||||
if ($this->dev === false) {
|
||||
return $this->kirby->url('media') . '/panel/' . $this->kirby->versionHash();
|
||||
}
|
||||
|
||||
// explicitly configured base URL
|
||||
$dev = $this->kirby->option('panel.dev');
|
||||
if (is_string($dev) === true) {
|
||||
return $dev;
|
||||
}
|
||||
|
||||
// port 3000 of the current Kirby request
|
||||
return rtrim($this->kirby->request()->url([
|
||||
'port' => 3000,
|
||||
'path' => null,
|
||||
'params' => null,
|
||||
'query' => null
|
||||
])->toString(), '/');
|
||||
}
|
||||
}
|
||||
71
kirby/src/Panel/ChangesDialog.php
Normal file
71
kirby/src/Panel/ChangesDialog.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Find;
|
||||
use Kirby\Http\Uri;
|
||||
use Kirby\Toolkit\Escape;
|
||||
use Throwable;
|
||||
|
||||
class ChangesDialog
|
||||
{
|
||||
public function changes(array $ids = []): array
|
||||
{
|
||||
$kirby = App::instance();
|
||||
$multilang = $kirby->multilang();
|
||||
$changes = [];
|
||||
|
||||
foreach ($ids as $id) {
|
||||
try {
|
||||
// parse the given ID to extract
|
||||
// the path and an optional query
|
||||
$uri = new Uri($id);
|
||||
$path = $uri->path()->toString();
|
||||
$query = $uri->query();
|
||||
$model = Find::parent($path);
|
||||
$item = $model->panel()->dropdownOption();
|
||||
|
||||
// add the language to each option, if it is included in the query
|
||||
// of the given ID and the language actually exists
|
||||
if (
|
||||
$multilang &&
|
||||
$query->language &&
|
||||
$language = $kirby->language($query->language)
|
||||
) {
|
||||
$item['text'] .= ' (' . $language->code() . ')';
|
||||
$item['link'] .= '?language=' . $language->code();
|
||||
}
|
||||
|
||||
$item['text'] = Escape::html($item['text']);
|
||||
|
||||
$changes[] = $item;
|
||||
} catch (Throwable) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $changes;
|
||||
}
|
||||
|
||||
public function load(): array
|
||||
{
|
||||
return $this->state();
|
||||
}
|
||||
|
||||
public function state(bool $loading = true, array $changes = [])
|
||||
{
|
||||
return [
|
||||
'component' => 'k-changes-dialog',
|
||||
'props' => [
|
||||
'changes' => $changes,
|
||||
'loading' => $loading
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(array $ids): array
|
||||
{
|
||||
return $this->state(false, $this->changes($ids));
|
||||
}
|
||||
}
|
||||
72
kirby/src/Panel/Dialog.php
Normal file
72
kirby/src/Panel/Dialog.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Http\Response;
|
||||
|
||||
/**
|
||||
* The Dialog response class handles Fiber
|
||||
* requests to render the JSON object for
|
||||
* Panel dialogs and creates the routes
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Dialog extends Json
|
||||
{
|
||||
protected static string $key = '$dialog';
|
||||
|
||||
/**
|
||||
* Renders dialogs
|
||||
*/
|
||||
public static function response($data, array $options = []): Response
|
||||
{
|
||||
// interpret true as success
|
||||
if ($data === true) {
|
||||
$data = [
|
||||
'code' => 200
|
||||
];
|
||||
}
|
||||
|
||||
return parent::response($data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the routes for a dialog
|
||||
*/
|
||||
public static function routes(
|
||||
string $id,
|
||||
string $areaId,
|
||||
string $prefix = '',
|
||||
array $options = []
|
||||
) {
|
||||
$routes = [];
|
||||
|
||||
// create the full pattern with dialogs prefix
|
||||
$pattern = trim($prefix . '/' . ($options['pattern'] ?? $id), '/');
|
||||
$type = str_replace('$', '', static::$key);
|
||||
|
||||
// load event
|
||||
$routes[] = [
|
||||
'pattern' => $pattern,
|
||||
'type' => $type,
|
||||
'area' => $areaId,
|
||||
'action' => $options['load'] ?? fn () => 'The load handler is missing'
|
||||
];
|
||||
|
||||
// submit event
|
||||
$routes[] = [
|
||||
'pattern' => $pattern,
|
||||
'type' => $type,
|
||||
'area' => $areaId,
|
||||
'method' => 'POST',
|
||||
'action' => $options['submit'] ?? fn () => 'The submit handler is missing'
|
||||
];
|
||||
|
||||
return $routes;
|
||||
}
|
||||
}
|
||||
72
kirby/src/Panel/Document.php
Normal file
72
kirby/src/Panel/Document.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Http\Response;
|
||||
use Kirby\Http\Uri;
|
||||
use Kirby\Toolkit\Tpl;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The Document is used by the View class to render
|
||||
* the full Panel HTML document in Fiber calls that
|
||||
* should not return just JSON objects
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Document
|
||||
{
|
||||
/**
|
||||
* Renders the panel document
|
||||
*/
|
||||
public static function response(array $fiber): Response
|
||||
{
|
||||
$kirby = App::instance();
|
||||
$assets = new Assets();
|
||||
|
||||
// Full HTML response
|
||||
// @codeCoverageIgnoreStart
|
||||
try {
|
||||
if ($assets->link() === true) {
|
||||
usleep(1);
|
||||
Response::go($kirby->url('base') . '/' . $kirby->path());
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
die('The Panel assets cannot be installed properly. ' . $e->getMessage());
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
// get the uri object for the panel url
|
||||
$uri = new Uri($kirby->url('panel'));
|
||||
|
||||
// proper response code
|
||||
$code = $fiber['$view']['code'] ?? 200;
|
||||
|
||||
// load the main Panel view template
|
||||
$body = Tpl::load($kirby->root('kirby') . '/views/panel.php', [
|
||||
'assets' => $assets->external(),
|
||||
'icons' => $assets->icons(),
|
||||
'nonce' => $kirby->nonce(),
|
||||
'fiber' => $fiber,
|
||||
'panelUrl' => $uri->path()->toString(true) . '/',
|
||||
]);
|
||||
|
||||
$frameAncestors = $kirby->option('panel.frameAncestors');
|
||||
$frameAncestors = match (true) {
|
||||
$frameAncestors === true => "'self'",
|
||||
is_array($frameAncestors) => "'self' " . implode(' ', $frameAncestors),
|
||||
is_string($frameAncestors) => $frameAncestors,
|
||||
default => "'none'"
|
||||
};
|
||||
|
||||
return new Response($body, 'text/html', $code, [
|
||||
'Content-Security-Policy' => 'frame-ancestors ' . $frameAncestors
|
||||
]);
|
||||
}
|
||||
}
|
||||
21
kirby/src/Panel/Drawer.php
Normal file
21
kirby/src/Panel/Drawer.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Http\Response;
|
||||
|
||||
/**
|
||||
* The Drawer response class handles Fiber
|
||||
* requests to render the JSON object for
|
||||
* Panel drawers
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Drawer extends Dialog
|
||||
{
|
||||
protected static string $key = '$drawer';
|
||||
}
|
||||
71
kirby/src/Panel/Dropdown.php
Normal file
71
kirby/src/Panel/Dropdown.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Closure;
|
||||
use Kirby\Http\Response;
|
||||
|
||||
/**
|
||||
* The Dropdown response class handles Fiber
|
||||
* requests to render the JSON object for
|
||||
* dropdown menus
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Dropdown extends Json
|
||||
{
|
||||
protected static string $key = '$dropdown';
|
||||
|
||||
/**
|
||||
* Renders dropdowns
|
||||
*/
|
||||
public static function response($data, array $options = []): Response
|
||||
{
|
||||
if (is_array($data) === true) {
|
||||
$data = [
|
||||
'options' => array_values($data)
|
||||
];
|
||||
}
|
||||
|
||||
return parent::response($data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes for the dropdown
|
||||
*/
|
||||
public static function routes(
|
||||
string $id,
|
||||
string $areaId,
|
||||
string $prefix = '',
|
||||
Closure|array $options = []
|
||||
): array {
|
||||
// Handle shortcuts for dropdowns. The name is the pattern
|
||||
// and options are defined in a Closure
|
||||
if ($options instanceof Closure) {
|
||||
$options = [
|
||||
'pattern' => $id,
|
||||
'action' => $options
|
||||
];
|
||||
}
|
||||
|
||||
// create the full pattern with dialogs prefix
|
||||
$pattern = trim($prefix . '/' . ($options['pattern'] ?? $id), '/');
|
||||
$type = str_replace('$', '', static::$key);
|
||||
|
||||
return [
|
||||
// load event
|
||||
[
|
||||
'pattern' => $pattern,
|
||||
'type' => $type,
|
||||
'area' => $areaId,
|
||||
'method' => 'GET|POST',
|
||||
'action' => $options['options'] ?? $options['action']
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
292
kirby/src/Panel/Field.php
Normal file
292
kirby/src/Panel/Field.php
Normal file
@@ -0,0 +1,292 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\File;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Cms\Page;
|
||||
use Kirby\Form\Form;
|
||||
use Kirby\Http\Router;
|
||||
use Kirby\Toolkit\I18n;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Provides common field prop definitions
|
||||
* for dialogs and other places
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Field
|
||||
{
|
||||
/**
|
||||
* Creates the routes for a field dialog
|
||||
* This is most definitely not a good place for this
|
||||
* method, but as long as the other classes are
|
||||
* not fully refactored, it still feels appropriate
|
||||
*/
|
||||
public static function dialog(
|
||||
ModelWithContent $model,
|
||||
string $fieldName,
|
||||
string|null $path = null,
|
||||
string $method = 'GET',
|
||||
) {
|
||||
$field = Form::for($model)->field($fieldName);
|
||||
$routes = [];
|
||||
|
||||
foreach ($field->dialogs() as $dialogId => $dialog) {
|
||||
$routes = array_merge($routes, Dialog::routes(
|
||||
id: $dialogId,
|
||||
areaId: 'site',
|
||||
options: $dialog
|
||||
));
|
||||
}
|
||||
|
||||
return Router::execute($path, $method, $routes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the routes for a field drawer
|
||||
* This is most definitely not a good place for this
|
||||
* method, but as long as the other classes are
|
||||
* not fully refactored, it still feels appropriate
|
||||
*/
|
||||
public static function drawer(
|
||||
ModelWithContent $model,
|
||||
string $fieldName,
|
||||
string|null $path = null,
|
||||
string $method = 'GET',
|
||||
) {
|
||||
$field = Form::for($model)->field($fieldName);
|
||||
$routes = [];
|
||||
|
||||
foreach ($field->drawers() as $drawerId => $drawer) {
|
||||
$routes = array_merge($routes, Drawer::routes(
|
||||
id: $drawerId,
|
||||
areaId: 'site',
|
||||
options: $drawer
|
||||
));
|
||||
}
|
||||
|
||||
return Router::execute($path, $method, $routes);
|
||||
}
|
||||
|
||||
/**
|
||||
* A standard email field
|
||||
*/
|
||||
public static function email(array $props = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'label' => I18n::translate('email'),
|
||||
'type' => 'email',
|
||||
'counter' => false,
|
||||
], $props);
|
||||
}
|
||||
|
||||
/**
|
||||
* File position
|
||||
*/
|
||||
public static function filePosition(File $file, array $props = []): array
|
||||
{
|
||||
$index = 0;
|
||||
$options = [];
|
||||
|
||||
foreach ($file->siblings(false)->sorted() as $sibling) {
|
||||
$index++;
|
||||
|
||||
$options[] = [
|
||||
'value' => $index,
|
||||
'text' => $index
|
||||
];
|
||||
|
||||
$options[] = [
|
||||
'value' => $sibling->id(),
|
||||
'text' => $sibling->filename(),
|
||||
'disabled' => true
|
||||
];
|
||||
}
|
||||
|
||||
$index++;
|
||||
|
||||
$options[] = [
|
||||
'value' => $index,
|
||||
'text' => $index
|
||||
];
|
||||
|
||||
return array_merge([
|
||||
'label' => I18n::translate('file.sort'),
|
||||
'type' => 'select',
|
||||
'empty' => false,
|
||||
'options' => $options
|
||||
], $props);
|
||||
}
|
||||
|
||||
|
||||
public static function hidden(): array
|
||||
{
|
||||
return ['hidden' => true];
|
||||
}
|
||||
|
||||
/**
|
||||
* Page position
|
||||
*/
|
||||
public static function pagePosition(Page $page, array $props = []): array
|
||||
{
|
||||
$index = 0;
|
||||
$options = [];
|
||||
$siblings = $page->parentModel()->children()->listed()->not($page);
|
||||
|
||||
foreach ($siblings as $sibling) {
|
||||
$index++;
|
||||
|
||||
$options[] = [
|
||||
'value' => $index,
|
||||
'text' => $index
|
||||
];
|
||||
|
||||
$options[] = [
|
||||
'value' => $sibling->id(),
|
||||
'text' => $sibling->title()->value(),
|
||||
'disabled' => true
|
||||
];
|
||||
}
|
||||
|
||||
$index++;
|
||||
|
||||
$options[] = [
|
||||
'value' => $index,
|
||||
'text' => $index
|
||||
];
|
||||
|
||||
// if only one available option,
|
||||
// hide field when not in debug mode
|
||||
if (count($options) < 2) {
|
||||
return static::hidden();
|
||||
}
|
||||
|
||||
return array_merge([
|
||||
'label' => I18n::translate('page.changeStatus.position'),
|
||||
'type' => 'select',
|
||||
'empty' => false,
|
||||
'options' => $options,
|
||||
], $props);
|
||||
}
|
||||
|
||||
/**
|
||||
* A regular password field
|
||||
*/
|
||||
public static function password(array $props = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'label' => I18n::translate('password'),
|
||||
'type' => 'password'
|
||||
], $props);
|
||||
}
|
||||
|
||||
/**
|
||||
* User role radio buttons
|
||||
*/
|
||||
public static function role(array $props = []): array
|
||||
{
|
||||
$kirby = App::instance();
|
||||
$isAdmin = $kirby->user()?->isAdmin() ?? false;
|
||||
$roles = [];
|
||||
|
||||
foreach ($kirby->roles() as $role) {
|
||||
// exclude the admin role, if the user
|
||||
// is not allowed to change role to admin
|
||||
if ($role->name() === 'admin' && $isAdmin === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$roles[] = [
|
||||
'text' => $role->title(),
|
||||
'info' => $role->description() ?? I18n::translate('role.description.placeholder'),
|
||||
'value' => $role->name()
|
||||
];
|
||||
}
|
||||
|
||||
return array_merge([
|
||||
'label' => I18n::translate('role'),
|
||||
'type' => count($roles) <= 1 ? 'hidden' : 'radio',
|
||||
'options' => $roles
|
||||
], $props);
|
||||
}
|
||||
|
||||
public static function slug(array $props = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'label' => I18n::translate('slug'),
|
||||
'type' => 'slug',
|
||||
'allow' => Str::$defaults['slug']['allowed']
|
||||
], $props);
|
||||
}
|
||||
|
||||
public static function template(
|
||||
array|null $blueprints = [],
|
||||
array|null $props = []
|
||||
): array {
|
||||
$options = [];
|
||||
|
||||
foreach ($blueprints as $blueprint) {
|
||||
$options[] = [
|
||||
'text' => $blueprint['title'] ?? $blueprint['text'] ?? null,
|
||||
'value' => $blueprint['name'] ?? $blueprint['value'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
return array_merge([
|
||||
'label' => I18n::translate('template'),
|
||||
'type' => 'select',
|
||||
'empty' => false,
|
||||
'options' => $options,
|
||||
'icon' => 'template',
|
||||
'disabled' => count($options) <= 1
|
||||
], $props);
|
||||
}
|
||||
|
||||
public static function title(array $props = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'label' => I18n::translate('title'),
|
||||
'type' => 'text',
|
||||
'icon' => 'title',
|
||||
], $props);
|
||||
}
|
||||
|
||||
/**
|
||||
* Panel translation select box
|
||||
*/
|
||||
public static function translation(array $props = []): array
|
||||
{
|
||||
$translations = [];
|
||||
foreach (App::instance()->translations() as $translation) {
|
||||
$translations[] = [
|
||||
'text' => $translation->name(),
|
||||
'value' => $translation->code()
|
||||
];
|
||||
}
|
||||
|
||||
return array_merge([
|
||||
'label' => I18n::translate('language'),
|
||||
'type' => 'select',
|
||||
'icon' => 'translate',
|
||||
'options' => $translations,
|
||||
'empty' => false
|
||||
], $props);
|
||||
}
|
||||
|
||||
public static function username(array $props = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'icon' => 'user',
|
||||
'label' => I18n::translate('name'),
|
||||
'type' => 'text',
|
||||
], $props);
|
||||
}
|
||||
}
|
||||
493
kirby/src/Panel/File.php
Normal file
493
kirby/src/Panel/File.php
Normal file
@@ -0,0 +1,493 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\File as CmsFile;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Filesystem\Asset;
|
||||
use Kirby\Toolkit\I18n;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Provides information about the file model for the Panel
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class File extends Model
|
||||
{
|
||||
/**
|
||||
* @var \Kirby\Cms\File
|
||||
*/
|
||||
protected ModelWithContent $model;
|
||||
|
||||
/**
|
||||
* Breadcrumb array
|
||||
*/
|
||||
public function breadcrumb(): array
|
||||
{
|
||||
$breadcrumb = [];
|
||||
$parent = $this->model->parent();
|
||||
|
||||
switch ($parent::CLASS_ALIAS) {
|
||||
case 'user':
|
||||
/** @var \Kirby\Cms\User $parent */
|
||||
// The breadcrumb is not necessary
|
||||
// on the account view
|
||||
if ($parent->isLoggedIn() === false) {
|
||||
$breadcrumb[] = [
|
||||
'label' => $parent->username(),
|
||||
'link' => $parent->panel()->url(true)
|
||||
];
|
||||
}
|
||||
break;
|
||||
case 'page':
|
||||
/** @var \Kirby\Cms\Page $parent */
|
||||
$breadcrumb = $this->model->parents()->flip()->values(
|
||||
fn ($parent) => [
|
||||
'label' => $parent->title()->toString(),
|
||||
'link' => $parent->panel()->url(true),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// add the file
|
||||
$breadcrumb[] = [
|
||||
'label' => $this->model->filename(),
|
||||
'link' => $this->url(true),
|
||||
];
|
||||
|
||||
return $breadcrumb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a kirbytag or markdown
|
||||
* tag for the file, which will be
|
||||
* used in the panel, when the file
|
||||
* gets dragged onto a textarea
|
||||
*
|
||||
* @internal
|
||||
* @param string|null $type (`auto`|`kirbytext`|`markdown`)
|
||||
*/
|
||||
public function dragText(
|
||||
string|null $type = null,
|
||||
bool $absolute = false
|
||||
): string {
|
||||
$type = $this->dragTextType($type);
|
||||
$url = $this->model->filename();
|
||||
$file = $this->model->type();
|
||||
|
||||
// By default only the filename is added as relative URL.
|
||||
// If an absolute URL is required, either use the permalink
|
||||
// for markdown notation or the UUID for Kirbytext (since
|
||||
// Kirbytags support can resolve UUIDs directly)
|
||||
if ($absolute === true) {
|
||||
$url = match ($type) {
|
||||
'markdown' => $this->model->permalink(),
|
||||
default => $this->model->uuid()
|
||||
};
|
||||
|
||||
// if UUIDs are disabled, fall back to URL
|
||||
$url ??= $this->model->url();
|
||||
}
|
||||
|
||||
if ($callback = $this->dragTextFromCallback($type, $url)) {
|
||||
return $callback;
|
||||
}
|
||||
|
||||
if ($type === 'markdown') {
|
||||
return match ($file) {
|
||||
'image' => '',
|
||||
default => '[' . $this->model->filename() . '](' . $url . ')'
|
||||
};
|
||||
}
|
||||
|
||||
return match ($file) {
|
||||
'image', 'video' => '(' . $file . ': ' . $url . ')',
|
||||
default => '(file: ' . $url . ')'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides options for the file dropdown
|
||||
*/
|
||||
public function dropdown(array $options = []): array
|
||||
{
|
||||
$file = $this->model;
|
||||
$request = $file->kirby()->request();
|
||||
$defaults = $request->get(['view', 'update', 'delete']);
|
||||
$options = array_merge($defaults, $options);
|
||||
|
||||
$permissions = $this->options(['preview']);
|
||||
$view = $options['view'] ?? 'view';
|
||||
$url = $this->url(true);
|
||||
$result = [];
|
||||
|
||||
if ($view === 'list') {
|
||||
$result[] = [
|
||||
'link' => $file->previewUrl(),
|
||||
'target' => '_blank',
|
||||
'icon' => 'open',
|
||||
'text' => I18n::translate('open')
|
||||
];
|
||||
$result[] = '-';
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeName',
|
||||
'icon' => 'title',
|
||||
'text' => I18n::translate('rename'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeName', $options, $permissions)
|
||||
];
|
||||
|
||||
if ($view === 'list') {
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeSort',
|
||||
'icon' => 'sort',
|
||||
'text' => I18n::translate('file.sort'),
|
||||
'disabled' => $this->isDisabledDropdownOption('update', $options, $permissions)
|
||||
];
|
||||
}
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeTemplate',
|
||||
'icon' => 'template',
|
||||
'text' => I18n::translate('file.changeTemplate'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeTemplate', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = '-';
|
||||
|
||||
$result[] = [
|
||||
'click' => 'replace',
|
||||
'icon' => 'upload',
|
||||
'text' => I18n::translate('replace'),
|
||||
'disabled' => $this->isDisabledDropdownOption('replace', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = '-';
|
||||
$result[] = [
|
||||
'dialog' => $url . '/delete',
|
||||
'icon' => 'trash',
|
||||
'text' => I18n::translate('delete'),
|
||||
'disabled' => $this->isDisabledDropdownOption('delete', $options, $permissions)
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setup for a dropdown option
|
||||
* which is used in the changes dropdown
|
||||
* for example
|
||||
*/
|
||||
public function dropdownOption(): array
|
||||
{
|
||||
return [
|
||||
'icon' => 'image',
|
||||
'text' => $this->model->filename(),
|
||||
] + parent::dropdownOption();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Panel icon color
|
||||
*/
|
||||
protected function imageColor(): string
|
||||
{
|
||||
$types = [
|
||||
'archive' => 'gray-500',
|
||||
'audio' => 'aqua-500',
|
||||
'code' => 'pink-500',
|
||||
'document' => 'red-500',
|
||||
'image' => 'orange-500',
|
||||
'video' => 'yellow-500',
|
||||
];
|
||||
|
||||
$extensions = [
|
||||
'csv' => 'green-500',
|
||||
'doc' => 'blue-500',
|
||||
'docx' => 'blue-500',
|
||||
'indd' => 'purple-500',
|
||||
'rtf' => 'blue-500',
|
||||
'xls' => 'green-500',
|
||||
'xlsx' => 'green-500',
|
||||
];
|
||||
|
||||
return
|
||||
$extensions[$this->model->extension()] ??
|
||||
$types[$this->model->type()] ??
|
||||
parent::imageDefaults()['color'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Default settings for the file's Panel image
|
||||
*/
|
||||
protected function imageDefaults(): array
|
||||
{
|
||||
return array_merge(parent::imageDefaults(), [
|
||||
'color' => $this->imageColor(),
|
||||
'icon' => $this->imageIcon(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Panel icon type
|
||||
*/
|
||||
protected function imageIcon(): string
|
||||
{
|
||||
$types = [
|
||||
'archive' => 'archive',
|
||||
'audio' => 'audio',
|
||||
'code' => 'code',
|
||||
'document' => 'document',
|
||||
'image' => 'image',
|
||||
'video' => 'video',
|
||||
];
|
||||
|
||||
$extensions = [
|
||||
'csv' => 'table',
|
||||
'doc' => 'pen',
|
||||
'docx' => 'pen',
|
||||
'md' => 'markdown',
|
||||
'mdown' => 'markdown',
|
||||
'rtf' => 'pen',
|
||||
'xls' => 'table',
|
||||
'xlsx' => 'table',
|
||||
];
|
||||
|
||||
return
|
||||
$extensions[$this->model->extension()] ??
|
||||
$types[$this->model->type()] ??
|
||||
'file';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image file object based on provided query
|
||||
* @internal
|
||||
*/
|
||||
protected function imageSource(
|
||||
string|null $query = null
|
||||
): CmsFile|Asset|null {
|
||||
if ($query === null && $this->model->isViewable()) {
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
return parent::imageSource($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether focus can be added in Panel view
|
||||
*/
|
||||
public function isFocusable(): bool
|
||||
{
|
||||
// blueprint option
|
||||
$option = $this->model->blueprint()->focus();
|
||||
// fallback to whether the file is viewable
|
||||
// (images should be focusable by default, others not)
|
||||
$option ??= $this->model->isViewable();
|
||||
|
||||
if ($option === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ensure that user can update content file
|
||||
if ($this->options()['update'] === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$kirby = $this->model->kirby();
|
||||
|
||||
// ensure focus is only added when editing primary/only language
|
||||
if (
|
||||
$kirby->multilang() === false ||
|
||||
$kirby->languages()->count() === 0 ||
|
||||
$kirby->language()->isDefault() === true
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all actions
|
||||
* that can be performed in the Panel
|
||||
*
|
||||
* @param array $unlock An array of options that will be force-unlocked
|
||||
*/
|
||||
public function options(array $unlock = []): array
|
||||
{
|
||||
$options = parent::options($unlock);
|
||||
|
||||
try {
|
||||
// check if the file type is allowed at all,
|
||||
// otherwise it cannot be replaced
|
||||
$this->model->match($this->model->blueprint()->accept());
|
||||
} catch (Throwable) {
|
||||
$options['replace'] = false;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path without leading slash
|
||||
*/
|
||||
public function path(): string
|
||||
{
|
||||
return 'files/' . $this->model->filename();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the response data for file pickers
|
||||
* and file fields
|
||||
*/
|
||||
public function pickerData(array $params = []): array
|
||||
{
|
||||
$name = $this->model->filename();
|
||||
$id = $this->model->id();
|
||||
|
||||
if (empty($params['model']) === false) {
|
||||
$parent = $this->model->parent();
|
||||
|
||||
// if the file belongs to the current parent model,
|
||||
// store only name as ID to keep its path relative to the model
|
||||
$id = $parent === $params['model'] ? $name : $id;
|
||||
$absolute = $parent !== $params['model'];
|
||||
}
|
||||
|
||||
$params['text'] ??= '{{ file.filename }}';
|
||||
|
||||
return array_merge(parent::pickerData($params), [
|
||||
'dragText' => $this->dragText('auto', $absolute ?? false),
|
||||
'filename' => $name,
|
||||
'id' => $id,
|
||||
'type' => $this->model->type(),
|
||||
'url' => $this->model->url()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for the
|
||||
* view's component props
|
||||
* @internal
|
||||
*/
|
||||
public function props(): array
|
||||
{
|
||||
$file = $this->model;
|
||||
$dimensions = $file->dimensions();
|
||||
|
||||
return array_merge(
|
||||
parent::props(),
|
||||
$this->prevNext(),
|
||||
[
|
||||
'blueprint' => $this->model->template() ?? 'default',
|
||||
'model' => [
|
||||
'content' => $this->content(),
|
||||
'dimensions' => $dimensions->toArray(),
|
||||
'extension' => $file->extension(),
|
||||
'filename' => $file->filename(),
|
||||
'link' => $this->url(true),
|
||||
'mime' => $file->mime(),
|
||||
'niceSize' => $file->niceSize(),
|
||||
'id' => $id = $file->id(),
|
||||
'parent' => $file->parent()->panel()->path(),
|
||||
'template' => $file->template(),
|
||||
'type' => $file->type(),
|
||||
'url' => $file->url(),
|
||||
],
|
||||
'preview' => [
|
||||
'focusable' => $this->isFocusable(),
|
||||
'image' => $this->image([
|
||||
'back' => 'transparent',
|
||||
'ratio' => '1/1'
|
||||
], 'cards'),
|
||||
'url' => $url = $file->previewUrl(),
|
||||
'details' => [
|
||||
[
|
||||
'title' => I18n::translate('template'),
|
||||
'text' => $file->template() ?? '—'
|
||||
],
|
||||
[
|
||||
'title' => I18n::translate('mime'),
|
||||
'text' => $file->mime()
|
||||
],
|
||||
[
|
||||
'title' => I18n::translate('url'),
|
||||
'text' => $id,
|
||||
'link' => $url
|
||||
],
|
||||
[
|
||||
'title' => I18n::translate('size'),
|
||||
'text' => $file->niceSize()
|
||||
],
|
||||
[
|
||||
'title' => I18n::translate('dimensions'),
|
||||
'text' => $file->type() === 'image' ? $file->dimensions() . ' ' . I18n::translate('pixel') : '—'
|
||||
],
|
||||
[
|
||||
'title' => I18n::translate('orientation'),
|
||||
'text' => $file->type() === 'image' ? I18n::translate('orientation.' . $dimensions->orientation()) : '—'
|
||||
],
|
||||
]
|
||||
]
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns navigation array with
|
||||
* previous and next file
|
||||
* @internal
|
||||
*/
|
||||
public function prevNext(): array
|
||||
{
|
||||
$file = $this->model;
|
||||
$siblings = $file->templateSiblings()->sortBy(
|
||||
'sort',
|
||||
'asc',
|
||||
'filename',
|
||||
'asc'
|
||||
);
|
||||
|
||||
return [
|
||||
'next' => function () use ($file, $siblings): array|null {
|
||||
$next = $siblings->nth($siblings->indexOf($file) + 1);
|
||||
return $this->toPrevNextLink($next, 'filename');
|
||||
},
|
||||
'prev' => function () use ($file, $siblings): array|null {
|
||||
$prev = $siblings->nth($siblings->indexOf($file) - 1);
|
||||
return $this->toPrevNextLink($prev, 'filename');
|
||||
}
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Returns the url to the editing view
|
||||
* in the panel
|
||||
*/
|
||||
public function url(bool $relative = false): string
|
||||
{
|
||||
$parent = $this->model->parent()->panel()->url($relative);
|
||||
return $parent . '/' . $this->path();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for
|
||||
* this model's Panel view
|
||||
* @internal
|
||||
*/
|
||||
public function view(): array
|
||||
{
|
||||
return [
|
||||
'breadcrumb' => fn (): array => $this->model->panel()->breadcrumb(),
|
||||
'component' => 'k-file-view',
|
||||
'props' => $this->props(),
|
||||
'search' => 'files',
|
||||
'title' => $this->model->filename(),
|
||||
];
|
||||
}
|
||||
}
|
||||
255
kirby/src/Panel/Home.php
Normal file
255
kirby/src/Panel/Home.php
Normal file
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\User;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Exception\NotFoundException;
|
||||
use Kirby\Http\Router;
|
||||
use Kirby\Http\Uri;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The Home class creates the secure redirect
|
||||
* URL after logins. The URL can either come
|
||||
* from the session to remember the last view
|
||||
* before the automatic logout, or from a user
|
||||
* blueprint to redirect to custom views.
|
||||
*
|
||||
* The Home class also makes sure to check access
|
||||
* before a redirect happens and avoids redirects
|
||||
* to inaccessible views.
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Home
|
||||
{
|
||||
/**
|
||||
* Returns an alternative URL if access
|
||||
* to the first choice is blocked.
|
||||
*
|
||||
* It will go through the entire menu and
|
||||
* take the first area which is not disabled
|
||||
* or locked in other ways
|
||||
*/
|
||||
public static function alternative(User $user): string
|
||||
{
|
||||
$permissions = $user->role()->permissions();
|
||||
|
||||
// no access to the panel? The only good alternative is the main url
|
||||
if ($permissions->for('access', 'panel') === false) {
|
||||
return App::instance()->site()->url();
|
||||
}
|
||||
|
||||
// needed to create a proper menu
|
||||
$areas = Panel::areas();
|
||||
$menu = new Menu($areas, $permissions->toArray());
|
||||
$menu = $menu->entries();
|
||||
|
||||
// go through the menu and search for the first
|
||||
// available view we can go to
|
||||
foreach ($menu as $menuItem) {
|
||||
// skip separators
|
||||
if ($menuItem === '-') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// skip disabled items
|
||||
if (($menuItem['disabled'] ?? false) === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// skip buttons that don't open a link
|
||||
// (but e.g. a dialog)
|
||||
if (isset($menuItem['link']) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// skip the logout button
|
||||
if ($menuItem['link'] === 'logout') {
|
||||
continue;
|
||||
}
|
||||
|
||||
return Panel::url($menuItem['link']);
|
||||
}
|
||||
|
||||
throw new NotFoundException('There’s no available Panel page to redirect to');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has access to the given
|
||||
* panel path. This is quite tricky, because we
|
||||
* need to call a trimmed down router to check
|
||||
* for available routes and their firewall status.
|
||||
*/
|
||||
public static function hasAccess(User $user, string $path): bool
|
||||
{
|
||||
$areas = Panel::areas();
|
||||
$routes = Panel::routes($areas);
|
||||
|
||||
// Remove fallback routes. Otherwise a route
|
||||
// would be found even if the view does
|
||||
// not exist at all.
|
||||
foreach ($routes as $index => $route) {
|
||||
if ($route['pattern'] === '(:all)') {
|
||||
unset($routes[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
// create a dummy router to check if we can access this route at all
|
||||
try {
|
||||
return Router::execute($path, 'GET', $routes, function ($route) use ($user) {
|
||||
$attrs = $route->attributes();
|
||||
$auth = $attrs['auth'] ?? true;
|
||||
$areaId = $attrs['area'] ?? null;
|
||||
$type = $attrs['type'] ?? 'view';
|
||||
|
||||
// only allow redirects to views
|
||||
if ($type !== 'view') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if auth is not required the redirect is allowed
|
||||
if ($auth === false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// check the firewall
|
||||
return Panel::hasAccess($user, $areaId);
|
||||
});
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given Uri has the same domain
|
||||
* as the index URL of the Kirby installation.
|
||||
* This is used to block external URLs to third-party
|
||||
* domains as redirect options.
|
||||
*/
|
||||
public static function hasValidDomain(Uri $uri): bool
|
||||
{
|
||||
$rootUrl = App::instance()->site()->url();
|
||||
$rootUri = new Uri($rootUrl);
|
||||
return $uri->domain() === $rootUri->domain();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given URL is a Panel Url
|
||||
*/
|
||||
public static function isPanelUrl(string $url): bool
|
||||
{
|
||||
$panel = App::instance()->url('panel');
|
||||
return Str::startsWith($url, $panel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path after /panel/ which can then
|
||||
* be used in the router or to find a matching view
|
||||
*/
|
||||
public static function panelPath(string $url): string|null
|
||||
{
|
||||
$after = Str::after($url, App::instance()->url('panel'));
|
||||
return trim($after, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Url that has been stored in the session
|
||||
* before the last logout. We take this Url if possible
|
||||
* to redirect the user back to the last point where they
|
||||
* left before they got logged out.
|
||||
*/
|
||||
public static function remembered(): string|null
|
||||
{
|
||||
// check for a stored path after login
|
||||
if ($remembered = App::instance()->session()->pull('panel.path')) {
|
||||
// convert the result to an absolute URL if available
|
||||
return Panel::url($remembered);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to find the best possible Url to redirect
|
||||
* the user to after the login.
|
||||
*
|
||||
* When the user got logged out, we try to send them back
|
||||
* to the point where they left.
|
||||
*
|
||||
* If they have a custom redirect Url defined in their blueprint
|
||||
* via the `home` option, we send them there if no Url is stored
|
||||
* in the session.
|
||||
*
|
||||
* If none of the options above find any result, we try to send
|
||||
* them to the site view.
|
||||
*
|
||||
* Before the redirect happens, the final Url is sanitized, the query
|
||||
* and params are removed to avoid any attacks and the domain is compared
|
||||
* to avoid redirects to external Urls.
|
||||
*
|
||||
* Afterwards, we also check for permissions before the redirect happens
|
||||
* to avoid redirects to inaccessible Panel views. In such a case
|
||||
* the next best accessible view is picked from the menu.
|
||||
*/
|
||||
public static function url(): string
|
||||
{
|
||||
$user = App::instance()->user();
|
||||
|
||||
// if there's no authenticated user, all internal
|
||||
// redirects will be blocked and the user is redirected
|
||||
// to the login instead
|
||||
if (!$user) {
|
||||
return Panel::url('login');
|
||||
}
|
||||
|
||||
// get the last visited url from the session or the custom home
|
||||
$url = static::remembered() ?? $user->panel()->home();
|
||||
|
||||
// inspect the given URL
|
||||
$uri = new Uri($url);
|
||||
|
||||
// compare domains to avoid external redirects
|
||||
if (static::hasValidDomain($uri) !== true) {
|
||||
throw new InvalidArgumentException('External URLs are not allowed for Panel redirects');
|
||||
}
|
||||
|
||||
// remove all params to avoid
|
||||
// possible attack vectors
|
||||
$uri->params = '';
|
||||
$uri->query = '';
|
||||
|
||||
// get a clean version of the URL
|
||||
$url = $uri->toString();
|
||||
|
||||
// Don't further inspect URLs outside of the Panel
|
||||
if (static::isPanelUrl($url) === false) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
// get the plain panel path
|
||||
$path = static::panelPath($url);
|
||||
|
||||
// a redirect to login, logout or installation
|
||||
// views would lead to an infinite redirect loop
|
||||
if (in_array($path, ['', 'login', 'logout', 'installation'], true) === true) {
|
||||
$path = 'site';
|
||||
}
|
||||
|
||||
// Check if the user can access the URL
|
||||
if (static::hasAccess($user, $path) === true) {
|
||||
return Panel::url($path);
|
||||
}
|
||||
|
||||
// Try to find an alternative
|
||||
return static::alternative($user);
|
||||
}
|
||||
}
|
||||
84
kirby/src/Panel/Json.php
Normal file
84
kirby/src/Panel/Json.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Http\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The Json abstract response class provides
|
||||
* common framework for Fiber requests
|
||||
* to render the JSON object for, e.g.
|
||||
* Panel dialogs, dropdowns etc.
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
abstract class Json
|
||||
{
|
||||
protected static string $key = '$response';
|
||||
|
||||
/**
|
||||
* Renders the error response with the provided message
|
||||
*/
|
||||
public static function error(string $message, int $code = 404): array
|
||||
{
|
||||
return [
|
||||
'code' => $code,
|
||||
'error' => $message
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the JSON response for the Panel
|
||||
*/
|
||||
public static function response($data, array $options = []): Response
|
||||
{
|
||||
$data = static::responseData($data);
|
||||
|
||||
// always inject the response code
|
||||
$data['code'] ??= 200;
|
||||
$data['path'] = $options['path'] ?? null;
|
||||
$data['query'] = App::instance()->request()->query()->toArray();
|
||||
$data['referrer'] = Panel::referrer();
|
||||
|
||||
return Panel::json([static::$key => $data], $data['code']);
|
||||
}
|
||||
|
||||
public static function responseData(mixed $data): array
|
||||
{
|
||||
// handle redirects
|
||||
if ($data instanceof Redirect) {
|
||||
return [
|
||||
'redirect' => $data->location(),
|
||||
];
|
||||
}
|
||||
|
||||
// handle Kirby exceptions
|
||||
if ($data instanceof Exception) {
|
||||
return static::error($data->getMessage(), $data->getHttpCode());
|
||||
}
|
||||
|
||||
// handle exceptions
|
||||
if ($data instanceof Throwable) {
|
||||
return static::error($data->getMessage(), 500);
|
||||
}
|
||||
|
||||
// only expect arrays from here on
|
||||
if (is_array($data) === false) {
|
||||
return static::error('Invalid response', 500);
|
||||
}
|
||||
|
||||
if (empty($data) === true) {
|
||||
return static::error('The response is empty', 404);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
134
kirby/src/Panel/Lab/Category.php
Normal file
134
kirby/src/Panel/Lab/Category.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel\Lab;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Filesystem\Dir;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Category of lab examples located in
|
||||
* `kirby/panel/lab` and `site/lab`.
|
||||
*
|
||||
* @internal
|
||||
* @since 4.0.0
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Category
|
||||
{
|
||||
protected string $root;
|
||||
|
||||
public function __construct(
|
||||
protected string $id,
|
||||
string|null $root = null,
|
||||
protected array $props = []
|
||||
) {
|
||||
$this->root = $root ?? static::base() . '/' . $this->id;
|
||||
|
||||
if (file_exists($this->root . '/index.php') === true) {
|
||||
$this->props = array_merge(
|
||||
require $this->root . '/index.php',
|
||||
$this->props
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function all(): array
|
||||
{
|
||||
// all core lab examples from `kirby/panel/lab`
|
||||
$examples = A::map(
|
||||
Dir::inventory(static::base())['children'],
|
||||
fn ($props) => (new static($props['dirname']))->toArray()
|
||||
);
|
||||
|
||||
// all custom lab examples from `site/lab`
|
||||
$custom = static::factory('site')->toArray();
|
||||
|
||||
array_push($examples, $custom);
|
||||
|
||||
return $examples;
|
||||
}
|
||||
|
||||
public static function base(): string
|
||||
{
|
||||
return App::instance()->root('panel') . '/lab';
|
||||
}
|
||||
|
||||
public function example(string $id, string|null $tab = null): Example
|
||||
{
|
||||
return new Example(parent: $this, id: $id, tab: $tab);
|
||||
}
|
||||
|
||||
public function examples(): array
|
||||
{
|
||||
return A::map(
|
||||
Dir::inventory($this->root)['children'],
|
||||
fn ($props) => $this->example($props['dirname'])->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public static function factory(string $id)
|
||||
{
|
||||
return match ($id) {
|
||||
'site' => static::site(),
|
||||
default => new static($id)
|
||||
};
|
||||
}
|
||||
|
||||
public function icon(): string
|
||||
{
|
||||
return $this->props['icon'] ?? 'palette';
|
||||
}
|
||||
|
||||
public function id(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public static function installed(): bool
|
||||
{
|
||||
return Dir::exists(static::base()) === true;
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->props['name'] ?? ucfirst($this->id);
|
||||
}
|
||||
|
||||
public function root(): string
|
||||
{
|
||||
return $this->root;
|
||||
}
|
||||
|
||||
public static function site(): static
|
||||
{
|
||||
return new static(
|
||||
'site',
|
||||
App::instance()->root('site') . '/lab',
|
||||
[
|
||||
'name' => 'Your examples',
|
||||
'icon' => 'live'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->name(),
|
||||
'examples' => $this->examples(),
|
||||
'icon' => $this->icon(),
|
||||
'path' => Str::after(
|
||||
$this->root(),
|
||||
App::instance()->root('index')
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
340
kirby/src/Panel/Lab/Docs.php
Normal file
340
kirby/src/Panel/Lab/Docs.php
Normal file
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel\Lab;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Data\Data;
|
||||
use Kirby\Filesystem\Dir;
|
||||
use Kirby\Filesystem\F;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* Docs for a single Vue component
|
||||
*
|
||||
* @internal
|
||||
* @since 4.0.0
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Docs
|
||||
{
|
||||
protected array $json;
|
||||
protected App $kirby;
|
||||
|
||||
public function __construct(
|
||||
protected string $name
|
||||
) {
|
||||
$this->kirby = App::instance();
|
||||
$this->json = $this->read();
|
||||
}
|
||||
|
||||
public static function all(): array
|
||||
{
|
||||
$dist = static::root();
|
||||
$tmp = static::root(true);
|
||||
$files = Dir::inventory($dist)['files'];
|
||||
|
||||
if (Dir::exists($tmp) === true) {
|
||||
$files = [...Dir::inventory($tmp)['files'], ...$files];
|
||||
}
|
||||
|
||||
$docs = A::map(
|
||||
$files,
|
||||
function ($file) {
|
||||
$component = 'k-' . Str::camelToKebab(F::name($file['filename']));
|
||||
|
||||
return [
|
||||
'image' => [
|
||||
'icon' => 'book',
|
||||
'back' => 'white',
|
||||
],
|
||||
'text' => $component,
|
||||
'link' => '/lab/docs/' . $component,
|
||||
];
|
||||
}
|
||||
);
|
||||
|
||||
usort($docs, fn ($a, $b) => $a['text'] <=> $b['text']);
|
||||
|
||||
return array_values($docs);
|
||||
}
|
||||
|
||||
public function deprecated(): string|null
|
||||
{
|
||||
return $this->kt($this->json['tags']['deprecated'][0]['description'] ?? '');
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return $this->kt($this->json['description'] ?? '');
|
||||
}
|
||||
|
||||
public function docBlock(): string
|
||||
{
|
||||
return $this->kt($this->json['docsBlocks'][0] ?? '');
|
||||
}
|
||||
|
||||
public function events(): array
|
||||
{
|
||||
$events = A::map(
|
||||
$this->json['events'] ?? [],
|
||||
fn ($event) => [
|
||||
'name' => $event['name'],
|
||||
'description' => $this->kt($event['description'] ?? ''),
|
||||
'deprecated' => $this->kt($event['tags']['deprecated'][0]['description'] ?? ''),
|
||||
'since' => $event['tags']['since'][0]['description'] ?? null,
|
||||
'properties' => A::map(
|
||||
$event['properties'] ?? [],
|
||||
fn ($property) => [
|
||||
'name' => $property['name'],
|
||||
'type' => $property['type']['names'][0] ?? '',
|
||||
'description' => $this->kt($property['description'] ?? '', true),
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
usort($events, fn ($a, $b) => $a['name'] <=> $b['name']);
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
public function examples(): array
|
||||
{
|
||||
if (empty($this->json['tags']['examples']) === false) {
|
||||
return $this->json['tags']['examples'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function file(string $context): string
|
||||
{
|
||||
$root = match ($context) {
|
||||
'dev' => $this->kirby->root('panel') . '/tmp',
|
||||
'dist' => $this->kirby->root('panel') . '/dist/ui',
|
||||
};
|
||||
|
||||
$name = Str::after($this->name, 'k-');
|
||||
$name = Str::kebabToCamel($name);
|
||||
return $root . '/' . $name . '.json';
|
||||
}
|
||||
|
||||
public function github(): string
|
||||
{
|
||||
return 'https://github.com/getkirby/kirby/tree/main/panel/' . $this->json['sourceFile'];
|
||||
}
|
||||
|
||||
public static function installed(): bool
|
||||
{
|
||||
return Dir::exists(static::root()) === true;
|
||||
}
|
||||
|
||||
protected function kt(string $text, bool $inline = false): string
|
||||
{
|
||||
return $this->kirby->kirbytext($text, [
|
||||
'markdown' => [
|
||||
'breaks' => false,
|
||||
'inline' => $inline,
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function lab(): string|null
|
||||
{
|
||||
$root = $this->kirby->root('panel') . '/lab';
|
||||
|
||||
foreach (glob($root . '/{,*/,*/*/,*/*/*/}index.php', GLOB_BRACE) as $example) {
|
||||
$props = require $example;
|
||||
|
||||
if (($props['docs'] ?? null) === $this->name) {
|
||||
return Str::before(Str::after($example, $root), 'index.php');
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function methods(): array
|
||||
{
|
||||
$methods = A::map(
|
||||
$this->json['methods'] ?? [],
|
||||
fn ($method) => [
|
||||
'name' => $method['name'],
|
||||
'description' => $this->kt($method['description'] ?? ''),
|
||||
'deprecated' => $this->kt($method['tags']['deprecated'][0]['description'] ?? ''),
|
||||
'since' => $method['tags']['since'][0]['description'] ?? null,
|
||||
'params' => A::map(
|
||||
$method['params'] ?? [],
|
||||
fn ($param) => [
|
||||
'name' => $param['name'],
|
||||
'type' => $param['type']['name'] ?? '',
|
||||
'description' => $this->kt($param['description'] ?? '', true),
|
||||
]
|
||||
),
|
||||
'returns' => $method['returns']['type']['name'] ?? null,
|
||||
]
|
||||
);
|
||||
|
||||
usort($methods, fn ($a, $b) => $a['name'] <=> $b['name']);
|
||||
|
||||
return $methods;
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function prop(string|int $key): array|null
|
||||
{
|
||||
$prop = $this->json['props'][$key];
|
||||
|
||||
// filter private props
|
||||
if (($prop['tags']['access'][0]['description'] ?? null) === 'private') {
|
||||
return null;
|
||||
}
|
||||
|
||||
// filter unset props
|
||||
if (($type = $prop['type']['name'] ?? null) === 'null') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$default = $prop['defaultValue']['value'] ?? null;
|
||||
$deprecated = $this->kt($prop['tags']['deprecated'][0]['description'] ?? '');
|
||||
|
||||
return [
|
||||
'name' => Str::camelToKebab($prop['name']),
|
||||
'type' => $type,
|
||||
'description' => $this->kt($prop['description'] ?? ''),
|
||||
'default' => $this->propDefault($default, $type),
|
||||
'deprecated' => $deprecated,
|
||||
'example' => $prop['tags']['example'][0]['description'] ?? null,
|
||||
'required' => $prop['required'] ?? false,
|
||||
'since' => $prop['tags']['since'][0]['description'] ?? null,
|
||||
'value' => $prop['tags']['value'][0]['description'] ?? null,
|
||||
'values' => $prop['values'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
protected function propDefault(
|
||||
string|null $default,
|
||||
string|null $type
|
||||
): string|null {
|
||||
if ($default !== null) {
|
||||
// normalize longform function
|
||||
if (preg_match('/function\(\) {.*return (.*);.*}/si', $default, $matches) === 1) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
// normalize object shorthand function
|
||||
if (preg_match('/\(\) => \((.*)\)/si', $default, $matches) === 1) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
// normalize all other defaults from shorthand function
|
||||
if (preg_match('/\(\) => (.*)/si', $default, $matches) === 1) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
// if type is boolean primarily and no default
|
||||
// value has been set, add `false` as default
|
||||
// for clarity
|
||||
if (Str::startsWith($type, 'boolean')) {
|
||||
return 'false';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
$props = A::map(
|
||||
array_keys($this->json['props'] ?? []),
|
||||
fn ($key) => $this->prop($key)
|
||||
);
|
||||
|
||||
// remove empty props
|
||||
$props = array_filter($props);
|
||||
|
||||
usort($props, fn ($a, $b) => $a['name'] <=> $b['name']);
|
||||
|
||||
// always return an array
|
||||
return array_values($props);
|
||||
}
|
||||
|
||||
protected function read(): array
|
||||
{
|
||||
$file = $this->file('dev');
|
||||
|
||||
if (file_exists($file) === false) {
|
||||
$file = $this->file('dist');
|
||||
}
|
||||
|
||||
return Data::read($file);
|
||||
}
|
||||
|
||||
public static function root(bool $tmp = false): string
|
||||
{
|
||||
return App::instance()->root('panel') . '/' . match ($tmp) {
|
||||
true => 'tmp',
|
||||
default => 'dist/ui',
|
||||
};
|
||||
}
|
||||
|
||||
public function since(): string|null
|
||||
{
|
||||
return $this->json['tags']['since'][0]['description'] ?? null;
|
||||
}
|
||||
|
||||
public function slots(): array
|
||||
{
|
||||
$slots = A::map(
|
||||
$this->json['slots'] ?? [],
|
||||
fn ($slot) => [
|
||||
'name' => $slot['name'],
|
||||
'description' => $this->kt($slot['description'] ?? ''),
|
||||
'deprecated' => $this->kt($slot['tags']['deprecated'][0]['description'] ?? ''),
|
||||
'since' => $slot['tags']['since'][0]['description'] ?? null,
|
||||
'bindings' => A::map(
|
||||
$slot['bindings'] ?? [],
|
||||
fn ($binding) => [
|
||||
'name' => $binding['name'],
|
||||
'type' => $binding['type']['name'] ?? '',
|
||||
'description' => $this->kt($binding['description'] ?? '', true),
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
usort($slots, fn ($a, $b) => $a['name'] <=> $b['name']);
|
||||
|
||||
return $slots;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'component' => $this->name(),
|
||||
'deprecated' => $this->deprecated(),
|
||||
'description' => $this->description(),
|
||||
'docBlock' => $this->docBlock(),
|
||||
'events' => $this->events(),
|
||||
'examples' => $this->examples(),
|
||||
'github' => $this->github(),
|
||||
'methods' => $this->methods(),
|
||||
'props' => $this->props(),
|
||||
'since' => $this->since(),
|
||||
'slots' => $this->slots(),
|
||||
];
|
||||
}
|
||||
}
|
||||
296
kirby/src/Panel/Lab/Example.php
Normal file
296
kirby/src/Panel/Lab/Example.php
Normal file
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel\Lab;
|
||||
|
||||
use Kirby\Exception\NotFoundException;
|
||||
use Kirby\Filesystem\Dir;
|
||||
use Kirby\Filesystem\F;
|
||||
use Kirby\Http\Response;
|
||||
|
||||
/**
|
||||
* One or multiple lab examples with one or multiple tabs
|
||||
*
|
||||
* @internal
|
||||
* @since 4.0.0
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Example
|
||||
{
|
||||
protected string $root;
|
||||
protected string|null $tab = null;
|
||||
protected array $tabs;
|
||||
|
||||
public function __construct(
|
||||
protected Category $parent,
|
||||
protected string $id,
|
||||
string|null $tab = null,
|
||||
) {
|
||||
$this->root = $this->parent->root() . '/' . $this->id;
|
||||
|
||||
if ($this->exists() === false) {
|
||||
throw new NotFoundException('The example could not be found');
|
||||
}
|
||||
|
||||
$this->tabs = $this->collectTabs();
|
||||
$this->tab = $this->collectTab($tab);
|
||||
}
|
||||
|
||||
public function collectTab(string|null $tab): string|null
|
||||
{
|
||||
if (empty($this->tabs) === true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (array_key_exists($tab, $this->tabs) === true) {
|
||||
return $tab;
|
||||
}
|
||||
|
||||
return array_key_first($this->tabs);
|
||||
}
|
||||
|
||||
public function collectTabs(): array
|
||||
{
|
||||
$tabs = [];
|
||||
|
||||
foreach (Dir::inventory($this->root)['children'] as $child) {
|
||||
$tabs[$child['dirname']] = [
|
||||
'name' => $child['dirname'],
|
||||
'label' => $child['slug'],
|
||||
'link' => '/lab/' . $this->parent->id() . '/' . $this->id . '/' . $child['dirname']
|
||||
];
|
||||
}
|
||||
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
public function exists(): bool
|
||||
{
|
||||
return is_dir($this->root) === true;
|
||||
}
|
||||
|
||||
public function file(string $filename): string
|
||||
{
|
||||
return $this->parent->root() . '/' . $this->path() . '/' . $filename;
|
||||
}
|
||||
|
||||
public function id(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function load(string $filename): array|null
|
||||
{
|
||||
if ($file = $this->file($filename)) {
|
||||
return F::load($file);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function module(): string
|
||||
{
|
||||
return $this->url() . '/index.vue';
|
||||
}
|
||||
|
||||
public function path(): string
|
||||
{
|
||||
return match ($this->tab) {
|
||||
null => $this->id,
|
||||
default => $this->id . '/' . $this->tab
|
||||
};
|
||||
}
|
||||
|
||||
public function props(): array
|
||||
{
|
||||
if ($this->tab !== null) {
|
||||
$props = $this->load('../index.php');
|
||||
}
|
||||
|
||||
return array_replace_recursive(
|
||||
$props ?? [],
|
||||
$this->load('index.php') ?? []
|
||||
);
|
||||
}
|
||||
|
||||
public function read(string $filename): string|null
|
||||
{
|
||||
$file = $this->file($filename);
|
||||
|
||||
if (is_file($file) === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return F::read($file);
|
||||
}
|
||||
|
||||
public function root(): string
|
||||
{
|
||||
return $this->root;
|
||||
}
|
||||
|
||||
public function serve(): Response
|
||||
{
|
||||
return new Response($this->vue()['script'], 'application/javascript');
|
||||
}
|
||||
|
||||
public function tab(): string|null
|
||||
{
|
||||
return $this->tab;
|
||||
}
|
||||
|
||||
public function tabs(): array
|
||||
{
|
||||
return $this->tabs;
|
||||
}
|
||||
|
||||
public function template(string $filename): string|null
|
||||
{
|
||||
$file = $this->file($filename);
|
||||
|
||||
if (is_file($file) === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $this->props();
|
||||
return (new Template($file))->render($data);
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return basename($this->id);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'image' => [
|
||||
'icon' => $this->parent->icon(),
|
||||
'back' => 'white',
|
||||
],
|
||||
'text' => $this->title(),
|
||||
'link' => $this->url()
|
||||
];
|
||||
}
|
||||
|
||||
public function url(): string
|
||||
{
|
||||
return '/lab/' . $this->parent->id() . '/' . $this->path();
|
||||
}
|
||||
|
||||
public function vue(): array
|
||||
{
|
||||
// read the index.vue file (or programmabel Vue PHP file)
|
||||
$file = $this->read('index.vue');
|
||||
$file ??= $this->template('index.vue.php');
|
||||
$file ??= '';
|
||||
|
||||
// extract parts
|
||||
$parts['script'] = $this->vueScript($file);
|
||||
$parts['template'] = $this->vueTemplate($file);
|
||||
$parts['examples'] = $this->vueExamples($parts['template'], $parts['script']);
|
||||
$parts['style'] = $this->vueStyle($file);
|
||||
|
||||
return $parts;
|
||||
}
|
||||
|
||||
public function vueExamples(string|null $template, string|null $script): array
|
||||
{
|
||||
$template ??= '';
|
||||
$examples = [];
|
||||
$scripts = [];
|
||||
|
||||
if (preg_match_all('!\/\*\* \@script: (.*?)\*\/(.*?)\/\*\* \@script-end \*\/!s', $script, $matches)) {
|
||||
foreach ($matches[1] as $key => $name) {
|
||||
$code = $matches[2][$key];
|
||||
$code = preg_replace('!const (.*?) \=!', 'default', $code);
|
||||
|
||||
$scripts[trim($name)] = $code;
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match_all('!<k-lab-example[\s|\n].*?label="(.*?)"(.*?)>(.*?)<\/k-lab-example>!s', $template, $matches)) {
|
||||
foreach ($matches[1] as $key => $name) {
|
||||
$tail = $matches[2][$key];
|
||||
$code = $matches[3][$key];
|
||||
|
||||
$scriptId = trim(preg_replace_callback(
|
||||
'!script="(.*?)"!',
|
||||
fn ($match) => trim($match[1]),
|
||||
$tail
|
||||
));
|
||||
|
||||
$scriptBlock = $scripts[$scriptId] ?? null;
|
||||
|
||||
if (empty($scriptBlock) === false) {
|
||||
$js = PHP_EOL . PHP_EOL;
|
||||
$js .= '<script>';
|
||||
$js .= $scriptBlock;
|
||||
$js .= '</script>';
|
||||
} else {
|
||||
$js = '';
|
||||
}
|
||||
|
||||
// only use the code between the @code and @code-end comments
|
||||
if (preg_match('$<!-- @code -->(.*?)<!-- @code-end -->$s', $code, $match)) {
|
||||
$code = $match[1];
|
||||
}
|
||||
|
||||
if (preg_match_all('/^(\t*)\S/m', $code, $indents)) {
|
||||
// get minimum indent
|
||||
$indents = array_map(fn ($i) => strlen($i), $indents[1]);
|
||||
$indents = min($indents);
|
||||
|
||||
if (empty($js) === false) {
|
||||
$indents--;
|
||||
}
|
||||
|
||||
// strip minimum indent from each line
|
||||
$code = preg_replace('/^\t{' . $indents . '}/m', '', $code);
|
||||
}
|
||||
|
||||
$code = trim($code);
|
||||
|
||||
if (empty($js) === false) {
|
||||
$code = '<template>' . PHP_EOL . "\t" . $code . PHP_EOL . '</template>';
|
||||
}
|
||||
|
||||
$examples[$name] = $code . $js;
|
||||
}
|
||||
}
|
||||
|
||||
return $examples;
|
||||
}
|
||||
|
||||
public function vueScript(string $file): string
|
||||
{
|
||||
if (preg_match('!<script>(.*)</script>!s', $file, $match)) {
|
||||
return trim($match[1]);
|
||||
}
|
||||
|
||||
return 'export default {}';
|
||||
}
|
||||
|
||||
public function vueStyle(string $file): string|null
|
||||
{
|
||||
if (preg_match('!<style>(.*)</style>!s', $file, $match)) {
|
||||
return trim($match[1]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function vueTemplate(string $file): string|null
|
||||
{
|
||||
if (preg_match('!<template>(.*)</template>!s', $file, $match)) {
|
||||
return preg_replace('!^\n!', '', $match[1]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
26
kirby/src/Panel/Lab/Snippet.php
Normal file
26
kirby/src/Panel/Lab/Snippet.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel\Lab;
|
||||
|
||||
use Kirby\Template\Snippet as BaseSnippet;
|
||||
|
||||
/**
|
||||
* Custom snippet class for lab examples
|
||||
*
|
||||
* @internal
|
||||
* @since 4.0.0
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Snippet extends BaseSnippet
|
||||
{
|
||||
public static function root(): string
|
||||
{
|
||||
return __DIR__ . '/snippets';
|
||||
}
|
||||
}
|
||||
34
kirby/src/Panel/Lab/Template.php
Normal file
34
kirby/src/Panel/Lab/Template.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel\Lab;
|
||||
|
||||
use Kirby\Template\Template as BaseTemplate;
|
||||
|
||||
/**
|
||||
* Custom template class for lab examples
|
||||
*
|
||||
* @internal
|
||||
* @since 4.0.0
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Template extends BaseTemplate
|
||||
{
|
||||
public function __construct(
|
||||
public string $file
|
||||
) {
|
||||
parent::__construct(
|
||||
name: basename($this->file)
|
||||
);
|
||||
}
|
||||
|
||||
public function file(): string|null
|
||||
{
|
||||
return $this->file;
|
||||
}
|
||||
}
|
||||
221
kirby/src/Panel/Menu.php
Normal file
221
kirby/src/Panel/Menu.php
Normal file
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Closure;
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
/**
|
||||
* The Menu class takes care of gathering
|
||||
* all menu entries for the Panel
|
||||
* @since 4.0.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Menu
|
||||
{
|
||||
public function __construct(
|
||||
protected array $areas = [],
|
||||
protected array $permissions = [],
|
||||
protected string|null $current = null
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all areas that are configured for the menu
|
||||
* @internal
|
||||
*/
|
||||
public function areas(): array
|
||||
{
|
||||
// get from config option which areas should be listed in the menu
|
||||
$kirby = App::instance();
|
||||
$areas = $kirby->option('panel.menu');
|
||||
|
||||
if ($areas instanceof Closure) {
|
||||
$areas = $areas($kirby);
|
||||
}
|
||||
|
||||
// if no config is defined…
|
||||
if ($areas === null) {
|
||||
// ensure that some defaults are on top in the right order
|
||||
$defaults = ['site', 'languages', 'users', 'system'];
|
||||
// add all other areas after that
|
||||
$additionals = array_diff(array_keys($this->areas), $defaults);
|
||||
$areas = array_merge($defaults, $additionals);
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($areas as $id => $area) {
|
||||
// separator, keep as is in array
|
||||
if ($area === '-') {
|
||||
$result[] = '-';
|
||||
continue;
|
||||
}
|
||||
|
||||
// for a simple id, get global area definition
|
||||
if (is_numeric($id) === true) {
|
||||
$id = $area;
|
||||
$area = $this->areas[$id] ?? null;
|
||||
}
|
||||
|
||||
// did not receive custom entry definition in config,
|
||||
// but also is not a global area
|
||||
if ($area === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// merge area definition (e.g. from config)
|
||||
// with global area definition
|
||||
if (is_array($area) === true) {
|
||||
$area = array_merge(
|
||||
$this->areas[$id] ?? [],
|
||||
['menu' => true],
|
||||
$area
|
||||
);
|
||||
$area = Panel::area($id, $area);
|
||||
}
|
||||
|
||||
$result[] = $area;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an area definition into a menu entry
|
||||
* @internal
|
||||
*/
|
||||
public function entry(array $area): array|false
|
||||
{
|
||||
// areas without access permissions get skipped entirely
|
||||
if ($this->hasPermission($area['id']) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check menu setting from the area definition
|
||||
$menu = $area['menu'] ?? false;
|
||||
|
||||
// menu setting can be a callback
|
||||
// that returns true, false or 'disabled'
|
||||
if ($menu instanceof Closure) {
|
||||
$menu = $menu($this->areas, $this->permissions, $this->current);
|
||||
}
|
||||
|
||||
// false will remove the area/entry entirely
|
||||
//just like with disabled permissions
|
||||
if ($menu === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$menu = match ($menu) {
|
||||
'disabled' => ['disabled' => true],
|
||||
true => [],
|
||||
default => $menu
|
||||
};
|
||||
|
||||
$entry = array_merge([
|
||||
'current' => $this->isCurrent(
|
||||
$area['id'],
|
||||
$area['current'] ?? null
|
||||
),
|
||||
'icon' => $area['icon'] ?? null,
|
||||
'link' => $area['link'] ?? null,
|
||||
'dialog' => $area['dialog'] ?? null,
|
||||
'drawer' => $area['drawer'] ?? null,
|
||||
'text' => I18n::translate($area['label'], $area['label'])
|
||||
], $menu);
|
||||
|
||||
// unset the link (which is always added by default to an area)
|
||||
// if a dialog or drawer should be opened instead
|
||||
if (isset($entry['dialog']) || isset($entry['drawer'])) {
|
||||
unset($entry['link']);
|
||||
}
|
||||
|
||||
return array_filter($entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all menu entries
|
||||
*/
|
||||
public function entries(): array
|
||||
{
|
||||
$entries = [];
|
||||
$areas = $this->areas();
|
||||
|
||||
foreach ($areas as $area) {
|
||||
if ($area === '-') {
|
||||
$entries[] = '-';
|
||||
} elseif ($entry = $this->entry($area)) {
|
||||
$entries[] = $entry;
|
||||
}
|
||||
}
|
||||
|
||||
$entries[] = '-';
|
||||
|
||||
return array_merge($entries, $this->options());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the access permission to a specific area is granted.
|
||||
* Defaults to allow access.
|
||||
* @internal
|
||||
*/
|
||||
public function hasPermission(string $id): bool
|
||||
{
|
||||
return $this->permissions['access'][$id] ?? true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the menu entry should receive aria-current
|
||||
* @internal
|
||||
*/
|
||||
public function isCurrent(
|
||||
string $id,
|
||||
bool|Closure|null $callback = null
|
||||
): bool {
|
||||
if ($callback !== null) {
|
||||
if ($callback instanceof Closure) {
|
||||
$callback = $callback($this->current);
|
||||
}
|
||||
|
||||
return $callback;
|
||||
}
|
||||
|
||||
return $this->current === $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default options entries for bottom of menu
|
||||
* @internal
|
||||
*/
|
||||
public function options(): array
|
||||
{
|
||||
$options = [
|
||||
[
|
||||
'icon' => 'edit-line',
|
||||
'dialog' => 'changes',
|
||||
'text' => I18n::translate('changes'),
|
||||
],
|
||||
[
|
||||
'current' => $this->isCurrent('account'),
|
||||
'icon' => 'account',
|
||||
'link' => 'account',
|
||||
'disabled' => $this->hasPermission('account') === false,
|
||||
'text' => I18n::translate('view.account'),
|
||||
],
|
||||
[
|
||||
'icon' => 'logout',
|
||||
'link' => 'logout',
|
||||
'text' => I18n::translate('logout')
|
||||
]
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
433
kirby/src/Panel/Model.php
Normal file
433
kirby/src/Panel/Model.php
Normal file
@@ -0,0 +1,433 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Closure;
|
||||
use Kirby\Cms\File as CmsFile;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Filesystem\Asset;
|
||||
use Kirby\Form\Form;
|
||||
use Kirby\Http\Uri;
|
||||
use Kirby\Toolkit\A;
|
||||
|
||||
/**
|
||||
* Provides information about the model for the Panel
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
abstract class Model
|
||||
{
|
||||
public function __construct(
|
||||
protected ModelWithContent $model
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content values for the model
|
||||
*/
|
||||
public function content(): array
|
||||
{
|
||||
return Form::for($this->model)->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the drag text from a custom callback
|
||||
* if the callback is defined in the config
|
||||
* @internal
|
||||
*
|
||||
* @param string $type markdown or kirbytext
|
||||
*/
|
||||
public function dragTextFromCallback(string $type, ...$args): string|null
|
||||
{
|
||||
$option = 'panel.' . $type . '.' . $this->model::CLASS_ALIAS . 'DragText';
|
||||
$callback = $this->model->kirby()->option($option);
|
||||
|
||||
if ($callback instanceof Closure) {
|
||||
return $callback($this->model, ...$args);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correct drag text type
|
||||
* depending on the given type or the
|
||||
* configuration
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @param string|null $type (`auto`|`kirbytext`|`markdown`)
|
||||
*/
|
||||
public function dragTextType(string|null $type = null): string
|
||||
{
|
||||
$type ??= 'auto';
|
||||
|
||||
if ($type === 'auto') {
|
||||
$kirby = $this->model->kirby();
|
||||
$type = $kirby->option('panel.kirbytext', true) ? 'kirbytext' : 'markdown';
|
||||
}
|
||||
|
||||
return $type === 'markdown' ? 'markdown' : 'kirbytext';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setup for a dropdown option
|
||||
* which is used in the changes dropdown
|
||||
* for example.
|
||||
*/
|
||||
public function dropdownOption(): array
|
||||
{
|
||||
return [
|
||||
'icon' => 'page',
|
||||
'image' => $this->image(['back' => 'black']),
|
||||
'link' => $this->url(true),
|
||||
'text' => $this->model->id(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Panel image definition
|
||||
* @internal
|
||||
*/
|
||||
public function image(
|
||||
string|array|false|null $settings = [],
|
||||
string $layout = 'list'
|
||||
): array|null {
|
||||
// completely switched off
|
||||
if ($settings === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// switched off from blueprint,
|
||||
// only if not overwritten by $settings
|
||||
$blueprint = $this->model->blueprint()->image();
|
||||
|
||||
if ($blueprint === false) {
|
||||
if (empty($settings) === true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$blueprint = null;
|
||||
}
|
||||
|
||||
// skip image thumbnail if option
|
||||
// is explicitly set to show the icon
|
||||
if ($settings === 'icon') {
|
||||
$settings = ['query' => false];
|
||||
} elseif (is_string($settings) === true) {
|
||||
// convert string settings to proper array
|
||||
$settings = ['query' => $settings];
|
||||
}
|
||||
|
||||
// merge with defaults and blueprint option
|
||||
$settings = array_merge(
|
||||
$this->imageDefaults(),
|
||||
$settings ?? [],
|
||||
$blueprint ?? [],
|
||||
);
|
||||
|
||||
if ($image = $this->imageSource($settings['query'] ?? null)) {
|
||||
// main url
|
||||
$settings['url'] = $image->url();
|
||||
|
||||
if ($image->isResizable() === true) {
|
||||
// only create srcsets for resizable files
|
||||
$settings['src'] = static::imagePlaceholder();
|
||||
$settings['srcset'] = $this->imageSrcset($image, $layout, $settings);
|
||||
} elseif ($image->isViewable() === true) {
|
||||
$settings['src'] = $image->url();
|
||||
}
|
||||
}
|
||||
|
||||
unset($settings['query']);
|
||||
|
||||
// resolve remaining options defined as query
|
||||
return A::map($settings, function ($option) {
|
||||
if (is_string($option) === false) {
|
||||
return $option;
|
||||
}
|
||||
|
||||
return $this->model->toString($option);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Default settings for Panel image
|
||||
*/
|
||||
protected function imageDefaults(): array
|
||||
{
|
||||
return [
|
||||
'back' => 'pattern',
|
||||
'color' => 'gray-500',
|
||||
'cover' => false,
|
||||
'icon' => 'page'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Data URI placeholder string for Panel image
|
||||
* @internal
|
||||
*/
|
||||
public static function imagePlaceholder(): string
|
||||
{
|
||||
return 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image file object based on provided query
|
||||
* @internal
|
||||
*/
|
||||
protected function imageSource(
|
||||
string|null $query = null
|
||||
): CmsFile|Asset|null {
|
||||
$image = $this->model->query($query ?? null);
|
||||
|
||||
// validate the query result
|
||||
if (
|
||||
$image instanceof CmsFile ||
|
||||
$image instanceof Asset
|
||||
) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the correct srcset string based on
|
||||
* the layout and settings
|
||||
* @internal
|
||||
*/
|
||||
protected function imageSrcset(
|
||||
CmsFile|Asset $image,
|
||||
string $layout,
|
||||
array $settings
|
||||
): string|null {
|
||||
// depending on layout type, set different sizes
|
||||
// to have multiple options for the srcset attribute
|
||||
$sizes = match ($layout) {
|
||||
'cards' => [352, 864, 1408],
|
||||
'cardlets' => [96, 192],
|
||||
default => [38, 76]
|
||||
};
|
||||
|
||||
// no additional modfications needed if `cover: false`
|
||||
if (($settings['cover'] ?? false) === false) {
|
||||
return $image->srcset($sizes);
|
||||
}
|
||||
|
||||
// for card layouts with `cover: true` provide
|
||||
// crops based on the card ratio
|
||||
if ($layout === 'cards') {
|
||||
$ratio = explode('/', $settings['ratio'] ?? '1/1');
|
||||
$ratio = $ratio[0] / $ratio[1];
|
||||
|
||||
return $image->srcset([
|
||||
$sizes[0] . 'w' => [
|
||||
'width' => $sizes[0],
|
||||
'height' => round($sizes[0] / $ratio),
|
||||
'crop' => true
|
||||
],
|
||||
$sizes[1] . 'w' => [
|
||||
'width' => $sizes[1],
|
||||
'height' => round($sizes[1] / $ratio),
|
||||
'crop' => true
|
||||
],
|
||||
$sizes[2] . 'w' => [
|
||||
'width' => $sizes[2],
|
||||
'height' => round($sizes[2] / $ratio),
|
||||
'crop' => true
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// for list and cardlets with `cover: true`
|
||||
// provide square crops in two resolutions
|
||||
return $image->srcset([
|
||||
'1x' => [
|
||||
'width' => $sizes[0],
|
||||
'height' => $sizes[0],
|
||||
'crop' => true
|
||||
],
|
||||
'2x' => [
|
||||
'width' => $sizes[1],
|
||||
'height' => $sizes[1],
|
||||
'crop' => true
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for disabled dropdown options according
|
||||
* to the given permissions
|
||||
*/
|
||||
public function isDisabledDropdownOption(
|
||||
string $action,
|
||||
array $options,
|
||||
array $permissions
|
||||
): bool {
|
||||
$option = $options[$action] ?? true;
|
||||
|
||||
return
|
||||
$permissions[$action] === false ||
|
||||
$option === false ||
|
||||
$option === 'false';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns lock info for the Panel
|
||||
*
|
||||
* @return array|false array with lock info,
|
||||
* false if locking is not supported
|
||||
*/
|
||||
public function lock(): array|false
|
||||
{
|
||||
return $this->model->lock()?->toArray() ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all actions
|
||||
* that can be performed in the Panel
|
||||
* This also checks for the lock status
|
||||
*
|
||||
* @param array $unlock An array of options that will be force-unlocked
|
||||
*/
|
||||
public function options(array $unlock = []): array
|
||||
{
|
||||
$options = $this->model->permissions()->toArray();
|
||||
|
||||
if ($this->model->isLocked()) {
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array($key, $unlock)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$options[$key] = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path without leading slash
|
||||
*/
|
||||
abstract public function path(): string;
|
||||
|
||||
/**
|
||||
* Prepares the response data for page pickers
|
||||
* and page fields
|
||||
*/
|
||||
public function pickerData(array $params = []): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->model->id(),
|
||||
'image' => $this->image(
|
||||
$params['image'] ?? [],
|
||||
$params['layout'] ?? 'list'
|
||||
),
|
||||
'info' => $this->model->toSafeString($params['info'] ?? false),
|
||||
'link' => $this->url(true),
|
||||
'sortable' => true,
|
||||
'text' => $this->model->toSafeString($params['text'] ?? false),
|
||||
'uuid' => $this->model->uuid()?->toString() ?? $this->model->id(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for the
|
||||
* view's component props
|
||||
* @internal
|
||||
*/
|
||||
public function props(): array
|
||||
{
|
||||
$blueprint = $this->model->blueprint();
|
||||
$request = $this->model->kirby()->request();
|
||||
$tabs = $blueprint->tabs();
|
||||
$tab = $blueprint->tab($request->get('tab')) ?? $tabs[0] ?? null;
|
||||
|
||||
$props = [
|
||||
'lock' => $this->lock(),
|
||||
'permissions' => $this->model->permissions()->toArray(),
|
||||
'tabs' => $tabs,
|
||||
];
|
||||
|
||||
// only send the tab if it exists
|
||||
// this will let the vue component define
|
||||
// a proper default value
|
||||
if ($tab) {
|
||||
$props['tab'] = $tab;
|
||||
}
|
||||
|
||||
return $props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns link url and title
|
||||
* for model (e.g. used for prev/next navigation)
|
||||
* @internal
|
||||
*/
|
||||
public function toLink(string $title = 'title'): array
|
||||
{
|
||||
return [
|
||||
'link' => $this->url(true),
|
||||
'title' => $title = (string)$this->model->{$title}()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns link url and title
|
||||
* for optional sibling model and
|
||||
* preserves tab selection
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function toPrevNextLink(
|
||||
ModelWithContent|null $model = null,
|
||||
string $title = 'title'
|
||||
): array|null {
|
||||
if ($model === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $model->panel()->toLink($title);
|
||||
|
||||
if ($tab = $model->kirby()->request()->get('tab')) {
|
||||
$uri = new Uri($data['link'], [
|
||||
'query' => ['tab' => $tab]
|
||||
]);
|
||||
|
||||
$data['link'] = $uri->toString();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the url to the editing view
|
||||
* in the Panel
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function url(bool $relative = false): string
|
||||
{
|
||||
if ($relative === true) {
|
||||
return '/' . $this->path();
|
||||
}
|
||||
|
||||
return $this->model->kirby()->url('panel') . '/' . $this->path();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for
|
||||
* this model's Panel view
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
abstract public function view(): array;
|
||||
}
|
||||
369
kirby/src/Panel/Page.php
Normal file
369
kirby/src/Panel/Page.php
Normal file
@@ -0,0 +1,369 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\File as CmsFile;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Filesystem\Asset;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
/**
|
||||
* Provides information about the page model for the Panel
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Page extends Model
|
||||
{
|
||||
/**
|
||||
* @var \Kirby\Cms\Page
|
||||
*/
|
||||
protected ModelWithContent $model;
|
||||
|
||||
/**
|
||||
* Breadcrumb array
|
||||
*/
|
||||
public function breadcrumb(): array
|
||||
{
|
||||
$parents = $this->model->parents()->flip()->merge($this->model);
|
||||
|
||||
return $parents->values(
|
||||
fn ($parent) => [
|
||||
'label' => $parent->title()->toString(),
|
||||
'link' => $parent->panel()->url(true),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a kirbytag or markdown
|
||||
* tag for the page, which will be
|
||||
* used in the panel, when the page
|
||||
* gets dragged onto a textarea
|
||||
*
|
||||
* @internal
|
||||
* @param string|null $type (`auto`|`kirbytext`|`markdown`)
|
||||
*/
|
||||
public function dragText(string|null $type = null): string
|
||||
{
|
||||
$type = $this->dragTextType($type);
|
||||
|
||||
if ($callback = $this->dragTextFromCallback($type)) {
|
||||
return $callback;
|
||||
}
|
||||
|
||||
$title = $this->model->title();
|
||||
|
||||
// type: markdown
|
||||
if ($type === 'markdown') {
|
||||
$url = $this->model->permalink() ?? $this->model->url();
|
||||
return '[' . $title . '](' . $url . ')';
|
||||
}
|
||||
|
||||
// type: kirbytext
|
||||
$link = $this->model->uuid() ?? $this->model->uri();
|
||||
return '(link: ' . $link . ' text: ' . $title . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides options for the page dropdown
|
||||
*/
|
||||
public function dropdown(array $options = []): array
|
||||
{
|
||||
$page = $this->model;
|
||||
$request = $page->kirby()->request();
|
||||
$defaults = $request->get(['view', 'sort', 'delete']);
|
||||
$options = array_merge($defaults, $options);
|
||||
|
||||
$permissions = $this->options(['preview']);
|
||||
$view = $options['view'] ?? 'view';
|
||||
$url = $this->url(true);
|
||||
$result = [];
|
||||
|
||||
if ($view === 'list') {
|
||||
$result['preview'] = [
|
||||
'link' => $page->previewUrl(),
|
||||
'target' => '_blank',
|
||||
'icon' => 'open',
|
||||
'text' => I18n::translate('open'),
|
||||
'disabled' => $this->isDisabledDropdownOption('preview', $options, $permissions)
|
||||
];
|
||||
$result[] = '-';
|
||||
}
|
||||
|
||||
$result['changeTitle'] = [
|
||||
'dialog' => [
|
||||
'url' => $url . '/changeTitle',
|
||||
'query' => [
|
||||
'select' => 'title'
|
||||
]
|
||||
],
|
||||
'icon' => 'title',
|
||||
'text' => I18n::translate('rename'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeTitle', $options, $permissions)
|
||||
];
|
||||
|
||||
$result['changeSlug'] = [
|
||||
'dialog' => [
|
||||
'url' => $url . '/changeTitle',
|
||||
'query' => [
|
||||
'select' => 'slug'
|
||||
]
|
||||
],
|
||||
'icon' => 'url',
|
||||
'text' => I18n::translate('page.changeSlug'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeSlug', $options, $permissions)
|
||||
];
|
||||
|
||||
$result['changeStatus'] = [
|
||||
'dialog' => $url . '/changeStatus',
|
||||
'icon' => 'preview',
|
||||
'text' => I18n::translate('page.changeStatus'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeStatus', $options, $permissions)
|
||||
];
|
||||
|
||||
$siblings = $page->parentModel()->children()->listed()->not($page);
|
||||
|
||||
$result['changeSort'] = [
|
||||
'dialog' => $url . '/changeSort',
|
||||
'icon' => 'sort',
|
||||
'text' => I18n::translate('page.sort'),
|
||||
'disabled' => $siblings->count() === 0 || $this->isDisabledDropdownOption('sort', $options, $permissions)
|
||||
];
|
||||
|
||||
$result['changeTemplate'] = [
|
||||
'dialog' => $url . '/changeTemplate',
|
||||
'icon' => 'template',
|
||||
'text' => I18n::translate('page.changeTemplate'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeTemplate', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = '-';
|
||||
|
||||
$result['move'] = [
|
||||
'dialog' => $url . '/move',
|
||||
'icon' => 'parent',
|
||||
'text' => I18n::translate('page.move'),
|
||||
'disabled' => $this->isDisabledDropdownOption('move', $options, $permissions)
|
||||
];
|
||||
|
||||
$result['duplicate'] = [
|
||||
'dialog' => $url . '/duplicate',
|
||||
'icon' => 'copy',
|
||||
'text' => I18n::translate('duplicate'),
|
||||
'disabled' => $this->isDisabledDropdownOption('duplicate', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = '-';
|
||||
|
||||
$result['delete'] = [
|
||||
'dialog' => $url . '/delete',
|
||||
'icon' => 'trash',
|
||||
'text' => I18n::translate('delete'),
|
||||
'disabled' => $this->isDisabledDropdownOption('delete', $options, $permissions)
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setup for a dropdown option
|
||||
* which is used in the changes dropdown
|
||||
* for example.
|
||||
*/
|
||||
public function dropdownOption(): array
|
||||
{
|
||||
return [
|
||||
'text' => $this->model->title()->value(),
|
||||
] + parent::dropdownOption();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the escaped Id, which is
|
||||
* used in the panel to make routing work properly
|
||||
*/
|
||||
public function id(): string
|
||||
{
|
||||
return str_replace('/', '+', $this->model->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Default settings for the page's Panel image
|
||||
*/
|
||||
protected function imageDefaults(): array
|
||||
{
|
||||
$defaults = [];
|
||||
|
||||
if ($icon = $this->model->blueprint()->icon()) {
|
||||
$defaults['icon'] = $icon;
|
||||
}
|
||||
|
||||
return array_merge(parent::imageDefaults(), $defaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image file object based on provided query
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function imageSource(
|
||||
string|null $query = null
|
||||
): CmsFile|Asset|null {
|
||||
$query ??= 'page.image';
|
||||
return parent::imageSource($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path without leading slash
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function path(): string
|
||||
{
|
||||
return 'pages/' . $this->id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the response data for page pickers
|
||||
* and page fields
|
||||
*/
|
||||
public function pickerData(array $params = []): array
|
||||
{
|
||||
$params['text'] ??= '{{ page.title }}';
|
||||
|
||||
return array_merge(parent::pickerData($params), [
|
||||
'dragText' => $this->dragText(),
|
||||
'hasChildren' => $this->model->hasChildren(),
|
||||
'url' => $this->model->url()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The best applicable position for
|
||||
* the position/status dialog
|
||||
*/
|
||||
public function position(): int
|
||||
{
|
||||
return
|
||||
$this->model->num() ??
|
||||
$this->model->parentModel()->children()->listed()->not($this->model)->count() + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns navigation array with
|
||||
* previous and next page
|
||||
* based on blueprint definition
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function prevNext(): array
|
||||
{
|
||||
$page = $this->model;
|
||||
|
||||
// create siblings collection based on
|
||||
// blueprint navigation
|
||||
$siblings = function (string $direction) use ($page) {
|
||||
$navigation = $page->blueprint()->navigation();
|
||||
$sortBy = $navigation['sortBy'] ?? null;
|
||||
$status = $navigation['status'] ?? null;
|
||||
$template = $navigation['template'] ?? null;
|
||||
$direction = $direction === 'prev' ? 'prev' : 'next';
|
||||
|
||||
// if status is defined in navigation,
|
||||
// all items in the collection are used
|
||||
// (drafts, listed and unlisted) otherwise
|
||||
// it depends on the status of the page
|
||||
$siblings = $status !== null ? $page->parentModel()->childrenAndDrafts() : $page->siblings();
|
||||
|
||||
// sort the collection if custom sortBy
|
||||
// defined in navigation otherwise
|
||||
// default sorting will apply
|
||||
if ($sortBy !== null) {
|
||||
$siblings = $siblings->sort(...$siblings::sortArgs($sortBy));
|
||||
}
|
||||
|
||||
$siblings = $page->{$direction . 'All'}($siblings);
|
||||
|
||||
if (empty($navigation) === false) {
|
||||
$statuses = (array)($status ?? $page->status());
|
||||
$templates = (array)($template ?? $page->intendedTemplate());
|
||||
|
||||
// do not filter if template navigation is all
|
||||
if (in_array('all', $templates) === false) {
|
||||
$siblings = $siblings->filter('intendedTemplate', 'in', $templates);
|
||||
}
|
||||
|
||||
// do not filter if status navigation is all
|
||||
if (in_array('all', $statuses) === false) {
|
||||
$siblings = $siblings->filter('status', 'in', $statuses);
|
||||
}
|
||||
} else {
|
||||
$siblings = $siblings
|
||||
->filter('intendedTemplate', $page->intendedTemplate())
|
||||
->filter('status', $page->status());
|
||||
}
|
||||
|
||||
return $siblings->filter('isListable', true);
|
||||
};
|
||||
|
||||
return [
|
||||
'next' => fn () => $this->toPrevNextLink($siblings('next')->first()),
|
||||
'prev' => fn () => $this->toPrevNextLink($siblings('prev')->last())
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for the
|
||||
* view's component props
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function props(): array
|
||||
{
|
||||
$page = $this->model;
|
||||
|
||||
return array_merge(
|
||||
parent::props(),
|
||||
$this->prevNext(),
|
||||
[
|
||||
'blueprint' => $page->intendedTemplate()->name(),
|
||||
'model' => [
|
||||
'content' => $this->content(),
|
||||
'id' => $page->id(),
|
||||
'link' => $this->url(true),
|
||||
'parent' => $page->parentModel()->panel()->url(true),
|
||||
'previewUrl' => $page->previewUrl(),
|
||||
'status' => $page->status(),
|
||||
'title' => $page->title()->toString(),
|
||||
],
|
||||
'status' => function () use ($page) {
|
||||
if ($status = $page->status()) {
|
||||
return $page->blueprint()->status()[$status] ?? null;
|
||||
}
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for
|
||||
* this model's Panel view
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function view(): array
|
||||
{
|
||||
$page = $this->model;
|
||||
|
||||
return [
|
||||
'breadcrumb' => $page->panel()->breadcrumb(),
|
||||
'component' => 'k-page-view',
|
||||
'props' => $this->props(),
|
||||
'title' => $page->title()->toString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
389
kirby/src/Panel/PageCreateDialog.php
Normal file
389
kirby/src/Panel/PageCreateDialog.php
Normal file
@@ -0,0 +1,389 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\File;
|
||||
use Kirby\Cms\Find;
|
||||
use Kirby\Cms\Page;
|
||||
use Kirby\Cms\PageBlueprint;
|
||||
use Kirby\Cms\PageRules;
|
||||
use Kirby\Cms\Site;
|
||||
use Kirby\Cms\User;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Form\Form;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
/**
|
||||
* Manages the Panel dialog to create new pages
|
||||
* @since 4.0.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class PageCreateDialog
|
||||
{
|
||||
protected PageBlueprint $blueprint;
|
||||
protected Page $model;
|
||||
protected Page|Site $parent;
|
||||
protected string $parentId;
|
||||
protected string|null $sectionId;
|
||||
protected string|null $slug;
|
||||
protected string|null $template;
|
||||
protected string|null $title;
|
||||
protected Page|Site|User|File $view;
|
||||
protected string|null $viewId;
|
||||
|
||||
public static array $fieldTypes = [
|
||||
'checkboxes',
|
||||
'date',
|
||||
'email',
|
||||
'info',
|
||||
'line',
|
||||
'link',
|
||||
'list',
|
||||
'number',
|
||||
'multiselect',
|
||||
'radio',
|
||||
'range',
|
||||
'select',
|
||||
'slug',
|
||||
'tags',
|
||||
'tel',
|
||||
'text',
|
||||
'toggles',
|
||||
'time',
|
||||
'url'
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
string|null $parentId,
|
||||
string|null $sectionId,
|
||||
string|null $template,
|
||||
string|null $viewId,
|
||||
|
||||
// optional
|
||||
string|null $slug = null,
|
||||
string|null $title = null,
|
||||
) {
|
||||
$this->parentId = $parentId ?? 'site';
|
||||
$this->parent = Find::parent($this->parentId);
|
||||
$this->sectionId = $sectionId;
|
||||
$this->slug = $slug;
|
||||
$this->template = $template;
|
||||
$this->title = $title;
|
||||
$this->viewId = $viewId;
|
||||
$this->view = Find::parent($this->viewId ?? $this->parentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the blueprint settings for the new page
|
||||
*/
|
||||
public function blueprint(): PageBlueprint
|
||||
{
|
||||
// create a temporary page object
|
||||
return $this->blueprint ??= $this->model()->blueprint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of all blueprints for the parent view
|
||||
*/
|
||||
public function blueprints(): array
|
||||
{
|
||||
return A::map(
|
||||
$this->view->blueprints($this->sectionId),
|
||||
function ($blueprint) {
|
||||
$blueprint['name'] ??= $blueprint['value'] ?? null;
|
||||
return $blueprint;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* All the default fields for the dialog
|
||||
*/
|
||||
public function coreFields(): array
|
||||
{
|
||||
$fields = [];
|
||||
|
||||
$title = $this->blueprint()->create()['title'] ?? null;
|
||||
$slug = $this->blueprint()->create()['slug'] ?? null;
|
||||
|
||||
if ($title === false || $slug === false) {
|
||||
throw new InvalidArgumentException('Page create dialog: title and slug must not be false');
|
||||
}
|
||||
|
||||
// title field
|
||||
if ($title === null || is_array($title) === true) {
|
||||
$label = $title['label'] ?? 'title';
|
||||
$fields['title'] = Field::title([
|
||||
...$title ?? [],
|
||||
'label' => I18n::translate($label, $label),
|
||||
'required' => true,
|
||||
'preselect' => true
|
||||
]);
|
||||
}
|
||||
|
||||
// slug field
|
||||
if ($slug === null) {
|
||||
$fields['slug'] = Field::slug([
|
||||
'required' => true,
|
||||
'sync' => 'title',
|
||||
'path' => $this->parent instanceof Page ? '/' . $this->parent->id() . '/' : '/'
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
...$fields,
|
||||
'parent' => Field::hidden(),
|
||||
'section' => Field::hidden(),
|
||||
'template' => Field::hidden(),
|
||||
'view' => Field::hidden(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads custom fields for the page type
|
||||
*/
|
||||
public function customFields(): array
|
||||
{
|
||||
$custom = [];
|
||||
$ignore = ['title', 'slug', 'parent', 'template'];
|
||||
$blueprint = $this->blueprint();
|
||||
$fields = $blueprint->fields();
|
||||
|
||||
foreach ($blueprint->create()['fields'] ?? [] as $name) {
|
||||
if (!$field = ($fields[$name] ?? null)) {
|
||||
throw new InvalidArgumentException('Unknown field "' . $name . '" in create dialog');
|
||||
}
|
||||
|
||||
if (in_array($field['type'], static::$fieldTypes) === false) {
|
||||
throw new InvalidArgumentException('Field type "' . $field['type'] . '" not supported in create dialog');
|
||||
}
|
||||
|
||||
if (in_array($name, $ignore) === true) {
|
||||
throw new InvalidArgumentException('Field name "' . $name . '" not allowed as custom field in create dialog');
|
||||
}
|
||||
|
||||
// switch all fields to 1/1
|
||||
$field['width'] = '1/1';
|
||||
|
||||
// add the field to the form
|
||||
$custom[$name] = $field;
|
||||
}
|
||||
|
||||
// create form so that field props, options etc.
|
||||
// can be properly resolved
|
||||
$form = new Form([
|
||||
'fields' => $custom,
|
||||
'model' => $this->model(),
|
||||
'strict' => true
|
||||
]);
|
||||
|
||||
return $form->fields()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the fields for the dialog
|
||||
*/
|
||||
public function fields(): array
|
||||
{
|
||||
return [
|
||||
...$this->coreFields(),
|
||||
...$this->customFields()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides all the props for the
|
||||
* dialog, including the fields and
|
||||
* initial values
|
||||
*/
|
||||
public function load(): array
|
||||
{
|
||||
$blueprints = $this->blueprints();
|
||||
|
||||
$this->template ??= $blueprints[0]['name'];
|
||||
|
||||
$status = $this->blueprint()->create()['status'] ?? 'draft';
|
||||
$status = $this->blueprint()->status()[$status]['label'] ?? null;
|
||||
$status ??= I18n::translate('page.status.' . $status);
|
||||
|
||||
$fields = $this->fields();
|
||||
$visible = array_filter(
|
||||
$fields,
|
||||
fn ($field) => ($field['hidden'] ?? null) !== true
|
||||
);
|
||||
|
||||
// immediately submit the dialog if there is no editable field
|
||||
if (count($visible) === 0 && count($blueprints) < 2) {
|
||||
$input = $this->value();
|
||||
$response = $this->submit($input);
|
||||
$response['redirect'] ??= $this->parent->panel()->url(true);
|
||||
Panel::go($response['redirect']);
|
||||
}
|
||||
|
||||
return [
|
||||
'component' => 'k-page-create-dialog',
|
||||
'props' => [
|
||||
'blueprints' => $blueprints,
|
||||
'fields' => $fields,
|
||||
'submitButton' => I18n::template('page.create', [
|
||||
'status' => $status
|
||||
]),
|
||||
'template' => $this->template,
|
||||
'value' => $this->value()
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Temporary model for the page to
|
||||
* be created, used to properly render
|
||||
* the blueprint for fields
|
||||
*/
|
||||
public function model(): Page
|
||||
{
|
||||
return $this->model ??= Page::factory([
|
||||
'slug' => 'new',
|
||||
'template' => $this->template,
|
||||
'model' => $this->template,
|
||||
'parent' => $this->parent instanceof Page ? $this->parent : null
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates values for title and slug
|
||||
* from template strings from the blueprint
|
||||
*/
|
||||
public function resolveFieldTemplates(array $input): array
|
||||
{
|
||||
$title = $this->blueprint()->create()['title'] ?? null;
|
||||
$slug = $this->blueprint()->create()['slug'] ?? null;
|
||||
|
||||
// create temporary page object
|
||||
// to resolve the template strings
|
||||
$page = new Page([
|
||||
'slug' => 'tmp',
|
||||
'template' => $this->template,
|
||||
'parent' => $this->model(),
|
||||
'content' => $input
|
||||
]);
|
||||
|
||||
if (is_string($title)) {
|
||||
$input['title'] = $page->toSafeString($title);
|
||||
}
|
||||
|
||||
if (is_string($slug)) {
|
||||
$input['slug'] = $page->toSafeString($slug);
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares and cleans up the input data
|
||||
*/
|
||||
public function sanitize(array $input): array
|
||||
{
|
||||
$input['title'] ??= $this->title ?? '';
|
||||
$input['slug'] ??= $this->slug ?? '';
|
||||
|
||||
$input = $this->resolveFieldTemplates($input);
|
||||
$content = ['title' => trim($input['title'])];
|
||||
|
||||
foreach ($this->customFields() as $name => $field) {
|
||||
$content[$name] = $input[$name] ?? null;
|
||||
}
|
||||
|
||||
// create temporary form to sanitize the input
|
||||
// and add default values
|
||||
$form = Form::for($this->model(), ['values' => $content]);
|
||||
|
||||
return [
|
||||
'content' => $form->strings(true),
|
||||
'slug' => $input['slug'],
|
||||
'template' => $this->template,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the dialog form and creates the new page
|
||||
*/
|
||||
public function submit(array $input): array
|
||||
{
|
||||
$input = $this->sanitize($input);
|
||||
$status = $this->blueprint()->create()['status'] ?? 'draft';
|
||||
|
||||
// validate the input before creating the page
|
||||
$this->validate($input, $status);
|
||||
|
||||
$page = $this->parent->createChild($input);
|
||||
|
||||
if ($status !== 'draft') {
|
||||
// grant all permissions as the status is set in the blueprint and
|
||||
// should not be treated as if the user would try to change it
|
||||
$page->kirby()->impersonate(
|
||||
'kirby',
|
||||
fn () => $page->changeStatus($status)
|
||||
);
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'event' => 'page.create'
|
||||
];
|
||||
|
||||
// add redirect, if not explicitly disabled
|
||||
if (($this->blueprint()->create()['redirect'] ?? null) !== false) {
|
||||
$payload['redirect'] = $page->panel()->url(true);
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
public function validate(array $input, string $status = 'draft'): bool
|
||||
{
|
||||
// basic validation
|
||||
PageRules::validateTitleLength($input['content']['title']);
|
||||
PageRules::validateSlugLength($input['slug']);
|
||||
|
||||
// if the page is supposed to be published directly,
|
||||
// ensure that all field validations are met
|
||||
if ($status !== 'draft') {
|
||||
// create temporary form to validate the input
|
||||
$form = Form::for($this->model(), ['values' => $input['content']]);
|
||||
|
||||
if ($form->isInvalid() === true) {
|
||||
throw new InvalidArgumentException([
|
||||
'key' => 'page.changeStatus.incomplete'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function value(): array
|
||||
{
|
||||
$value = [
|
||||
'parent' => $this->parentId,
|
||||
'section' => $this->sectionId,
|
||||
'slug' => $this->slug ?? '',
|
||||
'template' => $this->template,
|
||||
'title' => $this->title ?? '',
|
||||
'view' => $this->viewId,
|
||||
];
|
||||
|
||||
// add default values for custom fields
|
||||
foreach ($this->customFields() as $name => $field) {
|
||||
if ($default = $field['default'] ?? null) {
|
||||
$value[$name] = $default;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
593
kirby/src/Panel/Panel.php
Normal file
593
kirby/src/Panel/Panel.php
Normal file
@@ -0,0 +1,593 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Closure;
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Url as CmsUrl;
|
||||
use Kirby\Cms\User;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Exception\NotFoundException;
|
||||
use Kirby\Exception\PermissionException;
|
||||
use Kirby\Http\Response;
|
||||
use Kirby\Http\Router;
|
||||
use Kirby\Http\Uri;
|
||||
use Kirby\Http\Url;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Kirby\Toolkit\Tpl;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The Panel class is only responsible to create
|
||||
* a working panel view with all the right URLs
|
||||
* and other panel options. The view template is
|
||||
* located in `kirby/views/panel.php`
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Panel
|
||||
{
|
||||
/**
|
||||
* Normalize a panel area
|
||||
*/
|
||||
public static function area(string $id, array $area): array
|
||||
{
|
||||
$area['id'] = $id;
|
||||
$area['label'] ??= $id;
|
||||
$area['breadcrumb'] ??= [];
|
||||
$area['breadcrumbLabel'] ??= $area['label'];
|
||||
$area['title'] = $area['label'];
|
||||
$area['menu'] ??= false;
|
||||
$area['link'] ??= $id;
|
||||
$area['search'] ??= null;
|
||||
|
||||
return $area;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all registered areas
|
||||
*/
|
||||
public static function areas(): array
|
||||
{
|
||||
$kirby = App::instance();
|
||||
$system = $kirby->system();
|
||||
$user = $kirby->user();
|
||||
$areas = $kirby->load()->areas();
|
||||
|
||||
// the system is not ready
|
||||
if (
|
||||
$system->isOk() === false ||
|
||||
$system->isInstalled() === false
|
||||
) {
|
||||
return [
|
||||
'installation' => static::area(
|
||||
'installation',
|
||||
$areas['installation']
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
// not yet authenticated
|
||||
if (!$user) {
|
||||
return [
|
||||
'logout' => static::area('logout', $areas['logout']),
|
||||
// login area last because it defines a fallback route
|
||||
'login' => static::area('login', $areas['login']),
|
||||
];
|
||||
}
|
||||
|
||||
unset($areas['installation'], $areas['login']);
|
||||
|
||||
// Disable the language area for single-language installations
|
||||
// This does not check for installed languages. Otherwise you'd
|
||||
// not be able to add the first language through the view
|
||||
if (!$kirby->option('languages')) {
|
||||
unset($areas['languages']);
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($areas as $id => $area) {
|
||||
$result[$id] = static::area($id, $area);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for access permissions
|
||||
*/
|
||||
public static function firewall(
|
||||
User|null $user = null,
|
||||
string|null $areaId = null
|
||||
): bool {
|
||||
// a user has to be logged in
|
||||
if ($user === null) {
|
||||
throw new PermissionException(['key' => 'access.panel']);
|
||||
}
|
||||
|
||||
// get all access permissions for the user role
|
||||
$permissions = $user->role()->permissions()->toArray()['access'];
|
||||
|
||||
// check for general panel access
|
||||
if (($permissions['panel'] ?? true) !== true) {
|
||||
throw new PermissionException(['key' => 'access.panel']);
|
||||
}
|
||||
|
||||
// don't check if the area is not defined
|
||||
if (empty($areaId) === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// undefined area permissions means access
|
||||
if (isset($permissions[$areaId]) === false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// no access
|
||||
if ($permissions[$areaId] !== true) {
|
||||
throw new PermissionException(['key' => 'access.view']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Redirect to a Panel url
|
||||
*
|
||||
* @throws \Kirby\Panel\Redirect
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public static function go(string|null $url = null, int $code = 302): void
|
||||
{
|
||||
throw new Redirect(static::url($url), $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given user has access to the panel
|
||||
* or to a given area
|
||||
*/
|
||||
public static function hasAccess(
|
||||
User|null $user = null,
|
||||
string|null $area = null
|
||||
): bool {
|
||||
try {
|
||||
static::firewall($user, $area);
|
||||
return true;
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a Fiber request
|
||||
* via get parameters or headers
|
||||
*/
|
||||
public static function isFiberRequest(): bool
|
||||
{
|
||||
$request = App::instance()->request();
|
||||
|
||||
if ($request->method() === 'GET') {
|
||||
return
|
||||
(bool)($request->get('_json') ??
|
||||
$request->header('X-Fiber'));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JSON response
|
||||
* for Fiber calls
|
||||
*/
|
||||
public static function json(array $data, int $code = 200): Response
|
||||
{
|
||||
$request = App::instance()->request();
|
||||
|
||||
return Response::json($data, $code, $request->get('_pretty'), [
|
||||
'X-Fiber' => 'true',
|
||||
'Cache-Control' => 'no-store, private'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a multilanguage installation
|
||||
*/
|
||||
public static function multilang(): bool
|
||||
{
|
||||
// multilang setup check
|
||||
$kirby = App::instance();
|
||||
return $kirby->option('languages') || $kirby->multilang();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the referrer path if present
|
||||
*/
|
||||
public static function referrer(): string
|
||||
{
|
||||
$request = App::instance()->request();
|
||||
|
||||
$referrer = $request->header('X-Fiber-Referrer')
|
||||
?? $request->get('_referrer')
|
||||
?? '';
|
||||
|
||||
return '/' . trim($referrer, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Response object from the result of
|
||||
* a Panel route call
|
||||
*/
|
||||
public static function response($result, array $options = []): Response
|
||||
{
|
||||
// pass responses directly down to the Kirby router
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// interpret missing/empty results as not found
|
||||
if ($result === null || $result === false) {
|
||||
$result = new NotFoundException('The data could not be found');
|
||||
|
||||
// interpret strings as errors
|
||||
} elseif (is_string($result) === true) {
|
||||
$result = new Exception($result);
|
||||
}
|
||||
|
||||
// handle different response types (view, dialog, ...)
|
||||
return match ($options['type'] ?? null) {
|
||||
'dialog' => Dialog::response($result, $options),
|
||||
'drawer' => Drawer::response($result, $options),
|
||||
'dropdown' => Dropdown::response($result, $options),
|
||||
'request' => Request::response($result, $options),
|
||||
'search' => Search::response($result, $options),
|
||||
default => View::response($result, $options)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Router for the Panel views
|
||||
*/
|
||||
public static function router(string|null $path = null): Response|null
|
||||
{
|
||||
$kirby = App::instance();
|
||||
|
||||
if ($kirby->option('panel') === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// set the translation for Panel UI before
|
||||
// gathering areas and routes, so that the
|
||||
// `t()` helper can already be used
|
||||
static::setTranslation();
|
||||
|
||||
// set the language in multi-lang installations
|
||||
static::setLanguage();
|
||||
|
||||
$areas = static::areas();
|
||||
$routes = static::routes($areas);
|
||||
|
||||
// create a micro-router for the Panel
|
||||
return Router::execute($path, $method = $kirby->request()->method(), $routes, function ($route) use ($areas, $kirby, $method, $path) {
|
||||
// route needs authentication?
|
||||
$auth = $route->attributes()['auth'] ?? true;
|
||||
$areaId = $route->attributes()['area'] ?? null;
|
||||
$type = $route->attributes()['type'] ?? 'view';
|
||||
$area = $areas[$areaId] ?? null;
|
||||
|
||||
// call the route action to check the result
|
||||
try {
|
||||
// trigger hook
|
||||
$route = $kirby->apply(
|
||||
'panel.route:before',
|
||||
compact('route', 'path', 'method'),
|
||||
'route'
|
||||
);
|
||||
|
||||
// check for access before executing area routes
|
||||
if ($auth !== false) {
|
||||
static::firewall($kirby->user(), $areaId);
|
||||
}
|
||||
|
||||
$result = $route->action()->call($route, ...$route->arguments());
|
||||
} catch (Throwable $e) {
|
||||
$result = $e;
|
||||
}
|
||||
|
||||
$response = static::response($result, [
|
||||
'area' => $area,
|
||||
'areas' => $areas,
|
||||
'path' => $path,
|
||||
'type' => $type
|
||||
]);
|
||||
|
||||
return $kirby->apply(
|
||||
'panel.route:after',
|
||||
compact('route', 'path', 'method', 'response'),
|
||||
'response'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the routes from the given array
|
||||
* of active areas.
|
||||
*/
|
||||
public static function routes(array $areas): array
|
||||
{
|
||||
$kirby = App::instance();
|
||||
|
||||
// the browser incompatibility
|
||||
// warning is always needed
|
||||
$routes = [
|
||||
[
|
||||
'pattern' => 'browser',
|
||||
'auth' => false,
|
||||
'action' => fn () => new Response(
|
||||
Tpl::load($kirby->root('kirby') . '/views/browser.php')
|
||||
),
|
||||
]
|
||||
];
|
||||
|
||||
// register all routes from areas
|
||||
foreach ($areas as $areaId => $area) {
|
||||
$routes = array_merge(
|
||||
$routes,
|
||||
static::routesForViews($areaId, $area),
|
||||
static::routesForSearches($areaId, $area),
|
||||
static::routesForDialogs($areaId, $area),
|
||||
static::routesForDrawers($areaId, $area),
|
||||
static::routesForDropdowns($areaId, $area),
|
||||
static::routesForRequests($areaId, $area),
|
||||
);
|
||||
}
|
||||
|
||||
// if the Panel is already installed and/or the
|
||||
// user is authenticated, those areas won't be
|
||||
// included, which is why we add redirect routes
|
||||
// to main Panel view as fallbacks
|
||||
$routes[] = [
|
||||
'pattern' => [
|
||||
'/',
|
||||
'installation',
|
||||
'login',
|
||||
],
|
||||
'action' => fn () => Panel::go(Home::url()),
|
||||
'auth' => false
|
||||
];
|
||||
|
||||
// catch all route
|
||||
$routes[] = [
|
||||
'pattern' => '(:all)',
|
||||
'action' => fn (string $pattern) => 'Could not find Panel view for route: ' . $pattern
|
||||
];
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all routes from an area
|
||||
*/
|
||||
public static function routesForDialogs(string $areaId, array $area): array
|
||||
{
|
||||
$dialogs = $area['dialogs'] ?? [];
|
||||
$routes = [];
|
||||
|
||||
foreach ($dialogs as $dialogId => $dialog) {
|
||||
$routes = array_merge($routes, Dialog::routes(
|
||||
id: $dialogId,
|
||||
areaId: $areaId,
|
||||
prefix: 'dialogs',
|
||||
options: $dialog
|
||||
));
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all routes from an area
|
||||
*/
|
||||
public static function routesForDrawers(string $areaId, array $area): array
|
||||
{
|
||||
$drawers = $area['drawers'] ?? [];
|
||||
$routes = [];
|
||||
|
||||
foreach ($drawers as $drawerId => $drawer) {
|
||||
$routes = array_merge($routes, Drawer::routes(
|
||||
id: $drawerId,
|
||||
areaId: $areaId,
|
||||
prefix: 'drawers',
|
||||
options: $drawer
|
||||
));
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all routes for dropdowns
|
||||
*/
|
||||
public static function routesForDropdowns(string $areaId, array $area): array
|
||||
{
|
||||
$dropdowns = $area['dropdowns'] ?? [];
|
||||
$routes = [];
|
||||
|
||||
foreach ($dropdowns as $dropdownId => $dropdown) {
|
||||
$routes = array_merge($routes, Dropdown::routes(
|
||||
id: $dropdownId,
|
||||
areaId: $areaId,
|
||||
prefix: 'dropdowns',
|
||||
options: $dropdown
|
||||
));
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all routes from an area
|
||||
*/
|
||||
public static function routesForRequests(string $areaId, array $area): array
|
||||
{
|
||||
$routes = $area['requests'] ?? [];
|
||||
|
||||
foreach ($routes as $key => $route) {
|
||||
$routes[$key]['area'] = $areaId;
|
||||
$routes[$key]['type'] = 'request';
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all routes for searches
|
||||
*/
|
||||
public static function routesForSearches(string $areaId, array $area): array
|
||||
{
|
||||
$searches = $area['searches'] ?? [];
|
||||
$routes = [];
|
||||
|
||||
foreach ($searches as $name => $params) {
|
||||
// create the full routing pattern
|
||||
$pattern = 'search/' . $name;
|
||||
|
||||
// load event
|
||||
$routes[] = [
|
||||
'pattern' => $pattern,
|
||||
'type' => 'search',
|
||||
'area' => $areaId,
|
||||
'action' => function () use ($params) {
|
||||
$kirby = App::instance();
|
||||
$request = $kirby->request();
|
||||
$query = $request->get('query');
|
||||
$limit = (int)$request->get('limit', $kirby->option('panel.search.limit', 10));
|
||||
$page = (int)$request->get('page', 1);
|
||||
|
||||
return $params['query']($query, $limit, $page);
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract all views from an area
|
||||
*/
|
||||
public static function routesForViews(string $areaId, array $area): array
|
||||
{
|
||||
$views = $area['views'] ?? [];
|
||||
$routes = [];
|
||||
|
||||
foreach ($views as $view) {
|
||||
$view['area'] = $areaId;
|
||||
$view['type'] = 'view';
|
||||
|
||||
$when = $view['when'] ?? null;
|
||||
unset($view['when']);
|
||||
|
||||
// enable the route by default, but if there is a
|
||||
// when condition closure, it must return `true`
|
||||
if (
|
||||
$when instanceof Closure === false ||
|
||||
$when($view, $area) === true
|
||||
) {
|
||||
$routes[] = $view;
|
||||
}
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current language in multi-lang
|
||||
* installations based on the session or the
|
||||
* query language query parameter
|
||||
*/
|
||||
public static function setLanguage(): string|null
|
||||
{
|
||||
$kirby = App::instance();
|
||||
|
||||
// language switcher
|
||||
if (static::multilang()) {
|
||||
$fallback = 'en';
|
||||
|
||||
if ($defaultLanguage = $kirby->defaultLanguage()) {
|
||||
$fallback = $defaultLanguage->code();
|
||||
}
|
||||
|
||||
$session = $kirby->session();
|
||||
$sessionLanguage = $session->get('panel.language', $fallback);
|
||||
$language = $kirby->request()->get('language') ?? $sessionLanguage;
|
||||
|
||||
// keep the language for the next visit
|
||||
if ($language !== $sessionLanguage) {
|
||||
$session->set('panel.language', $language);
|
||||
}
|
||||
|
||||
// activate the current language in Kirby
|
||||
$kirby->setCurrentLanguage($language);
|
||||
|
||||
return $language;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the currently active Panel translation
|
||||
* based on the current user or config
|
||||
*/
|
||||
public static function setTranslation(): string
|
||||
{
|
||||
$kirby = App::instance();
|
||||
|
||||
// use the user language for the default translation or
|
||||
// fall back to the language from the config
|
||||
$translation = $kirby->user()?->language() ??
|
||||
$kirby->panelLanguage();
|
||||
|
||||
$kirby->setCurrentTranslation($translation);
|
||||
|
||||
return $translation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an absolute Panel URL
|
||||
* independent of the Panel slug config
|
||||
*/
|
||||
public static function url(string|null $url = null, array $options = []): string
|
||||
{
|
||||
// only touch relative paths
|
||||
if (Url::isAbsolute($url) === false) {
|
||||
$kirby = App::instance();
|
||||
$slug = $kirby->option('panel.slug', 'panel');
|
||||
$path = trim($url, '/');
|
||||
|
||||
$baseUri = new Uri($kirby->url());
|
||||
$basePath = trim($baseUri->path()->toString(), '/');
|
||||
|
||||
// removes base path if relative path contains it
|
||||
if (empty($basePath) === false && Str::startsWith($path, $basePath) === true) {
|
||||
$path = Str::after($path, $basePath);
|
||||
}
|
||||
// add the panel slug prefix if it it's not
|
||||
// included in the path yet
|
||||
elseif (Str::startsWith($path, $slug . '/') === false) {
|
||||
$path = $slug . '/' . $path;
|
||||
}
|
||||
|
||||
// create an absolute URL
|
||||
$url = CmsUrl::to($path, $options);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
139
kirby/src/Panel/Plugins.php
Normal file
139
kirby/src/Panel/Plugins.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Data\Json;
|
||||
use Kirby\Filesystem\F;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
/**
|
||||
* The Plugins class takes care of collecting
|
||||
* js and css plugin files for the panel and caches
|
||||
* them in the media folder
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Plugins
|
||||
{
|
||||
/**
|
||||
* Cache of all collected plugin files
|
||||
*/
|
||||
public array|null $files = null;
|
||||
|
||||
/**
|
||||
* Collects and returns the plugin files for all plugins
|
||||
*/
|
||||
public function files(): array
|
||||
{
|
||||
if ($this->files !== null) {
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
$this->files = [];
|
||||
|
||||
foreach (App::instance()->plugins() as $plugin) {
|
||||
$this->files[] = $plugin->root() . '/index.css';
|
||||
$this->files[] = $plugin->root() . '/index.js';
|
||||
// During plugin development, kirbyup adds an index.dev.mjs as entry point, which
|
||||
// Kirby will load instead of the regular index.js. Since kirbyup is based on Vite,
|
||||
// it can't use the standard index.js as entry for its development server:
|
||||
// Vite requires an entry of type module so it can use JavaScript imports,
|
||||
// but Kirbyup needs index.js to load as a regular script, synchronously.
|
||||
$this->files[] = $plugin->root() . '/index.dev.mjs';
|
||||
}
|
||||
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last modification
|
||||
* of the collected plugin files
|
||||
*/
|
||||
public function modified(): int
|
||||
{
|
||||
$files = $this->files();
|
||||
$modified = [0];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$modified[] = F::modified($file);
|
||||
}
|
||||
|
||||
return max($modified);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the files from all plugins and concatenate them
|
||||
*/
|
||||
public function read(string $type): string
|
||||
{
|
||||
$dist = [];
|
||||
|
||||
foreach ($this->files() as $file) {
|
||||
// filter out files with a different type
|
||||
if (F::extension($file) !== $type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// filter out empty files and files that don't exist
|
||||
$content = F::read($file);
|
||||
if (!$content) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($type === 'mjs') {
|
||||
// index.dev.mjs files are turned into data URIs so they
|
||||
// can be imported without having to copy them to /media
|
||||
// (avoids having to clean the files from /media again)
|
||||
$content = F::uri($file);
|
||||
}
|
||||
|
||||
if ($type === 'js') {
|
||||
// filter out all index.js files that shouldn't be loaded
|
||||
// because an index.dev.mjs exists
|
||||
if (F::exists(preg_replace('/\.js$/', '.dev.mjs', $file)) === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$content = trim($content);
|
||||
|
||||
// make sure that each plugin is ended correctly
|
||||
if (Str::endsWith($content, ';') === false) {
|
||||
$content .= ';';
|
||||
}
|
||||
}
|
||||
|
||||
$dist[] = $content;
|
||||
}
|
||||
|
||||
if ($type === 'mjs') {
|
||||
// if no index.dev.mjs modules exist, we MUST return an empty string instead
|
||||
// of loading an empty array; this is because the module loader code uses
|
||||
// top level await, which is not compatible with Kirby's minimum browser
|
||||
// version requirements and therefore must not appear in a default setup
|
||||
if (empty($dist)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$modules = Json::encode($dist);
|
||||
$modulePromise = "Promise.all($modules.map(url => import(url)))";
|
||||
return "try { await $modulePromise } catch (e) { console.error(e) }" . PHP_EOL;
|
||||
}
|
||||
|
||||
return implode(PHP_EOL . PHP_EOL, $dist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Absolute url to the cache file
|
||||
* This is used by the panel to link the plugins
|
||||
*/
|
||||
public function url(string $type): string
|
||||
{
|
||||
return App::instance()->url('media') . '/plugins/index.' . $type . '?' . $this->modified();
|
||||
}
|
||||
}
|
||||
42
kirby/src/Panel/Redirect.php
Normal file
42
kirby/src/Panel/Redirect.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* The Redirect exception can be thrown in all Fiber
|
||||
* routes to send a redirect response. It is
|
||||
* primarily used in `Panel::go($location)`
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Redirect extends Exception
|
||||
{
|
||||
/**
|
||||
* Returns the HTTP code for the redirect
|
||||
*/
|
||||
public function code(): int
|
||||
{
|
||||
$codes = [301, 302, 303, 307, 308];
|
||||
|
||||
if (in_array($this->getCode(), $codes) === true) {
|
||||
return $this->getCode();
|
||||
}
|
||||
|
||||
return 302;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for the redirect
|
||||
*/
|
||||
public function location(): string
|
||||
{
|
||||
return $this->getMessage();
|
||||
}
|
||||
}
|
||||
24
kirby/src/Panel/Request.php
Normal file
24
kirby/src/Panel/Request.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Http\Response;
|
||||
|
||||
/**
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
/**
|
||||
* Renders request responses
|
||||
*/
|
||||
public static function response($data, array $options = []): Response
|
||||
{
|
||||
$data = Json::responseData($data);
|
||||
return Panel::json($data, $data['code'] ?? 200);
|
||||
}
|
||||
}
|
||||
41
kirby/src/Panel/Search.php
Normal file
41
kirby/src/Panel/Search.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Http\Response;
|
||||
|
||||
/**
|
||||
* The Search response class handles Fiber
|
||||
* requests to render the JSON object for
|
||||
* search queries
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Search extends Json
|
||||
{
|
||||
protected static string $key = '$search';
|
||||
|
||||
public static function response($data, array $options = []): Response
|
||||
{
|
||||
if (
|
||||
is_array($data) === true &&
|
||||
array_key_exists('results', $data) === false
|
||||
) {
|
||||
$data = [
|
||||
'results' => $data,
|
||||
'pagination' => [
|
||||
'page' => 1,
|
||||
'limit' => $total = count($data),
|
||||
'total' => $total
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
return parent::response($data, $options);
|
||||
}
|
||||
}
|
||||
91
kirby/src/Panel/Site.php
Normal file
91
kirby/src/Panel/Site.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\File as CmsFile;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Filesystem\Asset;
|
||||
|
||||
/**
|
||||
* Provides information about the site model for the Panel
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class Site extends Model
|
||||
{
|
||||
/**
|
||||
* @var \Kirby\Cms\Site
|
||||
*/
|
||||
protected ModelWithContent $model;
|
||||
|
||||
/**
|
||||
* Returns the setup for a dropdown option
|
||||
* which is used in the changes dropdown
|
||||
* for example.
|
||||
*/
|
||||
public function dropdownOption(): array
|
||||
{
|
||||
return [
|
||||
'icon' => 'home',
|
||||
'text' => $this->model->title()->value(),
|
||||
] + parent::dropdownOption();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image file object based on provided query
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function imageSource(
|
||||
string|null $query = null
|
||||
): CmsFile|Asset|null {
|
||||
$query ??= 'site.image';
|
||||
return parent::imageSource($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path without leading slash
|
||||
*/
|
||||
public function path(): string
|
||||
{
|
||||
return 'site';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for the
|
||||
* view's component props
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function props(): array
|
||||
{
|
||||
return array_merge(parent::props(), [
|
||||
'blueprint' => 'site',
|
||||
'model' => [
|
||||
'content' => $this->content(),
|
||||
'link' => $this->url(true),
|
||||
'previewUrl' => $this->model->previewUrl(),
|
||||
'title' => $this->model->title()->toString(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for
|
||||
* this model's Panel view
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function view(): array
|
||||
{
|
||||
return [
|
||||
'component' => 'k-site-view',
|
||||
'props' => $this->props()
|
||||
];
|
||||
}
|
||||
}
|
||||
271
kirby/src/Panel/User.php
Normal file
271
kirby/src/Panel/User.php
Normal file
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\File as CmsFile;
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Cms\Translation;
|
||||
use Kirby\Cms\Url;
|
||||
use Kirby\Filesystem\Asset;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
/**
|
||||
* Provides information about the user model for the Panel
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class User extends Model
|
||||
{
|
||||
/**
|
||||
* @var \Kirby\Cms\User
|
||||
*/
|
||||
protected ModelWithContent $model;
|
||||
|
||||
/**
|
||||
* Breadcrumb array
|
||||
*/
|
||||
public function breadcrumb(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'label' => $this->model->username(),
|
||||
'link' => $this->url(true),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides options for the user dropdown
|
||||
*/
|
||||
public function dropdown(array $options = []): array
|
||||
{
|
||||
$account = $this->model->isLoggedIn();
|
||||
$i18nPrefix = $account ? 'account' : 'user';
|
||||
$permissions = $this->options(['preview']);
|
||||
$url = $this->url(true);
|
||||
$result = [];
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeName',
|
||||
'icon' => 'title',
|
||||
'text' => I18n::translate($i18nPrefix . '.changeName'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeName', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = '-';
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeEmail',
|
||||
'icon' => 'email',
|
||||
'text' => I18n::translate('user.changeEmail'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeEmail', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeRole',
|
||||
'icon' => 'bolt',
|
||||
'text' => I18n::translate('user.changeRole'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeRole', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changeLanguage',
|
||||
'icon' => 'translate',
|
||||
'text' => I18n::translate('user.changeLanguage'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changeLanguage', $options, $permissions)
|
||||
];
|
||||
|
||||
$result[] = '-';
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/changePassword',
|
||||
'icon' => 'key',
|
||||
'text' => I18n::translate('user.changePassword'),
|
||||
'disabled' => $this->isDisabledDropdownOption('changePassword', $options, $permissions)
|
||||
];
|
||||
|
||||
if ($this->model->kirby()->system()->is2FAWithTOTP() === true) {
|
||||
if ($account || $this->model->kirby()->user()->isAdmin()) {
|
||||
if ($this->model->secret('totp') !== null) {
|
||||
$result[] = [
|
||||
'dialog' => $url . '/totp/disable',
|
||||
'icon' => 'qr-code',
|
||||
'text' => I18n::translate('login.totp.disable.option'),
|
||||
];
|
||||
} elseif ($account) {
|
||||
$result[] = [
|
||||
'dialog' => $url . '/totp/enable',
|
||||
'icon' => 'qr-code',
|
||||
'text' => I18n::translate('login.totp.enable.option')
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result[] = '-';
|
||||
|
||||
$result[] = [
|
||||
'dialog' => $url . '/delete',
|
||||
'icon' => 'trash',
|
||||
'text' => I18n::translate($i18nPrefix . '.delete'),
|
||||
'disabled' => $this->isDisabledDropdownOption('delete', $options, $permissions)
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setup for a dropdown option
|
||||
* which is used in the changes dropdown
|
||||
* for example.
|
||||
*/
|
||||
public function dropdownOption(): array
|
||||
{
|
||||
return [
|
||||
'icon' => 'user',
|
||||
'text' => $this->model->username(),
|
||||
] + parent::dropdownOption();
|
||||
}
|
||||
|
||||
public function home(): string|null
|
||||
{
|
||||
if ($home = ($this->model->blueprint()->home() ?? null)) {
|
||||
$url = $this->model->toString($home);
|
||||
return Url::to($url);
|
||||
}
|
||||
|
||||
return Panel::url('site');
|
||||
}
|
||||
|
||||
/**
|
||||
* Default settings for the user's Panel image
|
||||
*/
|
||||
protected function imageDefaults(): array
|
||||
{
|
||||
return array_merge(parent::imageDefaults(), [
|
||||
'back' => 'black',
|
||||
'icon' => 'user',
|
||||
'ratio' => '1/1',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the image file object based on provided query
|
||||
* @internal
|
||||
*/
|
||||
protected function imageSource(
|
||||
string|null $query = null
|
||||
): CmsFile|Asset|null {
|
||||
if ($query === null) {
|
||||
return $this->model->avatar();
|
||||
}
|
||||
|
||||
return parent::imageSource($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path without leading slash
|
||||
*/
|
||||
public function path(): string
|
||||
{
|
||||
// path to your own account
|
||||
if ($this->model->isLoggedIn() === true) {
|
||||
return 'account';
|
||||
}
|
||||
|
||||
return 'users/' . $this->model->id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns prepared data for the panel user picker
|
||||
*/
|
||||
public function pickerData(array $params = []): array
|
||||
{
|
||||
$params['text'] ??= '{{ user.username }}';
|
||||
|
||||
return array_merge(parent::pickerData($params), [
|
||||
'email' => $this->model->email(),
|
||||
'username' => $this->model->username(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns navigation array with
|
||||
* previous and next user
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function prevNext(): array
|
||||
{
|
||||
$user = $this->model;
|
||||
|
||||
return [
|
||||
'next' => fn () => $this->toPrevNextLink($user->next(), 'username'),
|
||||
'prev' => fn () => $this->toPrevNextLink($user->prev(), 'username')
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for the
|
||||
* view's component props
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function props(): array
|
||||
{
|
||||
$user = $this->model;
|
||||
$account = $user->isLoggedIn();
|
||||
|
||||
return array_merge(
|
||||
parent::props(),
|
||||
$account ? [] : $this->prevNext(),
|
||||
[
|
||||
'blueprint' => $this->model->role()->name(),
|
||||
'model' => [
|
||||
'account' => $account,
|
||||
'avatar' => $user->avatar()?->url(),
|
||||
'content' => $this->content(),
|
||||
'email' => $user->email(),
|
||||
'id' => $user->id(),
|
||||
'language' => $this->translation()->name(),
|
||||
'link' => $this->url(true),
|
||||
'name' => $user->name()->toString(),
|
||||
'role' => $user->role()->title(),
|
||||
'username' => $user->username(),
|
||||
]
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Translation object
|
||||
* for the selected Panel language
|
||||
*/
|
||||
public function translation(): Translation
|
||||
{
|
||||
$kirby = $this->model->kirby();
|
||||
$lang = $this->model->language();
|
||||
return $kirby->translation($lang);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data array for
|
||||
* this model's Panel view
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function view(): array
|
||||
{
|
||||
return [
|
||||
'breadcrumb' => $this->breadcrumb(),
|
||||
'component' => 'k-user-view',
|
||||
'props' => $this->props(),
|
||||
'title' => $this->model->username(),
|
||||
];
|
||||
}
|
||||
}
|
||||
114
kirby/src/Panel/UserTotpDisableDialog.php
Normal file
114
kirby/src/Panel/UserTotpDisableDialog.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Find;
|
||||
use Kirby\Cms\User;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Exception\PermissionException;
|
||||
use Kirby\Toolkit\Escape;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
/**
|
||||
* Manages the Panel dialog to disable TOTP auth for a user
|
||||
* @since 4.0.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class UserTotpDisableDialog
|
||||
{
|
||||
public App $kirby;
|
||||
public User $user;
|
||||
|
||||
public function __construct(
|
||||
string|null $id = null
|
||||
) {
|
||||
$this->kirby = App::instance();
|
||||
$this->user = $id ? Find::user($id) : $this->kirby->user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Panel dialog state when opening the dialog
|
||||
*/
|
||||
public function load(): array
|
||||
{
|
||||
$currentUser = $this->kirby->user();
|
||||
$submitBtn = [
|
||||
'text' => I18n::translate('disable'),
|
||||
'icon' => 'protected',
|
||||
'theme' => 'negative'
|
||||
];
|
||||
|
||||
// admins can disable TOTP for other users without
|
||||
// entering their password (but not for themselves)
|
||||
if (
|
||||
$currentUser->isAdmin() === true &&
|
||||
$currentUser->is($this->user) === false
|
||||
) {
|
||||
$name = $this->user->name()->or($this->user->email());
|
||||
|
||||
return [
|
||||
'component' => 'k-remove-dialog',
|
||||
'props' => [
|
||||
'text' => I18n::template('login.totp.disable.admin', ['user' => Escape::html($name)]),
|
||||
'submitButton' => $submitBtn,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
// everybody else
|
||||
return [
|
||||
'component' => 'k-form-dialog',
|
||||
'props' => [
|
||||
'fields' => [
|
||||
'password' => [
|
||||
'type' => 'password',
|
||||
'required' => true,
|
||||
'counter' => false,
|
||||
'label' => I18n::translate('login.totp.disable.label'),
|
||||
'help' => I18n::translate('login.totp.disable.help'),
|
||||
]
|
||||
],
|
||||
'submitButton' => $submitBtn,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the user's TOTP secret when the dialog is submitted
|
||||
*/
|
||||
public function submit(): array
|
||||
{
|
||||
$password = $this->kirby->request()->get('password');
|
||||
|
||||
try {
|
||||
if ($this->kirby->user()->is($this->user) === true) {
|
||||
$this->user->validatePassword($password);
|
||||
} elseif ($this->kirby->user()->isAdmin() === false) {
|
||||
throw new PermissionException('You are not allowed to disable TOTP for other users');
|
||||
}
|
||||
|
||||
// Remove the TOTP secret from the account
|
||||
$this->user->changeTotp(null);
|
||||
|
||||
return [
|
||||
'message' => I18n::translate('login.totp.disable.success')
|
||||
];
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// Catch and re-throw exception so that any
|
||||
// Unauthenticated exception for incorrect passwords
|
||||
// does not trigger a logout
|
||||
throw new InvalidArgumentException([
|
||||
'key' => $e->getKey(),
|
||||
'data' => $e->getData(),
|
||||
'fallback' => $e->getMessage(),
|
||||
'previous' => $e
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
95
kirby/src/Panel/UserTotpEnableDialog.php
Normal file
95
kirby/src/Panel/UserTotpEnableDialog.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\User;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Image\QrCode;
|
||||
use Kirby\Toolkit\I18n;
|
||||
use Kirby\Toolkit\Totp;
|
||||
|
||||
/**
|
||||
* Manages the Panel dialog to enable TOTP auth for the current user
|
||||
* @since 4.0.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Nico Hoffmann <nico@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class UserTotpEnableDialog
|
||||
{
|
||||
public App $kirby;
|
||||
public Totp $totp;
|
||||
public User $user;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->kirby = App::instance();
|
||||
$this->user = $this->kirby->user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Panel dialog state when opening the dialog
|
||||
*/
|
||||
public function load(): array
|
||||
{
|
||||
return [
|
||||
'component' => 'k-totp-dialog',
|
||||
'props' => [
|
||||
'qr' => $this->qr()->toSvg(size: '100%'),
|
||||
'value' => ['secret' => $this->secret()]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a QR code with a new TOTP secret for the user
|
||||
*/
|
||||
public function qr(): QrCode
|
||||
{
|
||||
$issuer = $this->kirby->site()->title();
|
||||
$label = $this->user->email();
|
||||
$uri = $this->totp()->uri($issuer, $label);
|
||||
return new QrCode($uri);
|
||||
}
|
||||
|
||||
public function secret(): string
|
||||
{
|
||||
return $this->totp()->secret();
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the user's TOTP secret when the dialog is submitted
|
||||
*/
|
||||
public function submit(): array
|
||||
{
|
||||
$secret = $this->kirby->request()->get('secret');
|
||||
$confirm = $this->kirby->request()->get('confirm');
|
||||
|
||||
if ($confirm === null) {
|
||||
throw new InvalidArgumentException(
|
||||
['key' => 'login.totp.confirm.missing']
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->totp($secret)->verify($confirm) === false) {
|
||||
throw new InvalidArgumentException(
|
||||
['key' => 'login.totp.confirm.invalid']
|
||||
);
|
||||
}
|
||||
|
||||
$this->user->changeTotp($secret);
|
||||
|
||||
return [
|
||||
'message' => I18n::translate('login.totp.enable.success')
|
||||
];
|
||||
}
|
||||
|
||||
public function totp(string|null $secret = null): Totp
|
||||
{
|
||||
return $this->totp ??= new Totp($secret);
|
||||
}
|
||||
}
|
||||
384
kirby/src/Panel/View.php
Normal file
384
kirby/src/Panel/View.php
Normal file
@@ -0,0 +1,384 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Panel;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Http\Response;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The View response class handles Fiber
|
||||
* requests to render either a JSON object
|
||||
* or a full HTML document for Panel views
|
||||
* @since 3.6.0
|
||||
*
|
||||
* @package Kirby Panel
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://getkirby.com/license
|
||||
*/
|
||||
class View
|
||||
{
|
||||
/**
|
||||
* Filters the data array based on headers or
|
||||
* query parameters. Requests can return only
|
||||
* certain data fields that way or globals can
|
||||
* be injected on demand.
|
||||
*/
|
||||
public static function apply(array $data): array
|
||||
{
|
||||
$request = App::instance()->request();
|
||||
$only = $request->header('X-Fiber-Only') ?? $request->get('_only');
|
||||
|
||||
if (empty($only) === false) {
|
||||
return static::applyOnly($data, $only);
|
||||
}
|
||||
|
||||
$globals =
|
||||
$request->header('X-Fiber-Globals') ??
|
||||
$request->get('_globals');
|
||||
|
||||
if (empty($globals) === false) {
|
||||
return static::applyGlobals($data, $globals);
|
||||
}
|
||||
|
||||
return A::apply($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if globals should be included in a JSON Fiber request. They are normally
|
||||
* only loaded with the full document request, but sometimes need to be updated.
|
||||
*
|
||||
* A global request can be activated with the `X-Fiber-Globals` header or the
|
||||
* `_globals` query parameter.
|
||||
*/
|
||||
public static function applyGlobals(
|
||||
array $data,
|
||||
string|null $globals = null
|
||||
): array {
|
||||
// split globals string into an array of fields
|
||||
$globalKeys = Str::split($globals, ',');
|
||||
|
||||
// add requested globals
|
||||
if (empty($globalKeys) === true) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$globals = static::globals();
|
||||
|
||||
foreach ($globalKeys as $globalKey) {
|
||||
if (isset($globals[$globalKey]) === true) {
|
||||
$data[$globalKey] = $globals[$globalKey];
|
||||
}
|
||||
}
|
||||
|
||||
// merge with shared data
|
||||
return A::apply($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the request should only return a limited
|
||||
* set of data. This can be activated with the `X-Fiber-Only`
|
||||
* header or the `_only` query parameter in a request.
|
||||
*
|
||||
* Such requests can fetch shared data or globals.
|
||||
* Globals will be loaded on demand.
|
||||
*/
|
||||
public static function applyOnly(
|
||||
array $data,
|
||||
string|null $only = null
|
||||
): array {
|
||||
// split include string into an array of fields
|
||||
$onlyKeys = Str::split($only, ',');
|
||||
|
||||
// if a full request is made, return all data
|
||||
if (empty($onlyKeys) === true) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
// otherwise filter data based on
|
||||
// dot notation, e.g. `$props.tab.columns`
|
||||
$result = [];
|
||||
|
||||
// check if globals are requested and need to be merged
|
||||
if (Str::contains($only, '$')) {
|
||||
$data = array_merge_recursive(static::globals(), $data);
|
||||
}
|
||||
|
||||
// make sure the data is already resolved to make
|
||||
// nested data fetching work
|
||||
$data = A::apply($data);
|
||||
|
||||
// build a new array with all requested data
|
||||
foreach ($onlyKeys as $onlyKey) {
|
||||
$result[$onlyKey] = A::get($data, $onlyKey);
|
||||
}
|
||||
|
||||
// Nest dotted keys in array but ignore $translation
|
||||
return A::nest($result, ['$translation']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the shared data array for the individual views
|
||||
* The full shared data is always sent on every JSON and
|
||||
* full document request unless the `X-Fiber-Only` header or
|
||||
* the `_only` query parameter is set.
|
||||
*/
|
||||
public static function data(array $view = [], array $options = []): array
|
||||
{
|
||||
$kirby = App::instance();
|
||||
|
||||
// multilang setup check
|
||||
$multilang = Panel::multilang();
|
||||
|
||||
// get the authenticated user
|
||||
$user = $kirby->user();
|
||||
|
||||
// user permissions
|
||||
$permissions = $user?->role()->permissions()->toArray() ?? [];
|
||||
|
||||
// current content language
|
||||
$language = $kirby->language();
|
||||
|
||||
// shared data for all requests
|
||||
return [
|
||||
'$direction' => function () use ($kirby, $multilang, $language, $user) {
|
||||
if ($multilang === true && $language && $user) {
|
||||
$default = $kirby->defaultLanguage();
|
||||
|
||||
if (
|
||||
$language->direction() !== $default->direction() &&
|
||||
$language->code() !== $user->language()
|
||||
) {
|
||||
return $language->direction();
|
||||
}
|
||||
}
|
||||
},
|
||||
'$dialog' => null,
|
||||
'$drawer' => null,
|
||||
'$language' => function () use ($kirby, $multilang, $language) {
|
||||
if ($multilang === true && $language) {
|
||||
return [
|
||||
'code' => $language->code(),
|
||||
'default' => $language->isDefault(),
|
||||
'direction' => $language->direction(),
|
||||
'name' => $language->name(),
|
||||
'rules' => $language->rules(),
|
||||
];
|
||||
}
|
||||
},
|
||||
'$languages' => function () use ($kirby, $multilang): array {
|
||||
if ($multilang === true) {
|
||||
return $kirby->languages()->values(fn ($language) => [
|
||||
'code' => $language->code(),
|
||||
'default' => $language->isDefault(),
|
||||
'direction' => $language->direction(),
|
||||
'name' => $language->name(),
|
||||
'rules' => $language->rules(),
|
||||
]);
|
||||
}
|
||||
|
||||
return [];
|
||||
},
|
||||
'$menu' => function () use ($options, $permissions) {
|
||||
$menu = new Menu(
|
||||
$options['areas'] ?? [],
|
||||
$permissions,
|
||||
$options['area']['id'] ?? null
|
||||
);
|
||||
return $menu->entries();
|
||||
},
|
||||
'$permissions' => $permissions,
|
||||
'$license' => $kirby->system()->license()->status()->value(),
|
||||
'$multilang' => $multilang,
|
||||
'$searches' => static::searches($options['areas'] ?? [], $permissions),
|
||||
'$url' => $kirby->request()->url()->toString(),
|
||||
'$user' => function () use ($user) {
|
||||
if ($user) {
|
||||
return [
|
||||
'email' => $user->email(),
|
||||
'id' => $user->id(),
|
||||
'language' => $user->language(),
|
||||
'role' => $user->role()->id(),
|
||||
'username' => $user->username(),
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
'$view' => function () use ($kirby, $options, $view) {
|
||||
$defaults = [
|
||||
'breadcrumb' => [],
|
||||
'code' => 200,
|
||||
'path' => Str::after($kirby->path(), '/'),
|
||||
'props' => [],
|
||||
'query' => App::instance()->request()->query()->toArray(),
|
||||
'referrer' => Panel::referrer(),
|
||||
'search' => $kirby->option('panel.search.type', 'pages'),
|
||||
'timestamp' => (int)(microtime(true) * 1000),
|
||||
];
|
||||
|
||||
$view = array_replace_recursive(
|
||||
$defaults,
|
||||
$options['area'] ?? [],
|
||||
$view
|
||||
);
|
||||
|
||||
// make sure that views and dialogs are gone
|
||||
unset(
|
||||
$view['dialogs'],
|
||||
$view['drawers'],
|
||||
$view['dropdowns'],
|
||||
$view['requests'],
|
||||
$view['searches'],
|
||||
$view['views']
|
||||
);
|
||||
|
||||
// resolve all callbacks in the view array
|
||||
return A::apply($view);
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the error view with provided message
|
||||
*/
|
||||
public static function error(string $message, int $code = 404)
|
||||
{
|
||||
return [
|
||||
'code' => $code,
|
||||
'component' => 'k-error-view',
|
||||
'error' => $message,
|
||||
'props' => [
|
||||
'error' => $message,
|
||||
'layout' => Panel::hasAccess(App::instance()->user()) ? 'inside' : 'outside'
|
||||
],
|
||||
'title' => 'Error'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates global data for the Panel.
|
||||
* This will be injected in the full Panel
|
||||
* view via the script tag. Global data
|
||||
* is only requested once on the first page load.
|
||||
* It can be loaded partially later if needed,
|
||||
* but is otherwise not included in Fiber calls.
|
||||
*/
|
||||
public static function globals(): array
|
||||
{
|
||||
$kirby = App::instance();
|
||||
|
||||
return [
|
||||
'$config' => fn () => [
|
||||
'debug' => $kirby->option('debug', false),
|
||||
'kirbytext' => $kirby->option('panel.kirbytext', true),
|
||||
'translation' => $kirby->option('panel.language', 'en'),
|
||||
],
|
||||
'$system' => function () use ($kirby) {
|
||||
$locales = [];
|
||||
|
||||
foreach ($kirby->translations() as $translation) {
|
||||
$locales[$translation->code()] = $translation->locale();
|
||||
}
|
||||
|
||||
return [
|
||||
'ascii' => Str::$ascii,
|
||||
'csrf' => $kirby->auth()->csrfFromSession(),
|
||||
'isLocal' => $kirby->system()->isLocal(),
|
||||
'locales' => $locales,
|
||||
'slugs' => Str::$language,
|
||||
'title' => $kirby->site()->title()->or('Kirby Panel')->toString()
|
||||
];
|
||||
},
|
||||
'$translation' => function () use ($kirby) {
|
||||
if ($user = $kirby->user()) {
|
||||
$translation = $kirby->translation($user->language());
|
||||
} else {
|
||||
$translation = $kirby->translation($kirby->panelLanguage());
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => $translation->code(),
|
||||
'data' => $translation->dataWithFallback(),
|
||||
'direction' => $translation->direction(),
|
||||
'name' => $translation->name(),
|
||||
];
|
||||
},
|
||||
'$urls' => fn () => [
|
||||
'api' => $kirby->url('api'),
|
||||
'site' => $kirby->url('index')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the main panel view either as
|
||||
* JSON response or full HTML document based
|
||||
* on the request header or query params
|
||||
*/
|
||||
public static function response($data, array $options = []): Response
|
||||
{
|
||||
// handle redirects
|
||||
if ($data instanceof Redirect) {
|
||||
return Response::redirect($data->location(), $data->code());
|
||||
|
||||
// handle Kirby exceptions
|
||||
} elseif ($data instanceof Exception) {
|
||||
$data = static::error($data->getMessage(), $data->getHttpCode());
|
||||
|
||||
// handle regular exceptions
|
||||
} elseif ($data instanceof Throwable) {
|
||||
$data = static::error($data->getMessage(), 500);
|
||||
|
||||
// only expect arrays from here on
|
||||
} elseif (is_array($data) === false) {
|
||||
$data = static::error('Invalid Panel response', 500);
|
||||
}
|
||||
|
||||
// get all data for the request
|
||||
$fiber = static::data($data, $options);
|
||||
|
||||
// if requested, send $fiber data as JSON
|
||||
if (Panel::isFiberRequest() === true) {
|
||||
// filter data, if only or globals headers or
|
||||
// query parameters are set
|
||||
$fiber = static::apply($fiber);
|
||||
|
||||
return Panel::json($fiber, $fiber['$view']['code'] ?? 200);
|
||||
}
|
||||
|
||||
// load globals for the full document response
|
||||
$globals = static::globals();
|
||||
|
||||
// resolve and merge globals and shared data
|
||||
$fiber = array_merge_recursive(A::apply($globals), A::apply($fiber));
|
||||
|
||||
// render the full HTML document
|
||||
return Document::response($fiber);
|
||||
}
|
||||
|
||||
public static function searches(array $areas, array $permissions): array
|
||||
{
|
||||
$searches = [];
|
||||
|
||||
foreach ($areas as $areaId => $area) {
|
||||
// by default, all areas are accessible unless
|
||||
// the permissions are explicitly set to false
|
||||
if (($permissions['access'][$areaId] ?? true) !== false) {
|
||||
foreach ($area['searches'] ?? [] as $id => $params) {
|
||||
$searches[$id] = [
|
||||
'icon' => $params['icon'] ?? 'search',
|
||||
'label' => $params['label'] ?? Str::ucfirst($id),
|
||||
'id' => $id
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $searches;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user