#!/usr/bin/php -q
<?
$conn = array();
$filename = "/usr/ictfax/ictfax.conf";
$handle = @fopen($filename, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
$split = split("=", $buffer);
$field = trim($split['0']);
if (isset($split['1'])) $conn[$field] = trim($split['1']);
}
fclose($handle);
}
mysql_connect($conn['dbhost'],$conn['dbuser'],$conn['dbpass']);
@mysql_select_db($conn['dbname']) or die("Unable to select database");
$pass = 1;
$recordId = $argv[1]; // FAX Record No.
$fax_retry_wait = mysql_fetch_object(mysql_query("SELECT value FROM astsystem WHERE name = 'fax_retry_wait' and serverid = 'DEF'"));
$fax_retry_wait = $fax_retry_wait->value;
$faxRecord = mysql_fetch_assoc(mysql_query("SELECT fax_outbox.cid, fax_outbox.uid, fax_outbox.rating, fax_outbox.send_to, fax_outbox.send_from, fax_outbox.file_name, fax_outbox.file_path, fax_outbox.text, fax_outbox.accountcode, astaccount.callerid FROM fax_outbox, astaccount WHERE fax_outbox.send_from = astaccount.accountcode AND fax_outbox.cid = ".$recordId));
if ($faxRecord['rating'] < 2) {
astfax_sendfax($faxRecord['uid'], $faxRecord['send_from'], $faxRecord['send_to'], $faxRecord['file_path'], $faxRecord['cid'], $faxRecord['text'], $faxRecord['callerid'], $fax_retry_wait);
// here status set to 0 so originale status will be overwritten it can cos a wrong report on previose status between failure update and this update. this behavior should be fixed in feature TODO
mysql_query("UPDATE fax_outbox SET fax_outbox.rating = fax_outbox.rating + 1, status_string=CONCAT(status_string,'=>call still in queue'), status=0 WHERE fax_outbox.cid = ".$recordId);
}
//mysql_free_result($result);
mysql_close();
function astfax_sendfax($uid, $sendFrom, $accountcode, $tiffFile, $recordId, $description, $callid, $fax_retry_wait) {
$pass = 1;
$tiffFile = '/var/spool/asterisk/fax/outbox/' . $recordId . '/' . $tiffFile;
if (empty($callid)) {
$callid = 'Unknown';
}
//Local Channel in dialplan for faxing.
$channel = 'Local/'.$accountcode.'@dialfax';
$callerid = $callid;
$maxretries = '0';
$retrytime = '60';
$waittime = '45';
$timecreated = date('Y-m-d H-i-s');
$filename = 'outgoing.'.rand(1, 65535).'.call'; //TODO:
$tmppath = '/var/spool/asterisk/tmp';
$callpath = '/var/spool/asterisk/outgoing';
$content = "# Generated by sendfax.php
# Created: $timecreated
# $tmppath/$filename
Channel: $channel
Callerid: $callerid
MaxRetries: $maxretries
RetryTime: $retrytime
WaitTime: $waittime
Context: send-fax
Extension: s
Priority: 1
Set: fax_cid=$recordId
Set: fax_account=$sendFrom
Set: fax_filename=$tiffFile
Set: fax_description=$description";
if (!$handle = fopen($tmppath."/".$filename, 'x')) {
echo "Duplicate call file, unable to continue (".$tmppath."/".$filename.")";
exit;
}
if (fwrite($handle, $content) === FALSE) {
echo "Permission denied on file ".$tmppath."/".$filename;
exit;
} else {
$nextTime = time() + $fax_retry_wait;
$nextCallTime = strftime("%Y-%m-%d %H:%M:%S", $nextTime);
$cmd = "touch -d '$nextCallTime' ".$tmppath."/".$filename;
exec($cmd);
// echo "Success, wrote ($content) to file ($filename)";
rename ($tmppath."/".$filename, $callpath.'/'.$filename);
//unlink ($tmppath."/".$filename);
}
}
function arg($index) {
static $arguments, $q;
if (empty($arguments) || $q != $_GET['q']) {
$arguments = explode('/', $_GET['q']);
$q = $_GET['q'];
}
if (isset($arguments[$index])) {
return $arguments[$index];
}
}
?>