<?php
require_once('config.php');
if(!defined('ABSPATH')){exit('Direct access is not allowed');}
if(!isset($_SESSION['user'])){header("Location:login.php");exit;}
$allowedPages=['index.php','fax_settings.php','fax_inbox.php','fax_inbox_edit.php','fax_outbox.php','fax_outbox_edit.php','fax_contacts.php','fax_send.php','fax_queue_in.php','fax_queue_out.php'];
$currentPath=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);
$currentPage=basename($currentPath);
if(!$currentPage||$currentPage==='/'||$currentPage==='menu.php'){$currentPage='index.php';}
if(!in_array($currentPage,$allowedPages,true)){$currentPage='index.php';}
$safePage=ns($currentPage);
$msg=$_GET['msg']??'';
$cleanMsg=ns($msg);
?>
<div id="menu">
<div id="logo"><img src="img/logo.png" alt="Logo"></div>
<ul>
<?php
$menuItems=[
['perm'=>'faxdashboard','pages'=>['index.php','fax_queue_in.php','fax_queue_out.php'],'url'=>'index.php','label'=>'Fax Dashboard'],
['perm'=>'inboxmenu','pages'=>['fax_inbox.php','fax_inbox_edit.php'],'url'=>'fax_inbox.php','label'=>'Fax Inbox'],
['perm'=>'outboxmenu','pages'=>['fax_outbox.php','fax_outbox_edit.php'],'url'=>'fax_outbox.php','label'=>'Fax Outbox'],
['perm'=>'faxsend','pages'=>['fax_send.php'],'url'=>'fax_send.php','label'=>'Send Fax'],
['perm'=>'faxcontacts','pages'=>['fax_contacts.php'],'url'=>'fax_contacts.php','label'=>'Contacts'],
['perm'=>'faxsettings','pages'=>['fax_settings.php'],'url'=>'fax_settings.php','label'=>'Settings'],
];
foreach($menuItems as $item):
if(has_permission($item['perm'])):
$isActive=in_array($safePage,$item['pages'])?'active':'';
?>
<li class="<?=$isActive?>"><a href="<?=$item['url']?>"><?php _nsl($item['label']);?></a></li>
<?php
endif;
endforeach;
?>
<li>
<form id="logout-form" action="logout.php" method="POST" style="display:none;">
<input type="hidden" name="csrf" value="<?=htmlspecialchars($_SESSION['csrf']??'')?>">
</form>
<a href="#" onclick="event.preventDefault();modalConfirm('<?php _nsl('Are you sure you want to logout?');?>',()=>{document.getElementById('logout-form').submit();});">
<?php _nsl('Logout');?></a>
</li>
</ul>
</div>
<script>
(function(){
var rawMsg=<?=json_encode($msg,JSON_UNESCAPED_UNICODE)?>;
if(rawMsg){
var tempDiv=document.createElement('div');
tempDiv.textContent=rawMsg;
var safeMsg=tempDiv.innerHTML;
if(safeMsg.trim()!==""){alert(tempDiv.textContent);}
}
})();
</script>
