made fax relay compatible with new fax_retry.lua

This commit is contained in:
James Rose 2013-01-08 20:39:27 +00:00
parent 3281448254
commit 8ef0f8f08f
1 changed files with 362 additions and 340 deletions

View File

@ -1,340 +1,362 @@
<?php <?php
/* /*
FusionPBX FusionPBX
Version: MPL 1.1 Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with 1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/ http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis, Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the for the specific language governing rights and limitations under the
License. License.
The Original Code is FusionPBX The Original Code is FusionPBX
The Initial Developer of the Original Code is The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2012 Portions created by the Initial Developer are Copyright (C) 2008-2012
the Initial Developer. All Rights Reserved. the Initial Developer. All Rights Reserved.
Contributor(s): Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
*/ James Rose <james.o.rose@gmail.com>
*/
if (defined('STDIN')) {
//get the document root php file must be executed with the full path if (defined('STDIN')) {
$document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]); //get the document root php file must be executed with the full path
$document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]); $document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]);
preg_match("/^(.*)\/secure\/.*$/", $document_root, $matches); $document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]);
$document_root = $matches[1]; preg_match("/^(.*)\/secure\/.*$/", $document_root, $matches);
//set the include path $document_root = $matches[1];
set_include_path($document_root); //set the include path
$_SERVER["DOCUMENT_ROOT"] = $document_root; set_include_path($document_root);
} $_SERVER["DOCUMENT_ROOT"] = $document_root;
//echo "$document_root is document_root\n";
//includes }
if (!defined('STDIN')) { include "root.php"; }
require_once "includes/require.php"; //includes
include "resources/phpmailer/class.phpmailer.php"; if (!defined('STDIN')) { include "root.php"; }
include "resources/phpmailer/class.smtp.php"; // optional, gets called from within class.phpmailer.php if not already loaded require_once "includes/require.php";
include "resources/phpmailer/class.phpmailer.php";
//set php ini values include "resources/phpmailer/class.smtp.php"; // optional, gets called from within class.phpmailer.php if not already loaded
ini_set(max_execution_time,900); //15 minutes
ini_set('memory_limit', '96M'); //set php ini values
ini_set(max_execution_time,900); //15 minutes
//start the to cache the output ini_set('memory_limit', '96M');
ob_end_clean();
ob_start(); //start the to cache the output
ob_end_clean();
//add a delimeter to the log ob_start();
echo "\n---------------------------------\n";
//add a delimeter to the log
//get the parameters and save them as variables echo "\n---------------------------------\n";
$php_version = substr(phpversion(), 0, 1);
if ($php_version == '4') { //get the parameters and save them as variables
$domain_name = $_REQUEST["domain"]; $php_version = substr(phpversion(), 0, 1);
$fax_email = $_REQUEST["email"]; if ($php_version == '4') {
$fax_extension = $_REQUEST["extension"]; $domain_name = $_REQUEST["domain"];
$fax_name = $_REQUEST["name"]; $fax_email = $_REQUEST["email"];
$fax_messages = $_REQUEST["messages"]; $fax_extension = $_REQUEST["extension"];
$caller_id_name = $_REQUEST["caller_id_name"]; $fax_name = $_REQUEST["name"];
$caller_id_number = $_REQUEST["caller_id_number"]; $fax_messages = $_REQUEST["messages"];
$fax_retry = $_REQUEST["retry"]; $caller_id_name = $_REQUEST["caller_id_name"];
} $caller_id_number = $_REQUEST["caller_id_number"];
else { $fax_retry = $_REQUEST["retry"];
$tmp_array = explode("=", $_SERVER["argv"][1]); $mailfrom_address = $_REQUEST["mailfrom_address"];
$fax_email = $tmp_array[1]; }
unset($tmp_array); else {
$tmp_array = explode("=", $_SERVER["argv"][1]);
$tmp_array = explode("=", $_SERVER["argv"][2]); $fax_email = $tmp_array[1];
$fax_extension = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
$tmp_array = explode("=", $_SERVER["argv"][2]);
$tmp_array = explode("=", $_SERVER["argv"][3]); $fax_extension = $tmp_array[1];
$fax_name = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
$tmp_array = explode("=", $_SERVER["argv"][3]);
$tmp_array = explode("=", $_SERVER["argv"][4]); $fax_name = $tmp_array[1];
$fax_messages = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
$tmp_array = explode("=", $_SERVER["argv"][4]);
$tmp_array = explode("=", $_SERVER["argv"][5]); $fax_messages = $tmp_array[1];
$domain_name = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
$tmp_array = explode("=", $_SERVER["argv"][5]);
$tmp_array = explode("=", $_SERVER["argv"][6]); $domain_name = $tmp_array[1];
$caller_id_name = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
$tmp_array = explode("=", $_SERVER["argv"][6]);
$tmp_array = explode("=", $_SERVER["argv"][7]); $caller_id_name = $tmp_array[1];
$caller_id_number = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
$tmp_array = explode("=", $_SERVER["argv"][7]);
$tmp_array = explode("=", $_SERVER["argv"][8]); $caller_id_number = $tmp_array[1];
$fax_retry = $tmp_array[1]; unset($tmp_array);
unset($tmp_array);
} $tmp_array = explode("=", $_SERVER["argv"][8]);
$fax_retry = $tmp_array[1];
//used for debug unset($tmp_array);
//echo "fax_email $fax_email\n";
//echo "fax_extension $fax_extension\n"; $tmp_array = explode("=", $_SERVER["argv"][9]);
//echo "fax_name $fax_name\n"; $mailfrom_address = $tmp_array[1];
//echo "cd $dir_fax; /usr/bin/tiff2png ".$dir_fax.'/'.$fax_name.".png\n"; unset($tmp_array);
//get the fax details from the database //$tmp_array = explode("=", $_SERVER["argv"][10]);
$sql = "select * from v_domains "; //$destination_number = $tmp_array[1];
$sql .= "where domain_name = '".$domain_name."' "; //unset($tmp_array);
$prep_statement = $db->prepare($sql); }
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC); $mailto_address = $fax_email;
foreach ($result as &$row) { echo "mailto_adress is ".$mailto_address."\n";
$_SESSION["domain_uuid"] = $row["domain_uuid"]; echo "fax_email is ".$fax_email."\n";
}
unset ($prep_statement);
//used for debug
//get the fax details from the database echo "fax_email $fax_email\n";
$sql = "select * from v_fax "; echo "fax_extension $fax_extension\n";
$sql .= "where domain_uuid = '".$_SESSION["domain_uuid"]."' "; echo "fax_name $fax_name\n";
$sql .= "and fax_extension = '$fax_extension' "; echo "cd $dir_fax; /usr/bin/tiff2png ".$dir_fax.'/'.$fax_name.".png\n";
$prep_statement = $db->prepare($sql);
$prep_statement->execute(); //get the fax details from the database
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC); $sql = "select * from v_domains ";
foreach ($result as &$row) { $sql .= "where domain_name = '".$domain_name."' ";
//set database fields as variables $prep_statement = $db->prepare($sql);
//$fax_email = $row["fax_email"]; $prep_statement->execute();
$fax_pin_number = $row["fax_pin_number"]; $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
$fax_caller_id_name = $row["fax_caller_id_name"]; foreach ($result as &$row) {
$fax_caller_id_number = $row["fax_caller_id_number"]; $_SESSION["domain_uuid"] = $row["domain_uuid"];
$fax_forward_number = $row["fax_forward_number"]; }
//$fax_user_list = $row["fax_user_list"]; unset ($prep_statement);
$fax_description = $row["fax_description"];
} //get the fax details from the database
unset ($prep_statement); $sql = "select * from v_fax ";
$sql .= "where domain_uuid = '".$_SESSION["domain_uuid"]."' ";
//set the fax directory $sql .= "and fax_extension = '$fax_extension' ";
$dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$domain_name.'/'.$fax_extension.'/inbox'; $prep_statement = $db->prepare($sql);
if (!file_exists($dir_fax)) { $prep_statement->execute();
$dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$fax_extension.'/inbox'; $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
} foreach ($result as &$row) {
//set database fields as variables
//convert the tif to a pdf //$fax_email = $row["fax_email"];
//Ubuntu: apt-get install libtiff-tools $fax_pin_number = $row["fax_pin_number"];
$fax_file_warning = ""; $fax_caller_id_name = $row["fax_caller_id_name"];
if (file_exists($dir_fax.'/'.$fax_name.".tif")) { $fax_caller_id_number = $row["fax_caller_id_number"];
if (!file_exists($dir_fax.'/'.$fax_name.".pdf")) { $fax_forward_number = $row["fax_forward_number"];
$tmp_tiff2pdf = exec("which tiff2pdf"); //$fax_user_list = $row["fax_user_list"];
if (strlen($tmp_tiff2pdf) == 0) {$tmp_tiff2pdf = "/usr/bin/tiff2pdf"; } $fax_description = $row["fax_description"];
if (strlen($tmp_tiff2pdf) > 0) { }
$cmd = "cd ".$dir_fax."; ".$tmp_tiff2pdf." -f -o ".$fax_name.".pdf ".$dir_fax.'/'.$fax_name.".tif"; unset ($prep_statement);
echo $cmd."\n";
exec($cmd); //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)) {
else { $dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$fax_extension.'/inbox';
$fax_file_warning = " Fax image not available on server."; }
}
//convert the tif to a pdf
//forward the fax //Ubuntu: apt-get install libtiff-tools
if (strpos($fax_name,'#') !== false) { $fax_file_warning = "";
$tmp = explode("#",$fax_name); if (file_exists($dir_fax.'/'.$fax_name.".tif")) {
$fax_forward_number = $tmp[0]; if (!file_exists($dir_fax.'/'.$fax_name.".pdf")) {
} $tmp_tiff2pdf = exec("which tiff2pdf");
if (strlen($fax_forward_number) > 0) { if (strlen($tmp_tiff2pdf) == 0) {$tmp_tiff2pdf = "/usr/bin/tiff2pdf"; }
if (file_exists($dir_fax."/".$fax_name.".tif")) { if (strlen($tmp_tiff2pdf) > 0) {
//get the event socket information $cmd = "cd ".$dir_fax."; ".$tmp_tiff2pdf." -f -o ".$fax_name.".pdf ".$dir_fax.'/'.$fax_name.".tif";
$sql = "select * from v_settings "; echo $cmd."\n";
$prep_statement = $db->prepare(check_sql($sql)); exec($cmd);
$prep_statement->execute(); }
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC); }
foreach ($result as &$row) { }
$event_socket_ip_address = $row["event_socket_ip_address"]; else {
$event_socket_port = $row["event_socket_port"]; $fax_file_warning = " Fax image not available on server.";
$event_socket_password = $row["event_socket_password"]; echo "$fax_file_warning\n";
break; }
}
//create the event socket connection //forward the fax
$fp = event_socket_create($event_socket_ip_address, $event_socket_port, $event_socket_password); if (strpos($fax_name,'#') !== false) {
//send the command with event socket $tmp = explode("#",$fax_name);
if ($fp) { $fax_forward_number = $tmp[0];
//prepare the fax originate command }
$route_array = outbound_route_to_bridge($_SESSION['domain_uuid'], $fax_forward_number);
$fax_file = $dir_fax."/".$fax_name.".tif"; echo "fax_forward_number is $fax_forward_number\n";
if (count($route_array) == 0) { if (strlen($fax_forward_number) > 0) {
//send the internal call to the registered extension if (file_exists($dir_fax."/".$fax_name.".tif")) {
$fax_uri = "user/".$fax_forward_number."@".$domain_name; //get the event socket information
$t38 = ""; $sql = "select * from v_settings ";
} $prep_statement = $db->prepare(check_sql($sql));
else { $prep_statement->execute();
//send the external call $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
$fax_uri = $route_array[0]; foreach ($result as &$row) {
$t38 = "fax_enable_t38=true,fax_enable_t38_request=true"; $event_socket_ip_address = $row["event_socket_ip_address"];
} $event_socket_port = $row["event_socket_port"];
$cmd = "api originate {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."')"; $event_socket_password = $row["event_socket_password"];
//send info to the log break;
echo "fax forward\n"; }
echo $cmd."\n"; //create the event socket connection
//send the command to event socket $fp = event_socket_create($event_socket_ip_address, $event_socket_port, $event_socket_password);
$response = event_socket_request($fp, $cmd); //send the command with event socket
$response = str_replace("\n", "", $response); if ($fp) {
//send info to the log //prepare the fax originate command
echo "response: ".$response."\n"; $route_array = outbound_route_to_bridge($_SESSION['domain_uuid'], $fax_forward_number);
//get the uuid $fax_file = $dir_fax."/".$fax_name.".tif";
$uuid = str_replace("+OK ", "", $response); if (count($route_array) == 0) {
//close event socket //send the internal call to the registered extension
fclose($fp); $fax_uri = "user/".$fax_forward_number."@".$domain_name;
} $t38 = "";
} }
} else {
//send the external call
//send the email $fax_uri = $route_array[0];
if (strlen($fax_email) > 0 && file_exists($dir_fax."/".$fax_name.".tif")) { $t38 = "fax_enable_t38=true,fax_enable_t38_request=true";
//prepare the message }
$tmp_subject = "Fax Received: ".$fax_name; $cmd = "api originate {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."')";
$tmp_text_plain = "\nFax Received:\n"; //send info to the log
$tmp_text_plain .= "Name: ".$fax_name."\n"; echo "fax forward\n";
$tmp_text_plain .= "Extension: ".$fax_extension."\n"; echo $cmd."\n";
$tmp_text_plain .= "Messages: ".$fax_messages."\n"; //send the command to event socket
$tmp_text_plain .= $fax_file_warning."\n"; $response = event_socket_request($fp, $cmd);
if ($fax_retry == 'yes') { $response = str_replace("\n", "", $response);
$tmp_text_plain .= "This message arrived earlier and has been queued until now due to email server issues.\n"; //send info to the log
} echo "response: ".$response."\n";
$tmp_text_html = $tmp_text_plain; //get the uuid
$uuid = str_replace("+OK ", "", $response);
//prepare the mail object //close event socket
$mail = new PHPMailer(); fclose($fp);
$mail->IsSMTP(); // set mailer to use SMTP }
if ($_SESSION['email']['smtp_auth']['var'] == "true") { }
$mail->SMTPAuth = $_SESSION['email']['smtp_auth']['var']; // turn on/off SMTP authentication }
}
$mail->Host = $_SESSION['email']['smtp_host']['var']; //send the email
if ($_SESSION['email']['smtp_secure']['var'] == "none") { if (strlen($fax_email) > 0 && file_exists($dir_fax."/".$fax_name.".tif")) {
$_SESSION['email']['smtp_secure']['var'] = ''; //prepare the message
} $tmp_subject = "Fax Received: ".$fax_name;
if (strlen($_SESSION['email']['smtp_secure']['var']) > 0) { $tmp_text_plain = "\nFax Received:\n";
$mail->SMTPSecure = $_SESSION['email']['smtp_secure']['var']; $tmp_text_plain .= "Name: ".$fax_name."\n";
} $tmp_text_plain .= "Extension: ".$fax_extension."\n";
if ($_SESSION['email']['smtp_username']['var']) { $tmp_text_plain .= "Messages: ".$fax_messages."\n";
$mail->Username = $_SESSION['email']['smtp_username']['var']; $tmp_text_plain .= $fax_file_warning."\n";
$mail->Password = $_SESSION['email']['smtp_password']['var']; if ($fax_retry == 'yes') {
} $tmp_subject = "Fax Received for Relay: ".$fax_name;
$mail->SMTPDebug = 2; //$tmp_text_plain .= "This message arrived earlier and has been queued until now due to email server issues.\n";
$mail->From = $_SESSION['email']['smtp_from']['var']; $tmp_text_plain .= "\nThis message arrived successfully from your fax machine, and has been queued for outbound fax delivery. You will be notified as to the success or failure of this message at ".$email"\n";
$mail->FromName = $_SESSION['email']['smtp_from_name']['var']; }
$mail->Subject = $tmp_subject; $tmp_text_html = $tmp_text_plain;
$mail->AltBody = $tmp_text_plain;
$mail->MsgHTML($tmp_text_html); //prepare the mail object
$mail = new PHPMailer();
$tmp_to = $fax_email; $mail->IsSMTP(); // set mailer to use SMTP
$tmp_to = str_replace(";", ",", $tmp_to); if ($_SESSION['email']['smtp_auth']['var'] == "true") {
$tmp_to_array = explode(",", $tmp_to); $mail->SMTPAuth = $_SESSION['email']['smtp_auth']['var']; // turn on/off SMTP authentication
foreach($tmp_to_array as $tmp_to_row) { }
if (strlen($tmp_to_row) > 0) { $mail->Host = $_SESSION['email']['smtp_host']['var'];
echo "tmp_to_row: $tmp_to_row\n"; if ($_SESSION['email']['smtp_secure']['var'] == "none") {
$mail->AddAddress(trim($tmp_to_row)); $_SESSION['email']['smtp_secure']['var'] = '';
} }
} if (strlen($_SESSION['email']['smtp_secure']['var']) > 0) {
$mail->SMTPSecure = $_SESSION['email']['smtp_secure']['var'];
//output to the log }
echo "smtp_from: ".$_SESSION['email']['smtp_from']['var']."\n"; if ($_SESSION['email']['smtp_username']['var']) {
echo "smtp_from_name: ".$_SESSION['email']['smtp_from_name']['var']."\n"; $mail->Username = $_SESSION['email']['smtp_username']['var'];
echo "tmp_subject: $tmp_subject\n"; $mail->Password = $_SESSION['email']['smtp_password']['var'];
}
//add teh attachments $mail->SMTPDebug = 2;
if (strlen($fax_name) > 0) { $mail->From = $_SESSION['email']['smtp_from']['var'];
if (!file_exists($dir_fax.'/'.$fax_name.".pdf")) { $mail->FromName = $_SESSION['email']['smtp_from_name']['var'];
$mail->AddAttachment($dir_fax.'/'.$fax_name.'.tif'); // tif attachment $mail->Subject = $tmp_subject;
} $mail->AltBody = $tmp_text_plain;
if (file_exists($dir_fax.'/'.$fax_name.".pdf")) { $mail->MsgHTML($tmp_text_html);
$mail->AddAttachment($dir_fax.'/'.$fax_name.'.pdf'); // pdf attachment
} $tmp_to = $fax_email;
//$filename='fax.tif'; $encoding = "base64"; $type = "image/tif"; $tmp_to = str_replace(";", ",", $tmp_to);
//$mail->AddStringAttachment(base64_decode($strfax),$filename,$encoding,$type); $tmp_to_array = explode(",", $tmp_to);
} foreach($tmp_to_array as $tmp_to_row) {
if (strlen($tmp_to_row) > 0) {
//send the email echo "tmp_to_row: $tmp_to_row\n";
if(!$mail->Send()) { $mail->AddAddress(trim($tmp_to_row));
echo "Mailer Error: " . $mail->ErrorInfo; }
$email_status=$mail; }
}
else { //output to the log
echo "Message sent!"; echo "smtp_from: ".$_SESSION['email']['smtp_from']['var']."\n";
$email_status="ok"; echo "smtp_from_name: ".$_SESSION['email']['smtp_from_name']['var']."\n";
} echo "tmp_subject: $tmp_subject\n";
}
//add teh attachments
//when sending an email the following files are created: if (strlen($fax_name) > 0) {
// /usr/local/freeswitch/storage/fax if (!file_exists($dir_fax.'/'.$fax_name.".pdf")) {
// emailed_faxes.log - this is a log of all the faxes we have successfully emailed. (note that we need to work out how to rotate this log) $mail->AddAttachment($dir_fax.'/'.$fax_name.'.tif'); // tif attachment
// failed_fax_emails.log - this is a log of all the faxes we have failed to email. This log is in the form of instructions that we can re-execute in order to retry. }
// Whenever this exists there should be an at job present to run it sometime in the next 3 minutes (check with atq). If we succeed in sending the messages if (file_exists($dir_fax.'/'.$fax_name.".pdf")) {
// this file will be removed. $mail->AddAttachment($dir_fax.'/'.$fax_name.'.pdf'); // pdf attachment
// /tmp }
// fax_email_retry.sh - this is the renamed failed_fax_emails.log and is created only at the point in time that we are trying to re-send the emails. Note however //$filename='fax.tif'; $encoding = "base64"; $type = "image/tif";
// that this will continue to exist even if we succeed as we do not delete it when finished. //$mail->AddStringAttachment(base64_decode($strfax),$filename,$encoding,$type);
// failed_fax_emails.sh - this is created when we have a email we need to re-send. At the time it is created, an at job is created to execute it in 3 minutes time, }
// this allows us to try sending the email again at that time. If the file exists but there is no at job this is because there are no longer any emails queued
// as we have successfully sent them all. //send the email
if (strlen($fax_email) > 0 && file_exists($dir_fax."/".$fax_name.".tif")) { if(!$mail->Send()) {
if (stristr(PHP_OS, 'WIN')) { echo "Mailer Error: " . $mail->ErrorInfo;
//not compatible with windows $email_status=$mail;
} }
else { else {
$fax_to_email_queue_dir = $_SESSION['switch']['storage']['dir']."/fax"; echo "Message sent!";
if ($email_status == 'ok') { $email_status="ok";
// log the success }
$fp = fopen($fax_to_email_queue_dir."/emailed_faxes.log", "a"); }
fwrite($fp, $fax_name." received on ".$fax_extension." emailed to ".$fax_email." ".$fax_messages."\n");
fclose($fp); //when sending an email the following files are created:
} else { // /usr/local/freeswitch/storage/fax
// create an instruction log to email messages once the connection to the mail server has been restored // emailed_faxes.log - this is a log of all the faxes we have successfully emailed. (note that we need to work out how to rotate this log)
$fp = fopen($fax_to_email_queue_dir."/failed_fax_emails.log", "a"); // failed_fax_emails.log - this is a log of all the faxes we have failed to email. This log is in the form of instructions that we can re-execute in order to retry.
fwrite($fp, PHP_BINDIR."/php ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/fax_to_email.php email=$fax_email extension=$fax_extension name=$fax_name messages='$fax_messages' retry=yes\n"); // Whenever this exists there should be an at job present to run it sometime in the next 3 minutes (check with atq). If we succeed in sending the messages
fclose($fp); // this file will be removed.
// create a script to do the delayed mailing // /tmp
$fp = fopen($_SESSION['server']['temp']['dir']."/failed_fax_emails.sh", "w"); // fax_email_retry.sh - this is the renamed failed_fax_emails.log and is created only at the point in time that we are trying to re-send the emails. Note however
fwrite($fp, "rm ".$_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n"); // that this will continue to exist even if we succeed as we do not delete it when finished.
fwrite($fp, "mv ".$fax_to_email_queue_dir."/failed_fax_emails.log ".$_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n"); // failed_fax_emails.sh - this is created when we have a email we need to re-send. At the time it is created, an at job is created to execute it in 3 minutes time,
fwrite($fp, "chmod 777 ".$_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n"); // this allows us to try sending the email again at that time. If the file exists but there is no at job this is because there are no longer any emails queued
fwrite($fp, $_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n"); // as we have successfully sent them all.
fclose($fp); if (strlen($fax_email) > 0 && file_exists($dir_fax."/".$fax_name.".tif")) {
$tmp_response = exec("chmod 777 ".$_SESSION['server']['temp']['dir']."/failed_fax_emails.sh"); if (stristr(PHP_OS, 'WIN')) {
// note we use batch in order to execute when system load is low. Alternatively this could be replaced with AT. //not compatible with windows
$tmp_response = exec("batch -f ".$_SESSION['server']['temp']['dir']."/failed_fax_emails.sh now + 3 minutes"); }
} else {
} $fax_to_email_queue_dir = $_SESSION['switch']['storage']['dir']."/fax";
} if ($email_status == 'ok') {
// log the success
//open the file for writing $fp = fopen($fax_to_email_queue_dir."/emailed_faxes.log", "a");
$fp = fopen($_SESSION['server']['temp']['dir']."/fax_to_email.log", "w"); fwrite($fp, $fax_name." received on ".$fax_extension." emailed to ".$fax_email." ".$fax_messages."\n");
//get the output from the buffer fclose($fp);
$content = ob_get_contents(); } else {
//clean the buffer // create an instruction log to email messages once the connection to the mail server has been restored
ob_end_clean(); $fp = fopen($fax_to_email_queue_dir."/failed_fax_emails.log", "a");
//write the contents of the buffer fwrite($fp, PHP_BINDIR."/php ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/secure/fax_to_email.php email=$fax_email extension=$fax_extension name=$fax_name messages='$fax_messages' retry=yes\n");
fwrite($fp, $content); fclose($fp);
fclose($fp); // create a script to do the delayed mailing
?> $fp = fopen($_SESSION['server']['temp']['dir']."/failed_fax_emails.sh", "w");
fwrite($fp, "rm ".$_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n");
fwrite($fp, "mv ".$fax_to_email_queue_dir."/failed_fax_emails.log ".$_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n");
fwrite($fp, "chmod 777 ".$_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n");
fwrite($fp, $_SESSION['server']['temp']['dir']."/fax_email_retry.sh\n");
fclose($fp);
$tmp_response = exec("chmod 777 ".$_SESSION['server']['temp']['dir']."/failed_fax_emails.sh");
// note we use batch in order to execute when system load is low. Alternatively this could be replaced with AT.
$tmp_response = exec("batch -f ".$_SESSION['server']['temp']['dir']."/failed_fax_emails.sh now + 3 minutes");
}
}
}
//open the file for writing
$fp = fopen($_SESSION['server']['temp']['dir']."/fax_to_email.log", "w");
//get the output from the buffer
$content = ob_get_contents();
//clean the buffer
ob_end_clean();
//write the contents of the buffer
fwrite($fp, $content);
fclose($fp);
?>