Passed
Push — develop ( ef97e9...11ccd2 )
by Nikolay
04:33
created

CloudProvider::updatePbxSettings()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 15
rs 9.9
cc 3
nc 4
nop 2
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\System\CloudProvisioning;
21
22
use MikoPBX\Common\Models\LanInterfaces;
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\Common\Models\PbxSettingsConstants;
25
use MikoPBX\Core\System\Configs\SSHConf;
26
use MikoPBX\Core\System\Processes;
27
use MikoPBX\Core\System\Util;
28
use MikoPBX\PBXCoreREST\Lib\SysinfoManagementProcessor;
29
30
abstract class CloudProvider
31
{
32
    protected const HTTP_TIMEOUT = 10;
33
34
    abstract public function provision(): bool;
35
36
    /**
37
     * Updates the SSH keys.
38
     *
39
     * @param string $data The SSH keys data.
40
     */
41
    protected function updateSSHKeys(string $data): void
42
    {
43
        if (empty($data)) {
44
            return;
45
        }
46
        $arrData = explode(':', $data);
47
        if (count($arrData) === 2) {
48
            $data = $arrData[1];
49
        }
50
        $this->updatePbxSettings(PbxSettingsConstants::SSH_AUTHORIZED_KEYS, $data);
51
    }
52
53
    /**
54
     * Updates the PBX settings with the provided key and data.
55
     *
56
     * @param string $keyName The key name.
57
     * @param mixed $data The data to be stored.
58
     */
59
    protected function updatePbxSettings(string $keyName, $data): void
60
    {
61
        $setting = PbxSettings::findFirst('key="' . $keyName . '"');
62
        if (!$setting) {
63
            $setting = new PbxSettings();
64
            $setting->key = $keyName;
65
        }
66
        $setting->value = $data;
67
        $result = $setting->save();
68
        if ($result) {
69
            Util::sysLogMsg(__CLASS__, "Update $keyName ... ");
70
        } else {
71
            Util::sysLogMsg(__CLASS__, "FAIL Update $keyName ... ");
72
        }
73
        unset($setting);
74
    }
75
76
    /**
77
     * Updates the LAN settings.
78
     *
79
     * @param string $hostname The hostname.
80
     * @param string $extipaddr The external IP address.
81
     */
82
    protected function updateLanSettings(string $hostname, string $extipaddr): void
83
    {
84
        // Attempt to get the external IP if it's not provided
85
        if (empty($extipaddr)) {
86
            $ipInfoResult = SysinfoManagementProcessor::getExternalIpInfo();
87
            if ($ipInfoResult->success && isset($ipInfoResult->data['ip'])) {
88
                $extipaddr = $ipInfoResult->data['ip'];
89
                Util::sysLogMsg(__CLASS__, "Retrieved external IP: $extipaddr");
90
            } else {
91
                Util::sysLogMsg(__CLASS__, "Failed to retrieve external IP. Error: " . implode(", ", $ipInfoResult->messages));
92
                $extipaddr = '';
93
            }
94
        }
95
96
        /** @var LanInterfaces $lanData */
97
        $lanData = LanInterfaces::findFirst();
98
        if ($lanData !== null) {
99
            $updates = [];
100
            if (!empty($extipaddr)) {
101
                $lanData->extipaddr = $extipaddr;
102
                $lanData->topology = 'private';
103
                $updates[] = "External IP: $extipaddr";
104
            }
105
            if (!empty($hostname)) {
106
                $lanData->hostname = $hostname;
107
                $this->updatePbxSettings(PbxSettingsConstants::PBX_NAME, $hostname);
108
                $updates[] = "Hostname: $hostname";
109
            }
110
            $result = $lanData->save();
111
            if ($result) {
112
                Util::sysLogMsg(__CLASS__, "Updated LAN settings: " . implode(", ", $updates));
113
            } else {
114
                Util::sysLogMsg(__CLASS__, "Failed to update LAN settings: " . implode(", ", $updates));
115
            }
116
        } else {
117
            Util::sysLogMsg(__CLASS__, "LAN interface not found. ($hostname $extipaddr)");
118
        }
119
    }
120
121
    /**
122
     * Updates the SSH password.
123
     */
124
    protected function updateSSHPassword(string $hashSalt): void
125
    {
126
        $data = md5(shell_exec(Util::which('ifconfig')) . $hashSalt . time());
127
        $this->updatePbxSettings(PbxSettingsConstants::SSH_PASSWORD, $data);
128
        $this->updatePbxSettings(PbxSettingsConstants::SSH_DISABLE_SSH_PASSWORD, '1');
129
        $confSsh = new SSHConf();
130
        $confSsh->updateShellPassword();
131
    }
132
133
    /**
134
     * Updates the web password based on the instance name and ID.
135
     *
136
     * @param string $webPassword The web password.
137
     */
138
    protected function updateWebPassword(string $webPassword): void
139
    {
140
        if (empty($webPassword)) {
141
            return;
142
        }
143
        $this->updatePbxSettings(PbxSettingsConstants::WEB_ADMIN_PASSWORD, $webPassword);
144
        $this->updatePbxSettings(PbxSettingsConstants::CLOUD_INSTANCE_ID, $webPassword);
145
        $this->updatePbxSettings(PbxSettingsConstants::PBX_DESCRIPTION, 'Default password is the UniqueID value of your cloud instance');
146
    }
147
148
    /**
149
     * Checks and connects the storage disk automatically.
150
     */
151
    protected function checkConnectStorage(): void
152
    {
153
        $phpPath = Util::which('php');
154
        Processes::mwExec($phpPath . ' -f /etc/rc/connect.storage auto');
155
    }
156
}
157