CardDAV (pre-alpha/unstable)

This commit is contained in:
RainLoop Team 2013-12-23 04:06:48 +04:00
parent 0ec965bccb
commit 599e934b4a
76 changed files with 6984 additions and 1512 deletions

View file

@ -2,7 +2,10 @@
namespace RainLoop\Providers\PersonalAddressBook\Classes;
use RainLoop\Providers\PersonalAddressBook\Enumerations\PropertyType;
use
RainLoop\Providers\PersonalAddressBook\Enumerations\PropertyType,
RainLoop\Providers\PersonalAddressBook\Classes\Property
;
class Contact
{
@ -21,16 +24,6 @@ class Contact
*/
public $Display;
/**
* @var string
*/
public $DisplayName;
/**
* @var string
*/
public $DisplayEmail;
/**
* @var int
*/
@ -56,6 +49,21 @@ class Contact
*/
public $ReadOnly;
/**
* @var string
*/
public $CardDavData;
/**
* @var string
*/
public $CardDavHash;
/**
* @var int
*/
public $CardDavSize;
public function __construct()
{
$this->Clear();
@ -67,19 +75,25 @@ class Contact
$this->IdContactStr = '';
$this->IdUser = 0;
$this->Display = '';
$this->DisplayName = '';
$this->DisplayEmail = '';
$this->ScopeType = \RainLoop\Providers\PersonalAddressBook\Enumerations\ScopeType::DEFAULT_;
$this->Changed = \time();
$this->IdPropertyFromSearch = 0;
$this->Properties = array();
$this->ReadOnly = false;
$this->CardDavData = '';
$this->CardDavHash = '';
$this->CardDavSize = 0;
}
public function UpdateDependentValues()
public function UpdateDependentValues($bReparseVcard = true)
{
$sDisplayName = '';
$sDisplayEmail = '';
$sLastName = '';
$sFirstName = '';
$sEmail = '';
$sOther = '';
$oFullNameProperty = null;
foreach ($this->Properties as /* @var $oProperty \RainLoop\Providers\PersonalAddressBook\Classes\Property */ &$oProperty)
{
@ -87,16 +101,33 @@ class Contact
{
$oProperty->ScopeType = $this->ScopeType;
$oProperty->UpdateDependentValues();
if ('' === $sDisplayName && \RainLoop\Providers\PersonalAddressBook\Enumerations\PropertyType::FULLNAME === $oProperty->Type &&
0 < \strlen($oProperty->Value))
if (!$oFullNameProperty && PropertyType::FULLNAME === $oProperty->Type)
{
$sDisplayName = $oProperty->Value;
$oFullNameProperty =& $oProperty;
}
else if ('' === $sDisplayEmail && $oProperty->IsEmail() &&
0 < \strlen($oProperty->Value))
if (0 < \strlen($oProperty->Value))
{
$sDisplayEmail = $oProperty->Value;
if ('' === $sEmail && $oProperty->IsEmail())
{
$sEmail = $oProperty->Value;
}
else if ('' === $sLastName && PropertyType::LAST_NAME === $oProperty->Type)
{
$sLastName = $oProperty->Value;
}
else if ('' === $sFirstName && PropertyType::FIRST_NAME === $oProperty->Type)
{
$sFirstName = $oProperty->Value;
}
else if (\in_array($oProperty->Type, array(PropertyType::FULLNAME,
PropertyType::PHONE_PERSONAL, PropertyType::PHONE_BUSSINES,
PropertyType::MOBILE_PERSONAL, PropertyType::MOBILE_BUSSINES,
)))
{
$sOther = $oProperty->Value;
}
}
}
}
@ -106,10 +137,55 @@ class Contact
$this->IdContactStr = \Sabre\DAV\UUIDUtil::getUUID();
}
$this->DisplayName = $sDisplayName;
$this->DisplayEmail = $sDisplayEmail;
$sDisplay = '';
if (0 < \strlen($sLastName) || 0 < \strlen($sFirstName))
{
$sDisplay = \trim($sLastName.' '.$sFirstName);
}
$this->Display = 0 < \strlen($sDisplayName) ? $sDisplayName : (!empty($sDisplayEmail) ? $sDisplayEmail : '');
if ('' === $sDisplay && 0 < \strlen($sEmail))
{
$sDisplay = \trim($sEmail);
}
if ('' === $sDisplay)
{
$sDisplay = $sOther;
}
$this->Display = \trim($sDisplay);
$bNewFull = false;
if (!$oFullNameProperty)
{
$oFullNameProperty = new \RainLoop\Providers\PersonalAddressBook\Classes\Property(PropertyType::FULLNAME, $this->Display);
$bNewFull = true;
}
$oFullNameProperty->Value = $this->Display;
$oFullNameProperty->UpdateDependentValues();
if ($bNewFull)
{
$this->Properties[] = $oFullNameProperty;
}
if ($bReparseVcard || '' === $this->CardDavData)
{
$oVCard = $this->ToVCardObject($this->CardDavData);
$this->CardDavData = $oVCard ? $oVCard->serialize() : $this->CardDavData;
}
if (!empty($this->CardDavData))
{
$this->CardDavHash = \md5($this->CardDavData);
$this->CardDavSize = \strlen($this->CardDavData);
}
else
{
$this->CardDavHash = '';
$this->CardDavSize = 0;
}
}
/**
@ -129,19 +205,183 @@ class Contact
return \array_unique($aResult);
}
/**
* @param string $sVCard
*/
public function ParseVCard($sVCard)
{
$bNew = empty($this->IdContact);
if (!$bNew)
{
$this->Properties = array();
}
$oVCard = null;
$aProperties = array();
try
{
$oVCard = \Sabre\VObject\Reader::read($sVCard);
}
catch (\Exception $oExc) {}
if ($oVCard && $oVCard->UID)
{
$this->IdContactStr = (string) $oVCard->UID;
if (isset($oVCard->FN) && '' !== \trim($oVCard->FN))
{
$aProperties[] = new Property(PropertyType::FULLNAME, \trim($oVCard->FN));
}
if (isset($oVCard->NICKNAME) && '' !== \trim($oVCard->NICKNAME))
{
$aProperties[] = new Property(PropertyType::NICK_NAME, \trim($oVCard->NICKNAME));
}
// if (isset($oVCard->NOTE) && '' !== \trim($oVCard->NOTE))
// {
// $aProperties[] = new Property(PropertyType::NOTE, \trim($oVCard->NOTE));
// }
if (isset($oVCard->N))
{
$aNames = $oVCard->N->getParts();
foreach ($aNames as $iIndex => $sValue)
{
$sValue = \trim($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))
{
foreach($oVCard->EMAIL as $oEmail)
{
$oTypes = $oEmail ? $oEmail['TYPE'] : null;
$sEmail = $oTypes ? \trim((string) $oEmail) : '';
if ($oTypes && 0 < \strlen($sEmail))
{
if ($oTypes->has('WORK'))
{
$aProperties[] = new Property(PropertyType::EMAIl_BUSSINES, $sEmail);
}
else
{
$aProperties[] = new Property(PropertyType::EMAIl_PERSONAL, $sEmail);
}
}
}
}
if (isset($oVCard->TEL))
{
foreach($oVCard->TEL as $oTel)
{
$oTypes = $oTel ? $oTel['TYPE'] : null;
$sTel = $oTypes ? \trim((string) $oTel) : '';
if ($oTypes && 0 < \strlen($sTel))
{
if ($oTypes->has('VOICE'))
{
if ($oTypes->has('WORK'))
{
$aProperties[] = new Property(PropertyType::PHONE_BUSSINES, $sTel);
}
else
{
$aProperties[] = new Property(PropertyType::PHONE_PERSONAL, $sTel);
}
}
else if ($oTypes->has('CELL'))
{
if ($oTypes->has('WORK'))
{
$aProperties[] = new Property(PropertyType::MOBILE_BUSSINES, $sTel);
}
else
{
$aProperties[] = new Property(PropertyType::MOBILE_PERSONAL, $sTel);
}
}
else if ($oTypes->has('FAX'))
{
if ($oTypes->has('WORK'))
{
$aProperties[] = new Property(PropertyType::FAX_BUSSINES, $sTel);
}
else
{
$aProperties[] = new Property(PropertyType::FAX_PERSONAL, $sTel);
}
}
else if ($oTypes->has('WORK'))
{
$aProperties[] = new Property(PropertyType::MOBILE_BUSSINES, $sTel);
}
else
{
$aProperties[] = new Property(PropertyType::MOBILE_PERSONAL, $sTel);
}
}
}
}
$this->Properties = $aProperties;
$this->CardDavData = $sVCard;
}
$this->UpdateDependentValues(false);
}
/**
* @return string
*/
public function ToVCardObject()
public function ToVCardObject($sPreVCard = '')
{
$this->UpdateDependentValues();
// $this->UpdateDependentValues();
$oVCard = new \Sabre\VObject\Component\VCard('VCARD');
$oVCard = null;
if (0 < \strlen($sPreVCard))
{
try
{
$oVCard = \Sabre\VObject\Reader::read($sPreVCard);
}
catch (\Exception $oExc) {};
}
if (!$oVCard)
{
$oVCard = new \Sabre\VObject\Component\VCard();
}
$oVCard->VERSION = '3.0';
$oVCard->PRODID = '-//RainLoop//'.APP_VERSION.'//EN';
$sFirstName = $sSurName = '';
unset($oVCard->FN, $oVCard->EMAIL, $oVCard->TEL);
$sFirstName = $sLastName = $sMiddleName = $sSuffix = $sPrefix = '';
foreach ($this->Properties as /* @var $oProperty \RainLoop\Providers\PersonalAddressBook\Classes\Property */ &$oProperty)
{
if ($oProperty)
@ -151,55 +391,55 @@ class Contact
case PropertyType::FULLNAME:
$oVCard->FN = $oProperty->Value;
break;
case PropertyType::NICK:
case PropertyType::NICK_NAME:
$oVCard->NICKNAME = $oProperty->Value;
break;
case PropertyType::EMAIl_PERSONAL:
case PropertyType::EMAIl_OTHER:
$oVCard->add('EMAIL', $oProperty->Value, array('TYPE' => 'INTERNET', 'TYPE' => 'HOME'));
$oVCard->add('EMAIL', $oProperty->Value, array('TYPE' => array('INTERNET', 'HOME')));
break;
case PropertyType::EMAIl_BUSSINES:
$oVCard->add('EMAIL', $oProperty->Value, array('TYPE' => 'INTERNET', 'TYPE' => 'WORK'));
$oVCard->add('EMAIL', $oProperty->Value, array('TYPE' => array('INTERNET', 'WORK')));
break;
case PropertyType::PHONE_PERSONAL:
case PropertyType::PHONE_OTHER:
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => 'VOICE', 'TYPE' => 'HOME'));
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => array('VOICE', 'HOME')));
break;
case PropertyType::PHONE_BUSSINES:
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => 'VOICE', 'TYPE' => 'WORK'));
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => array('VOICE', 'WORK')));
break;
case PropertyType::MOBILE_PERSONAL:
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => array('CELL', 'HOME')));
break;
case PropertyType::MOBILE_BUSSINES:
case PropertyType::MOBILE_OTHER:
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => 'VOICE', 'TYPE' => 'CELL'));
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => array('CELL', 'WORK')));
break;
case PropertyType::FAX_PERSONAL:
case PropertyType::FAX_OTHER:
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => 'FAX', 'TYPE' => 'HOME'));
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => array('FAX', 'HOME')));
break;
case PropertyType::FAX_BUSSINES:
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => 'FAX', 'TYPE' => 'WORK'));
$oVCard->add('TEL', $oProperty->Value, array('TYPE' => array('FAX', 'WORK')));
break;
case PropertyType::FIRST_NAME:
$sFirstName = $oProperty->Value;
break;
case PropertyType::SUR_NAME:
$sSurName = $oProperty->Value;
case PropertyType::LAST_NAME:
$sLastName = $oProperty->Value;
break;
case PropertyType::MIDDLE_NAME:
$sMiddleName = $oProperty->Value;
break;
case PropertyType::NAME_SUFFIX:
$sSuffix = $oProperty->Value;
break;
case PropertyType::NAME_PREFIX:
$sPrefix = $oProperty->Value;
break;
}
}
}
$oVCard->UID = $this->VCardUID();
// $oVCard->N = array(
// $sSurName,
// $sFirstName,
// '',
// '',
// '',
// ''
// );
$oVCard->UID = $this->IdContactStr;
$oVCard->N = array($sLastName, $sFirstName, $sMiddleName, $sPrefix, $sSuffix);
$oVCard->REV = gmdate('Ymd').'T'.gmdate('His').'Z';
return $oVCard;
}
@ -207,10 +447,8 @@ class Contact
/**
* @return string
*/
public function VCardUID()
public function VCardUri()
{
return $this->IdContactStr.'.vcf';
}
}

View file

@ -41,9 +41,13 @@ class Property
*/
public $Frec;
public function __construct()
public function __construct(
$iType = \RainLoop\Providers\PersonalAddressBook\Enumerations\PropertyType::UNKNOWN, $sValue = '')
{
$this->Clear();
$this->Type = $iType;
$this->Value = $sValue;
}
public function Clear()
@ -66,7 +70,7 @@ class Property
public function IsEmail()
{
return \in_array($this->Type, array(
PropertyType::EMAIl_PERSONAL, PropertyType::EMAIl_BUSSINES, PropertyType::EMAIl_OTHER
PropertyType::EMAIl_PERSONAL, PropertyType::EMAIl_BUSSINES
));
}
@ -76,9 +80,9 @@ class Property
public function IsPhone()
{
return \in_array($this->Type, array(
PropertyType::PHONE_PERSONAL, PropertyType::PHONE_BUSSINES, PropertyType::PHONE_OTHER,
PropertyType::MOBILE_PERSONAL, PropertyType::MOBILE_BUSSINES, PropertyType::MOBILE_OTHER,
PropertyType::FAX_BUSSINES, PropertyType::FAX_OTHER
PropertyType::PHONE_PERSONAL, PropertyType::PHONE_BUSSINES,
PropertyType::MOBILE_PERSONAL, PropertyType::MOBILE_BUSSINES,
PropertyType::FAX_BUSSINES
));
}