<?php

header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
$db = new SQLite3('./api/db/slc_intro.db');
$table_name = 'slcintro';
$base_file = basename($_SERVER['SCRIPT_NAME']);
$intro_file = 'intro.mp4';
$target_dir = 'intro/';
$file_path = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/' . $target_dir;

if (!file_exists('intro')) {
	mkdir('intro', 511, true);
}

$db->exec('CREATE TABLE IF NOT EXISTS ' . $table_name . '(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, url VARCHAR(100), intro_on_off TEXT, type TEXT)');
$rows = $db->query('SELECT COUNT(*) as count FROM ' . $table_name);
$row = $rows->fetchArray();
$numRows = $row['count'];

if ($numRows == 0) {
	$introurl = $file_path . $intro_file;
	$db->exec('INSERT INTO ' . $table_name . '(id, url, intro_on_off, type) VALUES(\'1\', \'' . $introurl . '\', \'yes\', \'0\')');
}

@$resU = $db->query('SELECT * FROM ' . $table_name . ' WHERE id=\'1\'');
@$rowU = $resU->fetchArray();
$new_url = str_replace(' ', '', $rowU['url']);
$intro_on_off = $rowU['intro_on_off'];

if (isset($_POST['intro_on_off'])) {
	if (empty($_POST['intro_on_off']) || ($_POST['intro_on_off'] == 'no')) {
		$intro_on_off = 'no';
		$msg = '?messageE=Saved!, Intro video Inactive.';
	}
	else {
		$intro_on_off = $_POST['intro_on_off'];
		$msg = '?messageS=Saved!, Intro video Activated.';
	}

	$db->exec('UPDATE ' . $table_name . ' SET intro_on_off=\'' . $intro_on_off . '\'' . "\r\n\t\t\t\t\t\t\t\t\t" . 'WHERE ' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'id=\'1\'');
	$db->close();
	header('Location: ' . $base_file . $msg);
}

if (isset($_POST['submit'])) {
	if ($_POST['type'] == '0') {
		$target_file = $target_dir . $intro_file;
		$uploadOk = 1;
		$file_ext = strtolower(end(explode('.', $_FILES['image']['name'])));
		$extensions = ['gif', 'GIF', 'mpeg', 'MPEG', 'mov', 'MOV', 'avi', 'AVI', 'mp4', 'MP4'];

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

		$uploadOk = 1;

		if (5000000 < $_FILES['image']['size']) {
			$msg = '?messageE=Sorry, your file is too large.';
			$uploadOk = 0;
		}

		if (is_int(in_array($file_ext, $extensions))) {
			$msg = '?messageE=Sorry, only MP4 files are allowed.';
			$uploadOk = 0;
		}

		if ($uploadOk == 0) {
			header('Location: ' . $base_file . $msg);
		}
		else if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
			$intro_patch = '' . $file_path . $intro_file . '';
			$db->exec('UPDATE ' . $table_name . ' SET url=\'' . $intro_patch . '\',' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'intro_on_off=\'' . $intro_on_off . '\',' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'type=\'' . $_POST['type'] . '\'' . "\r\n\t\t\t\t\t\t\t\t\t" . 'WHERE id=\'1\'');
			$db->close();
			header('Location: ' . $base_file . $msg);
		}
	}
	else {
		$url = $_POST['url'];
		$target_file = $target_dir . basename($url);
		$file_ext = strtolower(end(explode('.', $url)));
		$extensions = ['jpeg', 'jpg', 'png', 'gif', 'JPEG', 'JPG', 'PNG', 'GIF', 'mp4', 'MP4'];

		if (is_int(in_array($file_ext, $extensions))) {
			$msg = '?messageE=Sorry, only MP4 files are allowed.';
			header('Location: ' . $base_file . $msg);
		}
		else {
			$msg = '?messageS=Saved!, Url of saved MP4 file..';
			$intro_patch = $url;
			$db->exec('UPDATE ' . $table_name . ' SET url=\'' . $intro_patch . '\',' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'intro_on_off=\'' . $intro_on_off . '\',' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'type=\'' . $_POST['type'] . '\'' . "\r\n\t\t\t\t\t\t\t\t\t" . 'WHERE id=\'1\'');
			$db->close();
			header('Location: ' . $base_file . $msg);
		}
	}
}

include 'includes/header.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo ' <!-- Begin Page Content -->' . "\n";
echo '        <div class="container-fluid">' . "\n";
echo "\n";
echo "\n";

if (isset($_GET['message'])) {
	echo '<div class="alert alert-danger bg-primary py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-check"></i> ' . $_GET['message'] . '</h4></div>';
}

if (isset($_GET['messageA'])) {
	echo '<div class="alert alert-danger bg-primary py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-check"></i> ' . $_GET['messageA'] . '</h4></div>';
}

if (isset($_GET['messageE'])) {
	echo '<div class="alert alert-danger bg-danger py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-times"></i> ' . $_GET['messageE'] . '</h4></div>';
}

if (isset($_GET['messageS'])) {
	echo '<div class="alert alert-danger bg-success py-3" id="flash-msg"><h4 align="center" class="m-0 font-weight-bold text-white"><i class="icon fa fa-check"></i> ' . $_GET['messageS'] . '</h4></div>';
}

echo '          <!-- Content Row -->' . "\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="fas fa-play-circle"' . "\r\n" . '                                    style="font-size:20px">&nbsp;&nbsp;</i>Intro Video</h4>' . "\r\n" . '                        ' . "\r\n\t\t\t\t\t\t" . '</center>' . "\r\n\n";
echo '            </div>' . "\n";
echo "\r\n" . '<!-- [ Main Content ] start -->' . "\r\n\r\n" . '                ' . "\t\t\t" . '<div class="card-body">' . "\r\n" . '                ' . "\t\t\t" . '    <center>' . "\r\n" . '        <form name="" id="onoff" method="post" enctype="multipart/form-data">' . "\r\n" . '                        <div class="form-group ">' . "\r\n" . '                            ' . "\t" . '<h6 align="center" class="m-0 font-weight-bold text-white">' . "\r\n" . '                             <strong> Active Intro Video ?</strong>' . "\r\n" . '                            </h6>' . "\r\n" . '<p>' . "\r\n" . '                        <label>Yes <input type="radio" onchange="formSubmit(this)" class="option--input" id="radio1" name="intro_on_off" value="yes"  ';

if ($intro_on_off == 'yes') {
	echo 'checked=\\"checked\\"';
}

echo '> </label> &nbsp&nbsp&nbsp' . "\r\n" . '            <label>No <input type="radio" onchange="formSubmit(this)" class="option--input" id="radio2" name="intro_on_off" value="no" ';

if ($intro_on_off == 'no') {
	echo 'checked=\\"checked\\"';
}

echo '></label>' . "\r\n" . '                     </form>' . "\r\n" . '                </div></center>' . "\r\n" . '                ' . "\r\n\t\t\t\t\t\t" . '<form method="post" enctype="multipart/form-data">' . "\r\n" . '                           ' . "\r\n" . '                                <div class="form-group ">' . "\r\n" . '                                   <label class="control-label " for="vpn_config">' . "\r\n" . '                                       <strong>Select Intro Video File Type</strong>' . "\r\n" . '                                  </label>' . "\r\n" . '                                 <div class="input-group">' . "\r\n\t\t\t\t\t\t\t\t\t" . '  <select class="form-control type" id="type" name="type">' . "\r\n\t\t\t\t\t\t\t\t\t\t" . '  <option data-value="op0" value="0" ';
echo $rowU['type'] == '0' ? 'selected' : '';
echo '>Upload MP4 File' . "\r\n\t\t\t\t\t\t\t\t\t\t" . '  </option>' . "\r\n\t\t\t\t\t\t\t\t\t\t" . '  <option data-value="op1" value="1" ';
echo $rowU['type'] == '1' ? 'selected' : '';
echo '>Enter MP4 External URL' . "\r\n\t\t\t\t\t\t\t\t\t\t" . '  </option>' . "\r\n\t\t\t\t\t\t\t\t\t" . '  </select>' . "\r\n" . '                                    </div>' . "\r\n" . '                                </div>' . "\r\n" . '                        ' . "\r\n" . '                    <div class="activeu">' . "\r\n" . '                     <div class="form-group ">' . "\r\n" . '                             <label class="control-label"><strong> Enter MP4 External URL </strong></label>' . "\r\n" . '                                <div class="input-group">' . "\r\n" . '                                 <input  class="form-control" type="text" name="url" value="';
echo $new_url;
echo '" placeholder="Enter URL file.mp4">' . "\r\n" . '                                </div>' . "\r\n" . '                             </div>' . "\r\n" . '                         </div>' . "\r\n\r\n" . '                      <div class="actived">' . "\r\n" . '                        <div class="form-group ">' . "\r\n" . '                            <label class="control-label"> <strong> Upload Intro Video File</strong></label>' . "\r\n\t\t\t\t\t\t" . '      <div class="input-group">' . "\r\n\t\t\t\t\t\t" . '      ' . "\t" . '<div class="custom-file">' . "\r\n\t\t\t\t\t" . '        ' . "\t" . '<input type="file" class="custom-file-input" name="image" id="intro" placeholder="Choose Intro" onchange="uploadintro(this)" aria-describedby="intro">' . "\r\n\t\t\t" . '<label class="custom-file-label" for="intro" placeholder="Choose Intro"><span id="image-intro"></span></label>' . "\r\n\t\t\t\t" . '          ' . "\t" . '   </div>' . "\r\n\t\t\t" . '                </div>' . "\r\n\t\t" . '        ' . "\t" . '     </div>' . "\r\n\t\t" . '    ' . "\t" . '       </div>' . "\r\n\t\t\t" . '        ' . "\r\n\t\t\t" . '         <div align="center" class="form-group">' . "\r\n\t\t\t\t\t" . '<br>' . "\r\n\t\t\t\t\t" . '<button class="btn btn-danger" name="submit"  type="submit"><span class="icon text-white-50"><i class="fa fa-save"></i>&nbsp;&nbsp;</span><span class="text">  Save Settings</span></button>' . "\r\n" . '                                </div>' . "\r\n\r\n" . '<br>' . "\r\n\t\t\t\t\t\t\t" . '</form>' . "\r\n\r\n" . '                <!-- Theme -->' . "\r\n" . '                    <div class="card-header bg-primary py-3  align-items-center">' . "\r\n" . '                    <center>' . "\r\n" . '                             <h6 class="m-0 font-weight-bold text-primary align-center"><i class="fa fa-video"></i> Current Intro <br><center></h6>' . "\r\n" . '                        </div>' . "\r\n" . '                    <div class="card-body  shadow py-2  align-items-center">' . "\r\n" . '                        <center>' . "\r\n" . '                                    <div class="h5 mb-0 font-weight-bold text-gray-800">' . "\r\n" . '                                        <video controls="" autoplay="" name="media" width="100%">' . "\r\n" . '                                            <source src="';

if ($intro_on_off == 'yes') {
	$intro = $new_url;
}
else {
	$intro = './intro/nointro.mp4';
}

echo $intro . '?' . time();
echo '" type="video/mp4"></video>' . "\r\n\r\n" . '                                </div>' . "\r\n" . '                    </div>' . "\r\n" . '                    ' . "\r\n" . '<!-- card body end -->' . "\r\n" . '              </div>' . "\r\n" . '<!-- Custom codes -->' . "\r\n" . '        </div>' . "\r\n" . '    </center>' . "\r\n" . '    </div>' . "\r\n" . '<!-- First Column -->' . "\r\n" . '<!-- Content Row -->' . "\r\n\r\n" . '<br><br>' . "\r\n\r\n\r\n" . '    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>' . "\r\n" . '    <script>' . "\r\n" . '//select activecode form' . "\r\n" . '//var response = {};' . "\r\n" . '//response.val = "op2";' . "\r\n" . '//$("#codemode option[data-value=\'" + response.val +"\']").attr("selected","selected");' . "\r\n\r\n" . '//hide activecode form' . "\r\n" . '$(\'.actived\').show(); ' . "\r\n\r\n" . '$(\'.activeu\').hide(); ' . "\r\n\r\n\r\n" . '//Show/hide activecode select' . "\r\n" . '$(document).ready(function(){' . "\r\n" . '  $(\'.type\').change(function(){' . "\r\n" . '    if($(\'.type\').val() > 0) {' . "\r\n" . '      $(\'.actived\').hide(); ' . "\r\n" . '      $(\'.activeu\').show(); ' . "\r\n" . '    } else {' . "\r\n" . '      $(\'.activeu\').hide(); ' . "\r\n" . '      $(\'.actived\').show(); ' . "\r\n" . '     document.getElementById("activeu").value = \' \';' . "\r\n" . '    } ' . "\r\n" . '  });' . "\r\n" . '  $(\'.type\').ready(function(){' . "\r\n" . '    if($(\'.type\').val() > 0) {' . "\r\n" . '      $(\'.actived\').hide(); ' . "\r\n" . '      $(\'.activeu\').show(); ' . "\r\n" . '    }else {' . "\r\n" . '      $(\'.activeu\').hide(); ' . "\r\n" . '      $(\'.actived\').show(); ' . "\r\n" . '     document.getElementById("actived").value = \' \';' . "\r\n" . '      ' . "\r\n" . '    } ' . "\r\n" . '  });' . "\r\n" . '});' . "\r\n" . '</script>' . "\r\n" . '<script>' . "\r\n" . 'function formSubmit(radioObj){' . "\r\n" . '  if(radioObj.checked){' . "\r\n" . '    document.getElementById("onoff").submit();' . "\r\n\r\n" . '  }' . "\r\n" . '}' . "\r\n" . '</script>' . "\r\n";
echo '<script>';
echo 'function uploadintro(target) {' . "\n";
echo "\t" . 'document.getElementById("image-intro").innerHTML = target.files[0].name;' . "\n";
echo '}' . "\n";
echo "\n";
echo '$(document).ready(function() {' . "\n";
echo '    if (location.hash) {' . "\n";
echo '        $("a[href=\'" + location.hash + "\']").tab("show");' . "\n";
echo '    }' . "\n";
echo '    $(document.body).on("click", "a[data-toggle=\'tab\']", function(event) {' . "\n";
echo '        location.hash = this.getAttribute("href");' . "\n";
echo '    });' . "\n";
echo '});' . "\n";
echo '$(window).on("popstate", function() {' . "\n";
echo '    var anchor = location.hash || $("a[data-toggle=\'tab\']").first().attr("href");' . "\n";
echo '    $("a[href=\'" + anchor + "\']").tab("show");' . "\n";
echo '});' . "\n";
echo "\n";
echo '</script>';
include 'includes/footer.php';

?>