Passed
Push — develop ( 4b3685...bcfbc9 )
by Портнов
04:21
created

updateFirewallRules()   B

Complexity

Conditions 9
Paths 24

Size

Total Lines 49
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

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