From 9ce9e606ff55730c3e4e5e43a4a2e2a002efce64 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 18 Jul 2019 12:00:12 +0200 Subject: [PATCH] Added script for accounts migration & tables deletion + SMS object fix --- flexisip-account-manager.spec | 2 +- src/misc/utilities.php | 18 ++++ src/objects/account.php | 4 +- src/objects/alias.php | 2 +- src/objects/sms.php | 6 +- src/tools/drop_tables.php | 58 +++++++++++ src/tools/migrate_accounts.php | 185 +++++++++++++++++++++++++++++++++ 7 files changed, 269 insertions(+), 6 deletions(-) create mode 100644 src/tools/drop_tables.php create mode 100644 src/tools/migrate_accounts.php diff --git a/flexisip-account-manager.spec b/flexisip-account-manager.spec index f542fc3..f432ddf 100644 --- a/flexisip-account-manager.spec +++ b/flexisip-account-manager.spec @@ -8,7 +8,7 @@ #%define _datadir %{_datarootdir} #%define _docdir %{_datadir}/doc -%define build_number 5 +%define build_number 6 #%if %{build_number} #%define build_number_ext -%{build_number} #%endif diff --git a/src/misc/utilities.php b/src/misc/utilities.php index 7c4e5b3..3e0553b 100644 --- a/src/misc/utilities.php +++ b/src/misc/utilities.php @@ -122,4 +122,22 @@ function password_match($pwd1, $pwd2) { return true; } +// Time + +function time_elapsed_as_string($secs) { + $bit = array( + 'y' => $secs / 31556926 % 12, + 'w' => $secs / 604800 % 52, + 'd' => $secs / 86400 % 7, + 'h' => $secs / 3600 % 24, + 'm' => $secs / 60 % 60, + 's' => $secs % 60 + ); + + foreach($bit as $k => $v) + if($v > 0) $ret[] = $v . $k; + + return join(' ', $ret); +} + ?> diff --git a/src/objects/account.php b/src/objects/account.php index 562892e..45cbaf9 100644 --- a/src/objects/account.php +++ b/src/objects/account.php @@ -127,7 +127,9 @@ class Account { $this->expire_time = htmlspecialchars(strip_tags($this->expire_time)); } - $this->creation_time = date('Y-m-d H:i:s'); + if (empty($this->creation_time)) { + $this->creation_time = date('Y-m-d H:i:s'); + } $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $stmt = $this->conn->prepare($query); diff --git a/src/objects/alias.php b/src/objects/alias.php index d3a0289..e4c3465 100644 --- a/src/objects/alias.php +++ b/src/objects/alias.php @@ -224,7 +224,7 @@ class Alias { $this->alias = $row['alias']; $this->domain = $row['domain']; return true; - } + } Logger::getInstance()->error($stmt->errorInfo()); return false; } diff --git a/src/objects/sms.php b/src/objects/sms.php index f237d62..6ebd432 100644 --- a/src/objects/sms.php +++ b/src/objects/sms.php @@ -81,7 +81,7 @@ class SMS { } function delete() { - $query = "DELETE FROM " . $this->table_name . " WHERE id = ?"; + $query = "DELETE FROM " . SMS_DB_TABLE . " WHERE id = ?"; $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $stmt = $this->conn->prepare($query); @@ -98,7 +98,7 @@ class SMS { } function create() { - $query = "INSERT INTO " . $this->table_name . " SET phone=:phone, last_sms=:last_sms, count=:count"; + $query = "INSERT INTO " . SMS_DB_TABLE . " SET phone=:phone, last_sms=:last_sms, count=:count"; $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $stmt = $this->conn->prepare($query); @@ -121,7 +121,7 @@ class SMS { } function update() { - $query = "UPDATE " . $this->table_name . " SET phone=:phone, last_sms=:last_sms, count=:count WHERE id=:id"; + $query = "UPDATE " . SMS_DB_TABLE . " SET phone=:phone, last_sms=:last_sms, count=:count WHERE id=:id"; $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $stmt = $this->conn->prepare($query); diff --git a/src/tools/drop_tables.php b/src/tools/drop_tables.php new file mode 100644 index 0000000..47f339a --- /dev/null +++ b/src/tools/drop_tables.php @@ -0,0 +1,58 @@ +#!/usr/bin/env php + +. +*/ + +include_once __DIR__ . '/../database/database.php'; +include_once __DIR__ . '/../objects/account.php'; +include_once __DIR__ . '/../objects/alias.php'; +include_once __DIR__ . '/../objects/device.php'; +include_once __DIR__ . '/../objects/password.php'; +include_once __DIR__ . '/../objects/sms.php'; + +$database = new Database(); +$db = $database->getConnection(); + +$account = new Account($db); +if (!$account->dropTable()) { + Logger::getInstance()->error("Couldn't drop account table"); +} + +$alias = new Alias($db); +if (!$alias->dropTable()) { + Logger::getInstance()->error("Couldn't drop alias table"); +} + +$device = new Device($db); +if (!$device->dropTable()) { + Logger::getInstance()->error("Couldn't drop device table"); +} + +$password = new Password($db); +if (!$password->dropTable()) { + Logger::getInstance()->error("Couldn't drop password table"); +} + +$sms = new SMS($db); +if (!$sms->dropTable()) { + Logger::getInstance()->error("Couldn't drop sms table"); +} + +?> \ No newline at end of file diff --git a/src/tools/migrate_accounts.php b/src/tools/migrate_accounts.php new file mode 100644 index 0000000..2c5ec97 --- /dev/null +++ b/src/tools/migrate_accounts.php @@ -0,0 +1,185 @@ +#!/usr/bin/env php + +. +*/ + +include_once __DIR__ . '/../database/database.php'; +include_once __DIR__ . '/../objects/account.php'; +include_once __DIR__ . '/../objects/alias.php'; +include_once __DIR__ . '/../objects/device.php'; +include_once __DIR__ . '/../objects/password.php'; +include_once __DIR__ . '/../objects/sms.php'; +include_once __DIR__ . '/../misc/utilities.php'; + +$database = new Database(); +$db = $database->getConnection(); +$old_db = null; + +try { + $old_db = new PDO("mysql:host=" . DB_HOST . ";dbname=belledonne_proxy", DB_USER, DB_PASSWORD); + $old_db->exec("set names utf8"); +} catch(PDOException $exception) { + Logger::getInstance()->error("Connection error: " . $exception->getMessage()); + return; +} + +Logger::getInstance()->message("Ready to migrate"); + +$start_time = time(); + +/* **************************************************** */ + +Logger::getInstance()->message("Starting accounts migration"); + +$query = "SELECT id, login, password, activated, email, confirmation_key, ip_address, date_last_update, user_agent FROM " . ACCOUNTS_DB_TABLE; +$old_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); +$stmt = $old_db->prepare($query); +$stmt->execute(); + +$accounts_to_migrate_count = 0; +$account_created_count = 0; +$password_created_count = 0; +$alias_created_count = 0; + +$alias_query = "SELECT alias FROM " . ALIAS_DB_TABLE . " WHERE account_id = ? LIMIT 0,1"; + +while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $accounts_to_migrate_count += 1; + extract($row); + + $account = new Account($db); + $account->username = $login; + $account->domain = SIP_DOMAIN; + $account->email = $email; + $account->activated = $activated; + $account->confirmation_key = $confirmation_key; + $account->ip_address = $ip_address; + $account->user_agent = $user_agent; + $account->creation_time = $date_last_update; + $account->expire_time = null; + + if ($account->create()) { + $account_created_count += 1; + + $pwd = new Password($db); + $pwd->account_id = $account->id; + $pwd->algorithm = 'MD5'; + $pwd->password = $password; + + if (!$pwd->create()) { + Logger::getInstance()->error("Failed to create password !"); + } else { + $password_created_count += 1; + } + + $alias_stmt = $old_db->prepare($alias_query); + $alias_stmt->bindParam(1, $id); + if ($alias_stmt->execute()) { + $alias_row = $alias_stmt->fetch(PDO::FETCH_ASSOC); + if ($alias_row != null) { + $alias = new Alias($db); + $alias->account_id = $account->id; + $alias->alias = $alias_row['alias']; + $alias->domain = $account->domain; + + if (!$alias->create()) { + Logger::getInstance()->error("Failed to create alias !"); + } else { + $alias_created_count += 1; + } + } + } + } else { + Logger::getInstance()->error("Failed to create account !"); + } +} + +Logger::getInstance()->message("Accounts migration done"); +Logger::getInstance()->message($accounts_to_migrate_count . " were to migrate, " . $account_created_count . " were succesfully created including " + . $password_created_count . " passwords and " . $alias_created_count . " aliases"); + +/* **************************************************** */ + +Logger::getInstance()->message("Starting SMS migration"); + +$all_sms = new SMS($old_db); +$stmt = $all_sms->getAll(); + +$sms_to_migrate_count = 0; +$sms_created_count = 0; + +while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $sms_to_migrate_count += 1; + extract($row); + + $sms = new SMS($db); + $sms->phone = $phone; + $sms->last_sms = $last_sms; + $sms->count = $count; + + if (!$sms->create()) { + Logger::getInstance()->error("Failed to create sms !"); + } else { + $sms_created_count += 1; + } +} + +Logger::getInstance()->message("SMS migration done"); +Logger::getInstance()->message($sms_to_migrate_count . " were to migrate, " . $sms_created_count . " were succesfully created"); + +/* **************************************************** */ + +Logger::getInstance()->message("Starting devices migration"); + +$all_devices = new Device($old_db); +$stmt = $all_devices->getAll(); + +$devices_to_migrate_count = 0; +$devices_created_count = 0; + +while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $devices_to_migrate_count += 1; + extract($row); + + $device = new Device($db); + $device->manufacturer = $manufacturer; + $device->model = $model; + $device->status = $status; + $device->delay = $delay; + $device->hardware_echo_canceller = $hardware_echo_canceller; + + if (!$device->create()) { + Logger::getInstance()->error("Failed to create device !"); + } else { + $devices_created_count += 1; + } +} + +Logger::getInstance()->message("Devices migration done"); +Logger::getInstance()->message($devices_to_migrate_count . " were to migrate, " . $devices_created_count . " were succesfully created"); + +/* **************************************************** */ + +$end_time = time(); + +$time_diff = $end_time - $start_time; +Logger::getInstance()->message("Migration took " . time_elapsed_as_string($time_diff)); + +?> \ No newline at end of file