Use more JsonSerializable

This commit is contained in:
djmaze 2020-10-21 22:44:32 +02:00
parent 500575dc32
commit f4b2fddf5e
8 changed files with 377 additions and 368 deletions

View file

@ -15,7 +15,7 @@ namespace MailSo\Base;
* @category MailSo * @category MailSo
* @package Base * @package Base
*/ */
abstract class Collection extends \ArrayObject abstract class Collection extends \ArrayObject implements \JsonSerializable
{ {
function __construct($input = array(), int $flags = 0, string $iterator_class = "ArrayIterator") function __construct($input = array(), int $flags = 0, string $iterator_class = "ArrayIterator")
{ {
@ -64,4 +64,14 @@ abstract class Collection extends \ArrayObject
); );
return $this; return $this;
} }
public function jsonSerialize()
{
$aNames = explode('\\', get_class($this));
return array(
'@Object' => 'Collection/' . end($aNames),
'@Count' => $this->Count(),
'@Collection' => $this->getArrayCopy()
);
}
} }

View file

@ -15,7 +15,7 @@ namespace MailSo\Mail;
* @category MailSo * @category MailSo
* @package Mail * @package Mail
*/ */
class Attachment class Attachment implements \JsonSerializable
{ {
/** /**
* @var string * @var string
@ -160,4 +160,20 @@ class Attachment
$this->oBodyStructure = $oBodyStructure; $this->oBodyStructure = $oBodyStructure;
return $this; return $this;
} }
public function jsonSerialize()
{
return array(
'@Object' => 'Object/Attachment',
'Folder' => $this->Folder(),
'Uid' => (string) $this->Uid(),
'MimeIndex' => (string) $this->MimeIndex(),
'MimeType' => $this->MimeType(),
'FileName' => \MailSo\Base\Utils::ClearFileName(\MailSo\Base\Utils::ClearXss($this->FileName(true))),
'EstimatedSize' => $this->EstimatedSize(),
'Cid' => $this->Cid(),
'ContentLocation' => $this->ContentLocation(),
'IsInline' => $this->IsInline()
);
}
} }

View file

@ -55,4 +55,11 @@ class AttachmentCollection extends \MailSo\Base\Collection
} }
return $aResult; return $aResult;
} }
public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), array(
'InlineCount' => $this->InlineCount()
));
}
} }

View file

@ -15,7 +15,7 @@ namespace MailSo\Mail;
* @category MailSo * @category MailSo
* @package Mail * @package Mail
*/ */
class Folder class Folder implements \JsonSerializable
{ {
/** /**
* @var string * @var string
@ -226,4 +226,20 @@ class Folder
return $iListType; return $iListType;
} }
public function jsonSerialize()
{
return array(
'@Object' => 'Object/Folder',
'Name' => $this->Name(),
'FullName' => $this->FullName(),
'FullNameRaw' => $this->FullNameRaw(),
'Delimiter' => (string) $this->Delimiter(),
'HasVisibleSubFolders' => $this->HasVisibleSubFolders(),
'IsSubscribed' => $this->IsSubscribed(),
'IsExists' => $this->IsExists(),
'IsSelectable' => $this->IsSelectable(),
'Flags' => $this->FlagsLowerCase()
);
}
} }

View file

@ -232,4 +232,17 @@ class FolderCollection extends \MailSo\Base\Collection
} }
} }
} }
public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), array(
'Namespace' => $this->GetNamespace(),
'FoldersHash' => $this->FoldersHash ?: '',
'IsThreadsSupported' => $this->IsThreadsSupported,
'Optimized' => $this->Optimized,
'CountRec' => $this->CountRec(),
'SystemFolders' => isset($this->SystemFolders) && \is_array($this->SystemFolders) ?
$this->SystemFolders : array()
));
}
} }

View file

@ -15,7 +15,7 @@ namespace MailSo\Mail;
* @category MailSo * @category MailSo
* @package Mail * @package Mail
*/ */
class Message class Message implements \JsonSerializable
{ {
/** /**
* @var string * @var string
@ -744,4 +744,44 @@ class Message
return $this; return $this;
} }
public function jsonSerialize()
{
$oAttachments = $this->Attachments();
$aFlags = $this->FlagsLowerCase();
return array(
'@Object' => 'Object/Message',
'Folder' => $this->Folder(),
'Uid' => (string) $this->Uid(),
'Subject' => \trim(\MailSo\Base\Utils::Utf8Clear($this->Subject())),
'MessageId' => $this->MessageId(),
'Size' => $this->Size(),
'DateTimeStampInUTC' => $this->InternalTimeStampInUTC(),
// \MailSo\Mime\EmailCollection
'From' => $this->From(),
'ReplyTo' => $this->ReplyTo(),
'To' => $this->To(),
'Cc' => $this->Cc(),
'Bcc' => $this->Bcc(),
'Sender' => $this->Sender(),
'DeliveredTo' => $this->DeliveredTo(),
'Priority' => $this->Priority(),
'Threads' => $this->Threads(),
'Sensitivity' => $this->Sensitivity(),
'UnsubsribeLinks' => $this->UnsubsribeLinks(),
'ExternalProxy' => false,
'ReadReceipt' => '',
'HasAttachments' => $oAttachments ? 0 < $oAttachments->Count() : false,
'AttachmentsSpecData' => $oAttachments ? $oAttachments->SpecData() : array(),
// Flags
'IsSeen' => \in_array('\\seen', $aFlags),
'IsFlagged' => \in_array('\\flagged', $aFlags),
'IsAnswered' => \in_array('\\answered', $aFlags),
'IsDeleted' => \in_array('\\deleted', $aFlags)
);
}
} }

View file

@ -110,4 +110,22 @@ class MessageCollection extends \MailSo\Base\Collection
$this->Filtered = false; $this->Filtered = false;
} }
public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), array(
'MessageCount' => $this->MessageCount,
'MessageUnseenCount' => $this->MessageUnseenCount,
'MessageResultCount' => $this->MessageResultCount,
'Folder' => $this->FolderName,
'FolderHash' => $this->FolderHash,
'UidNext' => $this->UidNext,
'ThreadUid' => $this->ThreadUid,
'NewMessages' => $this->NewMessages,
'Filtered' => $this->Filtered,
'Offset' => $this->Offset,
'Limit' => $this->Limit,
'Search' => $this->Search
));
}
} }

View file

@ -193,73 +193,47 @@ trait Response
private $aCheckableFolder = null; private $aCheckableFolder = null;
private function responseObject($mResponse, string $sParent = '', array $aParameters = array()) private function responseObject($mResponse, string $sParent = '', array $aParameters = array())
{ {
/** if (!($mResponse instanceof \JsonSerializable))
\MailSo\Mime\Email {
\RainLoop\Model\Domain if (\is_array($mResponse))
\RainLoop\Model\Identity {
\RainLoop\Model\Template foreach ($mResponse as $iKey => $oItem)
\RainLoop\Providers\AddressBook\Classes\Contact {
\RainLoop\Providers\AddressBook\Classes\Property $mResponse[$iKey] = $this->responseObject($oItem, $sParent, $aParameters);
\RainLoop\Providers\AddressBook\Classes\Tag }
\RainLoop\Providers\Filters\Classes\Filter
\RainLoop\Providers\Filters\Classes\FilterCondition
*/
if ($mResponse instanceof \JsonSerializable) {
// return $mResponse->jsonSerialize();
return $mResponse;
} }
if (\is_object($mResponse)) if (\is_object($mResponse))
{ {
$sClassName = \get_class($mResponse); return '["'.\get_class($mResponse).'"]';
}
if ('MailSo\Mail\Message' === $sClassName) return $mResponse;
}
if ($mResponse instanceof \MailSo\Mail\Message)
{ {
$mResult = $mResponse->jsonSerialize();
if (null === $this->oAccount) { if (null === $this->oAccount) {
$this->oAccount = $this->getAccountFromToken(false); $this->oAccount = $this->getAccountFromToken(false);
} }
$oAccount = $this->oAccount; $oAccount = $this->oAccount;
$iDateTimeStampInUTC = $mResponse->InternalTimeStampInUTC(); if (!$mResult['DateTimeStampInUTC'] || !!$this->Config()->Get('labs', 'date_from_headers', false)) {
if (0 === $iDateTimeStampInUTC || !!$this->Config()->Get('labs', 'date_from_headers', false))
{
$iDateTimeStampInUTC = $mResponse->HeaderTimeStampInUTC(); $iDateTimeStampInUTC = $mResponse->HeaderTimeStampInUTC();
if (0 === $iDateTimeStampInUTC) if ($iDateTimeStampInUTC) {
{ $mResult['DateTimeStampInUTC'] = $iDateTimeStampInUTC;
$iDateTimeStampInUTC = $mResponse->InternalTimeStampInUTC();
} }
} }
$mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array( // \MailSo\Mime\EmailCollection
'Folder' => $mResponse->Folder(), foreach (['ReplyTo','From','To','Cc','Bcc','Sender','DeliveredTo','ReplyTo'] as $prop) {
'Uid' => (string) $mResponse->Uid(), $mResult[$prop] = $this->responseObject($mResult[$prop], $sParent, $aParameters);
'Subject' => \trim(\MailSo\Base\Utils::Utf8Clear($mResponse->Subject())), }
'MessageId' => $mResponse->MessageId(),
'Size' => $mResponse->Size(),
'DateTimeStampInUTC' => $iDateTimeStampInUTC,
'ReplyTo' => $this->responseObject($mResponse->ReplyTo(), $sParent, $aParameters),
'From' => $this->responseObject($mResponse->From(), $sParent, $aParameters),
'To' => $this->responseObject($mResponse->To(), $sParent, $aParameters),
'Cc' => $this->responseObject($mResponse->Cc(), $sParent, $aParameters),
'Bcc' => $this->responseObject($mResponse->Bcc(), $sParent, $aParameters),
'Sender' => $this->responseObject($mResponse->Sender(), $sParent, $aParameters),
'DeliveredTo' => $this->responseObject($mResponse->DeliveredTo(), $sParent, $aParameters),
'Priority' => $mResponse->Priority(),
'Threads' => $mResponse->Threads(),
'Sensitivity' => $mResponse->Sensitivity(),
'UnsubsribeLinks' => $mResponse->UnsubsribeLinks(),
'ExternalProxy' => false,
'ReadReceipt' => ''
));
$mResult['SubjectParts'] = $this->explodeSubject($mResult['Subject']); $mResult['SubjectParts'] = $this->explodeSubject($mResult['Subject']);
$oAttachments = $mResponse->Attachments();
$iAttachmentsCount = $oAttachments ? $oAttachments->Count() : 0;
$mResult['HasAttachments'] = 0 < $iAttachmentsCount;
$mResult['AttachmentsSpecData'] = $mResult['HasAttachments'] ? $oAttachments->SpecData() : array();
$sSubject = $mResult['Subject']; $sSubject = $mResult['Subject'];
$mResult['Hash'] = \md5($mResult['Folder'].$mResult['Uid']); $mResult['Hash'] = \md5($mResult['Folder'].$mResult['Uid']);
$mResult['RequestHash'] = Utils::EncodeKeyValuesQ(array( $mResult['RequestHash'] = Utils::EncodeKeyValuesQ(array(
@ -271,13 +245,6 @@ trait Response
'FileName' => (0 === \strlen($sSubject) ? 'message-'.$mResult['Uid'] : \MailSo\Base\Utils::ClearXss($sSubject)).'.eml' 'FileName' => (0 === \strlen($sSubject) ? 'message-'.$mResult['Uid'] : \MailSo\Base\Utils::ClearXss($sSubject)).'.eml'
)); ));
// Flags
$aFlags = $mResponse->FlagsLowerCase();
$mResult['IsSeen'] = \in_array('\\seen', $aFlags);
$mResult['IsFlagged'] = \in_array('\\flagged', $aFlags);
$mResult['IsAnswered'] = \in_array('\\answered', $aFlags);
$mResult['IsDeleted'] = \in_array('\\deleted', $aFlags);
$sForwardedFlag = $this->Config()->Get('labs', 'imap_forwarded_flag', ''); $sForwardedFlag = $this->Config()->Get('labs', 'imap_forwarded_flag', '');
$sReadReceiptFlag = $this->Config()->Get('labs', 'imap_read_receipt_flag', ''); $sReadReceiptFlag = $this->Config()->Get('labs', 'imap_read_receipt_flag', '');
@ -399,8 +366,10 @@ trait Response
return $mResult; return $mResult;
} }
if ('MailSo\Mail\Attachment' === $sClassName) if ($mResponse instanceof \MailSo\Mail\Attachment)
{ {
$mResult = $mResponse->jsonSerialize();
if (null === $this->oAccount) { if (null === $this->oAccount) {
$this->oAccount = $this->getAccountFromToken(false); $this->oAccount = $this->getAccountFromToken(false);
} }
@ -422,29 +391,11 @@ trait Response
$mFoundedCIDs = 0 < \count($mFoundedCIDs) ? $mFoundedCIDs : null; $mFoundedCIDs = 0 < \count($mFoundedCIDs) ? $mFoundedCIDs : null;
} }
$mResult = \array_merge($this->objectData($mResponse, $sParent, $aParameters), array( $mResult['IsLinked'] = ($mFoundedCIDs && \in_array(\trim(\trim($mResponse->Cid()), '<>'), $mFoundedCIDs))
'Folder' => $mResponse->Folder(), || ($mFoundedContentLocationUrls && \in_array(\trim($mResponse->ContentLocation()), $mFoundedContentLocationUrls));
'Uid' => (string) $mResponse->Uid(),
'Framed' => false,
'MimeIndex' => (string) $mResponse->MimeIndex(),
'MimeType' => $mResponse->MimeType(),
'FileName' => \MailSo\Base\Utils::ClearFileName(
\MailSo\Base\Utils::ClearXss($mResponse->FileName(true))),
'EstimatedSize' => $mResponse->EstimatedSize(),
'Cid' => $mResponse->Cid(),
'ContentLocation' => $mResponse->ContentLocation(),
'IsInline' => $mResponse->IsInline(),
'IsThumbnail' => $this->GetCapa(false, false, Capa::ATTACHMENT_THUMBNAILS),
'IsLinked' => ($mFoundedCIDs && \in_array(\trim(\trim($mResponse->Cid()), '<>'), $mFoundedCIDs)) ||
($mFoundedContentLocationUrls && \in_array(\trim($mResponse->ContentLocation()), $mFoundedContentLocationUrls))
));
$mResult['Framed'] = $this->isFileHasFramedPreview($mResult['FileName']); $mResult['Framed'] = $this->isFileHasFramedPreview($mResult['FileName']);
$mResult['IsThumbnail'] = $this->GetCapa(false, false, Capa::ATTACHMENT_THUMBNAILS) && $this->isFileHasThumbnail($mResult['FileName']);
if ($mResult['IsThumbnail'])
{
$mResult['IsThumbnail'] = $this->isFileHasThumbnail($mResult['FileName']);
}
$mResult['Download'] = Utils::EncodeKeyValuesQ(array( $mResult['Download'] = Utils::EncodeKeyValuesQ(array(
'V' => APP_VERSION, 'V' => APP_VERSION,
@ -459,7 +410,7 @@ trait Response
return $mResult; return $mResult;
} }
if ('MailSo\Mail\Folder' === $sClassName) if ($mResponse instanceof \MailSo\Mail\Folder)
{ {
$aExtended = null; $aExtended = null;
@ -489,93 +440,31 @@ trait Response
$this->aCheckableFolder = \is_array($aCheckable) ? $aCheckable : array(); $this->aCheckableFolder = \is_array($aCheckable) ? $aCheckable : array();
} }
return \array_merge($this->objectData($mResponse, $sParent, $aParameters), array( return \array_merge(
'Name' => $mResponse->Name(), $mResponse->jsonSerialize(),
'FullName' => $mResponse->FullName(), array(
'FullNameRaw' => $mResponse->FullNameRaw(),
'FullNameHash' => $this->hashFolderFullName($mResponse->FullNameRaw(), $mResponse->FullName()), 'FullNameHash' => $this->hashFolderFullName($mResponse->FullNameRaw(), $mResponse->FullName()),
'Delimiter' => (string) $mResponse->Delimiter(),
'HasVisibleSubFolders' => $mResponse->HasVisibleSubFolders(),
'IsSubscribed' => $mResponse->IsSubscribed(),
'IsExists' => $mResponse->IsExists(),
'IsSelectable' => $mResponse->IsSelectable(),
'Flags' => $mResponse->FlagsLowerCase(),
'Checkable' => \in_array($mResponse->FullNameRaw(), $this->aCheckableFolder), 'Checkable' => \in_array($mResponse->FullNameRaw(), $this->aCheckableFolder),
'Extended' => $aExtended, 'Extended' => $aExtended,
'SubFolders' => $this->responseObject($mResponse->SubFolders(), $sParent, $aParameters) 'SubFolders' => $this->responseObject($mResponse->SubFolders(), $sParent, $aParameters)
)); )
} );
if ('MailSo\Mail\MessageCollection' === $sClassName)
{
return \array_merge($this->objectData($mResponse, $sParent, $aParameters), array(
'MessageCount' => $mResponse->MessageCount,
'MessageUnseenCount' => $mResponse->MessageUnseenCount,
'MessageResultCount' => $mResponse->MessageResultCount,
'Folder' => $mResponse->FolderName,
'FolderHash' => $mResponse->FolderHash,
'UidNext' => $mResponse->UidNext,
'ThreadUid' => $mResponse->ThreadUid,
'NewMessages' => $this->responseObject($mResponse->NewMessages),
'Filtered' => $mResponse->Filtered,
'Offset' => $mResponse->Offset,
'Limit' => $mResponse->Limit,
'Search' => $mResponse->Search
));
}
if ('MailSo\Mail\AttachmentCollection' === $sClassName)
{
return \array_merge($this->objectData($mResponse, $sParent, $aParameters), array(
'InlineCount' => $mResponse->InlineCount()
));
}
if ('MailSo\Mail\FolderCollection' === $sClassName)
{
return \array_merge($this->objectData($mResponse, $sParent, $aParameters), array(
'Namespace' => $mResponse->GetNamespace(),
'FoldersHash' => isset($mResponse->FoldersHash) ? $mResponse->FoldersHash : '',
'IsThreadsSupported' => $mResponse->IsThreadsSupported,
'Optimized' => $mResponse->Optimized,
'CountRec' => $mResponse->CountRec(),
'SystemFolders' => isset($mResponse->SystemFolders) && \is_array($mResponse->SystemFolders) ?
$mResponse->SystemFolders : array()
));
}
if ('MailSo\Mime\EmailCollection' === $sClassName)
{
$mResult = array();
if (100 < \count($mResponse)) {
$mResponse = $mResponse->Slice(0, 100);
}
foreach ($mResponse as $iKey => $oItem) {
$mResult[$iKey] = $this->responseObject($oItem, $sParent, $aParameters);
}
return $mResult;
} }
if ($mResponse instanceof \MailSo\Base\Collection) if ($mResponse instanceof \MailSo\Base\Collection)
{ {
$mResult = array(); $mResult = $mResponse->jsonSerialize();
foreach ($mResponse as $iKey => $oItem) { $mResult['@Collection'] = $this->responseObject($mResult['@Collection'], $sParent, $aParameters);
$mResult[$iKey] = $this->responseObject($oItem, $sParent, $aParameters); if ($mResponse instanceof \MailSo\Mail\EmailCollection) {
return array_slice($mResult['@Collection'], 0, 100);
} }
if ($mResponse instanceof \MailSo\Mail\AttachmentCollection
|| $mResponse instanceof \MailSo\Mail\FolderCollection
|| $mResponse instanceof \MailSo\Mail\MessageCollection
) {
return $mResult; return $mResult;
} }
return $mResult['@Collection'];
return '["'.$sClassName.'"]';
}
if (\is_array($mResponse))
{
foreach ($mResponse as $iKey => $oItem)
{
$mResponse[$iKey] = $this->responseObject($oItem, $sParent, $aParameters);
}
return $mResponse;
} }
return $mResponse; return $mResponse;