From 13b6353d4ae30782779f7dd5e7380da53278a205 Mon Sep 17 00:00:00 2001 From: Torben Dannhauer Date: Fri, 13 Nov 2026 18:39:59 +0100 Subject: [PATCH] fix(activesync): load user flags through the mail API Unserializing the msgflags preference with no allowed classes turned IMP flag objects into incomplete classes, which warned once per synced message and dropped the user's labels. Ask the mail app for mail/msgflags and cache the map for the request. --- lib/Horde/Core/ActiveSync/Imap/Factory.php | 79 +++++++--- .../ActiveSync/ImapFactoryMsgflagsTest.php | 140 ++++++++++++++++++ 2 files changed, 198 insertions(+), 21 deletions(-) create mode 100644 test/Unit/ActiveSync/ImapFactoryMsgflagsTest.php diff --git a/lib/Horde/Core/ActiveSync/Imap/Factory.php b/lib/Horde/Core/ActiveSync/Imap/Factory.php index f3e5c5160..a4b1a4152 100644 --- a/lib/Horde/Core/ActiveSync/Imap/Factory.php +++ b/lib/Horde/Core/ActiveSync/Imap/Factory.php @@ -7,6 +7,8 @@ * @package Core */ +use Horde\Util\HordeString; + /** * Horde_Core_ActiveSync_Imap_Factory implements a factory/builder for * providing a Horde_ActiveSync_Imap_Adapter object as well as building a tree @@ -23,6 +25,16 @@ class Horde_Core_ActiveSync_Imap_Factory implements Horde_ActiveSync_Interface_I protected $_mailboxlist; protected $_specialMailboxlist; + /** + * User keyword map from mail/msgflags, cached for this factory. + * + * Null until the first lookup. An empty array means the mail application + * has no user flags or does not expose the capability. + * + * @var array|null + */ + protected $_msgFlags = null; + /** * Logger. * @@ -190,37 +202,62 @@ public function getSpecialMailboxes() } /** - * Return a list of user-defined flags. + * Return user-defined IMAP keywords for the current user. + * + * Keys are lowercase flag keywords. Values are the display labels used + * as ActiveSync categories. The mail application owns the preference + * format and exposes the map as mail/msgflags. + * + * @return array * - * @return array An array of flag arrays keyed by the RFC 3501 flag name. + * @author Torben Dannhauer */ - public function getMsgFlags() + public function getMsgFlags(): array { - global $registry; + if ($this->_msgFlags !== null) { + return $this->_msgFlags; + } - $msgFlags = []; - $flags = unserialize($registry->horde->getPreference($registry->hasInterface('mail'), 'msgflags'), ['allowed_classes' => false]); + if (!$GLOBALS['registry']->hasMethod('mail/msgflags')) { + $this->_msgFlags = []; + return $this->_msgFlags; + } - // Remove any system flags, as these should never be user (un)set. - $system_flags = [ - Horde_Imap_Client::FLAG_ANSWERED, - Horde_Imap_Client::FLAG_DELETED, - Horde_Imap_Client::FLAG_DRAFT, - Horde_Imap_Client::FLAG_FLAGGED, - Horde_Imap_Client::FLAG_RECENT, - Horde_Imap_Client::FLAG_SEEN, - ]; - foreach ($system_flags as $flag) { - unset($flags[$flag]); + try { + $flags = $GLOBALS['registry']->call('mail/msgflags'); + } catch (Horde_Exception $e) { + $this->_logger->log(sprintf( + 'Error retrieving user message flags: %s', + $e->getMessage() + ), Horde_Log::ERR); + $this->_msgFlags = []; + return $this->_msgFlags; + } + + if (!is_array($flags)) { + $this->_logger->log( + 'Mail API mail/msgflags did not return an array.', + Horde_Log::WARN + ); + $this->_msgFlags = []; + return $this->_msgFlags; } - foreach ($flags as $flag) { - if ($flag->imapflag) { - $msgFlags[Horde_String::lower($flag->imapflag)] = $flag->label; + $msgFlags = []; + foreach ($flags as $keyword => $label) { + if (!is_scalar($keyword) || !is_scalar($label)) { + continue; } + $keyword = (string) $keyword; + if ($keyword === '') { + continue; + } + $msgFlags[HordeString::lower($keyword)] = (string) $label; } - return $msgFlags; + $this->_msgFlags = $msgFlags; + + return $this->_msgFlags; } /** diff --git a/test/Unit/ActiveSync/ImapFactoryMsgflagsTest.php b/test/Unit/ActiveSync/ImapFactoryMsgflagsTest.php new file mode 100644 index 000000000..717a2f81c --- /dev/null +++ b/test/Unit/ActiveSync/ImapFactoryMsgflagsTest.php @@ -0,0 +1,140 @@ + + * @category Horde + * @copyright 2026 The Horde Project + * @license http://www.horde.org/licenses/lgpl21 LGPL + * @package Core + * @subpackage UnitTests + */ + +namespace Horde\Core\Test\Unit\ActiveSync; + +use Horde_Core_ActiveSync_Imap_Factory; +use Horde_Exception; +use PHPUnit\Framework\TestCase; + +class ImapFactoryMsgflagsTest extends TestCase +{ + private mixed $previousRegistry = null; + + protected function setUp(): void + { + parent::setUp(); + $this->previousRegistry = $GLOBALS['registry'] ?? null; + } + + protected function tearDown(): void + { + if ($this->previousRegistry === null) { + unset($GLOBALS['registry']); + } else { + $GLOBALS['registry'] = $this->previousRegistry; + } + parent::tearDown(); + } + + public function testGetMsgFlagsReadsMailCapabilityAndCachesIt(): void + { + $registry = new ImapFactoryMsgflagsRegistry([ + 'Important' => 'Important', + 'to_do' => 'To Do', + ]); + $GLOBALS['registry'] = $registry; + $factory = new Horde_Core_ActiveSync_Imap_Factory(); + + $expected = [ + 'important' => 'Important', + 'to_do' => 'To Do', + ]; + $this->assertSame($expected, $factory->getMsgFlags()); + $this->assertSame($expected, $factory->getMsgFlags()); + $this->assertSame(1, $registry->calls); + } + + public function testGetMsgFlagsIsEmptyWhenCapabilityIsMissing(): void + { + $registry = new ImapFactoryMsgflagsRegistry(['lost' => 'Lost'], false); + $GLOBALS['registry'] = $registry; + $factory = new Horde_Core_ActiveSync_Imap_Factory(); + + $this->assertSame([], $factory->getMsgFlags()); + $this->assertSame(0, $registry->calls); + } + + public function testGetMsgFlagsIsEmptyWhenMailApiFails(): void + { + $registry = new ImapFactoryMsgflagsRegistry(new Horde_Exception('prefs unavailable')); + $GLOBALS['registry'] = $registry; + $factory = new Horde_Core_ActiveSync_Imap_Factory(); + + $this->assertSame([], $factory->getMsgFlags()); + $this->assertSame([], $factory->getMsgFlags()); + $this->assertSame(1, $registry->calls); + } + + public function testGetMsgFlagsDropsNonArrayResult(): void + { + $GLOBALS['registry'] = new ImapFactoryMsgflagsRegistry(null); + $factory = new Horde_Core_ActiveSync_Imap_Factory(); + + $this->assertSame([], $factory->getMsgFlags()); + } + + public function testGetMsgFlagsSkipsNonScalarEntries(): void + { + $GLOBALS['registry'] = new ImapFactoryMsgflagsRegistry([ + 'keep' => 'Keep', + 'drop' => ['not', 'a', 'label'], + '' => 'blank', + ]); + $factory = new Horde_Core_ActiveSync_Imap_Factory(); + + $this->assertSame(['keep' => 'Keep'], $factory->getMsgFlags()); + } +} + +/** + * Registry stub for mail/msgflags. + */ +class ImapFactoryMsgflagsRegistry +{ + public int $calls = 0; + + /** + * @param array|Horde_Exception|null $result + */ + public function __construct(private mixed $result, private bool $available = true) + { + } + + public function hasMethod(string $method, ?string $app = null): string|false + { + if ($method !== 'mail/msgflags' || !$this->available) { + return false; + } + + return 'imp'; + } + + /** + * @param array $args + */ + public function call(string $method, array $args = []): mixed + { + $this->calls++; + if ($this->result instanceof Horde_Exception) { + throw $this->result; + } + + return $this->result; + } +}