@@ -320,7 +320,7 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
320320 array ('subject_callback ' , $ subject , $ url , $ flags , $ icon , $ preview_msg ),
321321 array ('safe_output_callback ' , 'source ' , $ source ),
322322 array ('safe_output_callback ' , 'from ' .$ nofrom , $ from , null , str_replace (array ($ from , '< ' , '> ' ), '' , $ msg ['from ' ])),
323- array ('date_callback ' , $ date , $ timestamp ),
323+ array ('date_callback ' , $ date , $ timestamp, $ is_snoozed || $ is_scheduled ),
324324 array ('dates_holders_callback ' , $ msg ['internal_date ' ], $ msg ['date ' ]),
325325 ),
326326 $ id ,
@@ -336,7 +336,8 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
336336 array ('safe_output_callback ' , 'from ' .$ nofrom , $ from , null , str_replace (array ($ from , '< ' , '> ' ), '' , $ msg ['from ' ])),
337337 array ('subject_callback ' , $ subject , $ url , $ flags , null , $ preview_msg ),
338338 array ('date_callback ' , $ date , $ timestamp , $ is_snoozed || $ is_scheduled ),
339- array ('icon_callback ' , $ flags )
339+ array ('icon_callback ' , $ flags ),
340+ array ('dates_holders_callback ' , $ msg ['internal_date ' ], $ msg ['date ' ]),
340341 ),
341342 $ id ,
342343 $ style ,
@@ -1608,9 +1609,34 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima
16081609 }
16091610}
16101611
1611- function getCombinedMessagesLists ($ sources , $ cache , $ searchTerms , $ listPage , $ limit , $ offsets = [], $ defaultOffset = 0 , $ filter = 'ALL ' ) {
1612+ /**
1613+ * @param array $sources
1614+ * @param object $cache
1615+ * @param array $search
1616+ */
1617+ function getCombinedMessagesLists ($ sources , $ cache , $ search ) {
1618+ $ defaultSearch = [
1619+ 'filter ' => 'ALL ' ,
1620+ 'sort ' => 'ARRIVAL ' ,
1621+ 'reverse ' => true ,
1622+ 'terms ' => [],
1623+ 'limit ' => 10 ,
1624+ 'offsets ' => [],
1625+ 'defaultOffset ' => 0 ,
1626+ 'listPage ' => 1
1627+ ];
1628+ $ search = array_merge ($ defaultSearch , $ search );
1629+
1630+ $ filter = $ search ['filter ' ];
1631+ $ sort = $ search ['sort ' ];
1632+ $ reverse = $ search ['reverse ' ];
1633+ $ searchTerms = $ search ['terms ' ];
1634+ $ limit = $ search ['limit ' ];
1635+ $ offsets = $ search ['offsets ' ];
1636+ $ listPage = $ search ['listPage ' ];
1637+
16121638 $ totalMessages = 0 ;
1613- $ offset = $ defaultOffset ;
1639+ $ offset = $ search [ ' defaultOffset ' ] ;
16141640 $ messagesLists = [];
16151641 $ status = [];
16161642 foreach ($ sources as $ index => $ dataSource ) {
@@ -1623,14 +1649,27 @@ function getCombinedMessagesLists($sources, $cache, $searchTerms, $listPage, $li
16231649
16241650 $ mailbox = Hm_IMAP_List::get_connected_mailbox ($ dataSource ['id ' ], $ cache );
16251651 if ($ mailbox && $ mailbox ->authed ()) {
1652+ $ connection = $ mailbox ->get_connection ();
1653+
16261654 $ folder = $ dataSource ['folder ' ];
1627- $ state = $ mailbox ->get_connection ()->get_mailbox_status (hex2bin ($ folder ));
1655+ $ mailbox ->select_folder (hex2bin ($ folder ));
1656+ $ state = $ connection ->get_mailbox_status (hex2bin ($ folder ));
16281657 $ status ['imap_ ' .$ dataSource ['id ' ].'_ ' .$ folder ] = $ state ;
16291658
1630- $ uids = $ mailbox ->search (hex2bin ($ folder ), $ filter , false , $ searchTerms );
1659+ if ($ mailbox ->is_imap ()) {
1660+ if ($ connection ->is_supported ( 'SORT ' )) {
1661+ $ sortedUids = $ connection ->get_message_sort_order ($ sort , $ reverse , $ filter );
1662+ } else {
1663+ $ sortedUids = $ connection ->sort_by_fetch ($ sort , $ reverse , $ filter );
1664+ }
1665+
1666+ $ uids = $ mailbox ->search (hex2bin ($ folder ), $ filter , $ sortedUids , $ searchTerms );
1667+ } else {
1668+ // EWS
1669+ $ uids = $ connection ->search ($ folder , $ sort , $ reverse , $ filter , 0 , $ limit , $ searchTerms );
1670+ }
1671+
16311672 $ total = count ($ uids );
1632- // most recent messages at the top
1633- $ uids = array_reverse ($ uids );
16341673 $ uids = array_slice ($ uids , $ offset , $ limit );
16351674
16361675 $ headers = $ mailbox ->get_message_list (hex2bin ($ folder ), $ uids );
@@ -1682,6 +1721,18 @@ function flattenMessagesLists($messagesLists, $listSize) {
16821721 return ['messages ' => $ endList , 'offsets ' => $ sizesTaken ];
16831722}
16841723
1724+ function sortCombinedMessages ($ list , $ sort ) {
1725+ usort ($ list , function ($ a , $ b ) use ($ sort ) {
1726+ $ sortField = str_replace (['arrival ' , '- ' ], ['internal_date ' , '' ], $ sort );
1727+ if (strpos ($ sort , '- ' ) === 0 ) {
1728+ return strtotime ($ a [$ sortField ]) - strtotime ($ b [$ sortField ]);
1729+ }
1730+ return strtotime ($ b [$ sortField ]) - strtotime ($ a [$ sortField ]);
1731+ });
1732+
1733+ return $ list ;
1734+ }
1735+
16851736if (!hm_exists ('save_sent_msg ' )) {
16861737function save_sent_msg ($ handler , $ imap_id , $ mailbox , $ imap_details , $ msg , $ msg_id , $ show_errors = true ) {
16871738 $ specials = get_special_folders ($ handler , $ imap_id );
0 commit comments