From a98156ece576943f171a014d0540bd19a197a3bf Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Wed, 12 Mar 2025 21:46:37 -0300 Subject: [PATCH 1/7] remove include statement of class file --- resources/service/message_events.php | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/service/message_events.php b/resources/service/message_events.php index 4ecc80b..9585713 100644 --- a/resources/service/message_events.php +++ b/resources/service/message_events.php @@ -49,7 +49,6 @@ //includes require_once "resources/require.php"; - include "resources/classes/permissions.php"; //define the process id file $pid_file = "/var/run/fusionpbx/".basename( $argv[0], ".php") .".pid"; From fee0b86f010955acbf29ea40177ae5d667714f22 Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Wed, 12 Mar 2025 21:47:44 -0300 Subject: [PATCH 2/7] remove include statement of class file --- message_update.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/message_update.php b/message_update.php index 5320521..844f4ea 100644 --- a/message_update.php +++ b/message_update.php @@ -26,12 +26,9 @@ //includes files require_once dirname(__DIR__, 2) . "/resources/require.php"; - include "resources/classes/cache.php"; //get the last message in the cache if (is_uuid($_GET['id'])) { $cache = new cache; echo $cache->get("messages:user:last_message:".$_GET['id']); } - -?> From 51c8ffa94a482d65e0d875720ffad6e6dcef2beb Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Wed, 12 Mar 2025 21:48:32 -0300 Subject: [PATCH 3/7] remove include statement of class file --- resources/service/message_queue.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/resources/service/message_queue.php b/resources/service/message_queue.php index e48d6ed..2c9e460 100644 --- a/resources/service/message_queue.php +++ b/resources/service/message_queue.php @@ -40,7 +40,6 @@ //includes require_once "resources/require.php"; - include "resources/classes/permissions.php"; //define the process id file $pid_file = "/var/run/fusionpbx/".basename( $argv[0], ".php") .".pid"; @@ -55,7 +54,7 @@ function process_exists($file = false) { //check to see if the process is running if (file_exists($file)) { $pid = file_get_contents($file); - if (posix_getsid($pid) === false) { + if (posix_getsid($pid) === false) { //process is not running $exists = false; } @@ -188,5 +187,3 @@ function process_exists($file = false) { //how to use this feature // cd /var/www/fusionpbx && /usr/bin/php /var/www/fusionpbx/app/messages/resources/service/message_queue.php - -?> From 6c378f73832292f67d80b5def13571bd3fca003b Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Wed, 12 Mar 2025 21:48:59 -0300 Subject: [PATCH 4/7] remove include statement of class file --- resources/service/message_send_inbound.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/resources/service/message_send_inbound.php b/resources/service/message_send_inbound.php index 9b5d9b2..67b45e7 100644 --- a/resources/service/message_send_inbound.php +++ b/resources/service/message_send_inbound.php @@ -17,8 +17,6 @@ //include files require_once "resources/require.php"; - include "resources/classes/cache.php"; - include "resources/classes/permissions.php"; //save the arguments to variables $script_name = $argv[0]; @@ -206,8 +204,6 @@ //set the last message in the cache $cache = new cache; $cache->set("messages:user:last_message:".$user_uuid, date('r')); - + //how to use it // php /var/www/fusionpbx/app/messages/resources/service/message_send_inbound.php message_queue_uuid=39402652-1475-49f8-8366-7889335edd6f&hostname=voip.fusionpbx.com - -?> From d5a0ddfdee59cbd811a30be2116a0ab9edff06d8 Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Wed, 12 Mar 2025 21:50:15 -0300 Subject: [PATCH 5/7] remove include statement of class file --- resources/service/message_send_outbound.php | 24 +++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/resources/service/message_send_outbound.php b/resources/service/message_send_outbound.php index 48f3b04..a02ceec 100644 --- a/resources/service/message_send_outbound.php +++ b/resources/service/message_send_outbound.php @@ -17,11 +17,9 @@ //includes files require_once "resources/require.php"; - include "resources/classes/cache.php"; - include "resources/classes/permissions.php"; //connect to the database - $database = new database; + $database = database::new(); //save the arguments to variables $script_name = $argv[0]; @@ -156,7 +154,7 @@ function build_array($array, $path, $value) { if ($row['provider_setting_subcategory'] == 'content') { $content[$row['provider_setting_name']] = $row['provider_setting_value']; } - + //set the format array if ($row['provider_setting_subcategory'] == 'format') { $format[$row['provider_setting_name']] = $row['provider_setting_value']; @@ -194,13 +192,13 @@ function build_array($array, $path, $value) { if (isset($format['message_media_message_to']) && !empty($format['message_media_message_to'])) { $message_to = format_string($format['message_media_message_to'], $message_to); - } + } elseif (isset($format['message_to'])) { $message_to = format_string($format['message_to'], $message_to); } - } + } else { - //default formats. If setting is defined but format string is left blank, the format_string function + //default formats. If setting is defined but format string is left blank, the format_string function //will return the data as is (No changes made) if (isset($format['message_from'])) { $message_from = format_string($format['message_from'], $message_from); @@ -226,7 +224,7 @@ function build_array($array, $path, $value) { if (isset($setting['type'])) { $content_type = strtolower($setting['type']); } - + //set the content_type if ($message_type == 'mms' && isset($setting['message_media_content_type']) && !empty($setting['message_media_content_type'])) { $content_type = strtolower($setting['message_media_content_type']); @@ -287,15 +285,15 @@ function build_array($array, $path, $value) { if ($row['provider_setting_name'] == 'message_other') { $outbound_array = build_array($outbound_array ?? [], explode('=', $row['provider_setting_value'])[0], explode('=', $row['provider_setting_value'])[1]); } - if (is_array($message_media) && @sizeof($message_media) != 0) { + if (is_array($message_media) && @sizeof($message_media) != 0) { if ($row['provider_setting_name'] == 'message_media_other') { $outbound_array = build_array($outbound_array ?? [], explode('=', $row['provider_setting_value'])[0], explode('=', $row['provider_setting_value'])[1]); } - if ($row['provider_setting_name'] == 'message_media_url') { + if ($row['provider_setting_name'] == 'message_media_url') { foreach($message_media as $index => $media) { $outbound_array = build_array($outbound_array ?? [], $row['provider_setting_value'].".".$index, urldecode($media['message_media_url'])); - } + } } } } @@ -330,7 +328,7 @@ function build_array($array, $path, $value) { $y = 0; foreach($value as $v){ if ($y != 0) { $query_string .= '&'; } - $query_string .= $key.'='. urlencode($v); + $query_string .= $key.'='. urlencode($v); $y++; } } @@ -550,5 +548,3 @@ function build_array($array, $path, $value) { //how to use it // php /var/www/fusionpbx/app/messages/resources/service/message_send_outbound.php message_queue_uuid=39402652-1475-49f8-8366-7889335edd6f&hostname=voip.fusionpbx.com - -?> From a921234d10c510e273390404afb8b9fae330370e Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Wed, 12 Mar 2025 21:55:28 -0300 Subject: [PATCH 6/7] remove class_exists wrapper for class definitions --- resources/classes/messages.php | 45 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/resources/classes/messages.php b/resources/classes/messages.php index d004d14..4a17fa9 100644 --- a/resources/classes/messages.php +++ b/resources/classes/messages.php @@ -1,12 +1,12 @@ message_date_begin) || !empty($this->message_date_end)) { unset($this->quick_select); @@ -351,9 +351,9 @@ public function message_summary() { } } - $sql = "with \n"; + $sql = "with \n"; $sql .= "message_inbound as ( \n"; - $sql .= "select \n"; + $sql .= "select \n"; $sql .= "m.domain_uuid as domain_uuid, \n"; $sql .= "d.destination_uuid as destination_uuid, \n"; $sql .= "m.message_to as destination, \n"; @@ -375,7 +375,7 @@ public function message_summary() { $sql .= "0 as message_sent \n"; $sql .= "from v_messages m, v_destinations d \n"; - + if (!(!empty($_GET['show']) && $_GET['show'] === 'all' && permission_exists('message_summary_all'))) { $sql .= "where m.domain_uuid = :domain_uuid \n"; } @@ -389,10 +389,10 @@ public function message_summary() { $sql .= "), message_outbound as ( \n"; - $sql .= "select \n"; + $sql .= "select \n"; $sql .= "m.domain_uuid as domain_uuid, \n"; $sql .= "d.destination_uuid as destination_uuid, \n"; - $sql .= "m.message_from as destination, \n"; + $sql .= "m.message_from as destination, \n"; $sql .= "0 as message_read, \n"; $sql .= "0 as message_unread, \n"; $sql .= "0 as message_received, \n"; @@ -415,15 +415,15 @@ public function message_summary() { $sql .= ") \n"; $sql .= "select \n"; - $sql .= "n.domain_uuid, \n"; + $sql .= "n.domain_uuid, \n"; $sql .= "d.destination_uuid, \n"; $sql .= "d.destination_description, \n"; - $sql .= "n.domain_name, \n"; - $sql .= "t.destination, \n"; - $sql .= "sum(t.message_read) as message_read, \n"; - $sql .= "sum(t.message_unread) as message_unread, \n"; - $sql .= "sum(t.message_received) as message_received, \n"; - $sql .= "sum(t.message_sent) as message_sent \n"; + $sql .= "n.domain_name, \n"; + $sql .= "t.destination, \n"; + $sql .= "sum(t.message_read) as message_read, \n"; + $sql .= "sum(t.message_unread) as message_unread, \n"; + $sql .= "sum(t.message_received) as message_received, \n"; + $sql .= "sum(t.message_sent) as message_sent \n"; $sql .= "from ( \n"; $sql .= "select * from message_inbound \n"; $sql .= "union \n"; @@ -442,14 +442,11 @@ public function message_summary() { $database = database::new(); $summary = $database->select($sql, $parameters, 'all'); unset($parameters); - //view_array($database->message); - + //view_array($database->message); + //return the array return $summary; - + } //method - - } //class -} -?> + } //class From f4324f249410a693a61d2c46a37dcba5f5482dcf Mon Sep 17 00:00:00 2001 From: Tim Fry Date: Thu, 24 Jul 2025 00:27:34 -0300 Subject: [PATCH 7/7] Use the config and settings object --- resources/service/message_events.php | 65 ++++++++++++++++++---------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/resources/service/message_events.php b/resources/service/message_events.php index 9585713..c17f726 100644 --- a/resources/service/message_events.php +++ b/resources/service/message_events.php @@ -77,6 +77,23 @@ function process_exists($file = false) { return $exists; } + /** + * Reconnect to database to a max of "$tries". If the reconnect fails the script will exit with a non-zero status code + * @global database $database + * @param int $tries + * @return bool True when successfully reconnected and false when it failed + */ + function reconnected(int $tries = 3): bool { + global $database; + $count = 0; + while (!$database->is_connected() && $count++ < $tries) { + sleep(3); + //reconnect to the database + $database->connect(); + } + return $database->is_connected(); + } + //check to see if the process exists $pid_exists = process_exists($pid_file); @@ -100,18 +117,25 @@ function process_exists($file = false) { file_put_contents($pid_file, getmypid()); } +//create the config + $config = config::load(); + +//create the database connection + $database = database::new(); + +//get the global default settings + $settings = new settings(['database' => $database]); + + $event_socket_ip_address = $settings->get('switch', 'event_socket_ip_address', '127.0.0.1'); + $event_socket_port = $settings->get('switch', 'event_socket_port', '8021'); + $event_socket_password = $settings->get('switch', 'event_socket_password', 'ClueCon'); + //connect to event socket $socket = new event_socket; - if (!$socket->connect($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password'])) { + if (!$socket->connect($event_socket_ip_address, $event_socket_port, $event_socket_password)) { echo "Unable to connect to event socket\n"; } -//connect to event socket - $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']); - -//create the database connection - $database = new database; - //add the permission $p = permissions::new(); $p->add('message_queue_add', 'temp'); @@ -128,7 +152,7 @@ function process_exists($file = false) { $cmd = "event json MESSAGE"; //$cmd = "event json CUSTOM"; $result = $socket->request($cmd); - while ($socket) { + while ($socket->is_connected()) { //read events from socket $response = $socket->read_event(); @@ -141,25 +165,20 @@ function process_exists($file = false) { $message_type = 'sms'; //set variables from the event array - $event_name = $array['Event-Name']; - $event_type = $array['event_type']; - $event_subclass = $array['Event-Subclass']; $from = $array['from']; - $from_array = explode("@", $from); $from_user = $array['from_user']; $from_host = $array['from_host']; $to_user = $array['to_user']; $to_host = $array['to_host']; - $from_sip_ip = $array['from_sip_ip']; $message_content = $array['_body']; $to = $array['to_user']; //if the message is from an external number don't relay the message $from_command = "user_exists id ".$from_user." ".$from_host; - $from_response = event_socket_request($fp, "api ".$from_command); + $from_response = $socket->request("api ".$from_command); $to_command = "user_exists id ".$to_user." ".$to_host; - $to_response = event_socket_request($fp, "api ".$to_command); + $to_response = $socket->request("api ".$to_command); if ($debug) { echo "from command: ".$from_command."\n"; @@ -177,7 +196,7 @@ function process_exists($file = false) { //get the from user's external number $command = "user_data ".$from_user."@".$from_host." var outbound_caller_id_number"; //echo $command."\n"; - $source_number = event_socket_request($fp, "api ".$command); + $source_number = $socket->request("api ".$command); /* [from] => 1005@voip.fusionpbx.com @@ -202,8 +221,12 @@ function process_exists($file = false) { */ //reconnect to the database - if (!$database) { - $database = new database; + if (!$database->is_connected()) { + if (!reconnected()) { + echo "Unable to connect to database. Exiting...\n"; + //exit with non-zero status + exit(1); + } } //get the provider uuid - needed for sending the message @@ -337,9 +360,7 @@ function process_exists($file = false) { $p->delete('message_queue_add', 'temp'); //remove the old pid file - if (file_exists($file)) { - unlink($pid_file); - } + @unlink($pid_file); //save output to //$fp = fopen(sys_get_temp_dir()."/mailer-app.log", "a"); @@ -374,5 +395,3 @@ function process_exists($file = false) { //how to use this feature // cd /var/www/fusionpbx && /usr/bin/php /var/www/fusionpbx/app/messages/resources/service/message_events.php >> /dev/null 2>&1 & - -?>