" .$valid_paths);
end_page();
exit;
}
$db_name = $_SESSION["wa_current_user"]->company;
$msg = handle_form($db_connections[$db_name]);
page(_("Backup and Restore Database"), false, false, '', '', true);
//-------------------------------------------------------------------------------
start_form(true, true);
$cmb = get_backup_file_combo();
$compr = get_compr_combo();
echo "
$msg |
" . _("Backup scripts") . " |
$cmb |
|
" . _("Comments") . " (" . _("Create Backup") . ") |
|
";
end_form();
//-------------------------------------------------------------------------------------------------
end_page();
function handle_form($conn)
{
global $path_to_root;
//Generate Only
if (isset($_GET['c']))
{
if ($_GET['c']=='g')
{
$filename = generate_backup($conn, $_GET['comp'], $_GET['comm']);
header("Location: backups.php?c=gs&fn=" . urlencode($filename));
return "";
}
//Generate and download
if ($_GET['c']=='gd')
{
$filename = generate_backup($conn);
header("Location: backups.php?c=ds&fn=" . urlencode($filename));
return "";
}
//Download the file
if ($_GET['c']=='d')
{
download_file(BACKUP_PATH . $_GET['fn']);
exit;
}
//Delete the file
if ($_GET['c']=='df')
{
$filename = $_GET['fn'];
@unlink(BACKUP_PATH . $filename);
header("Location: backups.php?c=dff&fn=" . urlencode($filename));
return "";
}
if ($_GET['c']=='dff')
{
$msg = _("File successfully deleted.")." ";
$msg .= _("Filename") . " = " . $_GET['fn'];
return $msg;
}
//Write JS script to open download window
if ($_GET['c']=='ds')
{
$filename = urlencode($_GET['fn']);
$msg = _("Backup is being downloaded...");
$msg .= "";
return $msg;
}
//Print backup success message
if ($_GET['c']=='gs')
{
$msg = _("Backup successfully generated.")." ";
$msg .= _("Filename") . " = " . $_GET['fn'];
return $msg;
}
//Restore backup
if ($_GET['c']=='r')
{
$filename=$_GET['fn'];
restore_backup(BACKUP_PATH . $filename, $conn);
header("Location: backups.php?c=rs&fn=" . urlencode($filename));
return "";
}
//Print restore success message
if ($_GET['c']=='rs')
{
$msg = _("Restore backup completed.")." ";
return $msg;
}
if ($_GET['c']=='u')
{
$filename = $_FILES['uploadfile']['tmp_name'];
if (is_uploaded_file ($filename))
{
restore_backup($filename, $conn);
$msg = _("Uploaded file has been restored.");
}
else
{
$msg = _("Backup was not uploaded into the system.");
}
return $msg;
}
}
return "";
}
function generate_backup($conn, $ext='no', $comm='')
{
if ($conn['tbpref'] != "")
$filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
else
$filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
$filename = db_export($conn, $filename, $ext, $comm);
return $filename;
}
function restore_backup($filename, $conn)
{
return db_import($filename, $conn);
}
function get_backup_file_combo()
{
global $path_to_root;
$ar_files = array();
default_focus('cmb_backups');
$dh = opendir(BACKUP_PATH);
while (($file = readdir($dh)) !== false)
$ar_files[] = $file;
closedir($dh);
rsort($ar_files);
$opt_files = "";
foreach ($ar_files as $file)
if (strpos($file, ".sql") || strpos($file, ".sql"))
$opt_files .= "";
return "";
}
function get_compr_combo()
{
$ar_comps = array();
$ar_comps[] = _("No");
if (function_exists("gzcompress"))
$ar_comps[] = "zip";
if (function_exists("gzopen"))
$ar_comps[] = "gzip";
$opt_comps = "";
foreach ($ar_comps as $file)
$opt_comps .= "";
return "";
}
function download_file($filename)
{
if (empty($filename) || !file_exists($filename))
{
return FALSE;
}
$saveasname = basename($filename);
header('Content-type: application/octet-stream');
header('Content-Length: '.filesize($filename));
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
readfile($filename);
return true;
}
function valid_paths()
{
global $path_to_root;
$st = "";
if (!file_exists(BACKUP_PATH))
$st .= " - " . _("cannot find backup directory") . " - " . BACKUP_PATH . "
";
return $st;
}
?>