Portions created by the Initial Developer are Copyright (C) 2008-2023 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane Lewis Hallam */ //includes files require_once dirname(__DIR__, 2) . "/resources/require.php"; //check permissions require_once "resources/check_auth.php"; if (permission_exists('call_flow_add') || permission_exists('call_flow_edit')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get(); //set the defaults $call_flow_sound = ''; $call_flow_alternate_sound = ''; $call_flow_name = ''; $call_flow_extension = ''; $call_flow_feature_code = ''; $call_flow_pin_number = ''; $call_flow_label = ''; $call_flow_alternate_label = ''; $call_flow_description = ''; $call_flow_status = ''; //action add or update if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) { $action = "update"; $call_flow_uuid = $_REQUEST["id"]; } else { $action = "add"; } //initialize the destinations object $destination = new destinations; //get http post variables and set them to php variables if (!empty($_POST)) { //set the variables from the http values $call_flow_uuid = $_POST["call_flow_uuid"] ?? null; $dialplan_uuid = $_POST["dialplan_uuid"] ?? null; $call_flow_name = $_POST["call_flow_name"]; $call_flow_extension = $_POST["call_flow_extension"]; $call_flow_feature_code = $_POST["call_flow_feature_code"]; $call_flow_status = $_POST["call_flow_status"]; $call_flow_pin_number = $_POST["call_flow_pin_number"]; $call_flow_label = $_POST["call_flow_label"]; $call_flow_sound = $_POST["call_flow_sound"]; $call_flow_destination = $_POST["call_flow_destination"]; $call_flow_alternate_label = $_POST["call_flow_alternate_label"]; $call_flow_alternate_sound = $_POST["call_flow_alternate_sound"]; $call_flow_alternate_destination = $_POST["call_flow_alternate_destination"]; $call_flow_context = $_POST["call_flow_context"]; $call_flow_enabled = $_POST["call_flow_enabled"] ?? 'false'; $call_flow_description = $_POST["call_flow_description"]; //seperate the action and the param $destination_array = explode(":", $call_flow_destination); $call_flow_app = array_shift($destination_array); $call_flow_data = join(':', $destination_array); //seperate the action and the param call_flow_alternate_app $alternate_destination_array = explode(":", $call_flow_alternate_destination); $call_flow_alternate_app = array_shift($alternate_destination_array); $call_flow_alternate_data = join(':', $alternate_destination_array); } //process the user data and save it to the database if (!empty($_POST) && empty($_POST["persistformvar"])) { //get the uuid from the POST if ($action == "update") { $call_flow_uuid = $_POST["call_flow_uuid"]; } //validate the token $token = new token; if (!$token->validate($_SERVER['PHP_SELF'])) { message::add($text['message-invalid_token'],'negative'); header('Location: call_flows.php'); exit; } //check for all required data $msg = ''; //if (empty($domain_uuid)) { $msg .= $text['message-required']." ".$text['label-domain_uuid']."
\n"; } //if (empty($call_flow_uuid)) { $msg .= $text['message-required']." ".$text['label-call_flow_uuid']."
\n"; } //if (empty($dialplan_uuid)) { $msg .= $text['message-required']." ".$text['label-dialplan_uuid']."
\n"; } //if (empty($call_flow_name)) { $msg .= $text['message-required']." ".$text['label-call_flow_name']."
\n"; } if (empty($call_flow_extension)) { $msg .= $text['message-required']." ".$text['label-call_flow_extension']."
\n"; } if (empty($call_flow_feature_code)) { $msg .= $text['message-required']." ".$text['label-call_flow_feature_code']."
\n"; } //if (empty($call_flow_context)) { $msg .= $text['message-required']." ".$text['label-call_flow_context']."
\n"; } //if (empty($call_flow_status)) { $msg .= $text['message-required']." ".$text['label-call_flow_status']."
\n"; } //if (empty($call_flow_pin_number)) { $msg .= $text['message-required']." ".$text['label-call_flow_pin_number']."
\n"; } //if (empty($call_flow_label)) { $msg .= $text['message-required']." ".$text['label-call_flow_label']."
\n"; } //if (empty($call_flow_sound)) { $msg .= $text['message-required']." ".$text['label-call_flow_sound']."
\n"; } if (empty($call_flow_app)) { $msg .= $text['message-required']." ".($text['label-call_flow_app'] ?? '')."
\n"; } if (empty($call_flow_data)) { $msg .= $text['message-required']." ".($text['label-call_flow_data'] ?? '')."
\n"; } //if (empty($call_flow_alternate_label)) { $msg .= $text['message-required']." ".$text['label-call_flow_alternate_label']."
\n"; } //if (empty($call_flow_alternate_sound)) { $msg .= $text['message-required']." ".$text['label-call_flow_alternate_sound']."
\n"; } //if (empty($call_flow_alternate_app)) { $msg .= $text['message-required']." ".$text['label-call_flow_alternate_app']."
\n"; } //if (empty($call_flow_alternate_data)) { $msg .= $text['message-required']." ".$text['label-call_flow_alternate_data']."
\n"; } //if (empty($call_flow_description)) { $msg .= $text['message-required']." ".$text['label-call_flow_description']."
\n"; } if (!empty($msg) && empty($_POST["persistformvar"])) { require_once "resources/header.php"; require_once "resources/persist_form_var.php"; echo "
\n"; echo "
\n"; echo $msg."
"; echo "
\n"; persistformvar($_POST); echo "
\n"; require_once "resources/footer.php"; return; } //add the call_flow_uuid if (empty($call_flow_uuid)) { $call_flow_uuid = uuid(); } //add the dialplan_uuid if (empty($dialplan_uuid)) { $dialplan_uuid = uuid(); } //set the default context if (permission_exists("call_flow_context")) { //allow a user assigned to super admin to change the call_flow_context } else { //if the call_flow_context was not set then set the default value $call_flow_context = $_SESSION['domain_name']; } //escape special characters $destination_extension = $call_flow_extension; $destination_extension = str_replace("*", "\*", $destination_extension); $destination_extension = str_replace("+", "\+", $destination_extension); $destination_feature = $call_flow_feature_code; // Allows dial feature code as `flow+` if (substr($destination_feature, 0, 5) != 'flow+') { $destination_feature = '(?:flow+)?' . $destination_feature; } $destination_feature = str_replace("*", "\*", $destination_feature); $destination_feature = str_replace("+", "\+", $destination_feature); //build the xml dialplan $dialplan_xml = "\n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= " \n"; $dialplan_xml .= "\n"; //set the row id $i = 0; //build the dialplan array $array["dialplans"][$i]["domain_uuid"] = $_SESSION['domain_uuid']; $array["dialplans"][$i]["dialplan_uuid"] = $dialplan_uuid; $array["dialplans"][$i]["dialplan_name"] = $call_flow_name; $array["dialplans"][$i]["dialplan_number"] = $call_flow_extension; $array["dialplans"][$i]["dialplan_context"] = $call_flow_context; $array["dialplans"][$i]["dialplan_continue"] = "false"; $array["dialplans"][$i]["dialplan_xml"] = $dialplan_xml; $array["dialplans"][$i]["dialplan_order"] = "333"; $array["dialplans"][$i]["dialplan_enabled"] = $call_flow_enabled; $array["dialplans"][$i]["dialplan_description"] = $call_flow_description; $array["dialplans"][$i]["app_uuid"] = "b1b70f85-6b42-429b-8c5a-60c8b02b7d14"; $array["call_flows"][$i]["call_flow_uuid"] = $call_flow_uuid; $array["call_flows"][$i]["domain_uuid"] = $_SESSION['domain_uuid']; $array["call_flows"][$i]["dialplan_uuid"] = $dialplan_uuid; $array["call_flows"][$i]["call_flow_name"] = $call_flow_name; $array["call_flows"][$i]["call_flow_extension"] = $call_flow_extension; $array["call_flows"][$i]["call_flow_feature_code"] = $call_flow_feature_code; $array["call_flows"][$i]["call_flow_status"] = $call_flow_status; $array["call_flows"][$i]["call_flow_pin_number"] = $call_flow_pin_number; $array["call_flows"][$i]["call_flow_label"] = $call_flow_label; $array["call_flows"][$i]["call_flow_sound"] = $call_flow_sound; $array["call_flows"][$i]["call_flow_alternate_label"] = $call_flow_alternate_label; $array["call_flows"][$i]["call_flow_alternate_sound"] = $call_flow_alternate_sound; if ($destination->valid($call_flow_app.':'.$call_flow_data)) { $array["call_flows"][$i]["call_flow_app"] = $call_flow_app; $array["call_flows"][$i]["call_flow_data"] = $call_flow_data; } if ($destination->valid($call_flow_alternate_app.':'.$call_flow_alternate_data)) { $array["call_flows"][$i]["call_flow_alternate_app"] = $call_flow_alternate_app; $array["call_flows"][$i]["call_flow_alternate_data"] = $call_flow_alternate_data; } $array["call_flows"][$i]["call_flow_context"] = $call_flow_context; $array["call_flows"][$i]["call_flow_enabled"] = $call_flow_enabled; $array["call_flows"][$i]["call_flow_description"] = $call_flow_description; //add the dialplan permission $p = new permissions; $p->add("dialplan_add", "temp"); $p->add("dialplan_edit", "temp"); //save to the data $database = new database; $database->app_name = 'call_flows'; $database->app_uuid = 'b1b70f85-6b42-429b-8c5a-60c8b02b7d14'; if (!empty($call_flow_uuid)) { $database->uuid($call_flow_uuid); } $database->save($array); $message = $database->message; //remove the temporary permission $p->delete("dialplan_add", "temp"); $p->delete("dialplan_edit", "temp"); // Update subscribed endpoints $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); if ($fp) { //send the event $event = "sendevent PRESENCE_IN\n"; $event .= "proto: flow\n"; $event .= "event_type: presence\n"; $event .= "alt_event_type: dialog\n"; $event .= "Presence-Call-Direction: outbound\n"; $event .= "state: Active (1 waiting)\n"; $event .= "from: flow+".$call_flow_feature_code."@".$_SESSION['domain_name']."\n"; $event .= "login: flow+".$call_flow_feature_code."@".$_SESSION['domain_name']."\n"; $event .= "unique-id: ".$call_flow_uuid."\n"; if ($call_flow_status == "true") { $event .= "answer-state: confirmed\n"; } else { $event .= "answer-state: terminated\n"; } event_socket_request($fp, $event); //echo $event."
"; fclose($fp); } //debug info //echo "
";
			//print_r($message);
			//echo "
"; //exit; //apply settings reminder $_SESSION["reload_xml"] = true; //clear the cache $cache = new cache; $cache->delete("dialplan:".$call_flow_context); //clear the destinations session array if (isset($_SESSION['destinations']['array'])) { unset($_SESSION['destinations']['array']); } //redirect the user if (isset($action)) { if ($action == "add") { message::add($text['message-add']); } if ($action == "update") { message::add($text['message-update']); } header("Location: call_flows.php"); return; } } //(is_array($_POST) && empty($_POST["persistformvar"])) //pre-populate the form if (!empty($_GET) && empty($_POST["persistformvar"])) { $call_flow_uuid = $_GET["id"]; $sql = "select * from v_call_flows "; $sql .= "where domain_uuid = :domain_uuid "; $sql .= "and call_flow_uuid = :call_flow_uuid "; $parameters['domain_uuid'] = $_SESSION['domain_uuid']; $parameters['call_flow_uuid'] = $call_flow_uuid; $database = new database; $result = $database->select($sql, $parameters, 'all'); foreach ($result as $row) { //set the php variables $call_flow_uuid = $row["call_flow_uuid"]; $dialplan_uuid = $row["dialplan_uuid"]; $call_flow_name = $row["call_flow_name"]; $call_flow_extension = $row["call_flow_extension"]; $call_flow_feature_code = $row["call_flow_feature_code"]; $call_flow_context = $row["call_flow_context"]; $call_flow_status = $row["call_flow_status"]; $call_flow_pin_number = $row["call_flow_pin_number"]; $call_flow_label = $row["call_flow_label"]; $call_flow_sound = $row["call_flow_sound"]; $call_flow_app = $row["call_flow_app"]; $call_flow_data = $row["call_flow_data"]; $call_flow_alternate_label = $row["call_flow_alternate_label"]; $call_flow_alternate_sound = $row["call_flow_alternate_sound"]; $call_flow_alternate_app = $row["call_flow_alternate_app"]; $call_flow_alternate_data = $row["call_flow_alternate_data"]; $call_flow_enabled = $row["call_flow_enabled"]; $call_flow_description = $row["call_flow_description"]; //if superadmin show both the app and data if (if_group("superadmin")) { $destination_label = $call_flow_app.':'.$call_flow_data; } else { $destination_label = $call_flow_data; } //if superadmin show both the app and data if (if_group("superadmin")) { $alternate_destination_label = $call_flow_alternate_app.':'.$call_flow_alternate_data; } else { $alternate_destination_label = $call_flow_alternate_data; } } unset ($sql, $parameters, $result, $row); } //set the context for users that are not in the superadmin group if (empty($call_flow_context)) { $call_flow_context = $_SESSION['domain_name']; } //set the defaults if (empty($call_flow_enabled)) { $call_flow_enabled = 'true'; } //get the recordings $sql = "select recording_name, recording_filename from v_recordings "; $sql .= "where domain_uuid = :domain_uuid "; $sql .= "order by recording_name asc "; $parameters['domain_uuid'] = $_SESSION['domain_uuid']; $database = new database; $recordings = $database->select($sql, $parameters, 'all'); unset($parameters, $sql); if (if_group("superadmin")) { echo "\n"; echo "\n"; } function sound_select_list($var, $name, $description_name, $load_sound=false) { global $text, $recordings, $db; echo "\n"; echo "\n"; echo " ".$text['label-' . $description_name]."\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo $text['description-' . $description_name]."\n"; echo "\n"; echo "\n"; } //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); //show the header $document['title'] = $text['title-call_flow']; require_once "resources/header.php"; //show the content echo "
\n"; echo "
\n"; echo "
".$text['title-call_flow']."
\n"; echo "
\n"; echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'call_flows.php']); echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 15px;']); echo "
\n"; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; sound_select_list($call_flow_sound, 'call_flow_sound', 'call_flow_sound', true); /* echo "\n"; echo "\n"; echo "\n"; echo "\n"; */ echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; sound_select_list($call_flow_alternate_sound, 'call_flow_alternate_sound', 'call_flow_alternate_sound', true); /* echo "\n"; echo "\n"; echo "\n"; echo "\n"; */ echo "\n"; echo "\n"; echo "\n"; echo "\n"; if (permission_exists('call_flow_context')) { echo "\n"; echo "\n"; echo "\n"; echo "\n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo " ".$text['label-call_flow_name']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_name']."\n"; echo "
\n"; echo " ".$text['label-call_flow_extension']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_extension']."\n"; echo "
\n"; echo " ".$text['label-call_flow_feature_code']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_feature_code']."\n"; echo "
\n"; echo " ".$text['label-call_flow_status']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_status']."\n"; echo "
\n"; echo " ".$text['label-call_flow_pin_number']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_pin_number']."\n"; echo "
\n"; echo " ".$text['label-call_flow_label']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo !empty($text['description-call_flow_label'])."\n"; echo "
\n"; echo " ".$text['label-call_flow_sound']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_sound']."\n"; echo "
\n"; echo " ".$text['label-call_flow_destination']."\n"; echo "\n"; //set the selected value $select_value = ''; if (!empty($call_flow_app) && !empty($call_flow_data)) { $select_value = $call_flow_app.':'.$call_flow_data; } //show the destination list echo $destination->select('dialplan', 'call_flow_destination', $select_value); unset($select_value); echo "
\n"; echo $text['description-call_flow_destination']."\n"; echo "
\n"; echo " ".$text['label-call_flow_alternate_label']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_alternate_label']."\n"; echo "
\n"; echo " ".$text['label-call_flow_alternate_sound']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_alternate_sound']."\n"; echo "
\n"; echo " ".$text['label-call_flow_alternate_destination']."\n"; echo "\n"; $select_value = ''; if (!empty($call_flow_alternate_app) && !empty($call_flow_alternate_data)) { $select_value = $call_flow_alternate_app.':'.$call_flow_alternate_data; } echo $destination->select('dialplan', 'call_flow_alternate_destination', $select_value); unset($select_value); echo "
\n"; echo $text['description-call_flow_alternate_destination']."\n"; echo "
\n"; echo " ".$text['label-call_flow_context']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-call_flow_context']."\n"; echo "
\n"; echo " ".$text['label-enabled']."\n"; echo "\n"; if (substr($_SESSION['theme']['input_toggle_style']['text'], 0, 6) == 'switch') { echo " \n"; } else { echo " \n"; } echo "
\n"; echo " ".$text['label-call_flow_description']."\n"; echo "\n"; echo " \n"; echo "
\n"; echo !empty($text['description-call_flow_description'])."\n"; echo "
"; echo "

"; if ($action == "update") { echo "\n"; echo "\n"; } echo "\n"; echo "
"; //include the footer require_once "resources/footer.php"; ?>