Passed
Push — develop ( a04425...8843c6 )
by Nikolay
05:16 queued 14s
created

CloudProvider::updatePbxSettings()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 17
rs 9.8333
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\SystemMessages;
27
use MikoPBX\Core\System\Util;
28
29
abstract class CloudProvider
30
{
31
    protected const HTTP_TIMEOUT = 10;
32
33
    abstract public function provision(): bool;
34
35
    /**
36
     * Updates the SSH keys.
37
     *
38
     * @param string $data The SSH keys data.
39
     */
40
    protected function updateSSHKeys(string $data): void
41
    {
42
        if (empty($data)) {
43
            return;
44
        }
45
        $arrData = explode(':', $data);
46
        if (count($arrData) === 2) {
47
            $data = $arrData[1];
48
        }
49
        $this->updatePbxSettings(PbxSettingsConstants::SSH_AUTHORIZED_KEYS, $data);
50
    }
51
52
    /**
53
     * Updates the PBX settings with the provided key and data.
54
     *
55
     * @param string $keyName The key name.
56
     * @param mixed $data The data to be stored.
57
     */
58
    public function updatePbxSettings(string $keyName, $data): void
59
    {
60
        $setting = PbxSettings::findFirst('key="' . $keyName . '"');
61
        if (!$setting) {
62
            $setting = new PbxSettings();
63
            $setting->key = $keyName;
64
        }
65
        $setting->value = $data;
66
        $result = $setting->save();
67
        $message = "      |- Update PbxSettings - $keyName ... ";
68
        SystemMessages::echoToTeletype($message);
69
        if ($result) {
70
            SystemMessages::teletypeEchoResult($message);
71
        } else {
72
            SystemMessages::teletypeEchoResult($message, SystemMessages::RESULT_FAILED);
73
        }
74
        unset($setting);
75
    }
76
77
    /**
78
     * Updates the LAN settings.
79
     *
80
     * @param string $extipaddr The external IP address.
81
     */
82
    protected function updateLanSettings(string $extipaddr): void
83
    {
84
        /** @var LanInterfaces $lanData */
85
        $lanData = LanInterfaces::findFirst();
86
        if ($lanData !== null) {
87
88
            if (empty($extipaddr)) {
89
                $lanData->autoUpdateExtIp='1';
90
            } elseif ($lanData->ipaddr === $extipaddr) {
91
                $lanData->topology = LanInterfaces::TOPOLOGY_PUBLIC;
92
            } else {
93
                $lanData->extipaddr = $extipaddr;
94
                $lanData->topology = LanInterfaces::TOPOLOGY_PRIVATE;
95
                $lanData->autoUpdateExtIp='1';
96
            }
97
            $message = "      |- Update LAN settings external IP: $extipaddr";
98
            SystemMessages::echoToTeletype($message);
99
            $result = $lanData->save();
100
            if ($result) {
101
                SystemMessages::teletypeEchoResult($message);
102
            } else {
103
                SystemMessages::teletypeEchoResult($message, SystemMessages::RESULT_FAILED);
104
            }
105
        } else {
106
            $message = "      |- LAN interface not found";
107
            SystemMessages::echoToTeletype($message);
108
            SystemMessages::teletypeEchoResult($message, SystemMessages::RESULT_SKIPPED);
109
        }
110
    }
111
112
    /**
113
     * Updates host name
114
     *
115
     * @param string $hostname The hostname.
116
     */
117
    protected function updateHostName(string $hostname): void
118
    {
119
        $this->updatePbxSettings(PbxSettingsConstants::PBX_NAME, $hostname);
120
        $lanData = LanInterfaces::findFirst();
121
        if ($lanData !== null) {
122
            $lanData->hostname = $hostname;
123
            $lanData->save();
124
        }
125
    }
126
127
    /**
128
     * Updates the SSH password.
129
     */
130
    protected function updateSSHCredentials(string $sshLogin, string $hashSalt): void
131
    {
132
        $data = md5(shell_exec(Util::which('ifconfig')) . $hashSalt . time());
133
        $this->updatePbxSettings(PbxSettingsConstants::SSH_LOGIN, $sshLogin);
134
        $this->updatePbxSettings(PbxSettingsConstants::SSH_PASSWORD, $data);
135
        $this->updatePbxSettings(PbxSettingsConstants::SSH_DISABLE_SSH_PASSWORD, '1');
136
    }
137
138
    /**
139
     * Updates the web password based on the instance name and ID.
140
     *
141
     * @param string $webPassword The web password.
142
     */
143
    protected function updateWebPassword(string $webPassword): void
144
    {
145
        if (empty($webPassword)) {
146
            return;
147
        }
148
        $this->updatePbxSettings(PbxSettingsConstants::WEB_ADMIN_PASSWORD, $webPassword);
149
        $this->updatePbxSettings(PbxSettingsConstants::CLOUD_INSTANCE_ID, $webPassword);
150
        $this->updatePbxSettings(PbxSettingsConstants::PBX_DESCRIPTION, PbxSettingsConstants::DEFAULT_CLOUD_PASSWORD_DESCRIPTION);
151
    }
152
153
}
154