diff --git a/src/App/Controller/CommaController.php b/src/App/Controller/CommaController.php new file mode 100644 index 0000000..64014e6 --- /dev/null +++ b/src/App/Controller/CommaController.php @@ -0,0 +1,7 @@ +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)); } } diff --git a/src/App/Controller/MikrotikController.php b/src/App/Controller/MikrotikController.php index 3c792d1..e68a939 100644 --- a/src/App/Controller/MikrotikController.php +++ b/src/App/Controller/MikrotikController.php @@ -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; } diff --git a/src/App/Controller/TextController.php b/src/App/Controller/TextController.php index e585c77..954cc14 100644 --- a/src/App/Controller/TextController.php +++ b/src/App/Controller/TextController.php @@ -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'])) + ); } } diff --git a/src/App/Template/IndexTemplate.php b/src/App/Template/IndexTemplate.php index dc84127..e26ce11 100644 --- a/src/App/Template/IndexTemplate.php +++ b/src/App/Template/IndexTemplate.php @@ -547,42 +547,87 @@ use OpenCCK\App\Controller\TextController; border-radius: 10px; } + -
-
- - - -
-
- -
-
+
+
+
+ + + +
+
+ +
+
+
+ \ No newline at end of file