mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-13 16:59:36 +03:00
feat: allow multiple site selection, add comma-separated format
This commit is contained in:
7
src/App/Controller/CommaController.php
Normal file
7
src/App/Controller/CommaController.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
class CommaController extends TextController {
|
||||
const DELIMITER = ', ';
|
||||
}
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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']))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user