Passed
Push — develop ( ab926b...704199 )
by Nikolay
13:25 queued 08:47
created

UpdateConfigsUpToVer202402   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 73
dl 0
loc 116
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateFirewallRulesForIAX() 0 27 4
A updateSearchIndex() 0 52 3
A processUpdate() 0 7 2
A __construct() 0 5 1
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\Upgrade\Releases;
21
22
use MikoPBX\Common\Models\Extensions;
23
use MikoPBX\Common\Models\FirewallRules;
24
use MikoPBX\Common\Models\NetworkFilters;
25
use MikoPBX\Common\Models\PbxSettings;
26
use MikoPBX\Common\Models\PbxSettingsConstants;
27
use MikoPBX\Common\Models\Sip;
28
use MikoPBX\Common\Models\Users;
29
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
30
use Phalcon\Di\Injectable;
31
use MikoPBX\Core\System\MikoPBXConfig;
32
use Phalcon\Config as ConfigAlias;
33
34
class UpdateConfigsUpToVer202402 extends Injectable implements UpgradeSystemConfigInterface
35
{
36
  	public const PBX_VERSION = '2024.2.3';
37
38
	private ConfigAlias $config;
39
    private MikoPBXConfig $mikoPBXConfig;
40
    private bool $isLiveCD;
41
42
	/**
43
     * Class constructor.
44
     */
45
    public function __construct()
46
    {
47
        $this->config = $this->getDI()->getShared('config');
48
        $this->mikoPBXConfig = new MikoPBXConfig();
49
        $this->isLiveCD      = file_exists('/offload/livecd');
50
    }
51
52
	/**
53
     * Main function
54
     */
55
    public function processUpdate():void
56
    {
57
  		if ($this->isLiveCD) {
58
            return;
59
        }
60
        $this->updateSearchIndex();
61
        $this->updateFirewallRulesForIAX();
62
    }
63
64
    private function updateSearchIndex():void
65
    {
66
        $parameters = [
67
            'models' => [
68
                'Users' => Users::class,
69
            ],
70
            'joins' => [
71
                'Sip' => [
72
                    0 => Sip::class,
73
                    1 => 'Sip.extension=Extensions.number',
74
                    2 => 'Sip',
75
                    3 => 'INNER',
76
                ],
77
                'Extensions' => [
78
                    0 => Extensions::class,
79
                    1 => 'Extensions.userid=Users.id and Extensions.is_general_user_number = "1" and Extensions.type="' . Extensions::TYPE_SIP . '"',
80
                    2 => 'Extensions',
81
                    3 => 'INNER',
82
                ],
83
                'ExternalExtensions' => [
84
                    0 => Extensions::class,
85
                    1 => 'ExternalExtensions.userid=Users.id and Extensions.is_general_user_number = "1" and ExternalExtensions.type="' . Extensions::TYPE_EXTERNAL . '"',
86
                    2 => 'ExternalExtensions',
87
                    3 => 'LEFT',
88
                ],
89
            ],
90
            'columns' => [
91
                'extensionId'=>'Extensions.id',
92
                'user_username'=>'Users.username',
93
                'user_email'=>'Users.email',
94
                'extension_number'=>'Extensions.number',
95
                'mobile_number'=>'ExternalExtensions.number',
96
                'extension_callerid'=>'Extensions.callerid',
97
            ]
98
        ];
99
        $query = $this->di->get('modelsManager')->createBuilder($parameters)->getQuery();
100
        $selectedUsers = $query->execute()->toArray();
101
        foreach ($selectedUsers as $record) {
102
            $extension = Extensions::findFirst($record['extensionId']);
103
            if (!$extension) {
104
                continue;
105
            }
106
            // Collect data for the search index
107
            $username = mb_strtolower($record['user_username']??'');
108
            $callerId = mb_strtolower($record['extension_callerid']??'');
109
            $email = mb_strtolower($record['user_email']??'');
110
            $internalNumber = mb_strtolower($record['extension_number']??'');
111
            $mobileNumber = mb_strtolower($record['mobile_number']??'');
112
113
            // Combine all fields into a single string
114
            $extension->search_index =  $username . ' ' . $callerId . ' ' . $email . ' ' . $internalNumber . ' ' . $mobileNumber;
115
            $extension->save();
116
        }
117
    }
118
119
    /**
120
     * Update firewall rules for IAX
121
     * https://github.com/mikopbx/Core/issues/782
122
     */
123
    public function updateFirewallRulesForIAX(): void
124
    {
125
        $colName  = PbxSettingsConstants::IAX_PORT;
126
        $iax_port = PbxSettings::getValueByKey(PbxSettingsConstants::IAX_PORT);
127
        $nets = NetworkFilters::find(['columns' => 'id']);
128
        foreach ($nets as $net){
129
            $filter = [
130
                "portFromKey='$colName' AND networkfilterid='$net->id'",
131
                'columns' => 'id'
132
            ];
133
            $rule = FirewallRules::findFirst($filter);
134
            if($rule){
135
                continue;
136
            }
137
            $rule = new FirewallRules();
138
            foreach ($rule->toArray() as $key => $value){
139
                $rule->$key = $value;
140
            }
141
            $rule->networkfilterid = $net->id;
142
            $rule->action       = 'block';
143
            $rule->portfrom    = $iax_port;
144
            $rule->portto      = $iax_port;
145
            $rule->protocol    = 'udp';
146
            $rule->portFromKey = $colName;
147
            $rule->portToKey   = $colName;
148
            $rule->category    = 'IAX';
149
            $rule->save();
150
        }
151
    }
152
}