Improved IMAP FetchType handling

This commit is contained in:
djmaze 2022-01-06 15:56:24 +01:00
parent ab6b9bb412
commit d691e3c327
3 changed files with 41 additions and 63 deletions

View file

@ -39,27 +39,15 @@ abstract class FetchType
const BINARY = 'BINARY';
const BINARY_PEEK = 'BINARY.PEEK';
const BINARY_SIZE = 'BINARY.SIZE';
private static function addHelper(array &$aReturn, $mType)
{
if (\is_string($mType))
{
$aReturn[$mType] = '';
}
else if (\is_array($mType) && 2 === \count($mType) && \is_string($mType[0]) &&
\is_callable($mType[1]))
{
$aReturn[$mType[0]] = $mType[1];
}
}
// RFC 4551
const MODSEQ = 'MODSEQ';
public static function BuildBodyCustomHeaderRequest(array $aHeaders, bool $bPeek = true) : string
{
$sResult = '';
if (\count($aHeaders))
{
$aHeaders = \array_map('trim', $aHeaders);
$aHeaders = \array_map('strtoupper', $aHeaders);
$aHeaders = \array_map('strtoupper', \array_map('trim', $aHeaders));
$sResult = $bPeek ? self::BODY_PEEK : self::BODY;
$sResult .= '[HEADER.FIELDS ('.\implode(' ', $aHeaders).')]';
@ -67,45 +55,4 @@ abstract class FetchType
return $sResult;
}
public static function ChangeFetchItemsBefourRequest(array $aFetchItems) : array
{
$aReturn = array();
self::addHelper($aReturn, self::UID);
self::addHelper($aReturn, self::RFC822_SIZE);
foreach ($aFetchItems as $mFetchKey)
{
switch ($mFetchKey)
{
default:
if (is_string($mFetchKey) || is_array($mFetchKey))
{
self::addHelper($aReturn, $mFetchKey);
}
break;
case self::INDEX:
case self::UID:
case self::RFC822_SIZE:
break;
case self::ALL:
self::addHelper($aReturn, self::FLAGS);
self::addHelper($aReturn, self::INTERNALDATE);
self::addHelper($aReturn, self::ENVELOPE);
break;
case self::FAST:
self::addHelper($aReturn, self::FLAGS);
self::addHelper($aReturn, self::INTERNALDATE);
break;
case self::FULL:
self::addHelper($aReturn, self::FLAGS);
self::addHelper($aReturn, self::INTERNALDATE);
self::addHelper($aReturn, self::ENVELOPE);
self::addHelper($aReturn, self::BODY);
break;
}
}
return $aReturn;
}
}

View file

@ -11,6 +11,8 @@
namespace MailSo\Imap;
use MailSo\Imap\Enumerations\FetchType;
/**
* @category MailSo
* @package Imap
@ -862,16 +864,45 @@ class ImapClient extends \MailSo\Net\NetClient
$aReturn = array();
$this->aFetchCallbacks = array();
try {
$aFetchItems = Enumerations\FetchType::ChangeFetchItemsBefourRequest($aInputFetchItems);
foreach ($aFetchItems as $sName => $mItem)
$aFetchItems = array(
FetchType::UID,
FetchType::RFC822_SIZE
);
foreach ($aInputFetchItems as $mFetchKey)
{
if (\strlen($sName) && '' !== $mItem)
switch ($mFetchKey)
{
$this->aFetchCallbacks[$sName] = $mItem;
case FetchType::INDEX:
case FetchType::UID:
case FetchType::RFC822_SIZE:
// Already defined by default
break;
case FetchType::FULL:
$aFetchItems[] = FetchType::BODY;
// Falls through
case FetchType::ALL:
$aFetchItems[] = FetchType::ENVELOPE;
// Falls through
case FetchType::FAST:
$aFetchItems[] = FetchType::FLAGS;
$aFetchItems[] = FetchType::INTERNALDATE;
break;
default:
if (\is_string($mFetchKey)) {
$aFetchItems[] = $mFetchKey;
} else if (\is_array($mFetchKey) && 2 === \count($mFetchKey)
&& \is_string($mFetchKey[0]) && \is_callable($mFetchKey[1]))
{
$this->aFetchCallbacks[$mFetchKey[0]] = $mFetchKey[1];
}
break;
}
}
$aParams = array($sIndexRange, \array_keys($aFetchItems));
$aParams = array($sIndexRange, $aFetchItems);
/**
* TODO:
* https://datatracker.ietf.org/doc/html/rfc4551#section-3.3.1
@ -887,7 +918,7 @@ class ImapClient extends \MailSo\Net\NetClient
if (FetchResponse::hasUidAndSize($oResponse)) {
$aReturn[] = new FetchResponse($oResponse);
} else if ($this->oLogger) {
$this->oLogger->Write('Skipped Imap Response! ['.$oResponse->ToLine().']', \MailSo\Log\Enumerations\Type::NOTICE);
$this->oLogger->Write('Skipped Imap Response! ['.$oResponse.']', \MailSo\Log\Enumerations\Type::NOTICE);
}
}
}

View file

@ -83,7 +83,7 @@ class Response
}
}
public function ToLine() : string
public function __toString()
{
return $this->recToLine($this->ResponseList);
}