/** * @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 ("" === \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\AddressBook\Classes\Contact(); $oVCard = null; while ($oVCard = $oVCardSplitter->getNext()) { if ($oVCard instanceof \Sabre\VObject\Component\VCard) { \MailSo\Base\Utils::ResetTimeLimit(); if (empty($oVCard->UID)) { $oVCard->UID = \Sabre\DAV\UUIDUtil::getUUID(); } $oContact->PopulateByVCard($oVCard->serialize()); if (0 < \count($oContact->Properties)) { if ($this->ContactSave($sEmail, $oContact)) { $iCount++; } } $oContact->Clear(); } } } } return $iCount; }
/** * @param string $sEmail * @param string $sUrl * @param string $sUser * @param string $sPassword * @param string $sProxy = '' * * @return bool */ public function Sync($sEmail, $sUrl, $sUser, $sPassword, $sProxy = '') { $this->SyncDatabase(); $iUserID = $this->getUserId($sEmail); if (0 >= $iUserID) { return false; } $aUrl = \parse_url($sUrl); if (!\is_array($aUrl)) { $aUrl = array(); } $aUrl['scheme'] = isset($aUrl['scheme']) ? $aUrl['scheme'] : 'http'; $aUrl['host'] = isset($aUrl['host']) ? $aUrl['host'] : 'localhost'; $aUrl['port'] = isset($aUrl['port']) ? $aUrl['port'] : 80; $aUrl['path'] = isset($aUrl['path']) ? \rtrim($aUrl['path'], '\\/') . '/' : '/'; $aSettings = array('baseUri' => $aUrl['scheme'] . '://' . $aUrl['host'] . ('80' === (string) $aUrl['port'] ? '' : ':' . $aUrl['port']), 'userName' => $sUser, 'password' => $sPassword); $this->oLogger->AddSecret($sPassword); if (!empty($sProxy)) { $aSettings['proxy'] = $sProxy; } $sPath = $aUrl['path']; if (!\class_exists('Sabre\\DAV\\Client')) { return false; } $oClient = new \Sabre\DAV\Client($aSettings); $oClient->setVerifyPeer(false); $this->oLogger->Write('User: '******'userName'] . ', Url: ' . $sUrl, \MailSo\Log\Enumerations\Type::INFO, 'DAV'); $aRemoteSyncData = $this->prepearRemoteSyncData($oClient, $sPath); if (false === $aRemoteSyncData) { return false; } $aDatabaseSyncData = $this->prepearDatabaseSyncData($iUserID); // $this->oLogger->WriteDump($aDatabaseSyncData); // $this->oLogger->WriteDump($aRemoteSyncData); //+++del (from carddav) foreach ($aDatabaseSyncData as $sKey => $aData) { if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) { $this->davClientRequest($oClient, 'DELETE', $sPath . $aRemoteSyncData[$sKey]['vcf']); } } //---del //+++del (from db) $aIdsForDeletedion = array(); foreach ($aDatabaseSyncData as $sKey => $aData) { if (!$aData['deleted'] && !empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) { $aIdsForDeletedion[] = $aData['id_contact']; } } if (0 < \count($aIdsForDeletedion)) { $this->DeleteContacts($sEmail, $aIdsForDeletedion, false); } //---del $this->flushDeletedContacts($iUserID); //+++new or newer (from db) foreach ($aDatabaseSyncData as $sKey => $aData) { if (!$aData['deleted'] && (empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) || !empty($aData['etag']) && isset($aRemoteSyncData[$sKey]) && $aRemoteSyncData[$sKey]['etag'] !== $aData['etag'] && $aRemoteSyncData[$sKey]['changed'] < $aData['changed']) { $mID = $aData['id_contact']; $oContact = $this->GetContactByID($sEmail, $mID, false); if ($oContact) { $sExsistensBody = ''; $mExsistenRemoteID = isset($aRemoteSyncData[$sKey]['vcf']) && !empty($aData['etag']) ? $aRemoteSyncData[$sKey]['vcf'] : ''; if (0 < \strlen($mExsistenRemoteID)) { $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $mExsistenRemoteID); if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) { $sExsistensBody = \trim($oResponse['body']); } } $oResponse = $this->davClientRequest($oClient, 'PUT', $sPath . $oContact->CardDavNameUri(), $oContact->ToVCard($sExsistensBody)); if ($oResponse && isset($oResponse['headers'], $oResponse['headers']['etag'])) { $sEtag = \trim(\trim($oResponse['headers']['etag']), '"\''); $sDate = !empty($oResponse['headers']['date']) ? \trim($oResponse['headers']['date']) : ''; if (!empty($sEtag)) { $iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate); $this->updateContactEtagAndTime($iUserID, $mID, $sEtag, $iChanged); } } } unset($oContact); } } //---new //+++new or newer (from carddav) foreach ($aRemoteSyncData as $sKey => $aData) { if (!isset($aDatabaseSyncData[$sKey]) || $aDatabaseSyncData[$sKey]['etag'] !== $aData['etag'] && $aDatabaseSyncData[$sKey]['changed'] < $aData['changed']) { $mExsistenContactID = isset($aDatabaseSyncData[$sKey]['id_contact']) ? $aDatabaseSyncData[$sKey]['id_contact'] : ''; $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $aData['vcf']); if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) { $sBody = \trim($oResponse['body']); if (!empty($sBody)) { $oContact = null; if ($mExsistenContactID) { $oContact = $this->GetContactByID($sEmail, $mExsistenContactID); } if (!$oContact) { $oContact = new \RainLoop\Providers\AddressBook\Classes\Contact(); } $oContact->PopulateByVCard($sBody, !empty($oResponse['headers']['etag']) ? \trim(\trim($oResponse['headers']['etag']), '"\'') : ''); $this->ContactSave($sEmail, $oContact); unset($oContact); } } } } return true; }
/** * @param string $sEmail * @param string $sUrl * @param string $sUser * @param string $sPassword * @param string $sProxy = '' * * @return bool */ public function Sync($sEmail, $sUrl, $sUser, $sPassword, $sProxy = '') { $this->SyncDatabase(); $iUserID = $this->getUserId($sEmail); if (0 >= $iUserID) { return false; } $oClient = $this->getDavClient($sUrl, $sUser, $sPassword, $sProxy); if (!$oClient) { return false; } $sPath = $oClient->__UrlPath__; $aRemoteSyncData = $this->prepearRemoteSyncData($oClient, $sPath); if (false === $aRemoteSyncData) { return false; } $aDatabaseSyncData = $this->prepearDatabaseSyncData($iUserID); // $this->oLogger->WriteDump($aRemoteSyncData); // $this->oLogger->WriteDump($aDatabaseSyncData); //+++del (from carddav) foreach ($aDatabaseSyncData as $sKey => $aData) { if ($aData['deleted'] && isset($aRemoteSyncData[$sKey], $aRemoteSyncData[$sKey]['vcf'])) { $this->davClientRequest($oClient, 'DELETE', $sPath . $aRemoteSyncData[$sKey]['vcf']); } } //---del //+++del (from db) $aIdsForDeletedion = array(); foreach ($aDatabaseSyncData as $sKey => $aData) { if (!$aData['deleted'] && !empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) { $aIdsForDeletedion[] = $aData['id_contact']; } } if (0 < \count($aIdsForDeletedion)) { $this->DeleteContacts($sEmail, $aIdsForDeletedion, false); } //---del $this->flushDeletedContacts($iUserID); //+++new or newer (from db) foreach ($aDatabaseSyncData as $sKey => $aData) { if (!$aData['deleted'] && (empty($aData['etag']) && !isset($aRemoteSyncData[$sKey])) || !empty($aData['etag']) && isset($aRemoteSyncData[$sKey]) && $aRemoteSyncData[$sKey]['etag'] !== $aData['etag'] && $aRemoteSyncData[$sKey]['changed'] < $aData['changed']) { $mID = $aData['id_contact']; $oContact = $this->GetContactByID($sEmail, $mID, false); if ($oContact) { $sExsistensBody = ''; $mExsistenRemoteID = isset($aRemoteSyncData[$sKey]['vcf']) && !empty($aData['etag']) ? $aRemoteSyncData[$sKey]['vcf'] : ''; if (0 < \strlen($mExsistenRemoteID)) { $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $mExsistenRemoteID); if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) { $sExsistensBody = \trim($oResponse['body']); } // $this->oLogger->WriteDump($sExsistensBody); } $oResponse = $this->davClientRequest($oClient, 'PUT', $sPath . (0 < \strlen($mExsistenRemoteID) ? $mExsistenRemoteID : $oContact->CardDavNameUri()), $oContact->ToVCard($sExsistensBody, $this->oLogger) . "\r\n\r\n"); if ($oResponse && isset($oResponse['headers'], $oResponse['headers']['etag'])) { $sEtag = \trim(\trim($oResponse['headers']['etag']), '"\''); $sDate = !empty($oResponse['headers']['date']) ? \trim($oResponse['headers']['date']) : ''; if (!empty($sEtag)) { $iChanged = empty($sDate) ? \time() : \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDate); $this->updateContactEtagAndTime($iUserID, $mID, $sEtag, $iChanged); } } } unset($oContact); } } //---new //+++new or newer (from carddav) foreach ($aRemoteSyncData as $sKey => $aData) { if (!isset($aDatabaseSyncData[$sKey]) || $aDatabaseSyncData[$sKey]['etag'] !== $aData['etag'] && $aDatabaseSyncData[$sKey]['changed'] < $aData['changed']) { $mExsistenContactID = isset($aDatabaseSyncData[$sKey]['id_contact']) ? $aDatabaseSyncData[$sKey]['id_contact'] : ''; $oResponse = $this->davClientRequest($oClient, 'GET', $sPath . $aData['vcf']); if ($oResponse && isset($oResponse['headers'], $oResponse['body'])) { $sBody = \trim($oResponse['body']); if (!empty($sBody)) { $oContact = null; if ($mExsistenContactID) { $oContact = $this->GetContactByID($sEmail, $mExsistenContactID); } if (!$oContact) { $oContact = new \RainLoop\Providers\AddressBook\Classes\Contact(); } $oContact->PopulateByVCard($aData['uid'], $sBody, !empty($oResponse['headers']['etag']) ? \trim(\trim($oResponse['headers']['etag']), '"\'') : '', $this->oLogger); $this->ContactSave($sEmail, $oContact); unset($oContact); } } } } return true; }