AddressBook: Drop $sEmail parameter from functions and use new SetEmail()

AddressBook: Better CardDAV client with setDAVClientConfig()
This commit is contained in:
the-djmaze 2022-05-19 10:39:32 +02:00
parent 5f94e882a9
commit f12019d77a
9 changed files with 149 additions and 153 deletions

View file

@ -110,16 +110,21 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return true; return true;
} }
public function SetEmail(string $sEmail) : bool
{
return true;
}
/** /**
* Sync with davClient * Sync with davClient
*/ */
public function Sync(array $oConfig) : bool public function Sync() : bool
{ {
// TODO // TODO
return false; return false;
} }
public function Export(string $sEmail, string $sType = 'vcf') : bool public function Export(string $sType = 'vcf') : bool
{ {
$bVcf = 'vcf' === $sType; $bVcf = 'vcf' === $sType;
$bCsvHeader = true; $bCsvHeader = true;
@ -161,11 +166,8 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return true; return true;
} }
public function ContactSave(string $sEmail, Contact $oContact) : bool public function ContactSave(Contact $oContact) : bool
{ {
// TODO
// $emails = $oContact->GetEmails();
if (!$this->SelectFolder()) { if (!$this->SelectFolder()) {
return false; return false;
} }
@ -233,13 +235,26 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return true; return true;
} }
public function DeleteContacts(string $sEmail, array $aContactIds) : bool public function DeleteContacts(array $aContactIds) : bool
{ {
try { try {
$this->MailClient()->MessageDelete( $this->MailClient()->MessageDelete(
$this->sFolderName, $this->sFolderName,
new \MailSo\Imap\SequenceSet($aContactIds) new \MailSo\Imap\SequenceSet($aContactIds)
); );
/*
// Delete remote when Mode = read + write
if (1 === $oConfig['Mode']) {
$oClient = $this->getDavClient();
if ($oClient) {
$sPath = $oClient->__UrlPath__;
$aRemoteSyncData = $this->prepareDavSyncData($oClient, $sPath);
if ($aRemoteSyncData && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) {
$this->davClientRequest($oClient, 'DELETE', $sPath.$aRemoteSyncData[$sKey]['vcf']);
}
}
}
*/
return true; return true;
} catch (\Throwable $e) { } catch (\Throwable $e) {
} }
@ -254,7 +269,7 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return false; return false;
} }
public function GetContacts(string $sEmail, int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array
{ {
if (!\strlen($this->sFolderName)) { if (!\strlen($this->sFolderName)) {
// return []; // return [];
@ -292,7 +307,7 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return $aResult; return $aResult;
} }
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?Contact public function GetContactByID($mID, bool $bIsStrID = false) : ?Contact
{ {
if ($bIsStrID) { if ($bIsStrID) {
$oMessage = null; $oMessage = null;
@ -302,7 +317,7 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return $oMessage ? $this->MessageAsContact($oMessage) : null; return $oMessage ? $this->MessageAsContact($oMessage) : null;
} }
public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array public function GetSuggestions(string $sSearch, int $iLimit = 20) : array
{ {
$sSearch = \trim($sSearch); $sSearch = \trim($sSearch);
if (2 > \strlen($sSearch) || !$this->SelectFolder()) { if (2 > \strlen($sSearch) || !$this->SelectFolder()) {
@ -327,26 +342,14 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return $aResult; return $aResult;
} }
public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool
{ {
return false; return false;
} }
public function Test() : string public function Test() : string
{ {
$sResult = ''; // Nothing to test
try return '';
{
// $sResult = 'Unknown error';
}
catch (\Throwable $oException)
{
$sResult = $oException->getMessage();
if (!\is_string($sResult) || empty($sResult)) {
$sResult = 'Unknown error';
}
}
return $sResult;
} }
} }

View file

@ -501,7 +501,10 @@ class Actions
if ($bForceEnable || $this->GetCapa(Enumerations\Capa::CONTACTS)) { if ($bForceEnable || $this->GetCapa(Enumerations\Capa::CONTACTS)) {
$oDriver = $this->fabrica('address-book', $oAccount); $oDriver = $this->fabrica('address-book', $oAccount);
} }
if ($oAccount && $oDriver) {
$oDriver->SetEmail($this->GetMainEmail($oAccount));
$oDriver->setDAVClientConfig($this->getContactsSyncData($oAccount));
}
$this->oAddressBookProvider = new Providers\AddressBook($oDriver); $this->oAddressBookProvider = new Providers\AddressBook($oDriver);
$this->oAddressBookProvider->SetLogger($this->oLogger); $this->oAddressBookProvider->SetLogger($this->oLogger);
} }

View file

@ -32,31 +32,11 @@ trait Contacts
public function DoContactsSync() : array public function DoContactsSync() : array
{ {
$bResult = false;
$oAccount = $this->getAccountFromToken(); $oAccount = $this->getAccountFromToken();
$oAddressBookProvider = $this->AddressBookProvider($oAccount); $oAddressBookProvider = $this->AddressBookProvider($oAccount);
if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) if (!$oAddressBookProvider || !$oAddressBookProvider->Sync()) {
{
$mData = $this->getContactsSyncData($oAccount);
if (isset($mData['User'], $mData['Password'], $mData['Url']) && !empty($mData['Mode']))
{
$bResult = $oAddressBookProvider->Sync([
'Email' => $this->GetMainEmail($oAccount),
'Url' => $mData['Url'],
'User' => $mData['User'],
'Password' => $mData['Password'],
'Mode' => $mData['Mode'],
'Proxy' => ''
]);
}
}
if (!$bResult)
{
throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ContactsSyncError); throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ContactsSyncError);
} }
return $this->TrueResponse(__FUNCTION__); return $this->TrueResponse(__FUNCTION__);
} }
@ -77,8 +57,7 @@ trait Contacts
if ($oAbp->IsActive()) if ($oAbp->IsActive())
{ {
$iResultCount = 0; $iResultCount = 0;
$mResult = $oAbp->GetContacts($this->GetMainEmail($oAccount), $mResult = $oAbp->GetContacts($iOffset, $iLimit, $sSearch, $iResultCount);
$iOffset, $iLimit, $sSearch, $iResultCount);
} }
return $this->DefaultResponse(__FUNCTION__, array( return $this->DefaultResponse(__FUNCTION__, array(
@ -100,7 +79,7 @@ trait Contacts
$bResult = false; $bResult = false;
if (\count($aFilteredUids) && $this->AddressBookProvider($oAccount)->IsActive()) if (\count($aFilteredUids) && $this->AddressBookProvider($oAccount)->IsActive())
{ {
$bResult = $this->AddressBookProvider($oAccount)->DeleteContacts($this->GetMainEmail($oAccount), $aFilteredUids); $bResult = $this->AddressBookProvider($oAccount)->DeleteContacts($aFilteredUids);
} }
return $this->DefaultResponse(__FUNCTION__, $bResult); return $this->DefaultResponse(__FUNCTION__, $bResult);
@ -121,7 +100,7 @@ trait Contacts
$oContact = null; $oContact = null;
if (\strlen($sUid)) if (\strlen($sUid))
{ {
$oContact = $oAddressBookProvider->GetContactByID($this->GetMainEmail($oAccount), $sUid); $oContact = $oAddressBookProvider->GetContactByID($sUid);
} }
if (!$oContact) if (!$oContact)
@ -161,7 +140,7 @@ trait Contacts
$oContact->PopulateDisplayAndFullNameValue(true); $oContact->PopulateDisplayAndFullNameValue(true);
$bResult = $oAddressBookProvider->ContactSave($this->GetMainEmail($oAccount), $oContact); $bResult = $oAddressBookProvider->ContactSave($oContact);
} }
return $this->DefaultResponse(__FUNCTION__, array( return $this->DefaultResponse(__FUNCTION__, array(
@ -301,7 +280,7 @@ trait Contacts
$oAddressBookProvider = $this->AddressBookProvider($oAccount); $oAddressBookProvider = $this->AddressBookProvider($oAccount);
return $oAddressBookProvider->IsActive() ? return $oAddressBookProvider->IsActive() ?
$oAddressBookProvider->Export($this->GetMainEmail($oAccount), 'vcf') : false; $oAddressBookProvider->Export('vcf') : false;
} }
public function RawContactsCsv() : bool public function RawContactsCsv() : bool
@ -317,7 +296,7 @@ trait Contacts
$oAddressBookProvider = $this->AddressBookProvider($oAccount); $oAddressBookProvider = $this->AddressBookProvider($oAccount);
return $oAddressBookProvider->IsActive() ? return $oAddressBookProvider->IsActive() ?
$oAddressBookProvider->Export($this->GetMainEmail($oAccount), 'csv') : false; $oAddressBookProvider->Export('csv') : false;
} }
private function importContactsFromVcfFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile): int private function importContactsFromVcfFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile): int
@ -333,10 +312,7 @@ trait Contacts
if (is_string($sFile) && 5 < \strlen($sFile)) { if (is_string($sFile) && 5 < \strlen($sFile)) {
$this->Logger()->Write('Import contacts from vcf'); $this->Logger()->Write('Import contacts from vcf');
$iCount = $oAddressBookProvider->ImportVcfFile( $iCount = $oAddressBookProvider->ImportVcfFile($sFile);
$this->GetMainEmail($oAccount),
$sFile
);
} }
} }
} }
@ -379,10 +355,7 @@ trait Contacts
if (\count($aData)) { if (\count($aData)) {
$this->oLogger->Write('Import contacts from csv'); $this->oLogger->Write('Import contacts from csv');
$iCount = $oAddressBookProvider->ImportCsvArray( $iCount = $oAddressBookProvider->ImportCsvArray($aData);
$this->GetMainEmail($oAccount),
$aData
);
} }
} }
} }

View file

@ -308,9 +308,9 @@ trait Messages
$oSettings = $this->SettingsProvider()->Load($oAccount); $oSettings = $this->SettingsProvider()->Load($oAccount);
$this->AddressBookProvider($oAccount)->IncFrec( $this->AddressBookProvider($oAccount)->IncFrec(
$this->GetMainEmail($oAccount), \array_values($aArrayToFrec), \array_values($aArrayToFrec),
!!$oSettings->GetConf('ContactsAutosave', !!$oSettings->GetConf('ContactsAutosave', !!$oConfig->Get('defaults', 'contacts_autosave', true))
!!$oConfig->Get('defaults', 'contacts_autosave', true))); );
} }
} }
} }

View file

@ -399,7 +399,7 @@ trait User
$oAddressBookProvider = $this->AddressBookProvider($oAccount); $oAddressBookProvider = $this->AddressBookProvider($oAccount);
if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) if ($oAddressBookProvider && $oAddressBookProvider->IsActive())
{ {
$aSuggestions = $oAddressBookProvider->GetSuggestions($this->GetMainEmail($oAccount), $sQuery, $iLimit); $aSuggestions = $oAddressBookProvider->GetSuggestions($sQuery, $iLimit);
if (!\count($aResult)) if (!\count($aResult))
{ {
$aResult = $aSuggestions; $aResult = $aSuggestions;

View file

@ -27,24 +27,24 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
return $this->oDriver && $this->oDriver->IsSupported(); return $this->oDriver && $this->oDriver->IsSupported();
} }
public function Sync(array $oConfig) : bool public function Sync() : bool
{ {
return $this->IsActive() ? $this->oDriver->Sync($oConfig) : false; return $this->IsActive() ? $this->oDriver->Sync() : false;
} }
public function Export(string $sEmail, string $sType = 'vcf') : bool public function Export(string $sType = 'vcf') : bool
{ {
return $this->IsActive() ? $this->oDriver->Export($sEmail, $sType) : false; return $this->IsActive() ? $this->oDriver->Export($sType) : false;
} }
public function ContactSave(string $sEmail, \RainLoop\Providers\AddressBook\Classes\Contact $oContact) : bool public function ContactSave(\RainLoop\Providers\AddressBook\Classes\Contact $oContact) : bool
{ {
return $this->IsActive() ? $this->oDriver->ContactSave($sEmail, $oContact) : false; return $this->IsActive() ? $this->oDriver->ContactSave($oContact) : false;
} }
public function DeleteContacts(string $sEmail, array $aContactIds) : bool public function DeleteContacts(array $aContactIds) : bool
{ {
return $this->IsActive() ? $this->oDriver->DeleteContacts($sEmail, $aContactIds) : false; return $this->IsActive() ? $this->oDriver->DeleteContacts($aContactIds) : false;
} }
public function DeleteAllContacts(string $sEmail) : bool public function DeleteAllContacts(string $sEmail) : bool
@ -52,28 +52,27 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
return $this->IsActive() ? $this->oDriver->DeleteAllContacts($sEmail) : false; return $this->IsActive() ? $this->oDriver->DeleteAllContacts($sEmail) : false;
} }
public function GetContacts(string $sEmail, int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array
{ {
return $this->IsActive() ? $this->oDriver->GetContacts($sEmail, return $this->IsActive() ? $this->oDriver->GetContacts($iOffset, $iLimit, $sSearch, $iResultCount) : array();
$iOffset, $iLimit, $sSearch, $iResultCount) : array();
} }
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?\RainLoop\Providers\AddressBook\Classes\Contact public function GetContactByID($mID, bool $bIsStrID = false) : ?\RainLoop\Providers\AddressBook\Classes\Contact
{ {
return $this->IsActive() ? $this->oDriver->GetContactByID($sEmail, $mID, $bIsStrID) : null; return $this->IsActive() ? $this->oDriver->GetContactByID($mID, $bIsStrID) : null;
} }
/** /**
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array public function GetSuggestions(string $sSearch, int $iLimit = 20) : array
{ {
return $this->IsActive() ? $this->oDriver->GetSuggestions($sEmail, $sSearch, $iLimit) : array(); return $this->IsActive() ? $this->oDriver->GetSuggestions($sSearch, $iLimit) : array();
} }
public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool
{ {
return $this->IsActive() ? $this->oDriver->IncFrec($sEmail, $aEmails, $bCreateAuto) : false; return $this->IsActive() ? $this->oDriver->IncFrec($aEmails, $bCreateAuto) : false;
} }
private function csvNameToTypeConvertor(string $sCsvName) : int private function csvNameToTypeConvertor(string $sCsvName) : int
@ -156,7 +155,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
return !empty($sCsvNameLower) && isset($aMap[$sCsvNameLower]) ? $aMap[$sCsvNameLower] : PropertyType::UNKNOWN; return !empty($sCsvNameLower) && isset($aMap[$sCsvNameLower]) ? $aMap[$sCsvNameLower] : PropertyType::UNKNOWN;
} }
public function ImportCsvArray(string $sEmail, array $aCsvData) : int public function ImportCsvArray(array $aCsvData) : int
{ {
$iCount = 0; $iCount = 0;
if ($this->IsActive() && \count($aCsvData)) if ($this->IsActive() && \count($aCsvData))
@ -190,7 +189,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
if ($oContact && \count($oContact->Properties)) if ($oContact && \count($oContact->Properties))
{ {
if ($this->ContactSave($sEmail, $oContact)) if ($this->ContactSave($oContact))
{ {
$iCount++; $iCount++;
} }
@ -205,7 +204,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
return $iCount; return $iCount;
} }
public function ImportVcfFile(string $sEmail, string $sVcfData) : int public function ImportVcfFile(string $sVcfData) : int
{ {
$iCount = 0; $iCount = 0;
@ -243,7 +242,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
if (\count($oContact->Properties)) if (\count($oContact->Properties))
{ {
if ($this->ContactSave($sEmail, $oContact)) if ($this->ContactSave($oContact))
{ {
$iCount++; $iCount++;
} }

View file

@ -6,23 +6,25 @@ interface AddressBookInterface
{ {
public function IsSupported() : bool; public function IsSupported() : bool;
public function Sync(array $oConfig) : bool; public function SetEmail(string $sEmail) : bool;
public function Export(string $sEmail, string $sType = 'vcf') : bool; public function Sync() : bool;
public function ContactSave(string $sEmail, Classes\Contact $oContact) : bool; public function Export(string $sType = 'vcf') : bool;
public function DeleteContacts(string $sEmail, array $aContactIds) : bool; public function ContactSave(Classes\Contact $oContact) : bool;
public function DeleteContacts(array $aContactIds) : bool;
public function DeleteAllContacts(string $sEmail) : bool; public function DeleteAllContacts(string $sEmail) : bool;
public function GetContacts(string $sEmail, int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array; public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array;
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?Classes\Contact; public function GetContactByID($mID, bool $bIsStrID = false) : ?Classes\Contact;
public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array; public function GetSuggestions(string $sSearch, int $iLimit = 20) : array;
public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool; public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool;
public function Test() : string; public function Test() : string;
} }

View file

@ -6,6 +6,22 @@ use SnappyMail\DAV\Client as DAVClient;
trait CardDAV trait CardDAV
{ {
private $aDAVConfig = ['Mode' => 0];
public function setDAVClientConfig(?array $aConfig)
{
if (isset($aConfig['User'], $aConfig['Password'], $aConfig['Url']) && !empty($aConfig['Mode'])) {
$this->aDAVConfig = $aConfig;
} else {
$this->aDAVConfig = ['Mode' => 0];
}
}
protected function isDAVReadWrite()
{
return 1 == $this->aDAVConfig['Mode'];
}
protected function prepareDavSyncData(DAVClient $oClient, string $sPath) protected function prepareDavSyncData(DAVClient $oClient, string $sPath)
{ {
$mResult = false; $mResult = false;
@ -401,8 +417,16 @@ trait CardDAV
return $oClient; return $oClient;
} }
protected function getDavClient(string $sUrl, string $sUser, string $sPassword, string $sProxy = '') : ?DAVClient protected function getDavClient() : ?DAVClient
{ {
if (!$this->aDAVConfig['Mode']) {
return null;
}
$sUrl = $aConfig['Url'];
$sUser = $aConfig['User'];
$sPassword = $aConfig['Password'];
$sProxy = '';
$aMatch = array(); $aMatch = array();
$sUserAddressBookNameName = ''; $sUserAddressBookNameName = '';

View file

@ -10,6 +10,8 @@ class PdoAddressBook
{ {
use CardDAV; use CardDAV;
private $iUserID = 0;
/** /**
* @var string * @var string
*/ */
@ -72,6 +74,12 @@ class PdoAddressBook
return \is_array($aDrivers) && \in_array($this->sDsnType, $aDrivers); return \is_array($aDrivers) && \in_array($this->sDsnType, $aDrivers);
} }
public function SetEmail(string $sEmail) : bool
{
$this->iUserID = $this->getUserId($sEmail);
return 0 < $this->iUserID;
}
private function flushDeletedContacts(int $iUserID) : bool private function flushDeletedContacts(int $iUserID) : bool
{ {
return !!$this->prepareAndExecute('DELETE FROM rainloop_ab_contacts WHERE id_user = :id_user AND deleted = 1', array( return !!$this->prepareAndExecute('DELETE FROM rainloop_ab_contacts WHERE id_user = :id_user AND deleted = 1', array(
@ -127,18 +135,17 @@ class PdoAddressBook
return $aResult; return $aResult;
} }
public function Sync(array $oConfig) : bool public function Sync() : bool
{ {
$this->SyncDatabase(); $this->SyncDatabase();
$iUserID = $this->getUserId($oConfig['Email']); if (1 > $this->iUserID)
if (0 >= $iUserID)
{ {
\SnappyMail\Log::warning('PdoAddressBook', 'Sync() invalid $iUserID'); \SnappyMail\Log::warning('PdoAddressBook', 'Sync() invalid $iUserID');
return false; return false;
} }
$oClient = $this->getDavClient($oConfig['Url'], $oConfig['User'], $oConfig['Password'], $oConfig['Proxy']); $oClient = $this->getDavClient();
if (!$oClient) if (!$oClient)
{ {
\SnappyMail\Log::warning('PdoAddressBook', 'Sync() invalid DavClient'); \SnappyMail\Log::warning('PdoAddressBook', 'Sync() invalid DavClient');
@ -154,13 +161,13 @@ class PdoAddressBook
return false; return false;
} }
$aDatabaseSyncData = $this->prepareDatabaseSyncData($iUserID); $aDatabaseSyncData = $this->prepareDatabaseSyncData($this->iUserID);
// $this->oLogger->WriteDump($aRemoteSyncData); // $this->oLogger->WriteDump($aRemoteSyncData);
// $this->oLogger->WriteDump($aDatabaseSyncData); // $this->oLogger->WriteDump($aDatabaseSyncData);
// Delete remote when Mode = read + write // Delete remote when Mode = read + write
if (1 === $oConfig['Mode']) { if ($this->isDAVReadWrite()) {
foreach ($aDatabaseSyncData as $sKey => $aData) foreach ($aDatabaseSyncData as $sKey => $aData)
{ {
if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf']))
@ -181,10 +188,10 @@ class PdoAddressBook
} }
if (\count($aIdsForDeletedion)) if (\count($aIdsForDeletedion))
{ {
$this->DeleteContacts($oConfig['Email'], $aIdsForDeletedion, false); $this->DeleteContacts($aIdsForDeletedion, false);
} }
$this->flushDeletedContacts($iUserID); $this->flushDeletedContacts($this->iUserID);
//+++new or newer (from db) //+++new or newer (from db)
foreach ($aDatabaseSyncData as $sKey => $aData) foreach ($aDatabaseSyncData as $sKey => $aData)
@ -199,7 +206,7 @@ class PdoAddressBook
) )
{ {
$mID = $aData['id_contact']; $mID = $aData['id_contact'];
$oContact = $this->GetContactByID($oConfig['Email'], $mID, false); $oContact = $this->GetContactByID($mID);
if ($oContact) if ($oContact)
{ {
$sExsistensBody = ''; $sExsistensBody = '';
@ -216,7 +223,7 @@ class PdoAddressBook
} }
// Add remote when Mode = read + write // Add remote when Mode = read + write
if (1 === $oConfig['Mode']) { if ($this->isDAVReadWrite()) {
$oResponse = $this->davClientRequest($oClient, 'PUT', $oResponse = $this->davClientRequest($oClient, 'PUT',
$sPath.(\strlen($mExsistenRemoteID) ? $mExsistenRemoteID : $oContact->CardDavNameUri()), $sPath.(\strlen($mExsistenRemoteID) ? $mExsistenRemoteID : $oContact->CardDavNameUri()),
$oContact->ToVCard($sExsistensBody, $this->oLogger)."\r\n\r\n"); $oContact->ToVCard($sExsistensBody, $this->oLogger)."\r\n\r\n");
@ -227,7 +234,7 @@ class PdoAddressBook
if (!empty($sEtag)) if (!empty($sEtag))
{ {
$iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate); $iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate);
$this->updateContactEtagAndTime($iUserID, $mID, $sEtag, $iChanged); $this->updateContactEtagAndTime($this->iUserID, $mID, $sEtag, $iChanged);
} }
} }
} }
@ -284,7 +291,7 @@ class PdoAddressBook
$oContact = null; $oContact = null;
if ($mExsistenContactID) if ($mExsistenContactID)
{ {
$oContact = $this->GetContactByID($oConfig['Email'], $mExsistenContactID); $oContact = $this->GetContactByID($mExsistenContactID);
} }
if (!$oContact) if (!$oContact)
{ {
@ -296,7 +303,7 @@ class PdoAddressBook
\trim(\trim($oResponse->getHeader('etag')), '"\'') \trim(\trim($oResponse->getHeader('etag')), '"\'')
); );
$this->ContactSave($oConfig['Email'], $oContact); $this->ContactSave($oContact);
unset($oContact); unset($oContact);
// } else if ($this->oLogger) { // } else if ($this->oLogger) {
// $this->oLogger->WriteDump($sBody); // $this->oLogger->WriteDump($sBody);
@ -309,27 +316,25 @@ class PdoAddressBook
return true; return true;
} }
public function Export(string $sEmail, string $sType = 'vcf') : bool public function Export(string $sType = 'vcf') : bool
{ {
$this->SyncDatabase(); $this->SyncDatabase();
$iUserID = $this->getUserId($sEmail); if (1 > $this->iUserID) {
if (0 >= $iUserID)
{
return false; return false;
} }
$bVcf = 'vcf' === $sType; $bVcf = 'vcf' === $sType;
$bCsvHeader = true; $bCsvHeader = true;
$aDatabaseSyncData = $this->prepareDatabaseSyncData($iUserID); $aDatabaseSyncData = $this->prepareDatabaseSyncData($this->iUserID);
if (\count($aDatabaseSyncData)) if (\count($aDatabaseSyncData))
{ {
foreach ($aDatabaseSyncData as $mData) foreach ($aDatabaseSyncData as $mData)
{ {
if ($mData && isset($mData['id_contact'], $mData['deleted']) && !$mData['deleted']) if ($mData && isset($mData['id_contact'], $mData['deleted']) && !$mData['deleted'])
{ {
$oContact = $this->GetContactByID($sEmail, $mData['id_contact']); $oContact = $this->GetContactByID($mData['id_contact']);
if ($oContact) if ($oContact)
{ {
if ($bVcf) if ($bVcf)
@ -349,12 +354,10 @@ class PdoAddressBook
return true; return true;
} }
public function ContactSave(string $sEmail, Classes\Contact $oContact) : bool public function ContactSave(Classes\Contact $oContact) : bool
{ {
$this->SyncDatabase(); $this->SyncDatabase();
$iUserID = $this->getUserId($sEmail);
$iIdContact = \strlen($oContact->IdContact) && \is_numeric($oContact->IdContact) ? (int) $oContact->IdContact : 0; $iIdContact = \strlen($oContact->IdContact) && \is_numeric($oContact->IdContact) ? (int) $oContact->IdContact : 0;
$bUpdate = 0 < $iIdContact; $bUpdate = 0 < $iIdContact;
@ -367,14 +370,14 @@ class PdoAddressBook
$aFreq = array(); $aFreq = array();
if ($bUpdate) if ($bUpdate)
{ {
$aFreq = $this->getContactFreq($iUserID, $iIdContact); $aFreq = $this->getContactFreq($this->iUserID, $iIdContact);
$sSql = 'UPDATE rainloop_ab_contacts SET id_contact_str = :id_contact_str, display = :display, changed = :changed, etag = :etag '. $sSql = 'UPDATE rainloop_ab_contacts SET id_contact_str = :id_contact_str, display = :display, changed = :changed, etag = :etag '.
'WHERE id_user = :id_user AND id_contact = :id_contact'; 'WHERE id_user = :id_user AND id_contact = :id_contact';
$this->prepareAndExecute($sSql, $this->prepareAndExecute($sSql,
array( array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':id_contact' => array($iIdContact, \PDO::PARAM_INT), ':id_contact' => array($iIdContact, \PDO::PARAM_INT),
':id_contact_str' => array($oContact->IdContactStr, \PDO::PARAM_STR), ':id_contact_str' => array($oContact->IdContactStr, \PDO::PARAM_STR),
':display' => array($oContact->Display, \PDO::PARAM_STR), ':display' => array($oContact->Display, \PDO::PARAM_STR),
@ -387,7 +390,7 @@ class PdoAddressBook
$this->prepareAndExecute( $this->prepareAndExecute(
'DELETE FROM rainloop_ab_properties WHERE id_user = :id_user AND id_contact = :id_contact', 'DELETE FROM rainloop_ab_properties WHERE id_user = :id_user AND id_contact = :id_contact',
array( array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':id_contact' => array($iIdContact, \PDO::PARAM_INT) ':id_contact' => array($iIdContact, \PDO::PARAM_INT)
) )
); );
@ -401,7 +404,7 @@ class PdoAddressBook
$this->prepareAndExecute($sSql, $this->prepareAndExecute($sSql,
array( array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':id_contact_str' => array($oContact->IdContactStr, \PDO::PARAM_STR), ':id_contact_str' => array($oContact->IdContactStr, \PDO::PARAM_STR),
':display' => array($oContact->Display, \PDO::PARAM_STR), ':display' => array($oContact->Display, \PDO::PARAM_STR),
':changed' => array($oContact->Changed, \PDO::PARAM_INT), ':changed' => array($oContact->Changed, \PDO::PARAM_INT),
@ -430,7 +433,7 @@ class PdoAddressBook
$aParams[] = array( $aParams[] = array(
':id_contact' => array($iIdContact, \PDO::PARAM_INT), ':id_contact' => array($iIdContact, \PDO::PARAM_INT),
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':prop_type' => array($oProp->Type, \PDO::PARAM_INT), ':prop_type' => array($oProp->Type, \PDO::PARAM_INT),
':prop_type_str' => array($oProp->TypeStr, \PDO::PARAM_STR), ':prop_type_str' => array($oProp->TypeStr, \PDO::PARAM_STR),
':prop_value' => array($oProp->Value, \PDO::PARAM_STR), ':prop_value' => array($oProp->Value, \PDO::PARAM_STR),
@ -459,15 +462,13 @@ class PdoAddressBook
return 0 < $iIdContact; return 0 < $iIdContact;
} }
public function DeleteContacts(string $sEmail, array $aContactIds, bool $bSyncDb = true) : bool public function DeleteContacts(array $aContactIds, bool $bSyncDb = true) : bool
{ {
if ($bSyncDb) if ($bSyncDb)
{ {
$this->SyncDatabase(); $this->SyncDatabase();
} }
$iUserID = $this->getUserId($sEmail);
$aContactIds = \array_filter(\array_map('intval', $aContactIds)); $aContactIds = \array_filter(\array_map('intval', $aContactIds));
if (0 === \count($aContactIds)) if (0 === \count($aContactIds))
@ -476,12 +477,12 @@ class PdoAddressBook
} }
$sIDs = \implode(',', $aContactIds); $sIDs = \implode(',', $aContactIds);
$aParams = array(':id_user' => array($iUserID, \PDO::PARAM_INT)); $aParams = array(':id_user' => array($this->iUserID, \PDO::PARAM_INT));
$this->prepareAndExecute('DELETE FROM rainloop_ab_properties WHERE id_user = :id_user AND id_contact IN ('.$sIDs.')', $aParams); $this->prepareAndExecute('DELETE FROM rainloop_ab_properties WHERE id_user = :id_user AND id_contact IN ('.$sIDs.')', $aParams);
$aParams = array( $aParams = array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':changed' => array(\time(), \PDO::PARAM_INT) ':changed' => array(\time(), \PDO::PARAM_INT)
); );
@ -491,13 +492,11 @@ class PdoAddressBook
return true; return true;
} }
public function DeleteAllContacts(string $sEmail) : bool public function DeleteAllContacts() : bool
{ {
$this->SyncDatabase(); $this->SyncDatabase();
$iUserID = $this->getUserId($sEmail); $aParams = array(':id_user' => array($this->iUserID, \PDO::PARAM_INT));
$aParams = array(':id_user' => array($iUserID, \PDO::PARAM_INT));
$this->prepareAndExecute('DELETE FROM rainloop_ab_properties WHERE id_user = :id_user', $aParams); $this->prepareAndExecute('DELETE FROM rainloop_ab_properties WHERE id_user = :id_user', $aParams);
$this->prepareAndExecute('DELETE FROM rainloop_ab_contacts WHERE id_user = :id_user', $aParams); $this->prepareAndExecute('DELETE FROM rainloop_ab_contacts WHERE id_user = :id_user', $aParams);
@ -505,7 +504,7 @@ class PdoAddressBook
return true; return true;
} }
public function GetContacts(string $sEmail, int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array public function GetContacts(int $iOffset = 0, int $iLimit = 20, string $sSearch = '', int &$iResultCount = 0) : array
{ {
$this->SyncDatabase(); $this->SyncDatabase();
@ -513,8 +512,6 @@ class PdoAddressBook
$iLimit = 0 < $iLimit ? (int) $iLimit : 20; $iLimit = 0 < $iLimit ? (int) $iLimit : 20;
$sSearch = \trim($sSearch); $sSearch = \trim($sSearch);
$iUserID = $this->getUserId($sEmail);
$iCount = 0; $iCount = 0;
$aSearchIds = array(); $aSearchIds = array();
$aPropertyFromSearchIds = array(); $aPropertyFromSearchIds = array();
@ -537,7 +534,7 @@ class PdoAddressBook
') GROUP BY id_contact, id_prop'; ') GROUP BY id_contact, id_prop';
$aParams = array( $aParams = array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':search' => array($this->specialConvertSearchValue($sSearch, '='), \PDO::PARAM_STR) ':search' => array($this->specialConvertSearchValue($sSearch, '='), \PDO::PARAM_STR)
); );
@ -578,7 +575,7 @@ class PdoAddressBook
'WHERE id_user = :id_user'; 'WHERE id_user = :id_user';
$aParams = array( $aParams = array(
':id_user' => array($iUserID, \PDO::PARAM_INT) ':id_user' => array($this->iUserID, \PDO::PARAM_INT)
); );
$oStmt = $this->prepareAndExecute($sSql, $aParams); $oStmt = $this->prepareAndExecute($sSql, $aParams);
@ -600,7 +597,7 @@ class PdoAddressBook
$sSql = 'SELECT * FROM rainloop_ab_contacts WHERE deleted = 0 AND id_user = :id_user'; $sSql = 'SELECT * FROM rainloop_ab_contacts WHERE deleted = 0 AND id_user = :id_user';
$aParams = array( $aParams = array(
':id_user' => array($iUserID, \PDO::PARAM_INT) ':id_user' => array($this->iUserID, \PDO::PARAM_INT)
); );
if (\count($aSearchIds)) if (\count($aSearchIds))
@ -633,7 +630,7 @@ class PdoAddressBook
$oContact->IdContactStr = isset($aItem['id_contact_str']) ? (string) $aItem['id_contact_str'] : ''; $oContact->IdContactStr = isset($aItem['id_contact_str']) ? (string) $aItem['id_contact_str'] : '';
$oContact->Display = isset($aItem['display']) ? (string) $aItem['display'] : ''; $oContact->Display = isset($aItem['display']) ? (string) $aItem['display'] : '';
$oContact->Changed = isset($aItem['changed']) ? (int) $aItem['changed'] : 0; $oContact->Changed = isset($aItem['changed']) ? (int) $aItem['changed'] : 0;
$oContact->ReadOnly = $iUserID !== (isset($aItem['id_user']) ? (int) $aItem['id_user'] : 0); $oContact->ReadOnly = $this->iUserID !== (isset($aItem['id_user']) ? (int) $aItem['id_user'] : 0);
$oContact->IdPropertyFromSearch = isset($aPropertyFromSearchIds[$iIdContact]) && $oContact->IdPropertyFromSearch = isset($aPropertyFromSearchIds[$iIdContact]) &&
0 < $aPropertyFromSearchIds[$iIdContact] ? $aPropertyFromSearchIds[$iIdContact] : 0; 0 < $aPropertyFromSearchIds[$iIdContact] ? $aPropertyFromSearchIds[$iIdContact] : 0;
@ -698,16 +695,14 @@ class PdoAddressBook
/** /**
* @param mixed $mID * @param mixed $mID
*/ */
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?Classes\Contact public function GetContactByID($mID, bool $bIsStrID = false) : ?Classes\Contact
{ {
$mID = \trim($mID); $mID = \trim($mID);
$iUserID = $this->getUserId($sEmail);
$sSql = 'SELECT * FROM rainloop_ab_contacts WHERE deleted = 0 AND id_user = :id_user'; $sSql = 'SELECT * FROM rainloop_ab_contacts WHERE deleted = 0 AND id_user = :id_user';
$aParams = array( $aParams = array(
':id_user' => array($iUserID, \PDO::PARAM_INT) ':id_user' => array($this->iUserID, \PDO::PARAM_INT)
); );
if ($bIsStrID) if ($bIsStrID)
@ -744,7 +739,7 @@ class PdoAddressBook
$oContact->IdContactStr = isset($aItem['id_contact_str']) ? (string) $aItem['id_contact_str'] : ''; $oContact->IdContactStr = isset($aItem['id_contact_str']) ? (string) $aItem['id_contact_str'] : '';
$oContact->Display = isset($aItem['display']) ? (string) $aItem['display'] : ''; $oContact->Display = isset($aItem['display']) ? (string) $aItem['display'] : '';
$oContact->Changed = isset($aItem['changed']) ? (int) $aItem['changed'] : 0; $oContact->Changed = isset($aItem['changed']) ? (int) $aItem['changed'] : 0;
$oContact->ReadOnly = $iUserID !== (isset($aItem['id_user']) ? (int) $aItem['id_user'] : 0); $oContact->ReadOnly = $this->iUserID !== (isset($aItem['id_user']) ? (int) $aItem['id_user'] : 0);
$oContact->Etag = empty($aItem['etag']) ? '' : (string) $aItem['etag']; $oContact->Etag = empty($aItem['etag']) ? '' : (string) $aItem['etag'];
} }
} }
@ -798,7 +793,7 @@ class PdoAddressBook
/** /**
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function GetSuggestions(string $sEmail, string $sSearch, int $iLimit = 20) : array public function GetSuggestions(string $sSearch, int $iLimit = 20) : array
{ {
$sSearch = \trim($sSearch); $sSearch = \trim($sSearch);
if (0 === \strlen($sSearch)) if (0 === \strlen($sSearch))
@ -808,8 +803,6 @@ class PdoAddressBook
$this->SyncDatabase(); $this->SyncDatabase();
$iUserID = $this->getUserId($sEmail);
$sTypes = implode(',', array( $sTypes = implode(',', array(
PropertyType::EMAIl, PropertyType::FIRST_NAME, PropertyType::LAST_NAME, PropertyType::NICK_NAME PropertyType::EMAIl, PropertyType::FIRST_NAME, PropertyType::LAST_NAME, PropertyType::NICK_NAME
)); ));
@ -824,7 +817,7 @@ class PdoAddressBook
; ;
$aParams = array( $aParams = array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':limit' => array($iLimit, \PDO::PARAM_INT), ':limit' => array($iLimit, \PDO::PARAM_INT),
':search' => array($this->specialConvertSearchValue($sSearch, '='), \PDO::PARAM_STR) ':search' => array($this->specialConvertSearchValue($sSearch, '='), \PDO::PARAM_STR)
); );
@ -993,7 +986,7 @@ class PdoAddressBook
return array(); return array();
} }
public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool
{ {
$self = $this; $self = $this;
$aEmailsObjects = \array_map(function ($mItem) { $aEmailsObjects = \array_map(function ($mItem) {
@ -1016,7 +1009,6 @@ class PdoAddressBook
} }
$this->SyncDatabase(); $this->SyncDatabase();
$iUserID = $this->getUserId($sEmail);
$aExists = array(); $aExists = array();
$aEmailsToCreate = array(); $aEmailsToCreate = array();
@ -1026,7 +1018,7 @@ class PdoAddressBook
{ {
$sSql = 'SELECT prop_value FROM rainloop_ab_properties WHERE id_user = :id_user AND prop_type = :prop_type'; $sSql = 'SELECT prop_value FROM rainloop_ab_properties WHERE id_user = :id_user AND prop_type = :prop_type';
$oStmt = $this->prepareAndExecute($sSql, array( $oStmt = $this->prepareAndExecute($sSql, array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':prop_type' => array(PropertyType::EMAIl, \PDO::PARAM_INT) ':prop_type' => array(PropertyType::EMAIl, \PDO::PARAM_INT)
)); ));
@ -1126,7 +1118,7 @@ class PdoAddressBook
if (\count($oContact->Properties)) if (\count($oContact->Properties))
{ {
$this->ContactSave($sEmail, $oContact); $this->ContactSave($oContact);
} }
$oContact->Clear(); $oContact->Clear();
@ -1149,7 +1141,7 @@ class PdoAddressBook
} }
return !!$this->prepareAndExecute($sSql, array( return !!$this->prepareAndExecute($sSql, array(
':id_user' => array($iUserID, \PDO::PARAM_INT), ':id_user' => array($this->iUserID, \PDO::PARAM_INT),
':prop_type' => array(PropertyType::EMAIl, \PDO::PARAM_INT) ':prop_type' => array(PropertyType::EMAIl, \PDO::PARAM_INT)
)); ));
} }