Skip to content

Commit

Permalink
Add legacy backup
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinstrom committed Aug 20, 2018
1 parent 3914886 commit f4bd84d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
14 changes: 14 additions & 0 deletions Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@ class Restore Extends Base\RestoreBase{
public function runRestore($jobid){
$this->FreePBX->Superfecta->loadConfigs($this->getConfigs());
}

public function processLegacy($pdo, $data, $tables, $unknownTables, $tmpfiledir){
$tables = array_flip($tables+$unknownTables);
if(!isset(tables['superfectaconfig'])){
return $this;
}
$bmo = $this->FreePBX->Superfecta;
$bmo->setDatabase($pdo);
$configs = $bmo->dumpConfigs()
$bmo->resetDatabase();
$bmo->loadConfigs($configs);
return $this;
}

}
84 changes: 47 additions & 37 deletions Superfecta.class.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php
// vim: set ai ts=4 sw=4 ft=php:
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2013 POSSA Working Group
//
/*
* License for all code of this FreePBX module can be found in the license file inside the module directory
* Copyright (c) 2013 POSSA Working Group
* Copyright (c) 2018 Sangoma Technologies
*/


namespace FreePBX\modules;
use BMO;
use FreePBX_Helpers;
Expand All @@ -29,7 +33,7 @@ class Superfecta extends FreePBX_Helpers implements BMO {

public function install() {
$sql = "SELECT * FROM superfectaconfig LIMIT 1;";
$res = $this->FreePBX->Database->query($sql);
$res = $this->Database->query($sql);
if ($res->rowCount() != 1) {
$defaults = [
[':source' => 'base_Default', ':field' => 'order', ':value' => '0'],
Expand All @@ -41,7 +45,7 @@ public function install() {
[':source' => 'PhoneSpamFilter', ':field' => 'SPAM_Threshold', ':value' => '5'],
];
$sql = "REPLACE INTO superfectaconfig (source,field,value) VALUES(:source, :field, :value)";
$stmt = $this->FreePBX->Database->prepare($sql);
$stmt = $this->Database->prepare($sql);
foreach ($defaults as $default) {
$stmt->execute($default);
}
Expand All @@ -50,12 +54,7 @@ public function install() {
public function uninstall() {

}
public function backup(){

}
public function restore($backup){

}
public function doConfigPageInit($page){
return true;
}
Expand Down Expand Up @@ -118,7 +117,7 @@ public function execute($scheme='ALL', $request, $debug=0, $keepGoing=false) {

global $db, $amp_conf, $astman;
$options = array(
'db' => $this->FreePBX->Database,
'db' => $this->Database,
'amp_conf' => $amp_conf,
'astman' => $astman,
'debug' => 0,
Expand Down Expand Up @@ -354,7 +353,7 @@ public function ajaxHandler() {
case "sort":
$oper = $_POST['position'] == 'up' ? "-" : "+";
$sql = "UPDATE superfectaconfig SET value = ABS(value ".$oper." 11) WHERE source = ? AND field = 'order'";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$_REQUEST['scheme']));
$this->reorderSchemes();
return array("status" => true);
Expand All @@ -364,23 +363,23 @@ public function ajaxHandler() {

/** GET SCHEME CONFIGS **/
$sql = "SELECT * FROM superfectaconfig WHERE source = ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$scheme));
$options = $sth->fetchAll(PDO::FETCH_ASSOC);
$sql = "INSERT INTO superfectaconfig (source, field, value) VALUES (?,?,?)";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
foreach($options as $option) {
$source = preg_replace('/^base_'.$scheme.'/','base_'.$scheme.'copy'.$int,$option['source']);
$sth->execute(array($source,$option['field'],$option['value']));
}

/** GET MODULE CONFIGS **/
$sql = "SELECT * FROM superfectaconfig WHERE source LIKE ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($scheme.'\_%'));
$options = $sth->fetchAll(PDO::FETCH_ASSOC);
$sql = "INSERT INTO superfectaconfig (source, field, value) VALUES (?,?,?)";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
foreach($options as $option) {
$source = preg_replace('/^'.$scheme.'_/','base_'.$scheme.'copy'.$int.'_',$option['source']);
$sth->execute(array($source,$option['field'],$option['value']));
Expand Down Expand Up @@ -446,18 +445,18 @@ public function ajaxHandler() {
case "update_sources":
$sources = isset($_REQUEST['data'])?implode(",", $_REQUEST['data']):'';
$sql = "REPLACE INTO superfectaconfig (value, source, field) VALUES(?, ?, 'sources')";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($sources, 'base_'.$_REQUEST['scheme']));
return array("success" => true);
case "power":
$data = preg_replace('/^scheme_/i', '', $_REQUEST['scheme']);
$sql = "UPDATE superfectaconfig SET value = (value * -1) WHERE field = 'order' AND source = ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$data));
return array("status" => true);
case "delete":
$sql = "DELETE FROM superfectaconfig WHERE source LIKE ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$_REQUEST['scheme']));
$sth->execute(array('base_'.$_REQUEST['scheme'].'\_%'));
$this->reorderSchemes();
Expand All @@ -475,7 +474,7 @@ public function ajaxHandler() {
$scheme = $_REQUEST['scheme'];
$source = $_REQUEST['source'];
$sql = "REPLACE INTO superfectaconfig (source,field,value) VALUES (?, ?, ?)";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
foreach($params as $key => $data) {
if (strcmp($data['type'], 'internal') != 0) {
$sth->execute(array($scheme . "_" . $source, $key, $_POST[$key]));
Expand All @@ -488,7 +487,7 @@ public function ajaxHandler() {
$source = $_REQUEST['source'];

$sql = "SELECT field, value FROM superfectaconfig WHERE source = ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($scheme . "_" . $source));
$n_settings = $sth->fetchAll(PDO::FETCH_KEY_PAIR);

Expand Down Expand Up @@ -569,12 +568,12 @@ public function ajaxHandler() {
public function reorderSchemes() {
$sql = "SELECT * FROM superfectaconfig WHERE field = 'order' ORDER BY CONVERT(value, SIGNED INTEGER)";
$start = 1;
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $result) {
$sql = "UPDATE superfectaconfig SET value = ? WHERE source = ? AND field = 'order'";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($start*10, $result['source']));
$start++;
}
Expand All @@ -594,7 +593,7 @@ public function getAllPoweredSchemes() {

public function getAllSchemes() {
$sql = "SELECT source as scheme, value as powered FROM superfectaconfig WHERE source LIKE 'base\_%' AND field = 'order' ORDER BY ABS(CONVERT(value, SIGNED INTEGER))";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);

Expand Down Expand Up @@ -633,12 +632,12 @@ public function updateScheme($scheme, $data = array()) {
return array("status" => false, "message" => _("You cannot rename a scheme the same thing as an existing scheme"));
} else {
$sql = "UPDATE superfectaconfig SET source = REPLACE(source, ?, ?) WHERE source LIKE ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$scheme, 'base_'.$data['scheme_name'], 'base_'.$scheme));
$sth->execute(array('base_'.$scheme.'_', 'base_'.$data['scheme_name'].'_', 'base_'.$scheme.'\_%'));

$sql = "UPDATE superfecta_to_incoming SET scheme = ? WHERE scheme = ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$data['scheme_name'], 'base_'.$scheme));

$scheme = 'base_'.$data['scheme_name'];
Expand All @@ -648,7 +647,7 @@ public function updateScheme($scheme, $data = array()) {
}

$sql = "REPLACE INTO superfectaconfig (source,field,value) VALUES(?,?,?)";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($scheme,'spam_interceptor',(!empty($data['enable_interceptor']) && $data['enable_interceptor'] == 'Y' ? 'Y' : 'N')));
$sth->execute(array($scheme,'spam_destination',$data['destination']));
$sth->execute(array($scheme,'Prefix_URL',$data['Prefix_URL']));
Expand All @@ -671,7 +670,7 @@ public function getScheme($scheme) {
$scheme = preg_replace('/^base_/','',$scheme);
//set some default values for creating a new scheme
$sql = "SELECT field, value FROM superfectaconfig WHERE source = ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array('base_'.$scheme));
$return = $sth->fetchAll(PDO::FETCH_KEY_PAIR);

Expand All @@ -698,7 +697,7 @@ public function getScheme($scheme) {

public function getSchemeAllModuleSettings($scheme) {
$sql = "SELECT source, field, value FROM superfectaconfig WHERE source LIKE ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($scheme.'\_%'));
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
$return = array();
Expand All @@ -714,7 +713,7 @@ public function getSchemeAllModuleSettings($scheme) {

public function getSchemeModuleSettings($scheme, $module) {
$sql = "SELECT source, field, value FROM superfectaconfig WHERE source LIKE ?";
$sth = $this->FreePBX->Database->prepare($sql);
$sth = $this->Database->prepare($sql);
$sth->execute(array($scheme.'\_'.$module.'\_%'));
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
$return = array();
Expand Down Expand Up @@ -760,19 +759,19 @@ public function didList($id = false){
$sql .= " WHERE superfecta_to_incoming_id = :id";
$vars = [':id' => $id];
}
$stmt = $this->FreePBX->Database->prepare($sql);
$stmt = $this->Database->prepare($sql);
$stmt->execute($vars);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return is_array($results) ? $results : array();
}

public function dumpConfigs(){
return [
'configs' => $this->FreePBX->Database->query('SELECT * FROM superfectaconfig')->fetchAll(PDO::FETCH_ASSOC),
'cache' => $this->FreePBX->Database->query('SELECT * FROM superfectacache')->fetchAll(PDO::FETCH_ASSOC),
'incoming' => $this->FreePBX->Database->query('SELECT * FROM superfecta_to_incoming')->fetchAll(PDO::FETCH_ASSOC),
'mf' => $this->FreePBX->Database->query('SELECT * FROM superfecta_mf_child')->fetchAll(PDO::FETCH_ASSOC),
'mf_child' => $this->FreePBX->Database->query('SELECT * FROM superfecta_mf_child')->fetchAll(PDO::FETCH_ASSOC),
'configs' => $this->Database->query('SELECT * FROM superfectaconfig')->fetchAll(PDO::FETCH_ASSOC),
'cache' => $this->Database->query('SELECT * FROM superfectacache')->fetchAll(PDO::FETCH_ASSOC),
'incoming' => $this->Database->query('SELECT * FROM superfecta_to_incoming')->fetchAll(PDO::FETCH_ASSOC),
'mf' => $this->Database->query('SELECT * FROM superfecta_mf_child')->fetchAll(PDO::FETCH_ASSOC),
'mf_child' => $this->Database->query('SELECT * FROM superfecta_mf_child')->fetchAll(PDO::FETCH_ASSOC),
];
}
public function loadConfigs($configs){
Expand All @@ -797,10 +796,21 @@ public function loadConfigs($configs){
}
}
if($sql !== false){
$stmt = $this->FreePBX->Database->prepare($sql);
$stmt = $this->Database->prepare($sql);
foreach ($data as $item) {
$stmt->execute($item);
}
}
}

public function setDatabase($pdo){
$this->Database = $pdo;
return $this;
}

public function resetDatabase(){
$this->Database = $this->FreePBX->Database;
return $this;
}

}

0 comments on commit f4bd84d

Please sign in to comment.