From b5201883d6c9b38bd75c57d355e3cf787eda2a5f Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 11 Jan 2022 12:36:47 +0100 Subject: [PATCH] Restructure and cleanup some RainLoop Actions code --- dev/Remote/AbstractFetch.js | 7 - .../0.0.0/app/libraries/RainLoop/Actions.php | 138 +----------------- .../libraries/RainLoop/Actions/Accounts.php | 14 ++ .../libraries/RainLoop/Actions/Contacts.php | 46 ++++++ .../libraries/RainLoop/Actions/Folders.php | 31 ++++ .../app/libraries/RainLoop/Actions/User.php | 28 ++++ .../v/0.0.0/app/libraries/RainLoop/Api.php | 13 +- .../app/libraries/RainLoop/ServiceActions.php | 5 +- 8 files changed, 136 insertions(+), 146 deletions(-) diff --git a/dev/Remote/AbstractFetch.js b/dev/Remote/AbstractFetch.js index a0498e15b..67c495423 100644 --- a/dev/Remote/AbstractFetch.js +++ b/dev/Remote/AbstractFetch.js @@ -178,13 +178,6 @@ export class AbstractFetchRemote }); } - /** - * @param {?Function} fCallback - */ - noop(fCallback) { - this.request('Noop', fCallback); - } - /** * @param {?Function} fCallback */ diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php index 87ed7cf58..819069fad 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php @@ -4,7 +4,6 @@ namespace RainLoop; use RainLoop\Enumerations\UploadClientError; use RainLoop\Enumerations\UploadError; -use RainLoop\Providers\Identities; class Actions { @@ -74,11 +73,6 @@ class Actions */ private $aCachers = array(); - /** - * @var Providers\Identities - */ - private $oIdentitiesProvider; - /** * @var \RainLoop\Providers\Storage */ @@ -114,11 +108,6 @@ class Actions */ private $oAddressBookProvider = null; - /** - * @var \RainLoop\Providers\Suggestions - */ - private $oSuggestionsProvider = null; - /** * @var \RainLoop\Config\Application */ @@ -134,13 +123,9 @@ class Actions */ function __construct() { - $this->oConfig = new Config\Application(); - if (!$this->oConfig->Load()) { - usleep(10000); - $this->oConfig->Load(); - } + $this->oConfig = API::Config(); - $this->oLogger = \MailSo\Log\Logger::SingletonInstance(); + $this->oLogger = API::Logger(); if ($this->oConfig->Get('logs', 'enable', false)) { $sSessionFilter = (string)$this->oConfig->Get('logs', 'session_filter', ''); if (!empty($sSessionFilter)) { @@ -482,15 +467,6 @@ class Actions } } - public function IdentitiesProvider(): Identities - { - if (null === $this->oIdentitiesProvider) { - $this->oIdentitiesProvider = new Providers\Identities($this->fabrica('identities')); - } - - return $this->oIdentitiesProvider; - } - public function SettingsProvider(bool $bLocal = false): Providers\Settings { if ($bLocal) { @@ -530,16 +506,6 @@ class Actions return $this->oDomainProvider; } - public function SuggestionsProvider(): Providers\Suggestions - { - if (null === $this->oSuggestionsProvider) { - $this->oSuggestionsProvider = new Providers\Suggestions( - $this->fabrica('suggestions')); - } - - return $this->oSuggestionsProvider; - } - public function AddressBookProvider(?Model\Account $oAccount = null, bool $bForceEnable = false): Providers\AddressBook { if (null === $this->oAddressBookProvider) { @@ -673,18 +639,6 @@ class Actions } } - public function SetMailtoRequest(string $sTo): void - { - if (!empty($sTo)) { - Utils::SetCookie(self::AUTH_MAILTO_TOKEN_KEY, - Utils::EncodeKeyValuesQ(array( - 'Time' => \microtime(true), - 'MailTo' => 'MailTo', - 'To' => $sTo - )), 0); - } - } - public function AppDataSystem(bool $bAdmin = false): array { $oConfig = $this->oConfig; @@ -711,7 +665,7 @@ class Actions 'allowAppendMessage' => (bool)$oConfig->Get('labs', 'allow_message_append', false), 'folderSpecLimit' => (int)$oConfig->Get('labs', 'folders_spec_limit', 50), 'faviconStatus' => (bool)$oConfig->Get('labs', 'favicon_status', true), - 'listPermanentFiltered' => '' !== \trim(Api::Config()->Get('labs', 'imap_message_list_permanent_filter', '')), + 'listPermanentFiltered' => '' !== \trim($oConfig->Get('labs', 'imap_message_list_permanent_filter', '')), 'themes' => $this->GetThemes(), 'languages' => \SnappyMail\L10n::getLanguages(false), 'languagesAdmin' => \SnappyMail\L10n::getLanguages(true), @@ -1013,12 +967,6 @@ class Actions ); } - public function DoNoop(): array - { - $this->initMailClientConnection(); - return $this->TrueResponse(__FUNCTION__); - } - public function DoPing(): array { return $this->DefaultResponse(__FUNCTION__, 'Pong'); @@ -1210,86 +1158,6 @@ class Actions ) : false); } - private function importContactsFromCsvFile(Model\Account $oAccount, /*resource*/ $rFile, string $sFileStart): int - { - $iCount = 0; - $aHeaders = null; - $aData = array(); - - if ($oAccount && \is_resource($rFile)) { - $oAddressBookProvider = $this->AddressBookProvider($oAccount); - if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) { - $sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';'; - - \setlocale(LC_CTYPE, 'en_US.UTF-8'); - while (false !== ($mRow = \fgetcsv($rFile, 5000, $sDelimiter, '"'))) { - if (null === $aHeaders) { - if (3 >= \count($mRow)) { - return 0; - } - - $aHeaders = $mRow; - - foreach ($aHeaders as $iIndex => $sHeaderValue) { - $aHeaders[$iIndex] = \MailSo\Base\Utils::Utf8Clear($sHeaderValue); - } - } else { - $aNewItem = array(); - foreach ($aHeaders as $iIndex => $sHeaderValue) { - $aNewItem[$sHeaderValue] = isset($mRow[$iIndex]) ? $mRow[$iIndex] : ''; - } - - $aData[] = $aNewItem; - } - } - - if (\count($aData)) { - $this->oLogger->Write('Import contacts from csv'); - $iCount = $oAddressBookProvider->ImportCsvArray( - $this->GetMainEmail($oAccount), - $aData - ); - } - } - } - - return $iCount; - } - - /** - * @throws \MailSo\Base\Exceptions\Exception - */ - public function Append(): bool - { - $oAccount = $this->initMailClientConnection(); - - $sFolderFullName = $this->GetActionParam('Folder', ''); - - $_FILES = isset($_FILES) ? $_FILES : null; - if ($oAccount && - $this->oConfig->Get('labs', 'allow_message_append', false) && - isset($_FILES, $_FILES['AppendFile'], $_FILES['AppendFile']['name'], - $_FILES['AppendFile']['tmp_name'], $_FILES['AppendFile']['size'])) { - if (is_string($_FILES['AppendFile']['tmp_name']) && \strlen($_FILES['AppendFile']['tmp_name'])) { - if (\UPLOAD_ERR_OK === (int)$_FILES['AppendFile']['error'] && !empty($sFolderFullName)) { - $sSavedName = 'append-post-' . md5($sFolderFullName . $_FILES['AppendFile']['name'] . $_FILES['AppendFile']['tmp_name']); - - if ($this->FilesProvider()->MoveUploadedFile($oAccount, - $sSavedName, $_FILES['AppendFile']['tmp_name'])) { - $iMessageStreamSize = $this->FilesProvider()->FileSize($oAccount, $sSavedName); - $rMessageStream = $this->FilesProvider()->GetFile($oAccount, $sSavedName); - - $this->MailClient()->MessageAppendStream($rMessageStream, $iMessageStreamSize, $sFolderFullName); - - $this->FilesProvider()->Clear($oAccount, $sSavedName); - } - } - } - } - - return $this->DefaultResponse(__FUNCTION__, true); - } - public function Capa(bool $bAdmin, ?Model\Account $oAccount = null): array { $oConfig = $this->oConfig; diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Accounts.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Accounts.php index b3cddfc41..58f317db4 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Accounts.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Accounts.php @@ -9,17 +9,31 @@ use RainLoop\Model\MainAccount; use RainLoop\Model\AdditionalAccount; use RainLoop\Model\Identity; use RainLoop\Notifications; +use RainLoop\Providers\Identities; use RainLoop\Providers\Storage\Enumerations\StorageType; use RainLoop\Utils; trait Accounts { + /** + * @var RainLoop\Providers\Identities + */ + private $oIdentitiesProvider; protected function GetMainEmail(Account $oAccount) { return ($oAccount instanceof AdditionalAccount ? $this->getMainAccountFromToken() : $oAccount)->Email(); } + public function IdentitiesProvider(): Identities + { + if (null === $this->oIdentitiesProvider) { + $this->oIdentitiesProvider = new Identities($this->fabrica('identities')); + } + + return $this->oIdentitiesProvider; + } + public function GetAccounts(MainAccount $oAccount): array { if ($this->GetCapa(false, Capa::ADDITIONAL_ACCOUNTS, $oAccount)) { diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php index a5e2492bf..6361e83a8 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php @@ -334,4 +334,50 @@ trait Contacts return $iCount; } + private function importContactsFromCsvFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile, string $sFileStart): int + { + $iCount = 0; + $aHeaders = null; + $aData = array(); + + if ($oAccount && \is_resource($rFile)) { + $oAddressBookProvider = $this->AddressBookProvider($oAccount); + if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) { + $sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';'; + + \setlocale(LC_CTYPE, 'en_US.UTF-8'); + while (false !== ($mRow = \fgetcsv($rFile, 5000, $sDelimiter, '"'))) { + if (null === $aHeaders) { + if (3 >= \count($mRow)) { + return 0; + } + + $aHeaders = $mRow; + + foreach ($aHeaders as $iIndex => $sHeaderValue) { + $aHeaders[$iIndex] = \MailSo\Base\Utils::Utf8Clear($sHeaderValue); + } + } else { + $aNewItem = array(); + foreach ($aHeaders as $iIndex => $sHeaderValue) { + $aNewItem[$sHeaderValue] = isset($mRow[$iIndex]) ? $mRow[$iIndex] : ''; + } + + $aData[] = $aNewItem; + } + } + + if (\count($aData)) { + $this->oLogger->Write('Import contacts from csv'); + $iCount = $oAddressBookProvider->ImportCsvArray( + $this->GetMainEmail($oAccount), + $aData + ); + } + } + } + + return $iCount; + } + } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php index a7f8d5bed..48586cd08 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php @@ -19,6 +19,37 @@ trait Folders ); } + /** + * Appends uploaded rfc822 message to mailbox + * @throws \MailSo\Base\Exceptions\Exception + */ + public function Append(): bool + { + $oAccount = $this->initMailClientConnection(); + + $sFolderFullName = $this->GetActionParam('Folder', ''); + + if ($oAccount + && !empty($sFolderFullName) + && !empty($_FILES['AppendFile']) + && \is_uploaded_file($_FILES['AppendFile']['tmp_name']) + && \UPLOAD_ERR_OK == $_FILES['AppendFile']['error'] + && $this->oConfig->Get('labs', 'allow_message_append', false) + ) { + $sSavedName = 'append-post-' . \md5($sFolderFullName . $_FILES['AppendFile']['name'] . $_FILES['AppendFile']['tmp_name']); + if ($this->FilesProvider()->MoveUploadedFile($oAccount, $sSavedName, $_FILES['AppendFile']['tmp_name'])) { + $iMessageStreamSize = $this->FilesProvider()->FileSize($oAccount, $sSavedName); + $rMessageStream = $this->FilesProvider()->GetFile($oAccount, $sSavedName); + + $this->MailClient()->MessageAppendStream($rMessageStream, $iMessageStreamSize, $sFolderFullName); + + $this->FilesProvider()->Clear($oAccount, $sSavedName); + } + } + + return $this->DefaultResponse(__FUNCTION__, true); + } + public function DoFolders() : array { $oAccount = $this->initMailClientConnection(); diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php index 2d266bb31..fb9d092cf 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/User.php @@ -5,6 +5,7 @@ namespace RainLoop\Actions; use RainLoop\Enumerations\Capa; use RainLoop\Exceptions\ClientException; use RainLoop\Notifications; +use RainLoop\Providers\Suggestions; use RainLoop\Utils; trait User @@ -15,6 +16,33 @@ trait User use Folders; use Messages; + /** + * @var \RainLoop\Providers\Suggestions + */ + private $oSuggestionsProvider = null; + + public function SuggestionsProvider(): Suggestions + { + if (null === $this->oSuggestionsProvider) { + $this->oSuggestionsProvider = new Suggestions( + $this->fabrica('suggestions')); + } + + return $this->oSuggestionsProvider; + } + + public function SetMailtoRequest(string $sTo): void + { + if (!empty($sTo)) { + Utils::SetCookie(self::AUTH_MAILTO_TOKEN_KEY, + Utils::EncodeKeyValuesQ(array( + 'Time' => \microtime(true), + 'MailTo' => 'MailTo', + 'To' => $sTo + )), 0); + } + } + /** * @throws \MailSo\Base\Exceptions\Exception */ diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Api.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Api.php index 0be225c56..2d670de15 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Api.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Api.php @@ -41,12 +41,21 @@ class Api public static function Config() : Config\Application { - return static::Actions()->Config(); + static $oConfig = null; + if (!$oConfig) + { + $oConfig = new Config\Application(); + if (!$oConfig->Load()) { + usleep(10000); + $oConfig->Load(); + } + } + return $oConfig; } public static function Logger() : \MailSo\Log\Logger { - return static::Actions()->Logger(); + return \MailSo\Log\Logger::SingletonInstance(); } protected static function SetupDefaultMailSoConfig() : void diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php index 978a5d8d2..d1d9c239f 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php @@ -253,9 +253,10 @@ class ServiceActions $iError = UPLOAD_ERR_OK; $_FILES = isset($_FILES) ? $_FILES : null; - if (isset($_FILES, $_FILES[$sInputName], $_FILES[$sInputName]['name'], $_FILES[$sInputName]['tmp_name'], $_FILES[$sInputName]['size'])) + if (isset($_FILES[$sInputName], $_FILES[$sInputName]['name'], $_FILES[$sInputName]['tmp_name'], $_FILES[$sInputName]['size'])) { $iError = (isset($_FILES[$sInputName]['error'])) ? (int) $_FILES[$sInputName]['error'] : UPLOAD_ERR_OK; +// \is_uploaded_file($_FILES[$sInputName]['tmp_name']) if (UPLOAD_ERR_OK === $iError && 0 < $iSizeLimit && $iSizeLimit < (int) $_FILES[$sInputName]['size']) { @@ -267,7 +268,7 @@ class ServiceActions $aFile = $_FILES[$sInputName]; } } - else if (!isset($_FILES) || !is_array($_FILES) || !\count($_FILES)) + else if (empty($_FILES)) { $iError = UPLOAD_ERR_INI_SIZE; }