Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 58 additions & 21 deletions lib/Horde/Core/ActiveSync/Imap/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, string>|null
*/
protected $_msgFlags = null;

/**
* Logger.
*
Expand Down Expand Up @@ -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<string, string>
*
* @return array An array of flag arrays keyed by the RFC 3501 flag name.
* @author Torben Dannhauer <torben@dannhauer.de>
*/
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;
}

/**
Expand Down
140 changes: 140 additions & 0 deletions test/Unit/ActiveSync/ImapFactoryMsgflagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

/**
* Copyright 2026 The Horde Project (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Torben Dannhauer <torben@dannhauer.de>
* @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<string, mixed>|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<mixed> $args
*/
public function call(string $method, array $args = []): mixed
{
$this->calls++;
if ($this->result instanceof Horde_Exception) {
throw $this->result;
}

return $this->result;
}
}
Loading