<?php
ob_start();
require_once("init.php");
if(!permission('adminpanel')){exit();}

$tempdir =  BASEDIR . "/temp/";

$tmpl = new Koobi("templates/".ADMINTHEME."/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign('pref', $pref);
$tmpl->register_function("permission", "permission");


$gdversion = gdversion(); 
switch($_REQUEST['edimode']){
	case "2" :
	setcookie("fck", "no", time()+(365*24*3600));
	header("Location:body.php");
	break;
	
	case "1":
	setcookie("fck", "yes", time()+(365*24*3600));
	header("Location:body.php");
	break;
}


function db_size($name) {
global $dbname;
$sql = "SHOW TABLE STATUS FROM " . $dbname . ""; 
if($query = new dbc($sql)) {
while($result = $query->fetcharray()) {
	$tabledata[] = $result;
	}
	
$dbsize = 0;
	for($i=0; $i<count($tabledata); $i++) {
		$dbsize += $tabledata[$i]["Data_length"] + $tabledata[$i]["Index_length"];
		}
	return $dbsize/1024;
  }
} 

// systeminformationen
$off = $langadmin['info_disabled'];
$on = $langadmin['info_enabled'];
$undef = $langadmin['var_undefined'];

$sql = new dbc("SELECT uid FROM ".PREFIX."_user WHERE uid='1'");
$maxupload = str_replace(array('M','m'), '', @ini_get('upload_max_filesize'));
$mysqlversionc = $sql->mysql_version();

$tmpl->assign('magicquotes', (@ini_get('magic_quotes_gpc') == 1) ? $on : $off);
$tmpl->assign('maxmemory', (@ini_get('memory_limit')!='') ? @ini_get('memory_limit') : $undef);
$tmpl->assign('maxupload', file_size($maxupload * 1024));
$tmpl->assign('dbsize',file_size(db_size($dbname)));
$tmpl->assign('phpversion', (@PHP_VERSION != "") ? @PHP_VERSION : $undef);
$tmpl->assign('gdversion', (@gdversion() != "") ? @gdversion() : $undef);
$tmpl->assign('disabledfunctions', (strlen(ini_get('disable_functions')) > 1) ? @ini_get('disable_functions') : $undef);
$tmpl->assign('safemode', (@ini_get('safe_mode') == 1) ? $on : $off);
$tmpl->assign('mysqlversion', ($mysqlversionc!="") ? $mysqlversionc : $undef);
$tmpl->assign('max_execution_time', @ini_get('max_execution_time'));
$tmpl->assign('magic_quotes_runtime', (@ini_get('magic_quotes_runtime') == 1) ? $on : $off);
$tmpl->assign('magic_quotes_sybase', (@ini_get('magic_quotes_sybase') == 1) ? $on : $off);

// systeminformationen

// tabellen
function TableDef($table) {
	global $verbindung;
	$result = new dbc("SHOW FIELDS FROM $table");
	
	$felder = array();
	$prim = array();
	
	while ($row = $result->fetchrow()) {
		if (!$row->Extra=="") {
			$extra = " " . $row->Extra;
		} else {
			$extra = "";
		}
		if ($row->Default=="") {
			$default = " NULL";
		} else {
			$default = " '" . $row->Default . "'";
		}
		if ($row->Key=="PRI") {
			$prim_array[] = "`" . $row->Field . "`";
		}
		
		$felder[count($felder)+1] = "  " . $row->Field . " " . $row->Type . " DEFAULT$default$extra";
	}
	if($prim_array!=""){
		$prim[count($prim)+1] = "  PRIMARY KEY (" . @implode(",", $prim_array) . ")";
		}
		
	$res .= "DROP TABLE IF EXISTS `$table`;\n";
	$res .= "CREATE TABLE `$table` (\n";
	$frak = array_merge($felder, $prim);
	for ($i=0; $i<=count($frak)-1; $i++) {
		$res .= $frak[$i];
		if ($i == count($frak)-1) {
			$res .= "\n";
		} else {
			$res .= ",\n";
		}
	}
	$res .= ") TYPE=MyISAM;\n\n";
	$result->close();
	return $res;
}

function Felder($table) {
	$res = array();
	$result = new dbc("SHOW FIELDS FROM $table");
	while ($row = $result->fetchrow()) {
		$res[count($res)+1] = $row->Field;
	}
	$result->close();
	return $res;
}

if ($_REQUEST['action']=="dboption") {
	$arr = $_REQUEST['ta'];
	reset ($arr);
	
	if($_REQUEST['whattodo']=="dump"){
	// dump start
	
		$arr = $_REQUEST['ta'];
		while (list($key, $val) = each ($arr)) {
		$doit = true;
		if(!ereg("^" . preg_quote($dbprefix), $val)) {
			$doit = false;
		}
		
		if ($doit) {
			$dump .= (TableDef($val));
			$felder = Felder($val);
			$sql = "SELECT * FROM " . $val . "";
			$zeilen = new dbc($sql);
			while ($zrow = $zeilen->fetcharray()) {
				$def = "";
				$cnt = "";
				for ($i=1; $i<=count($felder); $i++) {
					$def .= ", " . $felder[$i];
					$cnt .= ", '" . str_replace("\n", "\\n", addslashes($zrow[$felder[$i]])) . "'";
				}
				$def = substr($def, 2);
				$cnt = substr($cnt, 2);
				$dump .= "INSERT INTO `" . $val . "` (" . $def . ") VALUES (" . $cnt . ");\n";
			}
			$dump .= "\n";
			$zeilen->close();
		}
	}
	#echo  $dump;
	downloadfile($dump, "koobi_" . $pref['k_version_s'] . "_" .  date("d-m-Y") . ".sql", 'text/plain');
	exit;
	// dump ende
	} 
	
	while (list($key, $val) = each ($arr)) {
		$tables .= ", `$val`";
	}
	$tables = substr($tables, 1);
	if ($_REQUEST['whattodo']=="optimize") {
		$query = "OPTIMIZE TABLE  ";
	} else {
		$query = "REPAIR TABLE ";
	}
	$query .= $tables;
	
	
	if ($sql = new dbc($query)) {
		$ok = true;
	} else {
		$ok = false;
	}
	header("Location:body.php");
	$sql->Close();
	if ($ok) {
		$tmpl->assign('message', $langadmin['optimize_table_ok']);
	} else {
		$tmpl->assign('message', $langadmin['optimize_table_notok']);
	}
	
	} else {
	$tabellen = "";
	
	$sql = new dbc("SHOW TABLES");
	while ($row = $sql->FetchArray()) {
		$titel = $row[0];
		if (substr($titel, 0, strlen($dbprefix)) == $dbprefix) {
			$tabellen .= "<option value=\"$titel\" selected>$titel</option>\n";
		}
	}
	$sql->Close();
	}


// datenbank wiederherstellen
if($_REQUEST['restore']==1){
$insert = false;
if($_FILES['file']['size']!=0) {
		$fupload_name = $_FILES['file']['name'];
		$end = @explode(".", $fupload_name);
		$check = $end[1];
		if($check == "sql") {
			@move_uploaded_file($_FILES['file']['tmp_name'], $tempdir . $fupload_name);
			@chmod($target . $fupload_name,0777);
			$insert = true;
		} else {
			$tmpl->assign('msg', '<span style="color:red">'.$langadmin['db_restore_wrong_type'].'</span>');
		}
	}
	// wenn dump existiert
	if($insert){
	  if($fupload_name != "" && file_exists($tempdir . $fupload_name)){
	  	
		// dump lesen
		$handle = @fopen($tempdir . $fupload_name, "r");
		$db = @fread($handle, @filesize($tempdir . $fupload_name));
		@fclose($handle);
		
		$m_ok = 0;
		$m_fail = 0;
		
		$ar = @explode(";\n", $db);
		while (@list($key,$val) = @each($ar)) {
		if (rtrim(ltrim($val)) != "") {
			$q = str_replace("\n","",$val);
			$q = $q . ";";
			if (new dbc($q)) {
				$m_ok++;
			} else {
				$m_fail++;
			}
		  }
		}
		// ende dump
		// temp loeschen
		@unlink($tempdir . $fupload_name);
		$msg = $langadmin['db_restore_ok'];
		$msg .= "<br /><br />" . $langadmin['db_restore_sets_ok'] . " <span style=\"color:green\">$m_ok</span><br/> " . $langadmin['db_restore_sets_notok'] . " <span style=\"color:red\">$m_fail</span><br />";
		$tmpl->assign('msg', $msg);
	    } else {  // if fupload_name
	   $tmpl->assign('msg', '<span style="color:red">'.$langadmin['db_restore_error'].'</span>');
	  }
	} // if insert 
} 
// ende dump einspielen

$tmpl->assign('v_msg', $v_msg);
$tmpl->assign('fetchversion', $versions[0]);
$tmpl->assign('aversion', $versions[1]);
$tmpl->assign('tabellen', $tabellen);
$tmpl->display('body.tpl');

?>