Updated to simplify building bridge statement

This commit is contained in:
FusionPBX 2023-08-19 11:04:52 -06:00 committed by GitHub
parent 30f028e6be
commit 1f75cfe2d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 262 additions and 7 deletions

View File

@ -17,7 +17,7 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2018 - 2019
Portions created by the Initial Developer are Copyright (C) 2018 - 2023
the Initial Developer. All Rights Reserved.
*/
@ -54,8 +54,12 @@
//get http post variables and set them to php variables
if (!empty($_POST)) {
$bridge_uuid = $_POST["bridge_uuid"] ?? null;
$bridge_uuid = $_POST["bridge_uuid"];
$bridge_name = $_POST["bridge_name"];
$bridge_action = $_POST["bridge_action"];
$bridge_variables = $_POST["bridge_variables"];
$bridge_gateways = $_POST["bridge_gateways"];
$destination_number = $_POST["destination_number"];
$bridge_destination = $_POST["bridge_destination"];
$bridge_enabled = $_POST["bridge_enabled"] ?? 'false';
$bridge_description = $_POST["bridge_description"];
@ -95,7 +99,7 @@
//check for all required data
$msg = '';
if (empty($bridge_name)) { $msg .= $text['message-required']." ".$text['label-bridge_name']."<br>\n"; }
if (empty($bridge_destination)) { $msg .= $text['message-required']." ".$text['label-bridge_destination']."<br>\n"; }
//if (empty($bridge_destination)) { $msg .= $text['message-required']." ".$text['label-bridge_destination']."<br>\n"; }
if (empty($bridge_enabled)) { $msg .= $text['message-required']." ".$text['label-bridge_enabled']."<br>\n"; }
if (!empty($msg) && empty($_POST["persistformvar"])) {
require_once "resources/header.php";
@ -115,6 +119,33 @@
$bridge_uuid = uuid();
}
//create the main bridge statement
$bridge_base = '';
if (!empty($bridge_gateways)) {
foreach($bridge_gateways as $gateway) {
if (!empty($gateway)) {
$gateway_array = explode(':', $gateway);
$bridge_base .= ',sofia/gateway/'.$gateway_array[0].'/'.$destination_number;
}
}
if (!empty($bridge_base)) {
$bridge_destination = trim($bridge_base, ',');
}
}
//add the variables back into the bridg_destination value
if (!empty($bridge_variables)) {
$variables = '';
foreach($bridge_variables as $key => $value) {
if (!empty($value)) {
$variables .= ','.$key.'='.$value;
}
}
if (!empty($variables)) {
$bridge_destination = '{'.trim($variables, ',').'}'.$bridge_destination;
}
}
//prepare the array
$array['bridges'][0]['bridge_uuid'] = $bridge_uuid;
$array['bridges'][0]['domain_uuid'] = $_SESSION["domain_uuid"];
@ -165,6 +196,111 @@
unset($sql, $parameters, $row);
}
//build the bridge_actions array from the session actions
$i = 0;
foreach($_SESSION['bridge']['action'] as $variable) {
$bridge_actions[$i]['action'] = $variable;
$bridge_actions[$i]['label'] = ucwords($variable);
$i++;
}
//initialize the bridge_variables array from session bridge variables
$session_variables = []; $i = 0;
foreach($_SESSION['bridge']['variable'] as $variable) {
$variable = explode("=", $variable);
$session_variables[$i]['name'] = $variable[0];
$session_variables[$i]['value'] = $variable[1] ?? '';
$session_variables[$i]['label'] = ucwords(str_replace('_', ' ', $variable[0]));
$session_variables[$i]['label'] = str_replace('Effective Caller Id', 'Caller ID', $session_variables[$i]['label']);
$i++;
}
//get the bridge variables from the database bridge_destination value
$database_variables = []; $x = 0;
if (!empty($bridge_destination)) {
//get the variables from inside the { and } brackets
preg_match('/\{([^}]+)\}/', $bridge_destination, $matches);
//create a variables array from the comma delimitted string
$variables = explode(",", $matches[1]);
//strip the variables from the $bridge_destination variable
$bridge_destination = str_replace("{$matches[0]}", '', $bridge_destination);
//build a bridge variables data set
$x = 0;
foreach($variables as $variable) {
$pairs = explode("=", $variable);
$database_variables[$x]['name'] = $pairs[0];
$database_variables[$x]['value'] = $pairs[1];
$database_variables[$x]['label'] = ucwords(str_replace('_', ' ', $pairs[0]));
$database_variables[$x]['label'] = str_replace('Effective Caller Id', 'Caller ID', $database_variables[$x]['label']);
$x++;
}
}
//merge the session and database bridge arrays together
$bridge_variables = $session_variables;
foreach($database_variables as $row) {
$found = false;
$i = 0;
foreach($bridge_variables as $field) {
if ($row['name'] == $field['name']) {
//matching row found
$found = true;
//override session value with the value from the database
if (!empty($row['value'])) {
$bridge_variables[$i]['value'] = $row['value'];
}
}
$i++;
}
if (!$found) {
$bridge_variables[] = $row;
}
}
//get the gateways
$actions = explode(',', $bridge_destination);
foreach($actions as $action) {
$action_array = explode('/',$action);
if ($action_array[1] == 'gateway') {
$bridge_gateways[] = $action_array[2];
$destination_number = $action_array[3];
}
}
//get the gateways
$sql = "select * from v_gateways ";
$sql .= "where enabled = 'true' ";
if (permission_exists('outbound_route_any_gateway')) {
$sql .= "order by domain_uuid = :domain_uuid DESC, gateway ";
}
else {
$sql .= "and domain_uuid = :domain_uuid ";
}
$parameters['domain_uuid'] = $_SESION['domain_uuid'];
$database = new database;
$gateways = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
//get the domains
$sql = "select * from v_domains ";
$sql .= "where domain_enabled = 'true' ";
$database = new database;
$domains = $database->select($sql, null, 'all');
unset($sql);
//get the sip profiles
$sql = "select sip_profile_name ";
$sql .= "from v_sip_profiles ";
$sql .= "where sip_profile_enabled = 'true' ";
$sql .= "order by sip_profile_name asc ";
$database = new database;
$sip_profiles = $database->select($sql, null, 'all');
unset($sql);
//set the defaults
if (empty($bridge_enabled)) { $bridge_enabled = 'true'; }
@ -208,12 +344,131 @@
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td width='30%' class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-bridge_action']."\n";
echo "</td>\n";
echo "<td width='70%' class='vtable' style='position: relative;' align='left'>\n";
echo " <select class='formfld' id='bridge_enabled' name='bridge_enabled'>\n";
echo " <option value=''></option>\n";
$i = 0;
foreach($bridge_actions as $row) {
echo " <option value='true' ".($bridge_action = $row['name'] ? "selected='selected'" : null).">".$row['label']."</option>\n";
}
echo " </select>\n";
echo "<br />\n";
echo $text['description-bridge_action']."\n";
echo "</td>\n";
echo "</tr>\n";
$i = 0;
foreach($bridge_variables as $row) {
echo "<tr>\n";
echo "<td width='30%' class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$row['label']."\n";
echo "</td>\n";
echo "<td width='70%' class='vtable' style='position: relative;' align='left'>\n";
echo " <input class='formfld' type='text' name='bridge_variables[".$row['name']."]' maxlength='255' value='".escape($row['value'])."'>\n";
echo "<br />\n";
echo $text['description-bridge_variables']."\n";
echo "</td>\n";
echo "</tr>\n";
$i++;
}
echo "<tr>\n";
echo "<td class='vncellreq' valign='top' align='left' nowrap>\n";
echo " ".$text['label-profile']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
echo " <select class='formfld' name='profile' required='required'>\n";
echo " <option value=''></option>\n";
foreach ($sip_profiles as $row) {
$sip_profile_name = $row["sip_profile_name"];
if ($profile == $sip_profile_name) {
echo " <option value='$sip_profile_name' selected='selected'>".escape($sip_profile_name)."</option>\n";
}
else {
echo " <option value='".escape($sip_profile_name)."'>".escape($sip_profile_name)."</option>\n";
}
}
echo " </select>\n";
echo "<br />\n";
echo $text['description-profile']."\n";
echo "</td>\n";
echo "</tr>\n";
for ($x = 0; $x <= 2; $x++) {
echo "<tr>\n";
echo "<td width='30%' class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-bridge_gateways']."\n";
echo "</td>\n";
echo "<td width='70%' class='vtable' style='position: relative;' align='left'>\n";
echo "<select name=\"bridge_gateways[]\" id=\"gateway\" class=\"formfld\" $onchange>\n";
echo "<option value=''></option>\n";
echo "<optgroup label='".$text['label-gateway']."gateway'>\n";
$previous_domain_uuid = '';
foreach($gateways as $row) {
if (permission_exists('outbound_route_any_gateway')) {
if ($previous_domain_uuid != $row['domain_uuid']) {
$domain_name = '';
foreach($domains as $field) {
if ($row['domain_uuid'] == $field['domain_uuid']) {
$domain_name = $field['domain_name'];
break;
}
}
if (empty($domain_name)) { $domain_name = $text['label-global']; }
echo "</optgroup>";
echo "<optgroup label='&nbsp; &nbsp;".$domain_name."'>\n";
}
if ($row['gateway_uuid'] == $bridge_gateways[$x]) {
echo "<option value=\"".escape($row['gateway_uuid']).":".escape($row['gateway'])."\" selected=\"selected\">".escape($row['gateway'])."</option>\n"; //." db:".$row['gateway_uuid']." bg:".$bridge_gateways[$x]
}
else {
echo "<option value=\"".escape($row['gateway_uuid']).":".escape($row['gateway'])."\">".escape($row['gateway'])."</option>\n";
}
}
else {
if ($row['gateway_uuid'] == $bridge_gateways[$x]) {
echo "<option value=\"".escape($row['gateway_uuid']).":".escape($row['gateway'])."\" $onchange selected=\"selected\">".escape($row['gateway'])."</option>\n";
}
else {
echo "<option value=\"".escape($row['gateway_uuid']).":".escape($row['gateway'])."\">".escape($row['gateway'])."</option>\n";
}
}
$previous_domain_uuid = $row['domain_uuid'];
}
//echo " </optgroup>\n";
//echo " <optgroup label='".$text['label-add-options']."Options'>\n";
//echo " <option value=\"loopback\">loopback</option>\n";
//echo " <option value=\"freetdm\">freetdm</option>\n";
//echo " <option value=\"xmpp\">xmpp</option>\n";
//echo " </optgroup>\n";
echo "</select>\n";
echo "<br />\n";
echo $text['description-bridge_gateways']."\n";
echo "</td>\n";
echo "</tr>\n";
}
echo "<tr>\n";
echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-destination_number']."\n";
echo "</td>\n";
echo "<td class='vtable' style='position: relative;' align='left'>\n";
echo " <input class='formfld' type='text' name='destination_number' maxlength='255' value='".escape($destination_number)."'>\n";
echo "<br />\n";
echo $text['description-destination_number']."\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
echo " ".$text['label-bridge_destination']."\n";
echo "</td>\n";
echo "<td class='vtable' style='position: relative;' align='left'>\n";
echo " <input class='formfld' type='text' name='bridge_destination' maxlength='255' value='".escape($bridge_destination)."'>\n";
echo " <textarea class='formfld' name='bridge_destination'>".escape($bridge_destination)."</textarea>\n";
echo "<br />\n";
echo $text['description-bridge_destination']."\n";
echo "</td>\n";
@ -252,10 +507,10 @@
echo "</td>\n";
echo "</tr>\n";
echo "</table>";
echo "<br /><br />";
echo "</table>\n";
echo "<br /><br />\n";
if ($action == "update") {
if (!empty($bridge_uuid)) {
echo "<input type='hidden' name='bridge_uuid' value='".escape($bridge_uuid)."'>\n";
}
echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";