different format for writeable and non-writeable address books

Also move "open" function up into constructor.
This commit is contained in:
Alexey Shpakovsky 2021-05-30 17:03:30 +02:00
parent 8781703a69
commit da870a6d11

View file

@ -45,7 +45,9 @@ class abook_carddav extends addressbook_backend {
if (!empty($param['password'])) { $this->password = $param['password']; } if (!empty($param['password'])) { $this->password = $param['password']; }
if (isset($param['writeable'])) { $this->writeable = $param['writeable']; } if (isset($param['writeable'])) { $this->writeable = $param['writeable']; }
if (isset($param['listing'])) { $this->listing = $param['listing']; } if (isset($param['listing'])) { $this->listing = $param['listing']; }
return $this->open(); $this->account = new Account($this->base_uri, $this->username, $this->password, $this->base_uri);
$this->abook = new AddressbookCollection($this->abook_uri, $this->account);
$this->abook_uri_len=strlen($this->abook->getUriPath());
} }
else { else {
return $this->set_error('Invalid argument to constructor'); return $this->set_error('Invalid argument to constructor');
@ -53,31 +55,36 @@ class abook_carddav extends addressbook_backend {
} }
/** /**
* * Given a $vcard object and its $uri, returns squirrelmail contact (array).
* Optional $email arg overwrites the one stored in vcard.
* Respects $this->writeable:
* for writeable addressbooks, 'nickname' must be unique identifier -
* in our case, last part of uid id used
* for non-writeable addressbooks, 'nickname' doesn't matter that much -
* so we put ORG there
*/ */
function open() { function vcard2sq($uri, $vcard, $email=null) {
// backend open function if($this->writeable) {
$this->account = new Account($this->base_uri, $this->username, $this->password, $this->base_uri); $nickname = substr($uri, $this->abook_uri_len);
$this->abook = new AddressbookCollection($this->abook_uri, $this->account); $label = (string)$vcard->ORG;
// TODO: check that it's valid } else {
return true; $nickname = (string)$vcard->ORG;
$label = '';
/* TODO: move this to discover page }
// Discover the addressbooks for that account if(!$email) {
try { $email = (string)$vcard->EMAIL;
$discover = new Discovery(); }
$abooks = $discover->discoverAddressbooks($this->account); $names = $vcard->N->getParts();
} catch (\Exception $e) { // last,first,additional,prefix,suffix
return $this->set_error("!!! Error during addressbook discovery: " . $e->getMessage()); return array(
} 'nickname' => $nickname,
if (count($abooks) <= 0) { 'name' => (string)$vcard->FN,
return $this->set_error("Cannot proceed because no addressbooks were found - exiting"); 'firstname' => (string)$names[1],
} 'lastname' => (string)$names[0],
$this->abook = $abooks[0]; 'email' => $email,
// HINT: use this line to get your discovered addressbook URI 'label' => $label,
// echo "discovered: " . $this->abook->getUri(); 'backend' => $this->bnum,
return true; 'source' => $this->sname);
*/
} }
/** /**
@ -96,7 +103,6 @@ class abook_carddav extends addressbook_backend {
*/ */
function run_query($query, $match_all=false, $limit=0) { function run_query($query, $match_all=false, $limit=0) {
$ret = array(); $ret = array();
// TODO: add nickname to list of fields if $this->writeable
$all=$this->abook->query($query,["FN", "N", "EMAIL", "ORG"],$match_all,$limit); $all=$this->abook->query($query,["FN", "N", "EMAIL", "ORG"],$match_all,$limit);
/* /*
Returns an array of matched VCards: Returns an array of matched VCards:
@ -104,26 +110,19 @@ class abook_carddav extends addressbook_backend {
The values are associative arrays with keys etag (type: string) and vcard (type: VCard) The values are associative arrays with keys etag (type: string) and vcard (type: VCard)
*/ */
$abook_uri_len=strlen($this->abook->getUriPath());
foreach($all as $uri => $one) { foreach($all as $uri => $one) {
$vcard = $one['vcard']; $vcard = $one['vcard'];
if(!isset($vcard->EMAIL)) { continue; } if(!isset($vcard->EMAIL)) { continue; }
$names = $vcard->N->getParts(); if($this->writeable) {
// last,first,additional,prefix,suffix // all one line per each vcard
// TODO: if !$this->writeable then we want to add one row per each email this vcard has $ret[] = $this.vcard2sq($uri, $vcard);
// foreach($vcard->EMAIL as $email) { ... } } else {
$value = array( foreach($vcard->EMAIL as $email) {
// TODO: nickname depends on $this->writeable // all one line per each email
'nickname' => substr($uri, $abook_uri_len), $ret[] = $this.vcard2sq($uri, $vcard, $email);
'name' => (string)$vcard->FN, }
'firstname' => (string)$names[1], }
'lastname' => (string)$names[0], if($limit == 1) { return $ret[0]; }
'email' => (string)$vcard->EMAIL,
'label' => (string)$vcard->ORG,
'backend' => $this->bnum,
'source' => $this->sname);
if($limit == 1) { return $value; }
array_push($ret,$value);
} }
return $ret; return $ret;
} }
@ -182,17 +181,7 @@ class abook_carddav extends addressbook_backend {
vcard(VCard): VCard as Sabre/VObject VCard vcard(VCard): VCard as Sabre/VObject VCard
*/ */
$vcard = $one['vcard']; $vcard = $one['vcard'];
$names = $vcard->N->getParts(); return vcard2sq($uri, $vcard);
return array(
// TODO: nickname depends on $this->writeable
'nickname' => substr($uri, $abook_uri_len),
'name' => (string)$vcard->FN,
'firstname' => (string)$names[1],
'lastname' => (string)$names[0],
'email' => (string)$vcard->EMAIL,
'label' => (string)$vcard->ORG,
'backend' => $this->bnum,
'source' => $this->sname);
} }
if($field == SM_ABOOK_FIELD_FIRSTNAME) { if($field == SM_ABOOK_FIELD_FIRSTNAME) {
// TODO: this will be harder // TODO: this will be harder
@ -307,11 +296,7 @@ class abook_carddav extends addressbook_backend {
$names[0]=$userdata['lastname']; $names[0]=$userdata['lastname'];
$names[1]=$userdata['firstname']; $names[1]=$userdata['firstname'];
$vcard->N = $names; $vcard->N = $names;
if($names[2]){ $vcard->FN = trim($names[3].' '.$names[1].' '.$names[2].' '.$names[0].' '.$names[4]);
$vcard->FN = trim($names[3].' '.$names[1].' '.$names[2].' '.$names[0].' '.$names[4]);
} else {
$vcard->FN = trim($names[3].' '.$names[1].' '.$names[0].' '.$names[4]);
}
// [prefix=3] first=1 [additional=2] last=0 [suffix=4] // [prefix=3] first=1 [additional=2] last=0 [suffix=4]
$vcard->EMAIL = $userdata['email']; $vcard->EMAIL = $userdata['email'];
$vcard->ORG = $userdata['label']; $vcard->ORG = $userdata['label'];