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;
}
public function SetEmail(string $sEmail) : bool
{
return true;
}
/**
* Sync with davClient
*/
public function Sync(array $oConfig) : bool
public function Sync() : bool
{
// TODO
return false;
}
public function Export(string $sEmail, string $sType = 'vcf') : bool
public function Export(string $sType = 'vcf') : bool
{
$bVcf = 'vcf' === $sType;
$bCsvHeader = true;
@ -161,11 +166,8 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return true;
}
public function ContactSave(string $sEmail, Contact $oContact) : bool
public function ContactSave(Contact $oContact) : bool
{
// TODO
// $emails = $oContact->GetEmails();
if (!$this->SelectFolder()) {
return false;
}
@ -233,13 +235,26 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return true;
}
public function DeleteContacts(string $sEmail, array $aContactIds) : bool
public function DeleteContacts(array $aContactIds) : bool
{
try {
$this->MailClient()->MessageDelete(
$this->sFolderName,
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;
} catch (\Throwable $e) {
}
@ -254,7 +269,7 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
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)) {
// return [];
@ -292,7 +307,7 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return $aResult;
}
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?Contact
public function GetContactByID($mID, bool $bIsStrID = false) : ?Contact
{
if ($bIsStrID) {
$oMessage = null;
@ -302,7 +317,7 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
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);
if (2 > \strlen($sSearch) || !$this->SelectFolder()) {
@ -327,26 +342,14 @@ class KolabAddressBook implements \RainLoop\Providers\AddressBook\AddressBookInt
return $aResult;
}
public function IncFrec(string $sEmail, array $aEmails, bool $bCreateAuto = true) : bool
public function IncFrec(array $aEmails, bool $bCreateAuto = true) : bool
{
return false;
}
public function Test() : string
{
$sResult = '';
try
{
// $sResult = 'Unknown error';
}
catch (\Throwable $oException)
{
$sResult = $oException->getMessage();
if (!\is_string($sResult) || empty($sResult)) {
$sResult = 'Unknown error';
}
}
return $sResult;
// Nothing to test
return '';
}
}

View file

@ -501,7 +501,10 @@ class Actions
if ($bForceEnable || $this->GetCapa(Enumerations\Capa::CONTACTS)) {
$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->SetLogger($this->oLogger);
}

View file

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

View file

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

View file

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

View file

@ -27,24 +27,24 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
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
@ -52,28 +52,27 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
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,
$iOffset, $iLimit, $sSearch, $iResultCount) : array();
return $this->IsActive() ? $this->oDriver->GetContacts($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
*/
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
@ -156,7 +155,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
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;
if ($this->IsActive() && \count($aCsvData))
@ -190,7 +189,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
if ($oContact && \count($oContact->Properties))
{
if ($this->ContactSave($sEmail, $oContact))
if ($this->ContactSave($oContact))
{
$iCount++;
}
@ -205,7 +204,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
return $iCount;
}
public function ImportVcfFile(string $sEmail, string $sVcfData) : int
public function ImportVcfFile(string $sVcfData) : int
{
$iCount = 0;
@ -243,7 +242,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
if (\count($oContact->Properties))
{
if ($this->ContactSave($sEmail, $oContact))
if ($this->ContactSave($oContact))
{
$iCount++;
}

View file

@ -6,23 +6,25 @@ interface AddressBookInterface
{
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 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;
}

View file

@ -6,6 +6,22 @@ use SnappyMail\DAV\Client as DAVClient;
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)
{
$mResult = false;
@ -401,8 +417,16 @@ trait CardDAV
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();
$sUserAddressBookNameName = '';

View file

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