Skip to content

Commit

Permalink
Cleanup some classes and improve the Accounts and Contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
djmaze committed Nov 10, 2021
1 parent 7ea4365 commit ef4790d
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace MailSo\Imap\Commands;

use \MailSo\Imap\Responses\ACL as ACLResponse;
use MailSo\Imap\Responses\ACL as ACLResponse;

/**
* @category MailSo
Expand Down
2 changes: 1 addition & 1 deletion snappymail/v/0.0.0/app/libraries/MailSo/Imap/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace MailSo\Imap;

use \MailSo\Imap\Enumerations\ResponseType;
use MailSo\Imap\Enumerations\ResponseType;

/**
* @category MailSo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace MailSo\Imap\Traits;

use \MailSo\Imap\Response;
use \MailSo\Imap\Enumerations\ResponseType;
use \MailSo\Imap\Exceptions\ResponseNotFoundException;
use MailSo\Imap\Response;
use MailSo\Imap\Enumerations\ResponseType;
use MailSo\Imap\Exceptions\ResponseNotFoundException;

/**
* @category MailSo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace MailSo\Mail;

use \MailSo\Imap\Enumerations\FolderResponseStatus;
use MailSo\Imap\Enumerations\FolderResponseStatus;

/**
* @category MailSo
Expand Down
66 changes: 0 additions & 66 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ private function fabrica(string $sName, ?Model\Account $oAccount = null)
$mResult = new Providers\AddressBook\PdoAddressBook($sDsn, $sUser, $sPassword, $sDsnType);
break;
case 'identities':
$mResult = [];
break;
case 'suggestions':
$mResult = [];
break;
Expand Down Expand Up @@ -1117,52 +1115,6 @@ private function getAdditionalLogParamsByUserLogin(string $sLogin, bool $bAdmin
);
}

public function GetAccounts(Model\Account $oAccount): array
{
if ($this->GetCapa(false, Enumerations\Capa::ADDITIONAL_ACCOUNTS, $oAccount)) {
$sAccounts = $this->StorageProvider()->Get($oAccount,
Providers\Storage\Enumerations\StorageType::CONFIG,
'accounts'
);

$aAccounts = array();
if ('' !== $sAccounts && '{' === \substr($sAccounts, 0, 1)) {
$aAccounts = \json_decode($sAccounts, true);
}

if (\is_array($aAccounts) && \count($aAccounts)) {
if (1 === \count($aAccounts)) {
$this->SetAccounts($oAccount, array());

} else if (1 < \count($aAccounts)) {
$sOrder = $this->StorageProvider()->Get($oAccount,
Providers\Storage\Enumerations\StorageType::CONFIG,
'accounts_identities_order'
);

$aOrder = empty($sOrder) ? array() : \json_decode($sOrder, true);
if (isset($aOrder['Accounts']) && \is_array($aOrder['Accounts']) &&
1 < \count($aOrder['Accounts'])) {
$aAccounts = \array_merge(\array_flip($aOrder['Accounts']), $aAccounts);

$aAccounts = \array_filter($aAccounts, function ($sHash) {
return 5 < \strlen($sHash);
});
}
}

return $aAccounts;
}
}

$aAccounts = array();
if (!$oAccount->IsAdditionalAccount()) {
$aAccounts[$oAccount->Email()] = $oAccount->GetAuthToken();
}

return $aAccounts;
}

public function GetIdentityByID(Model\Account $oAccount, string $sID, bool $bFirstOnEmpty = false): ?Model\Identity
{
$aIdentities = $this->GetIdentities($oAccount);
Expand All @@ -1176,24 +1128,6 @@ public function GetIdentityByID(Model\Account $oAccount, string $sID, bool $bFir
return $bFirstOnEmpty && isset($aIdentities[0]) ? $aIdentities[0] : null;
}

public function SetAccounts(Model\Account $oAccount, array $aAccounts = array()): void
{
$sParentEmail = $oAccount->ParentEmailHelper();
if (!$aAccounts ||
(1 === \count($aAccounts) && !empty($aAccounts[$sParentEmail]))) {
$this->StorageProvider()->Clear($oAccount,
Providers\Storage\Enumerations\StorageType::CONFIG,
'accounts'
);
} else {
$this->StorageProvider()->Put($oAccount,
Providers\Storage\Enumerations\StorageType::CONFIG,
'accounts',
\json_encode($aAccounts)
);
}
}

/**
* @throws \MailSo\Base\Exceptions\Exception
*/
Expand Down
73 changes: 67 additions & 6 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,72 @@
use RainLoop\Model\Identity;
use RainLoop\Notifications;
use RainLoop\Providers\Storage\Enumerations\StorageType;
use function trim;

trait Accounts
{

public function GetAccounts(Account $oAccount): array
{
if ($this->GetCapa(false, Capa::ADDITIONAL_ACCOUNTS, $oAccount)) {
$sAccounts = $this->StorageProvider()->Get($oAccount,
StorageType::CONFIG,
'accounts'
);

$aAccounts = $sAccounts ? \json_decode($sAccounts, true) : array();

if (\is_array($aAccounts) && \count($aAccounts)) {
if (1 === \count($aAccounts)) {
$this->SetAccounts($oAccount, array());

} else if (1 < \count($aAccounts)) {
$sOrder = $this->StorageProvider()->Get($oAccount,
StorageType::CONFIG,
'accounts_identities_order'
);

$aOrder = empty($sOrder) ? array() : \json_decode($sOrder, true);
if (isset($aOrder['Accounts']) && \is_array($aOrder['Accounts']) && 1 < \count($aOrder['Accounts'])) {
$aAccounts = \array_merge(\array_flip($aOrder['Accounts']), $aAccounts);

$aAccounts = \array_filter($aAccounts, function ($sHash) {
return 5 < \strlen($sHash);
});
}
}

return $aAccounts;
}
}

$aAccounts = array();
if (!$oAccount->IsAdditionalAccount()) {
$aAccounts[$oAccount->Email()] = $oAccount->GetAuthToken();
}

return $aAccounts;
}

protected function SetAccounts(Account $oAccount, array $aAccounts = array()): void
{
$sParentEmail = $oAccount->ParentEmailHelper();
if (!$aAccounts ||
(1 === \count($aAccounts) && !empty($aAccounts[$sParentEmail]))) {
$this->StorageProvider()->Clear(
$oAccount,
StorageType::CONFIG,
'accounts'
);
} else {
$this->StorageProvider()->Put(
$oAccount,
StorageType::CONFIG,
'accounts',
\json_encode($aAccounts)
);
}
}

/**
* @throws \MailSo\Base\Exceptions\Exception
*/
Expand All @@ -28,7 +89,7 @@ public function DoAccountSetup(): array

$aAccounts = $this->GetAccounts($oAccount);

$sEmail = trim($this->GetActionParam('Email', ''));
$sEmail = \trim($this->GetActionParam('Email', ''));
$sPassword = $this->GetActionParam('Password', '');
$bNew = '1' === (string)$this->GetActionParam('New', '1');

Expand Down Expand Up @@ -63,7 +124,7 @@ public function DoAccountDelete(): array
}

$sParentEmail = $oAccount->ParentEmailHelper();
$sEmailToDelete = trim($this->GetActionParam('EmailToDelete', ''));
$sEmailToDelete = \trim($this->GetActionParam('EmailToDelete', ''));
$sEmailToDelete = \MailSo\Base\Utils::IdnToAscii($sEmailToDelete, true);

$aAccounts = $this->GetAccounts($oAccount);
Expand Down Expand Up @@ -93,7 +154,7 @@ public function DoIdentityUpdate(): array
{
$oAccount = $this->getAccountFromToken();

$oIdentity = new \RainLoop\Model\Identity();
$oIdentity = new Identity();
if (!$oIdentity->FromJSON($this->GetActionParams(), true)) {
throw new ClientException(Notifications::InvalidInputArgument);
}
Expand All @@ -113,7 +174,7 @@ public function DoIdentityDelete(): array
return $this->FalseResponse(__FUNCTION__);
}

$sId = trim($this->GetActionParam('IdToDelete', ''));
$sId = \trim($this->GetActionParam('IdToDelete', ''));
if (empty($sId)) {
throw new ClientException(Notifications::UnknownError);
}
Expand All @@ -137,7 +198,7 @@ public function DoAccountsAndIdentitiesSortOrder(): array
}

return $this->DefaultResponse(__FUNCTION__, $this->StorageProvider()->Put($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG, 'accounts_identities_order',
StorageType::CONFIG, 'accounts_identities_order',
\json_encode(array(
'Accounts' => \is_array($aAccounts) ? $aAccounts : array(),
'Identities' => \is_array($aIdentities) ? $aIdentities : array()
Expand Down
12 changes: 6 additions & 6 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace RainLoop\Actions;

use \RainLoop\Enumerations\Capa;
use \RainLoop\Enumerations\PluginPropertyType;
use \RainLoop\Exceptions\ClientException;
use \RainLoop\KeyPathHelper;
use \RainLoop\Notifications;
use \RainLoop\Utils;
use RainLoop\Enumerations\Capa;
use RainLoop\Enumerations\PluginPropertyType;
use RainLoop\Exceptions\ClientException;
use RainLoop\KeyPathHelper;
use RainLoop\Notifications;
use RainLoop\Utils;

trait Admin
{
Expand Down
51 changes: 33 additions & 18 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@ public function DoSaveContactsSyncData() : array

$mData = $this->getContactsSyncData($oAccount);

$sPassword = APP_DUMMY === $sPassword && isset($mData['Password'])
? $mData['Password'] : (APP_DUMMY === $sPassword ? '' : $sPassword);

$bResult = $this->StorageProvider()->Put(
$oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG,
'contacts_sync',
\json_encode(array(
'Enable' => $bEnabled,
'User' => $sUser,
'Password' => $sPassword ? \SnappyMail\Crypt::EncryptToJSON($sPassword, $oAccount->PasswordHash()) : null,
'Url' => $sUrl
))
);
$bResult = $this->setContactsSyncData(array(
'Enable' => $bEnabled,
'User' => $sUser,
'Password' => APP_DUMMY === $sPassword && isset($mData['Password'])
? $mData['Password'] : (APP_DUMMY === $sPassword ? '' : $sPassword),
'Url' => $sUrl
));

return $this->DefaultResponse(__FUNCTION__, $bResult);
}
Expand Down Expand Up @@ -241,6 +234,20 @@ public function UploadContacts() : array
return $this->DefaultResponse(__FUNCTION__, $mResponse);
}

protected function setContactsSyncData(\RainLoop\Model\Account $oAccount, array $aData) : bool
{
if ($aData['Password']) {
$aData['Password'] = \SnappyMail\Crypt::EncryptToJSON($aData['Password'], $oAccount->PasswordHash());
}
$aData['PasswordHMAC'] = $aData['Password'] ? \hash_hmac('sha1', $aData['Password'], $oAccount->PasswordHash()) : null;
return $this->StorageProvider()->Put(
$oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG,
'contacts_sync',
\json_encode($aData)
);
}

protected function getContactsSyncData(\RainLoop\Model\Account $oAccount) : ?array
{
$sData = $this->StorageProvider()->Get($oAccount,
Expand All @@ -251,17 +258,25 @@ protected function getContactsSyncData(\RainLoop\Model\Account $oAccount) : ?arr
$aData = \json_decode($sData);
if ($aData) {
if ($aData['Password']) {
$aData['Password'] = \SnappyMail\Crypt::DecryptFromJSON(
$aData['Password'],
$oAccount->PasswordHash()
);
// Verify oAccount password hasn't changed so that Password can be decrypted
if ($aData['PasswordHMAC'] !== \hash_hmac('sha1', $aData['Password'], $oAccount->PasswordHash())) {
// Failed
$aData['Password'] = null;
} else {
// Success
$aData['Password'] = \SnappyMail\Crypt::DecryptFromJSON(
$aData['Password'],
$oAccount->PasswordHash()
);
}
}
return $aData;
}

// Try the old
$aData = \RainLoop\Utils::DecodeKeyValues($sData);
if ($aData) {
$this->setContactsSyncData($oAccount, $aData);
return array(
'Enable' => isset($aData['Enable']) ? !!$aData['Enable'] : false,
'Url' => isset($aData['Url']) ? \trim($aData['Url']) : '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RainLoop\Actions;

use \RainLoop\Enumerations\Capa;
use RainLoop\Enumerations\Capa;

trait Filters
{
Expand Down
8 changes: 4 additions & 4 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace RainLoop\Actions;

use \RainLoop\Enumerations\Capa;
use \RainLoop\Exceptions\ClientException;
use \RainLoop\Notifications;
use \MailSo\Imap\Enumerations\FolderType;
use RainLoop\Enumerations\Capa;
use RainLoop\Exceptions\ClientException;
use RainLoop\Notifications;
use MailSo\Imap\Enumerations\FolderType;

trait Folders
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace RainLoop\Actions;

use \RainLoop\Enumerations\Capa;
use \RainLoop\Exceptions\ClientException;
use \RainLoop\Model\Account;
use \RainLoop\Notifications;
use RainLoop\Enumerations\Capa;
use RainLoop\Exceptions\ClientException;
use RainLoop\Model\Account;
use RainLoop\Notifications;

trait Messages
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace RainLoop\Actions;

use \RainLoop\Enumerations\Capa;
use \RainLoop\Notifications;
use \RainLoop\Utils;
use RainLoop\Enumerations\Capa;
use RainLoop\Notifications;
use RainLoop\Utils;

trait Response
{
Expand Down
Loading

0 comments on commit ef4790d

Please sign in to comment.