<?php
require_once('../../config.php');
if($_SERVER['REQUEST_METHOD']!=='POST')exit;
$csrf=$_POST['csrf']??'';
if($csrf===''||!hash_equals($_SESSION['csrf'],$csrf)){$prev=$_SERVER['HTTP_REFERER']??'index.php';header("Location:$prev");exit;}
if(!isset($_SESSION['user'])||!has_permission('inboxdelete')){header("Location:".($applicationBaseDir??'/')."login.php");exit;}
$folder=trim($_POST['folder']??'');
$folder=basename($folder);
if($folder===''){header('Content-Type:text/html;charset=UTF-8');
echo"<script>alert('".addslashes(__('No folder specified for deletion'))."');history.back();</script>";exit;
}
$basePath=realpath(rtrim($applicationArchivePath,'/'));
$targetPath=realpath($basePath.DIRECTORY_SEPARATOR.$folder);
if(!$basePath||!$targetPath||!is_dir($targetPath)||$targetPath===$basePath||strpos($targetPath,$basePath.DIRECTORY_SEPARATOR)!==0){
error_log('Security Warning: Unauthorized folder delete attempt: '.($targetPath?:'INVALID'));
header('Content-Type:text/html;charset=UTF-8');
echo"<script>alert('".addslashes(__('Invalid folder path or access denied'))."');history.back();</script>";
exit;
}
$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($targetPath,FilesystemIterator::SKIP_DOTS),RecursiveIteratorIterator::CHILD_FIRST);
$deleteError=false;
foreach($it as $item){
$path=$item->getPathname();
if($item->isLink())continue;
if($item->isDir()){if(!@rmdir($path)){error_log('Folder Delete Error:'.$path);$deleteError=true;}
}else{if(!@unlink($path)){error_log('File Delete Error:'.$path);$deleteError=true;}}
}
if(!@rmdir($targetPath)){error_log('Folder Delete Error:'.$targetPath);$deleteError=true;}
if($deleteError){header('Content-Type:text/html;charset=UTF-8');
echo"<script>alert('".addslashes(__('Folder Delete Error'))."');history.back();</script>";
exit;
}
header("Location:".($applicationBaseDir??'/')."fax_inbox.php");
exit;
