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

View File

@@ -2,6 +2,8 @@
namespace OpenCCK\App\Controller; namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Factory\SiteFactory;
class MikrotikController extends AbstractIPListController { class MikrotikController extends AbstractIPListController {
/** /**
* @return string * @return string
@@ -9,32 +11,42 @@ class MikrotikController extends AbstractIPListController {
public function getBody(): string { public function getBody(): string {
$this->setHeaders(['content-type' => 'text/plain']); $this->setHeaders(['content-type' => 'text/plain']);
$site = $this->request->getQueryParameter('site') ?? ''; $sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []);
$data = $this->request->getQueryParameter('data') ?? ''; $data = $this->request->getQueryParameter('data') ?? '';
if ($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'"; 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 == '') { $response = [];
foreach ($this->service->sites as $site) { if (count($sites)) {
$response .= $this->render($site->name, $site->$data); foreach ($sites as $site) {
$response = array_merge($response, $this->generateList($site, $this->service->sites[$site]->$data));
} }
return $response;
} else { } 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 string $site
* @param iterable $array * @param array $array
* @return string * @return array
*/ */
private function render(string $site, iterable $array): string { private function generateList(string $site, array $array): array {
$response = ''; $response = [];
$listName = str_replace(' ', '', $site); $listName = str_replace(' ', '', $site);
foreach ($array as $item) { foreach ($array as $item) {
$response .= 'add list=' . $listName . ' address=' . $item . ' comment=' . $listName . "\n"; $response[] = 'add list=' . $listName . ' address=' . $item . ' comment=' . $listName;
} }
return $response; return $response;
} }

View File

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

View File

@@ -547,30 +547,57 @@ use OpenCCK\App\Controller\TextController;
border-radius: 10px; border-radius: 10px;
} }
</style> </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> </head>
<body> <body>
<form action="" method="get"> <main class="main">
<section> <form action="" method="get" class="main-form">
<label> <section class="main-section">
<label class="main-formItem">
Format: Format:
<select name="format"> <select name="format" class="main-formSelect">
<option value="json">JSON</option> <option value="json">JSON</option>
<option value="text">Text</option>
<option value="mikrotik">MikroTik</option> <option value="mikrotik">MikroTik</option>
<option value="text">Text</option>
<option value="comma">Comma</option>
</select> </select>
</label> </label>
<label> <label class="main-formItem">
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: Data:
<select name="data"> <select name="data" class="main-formSelect">
<option value="">All</option> <option value="">All</option>
<option value="domains">domains</option> <option value="domains">domains</option>
<option value="ip4">ip4</option> <option value="ip4">ip4</option>
@@ -579,10 +606,28 @@ use OpenCCK\App\Controller\TextController;
<option value="cidr6">cidr6</option> <option value="cidr6">cidr6</option>
</select> </select>
</label> </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>
<section> <section>
<button type="submit">Submit</button> <button type="submit">Submit</button>
</section> </section>
</form> </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> </body>
</html> </html>