<?php
#######################CHECK ERROR ###################################

ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL); 
include 'chk.php';
######################################################################

include 'includes/header.php';




$db = new SQLite3('./api/.boxbr_config.db');
$db->exec('CREATE TABLE IF NOT EXISTS config(id INTEGER PRIMARY KEY NOT NULL, Title TEXT, url TEXT, CreateDate TIMESTAMP, linktype TEXT)');
$res = $db->query('SELECT * ' . "\n\t\t\t\t" . '  FROM config ' . "\n\t\t\t\t" . '  WHERE id=\'' . $_GET['update'] . '\'');
$row = $res->fetchArray();
$id = $row['id'];
$Title = $row['Title'];
$url = $row['url'];
//$rowU = $row['type'];
$newtime = time();

//current file var
$base_file = basename($_SERVER["SCRIPT_NAME"]);
// file image
//$boxbr_file = $Title.'.gif'; 
$target_dir = 'api/sets/';
if (!file_exists($target_dir)) {
	mkdir($target_dir, 511, true);
}
$file_path = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/'.$target_dir;


/////////////////////////////////////////////////////////

if (isset($_POST['url'])) {
	
	if ($_POST['linktype'] == '0') {
	    
//die;
	$uploadOk = 1;
	

	$Title = str_replace(' ', '', $_POST['Title']);
	$file_ext = strtolower(end(explode('.', $_FILES['image']['name'])));
	$extensions = ['webp','WEBP','gif', 'GIF', 'png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'bmp', 'BMP','mp4','MP4','mpeg','MPEG','avi','AVI','mov','MOV'];

	$target_file = $target_dir. $Title.'.'.$file_ext;

	//$target_file = $target_dir. $_FILES['image']['name'];

	if (file_exists($target_file)) {
		unlink($target_file);
	$msg = '?messageA=The file already exists and has been replaced.';
	}else { 
		$msg = '?messageS=Success, File uploaded.'; }

	$uploadOk = 1;

	if (550000000 < $_FILES['image']['size']) {
		$msg = '?messageE=Sorry, your file is too large.';
		//echo $msg;
		$uploadOk = 0;
	}
	if (in_array($file_ext, $extensions) === false) {
		//$errors[] = 'extension not allowed, please choose a JPEG or PNG or GIF or MP4 file.';
				$msg = '?messageE=Sorry, only Image or Video files are allowed.';
		//echo $msg;
		$uploadOk = 0;
	}

	if ($uploadOk == 0) {
	    				
	$db->close();
	header('Location: ./launcher.php'.$msg);
echo '<script>
window.location.href = "./launcher.php'.$msg.'";
	</script>';
	}
	else if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
		$boxbr_patch = '' . $file_path . $Title.'.'.$file_ext . '';
		
		$icon == $boxbr_patch;
	$db->exec('UPDATE config SET url=\'' . $boxbr_patch . '\',' . "\n" . 'CreateDate=\'' . $newtime . '\', linktype=\'0\'' . "\n\t\t\t\t\t\t" . '  WHERE ' . "\n\t\t\t\t\t\t\t" . '  id=\'' . $_POST['id'] . '\'');								
									
	$db->close();
	header('Location: ./launcher.php'.$msg);
echo '<script>
window.location.href = "./launcher.php'.$msg.'";
	</script>';
	
	}
}else{
	$url = $_POST['url'];
    $target_file = $target_dir . basename($url);
	$file_ext = strtolower(end(explode('.', $url)));
	$extensions = ['webp','WEBP','gif', 'GIF', 'png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'bmp', 'BMP','mp4','MP4','mpeg','MPEG','avi','AVI','mov','MOV'];
	
	if (in_array($file_ext, $extensions) === false) {
		//$errors[] = 'extension not allowed, please choose a JPEG or PNG or GIF or MP4 file.';
				$msg = '?messageE=Sorry, only Image or Video files are allowed.';
		//echo $msg;
	$db->close();
	header('Location: ./launcher.php'.$msg);
echo '<script>
window.location.href = "./launcher.php'.$msg.'";
	</script>';
	}else{
	
		$msg = '?messageS=Saved!, Url of saved file..';
		
		$boxbr_patch = $url;
		
	$db->exec('UPDATE config SET url=\'' . $boxbr_patch . '\',' . "\n" . 'CreateDate=\'' . $newtime . '\', linktype=\'1\'' . "\n\t\t\t\t\t\t" . '  WHERE ' . "\n\t\t\t\t\t\t\t" . '  id=\'' . $_POST['id'] . '\'');								
							
									
	$db->close();
	header('Location: ./launcher.php'.$msg);
echo '<script>
window.location.href = "./launcher.php'.$msg.'";
	</script>';
	}
  }
}


echo ' <!-- Begin Page Content -->' . "\n";
echo '        <div class="container-fluid">' . "\n";
echo "\n";

if (isset($_GET['succes'])) {
	echo $msg_delete;
}

if (isset($_GET['success'])) {
	echo $msg_success;
}
echo '<div class="loader2">
  <div class="loading2">
  </div>
</div>   ' . "\n";
echo '
<style>
.loader2 {
  display: none;
  top: 50%;
  left: 50%;
  position: absolute;
  transform: translate(-50%, -50%);
}

.loading2 {
  border: 2px solid #ccc;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border-top-color: #1ecd97;
  border-left-color: #1ecd97;
  animation: spin 1s infinite ease-in;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style> ' . "\n";
echo '          <div class="row">' . "\n";
echo "\n";
echo '            <!-- First Column -->' . "\n";
echo '            <div class="col-lg-12">' . "\n";
echo "\n";
echo '              <!-- Custom codes -->' . "\n";
echo '                <div class="card text-text-primary">' . "\n";
echo '                <div class="card-header bg-primary py-3">' . "\n";
echo '                <center><h4 align="center" class="m-0 text-white"><i class="fa fa-pencil"
                                    style="font-size:20px">&nbsp;&nbsp;</i>Edit</h4>
						</center>' . "\n";
echo '            </div>' . "\n";

echo '                <div class="card-body">' . "\n";
echo '                  <form id="form-id" method="post" enctype="multipart/form-data">' . "\n";
echo '                        <div class="form group">' . "\n";
echo '                                    <label class="control-label " for="Title"><strong>' . "\n";
echo '                                        Title: (' . $Title . ')</strong>' . "\n";
echo '                                    </label>' . "\n";
echo '                                    <div class="form-group">' . "\n";
echo '                                        <input type="hidden" name="id" value="' . $id . '">' . "\n";
echo '                                        <input type="hidden" class="form-control" class="form-control" id="Title" name="Title"  value="' . $Title . '" type="text" readonly/>' . "\n";
echo '                                    </div></div>' . "\n";
echo '                                    ' . "\n";
?>
                           
                                <div class="form-group ">
                                   <label class="control-label " for="vpn_config">
                                   <strong>Select file upload type</strong>
                                  </label>
                                 <div class="input-group">
									  <select class="form-control linktype" id="linktype" name="linktype">
										  <option data-value="op0" value="0" <?=$row['linktype']=='0'?'selected':'' ?>>Upload a File
										  </option>
										  <option data-value="op1" value="1" <?=$row['linktype']=='1'?'selected':'' ?>>Enter External URL
										  </option>
									  </select>
                                    </div>
                                </div>
                        
                    <div class="activeu">
                     <div class="form-group ">
                             <label class="control-label"><strong> Enter External URL </strong></label>
                                <div class="input-group">
                                 <input  class="form-control" type="text" name="url" value="<?php echo $url; ?>" placeholder="Enter URL Link...">
                                  
                                </div>
                             </div>
                         </div>

                      <div class="actived">
                        <div class="form-group ">
                            <label class="control-label"> <strong> Upload a File</strong></label>
						      <div class="input-group">
						      	<div class="custom-file">
					        	<input type="file" class="custom-file-input" name="image" id="intro" placeholder="Choose Intro" onchange="uploadintro(this)" aria-describedby="intro">
		
			<label class="custom-file-label" for="intro" placeholder="Choose Background Image"><span id="image-intro"></span></label>
				          	   </div>
			                </div>
		        	     </div>
		    	       </div>
                         
                         <script>
                                function spinner() {
        $('#loading').show();
        document.getElementById('form-id').submit();
    }
   //
</script>
<?php 
echo '                            </form>' . "\n";
echo '                                <div class="form-group">' . "\n";
echo '                                    <div>' . "\n";
echo '  <button class="btn btn-success btn-icon-split" name="submit"  onclick="spinner();" dtype="submidt" data-loading-text="<i class=\'fa fa-spinner fa-spin \'></i> Processing...">' . "\n";
echo '                        <span class="icon text-white-50"><i class="fas fa-check"></i></span><span class="text">Submit</span>' . "\n";
echo '                        </button>' . "\n";
echo '                            </div>' . "\n";
echo '                            </div>' . "\n";
echo '                </div>' . "\n";
echo '              </div>' . "\n";
echo '            </div>' . "\n";
echo '            </div>' . "\n";
echo '            </div>' . "\n";
echo "\n";
?>
<br><br>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
//select activecode form
//var response = {};
//response.val = "op2";
//$("#codemode option[data-value='" + response.val +"']").attr("selected","selected");

//hide activecode form
$('.actived').show(); 

$('.activeu').hide(); 


//Show/hide activecode select
$(document).ready(function(){
  $('.linktype').change(function(){
    if($('.linktype').val() > 0) {
      $('.actived').hide(); 
      $('.activeu').show(); 
    } else {
      $('.activeu').hide(); 
      $('.actived').show(); 
     document.getElementById("activeu").value = ' ';
    } 
  });
  $('.linktype').ready(function(){
    if($('.linktype').val() > 0) {
      $('.actived').hide(); 
      $('.activeu').show(); 
    }else {
      $('.activeu').hide(); 
      $('.actived').show(); 
     document.getElementById("actived").value = ' ';
      
    } 
  });
});
</script>
<script>
function formSubmit(radioObj){
  if(radioObj.checked){
    document.getElementById("onoff").submit();

  }
}
</script>
<?php
include 'includes/footer.php';
require 'includes/boxbr.php';
echo '</body>' . "\n";

?>