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

UpdateConfigsUpToVer20220316::addNewCodecs()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 19
rs 9.4222
cc 5
nc 6
nop 1
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 UpdateConfigsUpToVer20220316 extends Injectable implements UpgradeSystemConfigInterface
33
{
34
  	public const PBX_VERSION = '2022.3.16';
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
        $u = new UpdateConfigsUpToVer20220284();
49
        $u->processUpdate();
50
51
        $u = new UpdateConfigsUpToVer202202103();
52
        $u->processUpdate();
53
    }
54
55
    /**
56
     * Обновление TLS порта для сетевого экрана.
57
     * @return void
58
     */
59
    private function updateFirewallRules():void{
0 ignored issues
show
Unused Code introduced by
The method updateFirewallRules() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
60
        $colName = 'TLS_PORT';
61
        $portTls = PbxSettings::getValueByKey('TLS_PORT');
62
63
        /** @var NetworkFilters $net */
64
        $nets = NetworkFilters::find(['columns' => 'id']);
65
        foreach ($nets as $net){
66
            $ruleTls = FirewallRules::findFirst([
67
                                                    "portFromKey='$colName' AND networkfilterid='$net->id'",
68
                                                    'columns' => 'id']
69
            );
70
            if($ruleTls){
71
                continue;
72
            }
73
            $rules   = FirewallRules::findFirst([
74
                                                    "portFromKey='SIPPort' AND networkfilterid='$net->id'",
75
                                                    'columns' => 'action,networkfilterid,category,description']
76
            );
77
            if(!$rules){
78
                continue;
79
            }
80
81
            $ruleTls = FirewallRules::findFirst(["portFromKey='$colName' AND networkfilterid='$net->id'"]);
82
            if($ruleTls){
83
                continue;
84
            }
85
            $ruleTls = new FirewallRules();
86
            foreach ($rules->toArray() as $key => $value){
87
                $ruleTls->$key = $value;
88
            }
89
            $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...
90
            $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...
91
            $ruleTls->protocol    = 'tcp';
92
            $ruleTls->portFromKey = $colName;
93
            $ruleTls->portToKey   = $colName;
94
            $ruleTls->save();
95
        }
96
97
        $db_data = Sip::find("type = 'friend' AND ( disabled <> '1')");
98
        foreach ($db_data as $sip_peer) {
99
            if(!empty($sip_peer->registration_type)){
100
                continue;
101
            }
102
            if($sip_peer->noregister === '0'){
103
                $sip_peer->registration_type = Sip::REG_TYPE_OUTBOUND;
104
            }else{
105
                $sip_peer->registration_type = Sip::REG_TYPE_NONE;
106
            }
107
            $sip_peer->save();
108
        }
109
    }
110
111
    /**
112
     * Update codecs.
113
     */
114
    private function updateCodecs():void
0 ignored issues
show
Unused Code introduced by
The method updateCodecs() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
115
    {
116
        $availCodecs = [
117
            'g729'  => 'G.729',
118
        ];
119
        $this->addNewCodecs($availCodecs);
120
121
        /** @var Codecs $codecForRemove */
122
        $codecForRemove = Codecs::findFirst("name='g719'");
123
        if($codecForRemove){
0 ignored issues
show
introduced by
$codecForRemove is of type MikoPBX\Common\Models\Codecs, thus it always evaluated to true.
Loading history...
124
            $codecForRemove->delete();
125
        }
126
    }
127
128
    /**
129
     * Adds new codecs from $availCodecs array if it doesn't exist
130
     *
131
     * @param array $availCodecs
132
     */
133
    private function addNewCodecs(array $availCodecs): void
134
    {
135
        foreach ($availCodecs as $availCodec => $desc) {
136
            $codecData = Codecs::findFirst('name="' . $availCodec . '"');
137
            if ($codecData === null) {
138
                $codecData = new Codecs();
139
            } elseif ($codecData->description === $desc) {
140
                unset($codecData);
141
                continue;
142
            }
143
            $codecData->name = $availCodec;
144
            $codecData->type        = 'audio';
145
            $codecData->disabled    = '1';
146
            $codecData->description = $desc;
147
            if ( ! $codecData->save()) {
148
                Util::sysLogMsg(
149
                    __CLASS__,
150
                    'Can not update codec info ' . $codecData->name . ' from \MikoPBX\Common\Models\Codecs',
151
                    LOG_ERR
152
                );
153
            }
154
        }
155
    }
156
157
    private function updateExtensions():void
0 ignored issues
show
Unused Code introduced by
The method updateExtensions() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
158
    {
159
        if ($this->isLiveCD) {
0 ignored issues
show
Bug Best Practice introduced by
The property isLiveCD does not exist on MikoPBX\Core\System\Upgr...eConfigsUpToVer20220316. Since you implemented __get, consider adding a @property annotation.
Loading history...
160
            return;
161
        }
162
        $extensions = [
163
            'voicemail',
164
        ];
165
        foreach ($extensions as $extension){
166
            $data                = Extensions::findFirst('number="' . $extension . '"');
167
            if ($data===null) {
168
                $data                    = new Extensions();
169
                $data->number            = $extension;
170
            }
171
            $data->type              = Extensions::TYPE_SYSTEM;
172
            $data->callerid          = 'System Extension';
173
            $data->public_access     = 0;
174
            $data->show_in_phonebook = 1;
175
            $data->save();
176
        }
177
    }
178
}