Mass Deface
Email Grabber
"SET NAMES 'utf8'"));
$zdbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
exit();
}
define('WHMPIDFILE', '/var/run/migration.php.pid');
function removePidFile(){
unlink(WHMPIDFILE);
}
function isProcessRunning($pidFile = '/var/run/migration.php.pid'){
if (!file_exists($pidFile) || !is_file($pidFile)) return false;
$pid = file_get_contents($pidFile);
return $pid;
}
if( ($pid = isProcessRunning())){
print "Server is running! PID is $pid\n";
//get the process ID and recheck it
if (file_exists("/proc/".$pid)){
echo "The server is running from proc system too" ;
exit ;
}
else{
echo "Proc entry missing for the server" ;
}
}
else{
print "Server NOT running!\n";
}
file_put_contents(WHMPIDFILE, getmypid());
function ErrorMigrationLogfile($buffer, $encode = false){
if($encode){
$buffer = json_encode($buffer);
}
$whm_migration_log = "/var/sentora/logs/migration/whm_migration.log";
$date = date('Y-m-d H:i:sO');
$buffer = "[ $date ] ==> $buffer \n\n";
file_put_contents($whm_migration_log, $buffer, FILE_APPEND);
}
if(!is_dir('/var/sentora/logs/migration')){
$md_out = shell_exec("mkdir -p /var/sentora/logs/migration");
}
// CHECK MIGARATION IS STARTED
$mig_status_file = "/tmp/migration_status.log";
if(!file_exists($mig_status_file)){
exit;
}
$mig_status = trim(file_get_contents($mig_status_file));
$whm_login_log = "/tmp/whm_login.log";
$PackageList_file = "/tmp/ovi_cpanel_migration_get_all_packages";
$AccountList_file = "/tmp/ovi_cpanel_migration_get_all_accounts";
$acct_data = "/tmp/migration_acc_list.log";
$migration_user_log = "/tmp/migration_user.log";
$migration_pack_file = "/tmp/migration_pack_list.log";
$migration_restore_user = "/tmp/restore_process_user.log";
$mig_user_file = "/tmp/migration_detail.log";
$whm_login_details = file_get_contents($whm_login_log);
$whm_login_details = json_decode($whm_login_details, true);
$mig_status_file_hold = $mig_status_file.".hold";
$jsonapi = new JsonAPI($whm_login_details);
// MIGARATION INITED
if(file_exists($mig_status_file_hold)){
$Migration_error = file_get_contents($mig_status_file_hold);
ErrorMigrationLogfile($Migration_error);
exit;
}
// Check Migration User Created
$user_exists = trim(shell_exec("grep '^migration:' /etc/passwd | head -1 | awk -F':' '{print $1}'"));
if($user_exists == "") {
$username = "migration";
if(is_dir("/home/$username")){
$rm_out = shell_exec("rm -rf /home/$username");
}
$return = array();
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 12; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
$password = implode($pass); //turn the array into a string
$cmd_useradd=trim(shell_exec("whereis useradd | awk '{print $2}'"));
$cmd_passwd=trim(shell_exec("whereis passwd | awk '{print $2}'"));
$cmd_chown=trim(shell_exec("whereis chown | awk '{print $2}'"));
$get_out=shell_exec(''.$cmd_useradd.' -m '.escapeshellarg($username).' -d /home/migration/');
$get_out=shell_exec("echo ".escapeshellarg($password)." | passwd ".escapeshellarg($username)." --stdin");
$return['migration_user'] = $username;
$return['migration_pass'] = $password;
$ret = serialize($return);
$get_create_file=shell_exec("$cmd_chown -R migration:migration /home/migration");
file_put_contents($mig_user_file,$ret);
exit;
}
if($mig_status == "MIGRATION_STARTED"){
ErrorMigrationLogfile("mig_status MIGRATION_STARTED");
if(file_exists($mig_status_file_hold)){
unlink($mig_status_file_hold);
}
$migration_hold_datail = "";
$SelAccountListDiskObj = file_get_contents($acct_data);
$SelAccountListDisk_arr = json_decode($SelAccountListDiskObj, true);
$AllAccountListDiskObj = file_get_contents($AccountList_file);
$AllAccountListDisk_arr = json_decode($AllAccountListDiskObj, true);
$ClientMaxDisk = $ClientTotalDisk = 0;
foreach($AllAccountListDisk_arr as $acct){
if(in_array($acct['user'], $SelAccountListDisk_arr)){
$clientDisk = (int)$acct['diskused'];
if($ClientMaxDisk < $clientDisk){
$ClientMaxDisk = $clientDisk;
}
$ClientTotalDisk = $ClientTotalDisk + $clientDisk;
}
}
$return_mgs = array("current_server_disk" => 0, "remote_server_disk" => 0, "remote_server_ssh_enable" => 0);
$connection = SSHconnection($whm_login_details);
if(!$connection != false){
$return_mgs["remote_server_ssh_enable"] = 1;
$server_ip = $jsonapi->GetXsettingValues("server_ip");
if($server_ip){
$stream = ssh2_exec($connection, "csf -a $server_ip");
}
$stream = ssh2_exec($connection, "df -m | grep '/$' | awk '{ print $4 }'");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$Cpanel_Disk_mb = (int)trim(stream_get_contents($stream_out));
fclose($stream_out);
fclose($stream);
$return_mgs['remote_server_disk'] = $Cpanel_Disk_mb;
}
$Ovipanel_Disk_mb = (int)shell_exec("df -m | grep '/$' | awk '{ print $4 }'");
$return_mgs['current_server_disk'] = $Ovipanel_Disk_mb;
ErrorMigrationLogfile("Ovipanel_Disk_mb $Ovipanel_Disk_mb ClientTotalDisk $ClientTotalDisk remote_server_disk ".$return_mgs['remote_server_disk']." ClientMaxDisk $ClientMaxDisk");
if($Ovipanel_Disk_mb < $ClientTotalDisk){
$migration_hold_datail = "Ovipanel server disk size $Ovipanel_Disk_mb MB, and Total Client Account Size $ClientTotalDisk MB, Due to disk issue migration Has holded";
file_put_contents($mig_status_file_hold, $migration_hold_datail);
file_put_contents($mig_status_file, "MIGRATION_STARTED");
exit;
}
if($return_mgs['remote_server_disk'] != 0 && $return_mgs['remote_server_ssh'] != 0){
if($return_mgs['remote_server_disk'] < $ClientMaxDisk){
$migration_hold_datail = "Maximum Account disk size $ClientMaxDisk MB, and Available Client Server Disk Size $ClientTotalDisk MB, Due to disk issue migration Has holded";
file_put_contents($mig_status_file_hold, $migration_hold_datail);
file_put_contents($mig_status_file, "MIGRATION_STARTED");
exit;
}
}
$return_mgs = json_encode($return_mgs);
ErrorMigrationLogfile("return_mgs $return_mgs");
file_put_contents($mig_status_file, $return_mgs);
}
function SSHconnection($params){
ErrorMigrationLogfile("SSHconnection");
ErrorMigrationLogfile($params, true);
$remote_server_api_url = $params["remote_server_api_url"];
$remote_server_ssh_port = $params["remote_server_ssh_port"];
$remote_server_username = $params["remote_server_username"];
$remote_server_password = $params["remote_server_password"];
$serverip = parse_url($remote_server_api_url, PHP_URL_HOST);
if($remote_server_username != 'root'){
return false;
}
if(!function_exists('ssh2_connect') || !function_exists('ssh2_auth_password')){
echo "\n ssh2_connect ssh2_auth_password function is missing \n";
return false;
}
ErrorMigrationLogfile("serverip $serverip remote_server_ssh_port $remote_server_ssh_port");
$connection = @ssh2_connect($serverip, $remote_server_ssh_port);
if (!$connection) {
return false;
}
ErrorMigrationLogfile("remote_server_username $remote_server_username remote_server_password $remote_server_password");
$auth =@ssh2_auth_password($connection, $remote_server_username, $remote_server_password);
if(!$auth){
return false;
}
return $connection;
}
$CheckSocket = ctrl_module::SocketPortEnabledCheck();
if($CheckSocket){
echo "$mig_status CheckSocket\n";
}else{
echo "$mig_status not CheckSocket\n";
exit;
}
ErrorMigrationLogfile("Cron Started ".date('d F Y, h:i:s A'));
// Create Packages in package files
if(file_exists($migration_pack_file)){
$jsonapi->executeCreatePackage();
}
// Create Account in Account files
if(file_exists($acct_data)){
$CheckBackupSQL = "SELECT * FROM vps_migration_process WHERE backup_status=-1 AND migration_status!=0";
$CheckBackupSQLnrows = $zdbh->prepare($CheckBackupSQL);
$CheckBackupSQLnrows->execute();
if($CheckBackupSQLnrows->rowCount() == 0){
$CheckBackupAttmpSQL = "SELECT * FROM vps_migration_process WHERE backup_status=0 AND migration_status!=0";
$CheckBackupAttmpSQLnrows = $zdbh->prepare($CheckBackupAttmpSQL);
$CheckBackupAttmpSQLnrows->execute();
if($CheckBackupAttmpSQLnrows->rowCount() <> 0){
$CheckBackupAttmpData = $CheckBackupAttmpSQLnrows->fetch(PDO::FETCH_OBJ);
$username = $CheckBackupAttmpData->mig_username;
$params = GetUserAccountData($username);
$jsonapi->executeAccountBackup($params);
$BackupProgress = "UPDATE vps_migration_process SET backup_status=-1 WHERE mig_username = '$username'";
ErrorMigrationLogfile("BackupProgress $BackupProgress");
$BackupProgress = $zdbh->prepare($BackupProgress);
$BackupProgress->execute();
}else{
$username = GetNextUserfromAccountList($acct_data);
if($username){
$params = GetUserAccountData($username);
GenrateUserAccountBackup($username);
$jsonapi->executeAccountBackup($params);
$BackupProgress = "UPDATE vps_migration_process SET backup_status=-1 WHERE mig_username = '$username'";
ErrorMigrationLogfile("BackupProgress $BackupProgress");
$BackupProgress = $zdbh->prepare($BackupProgress);
$BackupProgress->execute();
}
}
}else{
$data = $CheckBackupSQLnrows->fetch(PDO::FETCH_OBJ);
$mig_username = $data->mig_username;
$time = $data->timestamp;
$backup_attempted = $data->backup_attempted;
$client_backup_file_name = trim(cpanelBackupStatusCheck($mig_username));
if($client_backup_file_name){
$UdateAcctSQL = "UPDATE vps_migration_process SET backup_status=1,flag='backup completed',restore_status=0,backup_file_name='$client_backup_file_name',timestamp=now() WHERE mig_username = '$mig_username'";
$UdateAcctSQLnrows = $zdbh->prepare($UdateAcctSQL);
$UdateAcctSQLnrows->execute();
}else{
$backup_progress = false;
if($mig_status != "MIGRATION_STARTED"){
ErrorMigrationLogfile("Check Backup progressing...");
$mig_status = json_decode($mig_status, true);
$remote_server_ssh_enable = $mig_status['remote_server_ssh_enable'];
if($remote_server_ssh_enable == 1){
ErrorMigrationLogfile("remote_server_ssh_enable $remote_server_ssh_enable");
$connection = SSHconnection($whm_login_details);
if($connection){
$cmd = "ps aux | grep 'pkgacct' | grep '$mig_username'";
$stream = ssh2_exec($connection, $cmd);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$pkgacct_process = trim(stream_get_contents($stream_out));
$pkgacct_process = explode("\n", $pkgacct_process);
fclose($stream_out);
fclose($stream);
if(count($pkgacct_process) > 2){
$backup_progress = true;
}
}
}
}
if($backup_progress == false){
ErrorMigrationLogfile("$mig_username backup initied at ".date("F j, Y, g:i a",strtotime($time)));
$time = strtotime($time);
$ct_time = time();
$plus1htime = $time + 3600;
if($plus1htime < $ct_time){
ErrorMigrationLogfile("$mig_username backup process is goind on more that 1 hour");
if($backup_attempted > 4){
ErrorMigrationLogfile("$mig_username is more than $backup_attempted attempt");
$BackupFailedSQL = "UPDATE vps_migration_process SET backup_status=1,flag='backup failed',migration_status=0 WHERE mig_username = '$mig_username'";
ErrorMigrationLogfile("BackupFailedSQL $BackupFailedSQL");
$BackupFailedSQLnrows = $zdbh->prepare($BackupFailedSQL);
$BackupFailedSQLnrows->execute();
}else{
ErrorMigrationLogfile("backup attempt is $backup_attempted");
$backup_attempt = $backup_attempted + 1;
$BackupFailedAttemptSQL = "UPDATE vps_migration_process SET backup_status=0,flag='backup attempt $backup_attempted',backup_attempted=$backup_attempt,timestamp=now() WHERE mig_username = '$mig_username'";
ErrorMigrationLogfile("BackupFailedAttemptSQL $BackupFailedAttemptSQL");
$BackupFailedAttemptnrows = $zdbh->prepare($BackupFailedAttemptSQL);
$BackupFailedAttemptnrows->execute();
}
}
}
}
}
}
$CheckRestoreSQL = "SELECT * FROM vps_migration_process WHERE restore_status=-1 AND migration_status!=0";
$CheckRestoreSQLnrows = $zdbh->prepare($CheckRestoreSQL);
$CheckRestoreSQLnrows->execute();
if($CheckRestoreSQLnrows->rowCount() == 0){
$StartRestoreSQL = "SELECT * FROM vps_migration_process WHERE backup_status=1 AND restore_status=0 AND migration_status!=0 LIMIT 1";
$StartRestoreSQLnrows = $zdbh->prepare($StartRestoreSQL);
$StartRestoreSQLnrows->execute();
if ($StartRestoreSQLnrows->rowCount() <> 0 ){
$data = $StartRestoreSQLnrows->fetch(PDO::FETCH_OBJ);
$mig_username = $data->mig_username;
$id = $data->id;
$time = $data->timestamp;
$client_backup_file_name = trim($data->backup_file_name);
$UdateAcctSQL = "UPDATE vps_migration_process SET flag='account creation',restore_status=-1 WHERE mig_username = '$mig_username'";
$UdateAcctSQLnrows = $zdbh->prepare($UdateAcctSQL);
$UdateAcctSQLnrows->execute();
if($client_backup_file_name){
// CREATE MIGRATION ACCOUNT
$jsonapi->executeCreateAccount($mig_username);
// RELOAD Lighttpd SERVICES
$service_service = trim(shell_exec("whereis service | awk '{print $2}'"));
$service_service = str_replace("\n", "", $service_service);
$service_service = str_replace('\n', "", $service_service);
$restart = shell_exec("$service_service lighttpd force-reload");
// RESTORE MIGRATION BACKUP
cpanelBackupStatusCheck($mig_username, true);
RestoreUserAccount($mig_username, $client_backup_file_name, $id);
}
}
}else{
echo "Restoration is going on \n";
ErrorMigrationLogfile("Restoration is going on....");
$data = $CheckRestoreSQLnrows->fetch(PDO::FETCH_OBJ);
$mig_username = $data->mig_username;
$process_id = $data->id;
$time = $data->timestamp;
$time = strtotime($time);
$ct_time = time();
$plus1htime = $time + 3600;
ErrorMigrationLogfile("Migaration is going on for $mig_username is account");
if($plus1htime > $ct_time){
ErrorMigrationLogfile("Migaration user less then 1 hour $plus1htime > ".time()." >> ");
exit;
}else{
$out = null;
exec("ps aux | grep '/scripts/whm_migration_restoreclient.php' | grep -v -e 'grep'", $out);
if (count($out) > 0) {
ErrorMigrationLogfile("Migaration process is exists...");
exit;
}
}
$update_query = "UPDATE vps_migration_process SET restore_status=0,flag='restore retry',timestamp=now() WHERE id = '$process_id'";
$numrows = $zdbh->prepare($update_query);
$numrows->execute();
}
if(file_exists($acct_data) && empty(trim(file_get_contents($acct_data)))){
$sql = "SELECT * FROM vps_migration_process WHERE backup_status=1 AND restore_status=0 AND migration_status!=0";
$numrows = $zdbh->prepare($sql);
$numrows->execute();
if ($numrows->rowCount()==0){
$AllPackages = file_get_contents($PackageList_file);
$AllPackages = json_decode($AllPackages, true);
foreach($AllPackages as $migration_pack){
$package_name = $migration_pack["name"];
$max_no_mailboxes = $migration_pack["MAXPOP"];
$max_disk_quota = $migration_pack["QUOTA"];
$jsonapi->RevoltBackPackage($package_name, $quota, $max_no_mailboxes);
}
}
}
function GetUserAccountData($username){
global $AccountList_file;
$AccountListOBJ = file_get_contents($AccountList_file);
$AccountList_arr = json_decode($AccountListOBJ, true);
if(empty($AccountList_arr)){
file_put_contents($AccountList_file, "");
exit;
}
foreach($AccountList_arr as $AcctList){
$AcctList_User = $AcctList['user'];
if($AcctList_User == $username){
$ovi_control_panel_user = $AcctList_User;
$inNewPackage = $AcctList['domain_package'];
$inDomainName = $AcctList['domain'];
$email_notify = $AcctList['user_email'];
$status = $AcctList['status'];
$return = array("ovi_control_panel_user" => $ovi_control_panel_user, "inDomainName" => $inDomainName, "inNewPackage" => $inNewPackage, "email_notify" => $email_notify, "status" => $status);
return $return;
}
}
}
function RestoreUserAccount($mig_username, $client_backup_file_name, $id){
global $zdbh;
$update_query = "UPDATE vps_migration_process SET flag='restore progress' WHERE mig_username = '$mig_username'";
$numrows = $zdbh->prepare($update_query);
$numrows->execute();
$get_php_path=shell_exec("whereis php | awk '{print $2}'");
$get_php_path=str_replace("\n","",$get_php_path);
$get_php_path=str_replace('\n',"",$get_php_path);
$cmd="$get_php_path /scripts/whm_migration_restoreclient.php $client_backup_file_name $mig_username $id";
ErrorMigrationLogfile("RestoreCMD ==> $cmd");
$output=shell_exec("$cmd");
}
function ClearOldBackup($userbackup_dir){
$files = glob("$userbackup_dir");
foreach($files as $file){
if(is_file($file))
unlink($file);
}
}
function cpanelBackupStatusCheck($cpanel_user, $move_backup = false){
echo "cpanelBackupStatusCheck \n";
ErrorMigrationLogfile("cpanelBackupStatusCheck....");
ErrorMigrationLogfile("Backup process is going on $cpanel_user");
$mv_path = trim(shell_exec("whereis mv | awk '{print $2}' | tr -d '\n'"));
$chown_path = trim(shell_exec("whereis chown | awk '{print $2}' | tr -d '\n'"));
$chmod_path = trim(shell_exec("whereis chmod | awk '{print $2}' | tr -d '\n'"));
$backup_filename = "";
$folder_path = "/home/migration/$cpanel_user";
$files = glob($folder_path.'/*.tar.gz');
if($files){
echo "cpanelBackupStatusCheck file there \n";
$backup_filename = basename($files[0]);
if($move_backup){
$homedir = ctrl_options::GetSystemOption('hosted_dir') . $cpanel_user;
if(!is_dir($homedir)){
ErrorMigrationLogfile("homedir $homedir is not exists");
ErrorMigrationLogfile("Account Creation failed for $cpanel_user");
exit;
}
$folder_user = "$homedir/backups/";
ClearOldBackup($folder_user);
if(!is_dir($folder_user))
{
$create_dir=shell_exec("mkdir -p $folder_user");
$create_dir=shell_exec("chown -R $cpanel_user. $folder_user");
}
$mv_out = shell_exec("$mv_path $folder_path/$backup_filename $folder_user");
$mv_out = shell_exec("$chmod_path 777 $folder_user -R");
}
}
return $backup_filename;
}
function GetNextUserfromAccountList($acct_data){
$acct_dataObj = trim(file_get_contents($acct_data));
$acct_data_arr = json_decode($acct_dataObj, true);
if($acct_data_arr){
foreach($acct_data_arr as $key => $username){
unset($acct_data_arr[$key]);
$acct_dataObj = json_encode($acct_data_arr);
file_put_contents($acct_data, $acct_dataObj);
return $username;
}
}else{
unlink($acct_data);
exit;
}
}
function GenrateUserAccountBackup($username){
global $zdbh;
$sql = "SELECT * FROM vps_migration_process WHERE mig_username = '$username'";
$numrows = $zdbh->prepare($sql);
$numrows->execute();
if ($numrows->rowCount() == 0 ){
$InsertUsersql = "INSERT INTO vps_migration_process (`mig_username`, `flag`, `backup_status`, timestamp) VALUE('$username', 'backup process', -1, now())";
$numrows = $zdbh->prepare($InsertUsersql);
$numrows->execute();
}
}
?>