<?php
/**
*
* @ This file is created by http://DeZender.Net
* @ deZender (PHP7 Decoder for SourceGuardian Encoder)
*
* @ Version			:	5.0.1.0
* @ Author			:	DeZender
* @ Release on		:	22.04.2022
* @ Official site	:	http://DeZender.Net
*
*/


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/brbox_imgbg.db');
$table_name = 'brboxbg';
$base_file = basename($_SERVER['SCRIPT_NAME']);
$brboxs_file = 'gifbg.gif';
$target_dir = 'api/img/';
$file_path = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/' . $target_dir;

if (!file_exists($target_dir)) {
	mkdir($target_dir, 511, true);
}

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

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

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

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

	$db->exec('UPDATE ' . $table_name . ' SET on_off=\'' . $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);
	exit();
}

if (isset($_POST['submit'])) {
	if ($_POST['type'] == '0') {
		$target_file = $target_dir . $brboxs_file;
		$uploadOk = 1;
		$file_ext = strtolower(end(explode('.', $_FILES['image']['name'])));
		$extensions = ['gif', 'GIF', 'png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'bmp', 'BMP'];

		if (file_exists($target_file)) {
			unlink($target_file);
			$msg = '?messageA=The file already exists and has been replaced.';
		}
		else {
			$msg = '?messageS=Success, Image 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 JPEG or PNG or GIF files are allowed.';
			$uploadOk = 0;
		}

		if ($uploadOk == 0) {
			header('Location: ' . $base_file . $msg);
		}
		else if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
			$brbox_patch = '' . $file_path . $brboxs_file . '';
			$db->exec('UPDATE ' . $table_name . ' SET url=\'' . $brbox_patch . '\',' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'on_off=\'' . $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);
			exit();
		}
	}
	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'];

		if (is_int(in_array($file_ext, $extensions))) {
			$msg = '?messageE=Sorry, only JPG or PNG or GIF files are allowed.';
			header('Location: ' . $base_file . $msg);
			exit();
		}
		else {
			$msg = '?messageS=Saved!, Url of saved Image file..';
			$brbox_patch = $url;
			$db->exec('UPDATE ' . $table_name . ' SET url=\'' . $brbox_patch . '\',' . "\r\n\t\t\t\t\t\t\t\t\t\t" . 'on_off=\'' . $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);
			exit();
		}
	}
}

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-image"' . "\r\n" . '                                    style="font-size:20px">&nbsp;&nbsp;</i>Background Image</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 Background Image ?</strong>' . "\r\n" . '                            </h6>' . "\r\n" . '<p>' . "\r\n" . '                        <label>Yes <input type="radio" onchange="formSubmit(this)" class="option--input" id="radio1" name="on_off" value="yes"  ';

if ($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="on_off" value="no" ';

if ($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 Background Image 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 Image 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 Image 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 Image 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.gif or file.png or file.jpg...">' . "\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 Background Image 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 Background Image"><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 border-left-primary shadow py-2  align-items-center">' . "\r\n" . '                    <center>' . "\r\n" . '                             <h6 class="m-0 font-weight-bold text-primary align-center"><i class="fa fa-image"></i> Current Background <br><center></h6>' . "\r\n" . '                        </div>' . "\r\n" . '                    <div class="card border-left-primary shadow py-2  align-items-center">' . "\r\n" . '                        <center>' . "\r\n" . '                                   <div class="card-body align-items-center">' . "\r\n" . '                                    <div class="h5 mb-0 font-weight-bold text-gray-800">' . "\r\n" . '                                        <img class="card-img-bottom" src="';

if ($on_off == 'yes') {
	$intro = $new_url;
}
else {
	$intro = './api/img/nobg.gif';
}

echo $intro . '?' . time();
echo '" alt=" No image to show" style="width:100%; height:100%">' . "\r\n" . '                                    </div>' . "\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';

?>