feat: allow multiple site selection, add comma-separated format

This commit is contained in:
Rekryt
2024-08-31 15:36:08 +03:00
parent d304af915e
commit fe752cede2
5 changed files with 135 additions and 77 deletions

View File

@@ -0,0 +1,7 @@
<?php
namespace OpenCCK\App\Controller;
class CommaController extends TextController {
const DELIMITER = ', ';
}

View File

@@ -2,6 +2,9 @@
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Entity\Site;
use OpenCCK\Domain\Factory\SiteFactory;
class JsonController extends AbstractIPListController {
/**
* @return string
@@ -9,24 +12,14 @@ class JsonController extends AbstractIPListController {
public function getBody(): string {
$this->setHeaders(['content-type' => 'application/json']);
$site = $this->request->getQueryParameter('site') ?? '';
$sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []);
$data = $this->request->getQueryParameter('data') ?? '';
if ($site == '') {
if ($data == '') {
return json_encode($this->service->sites);
} else {
$result = [];
foreach ($this->service->sites as $site) {
$result[$site->name] = $site->$data;
}
return json_encode($result);
}
if (count($sites)) {
$items = array_filter($this->service->sites, fn(Site $siteEntity) => in_array($siteEntity->name, $sites));
} else {
if ($data == '') {
return json_encode($this->service->sites[$site]);
} else {
return json_encode($this->service->sites[$site]->$data);
}
$items = $this->service->sites;
}
return json_encode($data == '' ? $items : array_map(fn(Site $siteEntity) => $siteEntity->$data, $items));
}
}

View File

@@ -2,6 +2,8 @@
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Factory\SiteFactory;
class MikrotikController extends AbstractIPListController {
/**
* @return string
@@ -9,32 +11,42 @@ class MikrotikController extends AbstractIPListController {
public function getBody(): string {
$this->setHeaders(['content-type' => 'text/plain']);
$site = $this->request->getQueryParameter('site') ?? '';
$sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []);
$data = $this->request->getQueryParameter('data') ?? '';
if ($data == '') {
return "# Error: The 'data' GET parameter is required in the URL to access this page, but it cannot have the value 'All'";
}
$response = '/ip firewall address-list' . "\n";
if ($site == '') {
foreach ($this->service->sites as $site) {
$response .= $this->render($site->name, $site->$data);
$response = [];
if (count($sites)) {
foreach ($sites as $site) {
$response = array_merge($response, $this->generateList($site, $this->service->sites[$site]->$data));
}
return $response;
} else {
return $response . $this->render($site, $this->service->sites[$site]->$data);
foreach ($this->service->sites as $siteEntity) {
$response = array_merge($response, $this->generateList($siteEntity->name, $siteEntity->$data));
}
}
return implode(
"\n",
array_merge(
['/ip firewall address-list'],
SiteFactory::normalizeArray($response, in_array($data, ['ipv4', 'ipv6', 'cidr4', 'cidr6']))
)
);
}
/**
* @param string $site
* @param iterable $array
* @return string
* @param array $array
* @return array
*/
private function render(string $site, iterable $array): string {
$response = '';
private function generateList(string $site, array $array): array {
$response = [];
$listName = str_replace(' ', '', $site);
foreach ($array as $item) {
$response .= 'add list=' . $listName . ' address=' . $item . ' comment=' . $listName . "\n";
$response[] = 'add list=' . $listName . ' address=' . $item . ' comment=' . $listName;
}
return $response;
}

View File

@@ -2,35 +2,36 @@
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Factory\SiteFactory;
class TextController extends AbstractIPListController {
const DELIMITER = "\n";
/**
* @return string
*/
public function getBody(): string {
$this->setHeaders(['content-type' => 'text/plain']);
$site = $this->request->getQueryParameter('site') ?? '';
$sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []);
$data = $this->request->getQueryParameter('data') ?? '';
if ($data == '') {
return "# Error: The 'data' GET parameter is required in the URL to access this page, but it cannot have the value 'All'";
}
$response = '';
if ($site == '') {
foreach ($this->service->sites as $site) {
$response .= $this->render($site->name, $site->$data);
$response = [];
if (count($sites)) {
foreach ($sites as $site) {
$response = array_merge($response, $this->service->sites[$site]->$data);
}
return $response;
} else {
return $this->render($site, $this->service->sites[$site]->$data);
foreach ($this->service->sites as $siteEntity) {
$response = array_merge($response, $siteEntity->$data);
}
}
}
private function render(string $name, iterable $array): string {
$response = '# ' . $name . ' ' . date('Y-m-d H:i:s') . "\n";
foreach ($array as $item) {
$response .= $item . "\n";
}
return $response;
return implode(
$this::DELIMITER,
SiteFactory::normalizeArray($response, in_array($data, ['ipv4', 'ipv6', 'cidr4', 'cidr6']))
);
}
}

View File

@@ -547,42 +547,87 @@ use OpenCCK\App\Controller\TextController;
border-radius: 10px;
}
</style>
<style>
.main {
display: flex;
justify-content: center;
font-size: 18px;
line-height: 1.2;
}
.main-section {
justify-content: center;
row-gap: 4px;
column-gap: 16px;
}
.main-form {
}
.main-formItem {
margin-bottom: 0;
}
.main-formItem_wide {
flex: 0 0 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.main-formItemComment {
flex: 0 0 100%;
display: block;
font-size: 12px;
line-height: 1;
width: 150px;
text-align: center;
}
.main-formSelect {}
.main-formSelect_site {}
</style>
</head>
<body>
<form action="" method="get">
<section>
<label>
Format:
<select name="format">
<option value="json">JSON</option>
<option value="text">Text</option>
<option value="mikrotik">MikroTik</option>
</select>
</label>
<label>
Site:
<select name="site">
<option value="">All</option>
<?php foreach ($this->service->sites as $site): ?>
<option value="<?= $site->name ?>"><?= $site->name ?></option>
<?php endforeach; ?>
</select>
</label>
<label>
Data:
<select name="data">
<option value="">All</option>
<option value="domains">domains</option>
<option value="ip4">ip4</option>
<option value="cidr4">cidr4</option>
<option value="ip6">ip6</option>
<option value="cidr6">cidr6</option>
</select>
</label>
</section>
<section>
<button type="submit">Submit</button>
</section>
</form>
<main class="main">
<form action="" method="get" class="main-form">
<section class="main-section">
<label class="main-formItem">
Format:
<select name="format" class="main-formSelect">
<option value="json">JSON</option>
<option value="mikrotik">MikroTik</option>
<option value="text">Text</option>
<option value="comma">Comma</option>
</select>
</label>
<label class="main-formItem">
Data:
<select name="data" class="main-formSelect">
<option value="">All</option>
<option value="domains">domains</option>
<option value="ip4">ip4</option>
<option value="cidr4">cidr4</option>
<option value="ip6">ip6</option>
<option value="cidr6">cidr6</option>
</select>
</label>
<label class="main-formItem main-formItem_wide">
<span>
Site:
<select name="site" class="main-formSelect main-formSelect_site" multiple>
<?php foreach ($this->service->sites as $site): ?>
<option value="<?= $site->name ?>"><?= $site->name ?></option>
<?php endforeach; ?>
</select>
</span>
<span class="main-formItemComment">Don't choose sites if you want to get everything</span>
</label>
</section>
<section>
<button type="submit">Submit</button>
</section>
</form>
</main>
<footer>
<p style="text-align: center">
<a href="https://github.com/rekryt/iplist">GitHub</a>
<a href="https://github.com/rekryt/iplist/issues">Issues</a>
</p>
</footer>
</body>
</html>