Add. Use fax queue to do fax forward.

This commit is contained in:
Alexey Melnichuk 2015-11-23 15:12:55 +03:00
parent 9f59353061
commit d5e63317d6
1 changed files with 346 additions and 112 deletions

View File

@ -37,6 +37,253 @@ if (defined('STDIN')) {
//echo "$document_root is document_root\n";
}
if (stristr(PHP_OS, 'WIN')) { $IS_WINDOWS = true; } else { $IS_WINDOWS = false; }
if(!function_exists('exec_in_dir')) {
function exec_in_dir($dir, $cmd, &$ok){
$args = func_get_args();
$cwd = getcwd();
chdir($dir);
$output = array();
$ret = 0;
$result = exec($cmd, $output, $ret);
if($cwd)
chdir($cwd);
$ok = ($ret == 0);
return join($output, "\n");
}
}
if(!function_exists('correct_path')) {
function correct_path($p) {
global $IS_WINDOWS;
if ($IS_WINDOWS) {
return str_replace('/', '\\', $p);
}
return $p;
}
}
if(!function_exists('path_join')) {
function path_join() {
$args = func_get_args();
$paths = array();
foreach ($args as $arg) {
$paths = array_merge($paths, (array)$arg);
}
$paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths);
$paths = array_filter($paths);
return join('/', $paths);
}
}
if(!function_exists('tiff2pdf')) {
function tiff2pdf($tiff_file_name){
//convert the tif to a pdf
//Ubuntu: apt-get install libtiff-tools
global $IS_WINDOWS;
if(!file_exists($tiff_file_name)){
echo "tiff file does not exists";
return false; // "tiff file does not exists";
}
$GS = $IS_WINDOWS ? 'gswin32c' : 'gs';
$tiff_file = pathinfo($tiff_file_name);
$dir_fax = $tiff_file['dirname'];
$fax_file_name = $tiff_file['filename'];
$pdf_file_name = path_join( $dir_fax, $fax_file_name . '.pdf' );
if(file_exists($pdf_file_name))
return $pdf_file_name;
$dir_fax_temp = $_SESSION['server']['temp']['dir'];
if(!$dir_fax_temp){
$dir_fax_temp = path_join(dirname($dir_fax), 'temp');
}
if(!file_exists($dir_fax_temp)){
echo"can not create temporary directory";
return false; //
}
$cmd = "tiffinfo " . correct_path($tiff_file_name) . ' | grep "Resolution:"';
$ok = false;
$resp = exec_in_dir($dir_fax, $cmd, $ok);
if(!$ok){
echo"can not find fax resoulution";
return false; // "can not find fax resoulution"
}
$ppi_w = 0;
$ppi_h = 0;
$tmp = array();
if(preg_match('/Resolution.*?(\d+).*?(\d+)/', $resp, $tmp)){
$ppi_w = $tmp[1];
$ppi_h = $tmp[2];
}
$cmd = "tiffinfo " . $tiff_file_name . ' | grep "Image Width:"';
$resp = exec_in_dir($dir_fax, $cmd, $ok);
if(!$ok){
echo"can not find fax size";
return false; // "can not find fax size"
}
$pix_w = 0;
$pix_h = 0;
$tmp = array();
if(preg_match('/Width.*?(\d+).*?Length.*?(\d+)/', $resp, $tmp)){
$pix_w = $tmp[1];
$pix_h = $tmp[2];
}
$page_width = $pix_w / $ppi_w;
$page_height = $pix_h / $ppi_h;
$page_size = 'a4';
if (($page_width > 8.4) && ($page_height > 13)) {
$page_width = 8.5;
$page_height = 14;
$page_size = 'legal';
}
elseif (($page_width > 8.4) && ($page_height < 12)) {
$page_width = 8.5;
$page_height = 11;
$page_size = 'letter';
}
elseif (($page_width < 8.4) && ($page_height > 11)) {
$page_width = 8.3;
$page_height = 11.7;
$page_size = 'a4';
}
$page_width = sprintf('%.4f', $page_width);
$page_height = sprintf('%.4f', $page_height);
$cmd = join(array('tiff2pdf',
'-i -u i',
'-p', $page_size,
'-w', $page_width,
'-l', $page_height,
'-f',
'-o', correct_path(path_join($dir_fax_temp, $fax_file_name . '.pdf')),
correct_path($tiff_file_name),
), ' ');
$resp = exec_in_dir($dir_fax, $cmd, $ok);
if(!file_exists(path_join($dir_fax_temp, $fax_file_name . '.pdf'))){
echo "can not create temporary pdf: $resp";
return false;
}
$cmd = join(array($GS,
'-q -sDEVICE=tiffg3',
'-r' . $ppi_w . 'x' . $ppi_h,
'-g' . $pix_w . 'x' . $pix_h,
'-dNOPAUSE',
'-sOutputFile=' . $fax_file_name . '_temp.tif',
'--',
$fax_file_name . '.pdf',
'-c quit',
), ' ');
$resp = exec_in_dir($dir_fax_temp, $cmd, $ok);
unlink(path_join($dir_fax_temp, $fax_file_name . '.pdf'));
if(!file_exists(path_join($dir_fax_temp, $fax_file_name . '_temp.tif'))){
echo "can not temporary tiff: $resp";
return false;
}
$cmd = join(array('tiff2pdf',
'-i -u i',
'-p', $page_size,
'-w', $page_width,
'-l', $page_height,
'-f',
'-o', correct_path($pdf_file_name),
correct_path(path_join($dir_fax_temp, $fax_file_name . '_temp.tif')),
), ' ');
$resp = exec_in_dir($dir_fax, $cmd, $ok);
unlink(path_join($dir_fax_temp, $fax_file_name . '_temp.tif'));
if(!file_exists($pdf_file_name)){
echo "can not create pdf: $resp";
return false;
}
return $pdf_file_name;
}
}
if(!function_exists('fax_enqueue')) {
function fax_enqueue($fax_uuid, $fax_file, $wav_file, $fax_uri, $fax_dtmf, $dial_string){
global $db, $db_type;
$task_uuid = uuid();
$dial_string .= "task_uuid='" . $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')";
}
$sql = <<<HERE
INSERT INTO v_fax_tasks( task_uuid, fax_uuid,
task_next_time, task_lock_time,
task_fax_file, task_wav_file, task_uri, task_dial_string, task_dtmf,
task_interrupted, task_status, task_no_answer_counter, task_no_answer_retry_counter, task_retry_counter,
task_description)
VALUES (?, ?,
$date_utc_now_sql, NULL,
?, ?, ?, ?, ?,
'false', 0, 0, 0, 0,
?);
HERE;
$stmt = $db->prepare($sql);
$i = 0;
$stmt->bindValue(++$i, $task_uuid);
$stmt->bindValue(++$i, $fax_uuid);
$stmt->bindValue(++$i, $fax_file);
$stmt->bindValue(++$i, $wav_file);
$stmt->bindValue(++$i, $fax_uri);
$stmt->bindValue(++$i, $dial_string);
$stmt->bindValue(++$i, $fax_dtmf);
$stmt->bindValue(++$i, $description);
if ($stmt->execute()) {
$response = 'Enqueued';
}
else{
//! @todo log error
$response = 'Fail enqueue';
var_dump($db->errorInfo());
}
unset($stmt);
return $response;
}
}
if(!function_exists('fax_split_dtmf')) {
function fax_split_dtmf(&$fax_number, &$fax_dtmf){
$tmp = array();
$fax_dtmf = '';
if(preg_match('/^\s*(.*?)\s*\((.*)\)\s*$/', $fax_number, $tmp)){
$fax_number = $tmp[1];
$fax_dtmf = $tmp[2];
}
}
}
//includes
if (!defined('STDIN')) { include "root.php"; }
require_once "resources/require.php";
@ -115,9 +362,11 @@ if (defined('STDIN')) {
echo "fax_email is ".$fax_email."\n";
//get the fax file name (only) if a full path
$fax_file = realpath($fax_file);
$array = explode("/", $fax_file);
$fax_file_only = $array[count($array)-1];
$fax_file_name = pathinfo($fax_file_only, PATHINFO_FILENAME);
$dir_fax = pathinfo($fax_file, PATHINFO_DIRNAME);
unset($array);
//used for debug
@ -153,6 +402,7 @@ if (defined('STDIN')) {
foreach ($result as &$row) {
//set database fields as variables
//$fax_email = $row["fax_email"];
$fax_uuid = $row["fax_uuid"];
$fax_accountcode = $row["fax_accountcode"];
$fax_pin_number = $row["fax_pin_number"];
$fax_caller_id_name = $row["fax_caller_id_name"];
@ -164,124 +414,108 @@ if (defined('STDIN')) {
unset ($prep_statement);
//set the fax directory
$dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$domain_name.'/'.$fax_extension.'/inbox';
echo "dir_fax is $dir_fax\n";
if (!file_exists($dir_fax)) {
$dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$fax_extension.'/inbox';
}
//convert the tif to a pdf
//Ubuntu: apt-get install libtiff-tools
$fax_file_warning = "";
if (file_exists($dir_fax.'/'.$fax_file_name.".tif")) {
if (!file_exists($dir_fax.'/'.$fax_file_name.".pdf")) {
//define temp directory
$dir_fax_temp = str_replace('/inbox', '/temp', $dir_fax);
if (!is_dir($dir_fax_temp)) {
mkdir($dir_fax_temp,0774,true);
chmod($dir_fax_temp,0774);
}
//enter fax directory
chdir($dir_fax);
//get fax resolution (ppi, W & H)
$resp = exec("tiffinfo ".$fax_file_name.".tif | grep 'Resolution:'");
$resp_array = explode(' ', trim($resp));
$ppi_w = (int) $resp_array[1];
$ppi_h = (int) $resp_array[2];
unset($resp_array);
$gs_r = $ppi_w.'x'.$ppi_h; //used by ghostscript
//get page dimensions/size (pixels/inches, W & H)
$resp = exec("tiffinfo ".$fax_file_name.".tif | grep 'Image Width:'");
$resp_array = explode(' ', trim($resp));
$pix_w = $resp_array[2];
$pix_h = $resp_array[5];
unset($resp_array);
$gs_g = $pix_w.'x'.$pix_h; //used by ghostscript
$page_width = $pix_w / $ppi_w;
$page_height = $pix_h / $ppi_h;
if ($page_width > 8.4 && $page_height > 13) {
$page_width = 8.5;
$page_height = 14;
$page_size = 'legal';
}
else if ($page_width > 8.4 && $page_height < 12) {
$page_width = 8.5;
$page_height = 11;
$page_size = 'letter';
}
else if ($page_width < 8.4 && $page_height > 11) {
$page_width = 8.3;
$page_height = 11.7;
$page_size = 'a4';
}
//generate pdf (a work around, as tiff2pdf improperly inverts the colors)
$cmd_tif2pdf = "tiff2pdf -i -u i -p ".$page_size." -w ".$page_width." -l ".$page_height." -f -o ".$dir_fax_temp.'/'.$fax_file_name.".pdf ".$dir_fax.'/'.$fax_file_name.".tif";
exec($cmd_tif2pdf);
chdir($dir_fax_temp);
$cmd_pdf2tif = "gs -q -sDEVICE=tiffg3 -r".$gs_r." -g".$gs_g." -dNOPAUSE -sOutputFile=".$fax_file_name."_temp.tif -- ".$fax_file_name.".pdf -c quit";
exec($cmd_pdf2tif); //convert pdf to tif
@unlink($dir_fax_temp.'/'.$fax_file_name.".pdf");
$cmd_tif2pdf = "tiff2pdf -i -u i -p ".$page_size." -w ".$page_width." -l ".$page_height." -f -o ".$dir_fax.'/'.$fax_file_name.".pdf ".$dir_fax_temp.'/'.$fax_file_name."_temp.tif";
exec($cmd_tif2pdf);
@unlink($dir_fax_temp.'/'.$fax_file_name."_temp.tif");
$dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$domain_name.'/'.$fax_extension.'/inbox';
echo "dir_fax is $dir_fax\n";
if (!file_exists($dir_fax)) {
$dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$fax_extension.'/inbox';
}
}
else {
$fax_file_warning = " Fax image not available on server.";
echo $fax_file_warning."<br>";
$pdf_file = tiff2pdf($fax_file);
if(!$pdf_file){
$fax_file_warning = ' Fax image not available on server.';
}
//forward the fax
if (strpos($fax_file_name,'#') !== false) {
$tmp = explode("#",$fax_file_name);
$fax_forward_number = $tmp[0];
}
if(file_exists($fax_file)) {
if (strpos($fax_file_name,'#') !== false) {
$tmp = explode("#",$fax_file_name);
$fax_forward_number = $tmp[0];
}
echo "fax_forward_number is $fax_forward_number\n";
if (strlen($fax_forward_number) > 0) {
if (file_exists($dir_fax."/".$fax_file_name.".tif")) {
//get the event socket information
$sql = "select * from v_settings ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as &$row) {
$event_socket_ip_address = $row["event_socket_ip_address"];
$event_socket_port = $row["event_socket_port"];
$event_socket_password = $row["event_socket_password"];
break;
}
//create the event socket connection
$fp = event_socket_create($event_socket_ip_address, $event_socket_port, $event_socket_password);
//send the command with event socket
if ($fp) {
//prepare the fax originate command
$route_array = outbound_route_to_bridge($_SESSION['domain_uuid'], $fax_forward_number);
$fax_file = $dir_fax."/".$fax_file_name.".tif";
if (count($route_array) == 0) {
//send the internal call to the registered extension
$fax_uri = "user/".$fax_forward_number."@".$domain_name;
$t38 = "";
}
else {
//send the external call
$fax_uri = $route_array[0];
$t38 = "fax_enable_t38=true,fax_enable_t38_request=true";
}
$cmd = "api originate {absolute_codec_string='PCMU,PCMA',accountcode='".$fax_accountcode."',sip_h_X-accountcode='".$fax_accountcode."',domain_uuid=".$_SESSION["domain_uuid"].",domain_name=".$_SESSION["domain_name"].",mailto_address='".$mailto_address."',mailfrom_address='".$mailfrom_address."',origination_caller_id_name='".$fax_caller_id_name."',origination_caller_id_number=".$fax_caller_id_number.",fax_uri=".$fax_uri.",fax_file='".$fax_file."',fax_retry_attempts=1,fax_retry_limit=20,fax_retry_sleep=180,fax_verbose=true,fax_use_ecm=off,".$t38.",api_hangup_hook='lua fax_retry.lua'}".$fax_uri." &txfax('".$fax_file."')";
//send info to the log
echo "fax forward\n";
echo $cmd."\n";
//send the command to event socket
$response = event_socket_request($fp, $cmd);
$response = str_replace("\n", "", $response);
//send info to the log
echo "response: ".$response."\n";
//get the uuid
$uuid = str_replace("+OK ", "", $response);
//close event socket
fclose($fp);
}
echo "fax_forward_number is $fax_forward_number\n";
if (strlen($fax_forward_number) > 0) {
fax_split_dtmf($fax_forward_number, $fax_dtmf);
$fax_send_mode = $_SESSION['fax']['send_mode']['text'];
if(strlen($fax_send_mode) == 0){
$fax_send_mode = 'direct';
}
$route_array = outbound_route_to_bridge($_SESSION['domain_uuid'], $fax_forward_number);
if (count($route_array) == 0) {
//send the internal call to the registered extension
$fax_uri = "user/".$fax_forward_number."@".$domain_name;
$t38 = "";
}
else {
//send the external call
$fax_uri = $route_array[0];
$t38 = "fax_enable_t38=true,fax_enable_t38_request=true";
}
$common_dial_string = "absolute_codec_string='PCMU,PCMA',";
$common_dial_string .= "accountcode='" . $fax_accountcode . "',";
$common_dial_string .= "sip_h_X-accountcode='" . $fax_accountcode . "',";
$common_dial_string .= "domain_uuid=" . $_SESSION["domain_uuid"] . ",";
$common_dial_string .= "domain_name=" . $_SESSION["domain_name"] . ",";
$common_dial_string .= "mailto_address='" . $mailto_address . "',";
$common_dial_string .= "mailfrom_address='" . $mailfrom_address . "',";
$common_dial_string .= "origination_caller_id_name='" . $fax_caller_id_name . "',";
$common_dial_string .= "origination_caller_id_number='" . $fax_caller_id_number . "',";
$common_dial_string .= "fax_ident='" . $fax_caller_id_number . "',";
$common_dial_string .= "fax_header='" . $fax_caller_id_name . "',";
$common_dial_string .= "fax_file='" . $fax_file . "',";
if ($fax_send_mode != 'queue') {
$dial_string .= $t38;
$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."')";
//get the event socket information
$sql = "select * from v_settings ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as &$row) {
$event_socket_ip_address = $row["event_socket_ip_address"];
$event_socket_port = $row["event_socket_port"];
$event_socket_password = $row["event_socket_password"];
break;
}
//create the event socket connection
$fp = event_socket_create($event_socket_ip_address, $event_socket_port, $event_socket_password);
//send the command with event socket
if ($fp) {
//prepare the fax originate command
$cmd = "api originate " . $dial_string;
//send info to the log
echo "fax forward\n";
echo $cmd."\n";
//send the command to event socket
$response = event_socket_request($fp, $cmd);
$response = str_replace("\n", "", $response);
//send info to the log
echo "response: ".$response."\n";
//get the uuid
$uuid = str_replace("+OK ", "", $response);
//close event socket
fclose($fp);
}
}
else{
$wav_file = '';
$response = fax_enqueue($fax_uuid, $fax_file, $wav_file, $fax_uri, $fax_dtmf, $dial_string);
}
}
}
@ -413,4 +647,4 @@ if (defined('STDIN')) {
fwrite($fp, $content);
fclose($fp);
?>
?>