<?php
/**
 *  Copyright (C) 2021  Double Bastion LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

define('RESTRICTED', TRUE);

include 'install-signup-check.php';

if ($installcheck == true) {

 session_start();

 if ($_SESSION['validateaccess'] == "accessallowed") {

   if(isset($_POST['submitadminbttn'])) {

     if (isset($_POST['adminuser']) && $_POST['adminuser'] != '' && isset($_POST['adminuserpassword']) &&
         $_POST['adminuserpassword'] != '' && isset($_POST['adminuseremail']) && $_POST['adminuseremail'] != '') {

         $adminuser = $_POST['adminuser'];
         $adminuserpass = password_hash($_POST['adminuserpassword'], PASSWORD_DEFAULT);
         $adminuseremail = $_POST['adminuseremail'];

         define('ACCESSCONST', TRUE);

         require('db-connect.php');

         // Create table for Roundpin users
         $query1 = " CREATE TABLE IF NOT EXISTS app_users (
		        id                      INT UNSIGNED AUTO_INCREMENT  PRIMARY KEY,
                        userrole                VARCHAR (150) DEFAULT NULL,
		        username                VARCHAR (150) DEFAULT NULL,
		        password                VARCHAR (250) DEFAULT NULL,
		        emailaddress            VARCHAR (150) DEFAULT NULL,
		        registered              SMALLINT      DEFAULT NULL,
		        token                   VARCHAR (150) DEFAULT NULL,
		        temporarypass           VARCHAR (50)  DEFAULT NULL,
		        wss_server              VARCHAR (150) DEFAULT NULL,
		        web_socket_port         INT           DEFAULT NULL,
		        server_path             VARCHAR (10)  DEFAULT NULL,
		        profile_name            VARCHAR (100) DEFAULT NULL,
		        sip_username            VARCHAR (100) DEFAULT NULL,
		        sip_password            VARCHAR (250) DEFAULT NULL,
		        stun_server             VARCHAR (300) DEFAULT NULL,
		        audio_output_id         VARCHAR (100) DEFAULT NULL,
		        video_src_id            VARCHAR (500) DEFAULT NULL,
		        video_height            VARCHAR (10)  DEFAULT NULL,
		        frame_rate              SMALLINT      DEFAULT NULL,
		        aspect_ratio            VARCHAR (10)  DEFAULT NULL,
		        video_orientation       VARCHAR (100) DEFAULT NULL,
		        audio_src_id            VARCHAR (500) DEFAULT NULL,
		        auto_gain_control       VARCHAR (10)  DEFAULT NULL,
		        echo_cancellation       VARCHAR (10)  DEFAULT NULL,
		        noise_suppression       VARCHAR (10)  DEFAULT NULL,
		        ring_output_id          VARCHAR (100) DEFAULT NULL,
                        video_conf_extension    VARCHAR (100) DEFAULT NULL,
                        video_conf_window_width VARCHAR (10)  DEFAULT NULL,
		        profile_picture         LONGTEXT      DEFAULT NULL,
		        notifications           SMALLINT      DEFAULT NULL,
                        use_roundcube           SMALLINT      DEFAULT NULL,
                        rcdomain                VARCHAR (300) DEFAULT NULL,
                        rcbasicauthuser         VARCHAR (300) DEFAULT NULL,
                        rcbasicauthpass         VARCHAR (300) DEFAULT NULL,
                        rcuser                  VARCHAR (300) DEFAULT NULL,
                        rcpassword              VARCHAR (300) DEFAULT NULL,
                        enabled                 SMALLINT      DEFAULT NULL
		     ); ";

         $result1 = $mysqli->query($query1);

         $userrole = 'superadmin';
         $registeredcheck = 1;
         $enabled = 1;

         $query2 = $mysqli->prepare("INSERT INTO app_users (userrole, username, password, emailaddress, registered, enabled) VALUES (?, ?, ?, ?, ?, ?);");
         $query2->bind_param("ssssii", $userrole, $adminuser, $adminuserpass, $adminuseremail, $registeredcheck, $enabled);
         $query2->execute();

         // Create table for contacts of Roundpin users
         $query3 = " CREATE TABLE IF NOT EXISTS contacts (
		          id                INT UNSIGNED AUTO_INCREMENT  PRIMARY KEY,
		          user_id           INT UNSIGNED      NOT NULL,
		          contact_name      VARCHAR (300)     DEFAULT NULL,
		          contact_desc      VARCHAR (300)     DEFAULT NULL,
		          extension_number  VARCHAR (50)      DEFAULT NULL,
		          contact_mobile    VARCHAR (50)      DEFAULT NULL,
		          contact_num1      VARCHAR (50)      DEFAULT NULL,
		          contact_num2      VARCHAR (50)      DEFAULT NULL,
		          contact_fax       VARCHAR (50)      DEFAULT NULL,
		          contact_email     VARCHAR (300)     DEFAULT NULL,
                          profile_picture_c LONGTEXT          DEFAULT NULL,
                          groups            VARCHAR (1500)    DEFAULT NULL,
                          date_added        DATETIME          DEFAULT NULL,
                          date_modified     DATETIME          DEFAULT NULL,
                          CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
                     ); ";

         $result3 = $mysqli->query($query3);

         // Create table for external users of video conference
         $query4 = " CREATE TABLE IF NOT EXISTS external_users (
		          id                 INT UNSIGNED  AUTO_INCREMENT  PRIMARY KEY,
		          userid             INT UNSIGNED      NOT NULL,
		          exten_for_external VARCHAR (150)     DEFAULT NULL,
		          exten_for_ext_pass VARCHAR (2000)    DEFAULT NULL,
		          conf_access_link   LONGTEXT          DEFAULT NULL,
		          CONSTRAINT `fk_userid` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
                     ); ";
         $result4 = $mysqli->query($query4);

         $_SESSION['validateaccess'] = "noaccessallowed";

         $_SESSION['instcompleteaccess'] = "instaccess";

         header("Location: roundpin-inst-complete.php");

     }
  }

?>

<html>

<head>
  <title>Roundpin Setup</title>

  <link rel="stylesheet" href="css/setup.min.css">
  <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
  <script type="text/javascript" src="js/roundpin-setup.min.js"></script>

</head>

<body>

  <div id="mainarea">

    <img class="logosetuppage" src="images/login-logo.svg" />

    <h3 style="text-align:center;">Roundpin Setup</h3><br>

    <img class="setupStage" src="images/roundpin-setup-stages-2.svg" />

    <form id="admincredform" action="" method="POST">

       <label for="adminuser" class="fieldlabels">Superadmin username:</label><br>
       <input type="text" id="adminuser" class="textinput" name="adminuser"/><br><br>

       <label for="adminuserpassword" class="fieldlabels">Superadmin password:</label><br>
       <input type="password" id="adminuserpassword" class="textinput" name="adminuserpassword" title="The password should be
       at least 10 characters long, it should contain at least one letter, at least one digit and at least one special character"/><br><br>

       <label for="adminuseremail" class="fieldlabels">Superadmin email address:</label><br>
       <input type="text" id="adminuseremail" class="textinput" name="adminuseremail"/><br><br>

       <input type="submit" id="submitadminbttn" name="submitadminbttn" value="Submit" />

   </form>

  </div>

</body>

</html>

<?php
 }
}

?>
