mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-09 17:37:02 +03:00
Split RainLoop/Actions.php and use JsonSerializable
This commit is contained in:
parent
8cd2bd46d8
commit
9844c1882c
24 changed files with 3205 additions and 3066 deletions
451
snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php
Normal file
451
snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php
Normal file
|
|
@ -0,0 +1,451 @@
|
|||
<?php
|
||||
|
||||
namespace RainLoop\Actions;
|
||||
|
||||
use \RainLoop\Enumerations\Capa;
|
||||
use \RainLoop\Exceptions\ClientException;
|
||||
use \RainLoop\Notifications;
|
||||
use \MailSo\Imap\Enumerations\FolderType;
|
||||
|
||||
trait Folders
|
||||
{
|
||||
|
||||
public function DoFolders() : array
|
||||
{
|
||||
$oAccount = $this->initMailClientConnection();
|
||||
|
||||
$oFolderCollection = null;
|
||||
$this->Plugins()->RunHook('filter.folders-before', array($oAccount, $oFolderCollection));
|
||||
|
||||
$bUseFolders = $this->GetCapa(false, false, Capa::FOLDERS, $oAccount);
|
||||
|
||||
if (null === $oFolderCollection)
|
||||
{
|
||||
$oFolderCollection = $this->MailClient()->Folders('',
|
||||
$bUseFolders ? '*' : 'INBOX',
|
||||
!!$this->Config()->Get('labs', 'use_imap_list_subscribe', true),
|
||||
(int) $this->Config()->Get('labs', 'imap_folder_list_limit', 200)
|
||||
);
|
||||
}
|
||||
|
||||
$this->Plugins()->RunHook('filter.folders-post', array($oAccount, $oFolderCollection));
|
||||
|
||||
if ($oFolderCollection instanceof \MailSo\Mail\FolderCollection)
|
||||
{
|
||||
$oSettingsLocal = $this->SettingsProvider(true)->Load($oAccount);
|
||||
|
||||
$aSystemFolders = array();
|
||||
$this->recFoldersTypes($oAccount, $oFolderCollection, $aSystemFolders);
|
||||
$oFolderCollection->SystemFolders = $aSystemFolders;
|
||||
|
||||
if ($bUseFolders && $this->Config()->Get('labs', 'autocreate_system_folders', true))
|
||||
{
|
||||
$bDoItAgain = false;
|
||||
|
||||
$sNamespace = $oFolderCollection->GetNamespace();
|
||||
$sParent = empty($sNamespace) ? '' : \substr($sNamespace, 0, -1);
|
||||
|
||||
$sDelimiter = $oFolderCollection->FindDelimiter();
|
||||
|
||||
$aList = array();
|
||||
$aMap = $this->systemFoldersNames($oAccount);
|
||||
|
||||
if ('' === $oSettingsLocal->GetConf('SentFolder', ''))
|
||||
{
|
||||
$aList[] = FolderType::SENT;
|
||||
}
|
||||
|
||||
if ('' === $oSettingsLocal->GetConf('DraftFolder', ''))
|
||||
{
|
||||
$aList[] = FolderType::DRAFTS;
|
||||
}
|
||||
|
||||
if ('' === $oSettingsLocal->GetConf('SpamFolder', ''))
|
||||
{
|
||||
$aList[] = FolderType::JUNK;
|
||||
}
|
||||
|
||||
if ('' === $oSettingsLocal->GetConf('TrashFolder', ''))
|
||||
{
|
||||
$aList[] = FolderType::TRASH;
|
||||
}
|
||||
|
||||
if ('' === $oSettingsLocal->GetConf('ArchiveFolder', ''))
|
||||
{
|
||||
$aList[] = FolderType::ALL;
|
||||
}
|
||||
|
||||
$this->Plugins()->RunHook('filter.folders-system-types', array($oAccount, &$aList));
|
||||
|
||||
foreach ($aList as $iType)
|
||||
{
|
||||
if (!isset($aSystemFolders[$iType]))
|
||||
{
|
||||
$mFolderNameToCreate = \array_search($iType, $aMap);
|
||||
if (!empty($mFolderNameToCreate))
|
||||
{
|
||||
$iPos = \strrpos($mFolderNameToCreate, $sDelimiter);
|
||||
if (false !== $iPos)
|
||||
{
|
||||
$mNewParent = \substr($mFolderNameToCreate, 0, $iPos);
|
||||
$mNewFolderNameToCreate = \substr($mFolderNameToCreate, $iPos + 1);
|
||||
if (0 < \strlen($mNewFolderNameToCreate))
|
||||
{
|
||||
$mFolderNameToCreate = $mNewFolderNameToCreate;
|
||||
}
|
||||
|
||||
if (0 < \strlen($mNewParent))
|
||||
{
|
||||
$sParent = 0 < \strlen($sParent) ? $sParent.$sDelimiter.$mNewParent : $mNewParent;
|
||||
}
|
||||
}
|
||||
|
||||
$sFullNameToCheck = \MailSo\Base\Utils::ConvertEncoding($mFolderNameToCreate,
|
||||
\MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::UTF_7_IMAP);
|
||||
|
||||
if (0 < \strlen(\trim($sParent)))
|
||||
{
|
||||
$sFullNameToCheck = $sParent.$sDelimiter.$sFullNameToCheck;
|
||||
}
|
||||
|
||||
if (!$oFolderCollection->GetByFullNameRaw($sFullNameToCheck))
|
||||
{
|
||||
try
|
||||
{
|
||||
if ($this->MailClient()->FolderCreate($mFolderNameToCreate, $sParent, true, $sDelimiter))
|
||||
{
|
||||
$bDoItAgain = true;
|
||||
}
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
$this->Logger()->WriteException($oException);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($bDoItAgain)
|
||||
{
|
||||
$oFolderCollection = $this->MailClient()->Folders('', '*',
|
||||
!!$this->Config()->Get('labs', 'use_imap_list_subscribe', true),
|
||||
(int) $this->Config()->Get('labs', 'imap_folder_list_limit', 200)
|
||||
);
|
||||
|
||||
if ($oFolderCollection)
|
||||
{
|
||||
$aSystemFolders = array();
|
||||
$this->recFoldersTypes($oAccount, $oFolderCollection, $aSystemFolders);
|
||||
$oFolderCollection->SystemFolders = $aSystemFolders;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($oFolderCollection)
|
||||
{
|
||||
$oFolderCollection->FoldersHash = \md5(\implode("\x0", $this->recFoldersNames($oFolderCollection)));
|
||||
}
|
||||
}
|
||||
|
||||
$this->Plugins()->RunHook('filter.folders-complete', array($oAccount, $oFolderCollection));
|
||||
|
||||
return $this->DefaultResponse(__FUNCTION__, $oFolderCollection);
|
||||
}
|
||||
|
||||
public function DoFolderCreate() : array
|
||||
{
|
||||
$oAccount = $this->initMailClientConnection();
|
||||
|
||||
if (!$this->GetCapa(false, false, Capa::FOLDERS, $oAccount))
|
||||
{
|
||||
return $this->FalseResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$sFolderNameInUtf = $this->GetActionParam('Folder', '');
|
||||
$sFolderParentFullNameRaw = $this->GetActionParam('Parent', '');
|
||||
|
||||
$this->MailClient()->FolderCreate($sFolderNameInUtf, $sFolderParentFullNameRaw,
|
||||
!!$this->Config()->Get('labs', 'use_imap_list_subscribe', true));
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
throw new ClientException(Notifications::CantCreateFolder, $oException);
|
||||
}
|
||||
|
||||
return $this->TrueResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
public function DoFolderSubscribe() : array
|
||||
{
|
||||
$oAccount = $this->initMailClientConnection();
|
||||
|
||||
if (!$this->GetCapa(false, false, Capa::FOLDERS, $oAccount))
|
||||
{
|
||||
return $this->FalseResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
$sFolderFullNameRaw = $this->GetActionParam('Folder', '');
|
||||
$bSubscribe = '1' === (string) $this->GetActionParam('Subscribe', '0');
|
||||
|
||||
try
|
||||
{
|
||||
$this->MailClient()->FolderSubscribe($sFolderFullNameRaw, !!$bSubscribe);
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
if ($bSubscribe)
|
||||
{
|
||||
throw new ClientException(Notifications::CantSubscribeFolder, $oException);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ClientException(Notifications::CantUnsubscribeFolder, $oException);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->TrueResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
public function DoFolderCheckable() : array
|
||||
{
|
||||
$oAccount = $this->getAccountFromToken();
|
||||
|
||||
if (!$this->GetCapa(false, false, Capa::FOLDERS, $oAccount))
|
||||
{
|
||||
return $this->FalseResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
$sFolderFullNameRaw = $this->GetActionParam('Folder', '');
|
||||
$bCheckable = '1' === (string) $this->GetActionParam('Checkable', '0');
|
||||
|
||||
$oSettingsLocal = $this->SettingsProvider(true)->Load($oAccount);
|
||||
|
||||
$sCheckableFolder = $oSettingsLocal->GetConf('CheckableFolder', '[]');
|
||||
$aCheckableFolder = \json_decode($sCheckableFolder);
|
||||
|
||||
if (!\is_array($aCheckableFolder))
|
||||
{
|
||||
$aCheckableFolder = array();
|
||||
}
|
||||
|
||||
if ($bCheckable)
|
||||
{
|
||||
$aCheckableFolder[] = $sFolderFullNameRaw;
|
||||
}
|
||||
else
|
||||
{
|
||||
$aCheckableFolderNew = array();
|
||||
foreach ($aCheckableFolder as $sFolder)
|
||||
{
|
||||
if ($sFolder !== $sFolderFullNameRaw)
|
||||
{
|
||||
$aCheckableFolderNew[] = $sFolder;
|
||||
}
|
||||
}
|
||||
$aCheckableFolder = $aCheckableFolderNew;
|
||||
}
|
||||
|
||||
$aCheckableFolder = \array_unique($aCheckableFolder);
|
||||
|
||||
$oSettingsLocal->SetConf('CheckableFolder', \json_encode($aCheckableFolder));
|
||||
|
||||
return $this->DefaultResponse(__FUNCTION__,
|
||||
$this->SettingsProvider(true)->Save($oAccount, $oSettingsLocal));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFolderRename() : array
|
||||
{
|
||||
$oAccount = $this->initMailClientConnection();
|
||||
|
||||
if (!$this->GetCapa(false, false, Capa::FOLDERS, $oAccount))
|
||||
{
|
||||
return $this->FalseResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
$sPrevFolderFullNameRaw = $this->GetActionParam('Folder', '');
|
||||
$sNewTopFolderNameInUtf = $this->GetActionParam('NewFolderName', '');
|
||||
|
||||
try
|
||||
{
|
||||
$this->MailClient()->FolderRename($sPrevFolderFullNameRaw, $sNewTopFolderNameInUtf,
|
||||
!!$this->Config()->Get('labs', 'use_imap_list_subscribe', true));
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
throw new ClientException(Notifications::CantRenameFolder, $oException);
|
||||
}
|
||||
|
||||
return $this->TrueResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFolderDelete() : array
|
||||
{
|
||||
$oAccount = $this->initMailClientConnection();
|
||||
|
||||
if (!$this->GetCapa(false, false, Capa::FOLDERS, $oAccount))
|
||||
{
|
||||
return $this->FalseResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
$sFolderFullNameRaw = $this->GetActionParam('Folder', '');
|
||||
|
||||
try
|
||||
{
|
||||
$this->MailClient()->FolderDelete($sFolderFullNameRaw,
|
||||
!!$this->Config()->Get('labs', 'use_imap_list_subscribe', true));
|
||||
}
|
||||
catch (\MailSo\Mail\Exceptions\NonEmptyFolder $oException)
|
||||
{
|
||||
throw new ClientException(Notifications::CantDeleteNonEmptyFolder, $oException);
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
throw new ClientException(Notifications::CantDeleteFolder, $oException);
|
||||
}
|
||||
|
||||
return $this->TrueResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFolderClear() : array
|
||||
{
|
||||
$this->initMailClientConnection();
|
||||
|
||||
$sFolderFullNameRaw = $this->GetActionParam('Folder', '');
|
||||
|
||||
try
|
||||
{
|
||||
$this->MailClient()->FolderClear($sFolderFullNameRaw);
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
throw new ClientException(Notifications::MailServerError, $oException);
|
||||
}
|
||||
|
||||
return $this->TrueResponse(__FUNCTION__);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFolderInformation() : array
|
||||
{
|
||||
$sFolder = $this->GetActionParam('Folder', '');
|
||||
$sPrevUidNext = $this->GetActionParam('UidNext', '');
|
||||
$aFlagsUids = array();
|
||||
$sFlagsUids = (string) $this->GetActionParam('FlagsUids', '');
|
||||
|
||||
$aFlagsFilteredUids = array();
|
||||
if (0 < strlen($sFlagsUids))
|
||||
{
|
||||
$aFlagsUids = explode(',', $sFlagsUids);
|
||||
$aFlagsFilteredUids = array_filter($aFlagsUids, function (&$sUid) {
|
||||
$sUid = (int) trim($sUid);
|
||||
return 0 < (int) trim($sUid);
|
||||
});
|
||||
}
|
||||
|
||||
$this->initMailClientConnection();
|
||||
|
||||
$sForwardedFlag = $this->Config()->Get('labs', 'imap_forwarded_flag', '');
|
||||
$sReadReceiptFlag = $this->Config()->Get('labs', 'imap_read_receipt_flag', '');
|
||||
try
|
||||
{
|
||||
$aInboxInformation = $this->MailClient()->FolderInformation(
|
||||
$sFolder, $sPrevUidNext, $aFlagsFilteredUids
|
||||
);
|
||||
|
||||
if (isset($aInboxInformation['Flags']) && \is_array($aInboxInformation['Flags']))
|
||||
{
|
||||
foreach ($aInboxInformation['Flags'] as $iUid => $aFlags)
|
||||
{
|
||||
$aLowerFlags = array_map('strtolower', $aFlags);
|
||||
$aInboxInformation['Flags'][$iUid] = array(
|
||||
'IsSeen' => in_array('\\seen', $aLowerFlags),
|
||||
'IsFlagged' => in_array('\\flagged', $aLowerFlags),
|
||||
'IsAnswered' => in_array('\\answered', $aLowerFlags),
|
||||
'IsDeleted' => in_array('\\deleted', $aLowerFlags),
|
||||
'IsForwarded' => 0 < strlen($sForwardedFlag) && in_array(strtolower($sForwardedFlag), $aLowerFlags),
|
||||
'IsReadReceipt' => 0 < strlen($sReadReceiptFlag) && in_array(strtolower($sReadReceiptFlag), $aLowerFlags)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
throw new ClientException(Notifications::MailServerError, $oException);
|
||||
}
|
||||
|
||||
$aInboxInformation['Version'] = APP_VERSION;
|
||||
|
||||
return $this->DefaultResponse(__FUNCTION__, $aInboxInformation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \MailSo\Base\Exceptions\Exception
|
||||
*/
|
||||
public function DoFolderInformationMultiply() : array
|
||||
{
|
||||
$aResult = array(
|
||||
'List' => array(),
|
||||
'Version' => APP_VERSION
|
||||
);
|
||||
|
||||
$aFolders = $this->GetActionParam('Folders', null);
|
||||
if (\is_array($aFolders))
|
||||
{
|
||||
$this->initMailClientConnection();
|
||||
|
||||
$aFolders = \array_unique($aFolders);
|
||||
foreach ($aFolders as $sFolder)
|
||||
{
|
||||
if (0 < \strlen(\trim($sFolder)) && 'INBOX' !== \strtoupper($sFolder))
|
||||
{
|
||||
try
|
||||
{
|
||||
$aInboxInformation = $this->MailClient()->FolderInformation($sFolder, '', array());
|
||||
if (isset($aInboxInformation['Folder']))
|
||||
{
|
||||
$aResult['List'][] = $aInboxInformation;
|
||||
}
|
||||
}
|
||||
catch (\Throwable $oException)
|
||||
{
|
||||
$this->Logger()->WriteException($oException);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->DefaultResponse(__FUNCTION__, $aResult);
|
||||
}
|
||||
|
||||
public function DoSystemFoldersUpdate() : array
|
||||
{
|
||||
$oAccount = $this->getAccountFromToken();
|
||||
|
||||
$oSettingsLocal = $this->SettingsProvider(true)->Load($oAccount);
|
||||
|
||||
$oSettingsLocal->SetConf('SentFolder', $this->GetActionParam('SentFolder', ''));
|
||||
$oSettingsLocal->SetConf('DraftFolder', $this->GetActionParam('DraftFolder', ''));
|
||||
$oSettingsLocal->SetConf('SpamFolder', $this->GetActionParam('SpamFolder', ''));
|
||||
$oSettingsLocal->SetConf('TrashFolder', $this->GetActionParam('TrashFolder', ''));
|
||||
$oSettingsLocal->SetConf('ArchiveFolder', $this->GetActionParam('ArchiveFolder', ''));
|
||||
$oSettingsLocal->SetConf('NullFolder', $this->GetActionParam('NullFolder', ''));
|
||||
|
||||
return $this->DefaultResponse(__FUNCTION__,
|
||||
$this->SettingsProvider(true)->Save($oAccount, $oSettingsLocal));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue