<?php
-
+/**********************************************************************
+ Copyright (C) FrontAccounting, LLC.
+ Released under the terms of the GNU Affero General Public License,
+ AGPL, 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 License here <http://www.gnu.org/licenses/agpl-3.0.html>.
+***********************************************************************/
$page_security = 15;
$path_to_root="..";
$valid_paths = valid_paths();
//$valid_paths = '';
-if ($valid_paths != "")
+if ($valid_paths != "")
{
page(_("Backup and Restore Database - Error"));
display_error (_("Backup paths have not been set correctly.") ." " . _("Please contact System Administrator.") . "<br>" .$valid_paths);
}
$db_name = $_SESSION["wa_current_user"]->company;
-$msg = handle_form($db_connections[$db_name]);
+handle_form($db_connections[$db_name]);
-page(_("Backup and Restore Database"));
+page(_("Backup and Restore Database"), false, false, '', '', true);
//-------------------------------------------------------------------------------
start_form(true, true);
if (ext != 'sql') {
alert('" . _('This extension can not be be viewed: ') . "' + ext)
return
- }
- window.open('" . $path_to_root . BACKUP_PATH . "'+pFilename, '', 'toolbar=no,scrollbars=yes')
+ }
+ window.open('" . BACKUP_PATH . "'+pFilename, '', 'toolbar=no,scrollbars=yes')
}
function deleteBackup() {
pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
function downloadBackup() {
pFilename = document.forms[0].cmb_backups.options[document.forms[0].cmb_backups.selectedIndex].value
document.location.replace('backups.php?c=ds&fn='+pFilename)
+ Behaviour.apply();
}
function uploadBackup() {
if (document.forms[0].uploadfile.value=='') {
</script>
<center>
<table cellpadding=2 cellspacing=2 border=0>
- <tr><td colspan=2 style='color:darkred'><b>$msg</b> </td></tr>
<tr>
- <td style='padding-right:30'>" . _("Backup scripts") . "</td>
+ <td style='padding-right:30px'>" . _("Backup scripts") . "</td>
</tr>
<tr>
- <td style='padding-right:30'>$cmb</td>
+ <td style='padding-right:30px'>$cmb</td>
<script language='javascript'>
if (document.forms[0].cmb_backups.options.length!=0) document.forms[0].cmb_backups.selectedIndex=0
</script>
<td>
<table height=160 cellpadding=0 cellspacing=0 border=0>
- <tr><td><input onclick='javascript:createBackup()' type='button' style='width:150' value='". _("Create Backup") . "'></td><td style='padding-left:20'>" . _("Compression") . " $compr</td></tr>
- <tr><td><input onclick='javascript:restoreBackup()' type='button' style='width:150' value='". _("Restore Backup") . "'></td><td> </td></tr>
- <tr><td><input onclick='javascript:viewBackup()' type='button' style='width:150' value='". _("View Backup") . "'></td><td> </td></tr>
- <tr><td><input onclick='javascript:deleteBackup()' type='button' style='width:150' value='". _("Delete Backup") . "'></td><td> </td></tr>
- <tr><td><input onclick='javascript:downloadBackup()' type='button' style='width:150' value='". _("Download Backup") . "'></td><td> </td></tr>
- <tr><td><input onclick='javascript:uploadBackup()' type='button' style='width:150' value='". _("Upload Backup"). "'></td>
- <td style='padding-left:20'><input name='uploadfile' type='file'></td>
+ <tr><td><input onclick='javascript:createBackup()' type='button' style='width:150px' value='". _("Create Backup") . "'></td><td style='padding-left:20px'>" . _("Compression") . " $compr</td></tr>
+ <tr><td><input onclick='javascript:restoreBackup()' type='button' style='width:150px' value='". _("Restore Backup") . "'></td><td> </td></tr>
+ <tr><td><input onclick='javascript:viewBackup()' type='button' style='width:150px' value='". _("View Backup") . "'></td><td> </td></tr>
+ <tr><td><input onclick='javascript:deleteBackup()' type='button' style='width:150px' value='". _("Delete Backup") . "'></td><td> </td></tr>
+ <tr><td><input onclick='javascript:downloadBackup()' type='button' style='width:150px' value='". _("Download Backup") . "'></td><td> </td></tr>
+ <tr><td><input onclick='javascript:uploadBackup()' type='button' style='width:150px' value='". _("Upload Backup"). "'></td>
+ <td style='padding-left:20px'><input name='uploadfile' type='file'></td>
</tr>
</table>
</td>
</tr>
<tr>
- <td style='padding-right:30'>" . _("Comments") . " (" . _("Create Backup") . ")</td>
+ <td style='padding-right:30px'>" . _("Comments") . " (" . _("Create Backup") . ")</td>
</tr>
<tr>
- <td style='padding-right:30'><textarea rows=4 cols=30 name='comments'></textarea></td>
+ <td style='padding-right:30px'><textarea rows=4 cols=30 name='comments'></textarea></td>
</tr>
</table></center>";
end_page();
-function handle_form($conn)
+function handle_form($conn)
{
- global $path_to_root;
- //Generate Only
- if (isset($_GET['c']))
+if(isset($_GET['c']))
+ switch($_GET['c'])
{
- if ($_GET['c']=='g')
- {
+ case 'g': //Generate Only
$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')
- {
+ break;
+
+ case 'gd': //Generate and download
$filename = generate_backup($conn);
header("Location: backups.php?c=ds&fn=" . urlencode($filename));
- return "";
- }
- //Download the file
- if ($_GET['c']=='d')
- {
- download_file($path_to_root . BACKUP_PATH . $_GET['fn']);
+ break;
+
+ case 'd': //Download the file
+ download_file(BACKUP_PATH . $_GET['fn']);
exit;
- }
- //Delete the file
- if ($_GET['c']=='df')
- {
+
+ case 'df': //Delete the file
$filename = $_GET['fn'];
- @unlink($path_to_root . BACKUP_PATH . $filename);
+ @unlink(BACKUP_PATH . $filename);
header("Location: backups.php?c=dff&fn=" . urlencode($filename));
- return "";
- }
- if ($_GET['c']=='dff')
- {
- $msg = _("File successfully deleted.")." ";
+ break;
+
+ case 'dff':
+ $msg = _("File successfully deleted.")." ";
$msg .= _("Filename") . " = " . $_GET['fn'];
- return $msg;
- }
- //Write JS script to open download window
- if ($_GET['c']=='ds')
- {
+ display_notification($msg);
+ break;
+
+ case 'ds': //Write JS script to open download window
$filename = urlencode($_GET['fn']);
- $msg = _("Backup is being downloaded...");
- $msg .= "<script language='javascript'>";
- $msg .= "function download_file() {location.href ='backups.php?c=d&fn=$filename'}; window.onload=download_file";
- $msg .= "</script>";
- return $msg;
- }
- //Print backup success message
- if ($_GET['c']=='gs')
- {
- $msg = _("Backup successfully generated.")." ";
+ display_notification(_("Backup is being downloaded..."));
+
+ add_js_source("<script language='javascript'>
+ function download_file() {location.href ='backups.php?c=d&fn=$filename'};
+ Behaviour.addLoadEvent(download_file);
+ </script>");
+ break;
+
+ case 'gs': //Print backup success message
+ $msg = _("Backup successfully generated."). ' ';
$msg .= _("Filename") . " = " . $_GET['fn'];
- return $msg;
- }
- //Restore backup
- if ($_GET['c']=='r')
- {
+ display_notification($msg);
+ break;
+
+ case 'r': //Restore backup
$filename=$_GET['fn'];
- restore_backup($path_to_root . 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( restore_backup(BACKUP_PATH . $filename, $conn) )
+ header("Location: backups.php?c=rs&fn=" . urlencode($filename));
+ break;
- if ($_GET['c']=='u')
- {
+ case 'rs': //Print restore success message
+ display_notification(_("Restore backup completed."));
+ break;
+
+ case 'u':
$filename = $_FILES['uploadfile']['tmp_name'];
- if (is_uploaded_file ($filename))
+ if (is_uploaded_file ($filename))
{
- restore_backup($filename, $conn);
- $msg = _("Uploaded file has been restored.");
- }
- else
+ if( restore_backup($filename, $conn) )
+ display_notification(_("Uploaded file has been restored."));
+ else
+ display_error(_("Database restore failed."));
+ }
+ else
{
- $msg = _("Backup was not uploaded into the system.");
+ display_error(_("Backup was not uploaded into the system."));
}
- return $msg;
}
- }
- return "";
}
-function generate_backup($conn, $ext='no', $comm='')
+function generate_backup($conn, $ext='no', $comm='')
{
if ($conn['tbpref'] != "")
$filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
- else
+ else
$filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
-
- $filename = db_export($conn, $filename, $ext, $comm);
-
+
+ $filename = db_export($conn, $filename, $ext, $comm);
+
return $filename;
}
-function restore_backup($filename, $conn)
+function restore_backup($filename, $conn)
{
return db_import($filename, $conn);
-}
+}
-function get_backup_file_combo()
+function get_backup_file_combo()
{
global $path_to_root;
$ar_files = array();
-
- $dh = opendir($path_to_root . BACKUP_PATH);
+ 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"))
+ if (strpos($file, ".sql") || strpos($file, ".sql"))
$opt_files .= "<option value='$file'>$file</option>";
- return "<select name='cmb_backups' size=2 style='height:160;width:230'>$opt_files</select>";
+ return "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
}
-function get_compr_combo()
+function get_compr_combo()
{
$ar_comps = array();
-
+
$ar_comps[] = _("No");
- if (function_exists("gzcompress"))
+ if (function_exists("gzcompress"))
$ar_comps[] = "zip";
- if (function_exists("gzopen"))
+ if (function_exists("gzopen"))
$ar_comps[] = "gzip";
$opt_comps = "";
foreach ($ar_comps as $file)
$opt_comps .= "<option value='$file'>$file</option>";
return "<select name='cmb_comp'>$opt_comps</select>";
-}
-
-function download_file($filename)
+}
+
+function download_file($filename)
{
- if (empty($filename) || !file_exists($filename))
+ if (empty($filename) || !file_exists($filename))
{
- return FALSE;
+ return false;
}
$saveasname = basename($filename);
header('Content-type: application/octet-stream');
return true;
}
-function valid_paths()
+function valid_paths()
{
global $path_to_root;
-
+
$st = "";
- if (!file_exists($path_to_root . BACKUP_PATH))
- $st .= " - " . _("cannot find backup directory") . " - " . $path_to_root . BACKUP_PATH . "<br>";
+ if (!file_exists(BACKUP_PATH))
+ $st .= " - " . _("cannot find backup directory") . " - " . BACKUP_PATH . "<br>";
return $st;
}