From ffa116bb5d03a2274f2b42a5fdc121450a2af780 Mon Sep 17 00:00:00 2001 From: james Date: Wed, 24 Sep 2008 21:56:11 +0000 Subject: [PATCH] Add backup/restore functionality - need to add icon still Fix bug where title was leaving room for an icon even if there was no icon --- common.inc.php | 2 +- config/backuprestore.php | 240 +++++++++++++++++++++++++++++++++++++++ config/index.php | 2 +- theme/default/theme.php | 1 + 4 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 config/backuprestore.php diff --git a/common.inc.php b/common.inc.php index 5e7c03f..40079fb 100644 --- a/common.inc.php +++ b/common.inc.php @@ -605,7 +605,7 @@ if(committee_auth_has_access("admin")) echo ""; -if($icon) { +if($icon && theme_icon($icon)) { echo "
"; echo theme_icon($icon); echo ""; diff --git a/config/backuprestore.php b/config/backuprestore.php new file mode 100644 index 0000000..d1af3e1 --- /dev/null +++ b/config/backuprestore.php @@ -0,0 +1,240 @@ + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation, version 2. + + 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 GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +?> +Field."`,"; + $fields[]=$cr->Field; + } + $str=substr($str,0,-1); + $str.=") VALUES ("; + + $dataq=mysql_query("SELECT * FROM `$table` ORDER BY `{$fields[0]}`"); + while($data=mysql_fetch_object($dataq)) { + $insertstr=$str; + foreach($fields AS $field) { + if(is_null($data->$field)) + $insertstr.="NULL,"; + else + { + $escaped=str_replace("\\","\\\\",$data->$field); + $escaped=str_replace("'","''",$escaped); + $escaped=str_replace("\n","\\n",$escaped); + $escaped=str_replace("\r","\\r",$escaped); + $insertstr.="'".$escaped."',"; + } + } + $insertstr=substr($insertstr,0,-1); + $insertstr.=");"; + + $dump.=$insertstr."\n"; + } +} +header("Content-Type: text/sql"); +header("Content-Disposition: attachment; filename=sfiab_backup_".date("Y-m-d-H-i-s",$ts).".sql"); +header("Content-Length: ".strlen($dump)); +echo $dump; +} +else if($_POST['action']=="restore") { + echo send_header("Database Backup/Restore", + array('Committee Main' => 'committee_main.php', + 'SFIAB Configuration' => 'config/index.php') + ,"backup_restore" + ); + echo i18n("Processing file: %1",array($_FILES['restore']['name']))."
\n"; + echo "
\n"; + do { + //hmm just some random filename + $tmpfilename=md5(rand().time().$_FILES['restore']['name']); + } while(file_exists("../data/backuprestore/$tmpfilename")); + + move_uploaded_file($_FILES['restore']['tmp_name'],"../data/backuprestore/$tmpfilename"); + + $fp=fopen("../data/backuprestore/$tmpfilename","r"); + + for($x=0;$x<4;$x++) { + $line=fgets($fp,1024); + $hdr[$x]=split(":",trim($line),2); + } + fclose($fp); + + if(trim($hdr[0][0])=="#SFIAB SQL BACKUP") { + + echo "\n"; + $now=date("r"); + echo "\n"; + if(trim($hdr[0][1])\n"; + if(version_compare(trim($hdr[1][1]),$config['version'])==0) $cl="happy"; else { $cl="error"; $err=true; } + echo "\n"; + if(version_compare(trim($hdr[2][1]),$config['DBVERSION'])==0) $cl="happy"; else { $cl="error"; $err=true; } + echo "\n"; + if(trim($hdr[3][1])==$config['fairname']) $cl="happy"; else { $cl="error"; $err=true; } + echo "\n"; + echo "
".i18n("Information")."".i18n("Restore File")."".i18n("Live System")."
".i18n("Date/Time")."".$hdr[0][1]."$now
".i18n("SFIAB Version")."".$hdr[1][1]."".$config['version']."
".i18n("Database Version")."".$hdr[2][1]."".$config['DBVERSION']."
".i18n("Fair Name")."".$hdr[3][1]."".$config['fairname']."
\n"; + echo "
\n"; + if($err) { + echo error(i18n("Warning, there are discrepencies between the restore file and your current live system. Proceed at your own risk!")); + } + + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo ""; + echo "
\n"; + echo "
\n"; + echo ""; + echo i18n("If you are not going to proceed, please click here to clean up the temporary files which may contain confidential information!"); + echo "\n"; + } + else + { + echo error(i18n("This file is NOT a SFIAB SQL BACKUP file")); + echo i18n("Only backups created with the SFIAB Backup Creator can be used to restore from."); + echo "
\n"; + } + + send_footer(); +} +else if($_POST['action']=="restoreproceed") { + echo send_header("Database Backup/Restore", + array('Committee Main' => 'committee_main.php', + 'SFIAB Configuration' => 'config/index.php') + ,"backup_restore" + ); + + //make sure the filename's good before we used it + if(ereg("^[a-z0-9]{32}$",$_POST['realfilename']) && file_exists("../data/backuprestore/".$_POST['realfilename'])) { + $filename=$_POST['realfilename']; + echo i18n("Proceeding with database restore from %1",array($_POST['filename']))."..."; + $lines=file("../data/backuprestore/$filename"); + $err=false; + echo "
";
+		foreach($lines AS $line) {
+			$line=trim($line);
+			if(ereg("^#TABLE: (.*)",$line,$args)) {
+				//empty out the table
+				$sql="TRUNCATE TABLE `".$args[1]."`";
+		//			echo $sql."\n";
+				mysql_query($sql);
+			}
+			else if(ereg("^#",$line)) {
+				//just skip it
+			}
+			else
+			{
+				//insert the new data
+				mysql_query($line);
+				if(mysql_error()) {
+					echo $line."\n";
+					echo mysql_error()."\n";
+					$err=true;
+				}
+			}
+		}
+		echo "
"; + if($err) { + echo error(i18n("An error occured while importing the restore database")); + } + else + echo happy(i18n("Database successfully restored")); + + unlink("../data/backuprestore/$filename"); + } + else + echo error(i18n("Invalid filename")); + + send_footer(); + +} +else +{ + echo send_header("Database Backup/Restore", + array('Committee Main' => 'committee_main.php', + 'SFIAB Configuration' => 'config/index.php') + ,"backup_restore" + ); + + + //we try to remove temp files every time we load this page, who knows, maybe they navigated away + //last time instead of clicking the link to come back here + $dh=opendir("../data/backuprestore"); + $removed=false; + while($fn=readdir($dh)) { + if(ereg("[a-z0-9]{32}",$fn)) { + unlink("../data/backuprestore/$fn"); + $removed=true; + } + } + closedir($dh); + + if($removed) { + echo happy(i18n("Temporary files successfully removed")); + } + + + echo "

".i18n("Backup Database")."

\n"; + echo "".i18n("Create Database Backup File")."
\n"; + echo "

\n"; + echo "
\n"; + + echo "

".i18n("Restore Database")."

\n"; + echo i18n(error("WARNING: Importing a backup will completely DESTROY all data currently in the database and replace it with what is in the backup file")); + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "
\n"; + + + send_footer(); +} + +?> diff --git a/config/index.php b/config/index.php index 870d65c..e285fae 100644 --- a/config/index.php +++ b/config/index.php @@ -65,7 +65,7 @@ echo ""; echo " "; echo " "; - echo " \n"; + echo " "; echo " \n"; echo " \n"; echo " "; diff --git a/theme/default/theme.php b/theme/default/theme.php index 88f33d9..6348c22 100644 --- a/theme/default/theme.php +++ b/theme/default/theme.php @@ -66,5 +66,6 @@ $theme['icons']['language_pack_installer']="kanagram.png"; $theme['icons']['new_version_checker']="numbers.png"; $theme['icons']['rollover_fair_year']="svn_switch.png"; + $theme['icons']['backup_restore']=""; ?>
".theme_icon("rollover_fair_year")."
".i18n("Rollover Fair Year")."
".theme_icon("backup_restore")."
".i18n("Database Backup/Restore")."