Add a new FAX Queue

This commit is contained in:
markjcrane
2022-03-31 12:24:00 -06:00
parent 504228c2f8
commit 35fa6a2a08
12 changed files with 2537 additions and 200 deletions
+170 -200
View File
@@ -17,7 +17,7 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2021
Portions created by the Initial Developer are Copyright (C) 2008-2022
the Initial Developer. All Rights Reserved.
Contributor(s):
@@ -87,13 +87,13 @@ if (!$included) {
$row = $database->select($sql, $parameters, 'row');
if (is_array($row) && @sizeof($row) != 0) {
//set database fields as variables
$fax_uuid = $row["fax_uuid"];
$fax_extension = $row["fax_extension"];
$fax_caller_id_name = $row["fax_caller_id_name"];
$fax_caller_id_number = $row["fax_caller_id_number"];
$fax_toll_allow = $row["fax_toll_allow"];
$fax_accountcode = $row["accountcode"];
$fax_send_greeting = $row["fax_send_greeting"];
$fax_uuid = $row["fax_uuid"];
$fax_extension = $row["fax_extension"];
$fax_caller_id_name = $row["fax_caller_id_name"];
$fax_caller_id_number = $row["fax_caller_id_number"];
$fax_toll_allow = $row["fax_toll_allow"];
$fax_accountcode = $row["accountcode"];
$fax_send_greeting = $row["fax_send_greeting"];
}
else {
if (!permission_exists('fax_extension_view_domain')) {
@@ -102,11 +102,6 @@ if (!$included) {
}
}
unset($sql, $parameters, $row);
$fax_send_mode = $_SESSION['fax']['send_mode']['text'];
if(strlen($fax_send_mode) == 0){
$fax_send_mode = 'direct';
}
}
//set the fax directory
@@ -139,69 +134,6 @@ if (!function_exists('gs_cmd')) {
}
}
if (!function_exists('fax_enqueue')) {
function fax_enqueue($fax_uuid, $fax_file, $wav_file, $reply_address, $fax_uri, $fax_dtmf, $dial_string){
global $db_type;
$fax_task_uuid = uuid();
$dial_string .= "fax_task_uuid='" . $fax_task_uuid . "',";
$description = ''; //! @todo add description
if ($db_type == "pgsql") {
$date_utc_now_sql = "NOW() at time zone 'utc'";
}
if ($db_type == "mysql") {
$date_utc_now_sql = "UTC_TIMESTAMP()";
}
if ($db_type == "sqlite") {
$date_utc_now_sql = "datetime('now')";
}
$array['fax_tasks'][0]['fax_task_uuid'] = $fax_task_uuid;
$array['fax_tasks'][0]['fax_uuid'] = $fax_uuid;
$array['fax_tasks'][0]['task_next_time'] = $date_utc_now_sql;
$array['fax_tasks'][0]['task_lock_time'] = null;
$array['fax_tasks'][0]['task_fax_file'] = $fax_file;
$array['fax_tasks'][0]['task_wav_file'] = $wav_file;
$array['fax_tasks'][0]['task_uri'] = $fax_uri;
$array['fax_tasks'][0]['task_dial_string'] = $dial_string;
$array['fax_tasks'][0]['task_dtmf'] = $fax_dtmf;
$array['fax_tasks'][0]['task_interrupted'] = 'false';
$array['fax_tasks'][0]['task_status'] = 0;
$array['fax_tasks'][0]['task_no_answer_counter'] = 0;
$array['fax_tasks'][0]['task_no_answer_retry_counter'] = 0;
$array['fax_tasks'][0]['task_retry_counter'] = 0;
$array['fax_tasks'][0]['task_reply_address'] = $reply_address;
$array['fax_tasks'][0]['task_description'] = $description;
$p = new permissions;
$p->add('fax_task_add', 'temp');
$database = new database;
$database->app_name = 'fax';
$database->app_uuid = '24108154-4ac3-1db6-1551-4731703a4440';
$database->save($array);
$message = $database->message;
unset($array);
$p->delete('fax_task_add', 'temp');
if ($message['message'] == 'OK' && $message['code'] == 200) {
$response = 'Enqueued';
}
else {
$response = 'Fail Enqueue';
echo $message['message'].' ['.$message['code']."]<br />\n";
if (is_array($message['error']) && @sizeof($message['error']) != 0) {
foreach ($message['error'] as $error) {
echo "<pre>".$error."</pre><br /><br />\n";
}
}
}
return $response;
}
}
if (!function_exists('fax_split_dtmf')) {
function fax_split_dtmf(&$fax_number, &$fax_dtmf){
$tmp = array();
@@ -251,18 +183,19 @@ if (!function_exists('fax_split_dtmf')) {
$continue = false;
if (!$included) {
if (($_POST['action'] == "send")) {
$fax_numbers = $_POST['fax_numbers'];
$fax_uuid = $_POST["id"];
$fax_caller_id_name = $_POST['fax_caller_id_name'];
$fax_caller_id_number = $_POST['fax_caller_id_number'];
$fax_header = $_POST['fax_header'];
$fax_sender = $_POST['fax_sender'];
$fax_recipient = $_POST['fax_recipient'];
$fax_subject = $_POST['fax_subject'];
$fax_message = $_POST['fax_message'];
$fax_resolution = $_POST['fax_resolution'];
$fax_page_size = $_POST['fax_page_size'];
$fax_footer = $_POST['fax_footer'];
//Get values from the HTTP POST to set as variables
$fax_numbers = $_POST['fax_numbers'];
$fax_uuid = $_POST["id"];
$fax_caller_id_name = $_POST['fax_caller_id_name'];
$fax_caller_id_number = $_POST['fax_caller_id_number'];
$fax_header = $_POST['fax_header'];
$fax_sender = $_POST['fax_sender'];
$fax_recipient = $_POST['fax_recipient'];
$fax_subject = $_POST['fax_subject'];
$fax_message = $_POST['fax_message'];
$fax_resolution = $_POST['fax_resolution'];
$fax_page_size = $_POST['fax_page_size'];
$fax_footer = $_POST['fax_footer'];
//validate the token
$token = new token;
@@ -577,20 +510,20 @@ if (!function_exists('fax_split_dtmf')) {
$pages = $pdf->getNumPages();
if ($pages > 1) {
# save ynew for last page
//save ynew for last page
$yn = $pdf->GetY();
# First page
//first page
$pdf->setPage(1, 0);
$pdf->Rect($x + 0.5, $y + 3.4, 7.5, $page_height - 3.9, 'D');
# 2nd to N-th page
//2nd to n-th page
for ($n = 2; $n < $pages; $n++) {
$pdf->setPage($n, 0);
$pdf->Rect($x + 0.5, $y + 0.5, 7.5, $page_height - 1, 'D');
}
#Last page
//last page
$pdf->setPage($pages, 0);
$pdf->Rect($x + 0.5, 0.5, 7.5, $yn, 'D');
$y = $yn;
@@ -612,7 +545,7 @@ if (!function_exists('fax_split_dtmf')) {
$pdf->SetAutoPageBreak(false);
$pdf->SetTopMargin(0);
// save cover pdf
//save cover pdf
$pdf->Output($dir_fax_temp.'/'.$fax_instance_uuid.'_cover.pdf', "F"); // Display [I]nline, Save to [F]ile, [D]ownload
//convert pdf to tif, add to array of pages, delete pdf
@@ -727,16 +660,17 @@ if (!function_exists('fax_split_dtmf')) {
//send the fax
$fax_file = $dir_fax_temp."/".$fax_instance_uuid.".tif";
$tmp_dial_string = "for_fax=1,";
$tmp_dial_string .= "accountcode='" . $fax_accountcode . "',";
$tmp_dial_string .= "sip_h_X-accountcode='" . $fax_accountcode . "',";
$tmp_dial_string .= "domain_uuid=" . $_SESSION["domain_uuid"] . ",";
$tmp_dial_string .= "domain_name=" . $_SESSION["domain_name"] . ",";
$tmp_dial_string .= "origination_caller_id_name='" . $fax_caller_id_name . "',";
$tmp_dial_string .= "origination_caller_id_number='" . $fax_caller_id_number . "',";
$tmp_dial_string .= "fax_ident='" . $fax_caller_id_number . "',";
$tmp_dial_string .= "fax_header='" . $fax_caller_id_name . "',";
$tmp_dial_string .= "fax_file='" . $fax_file . "',";
$common_variables = "for_fax=1,";
$common_variables .= "accountcode='" . $fax_accountcode . "',";
$common_variables .= "sip_h_X-accountcode='" . $fax_accountcode . "',";
$common_variables .= "domain_uuid=" . $_SESSION["domain_uuid"] . ",";
$common_variables .= "domain_name=" . $_SESSION["domain_name"] . ",";
$common_variables .= "origination_caller_id_name='" . $fax_caller_id_name . "',";
$common_variables .= "origination_caller_id_number='" . $fax_caller_id_number . "',";
$common_variables .= "fax_ident='" . $fax_caller_id_number . "',";
$common_variables .= "fax_header='" . $fax_caller_id_name . "',";
$common_variables .= "fax_file='" . $fax_file . "',";
foreach ($fax_numbers as $fax_number) {
$fax_number = trim($fax_number);
@@ -761,40 +695,67 @@ if (!function_exists('fax_split_dtmf')) {
}
}
if ($fax_send_mode != 'queue') {
$dial_string = $tmp_dial_string;
$dial_string .= $fax_variables;
$dial_string .= "mailto_address='" . $mailto_address . "',";
$dial_string .= "mailfrom_address='" . $mailfrom_address . "',";
$dial_string .= "fax_uri=" . $fax_uri . ",";
$dial_string .= "fax_retry_attempts=1" . ",";
$dial_string .= "fax_retry_limit=20" . ",";
$dial_string .= "fax_retry_sleep=180" . ",";
$dial_string .= "fax_verbose=true" . ",";
$dial_string .= "fax_use_ecm=off" . ",";
$dial_string .= "api_hangup_hook='lua fax_retry.lua'";
$dial_string = "{" . $dial_string . "}" . $fax_uri." &txfax('".$fax_file."')";
//build the fax dial string
$dial_string = $common_variables;
$dial_string .= $fax_variables;
$dial_string .= "mailto_address='" . $mailto_address . "',";
$dial_string .= "mailfrom_address='" . $mailfrom_address . "',";
$dial_string .= "fax_uri=" . $fax_uri . ",";
$dial_string .= "fax_retry_attempts=1" . ",";
$dial_string .= "fax_retry_limit=20" . ",";
$dial_string .= "fax_retry_sleep=180" . ",";
$dial_string .= "fax_verbose=true" . ",";
$dial_string .= "fax_use_ecm=off" . ",";
$dial_string .= "api_hangup_hook='lua fax_retry.lua'";
$dial_string = "{" . $dial_string . "}" . $fax_uri." &txfax('".$fax_file."')";
//add fax to the fax queue or send it directly
if (isset($_SESSION['fax']['send_mode']['text']) && $_SESSION['fax']['send_mode']['text'] == 'queue') {
//build an array to add the fax to the queue
$fax_queue_uuid = uuid();
$array['fax_queue'][0]['fax_queue_uuid'] = $fax_queue_uuid;
$array['fax_queue'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
$array['fax_queue'][0]['fax_uuid'] = $fax_uuid;
$array['fax_queue'][0]['fax_date'] = 'now()';
$array['fax_queue'][0]['hostname'] = gethostname();
$array['fax_queue'][0]['fax_caller_id_name'] = $fax_caller_id_name;
$array['fax_queue'][0]['fax_caller_id_number'] = $fax_caller_id_number;
$array['fax_queue'][0]['fax_number'] = $fax_number;
$array['fax_queue'][0]['fax_prefix'] = $fax_prefix;
$array['fax_queue'][0]['fax_email_address'] = $mailto_address;
$array['fax_queue'][0]['fax_file'] = $fax_file;
$array['fax_queue'][0]['fax_status'] = 'waiting';
//$array['fax_queue'][0]['fax_retry_date'] = $fax_retry_date;
$array['fax_queue'][0]['fax_retry_count'] = 0;
$array['fax_queue'][0]['fax_accountcode'] = $fax_accountcode;
$array['fax_queue'][0]['fax_command'] = 'originate '.$dial_string;
//add temporary permisison
$p = new permissions;
$p->add('fax_queue_add', 'temp');
//save the data
$database = new database;
$database->app_name = 'fax queue';
$database->app_uuid = '3656287f-4b22-4cf1-91f6-00386bf488f4';
$database->save($array);
//remove temporary permisison
$p->delete('fax_queue_add', 'temp');
}
else {
//send the fax directly
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp) {
$cmd = "api originate " . $dial_string;
//send the command to event socket
$response = event_socket_request($fp, $cmd);
$response = str_replace("\n", "", $response);
$uuid = str_replace("+OK ", "", $response);
}
fclose($fp);
}
else { // enqueue
$wav_file = ''; //! @todo add custom message
$dial_string = $tmp_dial_string;
$response = fax_enqueue($fax_uuid, $fax_file, $wav_file, $mailto_address, $fax_uri, $fax_dtmf, $dial_string);
}
}
//wait for a few seconds
sleep(5);
//move the generated tif (and pdf) files to the sent directory
if (file_exists($dir_fax_temp.'/'.$fax_instance_uuid.".tif")) {
copy($dir_fax_temp.'/'.$fax_instance_uuid.".tif", $dir_fax_sent.'/'.$fax_instance_uuid.".tif");
@@ -804,11 +765,11 @@ if (!function_exists('fax_split_dtmf')) {
copy($dir_fax_temp.'/'.$fax_instance_uuid.".pdf ", $dir_fax_sent.'/'.$fax_instance_uuid.".pdf");
}
//redirect the browser
if (!$included && is_uuid($fax_uuid)) {
//redirect the browser
message::add($response, 'default');
if (isset($_SESSION['fax']['send_mode']['text']) && $_SESSION['fax']['send_mode']['text'] == 'queue') {
header("Location: fax_active.php?id=".$fax_uuid);
header("Location: ".PROJECT_PATH."/app/fax_queue/fax_queue.php?id=".$fax_uuid);
}
else {
header("Location: fax_files.php?id=".$fax_uuid."&box=sent");
@@ -822,6 +783,84 @@ if (!function_exists('fax_split_dtmf')) {
if (!$included) {
//retrieve current user's assigned groups (uuids)
foreach ($_SESSION['groups'] as $group_data) {
$user_group_uuids[] = $group_data['group_uuid'];
}
//add user's uuid to group uuid list to include private (non-shared) contacts
$user_group_uuids[] = $_SESSION["user_uuid"];
$sql = "select ";
$sql .= "c.contact_organization, ";
$sql .= "c.contact_name_given, ";
$sql .= "c.contact_name_family, ";
$sql .= "c.contact_nickname, ";
$sql .= "cp.phone_number ";
$sql .= "from ";
$sql .= "v_contacts as c, ";
$sql .= "v_contact_phones as cp ";
$sql .= "where ";
$sql .= "c.contact_uuid = cp.contact_uuid ";
$sql .= "and c.domain_uuid = :domain_uuid ";
$sql .= "and cp.domain_uuid = :domain_uuid ";
$sql .= "and cp.phone_type_fax = 1 ";
$sql .= "and cp.phone_number is not null ";
$sql .= "and cp.phone_number <> '' ";
if ($_SESSION['contact']['permissions']['boolean'] == "true") {
if (is_array($user_group_uuids) && @sizeof($user_group_uuids) != 0) {
//only show contacts assigned to current user's group(s) and those not assigned to any group
$sql .= "and (";
$sql .= " c.contact_uuid in ( ";
$sql .= " select contact_uuid from v_contact_groups ";
$sql .= " where (";
foreach ($user_group_uuids as $index => $user_group_uuid) {
$sql .= $or;
$sql .= " group_uuid = :group_uuid_".$index." ";
$parameters['group_uuid_'.$index] = $user_group_uuid;
$or = " or ";
}
unset($user_group_uuids, $index, $user_group_uuid, $or);
$sql .= " ) ";
$sql .= " and domain_uuid = :domain_uuid ";
$sql .= " ) ";
$sql .= " or ";
$sql .= " c.contact_uuid not in ( ";
$sql .= " select contact_uuid from v_contact_groups ";
$sql .= " where domain_uuid = :domain_uuid ";
$sql .= " ) ";
$sql .= ") ";
}
}
$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$database = new database;
$contacts = $database->select($sql, $parameters, 'all');
unset($sql, $parameters, $row);
//build the contact labels
if (is_array($contacts) && @sizeof($contacts) != 0) {
foreach ($contacts as &$row) {
if ($row['contact_organization'] != '') {
$contact_option_label = $row['contact_organization'];
}
if ($row['contact_name_given'] != '' || $row['contact_name_family'] != '' || $row['contact_nickname'] != '') {
$contact_option_label .= ($row['contact_organization'] != '') ? "," : null;
$contact_option_label .= ($row['contact_name_given'] != '') ? (($row['contact_organization'] != '') ? " " : null).$row['contact_name_given'] : null;
$contact_option_label .= ($row['contact_name_family'] != '') ? (($row['contact_organization'] != '' || $row['contact_name_given'] != '') ? " " : null).$row['contact_name_family'] : null;
$contact_option_label .= ($row['contact_nickname'] != '') ? (($row['contact_organization'] != '' || $row['contact_name_given'] != '' || $row['contact_name_family'] != '') ? " (".$row['contact_nickname'].")" : $row['contact_nickname']) : null;
}
$contact_option_value_recipient = $contact_option_label;
$contact_option_value_faxnumber = $row['phone_number'];
$contact_option_label .= " ".escape(format_phone($row['phone_number']));
$contact_labels[] = $contact_option_label;
$contact_values[] = $contact_option_value_faxnumber."|".$contact_option_value_recipient;
unset($contact_option_label);
}
if (is_array($contact_labels)) {
//sort by name(s)
asort($contact_labels, SORT_NATURAL);
}
}
//create token
$object = new token;
$token = $object->create($_SERVER['PHP_SELF']);
@@ -921,85 +960,16 @@ if (!$included) {
echo " ".$text['label-fax-recipient']."\n";
echo "</td>\n";
echo "<td class='vtable' align='left'>\n";
//retrieve current user's assigned groups (uuids)
foreach ($_SESSION['groups'] as $group_data) {
$user_group_uuids[] = $group_data['group_uuid'];
}
//add user's uuid to group uuid list to include private (non-shared) contacts
$user_group_uuids[] = $_SESSION["user_uuid"];
$sql = "select ";
$sql .= "c.contact_organization, ";
$sql .= "c.contact_name_given, ";
$sql .= "c.contact_name_family, ";
$sql .= "c.contact_nickname, ";
$sql .= "cp.phone_number ";
$sql .= "from ";
$sql .= "v_contacts as c, ";
$sql .= "v_contact_phones as cp ";
$sql .= "where ";
$sql .= "c.contact_uuid = cp.contact_uuid ";
$sql .= "and c.domain_uuid = :domain_uuid ";
$sql .= "and cp.domain_uuid = :domain_uuid ";
$sql .= "and cp.phone_type_fax = 1 ";
$sql .= "and cp.phone_number is not null ";
$sql .= "and cp.phone_number <> '' ";
if ($_SESSION['contact']['permissions']['boolean'] == "true") {
if (is_array($user_group_uuids) && @sizeof($user_group_uuids) != 0) {
//only show contacts assigned to current user's group(s) and those not assigned to any group
$sql .= "and (";
$sql .= " c.contact_uuid in ( ";
$sql .= " select contact_uuid from v_contact_groups ";
$sql .= " where (";
foreach ($user_group_uuids as $index => $user_group_uuid) {
$sql .= $or;
$sql .= " group_uuid = :group_uuid_".$index." ";
$parameters['group_uuid_'.$index] = $user_group_uuid;
$or = " or ";
}
unset($user_group_uuids, $index, $user_group_uuid, $or);
$sql .= " ) ";
$sql .= " and domain_uuid = :domain_uuid ";
$sql .= " ) ";
$sql .= " or ";
$sql .= " c.contact_uuid not in ( ";
$sql .= " select contact_uuid from v_contact_groups ";
$sql .= " where domain_uuid = :domain_uuid ";
$sql .= " ) ";
$sql .= ") ";
}
}
$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$database = new database;
$contacts = $database->select($sql, $parameters, 'all');
if (is_array($contacts) && @sizeof($contacts) != 0) {
foreach ($contacts as &$row) {
if ($row['contact_organization'] != '') {
$contact_option_label = $row['contact_organization'];
}
if ($row['contact_name_given'] != '' || $row['contact_name_family'] != '' || $row['contact_nickname'] != '') {
$contact_option_label .= ($row['contact_organization'] != '') ? "," : null;
$contact_option_label .= ($row['contact_name_given'] != '') ? (($row['contact_organization'] != '') ? " " : null).$row['contact_name_given'] : null;
$contact_option_label .= ($row['contact_name_family'] != '') ? (($row['contact_organization'] != '' || $row['contact_name_given'] != '') ? " " : null).$row['contact_name_family'] : null;
$contact_option_label .= ($row['contact_nickname'] != '') ? (($row['contact_organization'] != '' || $row['contact_name_given'] != '' || $row['contact_name_family'] != '') ? " (".$row['contact_nickname'].")" : $row['contact_nickname']) : null;
}
$contact_option_value_recipient = $contact_option_label;
$contact_option_value_faxnumber = $row['phone_number'];
$contact_option_label .= ":&nbsp;&nbsp;".escape(format_phone($row['phone_number']));
$contact_labels[] = $contact_option_label;
$contact_values[] = $contact_option_value_faxnumber."|".$contact_option_value_recipient;
unset($contact_option_label);
}
if (is_array($contact_labels)) {
asort($contact_labels, SORT_NATURAL); // sort by name(s)
}
echo " <select class='formfld' style='display: none;' id='fax_recipient_select' onchange='contact_load(this);'>\n";
echo " <option value=''></option>\n";
foreach ($contact_labels as $index => $contact_label) {
echo " <option value=\"".escape($contact_values[$index])."\">".$contact_label."</option>\n";
if (is_array($contact_labels) && @sizeof($contact_labels) != 0) {
foreach ($contact_labels as $index => $contact_label) {
echo " <option value=\"".escape($contact_values[$index])."\">".$contact_label."</option>\n";
}
}
echo " </select>\n";
}
unset($sql, $parameters, $row);
echo " <input type='text' name='fax_recipient' id='fax_recipient' class='formfld' style='max-width: 250px;' value=''>\n";
if (is_array($contacts)) {
echo " <input type='button' id='btn_toggle_recipient' class='btn' name='' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle('fax_recipient');\">\n";