Passed
Push — develop ( 4a07e0...593b5c )
by Портнов
13:37 queued 12s
created

UpdateConfigsUpToVer2022020103   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 65
dl 0
loc 122
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addNewCodecs() 0 19 5
B updateFirewallRules() 0 49 9
A processUpdate() 0 7 2
A __construct() 0 3 1
A updateCodecs() 0 11 2
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\FirewallRules;
24
use MikoPBX\Common\Models\NetworkFilters;
25
use MikoPBX\Common\Models\PbxSettings;
26
use MikoPBX\Common\Models\Sip;
27
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
28
use MikoPBX\Core\System\Util;
29
use Phalcon\Di\Injectable;
30
31
class UpdateConfigsUpToVer2022020103 extends Injectable implements UpgradeSystemConfigInterface
32
{
33
  	public const PBX_VERSION = '2022.2.103';
34
    private bool $isLiveCD;
35
36
	/**
37
     * Class constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->isLiveCD      = file_exists('/offload/livecd');
42
    }
43
44
    /**
45
     * https://github.com/mikopbx/Core/issues/269
46
     */
47
    public function processUpdate():void
48
    {
49
        if ($this->isLiveCD) {
50
            return;
51
        }
52
        $this->updateFirewallRules();
53
        $this->updateCodecs();
54
    }
55
56
    /**
57
     * Обновление TLS порта для сетевого экрана.
58
     * @return void
59
     */
60
    private function updateFirewallRules():void{
61
        $colName = 'TLS_PORT';
62
        $portTls = PbxSettings::getValueByKey('TLS_PORT');
63
64
        /** @var NetworkFilters $net */
65
        $nets = NetworkFilters::find(['columns' => 'id']);
66
        foreach ($nets as $net){
67
            $ruleTls = FirewallRules::findFirst([
68
                                                    "portFromKey='$colName' AND networkfilterid='$net->id'",
69
                                                    'columns' => 'id']
70
            );
71
            if($ruleTls){
72
                continue;
73
            }
74
            $rules   = FirewallRules::findFirst([
75
                                                    "portFromKey='SIPPort' AND networkfilterid='$net->id'",
76
                                                    'columns' => 'action,networkfilterid,category,description']
77
            );
78
            if(!$rules){
79
                continue;
80
            }
81
82
            $ruleTls = FirewallRules::findFirst(["portFromKey='$colName' AND networkfilterid='$net->id'"]);
83
            if($ruleTls){
84
                continue;
85
            }
86
            $ruleTls = new FirewallRules();
87
            foreach ($rules->toArray() as $key => $value){
88
                $ruleTls->$key = $value;
89
            }
90
            $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...
91
            $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...
92
            $ruleTls->protocol    = 'tcp';
93
            $ruleTls->portFromKey = $colName;
94
            $ruleTls->portToKey   = $colName;
95
            $ruleTls->save();
96
        }
97
98
        $db_data = Sip::find("type = 'friend' AND ( disabled <> '1')");
99
        foreach ($db_data as $sip_peer) {
100
            if(!empty($sip_peer->registration_type)){
101
                continue;
102
            }
103
            if($sip_peer->noregister === '0'){
104
                $sip_peer->registration_type = Sip::REG_TYPE_OUTBOUND;
105
            }else{
106
                $sip_peer->registration_type = Sip::REG_TYPE_NONE;
107
            }
108
            $sip_peer->save();
109
        }
110
    }
111
112
    /**
113
     * Update codecs.
114
     */
115
    private function updateCodecs():void
116
    {
117
        $availCodecs = [
118
            'g729'  => 'G.729',
119
        ];
120
        $this->addNewCodecs($availCodecs);
121
122
        /** @var Codecs $codecForRemove */
123
        $codecForRemove = Codecs::findFirst("name='g719'");
124
        if($codecForRemove){
0 ignored issues
show
introduced by
$codecForRemove is of type MikoPBX\Common\Models\Codecs, thus it always evaluated to true.
Loading history...
125
            $codecForRemove->delete();
126
        }
127
    }
128
129
    /**
130
     * Adds new codecs from $availCodecs array if it doesn't exist
131
     *
132
     * @param array $availCodecs
133
     */
134
    private function addNewCodecs(array $availCodecs): void
135
    {
136
        foreach ($availCodecs as $availCodec => $desc) {
137
            $codecData = Codecs::findFirst('name="' . $availCodec . '"');
138
            if ($codecData === null) {
139
                $codecData = new Codecs();
140
            } elseif ($codecData->description === $desc) {
141
                unset($codecData);
142
                continue;
143
            }
144
            $codecData->name = $availCodec;
145
            $codecData->type        = 'audio';
146
            $codecData->disabled    = '1';
147
            $codecData->description = $desc;
148
            if ( ! $codecData->save()) {
149
                Util::sysLogMsg(
150
                    __CLASS__,
151
                    'Can not update codec info ' . $codecData->name . ' from \MikoPBX\Common\Models\Codecs',
152
                    LOG_ERR
153
                );
154
            }
155
        }
156
    }
157
}