35227-vm/manual_email.php
Flatlogic Bot c5ff318833 v2
2025-10-25 15:54:17 +00:00

187 lines
8.7 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_content = file_get_contents($file_tmp_path);
if ($file_content) {
// Basic EML parsing
$headers = [];
$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; // Headers are done, body begins
}
} else {
$body_lines[] = $line;
}
}
$message = implode("\n", $body_lines);
} else {
$upload_error = 'Failed to read uploaded file.';
}
} 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'] : '';
if ($from && $subject && $message) {
try {
$pdo = db();
$stmt = $pdo->prepare('INSERT INTO manual_emails (sender, subject, message) VALUES (?, ?, ?)');
$stmt->execute([$from, $subject, $message]);
$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, 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(); ?>">
</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">
</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">
<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>
<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>
<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>
</body>
</html>