public function PopulateByVCard($sUid, $sVCard, $sEtag = '', $oLogger = null) { if ("" === \substr($sVCard, 0, 3)) { $sVCard = \substr($sVCard, 3); } $this->Properties = array(); if (!\class_exists('SabreForRainLoop\\DAV\\Client')) { return false; } if (!empty($sEtag)) { $this->Etag = $sEtag; } $this->IdContactStr = $sUid; try { $oVCard = \SabreForRainLoop\VObject\Reader::read($sVCard); } catch (\Exception $oExc) { if ($oLogger) { $oLogger->WriteException($oExc); $oLogger->WriteDump($sVCard); } } // if ($oLogger) // { // $oLogger->WriteDump($sVCard); // } $bOwnCloud = false; $aProperties = array(); if ($oVCard) { $bOwnCloud = empty($oVCard->PRODID) ? false : false !== \strpos(\strtolower($oVCard->PRODID), 'owncloud'); $bOldVersion = empty($oVCard->VERSION) ? false : \in_array((string) $oVCard->VERSION, array('2.1', '2.0', '1.0')); 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->NOTE) && '' !== \trim($oVCard->NOTE)) { $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'])) { if (0 < \strlen($sValue)) { $sEncValue = @\utf8_encode($sValue); if (0 === \strlen($sEncValue)) { $sEncValue = $sValue; } $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 : \SabreForRainLoop\DAV\UUIDUtil::getUUID(); $aProperties[] = new Property(PropertyType::UID, $sUidValue); if (empty($this->IdContactStr)) { $this->IdContactStr = $sUidValue; } $this->Properties = $aProperties; } $this->UpdateDependentValues(); return true; }
/** * Checks if the submitted iCalendar data is in fact, valid. * * An exception is thrown if it's not. * * @param resource|string $data * @return void */ protected function validateVCard(&$data) { // If it's a stream, we convert it to a string first. if (is_resource($data)) { $data = stream_get_contents($data); } // Converting the data to unicode, if needed. $data = DAV\StringUtil::ensureUTF8($data); try { $vobj = VObject\Reader::read($data); } catch (VObject\ParseException $e) { throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid vcard data. Parse error: ' . $e->getMessage()); } if ($vobj->name !== 'VCARD') { throw new DAV\Exception\UnsupportedMediaType('This collection can only support vcard objects.'); } if (!isset($vobj->UID)) { // No UID in vcards is invalid, but we'll just add it in anyway. $vobj->add('UID', DAV\UUIDUtil::getUUID()); $data = $vobj->serialize(); } }
/** * @param string $sEmail * @param string $sVcfData * * @return int */ public function ImportVcfFile($sEmail, $sVcfData) { $iCount = 0; if (\class_exists('SabreForRainLoop\\DAV\\Client') && $this->IsActive() && \is_string($sVcfData)) { $sVcfData = \trim($sVcfData); if ("" === \substr($sVcfData, 0, 3)) { $sVcfData = \substr($sVcfData, 3); } $oVCardSplitter = null; try { $oVCardSplitter = new \SabreForRainLoop\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 \SabreForRainLoop\VObject\Component\VCard) { \MailSo\Base\Utils::ResetTimeLimit(); if (empty($oVCard->UID)) { $oVCard->UID = \SabreForRainLoop\DAV\UUIDUtil::getUUID(); } $oContact->PopulateByVCard($oVCard->UID, $oVCard->serialize()); if (0 < \count($oContact->Properties)) { if ($this->ContactSave($sEmail, $oContact)) { $iCount++; } } $oContact->Clear(); } } } } return $iCount; }
/** * Parses a webdav lock xml body, and returns a new SabreForRainLoop\DAV\Locks\LockInfo object * * @param string $body * @return DAV\Locks\LockInfo */ protected function parseLockRequest($body) { $xml = simplexml_load_string(DAV\XMLUtil::convertDAVNamespace($body), null, LIBXML_NOWARNING); $xml->registerXPathNamespace('d', 'urn:DAV'); $lockInfo = new LockInfo(); $children = $xml->children("urn:DAV"); $lockInfo->owner = (string) $children->owner; $lockInfo->token = DAV\UUIDUtil::getUUID(); $lockInfo->scope = count($xml->xpath('d:lockscope/d:exclusive')) > 0 ? LockInfo::EXCLUSIVE : LockInfo::SHARED; return $lockInfo; }