<?php

//////////////////////////////////////////////////////////////
//===========================================================
// backup_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function databackup_theme(){

global $theme, $globals, $kernel, $user, $l, $error, $done, $vps_name, $vpslist, $filename, $tablesname, $checkval, $temp, $backup_settings, $backup_servers;


softheader($l['<title>']);
					
echo page_header(__(' Database Backup '), 'icon icon-databackup', '', 0, 0, '');

$breadcrumbs = ['dashboard', 'lm_databackup', 'lms_databackup'];

echo get_breadcrumb($breadcrumbs);

error_handle($error);

$email = !empty($globals['dbbackup_email']) ? $globals['dbbackup_email'] : $globals['soft_email'];

echo '	

	<script language="javascript" type="text/javascript">
				
		$(document).ready(function(){';
		echo '
		$("#dbtabs div#cron").show();				
		$("#dbtabs ul a").click(function(evt){
			evt.preventDefault();
			evt.stopImmediatePropagation();
			window.location.hash = $(this).attr("href");
			
			if($(this).attr("href")=="#cron"){
				$("#cron_tab").addClass("active");
				$("#immediate_tab").removeClass("active");
			}else{
				$("#immediate_tab").addClass("active");
				$("#cron_tab").removeClass("active");
			}
			
			$("#dbtabs ul a").removeClass("active");
			$(this).addClass("active");
			
			$("#dbtabs div.tab").hide();
			$("#dbtabs div"+$(this).attr("href")).show();
			
		});
		
		$("#type").change(dbbackup_type_change);
		
		dbbackup_type_change();
			
		';

		if(empty($backup_settings['vpsids'])){
			 echo '$("#vpses").each(function(){
					$("#vpses option").attr("selected", "selected");
			  });';
		}
		
	echo '
	});
	
	function Deldbback(id ,ele){

		id = id || 0;
		
		// List of ids to delete
		var dbback_list = new Array();
		
		if(id < 1){
			if($(ele).parent().prev().find("select").val() != 1){
				showalert("'.__J('No Action Selected !').'" ,"1");
				return false;
			}
			
			$(".ios:checked").each(function() {
				dbback_list.push($(this).val());
			});
		
		}else{
			
			dbback_list.push(id);
			
		}
		
		if(dbback_list.length < 1){
			showalert("'.__J('Nothing selected !').'" ,"1");
			return false;
		}
		
		showalert("'.__J('Are you sure, you want to delete the selected item(s) ?').'" ,"3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				var finalData = new Object();
				finalData["id"] = dbback_list.join(",");
				
				//alert(finalData);
				//return false;
				
				$("#progress_bar").show();
				
				$.ajax({
					type: "POST",
					url: "'.$globals['index'].'act=databackup&api=json",
					data : finalData,
					dataType : "json",
					success: function(data){
						$("#progress_bar").hide();
						if("done" in data){
							setTimeout(function(){showalert("'.__J('The action was completed successfully. Reloading the Page !').'");
							$("#alert-modal").on("hidden.bs.modal", function (e) {
							location.reload(true);
							});} ,200);
						}
					},
					error: function(data) {
						$("#progress_bar").hide();
						//alert(data.description);
						return false;
					}
				});
			}
		});
		return false;
	};
	
	function dbbackup_type_change() {
		var val = $("#type").val();
		
		var backup_server = '.((int) POSTval('dbbackup_server', $globals['dbbackup_server'])).';
		
		if(val == "EMAIL") {
			$("#dbbackup_server_row, #dbbackup_server_dir_row").hide();
			$("#dbbackup_email_row").show();
		} else if(val == "SSH") {
			$("#dbbackup_server_row, #dbbackup_server_dir_row").show();
			$("#dbbackup_email_row").hide();
			
			$("#dbbackup_server option.ssh").show(); // Show all SSH servers
			$("#dbbackup_server option.ftp").hide(); // Hide all FTP servers
		} else if(val == "FTP") {
			$("#dbbackup_server_row, #dbbackup_server_dir_row").show();
			$("#dbbackup_email_row").hide();
			
			$("#dbbackup_server option.ssh").hide(); // Hide all SSH servers
			$("#dbbackup_server option.ftp").show(); // Show all FTP servers
		}
		
		var $selected_server = $("#dbbackup_server option[value="+backup_server+"]");
		
		if($selected_server.is(":disabled")) {
			$("#dbbackup_server").val(0);
		} else {
			$("#dbbackup_server").val(backup_server);
		}
	}
	
	function dbsubmitval(ele){
		$("#progress_bar").show();
		var finaldata = new Object();
		finaldata = $(ele).serialize();
		finaldata = finaldata+"&dbdown=1";
		$.ajax({
			type: "POST",
			url: "'.$globals['index'].'act=databackup&api=json",
			data : finaldata,
			success: function(data){
				$("#progress_bar").hide();
				animate_task("'.__J('Database backup task started').'");
				location.reload(true);
			}
		});
	};
</script>
	
	
<div id="dbtabs">
	<div class="card p-2 mx-3 blue_tab">	
		<ul class="nav nav-tabs">
			<li class="col-xs-6 nav-item" id="cron_tab"><a href="#cron" class="nav-link active"><label class="control-label cursor-pointer">'.__('Scheduled Backup').'</label></a></li>
			<li class="col-xs-6 nav-item" id="immediate_tab"><a href="#immediate" class="nav-link"><label class="control-label cursor-pointer">'.__('Immediate Backup').'</label></a></li>
		</ul>
		<div id="cron" class="tab mt-4 p-2">
			<div>
				<form accept-charset="'.$globals['charset'].'" action="" method="post" name="databasebackups" class="form-horizontal">
					<div class="row mx-auto w-100 mb-3">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-8">
						<label class="control-label">'.__('Backup Type').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The type of the backup').'"><i class="fas text-primary fa-info-circle"></i></a></label><br>
							<select id="type" class="custom-select" name="type">
								<option value="EMAIL" '.ex_POSTselect('type', 'EMAIL', $globals['dbbackup_type']).'>'.__('Email Address').'</option>
								<option value="SSH" '.ex_POSTselect('type', 'SSH', $globals['dbbackup_type']).'>'.__('Access SSH').'</option>
								<option value="FTP" '.ex_POSTselect('type', 'FTP', $globals['dbbackup_type']).'>'.__('FTP').'</option>
							</select>
						</div>
					</div>
					<div class="row mx-auto w-100 my-3" id="dbbackup_email_row">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-8">
						<label class="control-label">'.__('Backup / Restore Email').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The email where backup / restore mails for success and error messages will be sent.<br /><b>Note:</b> If empty, the mails will be sent to the site admin email.').'"><i class="fas text-primary fa-info-circle"></i></a></label><br>
							<input type="text" id="email" size="40" class="form-control" name="email" value="'.POSTval('email', $email).'"/>
						</div>
					</div>
					<div class="row mx-auto w-100 my-3" id="dbbackup_server_row">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-8">
							<label class="control-label">'.__('Backup Server').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The server on which backups will be saved').'"><i class="fas text-primary fa-info-circle"></i></a></label><br>
							<select id="dbbackup_server" name="dbbackup_server" class="custom-select">
								<option value="0">'.__('Select....').'</option>';
								
								foreach($backup_servers as $bs) {
									echo '<option class="'._strtolower($bs['type']).'" value="'.$bs['bid'].'" '.ex_POSTselect('dbbackup_server', $bs['bid'], $globals['dbbackup_server']).'>'.$bs['name'].'</option>';
								}
								
							echo '</select>
						</div>
					</div>
					<div class="row mx-auto w-100 my-3" id="dbbackup_server_dir_row">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-8">
						<label class="control-label">'.__('Backup Server Directory').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('The directory where backups will be stored.').'"><i class="fas text-primary fa-info-circle"></i></a></label><br>
							<input type="text" id="dbbackup_server_dir" size="40" class="form-control" name="dbbackup_server_dir" value="'.POSTval('dbbackup_server_dir', $globals['dbbackup_server_dir']).'"/>
						</div>
					</div>
					<div class="row mx-auto w-100 my-3">
						<div class="col-sm-2">
						</div>
						<div class="col-sm-8">
						<label class="control-label">'.__('Enter Cron value').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Enter the cron value for scheduling the backups.<br />Eg: 18 16 * * 3').'"><i class="fas text-primary fa-info-circle"></i></a></label><br>
							<input type="text" id="dbbackup_cron" size="40" class="form-control" name="dbbackup_cron" value="'.POSTval('dbbackup_cron', $globals['dbbackup_cron']).'"/>
						</div>
					</div>
					<br /><br />
					<center>
						<input type="submit" class="go_btn" name="databasebackups"  id="databasebackups" value="'.__('Submit').'"/>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input type="submit" name="databasebackups_delete"  id="databasebackups_delete" value="'.__('Disable Database Backup').'" class="go_btn"/>
					</center>';
					if(!empty($done['cron_set'])){
						show_alert(__('The OS Template was created successfully'));
					}
					echo'<br />
				</form>
				<div class="notebox"><font class="bboxtxt"><b>'.__('Note').'</b> : '.__('The <b>Scheduled Database Backup</b> utility is only available for the Master Server database.').'</font></div>
			</div>
		</div>
		<div id="immediate" class="tab mt-4 p-2">';
		if(!empty($filename)){
			echo'<div class="row mx-auto w-100 mb-3">
				<div class="col-sm-8 col-lg-8 col-md-6 p-0 d-flex align-items-center">
					<form accept-charset="'.$globals['charset'].'" action="" onsubmit="dbsubmitval(this); return false;" method="post" name="databackup" onsubmit="return false;" class="form-horizontal d-flex align-items-center">
						'.get_checkbox('email_db', __('Email / Upload the backup')).'
						<input type="submit" name="dbdown" value="'.__('Start Database Backup').'" class="go_btn ml-2 mr-5" />
					</form>
					'.get_num_records(count($filename)).'
				</div>
				<div class="col-sm-4 col-lg-4 col-md-6 text-right d-flex align-items-center">
					<div class="bottom-menu float-right w-100">
						<div class="row my-auto">
							<div class="p-0">
								<label>'.__('With Selected').'</label>
							</div>
							<div class="col p-0">
								<select name="user_task_select" class="custom-select" id="dbback_task_select">
									<option value="0">---</option>
									<option value="1">'.__('Delete').'</option>
								</select>
							</div>
							<div class="pl-2">
								<button type="submit" class="go_btn" id="dbback_submit" name="dbback_submit" onclick="Deldbback(0 ,this); return false;">'.__('GO').'</button>
							</div>
						</div>
					</div>
				</div>
			</div>
			<form accept-charset="'.$globals['charset'].'" name="multi_dbback" id="multi_dbback" method="post" action="">
				<div class="w-100 mx-auto sticky">
					<table align="center" cellpadding="5" cellspacing="1" border="0" width="100%" class="table table-hover tablesorter common-table">
					<tr class="table_heading">
						<th class="z-index-1 min_width" align="center">
							'.get_checkbox('select_all', __('Check all'), 'select_all', array('chk_it' => 0), '', 'select_all').'
						</th>
						<th align="center">'.__('File Name').'</th>
						<th colspan="2" align="right" class="text-right pr-3">'.__('Source').'</th>
					</tr>';
					foreach($filename as $k =>$v){
						echo '
						<tr>
							<td width="20" valign="middle" align="center">
								'.get_checkbox('dbback_list[]', '', '', array('chk_it' => 0), rawurlencode($v)).'
							</td>
							<td>'.$v.'</td>
							<td width="20">
								<div class="badge_container">
									<a class="badge badge-settings" href="'.$globals['index'].'act=databackup&download='.$v.'" title="'.__('Download').'" ><i class="fas fa-download h6 m-0"></i></a>
									<a class="badge badge-trash" href="javascript:void(0);" onclick="Deldbback(\''.rawurlencode($v).'\'); return false;"><i class="fas fa-trash"></i></a>
								</div>
							</td>
						</tr>
						';
					}
				echo' </table></div>
				</form>
				';
			}
			echo '
			<div class="row mx-auto w-100 mt-5">
				<div class="col-sm-8 col-lg-8 col-md-6 mb-3 p-0">
					<form accept-charset="'.$globals['charset'].'" action="" onsubmit="dbsubmitval(this); return false;" method="post" name="databackup" onsubmit="return false;" class="form-horizontal d-flex align-items-center">
						'.get_checkbox('email_db', __('Email / Upload the backup')).'
						<input type="submit" name="dbdown" value="'.__('Start Database Backup').'" class="go_btn ml-2" />
					</form>
				</div>';
				if(!empty($filename)){
					echo '<div class="col-sm-4 col-lg-4 col-md-6 text-right mb-3">
					<div class="bottom-menu float-right w-100">
						<div class="row my-auto">
							<div class="p-0">
								<label>'.__('With Selected').'</label>
							</div>
							<div class="col p-0">
								<select name="user_task_select" class="custom-select" id="dbback_task_select">
									<option value="0">---</option>
									<option value="1">'.__('Delete').'</option>
								</select>
							</div>
							<div class="pl-2">
								<button type="submit" class="go_btn" id="dbback_submit" name="dbback_submit" onclick="Deldbback(0 ,this); return false;">'.__('GO').'</button>
							</div>
						</div>
					</div>
				</div>';
				}
				echo '
			</div>
			<div id="progress_bar" style="display:none;">
				<br />
				<center>
					<font id="progress_txt" size="4">'.__('Please wait, Action in Progress.').'</font>
				</center>
				<div class="my-2" align="center">
					<div class="progress w-50">
						<div class="progress-bar bg-primary progress-bar-striped progress-bar-animated w-100">
						</div>
					</div>
				</div>
				<center>
					'.__('You will be notified once the Action is Completed.').'
				</center>
			</div>
		</div>

		<br /><div class="notebox m-3 "><font class="bboxtxt"><b>'.__('Note').'</b> : '.__('Please do not restore the Database if its for an older version. It will cause Virtualizor to malfunction.').'</font></div>
		<script>

		if(window.location.hash == ""){
			$("#dbtabs ul a[href=#cron]").addClass("active");				
			$("#dbtabs div.tab").hide();
			$("#dbtabs div#cron").show();
			$("#cron_tab").addClass("active");
		}else{
			$("#dbtabs div.tab").hide();
			$("#dbtabs ul a[href="+window.location.hash+"]").addClass("active");
			$("#dbtabs "+window.location.hash).show();
			if(window.location.hash=="#cron"){
				$("#cron_tab").addClass("active");
				$("#immediate_tab").removeClass("active");
			}else{
				$("#immediate_tab").addClass("active");
				$("#cron_tab").removeClass("active");
			}
		}
		</script>
	</div>
</div>';

softfooter();

}

?>