Skip to content

Commit fcbc065

Browse files
amaninyumu1josaphatim
authored andcommitted
feat(backend): Fix php error Call to a member function authed() on null
1 parent aed9a95 commit fcbc065

File tree

4 files changed

+76
-27
lines changed

4 files changed

+76
-27
lines changed

modules/core/hm-mailbox.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public function server_type() {
7474
}
7575

7676
public function authed() {
77+
if (! $this->connection) {
78+
return false;
79+
}
7780
if ($this->is_imap()) {
7881
return $this->connection->get_state() == 'authenticated' || $this->connection->get_state() == 'selected';
7982
} elseif ($this->is_smtp()) {

modules/core/site.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -528,21 +528,11 @@ function Message_List() {
528528
var aval;
529529
var bval;
530530
var sort_result = listitems.sort(function(a, b) {
531-
switch (Math.abs(fld)) {
532-
case 1:
533-
case 2:
534-
case 3:
535-
aval = $($('td', a)[Math.abs(fld)]).text().replace(/^\s+/g, '');
536-
bval = $($('td', b)[Math.abs(fld)]).text().replace(/^\s+/g, '');
537-
break;
538-
case 4:
539-
default:
540-
aval = $('input', $($('td', a)[Math.abs(fld)])).val();
541-
bval = $('input', $($('td', b)[Math.abs(fld)])).val();
542-
break;
543-
}
544-
if (fld == 4 || fld == -4 || !fld) {
545-
if (fld == -4) {
531+
const sortField = fld.replace('-', '');
532+
if (['arrival', 'date'].includes(sortField)) {
533+
aval = new Date($(`input.${sortField}`, $('td.dates', a)).val());
534+
bval = new Date($(`input.${sortField}`, $('td.dates', b)).val());
535+
if (fld.startsWith('-')) {
546536
return aval - bval;
547537
}
548538
return bval - aval;
@@ -1735,6 +1725,12 @@ var hm_spinner = function(type = 'border', size = '') {
17351725
</div>`
17361726
};
17371727

1728+
var hm_spinner_text = function(text, id = 'spinner-text') {
1729+
return `<div class="d-flex justify-content-between align-items-center p-2 border-bottom" id="${id}">
1730+
<span class="mailbox-name text-primary">${text}</span>
1731+
<span class="spinner-border spinner-border-sm text-primary" role="status" aria-hidden="true"></span>
1732+
</div>`;
1733+
};
17381734
var fillImapData = function(details) {
17391735
$('#srv_setup_stepper_imap_address').val(details.server);
17401736
$('#srv_setup_stepper_imap_port').val(details.port);
@@ -1811,7 +1807,6 @@ var hasLeadingOrTrailingSpaces = function(str) {
18111807
var Hm_Message_List = new Message_List();
18121808

18131809
function sortHandlerForMessageListAndSearchPage() {
1814-
$('.combined_sort').on("change", function() { Hm_Message_List.sort($(this).val()); });
18151810
$('.source_link').on("click", function() { $('.list_sources').toggle(); $('#list_controls_menu').hide(); return false; });
18161811
if (getListPathParam() == 'unread' && $('.menu_unread > a').css('font-weight') == 'bold') {
18171812
$('.menu_unread > a').css('font-weight', 'normal');

modules/imap/functions.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
16851736
if (!hm_exists('save_sent_msg')) {
16861737
function save_sent_msg($handler, $imap_id, $mailbox, $imap_details, $msg, $msg_id, $show_errors = true) {
16871738
$specials = get_special_folders($handler, $imap_id);

modules/smtp/modules.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ public function process() {
15761576

15771577
foreach ($servers as $server_id => $config) {
15781578
$mailbox = new Hm_Mailbox($server_id, $this->user_config, $this->session, $config);
1579-
if ($mailbox->get_connection() != null && $mailbox->connect()) {
1579+
if ($mailbox->authed()) {
15801580
$folder = 'Scheduled';
15811581
$ret = $mailbox->get_messages($folder, 'DATE', false, 'ALL');
15821582
foreach ($ret[1] as $msg) {
@@ -1621,7 +1621,7 @@ public function process() {
16211621
$imap_server = Hm_IMAP_List::getForMailbox($imap_server_id);
16221622

16231623
$mailbox = new Hm_Mailbox($imap_server_id, $this->user_config, $this->session, $imap_server);
1624-
if ($mailbox->get_connection() != null && $mailbox->connect()) {
1624+
if ($mailbox->authed()) {
16251625
$folder = hex2bin($folder);
16261626
if (reschedule_message_sending($this, $mailbox, $msg_id, $folder, $new_schedule_date)) {
16271627
$scheduled_msg_count++;
@@ -2047,7 +2047,7 @@ function save_imap_draft($atts, $id, $session, $mod, $mod_cache, $uploaded_files
20472047
return -1;
20482048
}
20492049
$mailbox = new Hm_Mailbox($imap_profile['id'], $mod->user_config, $session, $imap_profile);
2050-
if (! $mailbox || ! $mailbox->connect()) {
2050+
if (! $mailbox->authed()) {
20512051
return -1;
20522052
}
20532053

0 commit comments

Comments
 (0)