2022-04-21 19:21:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
2023-06-15 19:28:23 +02:00
|
|
|
//run from command line only
|
|
|
|
|
if (!defined('STDIN')) {
|
2022-04-21 19:21:32 +02:00
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 07:50:31 +02:00
|
|
|
//includes files
|
2023-06-15 19:28:23 +02:00
|
|
|
require_once dirname(__DIR__, 4) . "/resources/require.php";
|
2022-10-23 07:50:31 +02:00
|
|
|
require_once "resources/pdo.php";
|
|
|
|
|
|
2022-04-21 19:21:32 +02:00
|
|
|
//increase limits
|
|
|
|
|
set_time_limit(0);
|
|
|
|
|
ini_set('max_execution_time', 0);
|
|
|
|
|
ini_set('memory_limit', '512M');
|
|
|
|
|
|
|
|
|
|
//save the arguments to variables
|
|
|
|
|
$script_name = $argv[0];
|
|
|
|
|
if (!empty($argv[1])) {
|
|
|
|
|
parse_str($argv[1], $_GET);
|
|
|
|
|
}
|
|
|
|
|
//print_r($_GET);
|
|
|
|
|
|
|
|
|
|
//set the variables
|
|
|
|
|
if (isset($_GET['hostname'])) {
|
|
|
|
|
$hostname = urldecode($_GET['hostname']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($_GET['debug'])) {
|
|
|
|
|
$debug = $_GET['debug'];
|
|
|
|
|
}
|
2022-04-21 23:36:39 +02:00
|
|
|
if (isset($_GET['sql'])) {
|
|
|
|
|
$debug_sql = $_GET['sql'];
|
|
|
|
|
}
|
2022-04-21 19:21:32 +02:00
|
|
|
|
|
|
|
|
//define the process id file
|
|
|
|
|
$pid_file = "/var/run/fusionpbx/".basename( $argv[0], ".php") .".pid";
|
|
|
|
|
//echo "pid_file: ".$pid_file."\n";
|
|
|
|
|
|
|
|
|
|
//function to check if the process exists
|
|
|
|
|
function process_exists($file = false) {
|
|
|
|
|
|
|
|
|
|
//set the default exists to false
|
|
|
|
|
$exists = false;
|
|
|
|
|
|
|
|
|
|
//check to see if the process is running
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
$pid = file_get_contents($file);
|
2022-09-13 04:49:04 +02:00
|
|
|
if (function_exists('posix_getsid')) {
|
|
|
|
|
if (posix_getsid($pid) === false) {
|
|
|
|
|
//process is not running
|
|
|
|
|
$exists = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//process is running
|
|
|
|
|
$exists = true;
|
|
|
|
|
}
|
2022-04-21 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2022-09-13 04:49:04 +02:00
|
|
|
if (file_exists('/proc/'.$pid)) {
|
|
|
|
|
//process is running
|
|
|
|
|
$exists = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//process is not running
|
|
|
|
|
$exists = false;
|
|
|
|
|
}
|
2022-04-21 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//return the result
|
|
|
|
|
return $exists;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//check to see if the process exists
|
|
|
|
|
$pid_exists = process_exists($pid_file);
|
|
|
|
|
|
|
|
|
|
//prevent the process running more than once
|
|
|
|
|
if ($pid_exists) {
|
|
|
|
|
echo "Cannot lock pid file {$pid_file}\n";
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//make sure the /var/run/fusionpbx directory exists
|
2022-07-08 00:14:01 +02:00
|
|
|
if (!file_exists('/var/run/fusionpbx')) {
|
|
|
|
|
$result = mkdir('/var/run/fusionpbx', 0777, true);
|
|
|
|
|
if (!$result) {
|
|
|
|
|
die('Failed to create /var/run/fusionpbx');
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-21 19:21:32 +02:00
|
|
|
|
|
|
|
|
//create the process id file if the process doesn't exist
|
|
|
|
|
if (!$pid_exists) {
|
|
|
|
|
//remove the old pid file
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
unlink($pid_file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//show the details to the user
|
|
|
|
|
//echo "The process id is ".getmypid()."\n";
|
|
|
|
|
//echo "pid_file: ".$pid_file."\n";
|
|
|
|
|
|
|
|
|
|
//save the pid file
|
|
|
|
|
file_put_contents($pid_file, getmypid());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 06:15:45 +02:00
|
|
|
//set the fax queue interval
|
2025-03-14 23:19:49 +01:00
|
|
|
if (!empty($settings->get('fax_queue', 'interval'))) {
|
|
|
|
|
$fax_queue_interval = $settings->get('fax_queue', 'interval');
|
2022-04-22 06:15:45 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$fax_queue_interval = '30';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//set the fax queue limit
|
2025-03-14 23:19:49 +01:00
|
|
|
if (!empty($settings->get('fax_queue', 'limit'))) {
|
|
|
|
|
$fax_queue_limit = $settings->get('fax_queue', 'limit');
|
2022-04-22 06:15:45 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$fax_queue_limit = '30';
|
|
|
|
|
}
|
2025-03-14 23:19:49 +01:00
|
|
|
if (!empty($settings->get('fax_queue', 'debug'))) {
|
|
|
|
|
$debug = $settings->get('fax_queue', 'debug');
|
2022-04-22 06:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-26 22:33:19 +02:00
|
|
|
//set the fax queue retry interval
|
2025-03-14 23:19:49 +01:00
|
|
|
if (!empty($settings->get('fax_queue', 'retry_interval'))) {
|
|
|
|
|
$fax_retry_interval = $settings->get('fax_queue', 'retry_interval');
|
2022-05-26 22:33:19 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$fax_retry_interval = '180';
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 02:17:45 +02:00
|
|
|
//change the working directory
|
2022-10-14 19:51:17 +02:00
|
|
|
chdir($_SERVER['DOCUMENT_ROOT']);
|
2022-04-24 02:17:45 +02:00
|
|
|
|
2023-12-21 20:30:51 +01:00
|
|
|
//get the messages waiting in the fax queue
|
2022-04-21 19:21:32 +02:00
|
|
|
while (true) {
|
|
|
|
|
|
|
|
|
|
//get the fax messages that are waiting to send
|
|
|
|
|
$sql = "select * from v_fax_queue ";
|
2022-07-08 00:14:01 +02:00
|
|
|
$sql .= "where hostname = :hostname ";
|
|
|
|
|
$sql .= "and ( ";
|
|
|
|
|
$sql .= " ( ";
|
2023-03-28 21:01:49 +02:00
|
|
|
$sql .= " (fax_status = 'waiting' or fax_status = 'trying' or fax_status = 'busy') ";
|
2022-07-08 00:14:01 +02:00
|
|
|
$sql .= " and (fax_retry_date is null or floor(extract(epoch from now()) - extract(epoch from fax_retry_date)) > :retry_interval) ";
|
|
|
|
|
$sql .= " ) ";
|
|
|
|
|
$sql .= " or ( ";
|
|
|
|
|
$sql .= " fax_status = 'sent' ";
|
|
|
|
|
$sql .= " and fax_email_address is not null ";
|
|
|
|
|
$sql .= " and fax_notify_date is null ";
|
|
|
|
|
$sql .= " ) ";
|
2022-04-21 19:21:32 +02:00
|
|
|
$sql .= ") ";
|
|
|
|
|
$sql .= "order by domain_uuid asc ";
|
|
|
|
|
$sql .= "limit :limit ";
|
|
|
|
|
if (isset($hostname)) {
|
|
|
|
|
$parameters['hostname'] = $hostname;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$parameters['hostname'] = gethostname();
|
|
|
|
|
}
|
|
|
|
|
$parameters['limit'] = $fax_queue_limit;
|
2022-05-26 22:33:19 +02:00
|
|
|
$parameters['retry_interval'] = $fax_retry_interval;
|
2022-04-21 23:36:39 +02:00
|
|
|
if (isset($debug_sql)) {
|
2022-07-08 00:14:01 +02:00
|
|
|
echo $sql."\n";
|
2022-04-21 23:36:39 +02:00
|
|
|
print_r($parameters);
|
|
|
|
|
}
|
2022-04-21 19:21:32 +02:00
|
|
|
$database = new database;
|
|
|
|
|
$fax_queue = $database->select($sql, $parameters, 'all');
|
|
|
|
|
unset($parameters);
|
2022-07-08 00:14:01 +02:00
|
|
|
|
2022-04-21 23:36:39 +02:00
|
|
|
//show results from the database
|
|
|
|
|
if (isset($debug_sql)) {
|
|
|
|
|
echo $sql."\n";
|
|
|
|
|
print_r($parameters);
|
|
|
|
|
}
|
2022-04-21 19:21:32 +02:00
|
|
|
|
|
|
|
|
//process the messages
|
|
|
|
|
if (is_array($fax_queue) && @sizeof($fax_queue) != 0) {
|
|
|
|
|
foreach($fax_queue as $row) {
|
2024-12-03 01:58:07 +01:00
|
|
|
$command = PHP_BINARY." ".$_SERVER['DOCUMENT_ROOT']."/app/fax_queue/resources/job/fax_send.php ";
|
2022-04-21 21:33:00 +02:00
|
|
|
$command .= "'action=send&fax_queue_uuid=".$row["fax_queue_uuid"]."&hostname=".$hostname."'";
|
2022-04-21 19:21:32 +02:00
|
|
|
if (isset($debug)) {
|
|
|
|
|
//run process inline to see debug info
|
|
|
|
|
echo $command."\n";
|
|
|
|
|
$result = system($command);
|
|
|
|
|
echo $result."\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//starts process rapidly doesn't wait for previous process to finish (used for production)
|
|
|
|
|
echo $command."\n";
|
|
|
|
|
$handle = popen($command." > /dev/null &", 'r');
|
|
|
|
|
echo "'$handle'; " . gettype($handle) . "\n";
|
|
|
|
|
$read = fread($handle, 2096);
|
|
|
|
|
echo $read;
|
|
|
|
|
pclose($handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//pause to prevent excessive database queries
|
2022-05-26 22:33:19 +02:00
|
|
|
sleep($fax_queue_interval);
|
2022-04-21 19:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//remove the old pid file
|
2022-04-24 08:18:36 +02:00
|
|
|
if (file_exists($pid_file)) {
|
2022-04-21 19:21:32 +02:00
|
|
|
unlink($pid_file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//save output to
|
|
|
|
|
//$fp = fopen(sys_get_temp_dir()."/mailer-app.log", "a");
|
|
|
|
|
|
|
|
|
|
//prepare the output buffers
|
|
|
|
|
//ob_end_clean();
|
|
|
|
|
//ob_start();
|
|
|
|
|
|
|
|
|
|
//message divider for log file
|
|
|
|
|
//echo "\n\n=============================================================================================================================================\n\n";
|
|
|
|
|
|
|
|
|
|
//get and save the output from the buffer
|
|
|
|
|
//$content = ob_get_contents(); //get the output from the buffer
|
|
|
|
|
//$content = str_replace("<br />", "", $content);
|
|
|
|
|
|
|
|
|
|
//ob_end_clean(); //clean the buffer
|
|
|
|
|
|
|
|
|
|
//fwrite($fp, $content);
|
|
|
|
|
//fclose($fp);
|
|
|
|
|
|
|
|
|
|
//notes
|
|
|
|
|
//echo __line__."\n";
|
|
|
|
|
// if not keeping the email then need to delete it after the voicemail is emailed
|
|
|
|
|
|
|
|
|
|
//how to use this feature
|
|
|
|
|
// cd /var/www/fusionpbx; /usr/bin/php /var/www/fusionpbx/app/fax_queue/resources/send.php
|
|
|
|
|
|
|
|
|
|
?>
|