mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-08 00:47:04 +03:00
Fix VCard (v2.1) parsing (#44)
This commit is contained in:
parent
3d2e7968d4
commit
46917754a4
3 changed files with 101 additions and 57 deletions
|
|
@ -5131,8 +5131,6 @@ class Actions
|
||||||
*/
|
*/
|
||||||
private function importContactsFromCsvFile($oAccount, $rFile, $sFileStart)
|
private function importContactsFromCsvFile($oAccount, $rFile, $sFileStart)
|
||||||
{
|
{
|
||||||
$this->Logger()->Write('Import Csv');
|
|
||||||
|
|
||||||
$iCount = 0;
|
$iCount = 0;
|
||||||
$aHeaders = null;
|
$aHeaders = null;
|
||||||
$aData = array();
|
$aData = array();
|
||||||
|
|
@ -5140,7 +5138,7 @@ class Actions
|
||||||
if ($oAccount && \is_resource($rFile))
|
if ($oAccount && \is_resource($rFile))
|
||||||
{
|
{
|
||||||
$oPab = $this->PersonalAddressBookProvider($oAccount);
|
$oPab = $this->PersonalAddressBookProvider($oAccount);
|
||||||
if ($oPab)
|
if ($oPab && $oPab->IsActive())
|
||||||
{
|
{
|
||||||
$sDelimiter = ((int) \strpos($sFileStart, ',') > (int) \strpos($sFileStart, ';')) ? ',' : ';';
|
$sDelimiter = ((int) \strpos($sFileStart, ',') > (int) \strpos($sFileStart, ';')) ? ',' : ';';
|
||||||
|
|
||||||
|
|
@ -5171,10 +5169,8 @@ class Actions
|
||||||
|
|
||||||
if (\is_array($aData) && 0 < \count($aData))
|
if (\is_array($aData) && 0 < \count($aData))
|
||||||
{
|
{
|
||||||
$this->Logger()->Write('Start to import '.\count($aData).' contacts from csv file');
|
$this->Logger()->Write('Import contacts from csv');
|
||||||
$oPab->ImportCsvArray($oAccount->ParentEmailHelper(), $aData);
|
$iCount = $oPab->ImportCsvArray($oAccount->ParentEmailHelper(), $aData);
|
||||||
|
|
||||||
$iCount = \count($aData);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5192,13 +5188,11 @@ class Actions
|
||||||
*/
|
*/
|
||||||
private function importContactsFromVcfFile($oAccount, $rFile, $sFileStart)
|
private function importContactsFromVcfFile($oAccount, $rFile, $sFileStart)
|
||||||
{
|
{
|
||||||
$this->Logger()->Write('Import Vcf');
|
|
||||||
|
|
||||||
$iCount = 0;
|
$iCount = 0;
|
||||||
if ($oAccount && \is_resource($rFile))
|
if ($oAccount && \is_resource($rFile))
|
||||||
{
|
{
|
||||||
$oPab = $this->PersonalAddressBookProvider($oAccount);
|
$oPab = $this->PersonalAddressBookProvider($oAccount);
|
||||||
if ($oPab)
|
if ($oPab && $oPab->IsActive())
|
||||||
{
|
{
|
||||||
$sFile = \stream_get_contents($rFile);
|
$sFile = \stream_get_contents($rFile);
|
||||||
if (\is_resource($rFile))
|
if (\is_resource($rFile))
|
||||||
|
|
@ -5208,49 +5202,8 @@ class Actions
|
||||||
|
|
||||||
if (is_string($sFile) && 5 < \strlen($sFile))
|
if (is_string($sFile) && 5 < \strlen($sFile))
|
||||||
{
|
{
|
||||||
$sFile = \trim($sFile);
|
$this->Logger()->Write('Import contacts from vcf');
|
||||||
if ("\xef\xbb\xbf" === \substr($sFile, 0, 3))
|
$iCount = $oPab->ImportVcfFile($oAccount->ParentEmailHelper(), $sFile);
|
||||||
{
|
|
||||||
$sFile = \substr($sFile, 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
$oVCard = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
$oVCardSplitter = new \Sabre\VObject\Splitter\VCard($sFile);
|
|
||||||
}
|
|
||||||
catch (\Exception $oExc)
|
|
||||||
{
|
|
||||||
$this->Logger()->WriteException($oExc);
|
|
||||||
};
|
|
||||||
|
|
||||||
if ($oVCardSplitter)
|
|
||||||
{
|
|
||||||
$oContact = new \RainLoop\Providers\PersonalAddressBook\Classes\Contact();
|
|
||||||
|
|
||||||
$oVCard = null;
|
|
||||||
$sEmail = $oAccount->ParentEmailHelper();
|
|
||||||
|
|
||||||
$this->Logger()->Write('Start to import contacts from vcf');
|
|
||||||
while ($oVCard = $oVCardSplitter->getNext())
|
|
||||||
{
|
|
||||||
if ($oVCard instanceof \Sabre\VObject\Component\VCard)
|
|
||||||
{
|
|
||||||
if (empty($oVCard->UID))
|
|
||||||
{
|
|
||||||
$oVCard->UID = \Sabre\DAV\UUIDUtil::getUUID();
|
|
||||||
}
|
|
||||||
|
|
||||||
$oContact->ParseVCard($oVCard, $oVCard->serialize());
|
|
||||||
if ($oPab->ContactSave($sEmail, $oContact))
|
|
||||||
{
|
|
||||||
$iCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
$oContact->Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -278,4 +278,64 @@ class PersonalAddressBook extends \RainLoop\Providers\AbstractProvider
|
||||||
|
|
||||||
return $iCount;
|
return $iCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sEmail
|
||||||
|
* @param string $sVcfData
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function ImportVcfFile($sEmail, $sVcfData)
|
||||||
|
{
|
||||||
|
$iCount = 0;
|
||||||
|
if ($this->IsActive() && \is_string($sVcfData))
|
||||||
|
{
|
||||||
|
$sVcfData = \trim($sVcfData);
|
||||||
|
if ("\xef\xbb\xbf" === \substr($sVcfData, 0, 3))
|
||||||
|
{
|
||||||
|
$sVcfData = \substr($sVcfData, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
$oVCardSplitter = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
$oVCardSplitter = new \Sabre\VObject\Splitter\VCard($sVcfData);
|
||||||
|
}
|
||||||
|
catch (\Exception $oExc)
|
||||||
|
{
|
||||||
|
$this->Logger()->WriteException($oExc);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($oVCardSplitter)
|
||||||
|
{
|
||||||
|
$oContact = new \RainLoop\Providers\PersonalAddressBook\Classes\Contact();
|
||||||
|
|
||||||
|
$oVCard = null;
|
||||||
|
|
||||||
|
while ($oVCard = $oVCardSplitter->getNext())
|
||||||
|
{
|
||||||
|
if ($oVCard instanceof \Sabre\VObject\Component\VCard)
|
||||||
|
{
|
||||||
|
if (empty($oVCard->UID))
|
||||||
|
{
|
||||||
|
$oVCard->UID = \Sabre\DAV\UUIDUtil::getUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
$oContact->ParseVCard($oVCard, $oVCard->serialize());
|
||||||
|
if (0 < \count($oContact->Properties))
|
||||||
|
{
|
||||||
|
if ($this->ContactSave($sEmail, $oContact))
|
||||||
|
{
|
||||||
|
$iCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$oContact->Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $iCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -174,6 +174,7 @@ class Contact
|
||||||
{
|
{
|
||||||
$oVCard = $this->ToVCardObject($this->CardDavData);
|
$oVCard = $this->ToVCardObject($this->CardDavData);
|
||||||
$this->CardDavData = $oVCard ? $oVCard->serialize() : $this->CardDavData;
|
$this->CardDavData = $oVCard ? $oVCard->serialize() : $this->CardDavData;
|
||||||
|
unset($oVCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($this->CardDavData))
|
if (!empty($this->CardDavData))
|
||||||
|
|
@ -221,21 +222,45 @@ class Contact
|
||||||
$aProperties = array();
|
$aProperties = array();
|
||||||
if ($oVCard && $oVCard->UID)
|
if ($oVCard && $oVCard->UID)
|
||||||
{
|
{
|
||||||
|
$bOldVersion = empty($oVCard->VERSION) ? false :
|
||||||
|
\in_array((string) $oVCard->VERSION, array('2.1', '2.0', '1.0'));
|
||||||
|
|
||||||
$this->IdContactStr = (string) $oVCard->UID;
|
$this->IdContactStr = (string) $oVCard->UID;
|
||||||
|
|
||||||
if (isset($oVCard->FN) && '' !== \trim($oVCard->FN))
|
if (isset($oVCard->FN) && '' !== \trim($oVCard->FN))
|
||||||
{
|
{
|
||||||
$aProperties[] = new Property(PropertyType::FULLNAME, \trim($oVCard->FN));
|
$sValue = \trim($oVCard->FN);
|
||||||
|
if ($bOldVersion && !isset($oVCard->FN->parameters['CHARSET']))
|
||||||
|
{
|
||||||
|
$sValue = \utf8_encode($sValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sValue = \MailSo\Base\Utils::Utf8Clear($sValue);
|
||||||
|
$aProperties[] = new Property(PropertyType::FULLNAME, $sValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($oVCard->NICKNAME) && '' !== \trim($oVCard->NICKNAME))
|
if (isset($oVCard->NICKNAME) && '' !== \trim($oVCard->NICKNAME))
|
||||||
{
|
{
|
||||||
$aProperties[] = new Property(PropertyType::NICK_NAME, \trim($oVCard->NICKNAME));
|
$sValue = \trim($oVCard->NICKNAME);
|
||||||
|
if ($bOldVersion && !isset($oVCard->NICKNAME->parameters['CHARSET']))
|
||||||
|
{
|
||||||
|
$sValue = \utf8_encode($sValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sValue = \MailSo\Base\Utils::Utf8Clear($sValue);
|
||||||
|
$aProperties[] = new Property(PropertyType::NICK_NAME, $sValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (isset($oVCard->NOTE) && '' !== \trim($oVCard->NOTE))
|
// if (isset($oVCard->NOTE) && '' !== \trim($oVCard->NOTE))
|
||||||
// {
|
// {
|
||||||
// $aProperties[] = new Property(PropertyType::NOTE, \trim($oVCard->NOTE));
|
// $sValue = \trim($oVCard->NOTE);
|
||||||
|
// if ($bOldVersion)
|
||||||
|
// {
|
||||||
|
// $sValue = \utf8_encode($sValue);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $sValue = \MailSo\Base\Utils::Utf8Clear($sValue);
|
||||||
|
// $aProperties[] = new Property(PropertyType::NOTE, $sValue);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (isset($oVCard->N))
|
if (isset($oVCard->N))
|
||||||
|
|
@ -244,6 +269,12 @@ class Contact
|
||||||
foreach ($aNames as $iIndex => $sValue)
|
foreach ($aNames as $iIndex => $sValue)
|
||||||
{
|
{
|
||||||
$sValue = \trim($sValue);
|
$sValue = \trim($sValue);
|
||||||
|
if ($bOldVersion && !isset($oVCard->N->parameters['CHARSET']))
|
||||||
|
{
|
||||||
|
$sValue = \utf8_encode($sValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sValue = \MailSo\Base\Utils::Utf8Clear($sValue);
|
||||||
switch ($iIndex) {
|
switch ($iIndex) {
|
||||||
case 0:
|
case 0:
|
||||||
$aProperties[] = new Property(PropertyType::LAST_NAME, $sValue);
|
$aProperties[] = new Property(PropertyType::LAST_NAME, $sValue);
|
||||||
|
|
@ -353,7 +384,7 @@ class Contact
|
||||||
|
|
||||||
$this->Properties = $aProperties;
|
$this->Properties = $aProperties;
|
||||||
|
|
||||||
$this->CardDavData = $sVCard;
|
$this->CardDavData = \MailSo\Base\Utils::Utf8Clear($sVCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->UpdateDependentValues(false);
|
$this->UpdateDependentValues(false);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue