update listing, collections and callbacks

This commit is contained in:
envoyr
2022-02-24 21:39:31 +01:00
parent 703e436b32
commit 5964c3b685
20 changed files with 544 additions and 372 deletions

View File

@@ -220,6 +220,29 @@ class Certificates extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resou
$cert['letsencrypt'] = Settings::Get('system.le_froxlor_enabled');
$cert['loginname'] = 'froxlor.panel';
}
// Set data from certificate
$cert_data = openssl_x509_parse($cert['ssl_cert_file']);
if ($cert_data) {
$cert['validfromdate'] = date('Y-m-d H:i:s', $cert_data['validFrom_time_t']);
$cert['validtodate'] = date('Y-m-d H:i:s', $cert_data['validTo_time_t']);
$cert['isvalid'] = (bool) $cert_data['validTo_time_t'] > time();
$cert['issuer'] = $cert_data['issuer']['O'] ?? null;
}
// Set subject alt names from certificate
$cert['san'] = null;
if (isset($cert_data['extensions']['subjectAltName']) && ! empty($cert_data['extensions']['subjectAltName'])) {
$SANs = explode(",", $cert_data['extensions']['subjectAltName']);
$SANs = array_map('trim', $SANs);
foreach ($SANs as $san) {
$san = str_replace("DNS:", "", $san);
if ($san != $cert_data['subject']['CN'] && strpos($san, "othername:") === false) {
$cert['san'][] = $san;
}
}
}
$result[] = $cert;
}
return $this->response(array(

View File

@@ -0,0 +1,58 @@
<?php
namespace Froxlor\UI\Callbacks;
use Froxlor\UI\Panel\UI;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
*
*/
class Impersonate
{
public static function admin(string $data, array $attributes): array
{
$linker = UI::getLinker();
return [
'type' => 'link',
'data' => [
'text' => $data,
'href' => $linker->getLink([
'section' => 'admins',
'page' => 'admins',
'action' => 'su',
'id' => $attributes['adminid'],
]),
]
];
}
public static function customer(string $data, array $attributes): array
{
$linker = UI::getLinker();
return [
'type' => 'link',
'data' => [
'text' => $data,
'href' => $linker->getLink([
'section' => 'customers',
'page' => 'customers',
'action' => 'su',
'sort' => $attributes['loginname'],
'id' => $attributes['customerid'],
]),
]
];
}
}

View File

@@ -18,11 +18,22 @@ namespace Froxlor\UI\Callbacks;
*/
class Text
{
public static function boolean(string $data): array
public static function boolean(?string $data): array
{
return [
'type' => 'boolean',
'data' => (bool) $data
];
}
}
public static function domainWithSan(string $data, array $attributes): array
{
return [
'type' => 'domainWithSan',
'data' => [
'domain' => $data,
'san' => implode(', ', $attributes['san'] ?? []),
]
];
}
}

View File

@@ -19,59 +19,85 @@ namespace Froxlor\UI;
*/
class Collection
{
private array $items;
private array $userInfo;
private array $params;
private string $class;
private array $has = [];
private array $params;
private array $userinfo;
public function __construct(string $class, array $userInfo, array $params = [])
{
$this->class = $class;
$this->params = $params;
$this->userInfo = $userInfo;
$this->items = $this->getListing($this->class, $this->params);
$this->userinfo = $userInfo;
}
private function getListing($class, $params)
private function getListing($class, $params): array
{
return json_decode($class::getLocal($this->userInfo, $params)->listing(), true);
return json_decode($class::getLocal($this->userinfo, $params)->listing(), true);
}
public function count()
public function count(): int
{
return json_decode($this->class::getLocal($this->userInfo, $this->params)->listingCount(), true);
return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data'];
}
public function get()
public function get(): array
{
return $this->items;
}
$result = $this->getListing($this->class, $this->params);
public function getData()
{
return $this->get()['data'];
}
public function getJson()
{
return json_encode($this->get());
}
public function has($column, $class, $parentKey = 'id', $childKey = 'id', $params = []): Collection
{
// check if the api result contains any items (not the overall listingCount as we might be in a search-resultset)
if (count($this->items) > 0) {
$attributes = $this->getListing($class, $params);
if (count($result)) {
foreach ($this->has as $has) {
$attributes = $this->getListing($has['class'], $has['params']);
foreach ($this->items['data']['list'] as $key => $item) {
foreach ($attributes['data']['list'] as $list) {
if ($item[$parentKey] == $list[$childKey]) {
$this->items['data']['list'][$key][$column] = $list;
foreach ($result['data']['list'] as $key => $item) {
foreach ($attributes['data']['list'] as $list) {
if ($item[$has['parentKey']] == $list[$has['childKey']]) {
$result['data']['list'][$key][$has['column']] = $list;
}
}
}
}
}
return $result;
}
public function getData(): array
{
return $this->get()['data'];
}
public function getList(): array
{
return $this->getData()['list'];
}
public function getJson(): string
{
return json_encode($this->get());
}
public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection
{
$this->has[] = [
'column' => $column,
'class' => $class,
'parentKey' => $parentKey,
'childKey' => $childKey,
'params' => $params
];
return $this;
}
public function withPagination(array $columns): Collection
{
// TODO: handle 'sortable' => true in $columns
$pagination = new Pagination($this->userinfo, $columns, $this->count());
$this->params = array_merge($this->params, $pagination->getApiCommandParams());
return $this;
}
}

View File

@@ -20,14 +20,14 @@ use Froxlor\UI\Panel\UI;
*/
class Listing
{
public static function format(Collection $collection, array $tabellisting): array
public static function format(array $list, array $tabellisting): array
{
return [
'title' => $tabellisting['title'],
'icon' => $tabellisting['icon'],
'table' => [
'th' => self::generateTableHeadings($tabellisting),
'tr' => self::generateTableRows($collection, $tabellisting),
'tr' => self::generateTableRows($list, $tabellisting),
],
'pagination' => null, // TODO: write some logic
];
@@ -43,26 +43,31 @@ class Listing
continue;
}
$heading[] = $tabellisting['columns'][$visible_column]['label'];
$heading[] = [
'text' => $tabellisting['columns'][$visible_column]['label'],
'class' => $tabellisting['columns'][$visible_column]['class'] ?? null,
];
}
// Table headings for actions
if (isset($tabellisting['actions'])) {
$heading[] = UI::getLng('panel.options');
$heading[] = [
'text' => UI::getLng('panel.options'),
'class' => 'text-end',
];
}
return $heading;
}
private static function generateTableRows(Collection $collection, array $tabellisting): array
private static function generateTableRows(array $list, array $tabellisting): array
{
$rows = [];
$items = $collection->getData()['list'];
// Create new row from item
foreach ($items as $key => $item) {
foreach ($list as $row => $item) {
// Generate columns from item
foreach ($tabellisting['visible_columns'] as $visible_column) {
foreach ($tabellisting['visible_columns'] as $col => $visible_column) {
if (isset($tabellisting['columns'][$visible_column]['visible']) && !$tabellisting['columns'][$visible_column]['visible']) {
continue;
}
@@ -71,22 +76,30 @@ class Listing
$column = $tabellisting['columns'][$visible_column]['column'];
$data = self::getMultiArrayFromString($item, $column);
// TODO: contextual_class ...
if ($format_callback) {
$rows[$key][] = call_user_func($format_callback, $data, $item);
$rows[$row]['td'][$col]['data'] = call_user_func($format_callback, $data, $item);
} else {
$rows[$key][] = $data;
$rows[$row]['td'][$col]['data'] = $data;
}
$rows[$row]['td'][$col]['class'] = $tabellisting['columns'][$visible_column]['class'] ?? null;
}
// TODO: contextual_class ...
//if (...) {
// $rows[$key]['class'] = '...';
//}
// Set all actions for row
if (isset($tabellisting['actions'])) {
$actions = self::setLinks($tabellisting['actions'], $item);
$rows[$key]['action'] = [
'type' => 'actions',
'data' => $actions
$rows[$row]['td'][] = [
'class' => 'text-end',
'data' => [
'type' => 'actions',
'data' => $actions
]
];
}
}
@@ -94,7 +107,7 @@ class Listing
return $rows;
}
private static function setLinks($actions, array $item)
private static function setLinks(array $actions, array $item): array
{
$linker = UI::getLinker();
@@ -119,7 +132,7 @@ class Listing
return $actions;
}
public static function getVisibleColumnsForListing($listing, $default_columns)
public static function getVisibleColumnsForListing(string $listing, array $default_columns): array
{
// Hier käme dann die Logik, die das aus der DB zieht ...
// alternativ nimmt er die $default_columns, wenn kein Eintrag
@@ -128,7 +141,7 @@ class Listing
return $default_columns;
}
public static function getMultiArrayFromString($arr, $str)
public static function getMultiArrayFromString(array $arr, string $str)
{
foreach (explode('.', $str) as $key) {
if (!array_key_exists($key, $arr)) {

View File

@@ -16,6 +16,10 @@
*
*/
use Froxlor\UI\Callbacks\ProgressBar;
use Froxlor\UI\Callbacks\Text;
use Froxlor\UI\Listing;
return [
'admin_list' => [
'title' => $lng['admin']['admin'],
@@ -37,25 +41,27 @@ return [
],
'customers_used' => [
'label' => $lng['admin']['customers'],
'column' => 'customers_used'
'column' => 'customers_used',
'class' => 'text-center',
],
'diskspace' => [
'label' => $lng['customer']['diskspace'],
'column' => 'diskspace',
'format_callback' => [\Froxlor\UI\Callbacks\ProgressBar::class, 'diskspace'],
'format_callback' => [ProgressBar::class, 'diskspace'],
],
'traffic' => [
'label' => $lng['customer']['traffic'],
'column' => 'traffic',
'format_callback' => [\Froxlor\UI\Callbacks\ProgressBar::class, 'traffic'],
'format_callback' => [ProgressBar::class, 'traffic'],
],
'deactivated' => [
'label' => $lng['admin']['deactivated'],
'column' => 'deactivated',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean'],
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
],
],
'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('admin_list', [
'visible_columns' => Listing::getVisibleColumnsForListing('admin_list', [
'loginname',
'name',
'customers_used',
@@ -64,24 +70,25 @@ return [
'deactivated',
]),
'actions' => [
'delete' => [
'icon' => 'fa fa-trash',
'href' => [
'section' => 'admins',
'page' => 'admins',
'action' => 'delete',
'id' => ':adminid'
],
],
'edit' => [
'text' => 'Edit',
'icon' => 'fa fa-edit',
'href' => [
'section' => 'admins',
'page' => 'admins',
'action' => 'edit',
'id' => ':adminid'
],
]
],
'delete' => [
'icon' => 'fa fa-trash',
'class' => 'text-danger',
'href' => [
'section' => 'admins',
'page' => 'admins',
'action' => 'delete',
'id' => ':adminid'
],
],
],
'contextual_class' => [
'deactivated' => [

View File

@@ -16,6 +16,10 @@
*
*/
use Froxlor\UI\Callbacks\Impersonate;
use Froxlor\UI\Callbacks\ProgressBar;
use Froxlor\UI\Listing;
return [
'customer_list' => [
'title' => $lng['admin']['customers'],
@@ -24,10 +28,12 @@ return [
'c.loginname' => [
'label' => $lng['login']['username'],
'column' => 'loginname',
'format_callback' => [Impersonate::class, 'customer'],
],
'a.loginname' => [
'label' => $lng['admin']['admin'],
'column' => 'admin.loginname',
'format_callback' => [Impersonate::class, 'admin'],
],
'c.name' => [
'label' => $lng['customer']['name'],
@@ -48,15 +54,15 @@ return [
'c.diskspace' => [
'label' => $lng['customer']['diskspace'],
'column' => 'diskspace',
'format_callback' => [\Froxlor\UI\Callbacks\ProgressBar::class, 'diskspace'],
'format_callback' => [ProgressBar::class, 'diskspace'],
],
'c.traffic' => [
'label' => $lng['customer']['traffic'],
'column' => 'traffic',
'format_callback' => [\Froxlor\UI\Callbacks\ProgressBar::class, 'traffic'],
'format_callback' => [ProgressBar::class, 'traffic'],
],
],
'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('customer_list', [
'visible_columns' => Listing::getVisibleColumnsForListing('customer_list', [
'c.loginname',
'a.loginname',
'c.email',
@@ -66,8 +72,18 @@ return [
'c.traffic',
]),
'actions' => [
'edit' => [
'icon' => 'fa fa-edit',
'href' => [
'section' => 'customers',
'page' => 'customers',
'action' => 'edit',
'id' => ':customerid'
],
],
'delete' => [
'icon' => 'fa fa-trash',
'class' => 'text-danger',
'href' => [
'section' => 'customers',
'page' => 'customers',
@@ -75,15 +91,6 @@ return [
'id' => ':customerid'
],
],
'edit' => [
'text' => 'Edit',
'href' => [
'section' => 'customers',
'page' => 'customers',
'action' => 'edit',
'id' => ':customerid'
],
]
],
]
];

View File

@@ -16,6 +16,9 @@
*
*/
use Froxlor\UI\Callbacks\Impersonate;
use Froxlor\UI\Listing;
return [
'domain_list' => [
'title' => $lng['admin']['domains'],
@@ -40,13 +43,14 @@ return [
'c.loginname' => [
'label' => $lng['login']['username'],
'column' => 'customer.loginname',
'format_callback' => [Impersonate::class, 'customer'],
],
'd.aliasdomain' => [
'label' => $lng['domains']['aliasdomain'],
'column' => 'aliasdomain',
],
],
'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('domain_list', [
'visible_columns' => Listing::getVisibleColumnsForListing('domain_list', [
'd.domain_ace',
'c.name',
'c.firstname',
@@ -55,17 +59,8 @@ return [
'd.aliasdomain',
]),
'actions' => [
'delete' => [
'icon' => 'fa fa-trash',
'href' => [
'section' => 'domains',
'page' => 'domains',
'action' => 'delete',
'id' => ':id'
],
],
'edit' => [
'text' => 'Edit',
'icon' => 'fa fa-edit',
'href' => [
'section' => 'domains',
'page' => 'domains',
@@ -88,7 +83,17 @@ return [
'page' => 'domaindnseditor',
'domain_id' => ':id'
],
]
],
'delete' => [
'icon' => 'fa fa-trash',
'class' => 'text-danger',
'href' => [
'section' => 'domains',
'page' => 'domains',
'action' => 'delete',
'id' => ':id'
],
],
]
]
];

View File

@@ -16,6 +16,10 @@
*
*/
use Froxlor\Settings;
use Froxlor\UI\Callbacks\Text;
use Froxlor\UI\Listing;
return [
'ipsandports_list' => [
'title' => $lng['admin']['ipsandports']['ipsandports'],
@@ -28,42 +32,49 @@ return [
'port' => [
'label' => $lng['admin']['ipsandports']['port'],
'column' => 'port',
'class' => 'text-center',
],
'listen' => [
'label' => 'Listen',
'column' => 'listen_statement',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean'],
'visible' => \Froxlor\Settings::Get('system.webserver') != 'nginx'
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.webserver') != 'nginx'
],
'namevirtualhost' => [
'label' => 'NameVirtualHost',
'column' => 'namevirtualhost_statement',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean'],
'visible' => \Froxlor\Settings::Get('system.webserver') == 'apache2' && (int) \Froxlor\Settings::Get('system.apache24') == 0
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.webserver') == 'apache2' && (int) Settings::Get('system.apache24') == 0
],
'vhostcontainer' => [
'label' => 'vHost-Container',
'column' => 'vhostcontainer',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean']
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean']
],
'specialsettings' => [
'label' => 'Specialsettings',
'column' => 'specialsettings',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean']
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean']
],
'servername' => [
'label' => 'ServerName',
'column' => 'vhostcontainer_servername_statement',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean'],
'visible' => \Froxlor\Settings::Get('system.webserver') == 'apache2'
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.webserver') == 'apache2'
],
'ssl' => [
'label' => 'SSL',
'column' => 'ssl',
'format_callback' => [\Froxlor\UI\Callbacks\Text::class, 'boolean']
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean']
],
],
'visible_columns' => \Froxlor\UI\Listing::getVisibleColumnsForListing('ipsandports_list', [
'visible_columns' => Listing::getVisibleColumnsForListing('ipsandports_list', [
'ip',
'port',
'listen',
@@ -73,5 +84,26 @@ return [
'servername',
'ssl'
]),
'actions' => [
'edit' => [
'icon' => 'fa fa-edit',
'href' => [
'section' => 'ipsandports',
'page' => 'ipsandports',
'action' => 'edit',
'id' => ':id'
],
],
'delete' => [
'icon' => 'fa fa-trash',
'class' => 'text-danger',
'href' => [
'section' => 'ipsandports',
'page' => 'ipsandports',
'action' => 'delete',
'id' => ':id'
],
],
]
]
];

View File

@@ -0,0 +1,72 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Tabellisting
*
*/
use Froxlor\Settings;
use Froxlor\UI\Listing;
return [
'plan_list' => [
'title' => $lng['domains']['ssl_certificates'],
'icon' => 'fa-solid fa-user',
'columns' => [
'p.name' => [
'label' => $lng['admin']['plans']['name'],
'column' => 'name',
],
'p.description' => [
'label' => $lng['admin']['plans']['description'],
'column' => 'description',
],
'p.adminname' => [
'label' => $lng['admin']['admin'],
'column' => 'adminname',
],
'p.ts' => [
'label' => $lng['admin']['plans']['last_update'],
'column' => 'ts',
],
],
'visible_columns' => Listing::getVisibleColumnsForListing('sslcertificates_list', [
'p.name',
'p.description',
'p.adminname',
'p.ts',
]),
'actions' => [
'edit' => [
'icon' => 'fa fa-edit',
'href' => [
'section' => 'plans',
'page' => 'overview',
'action' => 'edit',
'id' => ':id'
],
],
'delete' => [
'icon' => 'fa fa-trash',
'class' => 'text-danger',
'href' => [
'section' => 'plans',
'page' => 'overview',
'action' => 'delete',
'id' => ':id'
],
],
]
]
];

View File

@@ -0,0 +1,78 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Tabellisting
*
*/
use Froxlor\Settings;
use Froxlor\UI\Callbacks\Text;
use Froxlor\UI\Listing;
return [
'sslcertificates_list' => [
'title' => $lng['domains']['ssl_certificates'],
'icon' => 'fa-solid fa-user',
'columns' => [
'd.domain' => [
'label' => $lng['domains']['domainname'],
'column' => 'domain',
],
'c.domain' => [
'label' => $lng['ssl_certificates']['certificate_for'],
'column' => 'domain',
'format_callback' => [Text::class, 'domainWithSan'],
],
'c.issuer' => [
'label' => $lng['ssl_certificates']['issuer'],
'column' => 'issuer',
],
'c.validfromdate' => [
'label' => $lng['ssl_certificates']['valid_from'],
'column' => 'validfromdate',
],
'c.validtodate' => [
'label' => $lng['ssl_certificates']['valid_until'],
'column' => 'validtodate',
],
'c.letsencrypt' => [
'label' => $lng['panel']['letsencrypt'],
'column' => 'letsencrypt',
'class' => 'text-center',
'format_callback' => [Text::class, 'boolean'],
'visible' => Settings::Get('system.le_froxlor_enabled'),
],
],
'visible_columns' => Listing::getVisibleColumnsForListing('sslcertificates_list', [
'd.domain',
'c.domain',
'c.issuer',
'c.validfromdate',
'c.validtodate',
'c.letsencrypt',
]),
'actions' => [
'delete' => [
'icon' => 'fa fa-trash',
'class' => 'text-danger',
'href' => [
'section' => 'domains',
'page' => 'sslcertificates',
'action' => 'delete',
'id' => ':id'
],
],
]
]
];