';
$action=( isset($_GET['action']) ) ? $_GET['action'] : "status";
$checkit=( isset($_GET['checkit']) ) ? urldecode($_GET['checkit']) : "";
$repair=( isset($_GET['repair']) ) ? $_GET['repair'] : 0;
if ($action == "deletehtaccess")
{
	@unlink($config['paths']['root'] . '.htaccess');
	@unlink($config['paths']['root'] . '.htpasswd');
	$action="status";
}
$is_htaccess=( file_exists("./.htaccess") );
if ($is_htaccess) $htaccess_exist=file(".htaccess");
$dba=$hta_dir=$Overwrite=$msg="";
//MySQL-Verbindung herstellen
MSD_mysql_connect();
if (isset($_POST['htaccess']) || $action == "schutz")
{
	if ($is_htaccess) $Overwrite='
' . $lang['htaccess8'] . '
';
	$cry_txt=array(
					
					$lang['htaccess5'], 
					$lang['htaccess6'], 
					$lang['htaccess7']
	);
	$step=( isset($_POST['step']) ) ? $_POST['step'] : 0;
	$cryptart=( isset($_POST['cryptart']) ) ? $_POST['cryptart'] : 0;
	$uname=( isset($_POST['username']) ) ? $_POST['username'] : "";
	$upass1=( isset($_POST['userpass1']) ) ? $_POST['userpass1'] : "";
	$upass2=( isset($_POST['userpass2']) ) ? $_POST['userpass2'] : "";
	
	if ($step == 1)
	{
		$msg="";
		if ($uname == "") $msg=$lang['htaccess9'];
		if (( $upass1 != $upass2 ) || ( $upass1 == "" )) $msg.=$lang['htaccess10'];
		if ($msg != "")
		{
			$msg='' . $msg . '
';
			$step=0;
		}
		else
		{
			$msg='' . $lang['htaccess11'] . '
';
		}
	}
	elseif ($step == 2)
	{
		$htaccess="AuthName \"MySQLDumper\"\nAuthType Basic\nAuthUserFile \"" . $config['paths']['root'] . ".htpasswd\"\nrequire valid-user\n";
		if ($cryptart == 0) $userpass=crypt($upass1,CRYPT_STD_DES);
		elseif ($cryptart == 1) $userpass=md5($upass1);
		else $userpass=$upass1;
		$htpasswd=$uname . ":" . $userpass;
		@chmod($config['paths']['root'],0777);
		
		if ($file_htpasswd=@fopen(".htpasswd","w"))
		{
			fputs($file_htpasswd,$htpasswd);
			fclose($file_htpasswd);
			$file_htaccess=@fopen(".htaccess","w");
			fputs($file_htaccess,$htaccess);
			fclose($file_htaccess);
			$msg=$lang['htaccess12'] . '' . $lang['htaccess13'] . ' .htaccess: ' . "\n" . htmlspecialchars($htaccess) . '' . $lang['htaccess13'] . ' .htpasswd: ' . "\n" . htmlspecialchars($htpasswd) . " ";
			@chmod($config['paths']['root'],0755);
		}
		else
		{
			$msg='' . $lang['htaccess14'] . '
' . $lang['htaccess13'] . ' .htaccess: ' . "\n" . htmlspecialchars($htaccess) . '' . $lang['htaccess13'] . ' .htpasswd: ' . "\n" . htmlspecialchars($htpasswd) . ' ';
		}
	}
	
	//Ausgabe
	echo headline($lang['htaccess1']) . $Overwrite;
	if ($step < 2)
	{
		
		if ($step == 0)
		{
			$un=' ';
			$p1=' ';
			$p2=' ';
			$cry=' ' . $cry_txt[0] . '  ';
			$cry.=' ' . $cry_txt[1] . '  ';
			$cry.=' ' . $cry_txt[2] . '  ';
		
		}
		else
		{
			$un=$uname . ' ';
			$p1='*************** ';
			$p2='*************** ';
			$cry=' ' . $cry_txt[$cryptart];
		}
		echo $msg . ' ';
	
	}
	else
		echo $msg;
	echo " ";
	include ( "inc/footer.php" );
	die();
}
if ($action == "edithtaccess")
{
	$htaccessdontexist=0;
	
	if (( isset($_GET['create']) && $_GET['create'] == 1 ) || ( isset($_POST['create']) && $_POST['create'] == 1 ))
	{
		$fp=fopen("$hta_dir.htaccess","w");
		fwrite($fp,"# created by MySQLDumper " . MSD_VERSION . "\n");
		fclose($fp);
	}
	if (isset($_POST['newload']))
	{
		$hta_dir=( isset($_POST['newhtadir']) ) ? $_POST['newhtadir'] : "";
	}
	else
		$hta_dir=( isset($_POST['hta_dir']) ) ? $_POST['hta_dir'] : "";
	if ($hta_dir != "" && substr($hta_dir,-1) != "/") $hta_dir.="/";
	
	if (isset($_POST['submit']) && isset($_POST['thta']))
	{
		$fp=fopen("$hta_dir.htaccess","w");
		fwrite($fp,$_POST['thta']);
		fclose($fp);
	}
	if (file_exists("$hta_dir.htaccess"))
	{
		$htaccess_exist=file("$hta_dir.htaccess");
	}
	else
	{
		$htaccessdontexist=1;
	}
	
	echo headline($lang['htaccess16']);
	echo $lang['htaccess32'];
	echo '';
	}
	else
	{
		echo '' . $hta_dir . '.htaccess existiert nicht. Soll sie erstellt werden ?
';
		echo ' ';
	}
	echo '' . $lang['back'] . ' ';
	exit();
}
if ($action == "phpinfo")
{
	phpinfo();
	echo 'Home 
';
	exit();
}
if ($action == "db")
{
	for ($i=0; $i < count($databases['Name']); $i++)
	{
		if (isset($_POST['empty' . $i]))
		{
			EmptyDB($databases['Name'][$i]);
			$dba='' . $lang['db'] . " " . $databases['Name'][$i] . " " . $lang['info_cleared'] . "
";
			break;
		}
		if (isset($_POST['kill' . $i]))
		{
			$res=mysql_query("DROP DATABASE `" . $databases['Name'][$i] . "`") or die(mysql_error() . "");
			$dba='' . $lang['db'] . " " . $databases['Name'][$i] . " " . $lang['info_deleted'] . "
";
			SetDefault();
			include ( $config['files']['parameter'] );
			echo '';
			break;
		}
		if (isset($_POST['optimize' . $i]))
		{
			mysql_select_db($databases['Name'][$i],$config['dbconnection']);
			$res=mysql_list_tables($databases['Name'][$i],$config['dbconnection']);
			$tabellen='';
			WHILE ($row=mysql_fetch_row($res))
				$tabellen.='`' . $row[0] . '`,';
			$tabellen=substr($tabellen,0,( strlen($tabellen) - 1 ));
			if ($tabellen > "")
			{
				$query="OPTIMIZE TABLE " . $tabellen;
				$res=mysql_query($query) or die(mysql_error() . "");
			}
			$_GET['dbid']=$i;
			$dba='' . $lang['db'] . ' ' . $databases['Name'][$i] . '  ' . $lang['info_optimized'] . '.
';
			break;
		}
		if (isset($_POST['check' . $i]))
		{
			$checkit="ALL";
			$_GET['dbid']=$i;
		}
	}
}
//Hier beginnt die Ausgabe
echo headline('Home');
echo ' ';
if ($action == "status")
{
	//Infos ueber Backups
	$Sum_Files=$Sum_Size=0;
	$Last_BU=Array();
	$sm=( $config['safe_mode'] == 1 ) ? "  (Safemode)" : "";
	
	$dh=opendir($config['paths']['backup']);
	while (false !== ( $filename=readdir($dh) ))
	{
		if ($filename != "." && $filename != ".." && !is_dir($config['paths']['backup'] . $filename))
		{
			$files[]=$filename;
			$Sum_Files++;
			$Sum_Size+=filesize($config['paths']['backup'] . $filename);
			$ft=filectime($config['paths']['backup'] . $filename);
			if (!isset($Last_BU[2]) || ( isset($Last_BU[2]) && $ft > $Last_BU[2] ))
			{
				$Last_BU[0]=$filename;
				$Last_BU[1]=date("d.m.Y H:i",$ft);
				$Last_BU[2]=$ft;
			}
		}
	}
	
	$status='' . $lang['Statusinformationen'] . ' ';
	$status.=DirectoryWarnings();
	//Versionen
	$status.='' . $lang['Versionsinformationen'] . ' ';
	$status.=' ';
	
	$status.='MySQLDumper-Version: ' . MSD_VERSION . '  ';
	$status.='OS: ' . MSD_OS . '  (' . MSD_OS_EXT . ') ';
	$status.='MySQL-Version: ' . MSD_MYSQL_VERSION . '  ';
	$status.='PHP-Version: ' . PHP_VERSION . '  Speicher: ' . $config['php_ram'] . ' MB  ' . ( ( $config['zlib'] ) ? '' : '  ' . $lang['phpbug'] . '.   ' ) . $sm . '  ';
	$status.='Max Execution Time: ' . $config['max_execution_time'] . ' sec.   [PHP-Info]   ';
	$status.=' PHP-Extensions: ' . $config['phpextensions'] . '  ';
	
	if ($config['disabled'] != "") $status.=' ' . $lang['disabledfunctions'] . ': ' . str_replace(',',', ',$config['disabled']) . ' ';
	if (!extension_loaded("ftp")) $status.='' . $lang['noftppossible'] . ' ';
	if (!$config['zlib']) $status.='    ' . $lang['nogzpossible'] . ' ';
	
	//MySQLDumper Informationen
	$status.='' . $lang['MySQL Dumper Informationen'] . ' ' . $lang['info_location'] . ' "' . $_SERVER['SERVER_NAME'] . ' " (' . ( $config['paths']['root'] ) . ') ';
	$status.=$lang['info_actdb'] . ": " . $databases['db_actual'] . "  ";
	if ($is_htaccess)
	{
		$status.='' . $lang['htaccess16'] . ' ';
		$status.=' ' . $lang['delete_htaccess'] . ' ';
	}
	else
	{
		$status.='' . $lang['htaccess1'] . '   ' . $lang['htaccess15'] . ' ';
	}
	//History
	$status.='History ' . $lang['backupfilesanzahl'] . ' ' . $Sum_Files . '  Backups (' . byte_output($Sum_Size) . ' ) ';
	
	$status.=$lang['fm_freespace'] . ': ' . MD_FreeDiskSpace() . '  ';
	if ($Sum_Files > 0) $status.=$lang['lastbackup'] . ' ' . $lang['vom'] . ' ' . ( ( isset($Last_BU[1]) ) ? $Last_BU[1] : " - " ) . '  
	     ' . ( ( isset($Last_BU[1]) ) ? '' : " - " ) . '' . ( ( isset($Last_BU[0]) ) ? $Last_BU[0] : " - " ) . '   ';
	echo $status;
}
elseif ($action == "db")
{
	
	//Datenbanken
	echo '' . $lang['info_databases'] . ' ';
	echo $dba . '
';
	echo '
 ';
}
elseif ($action == "sys")
{
	$sysaction=( isset($_GET['dosys']) ) ? $_GET['dosys'] : 0;
	$msg="";
	$res=@mysql_query("SHOW VARIABLES LIKE 'datadir'",$config['dbconnection']);
	if ($res)
	{
		$row=mysql_fetch_array($res);
		$data_dir=$row[1];
	}
	switch ($sysaction)
	{
		case 1: //FLUSH PRIVILEGES
			$msg="> operating FLUSH PRIVILEGES ";
			$res=@mysql_query("FLUSH PRIVILEGES",$config['dbconnection']);
			$meldung=mysql_error($config['dbconnection']);
			if ($meldung != "")
			{
				$msg.='> MySQL-Error: ' . $meldung;
			}
			else
			{
				$msg.="> Privileges were reloaded.";
			}
			break;
		case 2: //FLUSH STATUS
			$msg="> operating FLUSH STATUS ";
			$res=@mysql_query("FLUSH STATUS",$config['dbconnection']);
			$meldung=mysql_error($config['dbconnection']);
			if ($meldung != "")
			{
				$msg.='> MySQL-Error: ' . $meldung;
			}
			else
			{
				$msg.="> Status was reset.";
			}
			break;
		case 3: //FLUSH HOSTS
			$msg="> operating FLUSH HOSTS ";
			$res=@mysql_query("FLUSH HOSTS",$config['dbconnection']);
			$meldung=mysql_error($config['dbconnection']);
			if ($meldung != "")
			{
				$msg.='> MySQL-Error: ' . $meldung;
			}
			else
			{
				$msg.="> Hosts were reloaded.";
				;
			}
			break;
		case 4: //SHOW MASTER LOGS
			$msg="> operating SHOW MASTER LOGS ";
			$res=@mysql_query("SHOW MASTER LOGS",$config['dbconnection']);
			$meldung=mysql_error($config['dbconnection']);
			if ($meldung != "")
			{
				$msg.='> MySQL-Error: ' . $meldung;
			}
			else
			{
				$numrows=mysql_num_rows($res);
				if ($numrows == 0 || $numrows === false)
				{
					$msg.='> there are no master log-files';
				}
				else
				{
					$msg.='> there are ' . $numrows . ' logfiles ';
					for ($i=0; $i < $numrows; $i++)
					{
						$row=mysql_fetch_row($res);
						$msg.='> ' . $row[0] . '   ' . ( ( $data_dir ) ? byte_output(@filesize($data_dir . $row[0])) : '' ) . ' ';
					}
				}
			}
			break;
		case 5: //RESET MASTER
			$msg="> operating RESET MASTER ";
			$res=@mysql_query("RESET MASTER",$config['dbconnection']);
			$meldung=mysql_error($config['dbconnection']);
			if ($meldung != "")
			{
				$msg.='> MySQL-Error: ' . $meldung;
			}
			else
			{
				$msg.="> All Masterlogs were deleted.";
			}
			break;
	}
	echo '' . $lang['mysqlsys'] . ' ';
	echo '';
	echo '';
	echo '> MysSQL Dumper v' . MSD_VERSION . ' - Output Console ';
	echo ( $msg != "" ) ? $msg : '> waiting for operation ... ';
	echo '
';
}
elseif ($action == "vars")
{
	$var=( isset($_GET['var']) ) ? $_GET['var'] : "prozesse";
	$Titelausgabe=array(
						
						"variables" => $lang['Variabeln'], 
						"status" => $lang['Status'], 
						"prozesse" => $lang['Prozesse']
	);
	echo '' . $lang['mysqlvars'] . ' ' . $Titelausgabe[$var] . '     ';
	echo '' . $lang['Prozesse'] . '    ';
	echo '' . $lang['Status'] . '    ';
	echo '' . $lang['Variabeln'] . '    ';
	
	echo ' 
';
	//Variabeln
	switch ($var)
	{
		case "variables":
			$res=@mysql_query("SHOW variables");
			if ($res) $numrows=mysql_num_rows($res);
			if ($numrows == 0)
			{
				echo $lang['info_novars'];
			}
			else
			{
				echo 'Name ' . $lang['Inhalt'] . '  ';
				for ($i=0; $i < $numrows; $i++)
				{
					$row=mysql_fetch_array($res);
					$cl=( $i % 2 ) ? "dbrow" : "dbrow1";
					echo '' . $row[0] . ' ' . $row[1] . '  ';
				}
			}
			echo '
';
			break;
		case "status":
			$res=@mysql_query("SHOW STATUS");
			if ($res) $numrows=mysql_num_rows($res);
			if ($numrows == 0)
			{
				echo $lang['info_nostatus'];
			}
			else
			{
				echo 'Name ' . $lang['Inhalt'] . '  ';
				for ($i=0; $i < $numrows; $i++)
				{
					$cl=( $i % 2 ) ? "dbrow" : "dbrow1";
					$row=mysql_fetch_array($res);
					echo '' . $row[0] . ' ' . $row[1] . '  ';
				}
			}
			echo '
';
			break;
		case "prozesse":
			if ($config['processlist_refresh'] < 1000) $config['processlist_refresh']=2000;
			if (isset($_GET['killid']) && $_GET['killid'] > 0)
			{
				$killid=( isset($_GET['killid']) ) ? $_GET['killid'] : 0;
				$wait=( isset($_GET['wait']) ) ? $_GET['wait'] : 0;
				if ($wait == 0)
				{
					$ret=mysql_query("KILL " . $_GET['killid']);
					$wait=2;
				}
				else
					$wait+=2;
				
				if ($wait == 0)
				{
					echo '' . $lang['processkill1'] . $_GET['killid'] . $lang['processkill2'] . $ret . '
';
				}
				else
				{
					echo '' . $lang['processkill3'] . $wait . $lang['processkill4'] . $_GET['killid'] . $lang['processkill2'] . $ret . '
';
				}
			
			}
			
			$killid=$wait=0;
			$res=@mysql_query("SHOW FULL PROCESSLIST ");
			if ($res) $numrows=mysql_num_rows($res);
			if ($numrows == 0)
			{
				echo $lang['info_noprocesses'];
			}
			else
			{
				echo 'ID User Host DB Command Time State Info RT: ' . round($config['processlist_refresh'] / 1000) . ' sec  ';
				for ($i=0; $i < $numrows; $i++)
				{
					$cl=( $i % 2 ) ? "dbrow" : "dbrow1";
					$row=mysql_fetch_array($res);
					echo '' . $row[0] . ' ' . $row[1] . ' 
					' . $row[2] . ' ' . $row[3] . ' ' . $row[4] . ' ' . $row[5] . ' 
					' . $row[6] . ' ' . $row[7] . ' 
					kill  ';
					if ($row[0] == $killid && $row[4] == "Killed")
					{
						$wait=$killid=0;
					}
				}
			}
			echo '
';
			echo '
			 
			 
			 
			 ';
			echo '';
			
			break;
	}
}
//Datenbankdetails
if (isset($_GET['dbid']))
{
	$dbid=$_GET['dbid'];
	echo '' . $lang['info_dbdetail'] . '"' . $databases['Name'][$dbid] . '" ';
	
	$res=@mysql_query("SHOW TABLE STATUS FROM `" . $databases['Name'][$dbid] . "`");
	mysql_select_db($databases['Name'][$dbid]);
	if ($res) $numrows=mysql_num_rows($res);
	if ($numrows == 0)
	{
		echo $lang['info_dbempty'];
	
	}
	else
	{
		echo $numrows . ' ' . $lang['info_table1'];
		echo ( $numrows > 1 ) ? $lang['info_table2'] : '';
		echo "\n" . 'Nr. ' . $lang['info_table1'] . ' ' . $lang['info_records'] . ' ' . $lang['info_size'] . ' ' . $lang['info_lastupdate'] . ' ' . $lang['info_optimized'] . ' Status ' . ' ' . "\n";
		$last_update="2000-01-01 00:00:00";
		$s=$s1=$s2="";
		for ($i=0; $i < $numrows; $i++)
		{
			$row=mysql_fetch_array($res);
			
			// Get nr of records -> need to do it this way because of incorrect returns when using InnoDBs
			$sql_2="SELECT count(*) as `count_records` FROM `" . $databases['Name'][$dbid] . "`.`" . $row['Name'] . "`";
			$res2=@mysql_query($sql_2);
			if ($res2 === false)
			{
				echo "\n" . '';
				echo '' . ( $i + 1 ) . ' ' . $row['Name'] . ' ' . $lang['unknown_number_of_records'] . ' ';
				echo '  ' . $lang['no'] . '  ';
				$akt_size=0;
			}
			else
			{
				$row2=mysql_fetch_array($res2);
				$row['Rows']=$row2['count_records'];
				
				$akt_size=$row['Data_length'] + $row['Index_length'];
				$cl=( $i % 2 ) ? 'dbrow' : 'dbrow1';
				echo "\n" . '  ';
				echo '' . ( $i + 1 ) . ' ' . $row['Name'] . ' ' . number_format($row['Rows'],0,",",".") . ' ' . byte_output($akt_size) . '  ' . $row['Update_time'] . ' ';
				if ($row['Data_free'] == 0) echo ' ';
				else echo '  ' . $lang['no'] . '  ';
				echo ' ';
			}
			
			echo '';
			if ($checkit == $row['Name'] || $repair == 1)
			{
				$tmp_res=mysql_query("REPAIR TABLE `" . $row['Name'] . "`");
			}
			if (( $checkit == $row['Name'] || $checkit == "ALL" )) // && $akt_size>0)
			{
				$tmp_res=mysql_query("CHECK TABLE `" . $row['Name'] . "`");
				if ($tmp_res)
				{
					$tmp_row=mysql_fetch_row($tmp_res);
					if ($tmp_row[3] != 'OK') echo $tmp_row[3] . ' ';
					echo ( $tmp_row[3] == "OK" ) ? ' ' : '  repair  ';
				}
				else
					echo "CHECK TABLE `" . $row['Name'] . "`";
			}
			else
			{
				if ($akt_size > 0) echo 'check ';
				else echo "-";
			}
			echo '  ';
			if (isset($row['Update_time'])) if (strtotime($row['Update_time']) > strtotime($last_update)) $last_update=$row['Update_time'];
			$s1=$s1 + $row['Rows'];
			$s2=$s2 + $row['Data_length'] + $row['Index_length'];
		}
		echo '' . $lang['info_sum'] . ' ' . number_format($s1,0,",",".") . ' ' . byte_output($s2) . ' ' . $last_update . ' ';
		echo '  
';
	}
	$edb=$lang['info_emptydb1'] . ' `' . $databases['Name'][$dbid] . '` ' . $lang['info_emptydb2'];
	$kdb=$lang['info_emptydb1'] . ' `' . $databases['Name'][$dbid] . '` ' . $lang['info_killdb'];
	
	echo '';
	if ($numrows > 0) echo ' ';
	echo ' ';
	if ($numrows > 0)
	{
		echo ' ';
		echo ' ';
	}
	echo '';
}
echo MSDFooter();
;
?>