From 09db30fe9fa1b94ef88cebafe7b24588f30851d2 Mon Sep 17 00:00:00 2001
From: steve-lad <72376554+steve-lad@users.noreply.github.com>
Date: Mon, 14 Jun 2021 11:47:09 +0200
Subject: [PATCH] Update Uninstaller
Create Backup of SCCP tables
Remove all tables
Remove views and keys
---
install.php | 8 ++--
uninstall.php | 102 +++++++++++++++++++++++++++++++++-----------------
2 files changed, 72 insertions(+), 38 deletions(-)
diff --git a/install.php b/install.php
index 0128f90..6f73ae7 100644
--- a/install.php
+++ b/install.php
@@ -45,7 +45,7 @@ $db_config = Get_DB_config($sccp_compatible);
$sccp_db_ver = CheckSCCPManagerDBVersion();
// BackUp Old config
-CreateBackUpConfig();
+createBackUpConfig();
RenameConfig();
InstallDB_updateSchema($db_config);
@@ -708,7 +708,7 @@ function InstallDB_CreateSccpDeviceConfigView($sccp_compatible)
}
return true;
}
-function CreateBackUpConfig()
+function createBackUpConfig()
{
global $amp_conf;
outn("
" . _("Creating Config BackUp") . "");
@@ -718,7 +718,7 @@ function CreateBackUpConfig()
$dir = $cnf_int->get('ASTETCDIR');
$fsql = $dir.'/sccp_backup_'.date("Ymd").'.sql';
- $result = exec('mysqldump '.$amp_conf['AMPDBNAME'].' --password='.$amp_conf['AMPDBPASS'].' --user='.$amp_conf['AMPDBUSER'].' --single-transaction >'.$fsql, $output);
+ $result = exec('mysqldump '.$amp_conf['AMPDBNAME'].' --password='.$amp_conf['AMPDBPASS'].' --user='.$amp_conf['AMPDBUSER'].' --single-transaction >'.$fsql);
try {
$zip = new \ZipArchive();
@@ -744,7 +744,7 @@ function CreateBackUpConfig()
outn("" . _("Error Creating BackUp: ") . $filename ."");
}
unlink($fsql);
- outn("" . _("Create Config BackUp: ") . $filename ."");
+ outn("" . _("Config backup created: ") . $filename ."");
}
function RenameConfig()
diff --git a/uninstall.php b/uninstall.php
index e3b2651..c4c7956 100644
--- a/uninstall.php
+++ b/uninstall.php
@@ -36,46 +36,80 @@ if (!defined('FREEPBX_IS_AUTH')) {
global $db;
$version = FreePBX::Config()->get('ASTVERSION');
+global $sqlTables;
+$sqlTables = array('sccpbuttonconfig','sccpdevice','sccpline','sccpuser','sccpsettings','sccpdevmodel');
+createBackUpConfig();
-out('Removing all Sccp_manager tables');
-$tables = array('sccpdevmodel', 'sccpsettings');
-foreach ($tables as $table) {
+function createBackUpConfig()
+{
+ global $amp_conf;
+ global $sqlTables;
+ outn("" . _("Creating Config BackUp") . "");
+ $cnf_int = \FreePBX::Config();
+ $backup_files = array('extensions','extconfig','res_mysql', 'res_config_mysql','sccp','sccp_hardware','sccp_extensions');
+ $backup_ext = array('_custom.conf', '_additional.conf','.conf');
+ $dir = $cnf_int->get('ASTETCDIR');
+
+
+ $sqlTables = array('sccpbuttonconfig','sccpdevice','sccpline','sccpuser','sccpsettings','sccpdevmodel');
+ $sqlTablesString = implode(' ',$sqlTables);
+ $sqlBuFile = $dir.'/sccp_backup_'.date("Ymd").'.sql';
+ $result = exec("mysqldump {$amp_conf['AMPDBNAME']} {$sqlTablesString}
+ --password={$amp_conf['AMPDBPASS']}
+ --user={$amp_conf['AMPDBUSER']}
+ --single-transaction >{$sqlBuFile}");
+ try {
+ $zip = new \ZipArchive();
+ } catch (\Exception $e) {
+ outn("
");
+ outn("PHPx.x-zip not installed where x.x is the installed PHP version. Install it before continuing !");
+ die_freepbx();
+ }
+ $filename = $dir . "/sccp_uninstall_backup" . date("Ymd"). ".zip";
+ if ($zip->open($filename, \ZIPARCHIVE::CREATE)) {
+ foreach ($backup_files as $file) {
+ foreach ($backup_ext as $b_ext) {
+ if (file_exists($dir . '/'.$file . $b_ext)) {
+ $zip->addFile($dir . '/'.$file . $b_ext);
+ }
+ }
+ }
+ if (file_exists($sqlBuFile)) {
+ $zip->addFile($sqlBuFile);
+ }
+ $zip->close();
+ } else {
+ outn("" . _("Error Creating BackUp: ") . $filename ."");
+ outn("
");
+ outn("PHPx.x-zip not installed where x.x is the installed PHP version. Install it before continuing !");
+ die_freepbx();
+ }
+ unlink($sqlBuFile);
+ outn("" . _("Config backup created: ") . $filename ."");
+}
+
+if (!empty($version)) {
+ $check = $db->getRow("SELECT 1 FROM `kvstore` LIMIT 0", DB_FETCHMODE_ASSOC);
+ if (!(DB::IsError($check))) {
+ outn("" . _("Deleting keys FROM kvstore..") . "");
+ sql("DELETE FROM kvstore WHERE module = 'sccpsettings'");
+ sql("DELETE FROM kvstore WHERE module = 'Sccp_manager'");
+ }
+}
+outn("" . _('Removing all Sccp_manager tables') . "");
+foreach ($sqlTables as $table) {
$sql = "DROP TABLE IF EXISTS {$table}";
$result = $db->query($sql);
if (DB::IsError($result)) {
die_freepbx($result->getDebugInfo());
}
+ $sql = "DROP VIEW IF EXISTS sccpdeviceconfig";
+ $result = $db->query($sql);
+ if (DB::IsError($result)) {
+ die_freepbx($result->getDebugInfo());
+ }
unset($result);
}
-if (!empty($version)) {
- // Woo, we have a version
- $check = $db->getRow("SELECT 1 FROM `kvstore` LIMIT 0", DB_FETCHMODE_ASSOC);
- if (!(DB::IsError($check))) {
- //print_r("none, creating table :". $value);
- echo "Deleting keys FROM kvstore..";
- sql("DELETE FROM kvstore WHERE module = 'sccpsettings'");
- sql("DELETE FROM kvstore WHERE module = 'Sccp_manager'");
- }
- // By accessing the database, we have recreated sccpsettings table so now delete
- // Need to rewrite this uninstaller.
- $sql = "DROP TABLE IF EXISTS sccpsettings";
- $result = $db->query($sql);
- if (DB::IsError($result)) {
- die_freepbx($result->getDebugInfo());
- }
-
-/* Comment: Maybe save in sccpsettings, if the chan_sccp tables already existed in the database or if they were created by install.php */
-/* So that you know if it is safe to drop/delete them */
-
-/* DROP VIEW IF EXISTS`sccpdeviceconfig`;
- DROP TABLE IF EXISTS `sccpbuttonconfig`;
- DROP TABLE IF EXISTS `sccpdevice`;
- DROP TABLE IF EXISTS `sccpdevmodel`;
- DROP TABLE IF EXISTS `sccpline`;
- DROP TABLE IF EXISTS `sccpsettings`;
- DROP TABLE IF EXISTS `sccpuser`;
- *
- */
-}
- echo "done
\n";
+ outn("" . _("Uninstall Complete") . "");
+ return true;
?>