Restructure and cleanup some RainLoop Actions code

This commit is contained in:
the-djmaze 2022-01-11 12:36:47 +01:00
parent 9d280ab97e
commit b5201883d6
8 changed files with 136 additions and 146 deletions

View file

@ -178,13 +178,6 @@ export class AbstractFetchRemote
});
}
/**
* @param {?Function} fCallback
*/
noop(fCallback) {
this.request('Noop', fCallback);
}
/**
* @param {?Function} fCallback
*/

View file

@ -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;

View file

@ -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)) {

View file

@ -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;
}
}

View file

@ -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();

View file

@ -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
*/

View file

@ -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

View file

@ -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;
}