Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions message_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

?>
45 changes: 21 additions & 24 deletions resources/classes/messages.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

//define the messages classs
if (!class_exists('messages')) {

class messages {

/**
* declare public variables
*/
/**
* declare public variables
*/
public $destinations;
public $domain_uuid;
public $message_date_begin;
Expand Down Expand Up @@ -319,7 +319,7 @@ public function message_summary() {

//set the time zone for php
date_default_timezone_set($time_zone);

//build the date range
if (!empty($this->message_date_begin) || !empty($this->message_date_end)) {
unset($this->quick_select);
Expand Down Expand Up @@ -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";
Expand All @@ -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";
}
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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
66 changes: 42 additions & 24 deletions resources/service/message_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -78,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);

Expand All @@ -101,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');
Expand All @@ -129,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();
Expand All @@ -142,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";
Expand All @@ -178,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
Expand All @@ -203,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
Expand Down Expand Up @@ -338,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");
Expand Down Expand Up @@ -375,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 &

?>
5 changes: 1 addition & 4 deletions resources/service/message_queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}
Expand Down Expand Up @@ -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

?>
6 changes: 1 addition & 5 deletions resources/service/message_send_inbound.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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

?>
24 changes: 10 additions & 14 deletions resources/service/message_send_outbound.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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);
Expand All @@ -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']);
Expand Down Expand Up @@ -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']));
}
}
}
}
}
Expand Down Expand Up @@ -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++;
}
}
Expand Down Expand Up @@ -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

?>