chore: refactor nexcloud plugin- related to NC-addressbook

This commit is contained in:
Fahim Salam Chowdhury 2024-09-03 12:37:37 +06:00
parent 2e3fbe0a12
commit 552a91acc2
5 changed files with 200 additions and 163 deletions

View file

@ -10,13 +10,27 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
DESCRIPTION = 'Integrate with Nextcloud v20+',
REQUIRED = '2.36.2';
private const IGNORE_SYSTEM_ADDRESSBOOK_KEY = 'ignoreSystemAddressbook';
private const IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE = true;
private const DEFAULT_ADDRESSBOOK_NAME = 'WebMail';
private const DEFAULT_ADDRESSBOOK_DESCRIPTION = 'Recipients from snappymail';
private const ENABLE_NC_ADDRESSBOOK_KEY = 'enableNcAddressbook';
private const ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE = false;
private const DISABLE_INHOUSE_ADDRESSBOOK_KEY = 'disableSnappymailContactsUI';
private const DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE = false;
private const DEFAULT_ADDRESSBOOK_URI_KEY = 'defaultNCAddressbookUri';
private const DEFAULT_ADDRESSBOOK_URI = 'webmail';
private const ADDRESSBOOK_SETTINGS_KEY = 'nextCloudAddressBookUri';
public function Init(): void
private const DEFAULT_ADDRESSBOOK_NAME_KEY = 'defaultNCAddressbookName';
private const DEFAULT_ADDRESSBOOK_NAME = 'WebMail';
private const DEFAULT_ADDRESSBOOK_DESCRIPTION_KEY = 'defaultNCAddressbookDescription';
private const DEFAULT_ADDRESSBOOK_DESCRIPTION = 'Recipients from snappymail';
private const ADDRESSBOOK_SETTINGS_KEY = 'nextcloudAddressBookUri';
public function Init() : void
{
if (static::IsIntegrated()) {
\SnappyMail\Log::debug('Nextcloud', 'integrated');
@ -42,19 +56,19 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
$this->addTemplate('templates/PopupsNextcloudFiles.html');
$this->addTemplate('templates/PopupsNextcloudCalendars.html');
// $this->addHook('login.credentials.step-2', 'loginCredentials2');
// $this->addHook('login.credentials', 'loginCredentials');
// $this->addHook('login.credentials.step-2', 'loginCredentials2');
// $this->addHook('login.credentials', 'loginCredentials');
$this->addHook('imap.before-login', 'beforeLogin');
$this->addHook('smtp.before-login', 'beforeLogin');
$this->addHook('sieve.before-login', 'beforeLogin');
if ($this->Config()->Get('plugin', 'enableNcAddressbook', false)) {
$this->addJs('js/addressbook.js');
if ($this->Config()->Get('plugin', self::ENABLE_NC_ADDRESSBOOK_KEY, self::ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE)) {
$this->addJs('js/nextcloudAddressbook.js');
$this->addJsonHook('NextcloudGetAddressBooks', 'GetAddressBooks');
$this->addJsonHook('NextcloudUpdateAddressBook', 'UpdateAddressBook');
}
if ($this->Config()->Get('plugin', 'disableInhouseAddressbook', false)) {
if ($this->Config()->Get('plugin', self::DISABLE_INHOUSE_ADDRESSBOOK_KEY, self::DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE)) {
$this->addJs('js/hideInhouseAddressbook.js');
}
} else {
@ -70,9 +84,9 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
$contactsManager = \OC::$server->getContactsManager();
$defaultUri = $this->Config()->Get('plugin', 'defaultNCAddressbookUri', self::DEFAULT_ADDRESSBOOK_URI);
$defaultUri = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_URI_KEY, self::DEFAULT_ADDRESSBOOK_URI);
$selectedUri = $this->UserSettings()->GetConf(self::ADDRESSBOOK_SETTINGS_KEY, $defaultUri);
$ignoreSystemAddressbook = $this->Config()->Get('plugin', 'ignoreSystemAddressbook', true);
$ignoreSystemAddressbook = $this->Config()->Get('plugin', self::IGNORE_SYSTEM_ADDRESSBOOK_KEY, self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE);
foreach ($contactsManager->getUserAddressBooks() as $addressBook) {
if ($ignoreSystemAddressbook && $addressBook->isSystemAddressBook()) {
@ -96,7 +110,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
));
}
public function UpdateAddressBook(): array
public function UpdateAddressBook() : array
{
$uri = $this->jsonParam('uri');
$oSettings = $this->UserSettings();
@ -115,7 +129,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
}
public function Supported(): string
public function Supported() : string
{
return static::IsIntegrated() ? '' : 'Nextcloud not found to use this plugin';
}
@ -130,24 +144,24 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
return static::IsIntegrated() && \OC::$server->getUserSession()->isLoggedIn();
}
public function loginCredentials(string &$sEmail, string &$sLogin, ?string &$sPassword = null): void
public function loginCredentials(string &$sEmail, string &$sLogin, ?string &$sPassword = null) : void
{
/**
* This has an issue.
* When user changes email address, all settings are gone as the new
* _data_/_default_/storage/{domain}/{local-part} is used
*/
// $ocUser = \OC::$server->getUserSession()->getUser();
// $sEmail = $ocUser->getEMailAddress() ?: $ocUser->getPrimaryEMailAddress() ?: $sEmail;
// $ocUser = \OC::$server->getUserSession()->getUser();
// $sEmail = $ocUser->getEMailAddress() ?: $ocUser->getPrimaryEMailAddress() ?: $sEmail;
}
public function loginCredentials2(string &$sEmail, ?string &$sPassword = null): void
public function loginCredentials2(string &$sEmail, ?string &$sPassword = null) : void
{
$ocUser = \OC::$server->getUserSession()->getUser();
$sEmail = $ocUser->getEMailAddress() ?: $ocUser->getPrimaryEMailAddress() ?: $sEmail;
}
public function beforeLogin(\RainLoop\Model\Account $oAccount, \MailSo\Net\NetClient $oClient, \MailSo\Net\ConnectSettings $oSettings): void
public function beforeLogin(\RainLoop\Model\Account $oAccount, \MailSo\Net\NetClient $oClient, \MailSo\Net\ConnectSettings $oSettings) : void
{
// https://apps.nextcloud.com/apps/oidc_login
$config = \OC::$server->getConfig();
@ -164,12 +178,11 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
// Only login with OIDC access token if
// it is enabled in config, the user is currently logged in with OIDC,
// the current snappymail account is the OIDC account and no account defined explicitly
if (
\OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-autologin-oidc', false)
&& \OC::$server->getSession()->get('is_oidc')
&& $sNcEmail === $oSettings->username
&& !$bAccountDefinedExplicitly
// && $oClient->supportsAuthType('OAUTHBEARER') // v2.28
if (\OC::$server->getConfig()->getAppValue('snappymail', 'snappymail-autologin-oidc', false)
&& \OC::$server->getSession()->get('is_oidc')
&& $sNcEmail === $oSettings->username
&& !$bAccountDefinedExplicitly
// && $oClient->supportsAuthType('OAUTHBEARER') // v2.28
) {
$sAccessToken = \OC::$server->getSession()->get('oidc_access_token');
if ($sAccessToken) {
@ -184,7 +197,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
\OC::$server->getLDAPProvider();
*/
public function NextcloudAttachFile(): array
public function NextcloudAttachFile() : array
{
$aResult = [
'success' => false,
@ -208,10 +221,10 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
return $this->jsonResponse(__FUNCTION__, $aResult);
}
public function NextcloudSaveMsg(): array
public function NextcloudSaveMsg() : array
{
$sSaveFolder = \ltrim($this->jsonParam('folder', ''), '/');
// $aValues = \RainLoop\Api::Actions()->decodeRawKey($this->jsonParam('msgHash', ''));
// $aValues = \RainLoop\Api::Actions()->decodeRawKey($this->jsonParam('msgHash', ''));
$msgHash = $this->jsonParam('msgHash', '');
$aValues = \json_decode(\MailSo\Base\Utils::UrlSafeBase64Decode($msgHash), true);
$aResult = [
@ -265,14 +278,14 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
foreach ($data->items as $aItem) {
$sSavedFileName = empty($aItem['fileName']) ? 'file.dat' : $aItem['fileName'];
if (!empty($aItem['data'])) {
$sSavedFileNameFull = static::SmartFileExists($sSaveFolder . '/' . $sSavedFileName, $oFiles);
$sSavedFileNameFull = static::SmartFileExists($sSaveFolder.'/'.$sSavedFileName, $oFiles);
if (!$oFiles->file_put_contents($sSavedFileNameFull, $aItem['data'])) {
$data->result = false;
}
} else if (!empty($aItem['fileHash'])) {
$fFile = $data->filesProvider->GetFile($data->account, $aItem['fileHash'], 'rb');
if (\is_resource($fFile)) {
$sSavedFileNameFull = static::SmartFileExists($sSaveFolder . '/' . $sSavedFileName, $oFiles);
$sSavedFileNameFull = static::SmartFileExists($sSaveFolder.'/'.$sSavedFileName, $oFiles);
if (!$oFiles->file_put_contents($sSavedFileNameFull, $fFile)) {
$data->result = false;
}
@ -286,19 +299,19 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
}
public function FilterAppData($bAdmin, &$aResult): void
public function FilterAppData($bAdmin, &$aResult) : void
{
if (!$bAdmin && \is_array($aResult)) {
$ocUser = \OC::$server->getUserSession()->getUser();
$sUID = $ocUser->getUID();
$oUrlGen = \OC::$server->getURLGenerator();
$sWebDAV = $oUrlGen->getAbsoluteURL($oUrlGen->linkTo('', 'remote.php') . '/dav');
// $sWebDAV = \OCP\Util::linkToRemote('dav');
// $sWebDAV = \OCP\Util::linkToRemote('dav');
$aResult['Nextcloud'] = [
'UID' => $sUID,
'WebDAV' => $sWebDAV,
'CalDAV' => $this->Config()->Get('plugin', 'calendar', false)
// 'WebDAV_files' => $sWebDAV . '/files/' . $sUID
// 'WebDAV_files' => $sWebDAV . '/files/' . $sUID
];
if (empty($aResult['Auth'])) {
$config = \OC::$server->getConfig();
@ -320,9 +333,9 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
if (!$sEmail) {
$sEmail = $ocUser->getEMailAddress();
// ?: $ocUser->getPrimaryEMailAddress();
// ?: $ocUser->getPrimaryEMailAddress();
}
/*
/*
if ($config->getAppValue('snappymail', 'snappymail-autologin-oidc', false)) {
if (\OC::$server->getSession()->get('is_oidc')) {
$sEmail = "{$sUID}@nextcloud";
@ -369,7 +382,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
}
public function FilterLanguage(&$sLanguage, $bAdmin): void
public function FilterLanguage(&$sLanguage, $bAdmin) : void
{
if (!\RainLoop\Api::Config()->Get('webmail', 'allow_languages_on_settings', true)) {
$aResultLang = \SnappyMail\L10n::getLanguages($bAdmin);
@ -392,7 +405,7 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
*
* @return string return locale
*/
private function determineLocale(string $langCode, array $languagesArray): ?string
private function determineLocale(string $langCode, array $languagesArray) : ?string
{
// Direct check for the language code
if (\in_array($langCode, $languagesArray)) {
@ -429,20 +442,20 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
include_once __DIR__ . '/NextcloudContactsSuggestions.php';
$mResult[] = new NextcloudContactsSuggestions(
$this->Config()->Get('plugin', 'ignoreSystemAddressbook', true)
$this->Config()->Get('plugin', self::IGNORE_SYSTEM_ADDRESSBOOK_KEY, self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE)
);
}
if ('address-book' === $sName && $this->Config()->Get('plugin', 'enableNcAddressbook', false)) {
if ('address-book' === $sName && $this->Config()->Get('plugin', self::ENABLE_NC_ADDRESSBOOK_KEY, self::ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE)) {
if (!\is_array($mResult)) {
$mResult = array();
}
include_once __DIR__ . '/NextcloudAddressBook.php';
$ignoreSystemAddressbook = $this->Config()->Get('plugin', 'ignoreSystemAddressbook', true);
$defaultName = $this->Config()->Get('plugin', 'defaultNCAddressbookName', self::DEFAULT_ADDRESSBOOK_NAME);
$defaultDescription = $this->Config()->Get('plugin', 'defaultNCAddressbookDescription', self::DEFAULT_ADDRESSBOOK_DESCRIPTION);
$defaultUri = $this->Config()->Get('plugin', 'defaultNCAddressbookUri', self::DEFAULT_ADDRESSBOOK_URI);
$ignoreSystemAddressbook = $this->Config()->Get('plugin', self::IGNORE_SYSTEM_ADDRESSBOOK_KEY, self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE);
$defaultName = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_NAME_KEY, self::DEFAULT_ADDRESSBOOK_NAME);
$defaultDescription = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_DESCRIPTION_KEY, self::DEFAULT_ADDRESSBOOK_DESCRIPTION);
$defaultUri = $this->Config()->Get('plugin', self::DEFAULT_ADDRESSBOOK_URI_KEY, self::DEFAULT_ADDRESSBOOK_URI);
$mResult = new NextcloudAddressBook(
$defaultUri,
@ -460,16 +473,16 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
}
}
protected function configMapping(): array
protected function configMapping() : array
{
return array(
\RainLoop\Plugins\Property::NewInstance('suggestions')->SetLabel('Suggestions')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(true),
\RainLoop\Plugins\Property::NewInstance('ignoreSystemAddressbook')->SetLabel('Ignore system addressbook')
\RainLoop\Plugins\Property::NewInstance(self::IGNORE_SYSTEM_ADDRESSBOOK_KEY)->SetLabel('Ignore system addressbook')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(true),
/*
->SetDefaultValue(self::IGNORE_SYSTEM_ADDRESSBOOK_DEFAULT_VALUE),
/*
\RainLoop\Plugins\Property::NewInstance('storage')->SetLabel('Use Nextcloud user ID in config storage path')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false)
@ -478,29 +491,29 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false),
\RainLoop\Plugins\Property::NewInstance('enableNcAddressbook')->SetLabel('Enable User to choose Nextcloud addressbook for recipients')
\RainLoop\Plugins\Property::NewInstance(self::ENABLE_NC_ADDRESSBOOK_KEY)->SetLabel('Enable User to choose Nextcloud addressbook for recipients')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false),
->SetDefaultValue(self::ENABLE_NC_ADDRESSBOOK_DEFAULT_VALUE),
\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookUri')->SetLabel('Default nextcloud addressbook URI for recipinets')
\RainLoop\Plugins\Property::NewInstance(self::DEFAULT_ADDRESSBOOK_URI_KEY)->SetLabel('Default nextcloud addressbook URI for recipinets')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
->SetDefaultValue('webmail'),
->SetDefaultValue(self::DEFAULT_ADDRESSBOOK_URI),
\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookName')->SetLabel('Default nextcloud addressbook Name for recipinets')
\RainLoop\Plugins\Property::NewInstance(self::DEFAULT_ADDRESSBOOK_NAME_KEY)->SetLabel('Default nextcloud addressbook Name for recipinets')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
->SetDefaultValue(self::DEFAULT_ADDRESSBOOK_NAME),
\RainLoop\Plugins\Property::NewInstance('defaultNCAddressbookDescription')->SetLabel('Default nextcloud addressbook description for recipinets')
\RainLoop\Plugins\Property::NewInstance(self::DEFAULT_ADDRESSBOOK_DESCRIPTION_KEY)->SetLabel('Default nextcloud addressbook description for recipinets')
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING)
->SetDefaultValue(self::DEFAULT_ADDRESSBOOK_DESCRIPTION),
\RainLoop\Plugins\Property::NewInstance('disableInhouseAddressbook')->SetLabel('Disable SnappyMail internal addressbook. This is recomended if nextcloud addressbook is being used.')
\RainLoop\Plugins\Property::NewInstance(self::DISABLE_INHOUSE_ADDRESSBOOK_KEY)->SetLabel('Disable SnappyMail internal addressbook. This is recomended if nextcloud addressbook is being used.')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
->SetDefaultValue(false)
->SetDefaultValue(self::DISABLE_INHOUSE_ADDRESSBOOK_DEFAULT_VALUE)
);
}
private static function SmartFileExists(string $sFilePath, $oFiles): string
private static function SmartFileExists(string $sFilePath, $oFiles) : string
{
$sFilePath = \str_replace('\\', '/', \trim($sFilePath));
@ -514,10 +527,11 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
while (true) {
++$iIndex;
$sFilePathNew = $aFileInfo['dirname'] . '/' .
\preg_replace('/\(\d{1,2}\)$/', '', $aFileInfo['filename']) .
' (' . $iIndex . ')' .
(empty($aFileInfo['extension']) ? '' : '.' . $aFileInfo['extension']);
$sFilePathNew = $aFileInfo['dirname'].'/'.
\preg_replace('/\(\d{1,2}\)$/', '', $aFileInfo['filename']).
' ('.$iIndex.')'.
(empty($aFileInfo['extension']) ? '' : '.'.$aFileInfo['extension'])
;
if (!$oFiles->file_exists($sFilePathNew)) {
return $sFilePathNew;
}
@ -528,17 +542,17 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin
return $sFilePath;
}
private function Account(): \RainLoop\Model\Account
private function Account() : \RainLoop\Model\Account
{
return \RainLoop\Api::Actions()->getAccountFromToken();
}
private function SettingsProvider(): \RainLoop\Providers\Settings
private function SettingsProvider() : \RainLoop\Providers\Settings
{
return \RainLoop\Api::Actions()->SettingsProvider(true);
}
private function UserSettings(): \RainLoop\Settings
private function UserSettings() : \RainLoop\Settings
{
return $this->SettingsProvider()->Load($this->Account());
}