-
Notifications
You must be signed in to change notification settings - Fork 71
/
Restore.php
212 lines (196 loc) · 8.68 KB
/
Restore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace FreePBX\modules\Core;
use FreePBX\modules\Backup as Base;
use SplObjectStorage;
use DirectoryIterator;
class Restore Extends Base\RestoreBase{
public function runRestore(){
global $astman;
$astman->database_deltree("AMPUSER");
$astman->database_deltree("AMPDEV");
$astman->database_deltree("CW");
$skipoptions = $this->getCliarguments();
$configs = $this->getConfigs();
$files = $this->getFiles();
$backupinfo = $this->getBackupInfo();
$dirs = $this->getDirs();
$excludetrunks = false;
if((isset($skipoptions['skiptrunksandroutes']) && $skipoptions['skiptrunksandroutes']) || ($backupinfo['warmspareenabled'] == 'yes' && $backupinfo['warmspare_excludetrunks'] == 'yes')) {
$excludetrunks = true;
$trunkConfig = $this->getTrunksconfig();
$outbound_routes = $this->getRouteConfigs();
$modulefunction = \module_functions::create();
$uninstall = $modulefunction->uninstall('core', 'true');
if(is_array($uninstall)) {
throw new \Exception(sprintf(_('Error uninstalling core reason(s): %s'),implode(",",$uninstall)));
}
$install = $modulefunction->install('core', 'true');
if(is_array($install)) {
throw new \Exception(sprintf(_('Error installing core reason(s): %s'),implode(",",$install)));
}
}
foreach ($this->getClasses($this->transactionId) as $class) {
if(empty($class)){
continue;
}
$this->log(sprintf(_("Processing %s"),$class->className));
if( $class->className == 'Trunks' && $excludetrunks) {
$class->setDirs($dirs)
->setbackupinfo($backupinfo)
->setFiles($files)
->setConfigs($trunkConfig);
}elseif( $class->className == 'Routing' && $excludetrunks){
$class->setDirs($dirs)
->setbackupinfo($backupinfo)
->setFiles($files)
->setConfigs($outbound_routes);
}
else {
$class->setDirs($dirs)
->setbackupinfo($backupinfo)
->setFiles($files);
$this->restoreDataFromDump($class->className, $this->tmpdir, $files);
if(isset($configs[$class->className])){
$class->setConfigs($configs[$class->className]);
}
}
}
if(isset($skipoptions['convertchansipexts2pjsip']) && $skipoptions['convertchansipexts2pjsip']) {
$this->log("chansip extensions will be converted to pjsip extensions!");
$this->FreePBX->Core->convert2pjsip();
}
if(isset($skipoptions['skipchansipexts']) && $skipoptions['skipchansipexts']) {
$this->log("chansip extensions will be skipped!");
$this->FreePBX->Core->skipchansip();
}
if(isset($skipoptions['convertchansiptrunks2pjsip']) && $skipoptions['convertchansiptrunks2pjsip']) {
$this->log("chansip trunks will be converted to pjsip trunks!");
$this->FreePBX->Core->chansipToPJSIP();
}
if(isset($skipoptions['skipchansiptrunks']) && $skipoptions['skipchansiptrunks']) {
$this->log("chansip trunks will be skipped!");
$this->FreePBX->Core->skipchansipTrunk();
}
$this->importKVStore($configs['kvstore']);
$this->importFeatureCodes($configs['features']);
$this->importAdvancedSettings($configs['settings']);
}
public function getClasses($transaction){
$classList = new DirectoryIterator(__DIR__ . '/Restore');
$classes = new SplObjectStorage();
foreach ($classList as $classItem) {
if ($classItem->isDot() || $classItem->getExtension() !== 'php') {
continue;
}
$classname = $classItem->getBasename('.php');
if ($classname === 'Corebase') {
continue;
}
$classname = '\\FreePBX\\modules\\Core\\Restore\\'.$classname;
$classes->attach(new $classname($this->FreePBX, $transaction));
}
return $classes;
}
public function processLegacy($pdo, $data, $tables, $unknownTables) {
global $astman;
$this->log("Processing legacy backup");
$skipoptions = $this->getCliarguments();
$excludetrunks = false;
if((isset($skipoptions['skiptrunksandroutes']) && $skipoptions['skiptrunksandroutes']) ) {
$excludetrunks = true;
$this->log("skiptrunksandroutes option passed ");
$trunkConfig = $this->getTrunksconfig();
$outbound_routes = $this->getRouteConfigs();
$this->log("preserve the trunk and outbound_routes");
$modulefunction = \module_functions::create();
$uninstall = $modulefunction->uninstall('core', 'true');
if(is_array($uninstall)) {
throw new \Exception(sprintf(_('Error uninstalling core reason(s): %s'),implode(",",$uninstall)));
}
$install = $modulefunction->install('core', 'true');
if(is_array($install)) {
throw new \Exception(sprintf(_('Error installing core reason(s): %s'),implode(",",$install)));
}
}
if($excludetrunks){
$this->log("skiping Trunks and outboundroutes From backup");
$ignoretables = ['pjsip','iax','trunks','outbound_route_patterns','outbound_route_sequence','outbound_route_trunks','outbound_routes','outbound_route_email','trunk_dialpatterns'];
$this->restoreLegacyDatabase($pdo,[],$ignoretables);
// remove the sip trunks from sip
$this->FreePBX->Database->query("Delete from sip where `id` like 'tr-peer-%'");
}else {
$this->restoreLegacyDatabase($pdo);
}
$this->restoreLegacyKvstore($pdo);
$this->restoreLegacyFeatureCodes($pdo);
$this->restoreLegacySettings($pdo);
$this->FreePBX->Core->devices2astdb();
if(isset($data['astdb']['AMPUSER'])) {
$ampuser = array();
foreach($data['astdb']['AMPUSER'] as $key => $value) {
$ampuser[] = ['AMPUSER' => [ $key => $value ]];
}
}
$astman->database_deltree("AMPUSER");
$this->importAstDB($ampuser);
if($excludetrunks){
foreach ($this->getClasses($this->transactionId) as $class) {
if(empty($class)){
continue;
}
$this->log(sprintf(_("Processing %s"),$class->className));
if( $class->className == 'Trunks' && $excludetrunks) {
$class->setConfigs($trunkConfig);
}elseif( $class->className == 'Routing' && $excludetrunks){
$class->setConfigs($outbound_routes);
}
}
}
if((isset($skipoptions['convertchansipexts2pjsip']) && $skipoptions['convertchansipexts2pjsip']) ) {
$this->log("chansip extensions will be converted to pjsip extensions!");
$this->FreePBX->Core->convert2pjsip();
}
if((isset($skipoptions['skipchansipexts']) && $skipoptions['skipchansipexts']) ) {
$this->log("chansip extensions will be skipped!");
$this->FreePBX->Core->skipchansip();
}
if(isset($skipoptions['convertchansiptrunks2pjsip']) && $skipoptions['convertchansiptrunks2pjsip']) {
$this->log("chansip trunks will be converted to pjsip trunks!");
$this->FreePBX->Core->chansipToPJSIP();
}
if(isset($skipoptions['skipchansiptrunks']) && $skipoptions['skipchansiptrunks']) {
$this->log("chansip trunks will be skipped!");
$this->FreePBX->Core->skipchansipTrunk();
}
}
private function getTrunksconfig(){
return [
"trunks" => $this->FreePBX->Database->query("SELECT * FROM trunks")->fetchAll(\PDO::FETCH_ASSOC),
"techTables" => [
"sip" => $this->FreePBX->Database->query("SELECT s.* FROM sip s, trunks t WHERE s.id in (CONCAT('tr-peer-', t.trunkid), CONCAT('tr-reg-', t.trunkid), CONCAT('tr-user-', t.trunkid))")->fetchAll(\PDO::FETCH_ASSOC),
"pjsip" => $this->FreePBX->Database->query("SELECT * FROM pjsip")->fetchAll(\PDO::FETCH_ASSOC),
"iax" => $this->FreePBX->Database->query("SELECT i.* FROM iax i, trunks t WHERE i.id in (CONCAT('tr-peer-', t.trunkid), CONCAT('tr-reg-', t.trunkid), CONCAT('tr-user-', t.trunkid))")->fetchAll(\PDO::FETCH_ASSOC)
],
"dialpatterns" => $this->FreePBX->Database->query("SELECT * FROM trunk_dialpatterns")->fetchAll(\PDO::FETCH_ASSOC)
];
}
public function getRouteConfigs(){
$final = [];
$routing = new \FreePBX\modules\Core\Components\Outboundrouting($this->FreePBX->Database);
$routes = $routing->listAll();
foreach($routes as $route){
$route['patterns'] = $routing->getRoutePatternsByID($route['route_id']);
$route['trunks'] = $routing->getRouteTrunksById($route['route_id']);
$final[] = $route;
}
return $final;
}
public function getResetInfo() {
$backupinfo = $this->getBackupInfo();
$skipoptions = $this->getCliarguments();
if ((isset($skipoptions['skiptrunksandroutes']) && $skipoptions['skiptrunksandroutes']) || (isset($backupinfo['warmspareenabled']) && isset($backupinfo['warmspare_excludetrunks']) && $backupinfo['warmspareenabled'] == 'yes' && $backupinfo['warmspare_excludetrunks'] == 'yes')) {
return true;
}
return false;
}
}