<?php

session_start();
include 'assets/includes/db.php';
include 'assets/includes/config.php';

if ($_ERRORS) {
	error_reporting(E_ALL);
	ini_set('display_errors', 1);
}
if (!isset($_SESSION['loggedin']) && !$_SESSION['loggedin']) {
	header('location: logout.php');
}

if (isset($_POST['add_service'])) {
	$sql = 'INSERT INTO xc_domains_ottrun (';
	$sql .= 'name, ';
	$sql .= 'ssl, ';
	$sql .= 'dns, ';
	$sql .= 'port) ';
	$sql .= 'VALUES (';
	$sql .= '\'' . $_POST['add-name'] . '\', ';
	$sql .= '\'' . $_POST['add-ssl'] . '\', ';
	$sql .= '\'' . $_POST['add-domain'] . '\',';
	$sql .= '\'' . $_POST['add-port'] . '\');';
	$sqlite3->exec($sql);
	header('Location: ottrunvpn_domains.php');
}

if (isset($_POST['edit_service'])) {
	$sql = 'UPDATE xc_domains_ottrun SET ';
	$sql .= 'name = \'' . $_POST['edit-name'] . '\', ';
	$sql .= 'ssl = \'' . $_POST['edit-ssl'] . '\', ';
	$sql .= 'dns = \'' . $_POST['edit-domain'] . '\',';
	$sql .= 'port = \'' . $_POST['edit-port'] . '\' ';
	$sql .= 'WHERE id = ' . $_POST['edit-id'] . ';';
	$sqlite3->exec($sql);
	header('Location: ottrunvpn_domains.php');
}

if (isset($_POST['delete_service'])) {
	$sql = 'DELETE FROM xc_domains_ottrun ';
	$sql .= 'WHERE id = ' . $_POST['delete-id'] . ';';
	$sqlite3->exec($sql);
	header('Location: ottrunvpn_domains.php');
}

echo '<!doctype html>' . "\r\n" . '<html lang="en">' . "\r\n\r\n" . '<head>' . "\r\n\r\n" . '    ';
include 'assets/includes/title-meta.php';
echo "\r\n" . '    ';
include 'assets/includes/head-css.php';
echo "\r\n" . '</head>' . "\r\n\r\n" . '<body data-sidebar="dark">' . "\r\n\r\n" . '    <!-- Loader -->' . "\r\n" . '    <div id="preloader">' . "\r\n" . '        <div id="status">' . "\r\n" . '            <div class="spinner">' . "\r\n" . '                <i class="ri-loader-line spin-icon"></i>' . "\r\n" . '            </div>' . "\r\n" . '        </div>' . "\r\n" . '    </div>' . "\r\n\r\n" . '    <div id="layout-wrapper">' . "\r\n\r\n" . '        ';
include 'assets/includes/topbar.php';
echo "\r\n" . '        ';
include 'assets/includes/sidebar.php';
echo "\r\n" . '        <div class="main-content">' . "\r\n\r\n" . '            <div class="page-content">' . "\r\n" . '                <div class="container-fluid">' . "\r\n\r\n" . '                    <div class="row">' . "\r\n" . '                        <div class="col-12">' . "\r\n" . '                            <div class="page-title-box d-flex align-items-center justify-content-between">' . "\r\n" . '                                <h4 class="mb-0"> </h4>' . "\r\n\r\n" . '                                <div class="page-title-right">' . "\r\n" . '                                    <ol class="breadcrumb m-0">' . "\r\n" . '                                        <li class="breadcrumb-item"><a href="javascript: void(0);">Applications</a></li>' . "\r\n" . '                                        <li class="breadcrumb-item"><a href="javascript: void(0);">OTTRun VPN</a></li>' . "\r\n" . '                                        <li class="breadcrumb-item active">Domain List</li>' . "\r\n" . '                                    </ol>' . "\r\n" . '                                </div>' . "\r\n\r\n" . '                            </div>' . "\r\n" . '                        </div>' . "\r\n" . '                    </div>' . "\r\n\r\n" . '                    <div class="row">' . "\r\n" . '                        <div class="col-12">' . "\r\n" . '                            <div class="card">' . "\r\n" . '                                <div class="card-body">' . "\r\n\r\n" . '                                    <h4 class="card-title">Active Domain List</h4>' . "\r\n" . '                                    <p class="card-title-desc">' . "\r\n" . '                                        <a data-toggle="modal" data-target="#add-modal" class="add-button btn-sm btn-secondary float-right"><i class="dripicons-document-new"></i> Add New Service</a>' . "\r\n" . '                                    </p>' . "\r\n\r\n" . '                                    <br />' . "\r\n\r\n" . '                                    <table id="datatable-buttons" class="table dt-responsive table-striped table-bordered nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">' . "\r\n" . '                                        <thead>' . "\r\n" . '                                            <tr>' . "\r\n" . '                                                <th>ID</th>' . "\r\n" . '                                                <th>Service Name</th>' . "\r\n" . '                                                <th>SSL</th>' . "\r\n" . '                                                <th>Domain</th>' . "\r\n" . '                                                <th>Port</th>' . "\r\n" . '                                                <th></th>' . "\r\n" . '                                            </tr>' . "\r\n" . '                                        </thead>' . "\r\n" . '                                        ';

while ($row = $service_ottrun_data->fetchArray()) {
	echo '                                            <tr>' . "\r\n" . '                                                <td>';
	echo $row['id'];
	echo '</td>' . "\r\n" . '                                                <td>';
	echo $row['name'];
	echo '</td>' . "\r\n" . '                                                <td>';
	echo $row['ssl'];
	echo '</td>' . "\r\n" . '                                                <td>';
	echo $row['dns'];
	echo '</td>' . "\r\n" . '                                                <td>';
	echo $row['port'];
	echo '</td>' . "\r\n" . '                                                <td>' . "\r\n" . '                                                    <button type="button" data-toggle="modal" data-target="#edit-modal" class="edit-button btn-sm btn-primary waves-effect waves-light"><i class="dripicons-document-edit"></i></button>' . "\r\n" . '                                                    <button type="button" data-toggle="modal" data-target="#delete-modal" class="delete-button btn-sm btn-danger waves-effect waves-light"><i class="dripicons-document-delete"></i></button>' . "\r\n" . '                                                </td>' . "\r\n" . '                                            </tr>' . "\r\n" . '                                        ';
}

echo '                                    </table>' . "\r\n\r\n" . '                                    <div class="modal fade" id="add-modal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">' . "\r\n" . '                                        <div class="modal-dialog modal-dialog-centered" role="document">' . "\r\n" . '                                            <div class="modal-content">' . "\r\n" . '                                                <div class="modal-header border-bottom-0">' . "\r\n" . '                                                    <h5 class="modal-title" id="addModalLabel">Add Service</h5>' . "\r\n" . '                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">' . "\r\n" . '                                                        <span class="text-danger" aria-hidden="true">&times;</span>' . "\r\n" . '                                                    </button>' . "\r\n" . '                                                </div>' . "\r\n" . '                                                <form method="POST">' . "\r\n" . '                                                    <div class="modal-body">' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="add-name">Service Name</label>' . "\r\n" . '                                                            <input type="text" class="form-control" id="add-name" name="add-name" placeholder="Enter service name">' . "\r\n" . '                                                            <small id="add-name-small" class="form-text text-muted"><em>Name for' . "\r\n" . '                                                                    service.</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="add-domain">Domain</label>' . "\r\n" . '                                                            <input type="text" class="form-control" id="add-domain" name="add-domain" placeholder="Enter service domain">' . "\r\n" . '                                                            <small id="add-domain-small" class="form-text text-muted"><em>Domain for service. (no' . "\r\n" . '                                                                    http/https)</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="add-port">Port</label>' . "\r\n" . '                                                            <input type="text" class="form-control" id="add-port" name="add-port" placeholder="Enter service port">' . "\r\n" . '                                                            <small id="add-port-small" class="form-text text-muted"><em>Port for' . "\r\n" . '                                                                    service.</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="add-ssl">SSL</label>' . "\r\n" . '                                                            <select class="custom-select" id="add-ssl" name="add-ssl">' . "\r\n" . '                                                                <option selected="false">Choose an option</option>' . "\r\n" . '                                                                <option value="false">false</option>' . "\r\n" . '                                                                <option value="true">true</option>' . "\r\n" . '                                                            </select>' . "\r\n" . '                                                            <small id="add-ssl-small" class="form-text text-muted"><em>Is service using' . "\r\n" . '                                                                    SSL.</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                    </div>' . "\r\n" . '                                                    <div class="modal-footer text-center mt-4">' . "\r\n" . '                                                        <button type="submit" name="add_service" class="btn-sm btn-primary">Submit</button>' . "\r\n" . '                                                    </div>' . "\r\n" . '                                                </form>' . "\r\n" . '                                            </div>' . "\r\n" . '                                        </div>' . "\r\n" . '                                    </div>' . "\r\n\r\n" . '                                    <div class="modal fade" id="edit-modal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">' . "\r\n" . '                                        <div class="modal-dialog modal-dialog-centered" role="document">' . "\r\n" . '                                            <div class="modal-content">' . "\r\n" . '                                                <div class="modal-header border-bottom-0">' . "\r\n" . '                                                    <h5 class="modal-title" id="editModalLabel">Edit Service</h5>' . "\r\n" . '                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">' . "\r\n" . '                                                        <span class="text-danger" aria-hidden="true">&times;</span>' . "\r\n" . '                                                    </button>' . "\r\n" . '                                                </div>' . "\r\n" . '                                                <form method="POST">' . "\r\n" . '                                                    <div class="modal-body">' . "\r\n" . '                                                        <div class="input-group row" hidden>' . "\r\n" . '                                                            <input type="text" class="form-control" id="edit-id" name="edit-id">' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="edit-name">Service Name</label>' . "\r\n" . '                                                            <input type="text" class="form-control" id="edit-name" name="edit-name" placeholder="Enter service name">' . "\r\n" . '                                                            <small id="edit-name-small" class="form-text text-muted"><em>Name for' . "\r\n" . '                                                                    service.</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="edit-domain">Domain</label>' . "\r\n" . '                                                            <input type="text" class="form-control" id="edit-domain" name="edit-domain" placeholder="Enter service domain">' . "\r\n" . '                                                            <small id="edit-domain-small" class="form-text text-muted"><em>Domain for service. (no' . "\r\n" . '                                                                    http/https)</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="edit-port">Port</label>' . "\r\n" . '                                                            <input type="text" class="form-control" id="edit-port" name="edit-port" placeholder="Enter service port">' . "\r\n" . '                                                            <small id="edit-port-small" class="form-text text-muted"><em>Port for' . "\r\n" . '                                                                    service.</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <div class="form-group">' . "\r\n" . '                                                            <label for="edit-ssl">Status</label>' . "\r\n" . '                                                            <select class="custom-select" id="edit-ssl" name="edit-ssl">' . "\r\n" . '                                                                <option selected="false">false</option>' . "\r\n" . '                                                                <option value="true">true</option>' . "\r\n" . '                                                            </select>' . "\r\n" . '                                                            <small id="edit-ssl-small" class="form-text text-muted"><em>Is service using' . "\r\n" . '                                                                    SSL.</em></small>' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                    </div>' . "\r\n" . '                                                    <div class="modal-footer text-center mt-4">' . "\r\n" . '                                                        <button type="submit" name="edit_service" class="btn-sm btn-primary">Save</button>' . "\r\n" . '                                                    </div>' . "\r\n" . '                                                </form>' . "\r\n" . '                                            </div>' . "\r\n" . '                                        </div>' . "\r\n" . '                                    </div>' . "\r\n\r\n" . '                                    <div class="modal fade" id="delete-modal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">' . "\r\n" . '                                        <div class="modal-dialog modal-dialog-centered" role="document">' . "\r\n" . '                                            <div class="modal-content">' . "\r\n" . '                                                <div class="modal-header border-bottom-0">' . "\r\n" . '                                                    <h5 class="modal-title" id="deleteModalLabel">Delete Service</h5>' . "\r\n" . '                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">' . "\r\n" . '                                                        <span class="text-danger" aria-hidden="true">&times;</span>' . "\r\n" . '                                                    </button>' . "\r\n" . '                                                </div>' . "\r\n" . '                                                <form method="POST">' . "\r\n" . '                                                    <div class="modal-body">' . "\r\n" . '                                                        <div class="form-group" hidden>' . "\r\n" . '                                                            <input type="text" class="form-control" id="delete-id" name="delete-id">' . "\r\n" . '                                                        </div>' . "\r\n" . '                                                        <h3 class="text-center mt-4">Are you sure?</h3>' . "\r\n" . '                                                    </div>' . "\r\n" . '                                                    <div class="modal-footer text-center mt-4">' . "\r\n" . '                                                        <button type="submit" name="delete_service" class="btn-sm btn-danger">Delete</button>' . "\r\n" . '                                                    </div>' . "\r\n" . '                                                </form>' . "\r\n" . '                                            </div>' . "\r\n" . '                                        </div>' . "\r\n" . '                                    </div>' . "\r\n\r\n" . '                                    <script>' . "\r\n" . '                                        $(function() {' . "\r\n" . '                                            $(".edit-button").on(\'click\', function() {' . "\r\n" . '                                                var currentRow = $(this).closest("tr");' . "\r\n" . '                                                var id = currentRow.find("td:eq(0)").text();' . "\r\n" . '                                                var name = currentRow.find("td:eq(1)").text();' . "\r\n" . '                                                var ssl = currentRow.find("td:eq(2)").text();' . "\r\n" . '                                                var dns = currentRow.find("td:eq(3)").text();' . "\r\n" . '                                                var port = currentRow.find("td:eq(4)").text();' . "\r\n" . '                                                $("#edit-id").val(id);' . "\r\n" . '                                                $("#edit-name").val(name);' . "\r\n" . '                                                $("#edit-ssl").val(ssl);' . "\r\n" . '                                                $("#edit-domain").val(dns);' . "\r\n" . '                                                $("#edit-port").val(port);' . "\r\n" . '                                            });' . "\r\n" . '                                            $(".delete-button").on(\'click\', function() {' . "\r\n" . '                                                var currentRow = $(this).closest("tr");' . "\r\n" . '                                                var id = currentRow.find("td:eq(0)").text();' . "\r\n" . '                                                $("#delete-id").val(id);' . "\r\n" . '                                            });' . "\r\n" . '                                        });' . "\r\n" . '                                    </script>' . "\r\n\r\n" . '                                </div>' . "\r\n" . '                            </div>' . "\r\n" . '                        </div>' . "\r\n" . '                    </div>' . "\r\n\r\n" . '                </div>' . "\r\n" . '            </div>' . "\r\n\r\n" . '            ';
include 'assets/includes/footer.php';
echo '        </div>' . "\r\n\r\n" . '    </div>' . "\r\n\r\n" . '    ';
include 'assets/includes/right-sidebar.php';
echo "\r\n" . '    ';
include 'assets/includes/vendor-scripts.php';
echo "\r\n" . '    <script src="./assets/js/app.js"></script>' . "\r\n\r\n" . '</body>' . "\r\n\r\n" . '</html>';

?>