Sessione scaduta. Effettua nuovamente il login.';
exit;
}
// Aruba IMAP settings
$host = "{imaps.aruba.it:993/imap/ssl}";
$username = $_SESSION['aruba_username'];
$password = $_SESSION['aruba_password'];
$folder = $_POST['folder'] ?? 'INBOX';
// Gestione ripristino messaggio dal cestino via AJAX
if (isset($_POST['action']) && $_POST['action'] === 'restore' && isset($_POST['msgnum'])) {
header('Content-Type: application/json');
$msgnum = (int)$_POST['msgnum'];
$result = false;
$trashHost = "{imaps.aruba.it:993/imap/ssl}INBOX.Trash";
$trash = @imap_open($trashHost, $username, $password);
if ($trash) {
// Sposta il messaggio dal cestino all'INBOX
if (@imap_mail_move($trash, $msgnum, 'INBOX')) {
imap_expunge($trash);
$result = true;
}
imap_close($trash);
}
if ($result) {
echo json_encode(['success' => true, 'msg' => 'Messaggio ripristinato nell\'INBOX.']);
} else {
echo json_encode(['success' => false, 'msg' => 'Errore nel ripristino del messaggio: ' . imap_last_error()]);
}
exit;
}
// Gestione svuotamento cestino via AJAX
if (isset($_POST['action']) && $_POST['action'] === 'empty_trash') {
header('Content-Type: application/json');
$trashHost = "{imaps.aruba.it:993/imap/ssl}INBOX.Trash";
$trash = @imap_open($trashHost, $username, $password);
$success = false;
if ($trash) {
$numTrash = imap_num_msg($trash);
if ($numTrash > 0) {
// contrassegna tutti i messaggi come \Deleted
imap_delete($trash, '1:' . $numTrash);
imap_expunge($trash);
// Risincronizza con il server IMAP
imap_check($trash);
$success = true;
}
imap_close($trash);
}
if ($success) {
echo json_encode(['success' => true, 'msg' => 'Cestino svuotato.']);
} else {
echo json_encode(['success' => false, 'msg' => 'Errore nello svuotamento del cestino o cestino già vuoto.']);
}
exit;
}
// Gestione richiesta tabella cestino via AJAX
if (isset($_POST['action']) && $_POST['action'] === 'get_trash') {
header('Content-Type: text/html; charset=utf-8');
$trashHost = "{imaps.aruba.it:993/imap/ssl}INBOX.Trash";
$trash = @imap_open($trashHost, $username, $password);
if ($trash) {
$numTrash = imap_num_msg($trash);
$allTrash = imap_search($trash, 'ALL');
if ($numTrash == 0 || !$allTrash) {
$lastErr = imap_last_error();
echo '
Nessun messaggio nel cestino.';
if ($lastErr) {
echo '
Errore IMAP: ' . htmlspecialchars($lastErr) . '';
}
echo '
';
} else {
echo '';
echo '| # | Da | @ | Oggetto | Data |
';
if (!is_array($allTrash)) $allTrash = [$allTrash];
rsort($allTrash);
foreach ($allTrash as $i) {
$header = imap_headerinfo($trash, $i);
$from = '';
$email = '';
$showEmailBtn = false;
if (isset($header->from[0])) {
$email = $header->from[0]->mailbox . '@' . $header->from[0]->host;
if (isset($header->from[0]->personal) && !empty($header->from[0]->personal)) {
$from = imap_utf8($header->from[0]->personal);
$showEmailBtn = true;
} else {
$from = $email;
}
}
$subject = isset($header->subject) ? imap_utf8($header->subject) : '';
$date = '';
if (isset($header->date)) {
$timestamp = strtotime($header->date);
if ($timestamp) {
$date = date('d/m/Y H:i', $timestamp);
} else {
$date = $header->date;
}
}
echo '';
echo '| ' . $i . ' | ';
echo '' . htmlspecialchars($from) . ' | ';
echo '';
if ($showEmailBtn) {
echo '';
}
echo ' | ';
echo '' . htmlspecialchars($subject) . ' | ';
echo '' . htmlspecialchars($date) . ' | ';
echo '
';
}
echo '
';
}
imap_close($trash);
}
exit;
}
// Gestione cancellazione via AJAX
if (isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['msgnum'])) {
header('Content-Type: application/json');
$msgnum = (int)$_POST['msgnum'];
$result = false;
// Apri la connessione INBOX per cancellare
$inbox = @imap_open($host, $username, $password);
if ($inbox) {
// Sposta il messaggio nel cestino (INBOX.Trash)
if (@imap_mail_move($inbox, $msgnum, 'INBOX.Trash')) {
imap_expunge($inbox);
$result = true;
}
imap_close($inbox);
}
if ($result) {
echo json_encode(['success' => true, 'msg' => 'Messaggio spostato nel cestino.']);
} else {
echo json_encode(['success' => false, 'msg' => 'Errore nello spostamento nel cestino: ' . imap_last_error()]);
}
exit;
}
// Impostazione header per risposte HTML
header('Content-Type: text/html; charset=utf-8');
// Apertura cartella richiesta
$folderPath = $folder === 'INBOX' ? $host : "{imaps.aruba.it:993/imap/ssl}" . $folder;
$mbox = @imap_open($folderPath, $username, $password);
if (!$mbox) {
echo 'Errore apertura cartella: ' . htmlspecialchars(imap_last_error()) . '
';
exit;
}
$numMessages = imap_num_msg($mbox);
echo '';
if ($numMessages == 0) {
$lastErr = imap_last_error();
echo '
';
echo '
';
echo '
Nessun messaggio in questa cartella
';
if ($lastErr) {
echo '
Errore IMAP: ' . htmlspecialchars($lastErr) . '';
}
echo '
';
} else {
for ($i = $numMessages; $i > 0 && $i > $numMessages-50; $i--) {
$header = imap_headerinfo($mbox, $i);
$from = '';
$email = '';
$showEmailBtn = false;
if (isset($header->from[0])) {
$email = $header->from[0]->mailbox . '@' . $header->from[0]->host;
if (isset($header->from[0]->personal) && !empty($header->from[0]->personal)) {
$from = imap_utf8($header->from[0]->personal);
$showEmailBtn = true;
} else {
$from = $email;
}
}
$subject = isset($header->subject) ? imap_utf8($header->subject) : '(Nessun oggetto)';
$date = '';
$dateShort = '';
if (isset($header->date)) {
$timestamp = strtotime($header->date);
if ($timestamp) {
$date = date('d/m/Y H:i', $timestamp);
$dateShort = date('d M', $timestamp);
} else {
$date = $header->date;
$dateShort = $header->date;
}
}
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '' . htmlspecialchars($from) . '';
echo '' . htmlspecialchars($subject) . '';
echo '' . htmlspecialchars($dateShort) . '';
echo '
';
echo '
';
echo '
';
echo '
';
}
}
echo '
'; // Chiudi container
imap_close($mbox);