Added script for accounts migration & tables deletion + SMS object fix

This commit is contained in:
Sylvain Berfini 2019-07-18 12:00:12 +02:00
parent 1d9a11c102
commit 9ce9e606ff
7 changed files with 269 additions and 6 deletions

View file

@ -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

View file

@ -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);
}
?>

View file

@ -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);

View file

@ -224,7 +224,7 @@ class Alias {
$this->alias = $row['alias'];
$this->domain = $row['domain'];
return true;
}
}
Logger::getInstance()->error($stmt->errorInfo());
return false;
}

View file

@ -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);

58
src/tools/drop_tables.php Normal file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env php
<?php
/*
Flexisip Account Manager is a set of tools to manage SIP accounts.
Copyright (C) 2019 Belledonne Communications SARL, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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");
}
?>

View file

@ -0,0 +1,185 @@
#!/usr/bin/env php
<?php
/*
Flexisip Account Manager is a set of tools to manage SIP accounts.
Copyright (C) 2019 Belledonne Communications SARL, All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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));
?>