Passed
Push — develop ( 6fe1b7...4c812d )
by Nikolay
05:04
created

WorkerModuleInstaller::installNewModuleFromFile()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 30
rs 9.2888
cc 5
nc 7
nop 3
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2021 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\PBXCoreREST\Workers;
21
22
23
require_once 'Globals.php';
24
25
use MikoPBX\Core\System\Processes;
26
use MikoPBX\Core\Workers\WorkerBase;
27
use MikoPBX\Core\System\Util;
28
use Throwable;
29
30
class WorkerModuleInstaller extends WorkerBase
31
{
32
33
    private string $progress_file = '';
34
    private string $error_file = '';
35
36
    /**
37
     * @param mixed $params
38
     */
39
    public function start($params): void
40
    {
41
        $settings_file = $params[2]??'';
42
        if ( ! file_exists($settings_file)) {
43
            Util::sysLogMsg(__CLASS__, 'File with settings did not found', LOG_ERR);
44
45
            return;
46
        }
47
        $settings = json_decode(file_get_contents($settings_file), true);
48
        $temp_dir            = dirname($settings['filePath']);
49
        $this->progress_file = $temp_dir . '/installation_progress';
50
        $this->error_file    = $temp_dir . '/installation_error';
51
        $this->installNewModuleFromFile(
52
            $settings['currentModuleDir'],
53
            $settings['filePath'],
54
            $settings['uniqid']
55
        );
56
57
    }
58
59
    /**
60
     * Starts module installation on separate php process
61
     *
62
     */
63
    private function installNewModuleFromFile(
64
        string $currentModuleDir,
65
        string $filePath,
66
        string $moduleUniqueID
67
    ): void {
68
        file_put_contents( $this->progress_file, '0');
69
        file_put_contents( $this->error_file, '');
70
71
        // Unzip module folder
72
        $semZaPath = Util::which('7za');
73
        Processes::mwExec("{$semZaPath} e -spf -aoa -o{$currentModuleDir} {$filePath}");
74
        Util::addRegularWWWRights($currentModuleDir);
75
        file_put_contents( $this->progress_file, '50');
76
        $pbxExtensionSetupClass = "\\Modules\\{$moduleUniqueID}\\Setup\\PbxExtensionSetup";
77
        if (class_exists($pbxExtensionSetupClass)
78
            && method_exists($pbxExtensionSetupClass, 'installModule')) {
79
            try {
80
                $setup = new $pbxExtensionSetupClass($moduleUniqueID);
81
                if ( ! $setup->installModule()) {
82
                    file_put_contents($this->error_file, implode(" ", $setup->getMessages()), FILE_APPEND);
83
                } else {
84
                    Processes::restartAllWorkers();
85
                }
86
            } catch (Throwable $e){
87
                file_put_contents($this->error_file, $e->getMessage(), FILE_APPEND);
88
            }
89
        } else {
90
            file_put_contents($this->error_file,"Install error: the class {$pbxExtensionSetupClass} not exists", FILE_APPEND);
91
        }
92
        file_put_contents( $this->progress_file, '100');
93
    }
94
}
95
96
// Start worker process
97
WorkerModuleInstaller::startWorker($argv??null);