Skip to content

Commit

Permalink
setting: networks descriptions implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
nightflyza committed Mar 19, 2024
1 parent 8bad67b commit ec4b538
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

All notable changes to this project will be documented in this file.

## [0.0.1] - rev 82
## [0.0.2] - rev 85

- installer: FreeBSD 13.2 setup works again.
- installer: FreeBSD 13.3 setup tested and works
- installer: Linux Debian 12.5 bookworm installer
- settings: networks descriptions implemented

## [0.0.1] - rev 77

Expand Down
2 changes: 1 addition & 1 deletion RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1 rev 83
0.0.2 rev 85
76 changes: 75 additions & 1 deletion api/libs/api.ophanimmgr.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ class OphanimMgr {
*/
protected $port = 42112;

/**
* Contains networks table structure as field=>index
*
* @var array
*/
protected $networksStruct = array();

//some predefined stuff here
const DB_PATCHES_PATH = 'dist/dumps/patches/';
const DB_PATCHES_EXT = '.sql';
const CONF_PATH = '/etc/of.conf';
const PRETAG_PATH = '/etc/pretag.map';
const TEMPLATE_PATH = 'dist/collector/of.template';
Expand All @@ -66,6 +75,7 @@ class OphanimMgr {
//and some routes
const URL_ME = '?module=settings';
const PROUTE_NETW_CREATE = 'newnetwork';
const PROUTE_NETW_DESC = 'newnetworkdescr';
const ROUTE_NETW_DEL = 'deletenetwork';
const ROUTE_START = 'startcollector';
const ROUTE_STOP = 'stopcollector';
Expand All @@ -75,6 +85,7 @@ public function __construct() {
$this->initMessages();
$this->loadConfigs();
$this->initNetsDb();
$this->loadNetStruct();
$this->loadNetworks();
}

Expand Down Expand Up @@ -118,19 +129,78 @@ protected function loadNetworks() {
$this->netsCount = sizeof($this->allNetworks);
}

/**
* Loads networks database struct and applies some patches if required
*
* @return void
*/
protected function loadNetStruct() {
$patchesApplied = false;
$structTmp = $this->networksDb->getTableStructure(true);
$structTmp = array_flip($structTmp);
$this->networksStruct = $structTmp;

//0.0.2 patch
if (!isset($this->networksStruct['descr'])) {
debarr($this->networksStruct);
$this->applyDbPatch('0.0.2');
$patchesApplied = true;
}

//viewport refresh
if ($patchesApplied) {
ubRouting::nav(self::URL_ME);
}
}

/**
* Apllies database patch by its name
*
* @param type $patchName
*
* @return void
*/
protected function applyDbPatch($patchName) {
if (!empty($patchName)) {
$patchPath = self::DB_PATCHES_PATH . $patchName . self::DB_PATCHES_EXT;
if (file_exists($patchPath)) {
$patchContent = file_get_contents($patchPath);
if (!empty($patchContent)) {
$patchContent = explode(';', $patchContent);
if (!empty($patchContent)) {
foreach ($patchContent as $io => $eachQuery) {
$eachQuery = trim($eachQuery);
if (!empty($eachQuery)) {
nr_query($eachQuery);
show_success(__('DB patch') . $patchName . ': ' . $eachQuery);
}
}
}
}
}
}
}

/**
* Renders available networks list
*
* @return string
*/
public function renderNetworksList() {
$result = '';

if (!empty($this->allNetworks)) {
$cells = wf_TableCell(__('Network'));
if (isset($this->networksStruct['descr'])) {
$cells .= wf_TableCell(__('Description'));
}
$cells .= wf_TableCell(__('Actions'));
$rows = wf_TableRow($cells, 'table-light');
foreach ($this->allNetworks as $io => $each) {
$cells = wf_TableCell($each['network']);
if (isset($this->networksStruct['descr'])) {
$cells .= wf_TableCell($each['descr']);
}
$delUrl = self::URL_ME . '&' . self::ROUTE_NETW_DEL . '=' . $each['id'];
$actLinks = wf_JSAlertStyled($delUrl, __('Delete'), __('Are you serious') . '?', 'btn cur-p btn-danger btn-color');
$cells .= wf_TableCell($actLinks);
Expand All @@ -151,6 +221,7 @@ public function renderNetworksList() {
public function renderNetworkCreateForm() {
$result = '';
$inputs = wf_TextInput(self::PROUTE_NETW_CREATE, __('Network') . '/CIDR', '', false, '20', 'net-cidr') . ' ';
$inputs .= wf_TextInput(self::PROUTE_NETW_DESC, __('Description'), '', false, '20', '') . ' ';
$inputs .= wf_Submit(__('Create new'), '', 'class="btn btn-primary btn-color"');
$result .= wf_delimiter();
$result .= wf_Form('', 'POST', $inputs, 'glamour');
Expand Down Expand Up @@ -196,13 +267,16 @@ public function isNetworkExists($network) {
* Creates new network database record
*
* @param string $network
* @param string $descr
*
* @return void
*/
public function createNetwork($network) {
public function createNetwork($network, $descr = '') {
$netF = ubRouting::filters($network, 'mres');
$descrF = ubRouting::filters($descr, 'mres');
if (!$this->isNetworkExists($network)) {
$this->networksDb->data('network', $netF);
$this->networksDb->data('descr', $descrF);
$this->networksDb->create();
}
}
Expand Down
5 changes: 4 additions & 1 deletion dist/dumps/ophanimflow.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ CREATE TABLE `networks` (
`id` int NOT NULL AUTO_INCREMENT,
`network` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

-- 0.0.2 patch
ALTER TABLE `networks` ADD `descr` VARCHAR(255) NULL AFTER `network`;
1 change: 1 addition & 0 deletions dist/dumps/patches/0.0.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `networks` ADD `descr` VARCHAR(255) NULL AFTER `network`;
7 changes: 4 additions & 3 deletions modules/general/settings/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

if (ubRouting::checkPost($settings::PROUTE_NETW_CREATE)) {
$netToCreate = ubRouting::post($settings::PROUTE_NETW_CREATE);
$descrToCreate = ubRouting::post($settings::PROUTE_NETW_DESC);
if (!$settings->isNetworkExists($netToCreate)) {
$settings->createNetwork($netToCreate);
$settings->createNetwork($netToCreate, $descrToCreate);
ubRouting::nav($settings::URL_ME);
} else {
show_error(__('Network') . ' `' . $netToCreate . '` ' . __('already exists'));
Expand All @@ -29,12 +30,12 @@
if (empty($reconfResult)) {
ubRouting::nav($settings::URL_ME);
} else {
show_error(__('Fatal').': '.$reconfResult);
show_error(__('Fatal') . ': ' . $reconfResult);
}
}

if (ubRouting::checkGet($settings::ROUTE_START)) {
$startResult=$settings->startCollector();
$startResult = $settings->startCollector();
if (empty($startResult)) {
ubRouting::nav($settings::URL_ME);
} else {
Expand Down

0 comments on commit ec4b538

Please sign in to comment.