mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-11-11 01:23:43 +08:00
Resolved and improved #79
This commit is contained in:
parent
d528a523ad
commit
30727b0e29
4 changed files with 127 additions and 142 deletions
|
@ -253,12 +253,7 @@ class AddressBook extends \RainLoop\Providers\AbstractProvider
|
|||
{
|
||||
\MailSo\Base\Utils::ResetTimeLimit();
|
||||
|
||||
if (empty($oVCard->UID))
|
||||
{
|
||||
$oVCard->UID = \SnappyMail\UUID::generate();
|
||||
}
|
||||
|
||||
$oContact->PopulateByVCard($oVCard->UID, $oVCard->serialize());
|
||||
$oContact->PopulateByVCard($oVCard);
|
||||
|
||||
if (0 < \count($oContact->Properties))
|
||||
{
|
||||
|
|
|
@ -155,17 +155,12 @@ class Contact implements \JsonSerializable
|
|||
{
|
||||
if (empty($this->IdContactStr))
|
||||
{
|
||||
$this->RegenerateContactStr();
|
||||
$this->IdContactStr = \SnappyMail\UUID::generate();
|
||||
}
|
||||
|
||||
$this->PopulateDisplayAndFullNameValue();
|
||||
}
|
||||
|
||||
public function RegenerateContactStr() : void
|
||||
{
|
||||
$this->IdContactStr = \SnappyMail\UUID::generate();
|
||||
}
|
||||
|
||||
public function GetEmails() : array
|
||||
{
|
||||
$aResult = array();
|
||||
|
@ -495,13 +490,8 @@ class Contact implements \JsonSerializable
|
|||
}
|
||||
}
|
||||
|
||||
public function PopulateByVCard(string $sUid, string $sVCard, string $sEtag = '', $oLogger = null) : bool
|
||||
public function PopulateByVCard(\Sabre\VObject\Component\VCard $oVCard, string $sEtag = '') : bool
|
||||
{
|
||||
if ("\xef\xbb\xbf" === \substr($sVCard, 0, 3))
|
||||
{
|
||||
$sVCard = \substr($sVCard, 3);
|
||||
}
|
||||
|
||||
$this->Properties = array();
|
||||
|
||||
if (!empty($sEtag))
|
||||
|
@ -509,118 +499,90 @@ class Contact implements \JsonSerializable
|
|||
$this->Etag = $sEtag;
|
||||
}
|
||||
|
||||
$this->IdContactStr = $sUid;
|
||||
|
||||
try
|
||||
{
|
||||
$oVCard = \Sabre\VObject\Reader::read($sVCard);
|
||||
}
|
||||
catch (\Throwable $oExc)
|
||||
{
|
||||
if ($oLogger)
|
||||
{
|
||||
$oLogger->WriteException($oExc);
|
||||
$oLogger->WriteDump($sVCard);
|
||||
}
|
||||
}
|
||||
|
||||
// if ($oLogger)
|
||||
// {
|
||||
// $oLogger->WriteDump($sVCard);
|
||||
// }
|
||||
|
||||
$aProperties = array();
|
||||
|
||||
if ($oVCard)
|
||||
$bOldVersion = empty($oVCard->VERSION) ? false :
|
||||
\in_array((string) $oVCard->VERSION, array('2.1', '2.0', '1.0'));
|
||||
|
||||
if (isset($oVCard->FN) && '' !== \trim($oVCard->FN))
|
||||
{
|
||||
$bOldVersion = empty($oVCard->VERSION) ? false :
|
||||
\in_array((string) $oVCard->VERSION, array('2.1', '2.0', '1.0'));
|
||||
$sValue = $this->getPropertyValueHelper($oVCard->FN, $bOldVersion);
|
||||
$aProperties[] = new Property(PropertyType::FULLNAME, $sValue);
|
||||
}
|
||||
|
||||
if (isset($oVCard->FN) && '' !== \trim($oVCard->FN))
|
||||
{
|
||||
$sValue = $this->getPropertyValueHelper($oVCard->FN, $bOldVersion);
|
||||
$aProperties[] = new Property(PropertyType::FULLNAME, $sValue);
|
||||
}
|
||||
if (isset($oVCard->NICKNAME) && '' !== \trim($oVCard->NICKNAME))
|
||||
{
|
||||
$sValue = $sValue = $this->getPropertyValueHelper($oVCard->NICKNAME, $bOldVersion);
|
||||
$aProperties[] = new Property(PropertyType::NICK_NAME, $sValue);
|
||||
}
|
||||
|
||||
if (isset($oVCard->NICKNAME) && '' !== \trim($oVCard->NICKNAME))
|
||||
{
|
||||
$sValue = $sValue = $this->getPropertyValueHelper($oVCard->NICKNAME, $bOldVersion);
|
||||
$aProperties[] = new Property(PropertyType::NICK_NAME, $sValue);
|
||||
}
|
||||
if (isset($oVCard->NOTE) && '' !== \trim($oVCard->NOTE))
|
||||
{
|
||||
$sValue = $this->getPropertyValueHelper($oVCard->NOTE, $bOldVersion);
|
||||
$aProperties[] = new Property(PropertyType::NOTE, $sValue);
|
||||
}
|
||||
|
||||
if (isset($oVCard->NOTE) && '' !== \trim($oVCard->NOTE))
|
||||
if (isset($oVCard->N))
|
||||
{
|
||||
$aNames = $oVCard->N->getParts();
|
||||
foreach ($aNames as $iIndex => $sValue)
|
||||
{
|
||||
$sValue = $this->getPropertyValueHelper($oVCard->NOTE, $bOldVersion);
|
||||
$aProperties[] = new Property(PropertyType::NOTE, $sValue);
|
||||
}
|
||||
|
||||
if (isset($oVCard->N))
|
||||
{
|
||||
$aNames = $oVCard->N->getParts();
|
||||
foreach ($aNames as $iIndex => $sValue)
|
||||
$sValue = \trim($sValue);
|
||||
if ($bOldVersion && !isset($oVCard->N->parameters['CHARSET']))
|
||||
{
|
||||
$sValue = \trim($sValue);
|
||||
if ($bOldVersion && !isset($oVCard->N->parameters['CHARSET']))
|
||||
if (0 < \strlen($sValue))
|
||||
{
|
||||
if (0 < \strlen($sValue))
|
||||
$sEncValue = \utf8_encode($sValue);
|
||||
if (0 === \strlen($sEncValue))
|
||||
{
|
||||
$sEncValue = \utf8_encode($sValue);
|
||||
if (0 === \strlen($sEncValue))
|
||||
{
|
||||
$sEncValue = $sValue;
|
||||
}
|
||||
|
||||
$sValue = $sEncValue;
|
||||
$sEncValue = $sValue;
|
||||
}
|
||||
}
|
||||
|
||||
$sValue = \MailSo\Base\Utils::Utf8Clear($sValue);
|
||||
switch ($iIndex) {
|
||||
case 0:
|
||||
$aProperties[] = new Property(PropertyType::LAST_NAME, $sValue);
|
||||
break;
|
||||
case 1:
|
||||
$aProperties[] = new Property(PropertyType::FIRST_NAME, $sValue);
|
||||
break;
|
||||
case 2:
|
||||
$aProperties[] = new Property(PropertyType::MIDDLE_NAME, $sValue);
|
||||
break;
|
||||
case 3:
|
||||
$aProperties[] = new Property(PropertyType::NAME_PREFIX, $sValue);
|
||||
break;
|
||||
case 4:
|
||||
$aProperties[] = new Property(PropertyType::NAME_SUFFIX, $sValue);
|
||||
break;
|
||||
$sValue = $sEncValue;
|
||||
}
|
||||
}
|
||||
|
||||
$sValue = \MailSo\Base\Utils::Utf8Clear($sValue);
|
||||
switch ($iIndex) {
|
||||
case 0:
|
||||
$aProperties[] = new Property(PropertyType::LAST_NAME, $sValue);
|
||||
break;
|
||||
case 1:
|
||||
$aProperties[] = new Property(PropertyType::FIRST_NAME, $sValue);
|
||||
break;
|
||||
case 2:
|
||||
$aProperties[] = new Property(PropertyType::MIDDLE_NAME, $sValue);
|
||||
break;
|
||||
case 3:
|
||||
$aProperties[] = new Property(PropertyType::NAME_PREFIX, $sValue);
|
||||
break;
|
||||
case 4:
|
||||
$aProperties[] = new Property(PropertyType::NAME_SUFFIX, $sValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($oVCard->EMAIL))
|
||||
{
|
||||
$this->addArrayPropertyHelper($aProperties, $oVCard->EMAIL, PropertyType::EMAIl);
|
||||
}
|
||||
|
||||
if (isset($oVCard->URL))
|
||||
{
|
||||
$this->addArrayPropertyHelper($aProperties, $oVCard->URL, PropertyType::WEB_PAGE);
|
||||
}
|
||||
|
||||
if (isset($oVCard->TEL))
|
||||
{
|
||||
$this->addArrayPropertyHelper($aProperties, $oVCard->TEL, PropertyType::PHONE);
|
||||
}
|
||||
|
||||
$sUidValue = $oVCard->UID ? (string) $oVCard->UID : \SnappyMail\UUID::generate();
|
||||
$aProperties[] = new Property(PropertyType::UID, $sUidValue);
|
||||
|
||||
if (empty($this->IdContactStr))
|
||||
{
|
||||
$this->IdContactStr = $sUidValue;
|
||||
}
|
||||
|
||||
$this->Properties = $aProperties;
|
||||
}
|
||||
|
||||
if (isset($oVCard->EMAIL))
|
||||
{
|
||||
$this->addArrayPropertyHelper($aProperties, $oVCard->EMAIL, PropertyType::EMAIl);
|
||||
}
|
||||
|
||||
if (isset($oVCard->URL))
|
||||
{
|
||||
$this->addArrayPropertyHelper($aProperties, $oVCard->URL, PropertyType::WEB_PAGE);
|
||||
}
|
||||
|
||||
if (isset($oVCard->TEL))
|
||||
{
|
||||
$this->addArrayPropertyHelper($aProperties, $oVCard->TEL, PropertyType::PHONE);
|
||||
}
|
||||
|
||||
$this->IdContactStr = $oVCard->UID ? (string) $oVCard->UID : \SnappyMail\UUID::generate();
|
||||
$aProperties[] = new Property(PropertyType::UID, $this->IdContactStr);
|
||||
|
||||
$this->Properties = $aProperties;
|
||||
|
||||
$this->UpdateDependentValues();
|
||||
|
||||
return true;
|
||||
|
|
|
@ -229,25 +229,53 @@ class PdoAddressBook
|
|||
if ($oResponse)
|
||||
{
|
||||
$sBody = \trim($oResponse->body);
|
||||
|
||||
// Remove UTF-8 BOM
|
||||
if ("\xef\xbb\xbf" === \substr($sBody, 0, 3))
|
||||
{
|
||||
$sBody = \substr($sBody, 3);
|
||||
}
|
||||
|
||||
if (!empty($sBody))
|
||||
{
|
||||
$oContact = null;
|
||||
if ($mExsistenContactID)
|
||||
$oVCard = null;
|
||||
try
|
||||
{
|
||||
$oContact = $this->GetContactByID($sEmail, $mExsistenContactID);
|
||||
$oVCard = \Sabre\VObject\Reader::read($sBody);
|
||||
}
|
||||
catch (\Throwable $oExc)
|
||||
{
|
||||
if ($this->oLogger)
|
||||
{
|
||||
$this->oLogger->WriteException($oExc);
|
||||
$this->oLogger->WriteDump($sBody);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$oContact)
|
||||
if ($oVCard instanceof \Sabre\VObject\Component\VCard)
|
||||
{
|
||||
$oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
|
||||
$oVCard->UID = $aData['uid'];
|
||||
|
||||
$oContact = null;
|
||||
if ($mExsistenContactID)
|
||||
{
|
||||
$oContact = $this->GetContactByID($sEmail, $mExsistenContactID);
|
||||
}
|
||||
if (!$oContact)
|
||||
{
|
||||
$oContact = new Classes\Contact();
|
||||
}
|
||||
|
||||
$oContact->PopulateByVCard(
|
||||
$oVCard,
|
||||
\trim(\trim($oResponse->getHeader('etag')), '"\'')
|
||||
);
|
||||
|
||||
$this->ContactSave($sEmail, $oContact);
|
||||
unset($oContact);
|
||||
// } else if ($this->oLogger) {
|
||||
// $this->oLogger->WriteDump($sBody);
|
||||
}
|
||||
|
||||
$oContact->PopulateByVCard($aData['uid'], $sBody,
|
||||
\trim(\trim($oResponse->getHeader('etag')), '"\''),
|
||||
$this->oLogger);
|
||||
|
||||
$this->ContactSave($sEmail, $oContact);
|
||||
unset($oContact);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -296,7 +324,7 @@ class PdoAddressBook
|
|||
return true;
|
||||
}
|
||||
|
||||
public function ContactSave(string $sEmail, \RainLoop\Providers\AddressBook\Classes\Contact $oContact, bool $bSyncDb = true) : bool
|
||||
public function ContactSave(string $sEmail, Classes\Contact $oContact, bool $bSyncDb = true) : bool
|
||||
{
|
||||
if ($bSyncDb)
|
||||
{
|
||||
|
@ -370,7 +398,7 @@ class PdoAddressBook
|
|||
if (0 < $iIdContact)
|
||||
{
|
||||
$aParams = array();
|
||||
foreach ($oContact->Properties as /* @var $oProp \RainLoop\Providers\AddressBook\Classes\Property */ $oProp)
|
||||
foreach ($oContact->Properties as /* @var $oProp Classes\Property */ $oProp)
|
||||
{
|
||||
$iFreq = $oProp->Frec;
|
||||
if ($oProp->IsEmail() && isset($aFreq[$oProp->Value]))
|
||||
|
@ -580,7 +608,7 @@ class PdoAddressBook
|
|||
if (0 < $iIdContact)
|
||||
{
|
||||
$aIdContacts[] = $iIdContact;
|
||||
$oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
|
||||
$oContact = new Classes\Contact();
|
||||
|
||||
$oContact->IdContact = (string) $iIdContact;
|
||||
$oContact->IdContactStr = isset($aItem['id_contact_str']) ? (string) $aItem['id_contact_str'] : '';
|
||||
|
@ -617,7 +645,7 @@ class PdoAddressBook
|
|||
$iId = (int) $aItem['id_contact'];
|
||||
if (0 < $iId && isset($aContacts[$iId]))
|
||||
{
|
||||
$oProperty = new \RainLoop\Providers\AddressBook\Classes\Property();
|
||||
$oProperty = new Classes\Property();
|
||||
$oProperty->IdProperty = (int) $aItem['id_prop'];
|
||||
$oProperty->Type = (int) $aItem['prop_type'];
|
||||
$oProperty->TypeStr = isset($aItem['prop_type_str']) ? (string) $aItem['prop_type_str'] : '';
|
||||
|
@ -651,7 +679,7 @@ class PdoAddressBook
|
|||
/**
|
||||
* @param mixed $mID
|
||||
*/
|
||||
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?\RainLoop\Providers\AddressBook\Classes\Contact
|
||||
public function GetContactByID(string $sEmail, $mID, bool $bIsStrID = false) : ?Classes\Contact
|
||||
{
|
||||
$mID = \trim($mID);
|
||||
|
||||
|
@ -691,7 +719,7 @@ class PdoAddressBook
|
|||
$iIdContact = $aItem && isset($aItem['id_contact']) ? (int) $aItem['id_contact'] : 0;
|
||||
if (0 < $iIdContact)
|
||||
{
|
||||
$oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
|
||||
$oContact = new Classes\Contact();
|
||||
|
||||
$oContact->IdContact = (string) $iIdContact;
|
||||
$oContact->IdContactStr = isset($aItem['id_contact_str']) ? (string) $aItem['id_contact_str'] : '';
|
||||
|
@ -723,7 +751,7 @@ class PdoAddressBook
|
|||
{
|
||||
if ((string) $oContact->IdContact === (string) $aItem['id_contact'])
|
||||
{
|
||||
$oProperty = new \RainLoop\Providers\AddressBook\Classes\Property();
|
||||
$oProperty = new Classes\Property();
|
||||
$oProperty->IdProperty = (int) $aItem['id_prop'];
|
||||
$oProperty->Type = (int) $aItem['prop_type'];
|
||||
$oProperty->TypeStr = isset($aItem['prop_type_str']) ? (string) $aItem['prop_type_str'] : '';
|
||||
|
@ -1031,13 +1059,13 @@ class PdoAddressBook
|
|||
|
||||
if (0 < \count($aEmailsToCreate))
|
||||
{
|
||||
$oContact = new \RainLoop\Providers\AddressBook\Classes\Contact();
|
||||
$oContact = new Classes\Contact();
|
||||
foreach ($aEmailsToCreate as $oEmail)
|
||||
{
|
||||
if ('' !== \trim($oEmail->GetEmail()))
|
||||
{
|
||||
$oPropEmail = new \RainLoop\Providers\AddressBook\Classes\Property();
|
||||
$oPropEmail->Type = \RainLoop\Providers\AddressBook\Enumerations\PropertyType::EMAIl;
|
||||
$oPropEmail = new Classes\Property();
|
||||
$oPropEmail->Type = Enumerations\PropertyType::EMAIl;
|
||||
$oPropEmail->Value = \trim($oEmail->GetEmail(true));
|
||||
|
||||
$oContact->Properties[] = $oPropEmail;
|
||||
|
@ -1060,8 +1088,8 @@ class PdoAddressBook
|
|||
|
||||
if (0 < \strlen($sFirst))
|
||||
{
|
||||
$oPropName = new \RainLoop\Providers\AddressBook\Classes\Property();
|
||||
$oPropName->Type = \RainLoop\Providers\AddressBook\Enumerations\PropertyType::FIRST_NAME;
|
||||
$oPropName = new Classes\Property();
|
||||
$oPropName->Type = Enumerations\PropertyType::FIRST_NAME;
|
||||
$oPropName->Value = \trim($sFirst);
|
||||
|
||||
$oContact->Properties[] = $oPropName;
|
||||
|
@ -1069,8 +1097,8 @@ class PdoAddressBook
|
|||
|
||||
if (0 < \strlen($sLast))
|
||||
{
|
||||
$oPropName = new \RainLoop\Providers\AddressBook\Classes\Property();
|
||||
$oPropName->Type = \RainLoop\Providers\AddressBook\Enumerations\PropertyType::LAST_NAME;
|
||||
$oPropName = new Classes\Property();
|
||||
$oPropName->Type = Enumerations\PropertyType::LAST_NAME;
|
||||
$oPropName->Value = \trim($sLast);
|
||||
|
||||
$oContact->Properties[] = $oPropName;
|
||||
|
|
|
@ -69,7 +69,7 @@ class Client
|
|||
$url = $this->baseUri . $url;
|
||||
}
|
||||
}
|
||||
$response = $this->HTTP->doRequest('PROPFIND', $url, $body, $headers);
|
||||
$response = $this->HTTP->doRequest($method, $url, $body, $headers);
|
||||
if (301 == $response->status) {
|
||||
// Like: RewriteRule ^\.well-known/carddav /nextcloud/remote.php/dav [R=301,L]
|
||||
$location = $response->getRedirectLocation();
|
||||
|
@ -77,10 +77,10 @@ class Client
|
|||
$url = \preg_replace('@^(https?:)?//[^/]+[/$]@', '/', $location);
|
||||
$parts = \parse_url($this->baseUri);
|
||||
$url = $parts['scheme'] . '://' . $parts['host'] . (isset($parts['port'])?':' . $parts['port']:'') . $url;
|
||||
$response = $this->HTTP->doRequest('PROPFIND', $url, $body, $headers);
|
||||
$response = $this->HTTP->doRequest($method, $url, $body, $headers);
|
||||
}
|
||||
if (300 <= $response->status) {
|
||||
throw new \SnappyMail\HTTP\Exception("PROPFIND {$url}", $response->status, $response);
|
||||
throw new \SnappyMail\HTTP\Exception("{$method} {$url}", $response->status, $response);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
|
Loading…
Reference in a new issue