#2 Début de résolution pour les caractères spéciaux, plus dans le front, mais toujour dans l'archive

This commit is contained in:
David 2023-03-26 22:51:51 +02:00
parent 69db3a5e1e
commit 9089f59a44
2 changed files with 95 additions and 35 deletions

View file

@ -1,3 +1,53 @@
/* Se RFC 2060 - no / ~ \ in folder names */
function ureplacer(pmatch) {
var ret = ""
pmatch = pmatch.replace(/\,/g,'/')
var ix = pmatch.substr(1,pmatch.length-2)
if (ix.length % 4 != 0)
ix = ix.padEnd(ix.length+ 4 - ix.length % 4,"=")
try {
var dx = atob(ix)
for (var j = 0; j < dx.length; j = j+2) {
ret = ret + String.fromCharCode((dx.charCodeAt(j) << 8) + dx.charCodeAt(j+1))
}
} catch(err) {
console.log("Error in decoding foldername IMAP UTF7, sending empty string back")
console.log(err)
ret = ""
}
return ret
}
function breplacer(umatch) {
var bst = ""
for (var i=0; i < umatch.length; i++) {
var f = umatch.charCodeAt(i)
bst = bst + String.fromCharCode(f >> 8) + String.fromCharCode(f & 255)
}
try {
bst = '&'+btoa(bst).replace(/\//g,',').replace(/=+/,'')+'-'
}catch(err) {
console.log("Error in encoding foldername IMAP UTF7, sending empty string back")
console.log(err)
bst = ""
}
return bst
}
function decode_imap_utf7(mstring) {
var stm = new RegExp(/(\&[A-Za-z0-9\+\,]+\-)/,'g')
return mstring.replace(stm,ureplacer).replace('&-','&')
}
function encode_imap_utf7(ustring) {
ustring = ustring.replace(/\/|\~|\\/g,'')
var vgm = new RegExp(/([^\x20-\x7e]+)/,'g')
return ustring.replace('&','&-').replace(vgm,breplacer)
}
function convertOctect2humain(value) { function convertOctect2humain(value) {
if (value > 1000000000) { if (value > 1000000000) {
returnVal=Math.round(value/1024/1024/1024, 1)+'Go'; returnVal=Math.round(value/1024/1024/1024, 1)+'Go';
@ -113,9 +163,9 @@ function imapDetectConfig() {
} }
$('#imapfolder').append('<ul>'); $('#imapfolder').append('<ul>');
if (regexSelected.test(item)) { if (regexSelected.test(item)) {
$('#imapfolder').append('<li><input type="checkbox" name="f1-imapfolder[]" class="f1-imapfolder" value="'+item+'" checked="checked"> ' + item+'</li>'); $('#imapfolder').append('<li><input type="checkbox" name="f1-imapfolder[]" class="f1-imapfolder" value="'+item+'" checked="checked"> ' + decode_imap_utf7(item)+'</li>');
}else{ }else{
$('#imapfolder').append('<li><input type="checkbox" name="f1-imapfolder[]" class="f1-imapfolder" value="'+item+'"> ' + item +'</li>'); $('#imapfolder').append('<li><input type="checkbox" name="f1-imapfolder[]" class="f1-imapfolder" value="'+item+'"> ' + decode_imap_utf7(item) +'</li>');
} }
$('#imapfolder').append('</ul>'); $('#imapfolder').append('</ul>');
}); });
@ -338,7 +388,7 @@ jQuery(document).ready(function() {
var tableBody = $("#folderPreviewList tbody"); var tableBody = $("#folderPreviewList tbody");
for (let key in data['folder']){ for (let key in data['folder']){
if(data['folder'].hasOwnProperty(key)){ if(data['folder'].hasOwnProperty(key)){
tableBody.append('<tr><td>'+key+'</td><td>'+data['folder'][key]['nb']+'</td><td>'+convertOctect2humain(data['folder'][key]['size'])+'</td></tr>'); tableBody.append('<tr><td>'+decode_imap_utf7(key)+'</td><td>'+data['folder'][key]['nb']+'</td><td>'+convertOctect2humain(data['folder'][key]['size'])+'</td></tr>');
} }
} }
tableBody.append('<tr><td>Total</td><td>&nbsp;</td><td>'+convertOctect2humain(data['totalSize'])+'</td><td></td></tr>'); tableBody.append('<tr><td>Total</td><td>&nbsp;</td><td>'+convertOctect2humain(data['totalSize'])+'</td><td></td></tr>');

View file

@ -184,7 +184,7 @@ function imapTestCon($session_id, $domain, $server, $port, $user, $password, $se
$list = imap_list($mailbox, "{".$server."}", "*"); $list = imap_list($mailbox, "{".$server."}", "*");
if (is_array($list)) { if (is_array($list)) {
foreach ($list as $val) { foreach ($list as $val) {
$return['folder'][] = str_replace("{".$server."}", '', utf8_encode(imap_utf7_decode($val))); $return['folder'][] = str_replace("{".$server."}", '', $val);
} }
} }
imap_close($mailbox); imap_close($mailbox);
@ -424,10 +424,10 @@ function saveMessage($session_id, $idClean, $parser, $header, $messageEML, $fold
$attachments = $parser->getAttachments(); $attachments = $parser->getAttachments();
if ($format != 'eml' && count($attachments) != 0) { if ($format != 'eml' && count($attachments) != 0) {
toLog(5, "Gestion des ". count($attachments) ." pièce(s) jointe(s)"); toLog(5, "Gestion des ". count($attachments) ." pièce(s) jointe(s)");
mkdir($config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean); mkdir($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean);
//var_dump($attachments); //var_dump($attachments);
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
$attachment->save($config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean); $attachment->save($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean);
$arrayTmp['attachmentsFilename'][]=$attachment->getFilename(); $arrayTmp['attachmentsFilename'][]=$attachment->getFilename();
} }
} }
@ -484,18 +484,18 @@ function saveMessage($session_id, $idClean, $parser, $header, $messageEML, $fold
// Recherche des formats supportés : // Recherche des formats supportés :
$text = $parser->getMessageBody('text'); $text = $parser->getMessageBody('text');
if ($text) { if ($text) {
file_put_contents($config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.txt', $headerTxt.$text); file_put_contents($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.txt', $headerTxt.$text);
} }
$html = $parser->getMessageBody('html'); $html = $parser->getMessageBody('html');
$htmlEmbedded = $parser->getMessageBody('htmlEmbedded'); $htmlEmbedded = $parser->getMessageBody('htmlEmbedded');
if ($htmlEmbedded) { if ($htmlEmbedded) {
file_put_contents($config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.html', $headerHtml.$htmlEmbedded); file_put_contents($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.html', $headerHtml.$htmlEmbedded);
} else if ($html) { } else if ($html) {
file_put_contents($config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.html', $headerHtml.$html); file_put_contents($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.html', $headerHtml.$html);
} }
} else { } else {
// toLog(5, "Export en EML"); // toLog(5, "Export en EML");
file_put_contents($config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.eml', $messageEML); file_put_contents($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.$folder.'/'.$idClean.'.eml', $messageEML);
} }
unset($arrayTmp); unset($arrayTmp);
} }
@ -503,9 +503,9 @@ function saveMessage($session_id, $idClean, $parser, $header, $messageEML, $fold
function imapGetData($mod, $session_id, $server, $port, $user, $password, $secure, $auth, $cert, $imapfolder, $dateSince, $dateBefore, $what, $format) { function imapGetData($mod, $session_id, $server, $port, $user, $password, $secure, $auth, $cert, $imapfolder, $dateSince, $dateBefore, $what, $format) {
global $config; global $config;
$parser = new PhpMimeMailParser\Parser(); $parser = new PhpMimeMailParser\Parser();
if (is_dir($config['dir'][$mod].'/'.$session_id)) { if (is_dir($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id)) {
toLog(5, "Ménage du répertoire ".$config['dir'][$mod].'/'.$session_id); toLog(5, "Ménage du répertoire ".$config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id);
rrmdir($config['dir'][$mod].'/'.$session_id); rrmdir($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id);
} }
$return['result'] = true; $return['result'] = true;
// Get folder information // Get folder information
@ -518,8 +518,8 @@ function imapGetData($mod, $session_id, $server, $port, $user, $password, $secur
toLog(2, "Connexion error"); toLog(2, "Connexion error");
} else { } else {
if ($mod != 'preview') { if ($mod != 'preview') {
toLog(5, "Création du répertoire ".$config['dir'][$mod].'/'.$session_id); toLog(5, "Création du répertoire ".$config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id);
mkdir($config['dir'][$mod].'/'.$session_id); mkdir($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id);
} }
//$dataFolderJson = array(); //$dataFolderJson = array();
$dataJson = array(); $dataJson = array();
@ -530,12 +530,13 @@ function imapGetData($mod, $session_id, $server, $port, $user, $password, $secur
// Connexion sur le dossier // Connexion sur le dossier
toLog(5, "Connexion sur le dossier ".$folder); toLog(5, "Connexion sur le dossier ".$folder);
$serverImapOpenString = serverImapOpenString($server, $port, $secure, $auth, $cert, $folder, $format); $serverImapOpenString = serverImapOpenString($server, $port, $secure, $auth, $cert, $folder, $format);
sleep(2);
imap_reopen($mailbox, $serverImapOpenString, null, 0); imap_reopen($mailbox, $serverImapOpenString, null, 0);
$info = imap_check($mailbox); $info = imap_check($mailbox);
// Vérification de l'existance du dossier // Vérification de l'existance du dossier
if (FALSE !== $info) { if (FALSE !== $info) {
if ($mod != 'preview') { if ($mod != 'preview') {
mkdir($config['dir'][$mod].'/'.$session_id.'/'.folderCleanName($folder)); mkdir($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/'.folderCleanName($folder));
} }
// Avant // Avant
$dateBeforeForImap = date ( "d-M-Y", $dateBefore); $dateBeforeForImap = date ( "d-M-Y", $dateBefore);
@ -556,24 +557,33 @@ function imapGetData($mod, $session_id, $server, $port, $user, $password, $secur
if ($mod != 'preview') { if ($mod != 'preview') {
// EML format message : // EML format message :
$messageEML = imap_fetchbody($mailbox,$mail, ''); $messageEML = imap_fetchbody($mailbox,$mail, '');
// Parse email // Evite bug "You must not call MimeMailParser::setText with an empty string parameter..." du parser juste après
$parser->setText($messageEML); if ($messageEML != '') {
// archive // Parse email
//createArchive($session_id,$mailbox,$mail); $parser->setText($messageEML);
// export // archive
if ($header->Deleted != 'D') { //createArchive($session_id,$mailbox,$mail);
//~ if ($mod != 'preview') { // export
//~ array_push($dataJson, jsonMessage($mod, $parser, $header, $messageEML, $folder, $format)); if ($header->Deleted != 'D') {
//~ } else { //~ if ($mod != 'preview') {
//~ array_push($dataJson, jsonMessage($mod, $parser, $header, $messageEML, $folder, $format));
//~ } else {
// Bug des messages sans id, on en génère un // Bug des messages sans id, on en génère un
if (empty($header->message_id)) { if (empty($header->message_id)) {
$idClean=rand(1000, mt_getrandmax()).rand(1000, mt_getrandmax()).rand(1000, mt_getrandmax()); $idClean=rand(1000, mt_getrandmax()).rand(1000, mt_getrandmax()).rand(1000, mt_getrandmax());
} else { } else {
$idClean=cleanTxt(substr(substr($header->message_id, 0, -1), 1)); $idClean=cleanTxt(substr(substr($header->message_id, 0, -1), 1));
}
array_push($dataJson, jsonMessage($mod, $idClean, $parser, $header, $messageEML, $folder, $format));
saveMessage($session_id, $idClean, $parser, $header, $messageEML, $folder, $format, $mod);
} }
array_push($dataJson, jsonMessage($mod, $idClean, $parser, $header, $messageEML, $folder, $format)); } else {
saveMessage($session_id, $idClean, $parser, $header, $messageEML, $folder, $format, $mod); var_dump($messageEML);
var_dump($header);
toLog(1, "Un message ignoré (".$session_id.")");
$return['result'] = false;
$return['resultMsg'] = '(probablement) Perte de connexion IMAP';
} }
} }
} }
@ -590,8 +600,8 @@ function imapGetData($mod, $session_id, $server, $port, $user, $password, $secur
if ($mod != 'preview') { if ($mod != 'preview') {
toLog(4, "Enregistrement json/js messages"); toLog(4, "Enregistrement json/js messages");
if (count($dataJson) > 0) { if (count($dataJson) > 0) {
file_put_contents($config['dir'][$mod].'/'.$session_id.'/messages.json', json_encode($dataJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE)); file_put_contents($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/messages.json', json_encode($dataJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE));
file_put_contents($config['dir'][$mod].'/'.$session_id.'/messages.js', 'var messages_json = '.json_encode($dataJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE).';'); file_put_contents($config['dir']['absolut'].'/'.$config['dir'][$mod].'/'.$session_id.'/messages.js', 'var messages_json = '.json_encode($dataJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE).';');
if (json_last_error() != 0) { if (json_last_error() != 0) {
toLog(1, "Erreur dans le json : ".json_last_error()); toLog(1, "Erreur dans le json : ".json_last_error());
} }