From 9b60d5cc3bd721917615a481877394dcf19f1e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Mon, 21 Sep 2020 19:00:38 +0200 Subject: [PATCH] adding #25 --- app/Commands/Fail2banManageCommand.php | 119 +++++++++++++++++++++ app/Menus/Nginx/NginxVhostCancelAction.php | 32 ++++++ app/Menus/Nginx/NginxVhostGoBackAction.php | 3 +- app/Menus/Nginx/TemplateMenuFactory.php | 114 ++++++++++++-------- 4 files changed, 220 insertions(+), 48 deletions(-) create mode 100644 app/Commands/Fail2banManageCommand.php create mode 100644 app/Menus/Nginx/NginxVhostCancelAction.php diff --git a/app/Commands/Fail2banManageCommand.php b/app/Commands/Fail2banManageCommand.php new file mode 100644 index 0000000..d190a9c --- /dev/null +++ b/app/Commands/Fail2banManageCommand.php @@ -0,0 +1,119 @@ +enabled[] = $this->name($file); + } + } + + foreach(scandir(base_path().self::SOURCE_FAIL2BAN_JAIL_DIRECTORY) as $file) { + if (!in_array($file, self::IGNORE_FILES)) { + $name = $this->name($file); + $this->configuration[$name] = in_array($name, $this->enabled); + } + } + + // create menu + $builder = $this->menu('Fail2ban'); + + foreach($this->configuration as $name => $single) { + + // create checkbox + $checkbox = new CheckboxItem($name, function(CliMenu $menu) use ($name) { + if ($this->configuration[$name] === true) { + $this->call('fail2ban:disable', [ 'configuration' => $name ]); + } else { + $this->call('fail2ban:enable', [ 'configuration' => $name ]); + } + + // getting new value + $this->configuration[$name] = $menu->getSelectedItem()->getChecked(); + $menu->redraw(); + }); + + // set default value + if ($this->configuration[$name]) { + $checkbox->setChecked(true); + } + + $builder->addMenuItem($checkbox); + } + + // apperance + $builder = StylesFactory::setMenuStyles($builder); + $builder->addLineBreak('-'); + + $mainmenu = $builder->build(); + $mainmenu->open(); + } +} diff --git a/app/Menus/Nginx/NginxVhostCancelAction.php b/app/Menus/Nginx/NginxVhostCancelAction.php new file mode 100644 index 0000000..9ab59d3 --- /dev/null +++ b/app/Menus/Nginx/NginxVhostCancelAction.php @@ -0,0 +1,32 @@ +getParent(); + $menu->closeThis(); + + $parent->open(); + } +} diff --git a/app/Menus/Nginx/NginxVhostGoBackAction.php b/app/Menus/Nginx/NginxVhostGoBackAction.php index 741d7f2..d2409c7 100644 --- a/app/Menus/Nginx/NginxVhostGoBackAction.php +++ b/app/Menus/Nginx/NginxVhostGoBackAction.php @@ -8,7 +8,7 @@ use App\Facades\NginxVhostFactory; /** * Action that override default-action for go back - * reload vhosts + * reload vhosts * * @author Björn Hase, Tentakelfabrik * @license http://opensource.org/licenses/MIT The MIT License @@ -17,7 +17,6 @@ use App\Facades\NginxVhostFactory; */ class NginxVhostGoBackAction { - // index for vhosts const VHOST_INDEX = 0; /** diff --git a/app/Menus/Nginx/TemplateMenuFactory.php b/app/Menus/Nginx/TemplateMenuFactory.php index 98d3aeb..4340696 100644 --- a/app/Menus/Nginx/TemplateMenuFactory.php +++ b/app/Menus/Nginx/TemplateMenuFactory.php @@ -16,7 +16,6 @@ use App\Menus\ItemValidator; use App\BladeFile; use App\Helpers\NginxTemplateHelper; - use App\Facades\TerminalHelper; /** @@ -32,6 +31,26 @@ class TemplateMenuFactory // path templates const TEMPLATES_DIR = '/resources/nginx/templates'; + private $configuration = []; + + /** + * default configuration + * + * @TODO will be removed after + * + * @return array + */ + private function getConfiguration() + { + return [ + 'domain' => '', + 'root' => '', + 'index' => 'index.php', + 'ssl' => true, + 'redirect_www' => true + ]; + } + /** * add item to select template * @@ -65,24 +84,6 @@ class TemplateMenuFactory return $menu; } - /** - * default configuration - * - * @TODO will be removed after - * - * @return array - */ - private function getConfiguration() - { - return [ - 'domain' => '', - 'root' => '', - 'index' => 'index.php', - 'ssl' => true, - 'redirect_www' => true - ]; - } - /** * add input item * @@ -90,18 +91,18 @@ class TemplateMenuFactory * @param string $label * @param array $configuration */ - private function addInputItem($key, $label, &$configuration, $itemValidator = NULL) + private function addInputItem($key, $label, $itemValidator = NULL) { - $callable = function(CliMenu $menu) use ($key, $label, &$configuration, $itemValidator) + $callable = function(CliMenu $menu) use ($key, $label, $itemValidator) { $input = $menu->askText(); - if ($configuration[$key]) { - $input->setPlaceholderText($configuration[$key]); + if ($this->configuration[$key]) { + $input->setPlaceholderText($this->configuration[$key]); } $result = $input->ask(); - $configuration[$key] = $result->fetch(); + $this->configuration[$key] = $result->fetch(); $menu->getSelectedItem()->setText($label.': '.$result->fetch()); @@ -122,12 +123,12 @@ class TemplateMenuFactory * @param object $bladeFile * @param array $configuration */ - private function addPublishItem($template, $bladeFile, &$configuration) + private function addPublishItem($template, $bladeFile) { - $callable = function(CliMenu $menu) use ($template, $bladeFile, &$configuration) + $callable = function(CliMenu $menu) use ($template, $bladeFile) { // getting configuration - $data = $configuration; + $data = $this->configuration; $validator = v::key('domain', v::domain(false)) ->key('root', v::notEmpty()) @@ -145,12 +146,14 @@ class TemplateMenuFactory } else { // create filename - $filename = $configuration['domain'].'.conf'; + $filename = $this->configuration['domain'].'.conf'; // write configuration to file - $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $configuration); + $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $this->configuration); $menu->confirm('Success!')->display('Ok!'); + $this->configuration = $this->getConfiguration(); + // invoke action $action = new NginxVhostGoBackAction(); is_callable($action($menu)); @@ -160,6 +163,24 @@ class TemplateMenuFactory return $callable; } + /** + * + * + * + */ + private function addCancelItem() + { + $callable = function(CliMenu $menu) + { + $this->configuration = $this->getConfiguration(); + + $action = new NginxVhostCancelAction(); + is_callable($action($menu)); + }; + + return $callable; + } + /** * adding radio buttons to select php-fpm version * @@ -167,7 +188,7 @@ class TemplateMenuFactory * @param CliMenuBuilder $builder * @param array $configuration */ - private function addPhpFpmItems($builder, &$configuration) + private function addPhpFpmItems($builder) { // get php-fpm services exec('find /lib/systemd/system/ -name "php[0-9\.]*-fpm.service"', $files); @@ -180,8 +201,8 @@ class TemplateMenuFactory // remove extension $file = str_replace('.service', '', $file); - $builder->addRadioItem($file, function(CliMenu $menu) use (&$configuration) { - $configuration['phpFpm'] = $menu->getSelectedItem()->getText(); + $builder->addRadioItem($file, function(CliMenu $menu) { + $this->configuration['phpFpm'] = $menu->getSelectedItem()->getText(); }); } @@ -200,41 +221,41 @@ class TemplateMenuFactory { $menu = function(CliMenuBuilder $builder) use ($template, $bladeFile) { - $configuration = $this->getConfiguration(); + $this->configuration = $this->getConfiguration(); // create checkbox for ssl - $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) { - $configuration['ssl'] = $menu->getSelectedItem()->getChecked(); + $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) { + $this->configuration['ssl'] = $menu->getSelectedItem()->getChecked(); }); - $checkboxSSL->setChecked($configuration['ssl']); + $checkboxSSL->setChecked($this->configuration['ssl']); // create checkbox for redirect from www - $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) { - $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked(); + $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) { + $this->configuration['redirect_www'] = $menu->getSelectedItem()->getChecked(); }); - $checkboxRedirect->setChecked($configuration['redirect_www']); + $checkboxRedirect->setChecked($this->configuration['redirect_www']); $validator = v::key('root', v::directory()); $itemValidator = new ItemValidator($validator); $builder ->setTitle('Nginx > Add > '.$template['name']) - ->setGoBackButtonText('Cancel') + ->disableDefaultItems() // input domain - ->addItem('domain: -', $this->addInputItem('domain', 'domain', $configuration)) + ->addItem('domain: -', $this->addInputItem('domain', 'domain')) // input root - ->addItem('root: -', $this->addInputItem('root', 'root', $configuration, $itemValidator)) + ->addItem('root: -', $this->addInputItem('root', 'root', $itemValidator)) // input index - ->addItem('index: '.$configuration['index'], $this->addInputItem('index', 'index', $configuration)) + ->addItem('index: '.$this->configuration['index'], $this->addInputItem('index', 'index')) ->addLineBreak('-'); // add php-fpm items - $builder = $this->addPhpFpmItems($builder, $configuration); + $builder = $this->addPhpFpmItems($builder); $builder ->addLineBreak('-') @@ -245,8 +266,9 @@ class TemplateMenuFactory ->addLineBreak('-') // create - ->addItem('publish', $this->addPublishItem($template, $bladeFile, $configuration)) - ->addLineBreak('-'); + ->addItem('Publish', $this->addPublishItem($template, $bladeFile)) + ->addLineBreak('-') + ->addItem('Cancel', $this->addCancelItem()); }; return $menu;