Browse Source

adding #25

master
Björn 4 years ago
parent
commit
9b60d5cc3b
4 changed files with 220 additions and 48 deletions
  1. +119
    -0
      app/Commands/Fail2banManageCommand.php
  2. +32
    -0
      app/Menus/Nginx/NginxVhostCancelAction.php
  3. +1
    -2
      app/Menus/Nginx/NginxVhostGoBackAction.php
  4. +68
    -46
      app/Menus/Nginx/TemplateMenuFactory.php

+ 119
- 0
app/Commands/Fail2banManageCommand.php View File

@ -0,0 +1,119 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
use PhpSchool\CliMenu\Builder\SplitItemBuilder;
use App\Facades\Menus\StylesFactory;
/**
* Manage Fail2ban Configuration
*
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
*
*/
class Fail2banManageCommand extends Command
{
// destination to jail
const DESTINATION_FAIL2BAN_JAIL_DIRECTORY = '/etc/fail2ban/jail.d';
// source to jail
const SOURCE_FAIL2BAN_JAIL_DIRECTORY = '/resources/fail2ban/jail.d';
// ignore files
const IGNORE_FILES = [
'.', '..', 'defaults-debian.conf'
];
// configuration
private $configuration = [];
// enabled
private $enabled = [];
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'fail2ban:manage';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'manage fail2ban configuration';
/**
*
* @param [type] $file [description]
* @return boolean [description]
*/
private function name($file)
{
return str_replace('.conf', '', $file);
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
foreach(scandir(self::DESTINATION_FAIL2BAN_JAIL_DIRECTORY) as $file) {
if (!in_array($file, self::IGNORE_FILES)) {
$this->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();
}
}

+ 32
- 0
app/Menus/Nginx/NginxVhostCancelAction.php View File

@ -0,0 +1,32 @@
<?php
namespace App\Menus\Nginx;
use PhpSchool\CliMenu\CliMenu;
use App\Facades\NginxVhost;
use App\Facades\NginxVhostFactory;
/**
* Action that override default-action for go back
* reload vhosts
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
*
*/
class NginxVhostCancelAction
{
/**
*
* @param CliMenu $menu [description]
* @return [type] [description]
*/
public function __invoke(CliMenu $menu): void
{
$parent = $menu->getParent();
$menu->closeThis();
$parent->open();
}
}

+ 1
- 2
app/Menus/Nginx/NginxVhostGoBackAction.php View File

@ -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;
/**


+ 68
- 46
app/Menus/Nginx/TemplateMenuFactory.php View File

@ -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;


Loading…
Cancel
Save