<?php
require_once('../../config.php');
$receivedToken=$_POST['csrf']??$_GET['csrf']??'';
if(empty($_SESSION['csrf'])||$receivedToken!==$_SESSION['csrf']){$prev=$_SERVER['HTTP_REFERER']??'index.php';header("Location:$prev");exit;}
if(!isset($_SESSION['user'])||!has_permission('outboxresend')){header("Location:".($applicationBaseDir??'/')."login.php");exit;}
$basedir=realpath(rtrim($applicationOutboxPath,'/'));
if(!$basedir||!is_dir($basedir)){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('System path error')));exit;}
$faxName=$_POST['fax']??$_GET['fax']??'';
$safeFaxName=basename($faxName);
$baseNameOnly=pathinfo($safeFaxName,PATHINFO_FILENAME);
if($baseNameOnly===''){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Invalid file name')));exit;}
$pdfFile=$basedir.DIRECTORY_SEPARATOR.$baseNameOnly.'.pdf';
$pdfReal=realpath($pdfFile);
$baseDirWithSep=$basedir.DIRECTORY_SEPARATOR;
if(!$pdfReal||!str_starts_with($pdfReal,$baseDirWithSep)){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Invalid file path')));exit;}
$oldMetaFile=$basedir.DIRECTORY_SEPARATOR.$baseNameOnly.'.meta';
if(!file_exists($oldMetaFile)){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Metadata not found')));exit;}
$metaData=loadMeta($oldMetaFile);
$faxNum=$metaData['destination']??$metaData['number']??'';
if(empty($faxNum)||!ctype_digit($faxNum)){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('The phone number format is invalid')));exit;}
function get_system_cmd($cmd){$path=shell_exec("command -v ".escapeshellarg($cmd));return $path?trim($path):null;}
$gsPath=get_system_cmd('gs');
$sendfaxPath=get_system_cmd('sendfax');
if(!$gsPath||!$sendfaxPath){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Fax tools not installed')));exit;}
$uid=bin2hex(random_bytes(4));
$newBaseName=date('Ymd_Hi').'_'.$faxNum.'_'.$uid;
$newPdf=$basedir.DIRECTORY_SEPARATOR.$newBaseName.'.pdf';
$newTiff=$basedir.DIRECTORY_SEPARATOR.$newBaseName.'.tiff';
$newMetaFile=$basedir.DIRECTORY_SEPARATOR.$newBaseName.'.meta';
$oldTiff=$basedir.DIRECTORY_SEPARATOR.$baseNameOnly.'.tiff';
if(!copy($pdfReal,$newPdf)){header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Resend failed')));exit;}
if(is_file($oldTiff)){copy($oldTiff,$newTiff);
}else{
$gsCmd=sprintf("%s -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg4 -sOutputFile=%s %s 2>&1",escapeshellarg($gsPath),escapeshellarg($newTiff),escapeshellarg($newPdf));
exec($gsCmd,$gsOut,$gsRet);
if($gsRet!==0||!is_file($newTiff)){@unlink($newPdf);header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Conversion failed')));exit;}
}
$retryCount=isset($fax_retry)?(int)$fax_retry:1;
$priority=$metaData['priority']??'63';
$sendCmd=sprintf("%s -n -t %s -P %s -d %s %s 2>&1",escapeshellarg($sendfaxPath),escapeshellarg($retryCount),escapeshellarg($priority),escapeshellarg($faxNum),escapeshellarg($newTiff));
exec($sendCmd,$sendOut,$sendRet);
$newMeta=$metaData;
$newMeta['created_time']=date('Y-m-d H:i:s');
$newMeta['status']=$sendRet===0?'sent':'failed';
if($sendRet!==0){$newMeta['error']=implode("\n",$sendOut);}
saveMeta($newMetaFile,$newMeta);
if($sendRet!==0){error_log("Fax resend failed: ".implode("\n",$sendOut));header("Location:".($applicationBaseDir??'/')."fax_outbox.php?msg=".urlencode(__('Fax transmission failed')));exit;}
header("Location:".($applicationBaseDir??'/')."fax_outbox.php");
exit;
