35227-vm/manual_email.php
Flatlogic Bot fa250b9936 v4
2025-10-25 16:43:28 +00:00

340 lines
15 KiB
PHP

<?php
require_once 'i18n.php';
require_once 'db/config.php';
$from = '';
$subject = '';
$message = '';
$success_message = '';
$upload_error = '';
// Handle File Upload and Parsing
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['email_file']) && $_FILES['email_file']['error'] == UPLOAD_ERR_OK) {
$file_tmp_path = $_FILES['email_file']['tmp_name'];
$file_name = $_FILES['email_file']['name'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$message = ''; // Initialize message
$subject = pathinfo($file_name, PATHINFO_FILENAME); // Use filename as subject by default
$from = ''; // From is not available in these files
$escaped_path = escapeshellarg($file_tmp_path);
switch ($file_ext) {
case 'pdf':
$message = shell_exec("pdftotext " . $escaped_path . " -");
break;
case 'docx':
$message = shell_exec("docx2txt < " . $escaped_path);
break;
case 'doc':
$message = shell_exec("antiword " . $escaped_path);
break;
case 'xls':
case 'xlsx':
$output_path = sys_get_temp_dir() . '/' . uniqid() . '.csv';
shell_exec("ssconvert " . $escaped_path . " " . $output_path);
if (file_exists($output_path)) {
$message = file_get_contents($output_path);
unlink($output_path);
} else {
$upload_error = t('excel_conversion_failed');
}
break;
case 'eml':
case 'txt':
$file_content = file_get_contents($file_tmp_path);
if ($file_content) {
$body_started = false;
$body_lines = [];
$lines = explode("\n", $file_content);
foreach ($lines as $line) {
if (!$body_started) {
if (preg_match('/^From:\s*(.*)/i', $line, $matches)) {
$from = trim($matches[1]);
} elseif (preg_match('/^Subject:\s*(.*)/i', $line, $matches)) {
$subject = trim($matches[1]);
} elseif (trim($line) === '') {
$body_started = true;
}
} else {
$body_lines[] = $line;
}
}
$message = implode("\n", $body_lines);
} else {
$upload_error = t('file_read_failed');
}
break;
default:
$upload_error = t('unsupported_file_type_parser');
break;
}
if (empty($message) && empty($upload_error)) {
$upload_error = t('text_extraction_failed');
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Handle Manual Form Submission
$from = isset($_POST['from']) ? $_POST['from'] : '';
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$attachments_list = [];
// Handle Attachments
if (isset($_FILES['attachments'])) {
$upload_dir = 'uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0775, true);
}
foreach ($_FILES['attachments']['name'] as $key => $name) {
if ($_FILES['attachments']['error'][$key] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES['attachments']['tmp_name'][$key];
// Sanitize filename
$safe_name = preg_replace('/[^A-Za-z0-9_\-\.]/', '_', basename($name));
$destination = $upload_dir . uniqid() . '-' . $safe_name;
if (move_uploaded_file($tmp_name, $destination)) {
$attachments_list[] = $destination;
}
}
}
}
if ($from && $subject && $message) {
try {
$pdo = db();
$stmt = $pdo->prepare('INSERT INTO manual_emails (sender, subject, message, attachments) VALUES (?, ?, ?, ?)');
$stmt->execute([$from, $subject, $message, json_encode($attachments_list)]);
$success_message = t('email_saved_successfully');
// Clear fields after successful save
$from = '';
$subject = '';
$message = '';
} catch (PDOException $e) {
$success_message = 'Error saving email: ' . $e->getMessage();
}
}
}
// Fetch all saved emails
$saved_emails = [];
try {
$pdo = db();
$stmt = $pdo->query('SELECT sender, subject, message, attachments, created_at FROM manual_emails ORDER BY created_at DESC');
$saved_emails = $stmt->fetchAll();
} catch (PDOException $e) {
$db_error = 'Error fetching emails: ' . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="<?php echo $current_lang; ?>" dir="<?php echo get_lang_dir(); ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo t('manual_entry'); ?></title>
<!-- Bootstrap 5 -->
<?php if (get_lang_dir() === 'rtl'): ?>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
<?php else:
?>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<?php endif; ?>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<?php if (get_lang_dir() === 'rtl'): ?>
<link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap" rel="stylesheet">
<?php else:
?>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
<?php endif; ?>
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<style>
.drop-zone {
border: 2px dashed #ccc;
border-radius: .5rem;
padding: 40px;
text-align: center;
cursor: pointer;
transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out;
}
.drop-zone.drag-over {
background-color: #f0f8ff;
border-color: #0d6efd;
}
.drop-zone-text {
color: #6c757d;
}
#file-list p { margin-bottom: 0.25rem; }
</style>
</head>
<body>
<div class="container py-4">
<header class="d-flex justify-content-between align-items-center pb-3 mb-4 border-bottom">
<h1 class="h4"><a href="index.php" class="text-dark text-decoration-none"><i class="bi bi-robot"></i> <?php echo t('dashboard'); ?></a></h1>
<div class="d-flex align-items-center">
<div class="dropdown me-2">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" id="langDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-translate me-1"></i> <?php echo t('language'); ?>
</button>
<ul class="dropdown-menu" aria-labelledby="langDropdown">
<li><a class="dropdown-item <?php if($current_lang == 'ar') echo 'active'; ?>" href="?lang=ar"><?php echo t('arabic'); ?></a></li>
<li><a class="dropdown-item <?php if($current_lang == 'en') echo 'active'; ?>" href="?lang=en"><?php echo t('english'); ?></a></li>
<li><a class="dropdown-item <?php if($current_lang == 'fr') echo 'active'; ?>" href="?lang=fr"><?php echo t('french'); ?></a></li>
</ul>
</div>
<a href="settings.php" class="btn btn-light" title="<?php echo t('settings'); ?>"><i class="bi bi-gear"></i></a>
</div>
</header>
<h2 class="mb-4"><?php echo t('manual_entry'); ?></h2>
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<?php if ($upload_error): ?>
<div class="alert alert-danger"><?php echo $upload_error; ?></div>
<?php endif; ?>
<div class="card mb-4">
<div class="card-header"><?php echo t('option_1'); ?></div>
<div class="card-body">
<form action="manual_email.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="email_file" class="form-label"><?php echo t('choose_email_file'); ?></label>
<input type="file" class="form-control" id="email_file" name="email_file" accept=".eml,.txt,.pdf,.doc,.docx,.xls,.xlsx">
</div>
<button type="submit" class="btn btn-secondary"><?php echo t('analyze_and_populate'); ?></button>
</form>
</div>
</div>
<div class="card mb-4">
<div class="card-header bg-primary text-white"><?php echo t('option_2'); ?></div>
<div class="card-body">
<form action="manual_email.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="from" class="form-label"><?php echo t('from'); ?></label>
<input type="email" class="form-control" id="from" name="from" value="<?php echo htmlspecialchars($from); ?>" required>
</div>
<div class="mb-3">
<label for="subject" class="form-label"><?php echo t('subject'); ?></label>
<input type="text" class="form-control" id="subject" name="subject" value="<?php echo htmlspecialchars($subject); ?>" required>
</div>
<div class="mb-3">
<label for="message" class="form-label"><?php echo t('message'); ?></label>
<textarea class="form-control" id="message" name="message" rows="8" required><?php echo htmlspecialchars($message); ?></textarea>
</div>
<!-- Attachments Drop Zone -->
<div class="mb-3">
<label class="form-label"><?php echo t('attachments'); ?></label>
<div id="drop-zone" class="drop-zone">
<p class="drop-zone-text"><i class="bi bi-cloud-arrow-up-fill fs-2"></i><br><?php echo t('drop_files_here'); ?></p>
</div>
<input type="file" id="attachment-input" name="attachments[]" multiple style="display: none;">
<div id="file-list" class="mt-3"></div>
</div>
<button type="submit" class="btn btn-primary"><?php echo t('save_email'); ?></button>
</form>
</div>
</div>
<div class="card">
<div class="card-header"><?php echo t('saved_emails'); ?></div>
<div class="card-body">
<?php if (isset($db_error)): ?>
<div class="alert alert-danger"><?php echo $db_error; ?></div>
<?php elseif (empty($saved_emails)): ?>
<p><?php echo t('no_saved_emails'); ?></p>
<?php else:
?>
<?php foreach ($saved_emails as $email):
?>
<div class="email-item border-bottom pb-3 mb-3">
<p><strong><?php echo t('from'); ?>:</strong> <?php echo htmlspecialchars($email['sender']); ?></p>
<p><strong><?php echo t('subject'); ?>:</strong> <?php echo htmlspecialchars($email['subject']); ?></p>
<p><strong><?php echo t('message'); ?>:</strong></p>
<pre class="bg-light p-2 rounded"><?php echo htmlspecialchars($email['message']); ?></pre>
<?php
if (!empty($email['attachments'])) {
$attachments = json_decode($email['attachments'], true);
if (is_array($attachments) && count($attachments) > 0) {
echo '<p><strong>' . t('attachments') . ':</strong></p><ul>';
foreach ($attachments as $attachment) {
echo '<li><a href="' . htmlspecialchars($attachment) . '" target="_blank">' . htmlspecialchars(basename($attachment)) . '</a></li>';
}
echo '</ul>';
}
}
?>
<small class="text-muted"><?php echo t('date'); ?>: <?php echo $email['created_at']; ?></small>
</div>
<?php endforeach;
?>
<?php endif; ?>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const dropZone = document.getElementById('drop-zone');
const attachmentInput = document.getElementById('attachment-input');
const fileList = document.getElementById('file-list');
// Open file dialog when drop zone is clicked
dropZone.addEventListener('click', () => attachmentInput.click());
// Handle drag and drop events
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('drag-over');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('drag-over');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('drag-over');
const files = e.dataTransfer.files;
attachmentInput.files = files;
updateFileList();
});
// Handle file selection from dialog
attachmentInput.addEventListener('change', () => {
updateFileList();
});
function updateFileList() {
fileList.innerHTML = ''; // Clear existing list
if (attachmentInput.files.length > 0) {
const list = document.createElement('ul');
list.className = 'list-group';
for (const file of attachmentInput.files) {
const item = document.createElement('li');
item.className = 'list-group-item d-flex justify-content-between align-items-center';
item.textContent = file.name;
const badge = document.createElement('span');
badge.className = 'badge bg-primary rounded-pill';
badge.textContent = `${(file.size / 1024).toFixed(2)} KB`;
item.appendChild(badge);
list.appendChild(item);
}
fileList.appendChild(list);
}
}
</script>
</body>
</html>