From 7d57cef6acde11f946e6ae35b6a2c341a654730a Mon Sep 17 00:00:00 2001 From: Andrew Querol Date: Thu, 5 Nov 2020 14:28:19 -0600 Subject: [PATCH] Fix device provisioning and event notification for multiple registrations across different SIP profiles. --- .../classes/feature_event_notify.php | 64 ++++++++++--------- app/devices/cmd.php | 30 ++++++--- app/devices/device_edit.php | 16 +---- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/app/calls/resources/classes/feature_event_notify.php b/app/calls/resources/classes/feature_event_notify.php index 28e0791011..b7efff6196 100644 --- a/app/calls/resources/classes/feature_event_notify.php +++ b/app/calls/resources/classes/feature_event_notify.php @@ -43,38 +43,40 @@ include "root.php"; //feature_event method public function send_notify() { - - $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); - if ($fp) { - //get the sip profile name - $command = "sofia_contact */".$this->extension."@".$this->domain_name; - $contact_string = event_socket_request($fp, "api ".$command); - if (substr($contact_string, 0, 5) == "sofia") { - $contact_array = explode("/", $contact_string); - $sip_profile_name = $contact_array[1]; - } - else { - $sip_profile_name = 'internal'; - } - //send the event - $event = "sendevent SWITCH_EVENT_PHONE_FEATURE\n"; - $event .= "profile: ".$sip_profile_name."\n"; - $event .= "user: ".$this->extension."\n"; - $event .= "host: ".$this->domain_name."\n"; - $event .= "device: \n"; - $event .= "Feature-Event: init\n"; - $event .= "forward_immediate_enabled: ".$this->forward_all_enabled."\n"; - $event .= "forward_immediate: ".$this->forward_all_destination."\n"; - $event .= "forward_busy_enabled: ".$this->forward_busy_enabled."\n"; - $event .= "forward_busy: ".$this->forward_busy_destination."\n"; - $event .= "forward_no_answer_enabled: ".$this->forward_no_answer_enabled."\n"; - $event .= "forward_no_answer: ".$this->forward_no_answer_destination."\n"; - $event .= "doNotDisturbOn: ".$this->do_not_disturb."\n"; - $event .= "ringCount: ".$this->ring_count."\n"; - event_socket_request($fp, $event); - - fclose($fp); + $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); + if ($fp) { + // Get the SIP profiles for the extension + $command = "sofia_contact */{$this->extension}@{$this->domain_name}"; + $contact_string = event_socket_request($fp, "api ".$command); + // The first value in the array will be full matching text, the second one will be the array of profile matches + preg_match_all('/sofia\/([^,]+)\/(?:[^,]+)/', $contact_string, $matches); + if (sizeof($matches) != 2 || sizeof($matches[1]) < 1) { + $profiles = array("internal"); + } else { + // We have at least one profile, get all of the unique profiles + $profiles = array_unique($matches[1]); } + + foreach ($profiles as $profile) { + //send the event + $event = "sendevent SWITCH_EVENT_PHONE_FEATURE\n"; + $event .= "profile: " . $profile . "\n"; + $event .= "user: " . $this->extension . "\n"; + $event .= "host: " . $this->domain_name . "\n"; + $event .= "device: \n"; + $event .= "Feature-Event: init\n"; + $event .= "forward_immediate_enabled: " . $this->forward_all_enabled . "\n"; + $event .= "forward_immediate: " . $this->forward_all_destination . "\n"; + $event .= "forward_busy_enabled: " . $this->forward_busy_enabled . "\n"; + $event .= "forward_busy: " . $this->forward_busy_destination . "\n"; + $event .= "forward_no_answer_enabled: " . $this->forward_no_answer_enabled . "\n"; + $event .= "forward_no_answer: " . $this->forward_no_answer_destination . "\n"; + $event .= "doNotDisturbOn: " . $this->do_not_disturb . "\n"; + $event .= "ringCount: " . $this->ring_count . "\n"; + event_socket_request($fp, $event); + } + fclose($fp); + } } //function } //class diff --git a/app/devices/cmd.php b/app/devices/cmd.php index 132001f441..7faeea7ffd 100644 --- a/app/devices/cmd.php +++ b/app/devices/cmd.php @@ -41,7 +41,6 @@ else { //set the variables $cmd = check_str($_GET['cmd']); $rdr = check_str($_GET['rdr']); - $profile = check_str($_GET['profile']); $domain = check_str($_GET['domain']); $show = check_str($_GET['show']); $user = check_str($_GET['user']); @@ -51,26 +50,39 @@ else { //create the event socket connection $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); if ($fp) { + // Get the SIP profiles for the user + $command = "sofia_contact */{$user}"; + $contact_string = event_socket_request($fp, "api ".$command); + // The first value in the array will be full matching text, the second one will be the array of profile matches + preg_match_all('/sofia\/([^,]+)\/(?:[^,]+)/', $contact_string, $matches); + if (sizeof($matches) != 2 || sizeof($matches[1]) < 1) { + $profiles = array("internal"); + } else { + // We have at least one profile, get all of the unique profiles + $profiles = array_unique($matches[1]); + } - //prepare the command + foreach ($profiles as $profile) { + //prepare the command if ($cmd == "unregister") { - $command = "sofia profile ".$profile." flush_inbound_reg ".$user." reboot"; + $command = "sofia profile {$profile} flush_inbound_reg {$user} reboot"; } else { - $command = "lua app.lua event_notify ".$profile." ".$cmd." ".$user." ".$vendor; + $command = "lua app.lua event_notify {$profile} {$cmd} {$user} {$vendor}"; //if ($cmd == "check_sync") { // $command = "sofia profile ".$profile." check_sync ".$user; //} } - //send the command - $response = event_socket_request($fp, "api ".$command); - $response = event_socket_request($fp, "api log notice ".$command); + //send the command + $response = event_socket_request($fp, "api {$command}"); + event_socket_request($fp, "api log notice {$command}"); - //show the response + //show the response message::add($text['label-event']." ".ucwords($cmd)."     ".$text['label-response'].htmlentities($response)); + } //close the connection - fclose($fp); + fclose($fp); } //redirect the user diff --git a/app/devices/device_edit.php b/app/devices/device_edit.php index b93f271737..f45281c94e 100644 --- a/app/devices/device_edit.php +++ b/app/devices/device_edit.php @@ -625,20 +625,6 @@ } } -//get the sip profile name - $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); - if ($fp) { - $command = "sofia_contact */".$user_id."@".$server_address; - $contact_string = event_socket_request($fp, "api ".$command); - if (substr($contact_string, 0, 5) == "sofia") { - $contact_array = explode("/", $contact_string); - $sip_profile_name = $contact_array[1]; - } - else { - $sip_profile_name = 'internal'; - } - } - //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); @@ -816,7 +802,7 @@ echo button::create(['type'=>'button','label'=>$text['button-qr_code'],'icon'=>'qrcode','style'=>$button_margin,'onclick'=>"$('#qr_code_container').fadeIn(400);"]); unset($button_margin); } - echo button::create(['type'=>'button','label'=>$text['button-provision'],'icon'=>'fax','style'=>$button_margin,'link'=>PROJECT_PATH."/app/devices/cmd.php?cmd=check_sync&profile=".urlencode($sip_profile_name)."&user=".urlencode($user_id)."@".urlencode($server_address)."&domain=".urlencode($server_address)."&agent=".urlencode($device_vendor)]); + echo button::create(['type'=>'button','label'=>$text['button-provision'],'icon'=>'fax','style'=>$button_margin,'link'=>PROJECT_PATH."/app/devices/cmd.php?cmd=check_sync"."&user=".urlencode($user_id)."@".urlencode($server_address)."&domain=".urlencode($server_address)."&agent=".urlencode($device_vendor)]); unset($button_margin); if (permission_exists("device_files")) { //get the template directory