<?php
$meta = '';

/**
 *   functions: cleardir, rmdirr
 *   Copyright (C) 2006  Dao Gottwald
 *
 */

function cleardir ($dir) {
	if (!($dir = dir ($dir))) {
		return false;
	}
	while (false !== $item = $dir->read()) {
		if ($item != '.' && $item != '..' && !rmdirr ($dir->path . DIRECTORY_SEPARATOR . $item)) {
			$dir->close();
			return false;
		}
	}
	$dir->close();
	return true;
}

function rmdirr ($dir) {
	if (is_dir ($dir) && !is_link ($dir)) {
		return cleardir ($dir) ? rmdir ($dir) : false;
	}
	return unlink ($dir);
}

function remove($filename){
	if (is_file($filename) or is_link($filename)) {
		unlink($filename);
	} elseif (is_dir($filename)) {
		rmdirr($filename);
	}
}

if (isset($_POST['reset']) and $_POST['reset'] == 'OK') {



	$doNOTdelete = array('.', '..', 'reset.sql', 'reset.zip', 'reset.php');
	//Alte Dateien löschen
	$dir = scandir('.');
	foreach ($dir as $file){
		if (!in_array($file, $doNOTdelete)) {
			remove($file);
		}
	}
	//Backup zurückspielen
	$zip = new ZipArchive();
	if ($zip->open('reset.zip') === TRUE) {
		$zip->extractTo('.');
		$zip->close();
	} else {
		die ('Fehler beim Entpacked der backup.zip');
	}

	define ( 'main' , TRUE );
	require_once('include/includes/config.php');
	require_once('include/includes/func/db/mysql.php');
	db_connect();

	//Alte Datenbank löschen
	mysql_query('DROP DATABASE `'.DBDATE.'`');
	mysql_query('CREATE DATABASE `'.DBDATE.'`');
	$db = @mysql_select_db(DBDATE, CONN);

	//Backup einspielen
	$sql_file = file_get_contents('reset.sql');
	$sql_file = preg_replace ("/(\015\012|\015|\012)/", "\n", $sql_file);
	$sql_statements = explode(";\n",$sql_file);
	foreach ( $sql_statements as $sql_statement ) {
		if ( trim($sql_statement) != '' ) {
			db_query($sql_statement);
		}
	}

	//Auf Hauptseite
	header('Location: reset.php?result=ok');
	exit();
} elseif ($_GET['result'] == 'ok') {
	$output = '<h1>Reset erfolgreich durchgef&uuml;hrt</h1>
		Du wirst in 7 Sekunden zur <a href="index.php">Startseite</a> weitergeleitet';
	$meta .= '<meta http-equiv="refresh" content="7; URL=index.php">';
} else {
	$output = '<h1>Testumgebung zur&uuml;cksetzen?</h1>
<input type="submit" value="OK" name="reset" /> <input type="button" onclick="history.back()" value="zur&uuml;ck" />';
}

?>

<html>

	<head><title>... ::: [ R E S E T ] ::: ...</title>
<link rel="stylesheet" href="include/designs/ilchClan/style.css" type="text/css">
<?php echo $meta; ?>
</head>

<body>
<form method="post">
<table width="700" class="border" border="0" cellspacing="1" cellpadding="3" align="center">
<tr class="Chead">
<td><b>Reset</b></td>
</tr><tr class="Cmite">
<td align="center">
<?php echo $output; ?>
</td>
</tr>
<tr class="Cdark"><td></td></tr>
 	</table>
</form>
</body>
</html>