<?php
	$path="../../";
	include("../../map.php");
	include("../../secure.php");
	if($_GET["cmd"]==""){
	include("../../scripts.php");
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
		$os="win";
	} else {
		$os="lin";
	}
	$mysql_ver=$conn->server_info;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	<script type="text/javascript" src="//cdn.<?php echo $cdnurl?>/js/wpanel.js" lang="<?php echo $sdil[150];?>" id="wjs" url="<?php echo $_SERVER['SERVER_NAME'];?>" pow="<?php echo $pow1?>" ver="<?php echo $pow2?>"></script>
    <title>Mysql</title>
</head>
<body>
    <div class="qmodal">
        <div class="qmodal-content">
		<div class="alert alert-warning" id="result" style="display:none;"></div>
            <!-- Header with Action Buttons -->
            <div class="qmodal-header">
			<div class="header-title">
				<nav aria-label="breadcrumb">
					<ol class="breadcrumb">
						<li class="breadcrumb-item"><a href="?"><i class="fa fa-home"></i></a></li>
						<li class="breadcrumb-item"><a href="#"><?php echo $mysql_ver?></a></li>
						<li class="breadcrumb-item text-<?php if(substr(phpversion(),0,3)<>"7.4"){echo "danger";}else{echo "success";}?> active" aria-current="page"><?php echo $os?> <?php echo phpversion()?></li>
					</ol>
				</nav>
			</div>
			<!-- İlk Menü -->
			<div class="btn-group" style="margin-right:5px;">
			  <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
				SQL İşlemleri <span class="caret"></span>
			  </button>
			  <ul class="dropdown-menu" role="menu">
				<li>
				  <a href="?islem=sqlbackup" onclick="show('preload');">
					<i class="fa fa-download" aria-hidden="true"></i> Yedekle
				  </a>
				</li>
				<li>
				  <a href="?islem=backups&ne=sql">
					<i class="fa fa-database" aria-hidden="true"></i> Yedek list
				  </a>
				</li>
			  </ul>
			</div>
			<!-- İkinci Menü -->
			<div class="btn-group msa">
			  <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
				Yönetici İşlemleri <span class="caret"></span>
			  </button>
			  <ul class="dropdown-menu" role="menu">
				<li>
				  <a href="?islem=filebackup&file=deppo" onclick="show('preload');">
					<i class="fa fa-download" aria-hidden="true"></i> Web Yedek
				  </a>
				</li>
				<li>
				  <a href="?islem=filebackup" onclick="show('preload');">
					<i class="fa fa-download" aria-hidden="true"></i> Dosya Yedek
				  </a>
				</li>
				<li>
				  <a href="?islem=backups&ne=zip">
					<i class="fas fa-file-archive"></i> Yedekler
				  </a>
				</li>
				<li>
				  <a href="?islem=mysql">
					<i class="fa fa-list" aria-hidden="true"></i> Mysql Yapı
				  </a>
				</li>
				<li>
				  <a href="?islem=repair" onclick="show('preload');">
					<i class="fas fa-wrench"></i> Mysql Repair
				  </a>
				</li>
			  </ul>
			</div>
            </div>
            <!-- Left Tab Menu -->
            <div class="v8-columns">
<div class="alert alert-warning"><?php if($module["backup_notify"]==1){echo "Otomatik yedekleme aktif, <b>".$module["backup_days"]." Gün</b>";}else{echo "Otomatik yedekleme Pasif";}?></div>
                <!-- Content Area -->
                <div class="">
<?php
if($_GET["islem"]=="sqlbackup"){
	define("BACKUP_PATH", "../../../deppo/backup/");
	$date_string   = date("dmY");
	include("backup_lin.php");
	echo location("?islem=backups&ne=sql",self,500,1);
}elseif($_GET["islem"]=="check"){
	$dir = opendir("../../../deppo/backup");
	while (($dosya = readdir($dir)) !== false){
		if(! is_dir($dosya)){
			if($dosya[0]=="i"){
				$sqlfile="../../../deppo/backup/".$dosya."";
			}
		}
	}
	closedir($dir);
}elseif($_GET["islem"]=="repair"){
// Tüm tabloları al
$tablesResult = $conn->query("SHOW TABLES");
while ($tableRow = $tablesResult->fetch_array()) {
    $tableName = $tableRow[0];
    $conn->query("DELETE FROM `$tableName` WHERE id = 0");
    // id sütununu AUTO_INCREMENT ve birincil anahtar yap
    $conn->query("ALTER TABLE `$tableName` DROP PRIMARY KEY");
    $conn->query("ALTER TABLE `$tableName` MODIFY id INT AUTO_INCREMENT");
    $conn->query("ALTER TABLE `$tableName` ADD PRIMARY KEY (id)");
}
echo "İşlem tamamlandı.";
$conn->close();
}elseif($_GET["islem"]=="filebackup"){
	define("BACKUP_PATH", "../../../deppo/backup/");
	$date_string   = date("dmY");
	//$cmd = "mysqldump --routines -h {$V3Server} -u {$V3User} -p{$V3Pass} {$V3Db} > " . BACKUP_PATH . "{$date_string}_lin_{$V3Db}.sql"; exec($cmd);
	include("backup_lin.php");
	$the_folder = "../../../".$_GET["file"]."";
	$zip_file_name = "../../../deppo/backup/files_".date("dmY").".zip";
	class FlxZipArchive extends ZipArchive{
		public function addDir($location="", $name=""){
			$this->addEmptyDir($name);
			$this->addDirDo($location, $name);
		}
		private function addDirDo($location="", $name="") {
			$name .= '/'; $location .= '/';
			$dir = opendir ($location);
			while ($file = readdir($dir)){
				if ($file == '.' || $file == '..' || $file == 'cache' || $file == 'online.txt' || $file == '.htaccess' || $file == 'counter.json' || $file == 'crmplus' || $file == 'account' || $file == 'sale' || $file == 'tools') continue;
				$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
				$this->$do($location . $file, $name . $file);
			}
		} 
	}
	$za = new FlxZipArchive;
	$res = $za->open($zip_file_name, ZipArchive::CREATE);
	if($res === TRUE) {
		$za->addDir($the_folder, basename($the_folder)); $za->close();
	}
	else  { echo 'Could not create a zip archive';}
	echo location("?islem=backups&ne=zip",self,500,1);
}elseif($_GET["islem"]=="backups"){
	echo "<ul class=\"list-group\">";
	$dir = opendir("../../../deppo/backup");
	while (($dosya = readdir($dir)) !== false){
		if(! is_dir($dosya)){
			if(substr($dosya,-3)==$_GET["ne"] or substr($dosya,-2)==$_GET["ne"]){
				if($MSA==1){
					echo "<li class=\"list-group-item\"> <span class=\"badge\"><a href=\"?islem=import&file=".$dosya."\" onclick=\"return confirm('#SqlImport Tüm kayıtlı veriler silinecek ?')\" class=\"fa fa-upload\" style=\"color:#fff;\"></a></span> <span class=\"badge\"><a href=\"../../../deppo/backup/".$dosya."\" download class=\"fa fa-download\" style=\"color:#fff;\"></a></span> <span class=\"badge\"><a href=\"?islem=sil&file=".$dosya."\" class=\"fa fa-trash\" style=\"color:#fff;\"></a></span> ".$dosya."</li>";
				}elseif($_SESSION["kim"]=="yonetici"){
					echo "<li class=\"list-group-item\"> <span class=\"badge\" style=\"background:#ccc;\"><a href=\"#\" download class=\"fa fa-download\" style=\"color:#fff;\"></a></span> <span class=\"badge\" style=\"background:#ccc;\"><a href=\"#\" class=\"fa fa-trash\" style=\"color:#fff;\"></a></span> ".$dosya."</li>";
				}else{
					echo "<li class=\"list-group-item\"> <span class=\"badge\"><a href=\"../../../deppo/backup/".$dosya."\" download class=\"fa fa-download\" style=\"color:#fff;\"></a></span> <span class=\"badge\"><a href=\"?islem=sil&file=".$dosya."\" class=\"fa fa-trash\" style=\"color:#fff;\"></a></span> ".$dosya."</li>";
				}
			}
		}
	}
	closedir($dir);
	echo "</ul>";
}elseif($_GET["islem"]=="sil"){
	unlink("../../../deppo/backup/".$_GET["file"]."");
	if(substr($_GET["file"],-3)=="sql"){
		echo location("?islem=backups&ne=sql",self,500,1);
	}else{
		echo location("?islem=backups&ne=zip",self,500,1);
	}	
}elseif($_GET["islem"]=="import" and ($_SESSION["user"]==V3d('TWFzdGVy') or $_SESSION["user"]==V3d('U3VwZXJBZG1pbg=='))){
	$filepath="../../../deppo/backup";
	$sqlfile=$_GET["file"];
	define("BACKUP_PATH", "../../../deppo/backup/");
	//$cmd = "mysql -h {$V3Server} -u {$V3User} -p{$V3Pass} {$V3Db} < $sqlfile"; exec($cmd);
	echo "ok";
}elseif($_GET["islem"]=="mysql"){
		if (!$conn) { die('Hata: ' . mysqli_connect_error()); }else{
		if($_GET["ne"]==""){
			echo "<form action=\"?islem=mysql&ne=addtbl\" method=\"post\">";
			echo "<div class=\"input-group\">";
			echo "<span class=\"input-group-addon\" id=\basic-addon1\"><i class=\"fa fa-clone\"></i></span>";
			echo "<select name=\"from\" class=\"form-control\" style=\"width:20%; float:left;\">";
			$rt = mysqli_query($conn, "SHOW TABLES FROM $V3Db"); while ($r0 = mysqli_fetch_row($rt)){
				echo "<option value=\"{$r0[0]}\">{$r0[0]}</option>";
			}
			echo "</select>";
			echo "<input type=\"text\" class=\"form-control\"  style=\"width:80%; float:left;\" placeholder=\"Hedef Tablo\" name=\"to\" aria-describedby=\"basic-addon1\">";
			echo "</div>";
			echo "</form>";
			
			echo "<ul class=\"list-group msa\">";
				$result = mysqli_query($conn, "SHOW TABLES FROM $V3Db"); while ($row = mysqli_fetch_row($result)) {
				echo "<li class=\"list-group-item\"> <span class=\"badge\"><a href=\"?islem=mysql&ne=deltbl&tbl=".$row[0]."\" class=\"fa fa-trash\" style=\"color:#fff;\"></a></span><span class=\"badge\"><a href=\"#\" onclick=\"toggle('#".$row[0]."')\" class=\"fa fa-eye\" style=\"color:#fff;\"></a></span> ".$row[0]."</li>";
				echo "<ul class=\"list-group\" style=\"display:none;\" id=\"".$row[0]."\">";
				$sql2 = mysqli_query($conn, "SHOW COLUMNS FROM ".$row[0].""); while($col = mysqli_fetch_row($sql2)){
					echo "<li class=\"list-group-item\"> <span class=\"badge\">".$col[1]."</span> ".$col[0]."</li>";
				}
				echo "</ul>";
			}
			echo "</ul>";
		}elseif($_GET["ne"]=="addtbl"){
			mysqli_query($conn, "CREATE TABLE ".$_POST["to"]." LIKE ".$_POST["from"]."");
			$q = array();
			$columns = array();
			$resultc = mysqli_query($conn, "SHOW COLUMNS FROM ".$_POST["from"]."");
			while ($rowc = mysqli_fetch_array($resultc)) {
				$columns[] = $rowc[0];
			}
			$q[] = "TRUNCATE TABLE ".$_POST["to"]."" ;
			$result = mysqli_query($conn, "SELECT * FROM ".$_POST["from"]."");
			while ($row = mysqli_fetch_array($result)) {
				$query = "INSERT INTO ".$_POST["to"]." (" ;
				$comma = "" ;
				foreach ($columns as $column) { $query .= $comma."`".$column."`"; $comma = ","; }
				$query .= ") VALUES (" ;
				$comma = "" ;
				foreach ($columns as $column) { $query .= $comma."'".addslashes($row[$column])."'"; $comma = ","; }
				$query .= ")" ;
				$q[] = $query;
			}
			foreach ($q as $query) {
				mysqli_query($conn, $query) or die('Hata: ' . mysqli_connect_error());
			}
			echo location("?islem=mysql",self,500,1);
			mysqli_close($conn);
		}elseif($_GET["ne"]=="deltbl"){
			$resultc = mysqli_query($conn, "DROP TABLE ".$_GET["tbl"]."");
			echo location("?islem=mysql",self,500,1);
		}
	}	
}
?>
                 </div>
            </div>
<?php }?>
<?php
if($_GET["cmd"]=="sqlislem"){
	$filename="../../../deppo/backup/isql.sql";
	define("BACKUP_PATH", "../../../deppo/backup/");
	echo "<i class='fa fa-check' aria-hidden='true'></i>";
}
?>
</body>
<?php echo $footer?>
</html>