Use single event socket object in registrations class (#7288)

* Use single event socket object in registrations class
Allow passing an event_socket in the constructor
Ensure the event_socket is connected before using it in loops
This commit is contained in:
frytimo 2025-03-05 13:08:08 -04:00 committed by GitHub
parent b714d25150
commit e9739d2b06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 51 additions and 18 deletions

View File

@ -24,8 +24,6 @@
Mark J Crane <markjcrane@fusionpbx.com>
*/
//define the registrations class
if (!class_exists('registrations')) {
class registrations {
/**
@ -44,6 +42,12 @@ if (!class_exists('registrations')) {
*/
private $database;
/**
* Set in the constructor. Must be an event_socket object and cannot be null.
* @var event_socket Event Socket Connection Object
*/
private $event_socket;
/**
* called when the object is created
*/
@ -59,8 +63,21 @@ if (!class_exists('registrations')) {
//trap passing a PDO object instead of the required database object
if (!($this->database instanceof database)) {
//should never happen but will trap it here just-in-case
throw new \InvalidArgumentException("Database object passed in settings class constructor is not a valid database object");
//should never happen but will trap it here just in case
throw new \InvalidArgumentException("Database object passed in the constructor is not a valid database object");
}
if (!empty($setting_array['event_socket'])) {
$this->event_socket = $setting_array['event_socket'];
}
else {
$this->event_socket = event_socket::create();
}
//trap passing an invalid connection object for communicating to the switch
if (!($this->event_socket instanceof event_socket)) {
//should never happen but will trap it here just in case
throw new \InvalidArgumentException('Event socket object passed in the constructor is not a valid event_socket object');
}
//assign private variables
@ -89,7 +106,21 @@ if (!class_exists('registrations')) {
$id = 0;
//create the event socket connection
$esl = event_socket::create();
$event_socket = $this->event_socket;
//make sure the event socket is connected
if (!$event_socket->is_connected()) {
//connect to event socket
$event_socket->connect();
//check again and throw an error if it can't connect
if (!$event_socket->is_connected()) {
message::add($text['error-event-socket'], 'negative', 5000);
return null;
}
}
//get the default settings
$sql = "select sip_profile_name from v_sip_profiles ";
@ -100,12 +131,18 @@ if (!class_exists('registrations')) {
}
$sql .= "and sip_profile_enabled = 'true' ";
$sip_profiles = $this->database->select($sql, $parameters ?? null, 'all');
if (!empty($sip_profiles) && @sizeof($sip_profiles) != 0) {
foreach ($sip_profiles as $field) {
if (!empty($sip_profiles)) {
//use a while loop to ensure the event socket stays connected while communicating
$count = count($sip_profiles);
$i = 0;
while ($event_socket->is_connected() && $i++ < $count) {
$field = $sip_profiles[$i];
//get sofia status profile information including registrations
$cmd = "api sofia xmlstatus profile '".$field['sip_profile_name']."' reg";
$xml_response = trim(event_socket::command($cmd));
$xml_response = trim($event_socket->request($cmd));
//show an error message
if ($xml_response == "Invalid Profile!") {
@ -117,7 +154,7 @@ if (!class_exists('registrations')) {
$xml_response = "<error_msg>".escape($text['label-message'])."</error_msg>";
}
//santize the XML
//sanitize the XML
if (function_exists('iconv')) { $xml_response = iconv("utf-8", "utf-8//IGNORE", $xml_response); }
$xml_response = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $xml_response);
$xml_response = str_replace("<profile-info>", "<profile_info>", $xml_response);
@ -301,10 +338,10 @@ if (!class_exists('registrations')) {
unset($sql);
//create the event socket connection
$esl = event_socket::create();
$event_socket = $this->event_socket;
//loop through registrations
if ($esl->is_connected()) {
if ($event_socket->is_connected()) {
//check if registrations exist
if (is_array($registrations)) {
foreach ($registrations as $registration) {
@ -364,11 +401,10 @@ if (!class_exists('registrations')) {
}
//send the api command
if (!empty($command) && $esl->is_connected()) {
$response_api[$registration['user']]['command'] = event_socket::api($command);
$response_api[$registration['user']]['log'] = event_socket::api("log notice $command");
if (!empty($command) && $event_socket->is_connected()) {
$response_api[$registration['user']]['command'] = $event_socket->request('api ' . $command);
$response_api[$registration['user']]['log'] = $event_socket->request("log notice $command");
}
}
}
@ -394,6 +430,3 @@ if (!class_exists('registrations')) {
} //method
} //class
}
?>