<?php
/**
 *  Copyright (C) 2022, 2024  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.
 */

session_start();

define('RESTRICTED', TRUE);

include 'install-signup-check.php';

if ($installcheck == true) {

 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,
                        temp_key                VARCHAR (150)   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 (150)   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,
		        ringtone                VARCHAR (40)    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_window_width VARCHAR (10)    DEFAULT NULL,
		        profile_picture         LONGTEXT        DEFAULT NULL,
                        language                VARCHAR (10)    DEFAULT NULL,
		        notifications           SMALLINT        DEFAULT NULL,
                        show_vconf_usernames    SMALLINT        DEFAULT NULL,
                        display_vconf_user      SMALLINT        DEFAULT NULL,
                        use_sms                 SMALLINT        DEFAULT NULL,
                        use_fax                 SMALLINT        DEFAULT NULL,
                        use_email               SMALLINT        DEFAULT NULL,
                        user_groups             VARCHAR (900)   DEFAULT NULL,
                        load_groups             VARCHAR (900)   DEFAULT NULL,
                        date_added              DATETIME        DEFAULT NULL,
                        date_modified           DATETIME        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,
		          contact_long_desc VARCHAR (500)   DEFAULT NULL,
                          address_street    VARCHAR (180)   DEFAULT NULL,
                          address_zip       VARCHAR (30)    DEFAULT NULL,
                          address_town      VARCHAR (50)    DEFAULT NULL,
                          address_country   VARCHAR (50)    DEFAULT NULL,
                          address_state     VARCHAR (50)    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 (900)   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,
		          profile_name         VARCHAR (150)   DEFAULT NULL,
		          exten_for_external   VARCHAR (50)    DEFAULT NULL,
		          exten_for_ext_pass   VARCHAR (500)   DEFAULT NULL,
		          conf_extension       VARCHAR (50)    DEFAULT NULL,
		          conf_tag             VARCHAR (150)   DEFAULT NULL,
		          conf_access_link     LONGTEXT        DEFAULT NULL,
                          show_vconf_usernames SMALLINT        DEFAULT NULL,
                          display_vconf_user   SMALLINT        DEFAULT NULL,
                          date_added           DATETIME        DEFAULT NULL,
                          date_modified        DATETIME        DEFAULT NULL,
		          CONSTRAINT `fk_userid` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
                     ); ";
         $result4 = $mysqli->query($query4);

         // Create table for external users of video conference
         $query5 = " CREATE TABLE IF NOT EXISTS groups (
		          id                   INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid               INT UNSIGNED    NOT NULL,
		          group_name           VARCHAR (150)   DEFAULT NULL,
		          CONSTRAINT `fkuserid` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON UPDATE RESTRICT
                     ); ";
         $result5 = $mysqli->query($query5);

         $superuid = 1;
         $admingroup = 'Admin';
         $generalgroup = 'General';
         $query6 = $mysqli->prepare("INSERT INTO groups (userid, group_name) VALUES (?, ?);");
         $query6->bind_param("is", $superuid, $admingroup);
         $query6->execute();

         $query7 = $mysqli->prepare("INSERT INTO groups (userid, group_name) VALUES (?, ?);");
         $query7->bind_param("is", $superuid, $generalgroup);
         $query7->execute();

         // Create table for text conferences
         $query8 = " CREATE TABLE IF NOT EXISTS conferences_text (
		          id                    INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid                INT UNSIGNED    NOT NULL,
                          text_conf_extension   VARCHAR (100)   DEFAULT NULL,
		          text_conf_label       VARCHAR (150)   DEFAULT NULL,
                          limit_to_groups       VARCHAR (1000)  DEFAULT NULL,
		          CONSTRAINT `fkuseridtc` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON UPDATE RESTRICT
                     ); ";
         $result8 = $mysqli->query($query8);

         $query9 = " CREATE TABLE IF NOT EXISTS conferences_audio (
		          id                    INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid                INT UNSIGNED    NOT NULL,
                          audio_conf_extension  VARCHAR (100)   DEFAULT NULL,
		          audio_conf_label      VARCHAR (150)   DEFAULT NULL,
                          limit_to_groups       VARCHAR (1000)  DEFAULT NULL,
		          CONSTRAINT `fkuseridac` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON UPDATE RESTRICT
                     ); ";
         $result9 = $mysqli->query($query9);

         $query10 = " CREATE TABLE IF NOT EXISTS conferences_video (
		          id                    INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid                INT UNSIGNED    NOT NULL,
                          video_conf_extension  VARCHAR (100)   DEFAULT NULL,
		          video_conf_label      VARCHAR (150)   DEFAULT NULL,
                          limit_to_groups       VARCHAR (1000)  DEFAULT NULL,
		          CONSTRAINT `fkuseridvc` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON UPDATE RESTRICT
                      ); ";
         $result10 = $mysqli->query($query10);

         $query11 = " CREATE TABLE IF NOT EXISTS email_conf (
		          id                      INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid                  INT UNSIGNED    NOT NULL,
                          rcdomain                VARCHAR (300)   DEFAULT NULL,
                          rcuser                  VARCHAR (300)   DEFAULT NULL,
                          rcpassword              VARCHAR (300)   DEFAULT NULL,
                          rcbasicauthuser         VARCHAR (300)   DEFAULT NULL,
                          rcbasicauthpass         VARCHAR (300)   DEFAULT NULL,
		          CONSTRAINT `fkuserideml` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
                      ); ";
         $result11 = $mysqli->query($query11);


         $query12 = " CREATE TABLE IF NOT EXISTS banned_users (
		          id                      INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          ban_creator             VARCHAR (150)   DEFAULT NULL,
		          banned_profilename      VARCHAR (150)   DEFAULT NULL,
		          banned_sipusername      VARCHAR (100)   DEFAULT NULL,
                          conf_extension          VARCHAR (100)   DEFAULT NULL,
		          conf_label              VARCHAR (150)   DEFAULT NULL,
                          banned_until            DATETIME        DEFAULT NULL
                      ); ";
         $result12 = $mysqli->query($query12);

         // Create table for voice/sms/fax numbers
         $query13 = " CREATE TABLE IF NOT EXISTS phone_numbers (
		          id                    INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid                INT UNSIGNED    NOT NULL,
                          voice_numbers         VARCHAR (200)   DEFAULT NULL,
                          default_voice_number  VARCHAR (25)    DEFAULT NULL,
                          sms_numbers           VARCHAR (200)   DEFAULT NULL,
                          default_sms_number    VARCHAR (25)    DEFAULT NULL,
                          fax_numbers           VARCHAR (200)   DEFAULT NULL,
                          default_fax_number    VARCHAR (25)    DEFAULT NULL,
		          CONSTRAINT `fkuseridphnb` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
                     ); ";
         $result13 = $mysqli->query($query13);

         // Create table for external users of video conference
         $query14 = " CREATE TABLE IF NOT EXISTS fax_settings (
		          id                 INT UNSIGNED    AUTO_INCREMENT  PRIMARY KEY,
		          userid             INT UNSIGNED    NOT NULL,
			  tel_secret_key     VARCHAR (512)   DEFAULT NULL,
			  tel_fax_app_id     VARCHAR (512)   DEFAULT NULL,
			  tel_recwh_url      VARCHAR (1024)  DEFAULT NULL,
			  phax_api_key       VARCHAR (512)   DEFAULT NULL,
			  phax_api_secret    VARCHAR (512)   DEFAULT NULL,
			  phax_webhook_token VARCHAR (512)   DEFAULT NULL,
			  phax_receive_url   VARCHAR (1024)  DEFAULT NULL,
			  get_notification   SMALLINT        DEFAULT NULL,
			  notification_email VARCHAR (512)   DEFAULT NULL,

		          CONSTRAINT `fk_faxusrid` FOREIGN KEY (`userid`) REFERENCES `app_users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
                     ); ";
         $result14 = $mysqli->query($query14);

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

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

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

     }
  }

?>

<!DOCTYPE html>
<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
 }
}

?>
