Passed
Push — develop ( 5e850f...f7133b )
by Nikolay
04:00
created

UpdateConfigsUpToVer20212183::moveDirCreateLink()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 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\FirewallRules;
23
use MikoPBX\Core\System\Processes;
24
use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface;
25
use MikoPBX\Core\System\Util;
26
use Phalcon\Di;
27
use Phalcon\Di\Injectable;
28
use MikoPBX\Core\System\MikoPBXConfig;
29
use Phalcon\Config as ConfigAlias;
30
31
class UpdateConfigsUpToVer20212183 extends Injectable implements UpgradeSystemConfigInterface
32
{
33
    public const PBX_VERSION = '20212183';
34
35
    private ConfigAlias $config;
36
    private MikoPBXConfig $mikoPBXConfig;
37
    private bool $isLiveCD;
38
39
    private const  OLD_MONITOR_PATH = '/storage/usbdisk1/mikopbx/voicemailarchive/monitor';
40
41
    private string $monitorDir = '/storage/usbdisk1/mikopbx/astspool/monitor';
42
    private string $mvPath;
43
    private string $findPath;
44
    private string $rmPath;
45
46
    /**
47
     * Class constructor.
48
     */
49
    public function __construct()
50
    {
51
        $this->config        = $this->getDI()->getShared('config');
52
        $this->mikoPBXConfig = new MikoPBXConfig();
53
        $this->isLiveCD      = file_exists('/offload/livecd');
54
55
        $di = Di::getDefault();
56
        if ($di !== null) {
57
            $this->monitorDir = $di->getConfig()->path('asterisk.monitordir');
58
        }
59
60
        $this->mvPath   = Util::which('mv');
61
        $this->rmPath   = Util::which('rm');
62
        $this->findPath = Util::which('find');
63
    }
64
65
    /**
66
     * Main function
67
     */
68
    public function processUpdate(): void
69
    {
70
        if ($this->isLiveCD) {
71
            return;
72
        }
73
        $this->moveOldRecords();
74
        $this->updateFirewallRules();
75
    }
76
77
    /**
78
     * Moves directory
79
     *
80
     * @param $oldPath
81
     * @param $newDir
82
     */
83
    public function moveDirCreateLink($oldPath, $newDir): void
84
    {
85
        if (file_exists($newDir)) {
86
            $lsDir = scandir($oldPath);
87
            foreach ($lsDir as $filename) {
88
                if ($filename === '.' || '..' === $filename) {
89
                    continue;
90
                }
91
                $_oldPath = $oldPath . "/" . $filename;
92
                $_newDir  = $newDir . "/" . $filename;
93
                $this->moveDirCreateLink($_oldPath, $_newDir);
94
            }
95
        } else {
96
            Processes::mwExec("{$this->mvPath} '{$oldPath}' '{$newDir}'");
97
        }
98
    }
99
100
    /**
101
     * Creates symlinks to old call records
102
     */
103
    private function moveOldRecords(): void
104
    {
105
        $lsDir = scandir(self::OLD_MONITOR_PATH);
106
        foreach ($lsDir as $filename) {
107
            if ($filename === '.' || '..' === $filename) {
108
                continue;
109
            }
110
            $oldPath = self::OLD_MONITOR_PATH . "/" . $filename;
111
            $newDir  = $this->monitorDir . "/" . $filename;
112
            $this->moveDirCreateLink($oldPath, $newDir);
113
            $out = [];
114
            Processes::mwExec("{$this->findPath} " . $this::OLD_MONITOR_PATH . " -type f", $out);
115
            if (count($out) !== 0) {
116
                Util::sysLogMsg(static::class, 'Error moving old recording dir.');
117
            } else {
118
                Processes::mwExec("{$this->rmPath} -rf '{$oldPath}'");
119
                Util::createUpdateSymlink($newDir, $oldPath);
120
            }
121
        }
122
    }
123
124
    /**
125
     * Fills new fields portFromKey and portToKey in FirewallRules table
126
     */
127
    private function updateFirewallRules(): void
128
    {
129
        $defaultRules = FirewallRules::getDefaultRules();
130
        $existsRules = FirewallRules::find();
131
        foreach ($existsRules as $rule){
132
            $category = $rule->category;
133
            foreach ($defaultRules as $defCategory=>$defaultRuleSet){
134
                if ($defCategory!==$category){
135
                    continue;
136
                }
137
                foreach ($defaultRuleSet as $defaultRule){
138
                    if ($defaultRule['portfrom']===$rule->portfrom){
139
                        $rule->portFromKey = $defaultRule['portFromKey'];
140
                    }
141
                    if ($defaultRule['portto']===$rule->portto){
142
                        $rule->portToKey = $defaultRule['portToKey'];
143
                    }
144
                }
145
            }
146
            $rule->update();
147
        }
148
    }
149
}