|
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; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Common\Providers\ConfigProvider; |
|
23
|
|
|
use Phalcon\Di; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SystemConfiguration class |
|
27
|
|
|
* |
|
28
|
|
|
* @package MikoPBX\Core\System |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
class SystemConfiguration extends Di\Injectable |
|
32
|
|
|
{ |
|
33
|
|
|
private string $configDBPath=''; |
|
34
|
|
|
private const DEFAULT_CONFIG_DB = '/conf.default/mikopbx.db'; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->configDBPath = $this->di->getShared(ConfigProvider::SERVICE_NAME)->path('database.dbfile'); |
|
39
|
|
|
} |
|
40
|
|
|
/** |
|
41
|
|
|
* Trying to restore the backup |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function tryRestoreConf():void |
|
45
|
|
|
{ |
|
46
|
|
|
$di = Di::getDefault(); |
|
47
|
|
|
if ($di === null) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
$storage = new Storage(); |
|
51
|
|
|
$storages = $storage->getStorageCandidate(); |
|
52
|
|
|
$tmpMountDir = '/tmp/mnt'; |
|
53
|
|
|
$confBackupDir = Directories::getDir(Directories::CORE_CONF_BACKUP_DIR); |
|
54
|
|
|
$backupDir = str_replace(['/storage/usbdisk1','/mountpoint'], ['',''], $confBackupDir); |
|
55
|
|
|
$confFile = $this->configDBPath; |
|
56
|
|
|
foreach ($storages as $dev => $fs){ |
|
57
|
|
|
SystemMessages::echoToTeletype(" - mount $dev ..."."\n", true); |
|
58
|
|
|
Util::mwMkdir($tmpMountDir."/$dev"); |
|
59
|
|
|
$res = Storage::mountDisk($dev, $fs, $tmpMountDir."/$dev"); |
|
60
|
|
|
if(!$res){ |
|
61
|
|
|
SystemMessages::echoToTeletype(" - fail mount $dev ..."."\n", true); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$tail = Util::which('tail'); |
|
66
|
|
|
$sort = Util::which('sort'); |
|
67
|
|
|
$find = Util::which('find'); |
|
68
|
|
|
$mount = Util::which('umount'); |
|
69
|
|
|
$rm = Util::which('rm'); |
|
70
|
|
|
$cut = Util::which('cut'); |
|
71
|
|
|
$gzip = Util::which('gzip'); |
|
72
|
|
|
$sqlite3 = Util::which('sqlite3'); |
|
73
|
|
|
$lastBackUp = trim(shell_exec("$find $tmpMountDir/dev/*$backupDir -type f -printf '%T@ %p\\n' | $sort -n | $tail -1 | $cut -f2- -d' '")); |
|
74
|
|
|
if(empty($lastBackUp)){ |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
SystemMessages::echoToTeletype(" - Restore $lastBackUp ..."."\n", true); |
|
78
|
|
|
shell_exec("$rm -rf {$confFile}*"); |
|
79
|
|
|
shell_exec("$gzip -c -d $lastBackUp | sqlite3 $confFile"); |
|
80
|
|
|
Processes::mwExec("$sqlite3 $confFile 'select * from m_Storage'", $out, $ret); |
|
81
|
|
|
if($ret !== 0){ |
|
82
|
|
|
SystemMessages::echoToTeletype(" - restore $lastBackUp failed..."."\n", true); |
|
83
|
|
|
copy(self::DEFAULT_CONFIG_DB, $confFile); |
|
84
|
|
|
}elseif(!$this->isDefaultConf()){ |
|
85
|
|
|
System::reboot(); |
|
86
|
|
|
} |
|
87
|
|
|
foreach ($storages as $dev => $fs){ |
|
88
|
|
|
shell_exec("$mount $dev"); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Is the configuration default? |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function isDefaultConf():bool |
|
97
|
|
|
{ |
|
98
|
|
|
$di = Di::getDefault(); |
|
99
|
|
|
if ($di === null) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
$confFile = $this->configDBPath; |
|
103
|
|
|
$defaultConfFile = self::DEFAULT_CONFIG_DB; |
|
104
|
|
|
$sqlite3 = Util::which('sqlite3'); |
|
105
|
|
|
$md5sum = Util::which('md5sum'); |
|
106
|
|
|
$cut = Util::which('cut'); |
|
107
|
|
|
$md5_1 = shell_exec("$sqlite3 $confFile .dump | $md5sum | $cut -f 1 -d ' '"); |
|
108
|
|
|
$md5_2 = shell_exec("$sqlite3 $defaultConfFile .dump | $md5sum | $cut -f 1 -d ' '"); |
|
109
|
|
|
return $md5_1 === $md5_2; |
|
110
|
|
|
} |
|
111
|
|
|
} |