Passed
Push — develop ( 75092d...34130d )
by Портнов
04:55
created

UpdateConfigsUpToVer2022020103::updateExtensions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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\Upgrade\Releases;
21
22
use MikoPBX\Common\Models\Codecs;
23
use MikoPBX\Common\Models\Extensions;
24
use MikoPBX\Common\Models\FirewallRules;
25
use MikoPBX\Common\Models\NetworkFilters;
26
use MikoPBX\Common\Models\PbxSettings;
27
use MikoPBX\Common\Models\Sip;
28
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
29
use MikoPBX\Core\System\Util;
30
use Phalcon\Di\Injectable;
31
32
class UpdateConfigsUpToVer2022020103 extends Injectable implements UpgradeSystemConfigInterface
33
{
34
  	public const PBX_VERSION = '2022.2.103';
35
    private bool $isLiveCD;
36
37
	/**
38
     * Class constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->isLiveCD      = file_exists('/offload/livecd');
43
    }
44
45
    /**
46
     * https://github.com/mikopbx/Core/issues/269
47
     */
48
    public function processUpdate():void
49
    {
50
        if ($this->isLiveCD) {
51
            return;
52
        }
53
        $this->updateFirewallRules();
54
        $this->updateCodecs();
55
        $this->updateExtensions();
56
    }
57
58
    /**
59
     * Обновление TLS порта для сетевого экрана.
60
     * @return void
61
     */
62
    private function updateFirewallRules():void{
63
        $colName = 'TLS_PORT';
64
        $portTls = PbxSettings::getValueByKey('TLS_PORT');
65
66
        /** @var NetworkFilters $net */
67
        $nets = NetworkFilters::find(['columns' => 'id']);
68
        foreach ($nets as $net){
69
            $ruleTls = FirewallRules::findFirst([
70
                                                    "portFromKey='$colName' AND networkfilterid='$net->id'",
71
                                                    'columns' => 'id']
72
            );
73
            if($ruleTls){
74
                continue;
75
            }
76
            $rules   = FirewallRules::findFirst([
77
                                                    "portFromKey='SIPPort' AND networkfilterid='$net->id'",
78
                                                    'columns' => 'action,networkfilterid,category,description']
79
            );
80
            if(!$rules){
81
                continue;
82
            }
83
84
            $ruleTls = FirewallRules::findFirst(["portFromKey='$colName' AND networkfilterid='$net->id'"]);
85
            if($ruleTls){
86
                continue;
87
            }
88
            $ruleTls = new FirewallRules();
89
            foreach ($rules->toArray() as $key => $value){
90
                $ruleTls->$key = $value;
91
            }
92
            $ruleTls->portfrom    = $portTls;
0 ignored issues
show
Documentation Bug introduced by
It seems like $portTls of type string is incompatible with the declared type integer|null of property $portfrom.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
93
            $ruleTls->portto      = $portTls;
0 ignored issues
show
Documentation Bug introduced by
It seems like $portTls of type string is incompatible with the declared type integer|null of property $portto.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
            $ruleTls->protocol    = 'tcp';
95
            $ruleTls->portFromKey = $colName;
96
            $ruleTls->portToKey   = $colName;
97
            $ruleTls->save();
98
        }
99
100
        $db_data = Sip::find("type = 'friend' AND ( disabled <> '1')");
101
        foreach ($db_data as $sip_peer) {
102
            if(!empty($sip_peer->registration_type)){
103
                continue;
104
            }
105
            if($sip_peer->noregister === '0'){
106
                $sip_peer->registration_type = Sip::REG_TYPE_OUTBOUND;
107
            }else{
108
                $sip_peer->registration_type = Sip::REG_TYPE_NONE;
109
            }
110
            $sip_peer->save();
111
        }
112
    }
113
114
    /**
115
     * Update codecs.
116
     */
117
    private function updateCodecs():void
118
    {
119
        $availCodecs = [
120
            'g729'  => 'G.729',
121
        ];
122
        $this->addNewCodecs($availCodecs);
123
124
        /** @var Codecs $codecForRemove */
125
        $codecForRemove = Codecs::findFirst("name='g719'");
126
        if($codecForRemove){
0 ignored issues
show
introduced by
$codecForRemove is of type MikoPBX\Common\Models\Codecs, thus it always evaluated to true.
Loading history...
127
            $codecForRemove->delete();
128
        }
129
    }
130
131
    /**
132
     * Adds new codecs from $availCodecs array if it doesn't exist
133
     *
134
     * @param array $availCodecs
135
     */
136
    private function addNewCodecs(array $availCodecs): void
137
    {
138
        foreach ($availCodecs as $availCodec => $desc) {
139
            $codecData = Codecs::findFirst('name="' . $availCodec . '"');
140
            if ($codecData === null) {
141
                $codecData = new Codecs();
142
            } elseif ($codecData->description === $desc) {
143
                unset($codecData);
144
                continue;
145
            }
146
            $codecData->name = $availCodec;
147
            $codecData->type        = 'audio';
148
            $codecData->disabled    = '1';
149
            $codecData->description = $desc;
150
            if ( ! $codecData->save()) {
151
                Util::sysLogMsg(
152
                    __CLASS__,
153
                    'Can not update codec info ' . $codecData->name . ' from \MikoPBX\Common\Models\Codecs',
154
                    LOG_ERR
155
                );
156
            }
157
        }
158
    }
159
160
    private function updateExtensions():void
161
    {
162
        if ($this->isLiveCD) {
163
            return;
164
        }
165
        $extensions = [
166
            'voicemail',
167
        ];
168
        foreach ($extensions as $extension){
169
            $data                = Extensions::findFirst('number="' . $extension . '"');
170
            if ($data===null) {
171
                $data                    = new Extensions();
172
                $data->number            = $extension;
173
            }
174
            $data->type              = Extensions::TYPE_SYSTEM;
175
            $data->callerid          = 'System Extension';
176
            $data->public_access     = 0;
177
            $data->show_in_phonebook = 1;
178
            $data->save();
179
        }
180
    }
181
}