<?php 
include ('includes/header.php');

//table name
$table_name = "vpn";

//create if not
$db->exec("CREATE TABLE IF NOT EXISTS {$table_name}(id INTEGER PRIMARY KEY	AUTOINCREMENT  NOT NULL, vpn_country TEXT ,vpn_config TEXT,vpn_file_name TEXT)");

//table call
$res = $db->query("SELECT * FROM {$table_name}");

//submit new
if (!file_exists('vpn')) {
	mkdir('vpn', 511, true);
}

if (isset($_POST['submit'])) {
	$target_dir = 'vpn/';
	$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
	$uploadOk = 1;
	$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
	$file_path = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/vpn/';

	if (file_exists($target_file)) {
		unlink($target_file);
	}

	$msgg = '<div class="alert alert-primary" id="flash-msg"><h4><i class="icon fa fa-check"></i>The file already exists and has been replaced.</h4></div>';
	$uploadOk = 1;

	if (50000 < $_FILES['fileToUpload']['size']) {
		echo '<div class="alert alert-primary" id="flash-msg"><h4><i class="icon fa fa-times"></i>Sorry, your file is too large.</h4></div>';
		$uploadOk = 0;
	}

	if ($imageFileType != 'ovpn') {
		echo '<div class="alert alert-primary" id="flash-msg"><h4><i class="icon fa fa-times"></i>Sorry, only OVPN files are allowed.</h4></div>';
		$uploadOk = 0;
	}

	if ($uploadOk == 0) {
	}
	else if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
		$config = '' . $file_path . htmlspecialchars(basename($_FILES['fileToUpload']['name'])) . '';
		$file_name = htmlspecialchars(basename($_FILES['fileToUpload']['name']));
		$db->exec("INSERT INTO {$table_name}(vpn_country, vpn_config, vpn_file_name) VALUES('".sanitize($_POST['vpn_country'])."', '{$config}', '{$file_name}')");
		$db->close();
		header("Location: {$base_file}");
	}
}

//delete row
if (isset($_GET['delete'])) {
	$arr = $db->query("SELECT * FROM {$table_name} WHERE id=".sanitize($_GET['delete']));
	$del = $arr->fetchArray();
	$ftd = $del['vpn_file_name'];
	$ftd = 'vpn/' . $ftd;
	unlink($ftd);
	$db->exec("DELETE FROM {$table_name} WHERE id=".sanitize($_GET['delete']));
	$db->close();
	header("Location: {$base_file}?status=2");
}

?>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content" style="background-color: black;">
			<div class="modal-header">
				<h2 style="color: white;">Confirm</h2>
			</div>
			<div class="modal-body" style="color: white;">
				Do you really want to delete?
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
				<a style="color: white;" class="btn btn-danger btn-ok">Delete</a>
			</div>
		</div>
	</div>
</div>
<?php
if (isset($_GET['create'])){

//create form
?>


        <div class="col-md-6 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-bullhorn"></i> OVPN</h2>
                        </center>
                    </div>
                    
                    <div class="card-body">
                        <div class="col-12">
                            <h3>Upload OVPN</h3>
                        </div>
                            <form action="" method="post" enctype="multipart/form-data">
				                <div class="form-group">
				                	<div class="form-group form-float form-group-lg">
                                        <div class="form-line">
                                            <label class="form-label"><strong>Location</strong></label>
				                			<input type="text" class="form-control" name="vpn_country" placeholder="Country / State">
				                		</div>
				                	</div>
				                </div>
                                <div class="form-group ">
                                    <label class="control-label " for="vpn_config">
                                        <strong>OVPN File</strong>
                                    </label>
                                    <div class="input-group">
                                        <input type="file" name="fileToUpload" id="fileToUpload" >
                                    </div>
                                </div>
                                <div class="form-group">
                                    <center>
                                        <button class="btn btn-info " name="submit" type="submit">
                                            <i class="icon icon-check"></i> Submit
                                        </button>
                                    </center>
                                </div>
                            </form>
                    </div>
                </div>
            </div>
        </div>

<?php
 }else{
//main table/form
	 ?>

        <div class="col-md-12 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-commenting"></i> OVPN's</h2>
                        </center>
                    </div>

                    <div class="card-body">
                        <div class="col-12">
                        	<center>
	        					<a id="button" href="./<?php echo $base_file ?>?create" class="btn btn-info">New OVPN</a>
	        				</center>
    					</div>
						<br>
						<div class="table-responsive">
							<table class="table table-striped table-sm">
							<thead style="color:white!important">
								<tr>
								<th>Index</th>
								<th>Location</th>
								<th>File Name</th>
								<th>Delete</th>
								</tr>
							</thead>
							<?php while ($row = $res->fetchArray()) {?>
							<tbody>
								<tr>
								<td><?=$row['id'] ?></td>
								<td><?=$row['vpn_country'] ?></a></td>
								<td><?=$row['vpn_file_name'] ?></td>
								<td>
								<a class="btn btn-danger btn-ok" href="#" data-href="./<?=$base_file ?>?delete=<?=$row['id'] ?>" data-toggle="modal" data-target="#confirm-delete"><i class="fa fa-trash-o"></i></a>
								</td>
								</tr>
							</tbody>
							<?php }?>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
<?php }?>

<?php include ('includes/footer.php');?>

</body>
</html>