Skip to content

Commit

Permalink
Fix test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jorikfon committed Apr 11, 2024
1 parent 35e3124 commit 58e2ae4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/Core/System/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function sendAdminNotification(string $subject, array $messages, b
foreach ($messages as $message) {
$text .= '<br>' . Util::translate($message, false);
}
$text = $text . '<br><br>' . Network::getInfoMessage();
$text = $text . '<br><br>' . SystemMessages::getInfoMessage();

// Get the admin email address from PbxSettings.
$adminMail = PbxSettings::getValueByKey(PbxSettingsConstants::SYSTEM_NOTIFICATIONS_EMAIL);
Expand Down
98 changes: 1 addition & 97 deletions src/Core/System/SystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,108 +348,12 @@ public function startMikoPBX(): bool
$this->echoResultMsg();

// Display network information
$this->echoStartMsg(self::getInfoMessage());
$this->echoStartMsg(SystemMessages::getInfoMessage(true));

$this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = false;

return true;
}

/**
* Retrieves the information message containing available web interface addresses.
*
* @return string The information message.
*/
public static function getInfoMessage(): string
{
// Assuming a total width of 53 characters for each line
$lineWidth = 53;

$info = PHP_EOL . "┌────────────────────────────────────────────────┐";
$info .= PHP_EOL . "│ │";
$headerSpace = $lineWidth - 3 - 5; // 3 for "│ 🌟 MikoPBX - All services are fully loaded 🌟 " and 5 for " │" at the end
$headerLine = sprintf("│ %-{$headerSpace}s │", "🌟 MikoPBX - All services are fully loaded 🌟");
$info .= PHP_EOL . $headerLine;
$info .= PHP_EOL . "│ │";
$info .= PHP_EOL . "├────────────────────────────────────────────────┤";

$addresses = [
'local' => [],
'external' => []
];
/** @var LanInterfaces $interface */
$interfaces = LanInterfaces::find("disabled='0'");
foreach ($interfaces as $interface) {
if (!empty($interface->ipaddr)) {
$addresses['local'][] = $interface->ipaddr;
}
if (!empty($interface->exthostname) && !in_array($interface->exthostname, $addresses['local'], true)) {
$addresses['external'][] = explode(':', $interface->exthostname)[0] ?? '';
}
if (!empty($interface->extipaddr) && !in_array($interface->extipaddr, $addresses['local'], true)) {
$addresses['external'][] = explode(':', $interface->extipaddr)[0] ?? '';
}
}
unset($interfaces);


// Local network
$port = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_HTTPS_PORT);
$info .= PHP_EOL . "│ │";
$headerSpace = $lineWidth - 13 - 1; // 13 for "│ 🌐 Web Interface Access 🌐 " and 1 for " │"
$headerLine = sprintf("│ %-{$headerSpace}s │", "🌐 Web Interface Access 🌐");
$info .= PHP_EOL . $headerLine;
$info .= PHP_EOL . "│ │";
$info .= PHP_EOL . "│ Local Network Address: │";

$addressSpace = $lineWidth - 7 - 5; // 7 for "│ ➜ " and 5 for " │" at the end
foreach ($addresses['local'] as $address) {
if (empty($address)) {
continue;
}
$formattedAddress = $port === '443' ? "https://$address" : "https://$address:$port";
// Use sprintf to format the string with padding to ensure constant length
$info .= PHP_EOL . sprintf("│ ➜ %-{$addressSpace}s │", $formattedAddress);

}
$info .= PHP_EOL . "│ │";

// External web address info
if (!empty($addresses['external'])) {
$info .= PHP_EOL . "│ External Network Address: │";
foreach ($addresses['external'] as $address) {
if (empty($address)) {
continue;
}
$formattedAddress = $port === '443' ? "https://$address" : "https://$address:$port";
// Use sprintf to format the string with padding to ensure constant length
$info .= PHP_EOL . sprintf("│ ➜ %-{$addressSpace}s │", $formattedAddress);

}
$info .= PHP_EOL . "│ │";
}

// Default web user info
$cloudInstanceId = PbxSettings::getValueByKey(PbxSettingsConstants::CLOUD_INSTANCE_ID);
$webAdminPassword = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_PASSWORD);
$defaultPassword = PbxSettings::getDefaultArrayValues()[PbxSettingsConstants::WEB_ADMIN_PASSWORD];
if ($cloudInstanceId === $webAdminPassword || $webAdminPassword === $defaultPassword) {
$adminUser = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LOGIN);

$credentialSpace = $lineWidth - 5 - 3; // 5 for "│ 🔑 Default Credentials: " and 3 for " │"
$credentialLine = sprintf("│ %-{$credentialSpace}s │", "🔑 Default web credentials:");
$info .= PHP_EOL . $credentialLine;
// Login
$loginSpace = $lineWidth - 12 - 5; // 12 for "│ Login: " and 5 for " │" at the end
$loginLine = sprintf("│ Login: %-{$loginSpace}s │", $adminUser); // Format the login line
$info .= PHP_EOL . $loginLine;

// Password
$passwordSpace = $lineWidth - 15 - 5; // 15 for "│ Password: " and 5 for " │" at the end
$passwordLine = sprintf("│ Password: %-{$passwordSpace}s │", $cloudInstanceId); // Format the password line
$info .= PHP_EOL . $passwordLine;
}
$info .= PHP_EOL . "└────────────────────────────────────────────────┘" . PHP_EOL . PHP_EOL;
return $info;
}
}
103 changes: 103 additions & 0 deletions src/Core/System/SystemMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

namespace MikoPBX\Core\System;

use MikoPBX\Common\Models\LanInterfaces;
use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Models\PbxSettingsConstants;
use MikoPBX\Common\Providers\LoggerProvider;
use Phalcon\Di;

Expand Down Expand Up @@ -198,4 +201,104 @@ public static function echoResultMsg(string $message, string $result = SystemMes
SystemMessages::teletypeEchoResult($message, $result);
SystemMessages::echoResult($message, $result);
}

/**
* Retrieves the information message containing available web interface addresses.
* @param bool $showCredentials Optional, if true the message will have the login information
* @return string The information message.
*/
public static function getInfoMessage(bool $showCredentials=false): string
{
// Assuming a total width of 53 characters for each line
$lineWidth = 53;

$info = PHP_EOL . "┌────────────────────────────────────────────────┐";
$info .= PHP_EOL . "│ │";
$headerSpace = $lineWidth - 3 - 5; // 3 for "│ 🌟 MikoPBX - All services are fully loaded 🌟 " and 5 for " │" at the end
$headerLine = sprintf("│ %-{$headerSpace}s │", "🌟 MikoPBX - All services are fully loaded 🌟");
$info .= PHP_EOL . $headerLine;
$info .= PHP_EOL . "│ │";
$info .= PHP_EOL . "├────────────────────────────────────────────────┤";

$addresses = [
'local' => [],
'external' => []
];
/** @var LanInterfaces $interface */
$interfaces = LanInterfaces::find("disabled='0'");
foreach ($interfaces as $interface) {
if (!empty($interface->ipaddr)) {
$addresses['local'][] = $interface->ipaddr;
}
if (!empty($interface->exthostname) && !in_array($interface->exthostname, $addresses['local'], true)) {
$addresses['external'][] = explode(':', $interface->exthostname)[0] ?? '';
}
if (!empty($interface->extipaddr) && !in_array($interface->extipaddr, $addresses['local'], true)) {
$addresses['external'][] = explode(':', $interface->extipaddr)[0] ?? '';
}
}
unset($interfaces);


// Local network
$port = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_HTTPS_PORT);
$info .= PHP_EOL . "│ │";
$headerSpace = $lineWidth - 13 - 1; // 13 for "│ 🌐 Web Interface Access 🌐 " and 1 for " │"
$headerLine = sprintf("│ %-{$headerSpace}s │", "🌐 Web Interface Access 🌐");
$info .= PHP_EOL . $headerLine;
$info .= PHP_EOL . "│ │";
$info .= PHP_EOL . "│ Local Network Address: │";

$addressSpace = $lineWidth - 7 - 5; // 7 for "│ ➜ " and 5 for " │" at the end
foreach ($addresses['local'] as $address) {
if (empty($address)) {
continue;
}
$formattedAddress = $port === '443' ? "https://$address" : "https://$address:$port";
// Use sprintf to format the string with padding to ensure constant length
$info .= PHP_EOL . sprintf("│ ➜ %-{$addressSpace}s │", $formattedAddress);

}
$info .= PHP_EOL . "│ │";

// External web address info
if (!empty($addresses['external'])) {
$info .= PHP_EOL . "│ External Network Address: │";
foreach ($addresses['external'] as $address) {
if (empty($address)) {
continue;
}
$formattedAddress = $port === '443' ? "https://$address" : "https://$address:$port";
// Use sprintf to format the string with padding to ensure constant length
$info .= PHP_EOL . sprintf("│ ➜ %-{$addressSpace}s │", $formattedAddress);

}
$info .= PHP_EOL . "│ │";
}

if ($showCredentials) {
// Default web user info
$cloudInstanceId = PbxSettings::getValueByKey(PbxSettingsConstants::CLOUD_INSTANCE_ID);
$webAdminPassword = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_PASSWORD);
$defaultPassword = PbxSettings::getDefaultArrayValues()[PbxSettingsConstants::WEB_ADMIN_PASSWORD];
if ($cloudInstanceId === $webAdminPassword || $webAdminPassword === $defaultPassword) {
$adminUser = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LOGIN);

$credentialSpace = $lineWidth - 5 - 3; // 5 for "│ 🔑 Default Credentials: " and 3 for " │"
$credentialLine = sprintf("│ %-{$credentialSpace}s │", "🔑 Default web credentials:");
$info .= PHP_EOL . $credentialLine;
// Login
$loginSpace = $lineWidth - 12 - 5; // 12 for "│ Login: " and 5 for " │" at the end
$loginLine = sprintf("│ Login: %-{$loginSpace}s │", $adminUser); // Format the login line
$info .= PHP_EOL . $loginLine;

// Password
$passwordSpace = $lineWidth - 15 - 5; // 15 for "│ Password: " and 5 for " │" at the end
$passwordLine = sprintf("│ Password: %-{$passwordSpace}s │", $cloudInstanceId); // Format the password line
$info .= PHP_EOL . $passwordLine;
}
}
$info .= PHP_EOL . "└────────────────────────────────────────────────┘" . PHP_EOL . PHP_EOL;
return $info;
}
}
5 changes: 2 additions & 3 deletions tests/Core/System/NetworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@

namespace MikoPBX\Tests\Core\System;

use MikoPBX\Core\System\Network;
use MikoPBX\Core\System\SystemLoader;
use MikoPBX\Core\System\SystemMessages;
use MikoPBX\Tests\Unit\AbstractUnitTest;

class NetworkTest extends \MikoPBX\Tests\Unit\AbstractUnitTest
{
public function testGetInfoMessage()
{
$networkInfo = SystemLoader::getInfoMessage();
$networkInfo = SystemMessages::getInfoMessage();
$this->assertTrue(true);
}
}

0 comments on commit 58e2ae4

Please sign in to comment.