<?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.
 */

session_start();

 // header('Set-Cookie: PHPSESSID= ' . session_id() . '; SameSite=strict; Secure=true; HttpOnly=true;');

if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {

    define('ACCESSCONST', TRUE);

    require('db-connect.php');

    $username = $_POST['username'];
    $audioConfExtension = $_POST['audio_conf_extension'];
    $audioConfTag = $_POST['audio_conf_tag'];
    $limitToGroups = $_POST['limit_to_groups'];

    // Get the id and role of the current user from the 'app_users' table
    $querysel = $mysqli->prepare("SELECT id, userrole, username FROM app_users WHERE BINARY username = ?");
    $querysel->bind_param("s", $username);
    $querysel->execute();
    $confdatafromdb = $querysel->get_result()->fetch_assoc();
    $cruserid = $confdatafromdb['id'];
    $cruserrole = $confdatafromdb['userrole'];

    // Check if the current audio conference extension has been already introduced in the 'conferences_audio' table
    $queryselck = $mysqli->prepare("SELECT id, audio_conf_extension FROM conferences_audio WHERE BINARY audio_conf_extension = ?");
    $queryselck->bind_param("s", $audioConfExtension);
    $queryselck->execute();
    $confdatafromdbck = $queryselck->get_result()->fetch_assoc();

    $queryselckad = $mysqli->prepare("SELECT id, userid, audio_conf_extension FROM conferences_audio WHERE BINARY userid = ? AND audio_conf_extension = ?");
    $queryselckad->bind_param("is", $cruserid, $audioConfExtension);
    $queryselckad->execute();
    $confdatafromdbckad = $queryselckad->get_result()->fetch_assoc();

    if ($cruserrole == 'superadmin') {

        if ($confdatafromdbck) {

            $updatequerysp = $mysqli->prepare("UPDATE conferences_audio SET audio_conf_label = ?, limit_to_groups = ? WHERE audio_conf_extension = ?");
            $updatequerysp->bind_param("sss", $audioConfTag, $limitToGroups, $audioConfExtension);
            if ($updatequerysp->execute()) { $messagetosend = 'The data has been successfully saved to the database !'; } else { $messagetosend = 'Error while updating the data !'; }

        } else {

            $insertquerysp = $mysqli->prepare("INSERT INTO conferences_audio (userid, audio_conf_extension, audio_conf_label, limit_to_groups) VALUES(?, ?, ?, ?)");
            $insertquerysp->bind_param("isss", $cruserid, $audioConfExtension, $audioConfTag, $limitToGroups);
            if ($insertquerysp->execute()) { $messagetosend = 'The data has been successfully saved to the database !'; } else { $messagetosend = 'Error while inserting the data !'; }
        }

    } elseif ($cruserrole == 'admin') {

	      if ($confdatafromdbckad) {

		  $updatequery = $mysqli->prepare("UPDATE conferences_audio SET audio_conf_label = ?, limit_to_groups = ? WHERE userid = ? AND audio_conf_extension = ?");
		  $updatequery->bind_param("ssis", $audioConfTag, $limitToGroups, $cruserid, $audioConfExtension);
		  if ($updatequery->execute()) { $messagetosend = 'The data has been successfully saved to the database !'; } else { $messagetosend = 'Error while updating the data !'; }

	      } elseif (!$confdatafromdbckad && !$confdatafromdbck) {

		  $insertquery = $mysqli->prepare("INSERT INTO conferences_audio (userid, audio_conf_extension, audio_conf_label, limit_to_groups) VALUES(?, ?, ?, ?)");
		  $insertquery->bind_param("isss", $cruserid, $audioConfExtension, $audioConfTag, $limitToGroups);
		  if ($insertquery->execute()) { $messagetosend = 'The data has been successfully saved to the database !'; } else { $messagetosend = 'Error while inserting the data !'; }

	      } else { $messagetosend = "You are not allowed to change this conference data!"; }

    } elseif ($cruserrole == 'regular_user') { $messagetosend = 'Only Admins and Superadmins can configure conferences.'; }

    $response = array('result' => $messagetosend);
    echo json_encode($response);

} else {
    header("Location: ../login.php");
}

?>
