diff --git a/lang/ar.php b/lang/ar.php index 83d482f..5bff107 100644 --- a/lang/ar.php +++ b/lang/ar.php @@ -22,7 +22,7 @@ $lang = [ // Manual Email "option_1" => "الخيار 1: رفع ملف بريد إلكتروني", - "choose_email_file" => "اختر ملف البريد الإلكتروني (.eml أو .txt)", + "choose_email_file" => "اختر ملفًا (.eml, .txt, .pdf, .doc, .docx)", "analyze_and_populate" => "تحليل الملف وتعبئة الحقول", "option_2" => "الخيار 2: إدخال يدوي", "from" => "من", @@ -43,6 +43,10 @@ $lang = [ "language" => "اللغة", "arabic" => "العربية", "english" => "English", - "french" => "Français" + "french" => "Français", + + // أخطاء تحميل الملفات + "unsupported_file_type" => "نوع الملف غير مدعوم. يرجى رفع ملف من نوع .eml, .txt, .pdf, .doc, or .docx.", + "error_parsing_file" => "تعذر استخراج النص من الملف. قد يكون فارغًا أو تالفًا أو محميًا بكلمة مرور." ]; ?> \ No newline at end of file diff --git a/lang/en.php b/lang/en.php index ae5bae2..35e1a2e 100644 --- a/lang/en.php +++ b/lang/en.php @@ -22,7 +22,7 @@ $lang = [ // Manual Email "option_1" => "Option 1: Upload Email File", - "choose_email_file" => "Choose an email file (.eml or .txt)", + "choose_email_file" => "Choose a file (.eml, .txt, .pdf, .doc, .docx)", "analyze_and_populate" => "Analyze and Populate Fields", "option_2" => "Option 2: Manual Entry", "from" => "From", @@ -43,6 +43,10 @@ $lang = [ "language" => "Language", "arabic" => "العربية", "english" => "English", - "french" => "Français" + "french" => "Français", + + // File Upload Errors + "unsupported_file_type" => "Unsupported file type. Please upload a .eml, .txt, .pdf, .doc, or .docx file.", + "error_parsing_file" => "Could not extract text from the file. It might be empty, corrupted, or password-protected." ]; ?> \ No newline at end of file diff --git a/lang/fr.php b/lang/fr.php index c3b7b24..c74ce7b 100644 --- a/lang/fr.php +++ b/lang/fr.php @@ -22,7 +22,7 @@ $lang = [ // Manual Email "option_1" => "Option 1 : Télécharger un fichier e-mail", - "choose_email_file" => "Choisissez un fichier e-mail (.eml ou .txt)", + "choose_email_file" => "Choisissez un fichier (.eml, .txt, .pdf, .doc, .docx)", "analyze_and_populate" => "Analyser et remplir les champs", "option_2" => "Option 2 : Saisie manuelle", "from" => "De", @@ -43,6 +43,10 @@ $lang = [ "language" => "Langue", "arabic" => "العربية", "english" => "English", - "french" => "Français" + "french" => "Français", + + // Erreurs de téléchargement de fichiers + "unsupported_file_type" => "Type de fichier non pris en charge. Veuillez télécharger un fichier .eml, .txt, .pdf, .doc ou .docx.", + "error_parsing_file" => "Impossible d'extraire le texte du fichier. Il est peut-être vide, corrompu ou protégé par un mot de passe." ]; ?> \ No newline at end of file diff --git a/manual_email.php b/manual_email.php index 64bd062..c1157a9 100644 --- a/manual_email.php +++ b/manual_email.php @@ -11,32 +11,62 @@ $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); + $file_name = $_FILES['email_file']['name']; + $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); - if ($file_content) { - // Basic EML parsing - $headers = []; - $body_started = false; - $body_lines = []; - $lines = explode("\n", $file_content); + $message = ''; // Initialize message + $subject = pathinfo($file_name, PATHINFO_FILENAME); // Use filename as subject by default + $from = ''; // From is not available in these files - 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 + // IMPORTANT: Ensure the temp file path is properly escaped to prevent command injection + $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 'eml': + case 'txt': + $file_content = file_get_contents($file_tmp_path); + if ($file_content) { + // Basic EML parsing + $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 { - $body_lines[] = $line; + $upload_error = 'Failed to read uploaded file.'; } - } - $message = implode("\n", $body_lines); - } else { - $upload_error = 'Failed to read uploaded file.'; + break; + default: + $upload_error = 'Unsupported file type. Please upload a .eml, .txt, .pdf, .doc, or .docx file.'; + break; } + + if (empty($message) && empty($upload_error)) { + $upload_error = 'Could not extract text from the file. It might be empty, corrupted, or password-protected.'; + } + } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { // Handle Manual Form Submission $from = isset($_POST['from']) ? $_POST['from'] : ''; @@ -129,7 +159,7 @@ try {
- +