From f970a88c599209f02bf889398846de4e79dfc6b3 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Mon, 20 Apr 2009 08:17:32 +0000 Subject: [PATCH] Page rewritten, fixed ajax-loader. --- admin/backups.php | 323 ++++++++++++++++++---------------------------- 1 file changed, 126 insertions(+), 197 deletions(-) diff --git a/admin/backups.php b/admin/backups.php index f1353193..cfc54c7f 100644 --- a/admin/backups.php +++ b/admin/backups.php @@ -16,179 +16,19 @@ include_once($path_to_root . "/includes/session.inc"); include_once($path_to_root . "/includes/ui.inc"); include_once($path_to_root . "/admin/db/maintenance_db.inc"); -$valid_paths = valid_paths(); -//$valid_paths = ''; -if ($valid_paths != "") -{ - page(_("Backup and Restore Database - Error")); - display_error (_("Backup paths have not been set correctly.") ."   " . _("Please contact System Administrator.") . "
" .$valid_paths); - end_page(); - exit; -} - -$db_name = $_SESSION["wa_current_user"]->company; -handle_form($db_connections[$db_name]); - -page(_("Backup and Restore Database"), false, false, '', '', true); +page(_("Backup and Restore Database"), false, false, '', ''); -//------------------------------------------------------------------------------- -start_form(true, true); +check_paths(); -$cmb = get_backup_file_combo(); -$compr = get_compr_combo(); - -echo " - -
- - - - - - - - - - - - - - - -
" . _("Backup scripts") . "
$cmb - - - - - - - - - -
" . _("Compression") . "  $compr
 
 
 
 
-
" . _("Comments") . " (" . _("Create Backup") . ")
"; - -end_form(); - -//------------------------------------------------------------------------------------------------- - -end_page(); - - -function handle_form($conn) +function check_paths() { -if(isset($_GET['c'])) - switch($_GET['c']) - { - case 'g': //Generate Only - $filename = generate_backup($conn, $_GET['comp'], $_GET['comm']); - header("Location: backups.php?c=gs&fn=" . urlencode($filename)); - break; - - case 'gd': //Generate and download - $filename = generate_backup($conn); - header("Location: backups.php?c=ds&fn=" . urlencode($filename)); - break; - - case 'd': //Download the file - download_file(BACKUP_PATH . $_GET['fn']); - exit; - - case 'df': //Delete the file - $filename = $_GET['fn']; - @unlink(BACKUP_PATH . $filename); - header("Location: backups.php?c=dff&fn=" . urlencode($filename)); - break; - - case 'dff': - $msg = _("File successfully deleted.")." "; - $msg .= _("Filename") . " = " . $_GET['fn']; - display_notification($msg); - break; - - case 'ds': //Write JS script to open download window - $filename = urlencode($_GET['fn']); - display_notification(_("Backup is being downloaded...")); - - add_js_source(""); - break; - - case 'gs': //Print backup success message - $msg = _("Backup successfully generated."). ' '; - $msg .= _("Filename") . " = " . $_GET['fn']; - display_notification($msg); - break; - - case 'r': //Restore backup - $filename=$_GET['fn']; - if( restore_backup(BACKUP_PATH . $filename, $conn) ) - header("Location: backups.php?c=rs&fn=" . urlencode($filename)); - break; - - 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( restore_backup($filename, $conn) ) - display_notification(_("Uploaded file has been restored.")); - else - display_error(_("Database restore failed.")); - } - else - { - display_error(_("Backup was not uploaded into the system.")); - } - } + if (!file_exists(BACKUP_PATH)) { + display_error (_("Backup paths have not been set correctly.") + ._("Please contact System Administrator.")."
" + . _("cannot find backup directory") . " - " . BACKUP_PATH . "
"); + end_page(); + exit; + } } function generate_backup($conn, $ext='no', $comm='') @@ -199,18 +39,20 @@ function generate_backup($conn, $ext='no', $comm='') $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql"; $filename = db_export($conn, $filename, $ext, $comm); - + if ($filename) + display_notification(_("Backup successfully generated."). ' ' + . _("Filename") . ": " . $filename); + else + display_error(_("Database backup failed.")); + return $filename; } -function restore_backup($filename, $conn) -{ - return db_import($filename, $conn); -} function get_backup_file_combo() { - global $path_to_root; + global $path_to_root, $Ajax; + $ar_files = array(); default_focus('cmb_backups'); $dh = opendir(BACKUP_PATH); @@ -221,26 +63,29 @@ function get_backup_file_combo() rsort($ar_files); $opt_files = ""; foreach ($ar_files as $file) - if (strpos($file, ".sql") || strpos($file, ".sql")) + if (preg_match("/.sql(.zip|.gz)?$/", $file)) $opt_files .= ""; - return ""; + $selector = ""; + + $Ajax->addUpdate('cmd_backups', "_cmd_backups_sel", $selector); + $selector = "".$selector."\n"; + + return $selector; } -function get_compr_combo() +function compress_list_row($label, $name, $value=null) { - $ar_comps = array(); + $ar_comps = array('no'=>_("No")); - $ar_comps[] = _("No"); if (function_exists("gzcompress")) - $ar_comps[] = "zip"; + $ar_comps['zip'] = "zip"; if (function_exists("gzopen")) - $ar_comps[] = "gzip"; - $opt_comps = ""; - foreach ($ar_comps as $file) - $opt_comps .= ""; + $ar_comps['gzip'] = "gzip"; - return ""; + echo "$label"; + array_selector('comp', $value, $ar_comps); + echo ""; } function download_file($filename) @@ -251,20 +96,104 @@ function download_file($filename) } $saveasname = basename($filename); header('Content-type: application/octet-stream'); - header('Content-Length: '.filesize($filename)); - header('Content-Disposition: attachment; filename="'.$saveasname.'"'); + header('Content-Length: '.filesize($filename)); + header('Content-Disposition: attachment; filename="'.$saveasname.'"'); readfile($filename); + return true; } -function valid_paths() -{ - global $path_to_root; +$db_name = $_SESSION["wa_current_user"]->company; +$conn = $db_connections[$db_name]; - $st = ""; - if (!file_exists(BACKUP_PATH)) - $st .= "   - " . _("cannot find backup directory") . " - " . BACKUP_PATH . "
"; - return $st; +if (get_post('creat')) { + generate_backup($conn, get_post('comp'), get_post('comments')); + $Ajax->activate('cmd_backups'); +}; + +if (get_post('restore')) { + if (db_import(BACKUP_PATH . get_post('cmb_backups'), $conn)) + display_notification(_("Restore backup completed.")); } -?> \ No newline at end of file +if (get_post('view')) { + $filename = BACKUP_PATH . get_post('cmb_backups'); + if (in_ajax()) + $Ajax->popup( $filename ); + else { + header('Content-type: application/octet-stream'); + header('Content-Length: '.filesize($filename)); + header("Content-Disposition: inline; filename=$filename"); + readfile($filename); + exit(); + } +}; + +if (get_post('download')) { + download_file(BACKUP_PATH . get_post('cmb_backups')); + exit; +} + +if (get_post('delete')) { + if (unlink(BACKUP_PATH . get_post('cmb_backups'))) { + display_notification(_("File successfully deleted.")." " + . _("Filename") . ": " . get_post('cmb_backups')); + $Ajax->activate('cmd_backups'); + } + else + display_error(_("Can't delete backup file.")); +}; + +if (get_post('upload')) +{ + $tmpname = $_FILES['uploadfile']['tmp_name']; + $fname = $_FILES['uploadfile']['name']; + + if (!preg_match("/.sql(.zip|.gz)?$/", $fname)) + display_error(_("You can only upload *.sql backup files")); + elseif (is_uploaded_file($tmpname)) { + rename($tmpname, BACKUP_PATH . $fname); + display_notification( "File uploaded to backup directory"); + $Ajax->activate('cmd_backups'); + } else + display_error(_("File was not uploaded into the system.")); +} +//------------------------------------------------------------------------------- +start_form(true, true); +start_outer_table($table_style2); +table_section(1); +table_section_title(_("Create backup")); + textarea_row(_("Comments:"), 'comments', null, 30, 8); + compress_list_row(_("Compression:"),'comp'); + vertical_space("height='20px'"); + submit_row('creat',_("Create Backup"), false, "colspan=2 align='center'", '', 'process'); +table_section(2); +table_section_title(_("Backup scripts maintenance")); + + start_row(); + echo "".get_backup_file_combo().""; + echo ""; + start_table(); + submit_row('view',_("View Backup"), false, '', '', true); + submit_row('download',_("Download Backup"), false, '', '', false); + submit_row('restore',_("Restore Backup"), false, '','', 'process'); + + $js = "if(confirm(\"" + .sprintf(_('You are about to remove %s backup file.
Do you want to continue ?'), + get_post('cmb_backups')) + ."\")) { JsHttpRequest.request(\"delete\"); }"; + + submit_row('delete', _("Delete Backup"), false, '','', 'dialog'); + end_table(); + echo ""; + end_row(); +start_row(); +echo ""; + submit_cells('upload',_("Upload file"),'', '', true); +end_row(); +end_outer_table(); + +end_form(); + +end_page(); +?> -- 2.30.2