<?php
/**
 *  Copyright (C) 2021  Cover Tower 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();

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

 define('ACCESSCONST', TRUE);

 require('db-connect.php');

    $username = $_POST['username'];

    // Get the id of the user for which we want to get the external user data
    $query1 = $mysqli->query("SELECT id, userrole, username, enabled FROM app_users WHERE userrole = 'superadmin' AND BINARY username = '$username' AND enabled = 1");
    $queryres = $query1->fetch_array();
    $userID = $queryres[0];

    $extdatafromdb = [];

    if ($userID != '') {

	    // Get the data for the current user from the 'external_users' table
	    $querysel = $mysqli->query("SELECT id, userid, exten_for_external, exten_for_ext_pass, conf_access_link FROM external_users WHERE userid = '$userID'");

            while ($extUserDataRow = $querysel->fetch_row()) {

                    $extenforexternal = $extUserDataRow[2];

		    if ($extUserDataRow[3] != '' && $extUserDataRow[3] != null && $extUserDataRow[3] != 'undefined') {
			     $sippassword = "%20%20%20%20%20%20%20";
		    } else { $sippassword = ''; }

                    $confaccesslink = $extUserDataRow[4];

                   $extdatafromdb[] = ['exten_for_external' => $extenforexternal, 'exten_for_ext_pass' => $sippassword, 'conf_access_link' => $confaccesslink];                   
            }
    }
    echo json_encode($extdatafromdb);

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

?>