<?php 


$db = new SQLite3('./api/.db.db');
$db->exec("CREATE TABLE IF NOT EXISTS appstore(id INTEGER PRIMARY KEY NOT NULL, app_name VARCHAR(25), app_id VARCHAR(25), app_description VARCHAR(50), app_banner VARCHAR(25), app_install_url VARCHAR(75))");
//$db->exec("INSERT INTO appstore(app_name, app_id , app_description, app_banner , app_install_url) VALUES('testtest', 'test.test.test', 'app description', 'banner.jpg', 'http://app_install_url.com')");
$res = $db->query('SELECT * FROM appstore');

if(isset($_GET['delete'])){
$db->exec("DELETE FROM appstore WHERE id=".$_GET['delete']);
header("Location: dns1manager.php");
}

?>


<div class="main-panel">
      <!-- Navbar -->
<?php 
///////////////////////////////////////////////
///////////VARIABLES//////////////////
////////ONLY CHANGE ME////////////////
$folder_name = "";
$file = $folder_name.'6.json';
$logo_img = $folder_name.'logo6.png';
$bg_img = $folder_name.'mainbg6.jpg';
$intro_img = $folder_name.'intro6.mp4';
//////////////////////////////////////

//read json data array
$jsondata = file_get_contents($file);
$json = json_decode($jsondata, true);
//read and change dns
if(isset($_POST['text'])){
$replacementData = array('servers'	=>	$_POST["servers"],'message' 	=>	$_POST["message"]);
$newArrayData = array_replace_recursive($json, $replacementData);
$newJsonData = json_encode($newArrayData, JSON_PRETTY_PRINT);
file_put_contents($file, $newJsonData);
header('Location: ' . $_SERVER['PHP_SELF']);
}
// Check image files
if(isset($_POST["images"])) {
	$errors= array();
	$file_name = $_FILES['image']['name'];
	$file_size =$_FILES['image']['size'];
	$file_tmp =$_FILES['image']['tmp_name'];
	$file_type=$_FILES['image']['type'];
	$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
	$extensions= array("jpeg","jpg","png","gif","JPEG","JPG","PNG","GIF", "mp4", "MP4");
	//check ext
	if(in_array($file_ext,$extensions)=== false){
		$errors[]="extension not allowed, please choose a JPEG or PNG or GIF or MP4 file.";
	}
	//check file size
	if($file_size > 5242880 ){
		$errors[]='File size must not exceed 5 MB';
	}
	//changes file name based on logo or main bg or intro
	if(empty($errors)==true){
		if($_POST["logo"] == 'logo'){
			$file_name1 = 'logo6.png';}
		if($_POST["bg"] == 'bg'){
			$file_name1 = 'mainbg6.jpg';}
		if($_POST["intro"] == 'intro'){
			$file_name1 = 'intro6.mp4';}
		//push temp file to server folder
		move_uploaded_file($file_tmp,$folder_name.$file_name1);
	}else{
		print_r($errors);
	}
	header('Location: ' . $_SERVER['PHP_SELF']);
}
//get current url address
if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  $protocol = 'https://';
}else {
  $protocol = 'http://';
}
$basedir = $protocol.$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/'));
//makes dir
if (!is_dir($folder_name)) {
	mkdir($folder_name, 0755);
}
//makes json file
if (!file_exists($file)) {
	$json ="{
  \"token\": \"OK\",
  \"servers\": \"http://change_me6.dns:8080\",
  \"message\":\"Welcome to service6\", 
  \"package\": \"3.3\",
  \"background\": \"$basedir/$folder_name/mainbg6.jpg\",
  \"logo\": \"$basedir/$folder_name/logo6.png\",
  \"intro\": \"$basedir/$folder_name/intro6.mp4\",
  \"help\": \"A\",
  \"estatus\": \"1\" 
}";
	file_put_contents($file, $json);
}
//makes php file to echo json
$php = $folder_name.'6.php';
if (!file_exists($php)) {
		$data ="<?php
echo file_get_contents('6.json');
?>";
	file_put_contents($php, $data);
}
//makes index to prevent snoopers
$index = $folder_name.'index.php';
if (!file_exists($index)) {
		$data ="<?php
echo 'Hello World';
?>";
	file_put_contents($index, $data);
}
include ('includes/header.php');
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<style>
body {
	background-color: #343a40;
}
input[type=text] {
  background-color: #3CBC8D;
  color: white;
}
.box {
  background: #ffffff;
  color: #ffffff;
  width: 250px;
  padding: 10px;
  margin: 1em auto;
}
p {
  margin: 1.5em 0;
  padding: 0;
}
input[type="checkbox"] {
  visibility: hidden;
}
label {
  cursor: pointer;
}
input[type="checkbox"] + label:before {
  border: 1px solid #ffffff;
  content: "\00a0";
  display: inline-block;
  font: 16px/1em sans-serif;
  height: 16px;
  margin: 0 .25em 0 0;
  padding: 0;
  vertical-align: top;
  width: 16px;
}
input[type="checkbox"]:checked + label:before {
  background: #fff;
  color: #333;
  content: "\2713";
  text-align: center;
}
input[type="checkbox"]:checked + label:after {
  font-weight: bold;
}

input[type="checkbox"]:focus + label::before {
    outline: rgb(59, 153, 252) auto 5px;
}


</style>
<body>

      <!-- Navbar -->
      <div class="wrapper ">
<p class="m-0 text-center text-white small">Copyright &copy; LiveWireApps 2020</p>

<div class="main-panel">
      <div class="content">
        <div class="container-fluid">

	<?if(isset($_GET['message'])){echo $_GET['message'];}?>
    <div style="width:600px; margin:0 auto;">
		<div class="row">
		<div class="center">
					<div style="width:600px; margin:0 auto;">
			<h1 class="text-center text-light">Legazy Manager</h1>
				<br>   
  				<br>
	<div class="container" >
	    <div style="width:600px; margin:0 auto;">
		<div class="row">
		<div class="center">
					<div style="width:600px; margin:0 auto;">
					<h2 class="text-center text-warning">Server 6</h2>
						<form method="post"> 
							<div class="form-group">
								<h4 class="text-center text-success">Change DNS & Message</h4>
								<h6 class="form-text text-info">Current DNS is in the box</h6>
								<input type="text" class="form-control" placeholder="DNS:PORT" name="servers" value="<?=$json['servers']?>">
							</div>
							<div class="form-group">
								<h6 class="form-text text-info">Current Message is in the box</h6>
								<input type="text" class="form-control" placeholder="Type a Welcome Message" name="message" value="<?=$json['message']?>">
							</div>
							<button type="submit" name="text" class="btn btn-primary">Update</button>
							<button type="submit" class="btn btn-primary" onClick="refreshPage()">Click Here To Refresh Page And See Changes</button>
						</form>
						<br>
						<h2 class="text-center text-warning">Change Images</h2>
						<br>
						<form method="post" enctype="multipart/form-data">
							<h4 class="text-center text-success">Change Logo</h4>
							<h6 class="form-text text-info">Image name will be renamed to 'logo6.png'</h6>
							<div class="input-group">
								<div class="custom-file">
									<input type="file" class="custom-file-input" name="image" id="inlogo" onchange="uploadlogo(this)" aria-describedby="inlogo">
									<label class="custom-file-label" for="inlogo" placeholder="Choose Image"><span id="image-logo"></span></label>
									<input type="hidden" name="logo" value="logo">
								</div>
								<button type="submit" name="images" class="btn btn-primary">Upload</button>
							</div>
						</form>
						<br>
						<form method="post" enctype="multipart/form-data">
							<h4 class="text-center text-success">Change Background</h4>
							<h6 class="form-text text-info">Image name will be renamed to 'mainbg6.jpg'</h6>
							<div class="input-group">
								<div class="custom-file">
									<input type="file" class="custom-file-input" name="image" id="inbg" onchange="uploadbg(this)" aria-describedby="inbg">
									<label class="custom-file-label" for="inbg" placeholder="Choose Image"><span id="image-bg"></span></label>
									<input type="hidden" name="bg" value="bg">
								</div>
								<button type="submit" name="images" class="btn btn-primary">Upload</button>
							</div>
						</form>
						<br>
						<form method="post" enctype="multipart/form-data">
							<h4 class="text-center text-success">Change Intro Video</h4>
							<h6 class="form-text text-info">Video name will be renamed to 'intro6.mp4'</h6>
							<div class="input-group">
								<div class="custom-file">
									<input type="file" class="custom-file-input" name="image" id="intro" onchange="uploadintro(this)" aria-describedby="intro">
									<label class="custom-file-label" for="intro" placeholder="Choose Intro"><span id="image-intro"></span></label>
									<input type="hidden" name="intro" value="intro">
								</div>
								<button type="submit" name="images" class="btn btn-primary">Upload</button>
							</div>
						</form>
						<br>
						<div class="container">
							<div class="row">
								<div class="col-sm-6">
									<div class="card">
										<div class="card-block">
											<h4 class="card-title text-center card card bg-light text-dark">Current logo6.png</h4>
										</div>
										<img class="card-img-bottom" src="<? echo "$logo_img?".filemtime($logo_img)?>" alt="Card image" style="width:100%">
									</div>
								</div>
								<div class="col-sm-6">
									<div class="card">
										<div class="card-block">
											<h4 class="card-title text-center card card bg-light text-dark">Current mainbg6.jpg</h4>
										</div>
										<img class="card-img-bottom" src="<?echo "$bg_img?".filemtime($bg_img)?>" alt="Card image" style="width:100%">
									</div>
								</div>
							</div>
						</div>
						<br>
						<div class="container">
							<div class="row">
								<div class="col-sm-12">
									<div class="card">
										<div class="card-block">
											<h4 class="card-title text-center card card bg-light text-dark">Current intro6.mp4</h4>
										</div>
										<video class="card-img-bottom" width="400" controls>  <source src="<?php echo "$intro_img?t=" .time();?>" type="video/mp4"></video>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
<br><br>
<br><br>

</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script>
function uploadlogo(target) {
	document.getElementById("image-logo").innerHTML = target.files[0].name;
}
function uploadbg(target) {
	document.getElementById("image-bg").innerHTML = target.files[0].name;
}
function uploadintro(target) {
	document.getElementById("image-intro").innerHTML = target.files[0].name;
}
</script>