Skip to content

Commit

Permalink
Add default PORT values if not provided in the form
Browse files Browse the repository at this point in the history
  • Loading branch information
jorikfon committed May 9, 2024
1 parent 6e08b3e commit 66be86a
Showing 1 changed file with 69 additions and 63 deletions.
132 changes: 69 additions & 63 deletions src/AdminCabinet/Controllers/NetworkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ public function modifyAction(): void

$this->view->setVars(
[
'SIP_PORT'=>PbxSettings::getValueByKey(PbxSettingsConstants::SIP_PORT),
'EXTERNAL_SIP_PORT'=>PbxSettings::getValueByKey(PbxSettingsConstants::EXTERNAL_SIP_PORT),
'TLS_PORT'=>PbxSettings::getValueByKey(PbxSettingsConstants::TLS_PORT),
'EXTERNAL_TLS_PORT'=>PbxSettings::getValueByKey(PbxSettingsConstants::EXTERNAL_TLS_PORT),
'RTP_PORT_FROM'=>PbxSettings::getValueByKey(PbxSettingsConstants::RTP_PORT_FROM),
'RTP_PORT_TO'=>PbxSettings::getValueByKey(PbxSettingsConstants::RTP_PORT_TO),
'form'=> $form,
'eths'=>$arrEth,
'deletableEths'=>$deletableInterfaces,
'isDocker'=>Util::isDocker(),
'submitMode'=>null,
'SIP_PORT' => PbxSettings::getValueByKey(PbxSettingsConstants::SIP_PORT),
'EXTERNAL_SIP_PORT' => PbxSettings::getValueByKey(PbxSettingsConstants::EXTERNAL_SIP_PORT),
'TLS_PORT' => PbxSettings::getValueByKey(PbxSettingsConstants::TLS_PORT),
'EXTERNAL_TLS_PORT' => PbxSettings::getValueByKey(PbxSettingsConstants::EXTERNAL_TLS_PORT),
'RTP_PORT_FROM' => PbxSettings::getValueByKey(PbxSettingsConstants::RTP_PORT_FROM),
'RTP_PORT_TO' => PbxSettings::getValueByKey(PbxSettingsConstants::RTP_PORT_TO),
'form' => $form,
'eths' => $arrEth,
'deletableEths' => $deletableInterfaces,
'isDocker' => Util::isDocker(),
'submitMode' => null,
]
);
}
Expand All @@ -93,15 +93,15 @@ public function saveAction(): void
$this->db->begin();


list($result, $messages)=$this->saveLanInterfaces($data);
list($result, $messages) = $this->saveLanInterfaces($data);
if (!$result) {
$this->flash->warning(implode('<br>', $messages));
$this->view->success = false;
$this->db->rollback();
return;
}

list($result, $messages)=$this->saveNatSettings($data);
list($result, $messages) = $this->saveNatSettings($data);
if (!$result) {
$this->flash->warning(implode('<br>', $messages));
$this->view->success = false;
Expand All @@ -115,6 +115,46 @@ public function saveAction(): void
$this->db->commit();
}

/**
* Saves the LAN interface configurations.
*
* This method iterates through each existing LAN interface, updates its configuration based on the provided data,
* and saves the changes. If a new interface needs to be added (specified by 'interface_0'), it creates and saves this
* new interface as well. If any save operation fails, the method returns an array containing `false` and the error messages.
*
* @param array $data Array containing the interface configurations.
* Expected to contain interface settings such as IP, mask, etc., and a special key 'interface_0' for new interfaces.
* @return array Returns an array with a boolean success flag and an array of error messages if applicable.
*/
private function saveLanInterfaces(array $data): array
{
$networkInterfaces = LanInterfaces::find();

// Update interface settings
foreach ($networkInterfaces as $eth) {
$this->fillEthStructure($eth, $data);
if ($eth->save() === false) {
$errors = $eth->getMessages();
return [false, $errors];
}
}

// Save additional interface settings if it exists
if ($data['interface_0'] !== '') {
$eth = new LanInterfaces();
$eth->id = 0;
$this->fillEthStructure($eth, $data);
$eth->id = null;
$eth->disabled = '0';
if ($eth->create() === false) {
$errors = $eth->getMessages();
return [false, $errors];
}
}

return [true, []];
}

/**
* Fills network interface settings
*
Expand Down Expand Up @@ -161,7 +201,7 @@ private function fillEthStructure(LanInterfaces $eth, array $data): void
if (array_key_exists($name . '_' . $eth->id, $data)) {
$eth->$name = ($data['dhcp_' . $eth->id]) === 'on' ? '1' : '0';
}
if ($isDocker){
if ($isDocker) {
$eth->dhcp = '1';
}
break;
Expand Down Expand Up @@ -199,46 +239,6 @@ private function fillEthStructure(LanInterfaces $eth, array $data): void
}
}

/**
* Saves the LAN interface configurations.
*
* This method iterates through each existing LAN interface, updates its configuration based on the provided data,
* and saves the changes. If a new interface needs to be added (specified by 'interface_0'), it creates and saves this
* new interface as well. If any save operation fails, the method returns an array containing `false` and the error messages.
*
* @param array $data Array containing the interface configurations.
* Expected to contain interface settings such as IP, mask, etc., and a special key 'interface_0' for new interfaces.
* @return array Returns an array with a boolean success flag and an array of error messages if applicable.
*/
private function saveLanInterfaces(array $data): array
{
$networkInterfaces = LanInterfaces::find();

// Update interface settings
foreach ($networkInterfaces as $eth) {
$this->fillEthStructure($eth, $data);
if ($eth->save() === false) {
$errors = $eth->getMessages();
return [false, $errors];
}
}

// Save additional interface settings if it exists
if ($data['interface_0'] !== '') {
$eth = new LanInterfaces();
$eth->id = 0;
$this->fillEthStructure($eth, $data);
$eth->id = null;
$eth->disabled = '0';
if ($eth->create() === false) {
$errors = $eth->getMessages();
return [false, $errors];
}
}

return [true, []];
}

/**
* Saves the NAT-related settings for external access.
*
Expand All @@ -251,23 +251,29 @@ private function saveLanInterfaces(array $data): array
*/
private function saveNatSettings(array $data): array
{
foreach ($data as $key=>$value) {
$messages['error'] = [];
foreach ($data as $key => $value) {
switch ($key) {
case PbxSettingsConstants::AUTO_UPDATE_EXTERNAL_IP:
if (!PbxSettings::setValue($key, $value === 'on' ? '1' : '0')){
return [false, ['Error on save '.$key]];
}
PbxSettings::setValue($key, $value === 'on' ? '1' : '0', $messages['error']);
break;
case PbxSettingsConstants::EXTERNAL_SIP_PORT:
if (empty($value)) {
$value = PbxSettings::getValueByKey(PbxSettingsConstants::SIP_PORT);
}
PbxSettings::setValue($key, trim($value), $messages['error']);
break;
case PbxSettingsConstants::EXTERNAL_TLS_PORT:
if (!PbxSettings::setValue($key, trim($value))) {
return [false, ['Error on save '.$key]];
if (empty($value)) {
$value = PbxSettings::getValueByKey(PbxSettingsConstants::TLS_PORT);
}
break;
default:
PbxSettings::setValue($key, trim($value), $messages['error']);
break;
default:
}
}
return [true, []];
$result = count($messages['error']) === 0;
return [$result, $messages];
}

/**
Expand Down

0 comments on commit 66be86a

Please sign in to comment.