<?php
require_once('../../config.php');
$receivedToken=$_GET['csrf']??'';
if(empty($_SESSION['csrf'])||$receivedToken!==$_SESSION['csrf']){$page=$_GET['page']??'fax_inbox';header("Location:".($applicationBaseDir??'/')."{$page}.php?msg=".urlencode(__('Invalid security token')));exit;}
if(!isset($_SESSION['user'])||!has_permission('inboxdownload')){header("Location:".($applicationBaseDir??'/')."login.php");exit;}
set_time_limit(300);
ignore_user_abort(true);
$dir=isset($_GET['dir'])?basename($_GET['dir']):'';
$folder=isset($_GET['folder'])?basename($_GET['folder']):'';
$type=$_GET['type']??'pdf';
$page=$_POST['pagename']??($_GET['page']??'fax_inbox');
if($folder===''){header("Location:".($applicationBaseDir??'/')."{$page}.php?msg=".urlencode(__('Folder name is required')));exit;}
$baseArchivePath=realpath(rtrim($applicationArchivePath,'/\\'));
$folderPath=$baseArchivePath.($dir?DIRECTORY_SEPARATOR.$dir:'').DIRECTORY_SEPARATOR.$folder;
$realFolderPath=realpath($folderPath);
if(!$realFolderPath||!is_dir($realFolderPath)||!str_starts_with($realFolderPath,$baseArchivePath.DIRECTORY_SEPARATOR)){
header("Location:".($applicationBaseDir??'/')."{$page}.php?msg=".urlencode(__('Invalid folder path or access denied')));
exit;
}
$zipName=$page."_".$folder."_".date('Ymd_Hi').".zip";
$zipPath=sys_get_temp_dir().DIRECTORY_SEPARATOR.$zipName;
register_shutdown_function(function() use($zipPath){if(is_file($zipPath))@unlink($zipPath);});
$zip=new ZipArchive();
if($zip->open($zipPath,ZipArchive::CREATE|ZipArchive::OVERWRITE)!==true){header("Location:".($applicationBaseDir??'/')."{$page}.php?msg=".urlencode(__('Cannot create zip file')));
exit;
}
$files=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($realFolderPath,RecursiveDirectoryIterator::SKIP_DOTS),RecursiveIteratorIterator::LEAVES_ONLY);
$fileCount=0;
foreach($files as $file){
if($file->isLink()||$file->isDir())continue;
$filePath=$file->getRealPath();
if(!$filePath||!str_starts_with($filePath,$realFolderPath.DIRECTORY_SEPARATOR))continue;
$ext=strtolower(pathinfo($filePath,PATHINFO_EXTENSION));
if($type==='tiff'){if(!in_array($ext,['tif','tiff'],true))continue;
}else{if($ext!=='pdf')continue;}
$relativePath=substr($filePath,strlen($realFolderPath)+1);
$zip->addFile($filePath,$relativePath);
$metaFile=dirname($filePath).DIRECTORY_SEPARATOR.pathinfo($filePath,PATHINFO_FILENAME).'.meta';
if(is_file($metaFile)){
$meta=loadMeta($metaFile);
if(!is_array($meta))$meta=[];
$meta['read']=true;
saveMeta($metaFile,$meta);
}
$fileCount++;
}
if($fileCount===0||!$zip->close()){@$zip->close();header("Location:".($applicationBaseDir??'/')."{$page}.php?msg=".urlencode(__('No files found or compression failed')));exit;}
if(is_file($zipPath)){
while(ob_get_level())ob_end_clean();
header('Content-Type:application/zip');
$encodedName=rawurlencode($zipName);
header("Content-Disposition: attachment;filename=\"$encodedName\";filename*=UTF-8''$encodedName");
header('Content-Length:'.filesize($zipPath));
header('Cache-Control:private,max-age=0,must-revalidate');
header('Pragma:public');
header('Expires: 0');
$fp=fopen($zipPath,'rb');
if($fp){while(!feof($fp)){echo fread($fp,8192);flush();}fclose($fp);}
}
exit;
