This commit is contained in:
the-djmaze 2023-02-09 09:29:19 +01:00
parent 58e1c2e535
commit 4145c87e55
3 changed files with 82 additions and 102 deletions

View file

@ -133,9 +133,8 @@ trait Contacts
\ini_set('auto_detect_line_endings', '1'); \ini_set('auto_detect_line_endings', '1');
$mData = $this->FilesProvider()->GetFile($oAccount, $sSavedName); $mData = $this->FilesProvider()->GetFile($oAccount, $sSavedName);
if ($mData) { if ($mData) {
$sFileStart = \fread($mData, 20); $sFileStart = \fread($mData, 128);
\rewind($mData); \rewind($mData);
if (false !== $sFileStart) { if (false !== $sFileStart) {
$sFileStart = \trim($sFileStart); $sFileStart = \trim($sFileStart);
if (false !== \strpos($sFileStart, 'BEGIN:VCARD')) { if (false !== \strpos($sFileStart, 'BEGIN:VCARD')) {
@ -254,65 +253,30 @@ trait Contacts
private function importContactsFromVcfFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile): int private function importContactsFromVcfFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile): int
{ {
$iCount = 0;
if ($oAccount && \is_resource($rFile)) {
$oAddressBookProvider = $this->AddressBookProvider($oAccount); $oAddressBookProvider = $this->AddressBookProvider($oAccount);
if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) { if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
$sFile = \stream_get_contents($rFile); $sFile = \stream_get_contents($rFile);
if (\is_resource($rFile)) { if (\is_string($sFile) && 5 < \strlen($sFile)) {
\fclose($rFile);
}
if (is_string($sFile) && 5 < \strlen($sFile)) {
$this->Logger()->Write('Import contacts from vcf'); $this->Logger()->Write('Import contacts from vcf');
$iCount = $oAddressBookProvider->ImportVcfFile($sFile); return $oAddressBookProvider->ImportVcfFile($sFile);
} }
} }
} return 0;
return $iCount;
} }
private function importContactsFromCsvFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile, string $sFileStart): int private function importContactsFromCsvFile(\RainLoop\Model\Account $oAccount, /*resource*/ $rFile, string $sFileStart): int
{ {
$iCount = 0; $iCount = 0;
$aHeaders = null;
$aData = array();
if ($oAccount && \is_resource($rFile)) {
$oAddressBookProvider = $this->AddressBookProvider($oAccount); $oAddressBookProvider = $this->AddressBookProvider($oAccount);
if ($oAddressBookProvider && $oAddressBookProvider->IsActive()) { if (\is_resource($rFile) && $oAddressBookProvider && $oAddressBookProvider->IsActive()) {
$sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';';
\setlocale(LC_CTYPE, 'en_US.UTF-8');
while (false !== ($mRow = \fgetcsv($rFile, 5000, $sDelimiter, '"'))) {
if (null === $aHeaders) {
if (3 >= \count($mRow)) {
return 0;
}
$aHeaders = $mRow;
foreach ($aHeaders as $iIndex => $sHeaderValue) {
$aHeaders[$iIndex] = \MailSo\Base\Utils::Utf8Clear($sHeaderValue);
}
} else {
$aNewItem = array();
foreach ($aHeaders as $iIndex => $sHeaderValue) {
$aNewItem[$sHeaderValue] = isset($mRow[$iIndex]) ? $mRow[$iIndex] : '';
}
$aData[] = $aNewItem;
}
}
if (\count($aData)) {
$this->oLogger->Write('Import contacts from csv'); $this->oLogger->Write('Import contacts from csv');
$iCount = $oAddressBookProvider->ImportCsvArray($aData); $sDelimiter = ((int)\strpos($sFileStart, ',') > (int)\strpos($sFileStart, ';')) ? ',' : ';';
foreach (\RainLoop\Providers\AddressBook\Utils::CsvStreamToContacts($rFile, $sDelimiter) as $oContact) {
if ($oAddressBookProvider->ContactSave($oContact)) {
++$iCount;
} }
} }
} }
return $iCount; return $iCount;
} }

View file

@ -83,19 +83,6 @@ class AddressBook extends AbstractProvider
return $this->IsActive() ? $this->oDriver->IncFrec($aEmails, $bCreateAuto) : false; return $this->IsActive() ? $this->oDriver->IncFrec($aEmails, $bCreateAuto) : false;
} }
public function ImportCsvArray(array $aCsvData) : int
{
$iCount = 0;
if ($this->IsActive()) {
foreach (AddressBook\Utils::CsvArrayToContacts($aCsvData) as $oContact) {
if ($this->ContactSave($oContact)) {
++$iCount;
}
}
}
return $iCount;
}
public function ImportVcfFile(string $sVcfData) : int public function ImportVcfFile(string $sVcfData) : int
{ {
$iCount = 0; $iCount = 0;

View file

@ -22,20 +22,29 @@ class Utils
'namesuffix' => 4, 'namesuffix' => 4,
'shortname' => 'NICKNAME', 'shortname' => 'NICKNAME',
'nickname' => 'NICKNAME', 'nickname' => 'NICKNAME',
'birthday' => 'BDAY',
'mobile' => array('TEL', 'CELL'),
'mobilephone' => array('TEL', 'CELL'),
'businessemail' => array('EMAIL', 'WORK'),
'businessemail2' => array('EMAIL', 'WORK'),
'businessemail3' => array('EMAIL', 'WORK'),
'businessphone' => array('TEL', 'WORK'), 'businessphone' => array('TEL', 'WORK'),
'businessphone2' => array('TEL', 'WORK'), 'businessphone2' => array('TEL', 'WORK'),
'businessphone3' => array('TEL', 'WORK'), 'businessphone3' => array('TEL', 'WORK'),
'businessmobile' => array('TEL', 'WORK,CELL'),
'businessmobilephone' => array('TEL', 'WORK,CELL'),
'businessweb' => array('URL', 'WORK'),
'businesswebpage' => array('URL', 'WORK'),
'businesswebsite' => array('URL', 'WORK'),
'companyphone' => array('TEL', 'WORK'), 'companyphone' => array('TEL', 'WORK'),
'companymainphone' => array('TEL', 'WORK'), 'companymainphone' => array('TEL', 'WORK'),
'primaryphone' => array('TEL', 'PREF,HOME'),
'homephone' => array('TEL', 'HOME'), 'homephone' => array('TEL', 'HOME'),
'homephone2' => array('TEL', 'HOME'), 'homephone2' => array('TEL', 'HOME'),
'homephone3' => array('TEL', 'HOME'), 'homephone3' => array('TEL', 'HOME'),
'mobile' => array('TEL', 'CELL'),
'mobilephone' => array('TEL', 'CELL'),
'businessmobile' => array('TEL', 'WORK,CELL'),
'businessmobilephone' => array('TEL', 'WORK,CELL'),
'otherphone' => 'TEL',
'primaryphone' => array('TEL', 'PREF,HOME'),
'email' => array('EMAIL', 'HOME'), 'email' => array('EMAIL', 'HOME'),
'email2' => array('EMAIL', 'HOME'), 'email2' => array('EMAIL', 'HOME'),
'email3' => array('EMAIL', 'HOME'), 'email3' => array('EMAIL', 'HOME'),
@ -48,48 +57,69 @@ class Utils
'emailaddress' => array('EMAIL', 'HOME'), 'emailaddress' => array('EMAIL', 'HOME'),
'email2address' => array('EMAIL', 'HOME'), 'email2address' => array('EMAIL', 'HOME'),
'email3address' => array('EMAIL', 'HOME'), 'email3address' => array('EMAIL', 'HOME'),
'otheremail' => 'EMAIL',
'businessemail' => array('EMAIL', 'WORK'),
'businessemail2' => array('EMAIL', 'WORK'),
'businessemail3' => array('EMAIL', 'WORK'),
'personalemail' => array('EMAIL', 'HOME'), 'personalemail' => array('EMAIL', 'HOME'),
'personalemail2' => array('EMAIL', 'HOME'), 'personalemail2' => array('EMAIL', 'HOME'),
'personalemail3' => array('EMAIL', 'HOME'), 'personalemail3' => array('EMAIL', 'HOME'),
'personalwebsite' => array('URL', 'HOME'),
'otheremail' => 'EMAIL',
'otherphone' => 'TEL',
'notes' => 'NOTE', 'notes' => 'NOTE',
'web' => 'URL', 'web' => 'URL',
'businessweb' => array('URL', 'WORK'),
'webpage' => 'URL', 'webpage' => 'URL',
'businesswebpage' => array('URL', 'WORK'), 'website' => 'URL'
'website' => 'URL', /*
'businesswebsite' => array('URL', 'WORK'), TODO:
'personalwebsite' => 'URL', 'company' => '',
'birthday' => 'BDAY' 'department' => '',
'jobtitle' => '',
'officelocation' => '',
'homestreet' => '',
'homecity' => '',
'homestate' => '',
'homepostalcode' => '',
'homecountry' => '',
'businessstreet' => '',
'businesscity' => '',
'businessstate' => '',
'businesspostalcode' => '',
'businesscountry' => '',
*/
); );
public static function CsvArrayToContacts(array $aCsvData) : iterable public static function CsvStreamToContacts(/*resource*/ $rFile, string $sDelimiter) : iterable
{ {
foreach ($aCsvData as $aItem) { \setlocale(LC_CTYPE, 'en_US.UTF-8');
$aHeaders = \fgetcsv($rFile, 5000, $sDelimiter, '"');
if (!$aHeaders || 3 >= \count($aHeaders)) {
return;
}
foreach ($aHeaders as $iIndex => $sItemName) {
$sItemName = \MailSo\Base\Utils::Utf8Clear($sItemName);
$sItemName = \strtoupper(\trim(\preg_replace('/[\s\-]+/', '', $sItemName)));
if (!\array_key_exists($sItemName, \Sabre\VObject\Component\VCard::$propertyMap)) {
$sItemName = \strtolower($sItemName);
$sItemName = isset(static::$aMap[$sItemName]) ? static::$aMap[$sItemName] : null;
}
$aHeaders[$iIndex] = $sItemName;
}
while (false !== ($mRow = \fgetcsv($rFile, 5000, $sDelimiter, '"'))) {
\MailSo\Base\Utils::ResetTimeLimit(); \MailSo\Base\Utils::ResetTimeLimit();
$iCount = 0; $iCount = 0;
$oVCard = new \Sabre\VObject\Component\VCard; $oVCard = new \Sabre\VObject\Component\VCard;
$aName = ['','','','','']; $aName = ['','','','',''];
foreach ($aItem as $sItemName => $sItemValue) { foreach ($mRow as $iIndex => $sItemValue) {
$sItemName = \strtoupper(\trim(\preg_replace('/[\s\-]+/', '', $sItemName))); $sItemName = $aHeaders[$iIndex];
$sItemValue = \trim($sItemValue); $sItemValue = \trim($sItemValue);
if (!empty($sItemName) && !empty($sItemValue)) { if (isset($sItemName) && !empty($sItemValue)) {
if (\array_key_exists($sItemName, \Sabre\VObject\Component\VCard::$propertyMap)) { $mType = \is_array($sItemName) ? $sItemName[0] : $sItemName;
$mData = $sItemName;
} else {
$sItemName = \strtolower($sItemName);
$mData = !empty($sItemName) && isset($aMap[$sItemName]) ? $aMap[$sItemName] : null;
}
if ($mData) {
$mType = \is_array($mData) ? $mData[0] : $mData;
++$iCount; ++$iCount;
if (\is_int($mType)) { if (\is_int($mType)) {
$aName[$mType] = $sItemValue; $aName[$mType] = $sItemValue;
} else if (\is_array($mData)) { } else if (\is_array($sItemName)) {
$oVCard->add($mType, $sItemValue, ['type' => $mData[1]]); $oVCard->add($mType, $sItemValue, ['type' => $sItemName[1]]);
} else if ('FN' === $mType || 'NICKNAME' === $mType) { } else if ('FN' === $mType || 'NICKNAME' === $mType) {
$oVCard->$mType = $sItemValue; $oVCard->$mType = $sItemValue;
} else { } else {
@ -97,7 +127,6 @@ class Utils
} }
} }
} }
}
if ($iCount) { if ($iCount) {
if ('' !== \implode('', $aName)) { if ('' !== \implode('', $aName)) {
$oVCard->N = $aName; $oVCard->N = $aName;