Skip to content

Commit 345c5f5

Browse files
committed
fix(backend): fix date formatting in message retrieval
1 parent 1c7f743 commit 345c5f5

File tree

2 files changed

+12
-94
lines changed

2 files changed

+12
-94
lines changed

modules/imap/handler_modules.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,6 @@ public function process() {
550550
$this->out('list_filter', $this->request->get['filter']);
551551
}
552552
}
553-
554-
// Handle folder conversion - both IMAP and EWS use hex2bin but for different purposes
555-
// IMAP: hex2bin("494e424f58") -> "INBOX" (folder name)
556-
// EWS: hex2bin("41414d6b...") -> "AAMkADk5..." (base64 Exchange folder ID)
557553
$folder = hex2bin($parts[2]);
558554
$spcial_folders = get_special_folders($this, $parts[1]);
559555
if (array_key_exists(strtolower($folder), $spcial_folders)) {
@@ -563,21 +559,13 @@ public function process() {
563559
if (array_key_exists('folder_label', $this->request->get)) {
564560
$folder = $this->request->get['folder_label'];
565561
$this->out('folder_label', $folder);
562+
} else {
563+
$folder = hex2bin($parts[2]);
566564
}
567565

568566
$mailbox = Hm_IMAP_List::get_mailbox_without_connection($details);
569567
$label = $mailbox->get_folder_name($folder);
570-
571-
// For EWS, if we can't get a friendly name, try to use a connected mailbox
572-
if (!$label && isset($details['type']) && $details['type'] === 'ews') {
573-
$connected_mailbox = Hm_IMAP_List::get_connected_mailbox($parts[1], $this->cache);
574-
if ($connected_mailbox && $connected_mailbox->authed()) {
575-
$folder_status = $connected_mailbox->get_folder_status($folder, false);
576-
$label = $folder_status['name'] ?? null;
577-
}
578-
}
579-
580-
$title = array(strtoupper($details['type'] ?? 'IMAP'), $details['name'], $label ?: $folder);
568+
$title = array(strtoupper($details['type'] ?? 'IMAP'), $details['name'], $label);
581569
if ($this->get('list_page', 0)) {
582570
$title[] = sprintf('Page %d', $this->get('list_page', 0));
583571
}
@@ -947,7 +935,7 @@ public function process() {
947935
}
948936

949937
$mailbox = Hm_IMAP_List::get_connected_mailbox($form['imap_server_id'], $this->cache);
950-
if ($mailbox && ! $mailbox->is_imap() && empty($archive_folder)) {
938+
if ($mailbox && ! $mailbox->is_imap()) {
951939
// EWS supports archiving to user archive folders
952940
$status = $mailbox->message_action($form_folder, 'ARCHIVE', array($form['imap_msg_uid']))['status'];
953941
} else {

modules/imap/hm-ews.php

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function find_folder_by_name($folderName, $parentFolder = null) {
201201
return [
202202
'id' => bin2hex($folder->getFolderId()->getId()),
203203
'name' => $folder->getDisplayName(),
204-
'parent' => bin2hex($folder->getParentFolderId()->getId()),
204+
'parent' => !is_null($folder->getParentFolderId()) ? bin2hex($folder->getParentFolderId()->getId()) : null,
205205
'messages' => $folder->getTotalCount() ?? 0,
206206
'unseen' => $folder->getUnreadCount() ?? 0,
207207
];
@@ -220,12 +220,9 @@ public function get_folder_status($folder, $report_error = true) {
220220
$folderObj = new Type\DistinguishedFolderIdType($folder);
221221
$result = $this->api->getFolder($folderObj->toArray(true));
222222
} elseif (ctype_xdigit($folder) || base64_encode(base64_decode($folder, true)) === $folder) {
223-
if(!hex2bin($folder)) {
224-
$folderObj = new Type\FolderIdType($folder);
225-
}else {
226-
$folderObj = new Type\FolderIdType(hex2bin($folder));
227-
}
223+
$folderObj = new Type\FolderIdType(hex2bin($folder));
228224
$result = $this->api->getFolder($folderObj->toArray(true));
225+
229226
} else {
230227
// TODO: To be removed, we needed it only while testing
231228
// Try to find by display name using our new function
@@ -241,7 +238,8 @@ public function get_folder_status($folder, $report_error = true) {
241238
'unseen' => $folderData['unseen'],
242239
];
243240
} else {
244-
throw new Exception('Folder not found: ' . $folder);
241+
return [];
242+
// ; throw new Exception('Folder not found: ' . $folder);
245243
}
246244
}
247245
return [
@@ -637,9 +635,9 @@ public function get_message_list($itemIds, $include_preview = false) {
637635
$msg = [
638636
'uid' => $uid,
639637
'flags' => implode(' ', $flags),
640-
'internal_date' => $message->getDateTimeCreated()->format('Y-m-d H:i:s.u'),
638+
'internal_date' => $message->getDateTimeCreated()->format('d-M-Y H:i:s O'),
641639
'size' => $message->getSize(),
642-
'date' => $message->getDateTimeReceived()->format('Y-m-d H:i:s.u'),
640+
'date' => $message->getDateTimeReceived()->format('d-M-Y H:i:s O'),
643641
'from' => $this->extract_mailbox($message->getFrom()),
644642
'to' => $this->extract_mailbox($message->getToRecipients()),
645643
'subject' => $message->getSubject(),
@@ -975,27 +973,14 @@ protected function parse_mime_part($part, &$struct, $part_num) {
975973
$struct[$part_num]['md5'] = '';
976974
$struct[$part_num]['disposition'] = $part->getContentDisposition();
977975

978-
$filename = $this->extract_attachment_filename($part);
979-
980-
if ($filename) {
981-
$struct[$part_num]['attributes']['name'] = $filename;
982-
$struct[$part_num]['attributes']['filename'] = $filename;
976+
if ($filename = $part->getFilename()) {
983977
$struct[$part_num]['file_attributes'] = ['filename' => $filename];
984-
$struct[$part_num]['name'] = $filename;
985-
$struct[$part_num]['description'] = $filename;
986-
$struct[$part_num]['filename'] = $filename;
987978

988979
if ($part->getContentDisposition() == 'attachment') {
989980
$struct[$part_num]['file_attributes']['attachment'] = true;
990-
$struct[$part_num]['disposition'] = [
991-
'attachment' => ['filename', $filename]
992-
];
993981
}
994982
} else {
995983
$struct[$part_num]['file_attributes'] = '';
996-
if (!$struct[$part_num]['description']) {
997-
$struct[$part_num]['description'] = '';
998-
}
999984
}
1000985
$struct[$part_num]['language'] = '';
1001986
$struct[$part_num]['location'] = '';
@@ -1041,61 +1026,6 @@ protected function search_mime_part_in_struct($struct, $conditions, $all = false
10411026
return $found;
10421027
}
10431028

1044-
protected function extract_attachment_filename($part) {
1045-
$filename = $part->getFilename();
1046-
if ($filename) {
1047-
return $filename;
1048-
}
1049-
1050-
$filename = $part->getHeaderParameter('Content-Disposition', 'filename');
1051-
if ($filename) {
1052-
return $this->decode_attachment_name($filename);
1053-
}
1054-
1055-
$filename = $part->getHeaderParameter('Content-Type', 'name');
1056-
if ($filename) {
1057-
return $this->decode_attachment_name($filename);
1058-
}
1059-
1060-
$filename = $part->getHeaderParameter('Content-Disposition', 'name');
1061-
if ($filename) {
1062-
return $this->decode_attachment_name($filename);
1063-
}
1064-
1065-
$description = $part->getHeaderValue('Content-Description');
1066-
if ($description && strlen($description) < 100) {
1067-
return $description;
1068-
}
1069-
1070-
return null;
1071-
}
1072-
1073-
protected function decode_attachment_name($name) {
1074-
$name = trim($name, '"\'');
1075-
1076-
if (preg_match('/=\?([^?]+)\?([BQ])\?([^?]+)\?=/i', $name, $matches)) {
1077-
$charset = $matches[1];
1078-
$encoding = strtoupper($matches[2]);
1079-
$text = $matches[3];
1080-
1081-
if ($encoding === 'B') {
1082-
$decoded = base64_decode($text);
1083-
} elseif ($encoding === 'Q') {
1084-
$decoded = quoted_printable_decode(str_replace('_', ' ', $text));
1085-
} else {
1086-
$decoded = $text;
1087-
}
1088-
1089-
return mb_convert_encoding($decoded, 'UTF-8', $charset);
1090-
}
1091-
1092-
if (strpos($name, '%') !== false) {
1093-
$name = urldecode($name);
1094-
}
1095-
1096-
return $name;
1097-
}
1098-
10991029
protected function extract_mailbox($data) {
11001030
if (is_array($data)) {
11011031
$result = [];

0 commit comments

Comments
 (0)