1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-10 21:24:33 +01:00
OwncloudContactsOfficial/lib/carddav/backend.php

281 lines
7.8 KiB
PHP
Raw Normal View History

2011-08-06 22:32:06 +02:00
<?php
/**
2011-08-09 13:53:58 +02:00
* ownCloud - Addressbook
*
* @author Jakob Sack
2014-01-26 00:40:22 +01:00
* @author Thomas Tanghus
2011-08-09 13:53:58 +02:00
* @copyright 2011 Jakob Sack mail@jakobsack.de
2014-01-26 00:40:22 +01:00
* @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net)
2011-08-09 13:53:58 +02:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
2011-08-06 22:32:06 +02:00
*/
2013-03-05 11:08:04 +01:00
namespace OCA\Contacts\CardDAV;
use OCA\Contacts;
class Backend extends \Sabre_CardDAV_Backend_Abstract {
public function __construct($backends) {
$this->backends = $backends;
}
2011-08-06 22:32:06 +02:00
/**
* Returns the list of addressbooks for a specific user.
*
* @param string $principaluri
* @return array
*/
public function getAddressBooksForUser($principaluri) {
$app = new Contacts\App();
$userAddressBooks = array();
foreach($this->backends as $backendName) {
$backend = $app->getBackend($backendName);
$addressBooks = $backend->getAddressBooksForUser();
2013-08-04 04:40:52 +02:00
if (is_array($addressBooks)) {
foreach($addressBooks as $addressBook) {
if($addressBook['owner'] != \OCP\USER::getUser()) {
$addressBook['uri'] = $addressBook['uri'] . '_shared_by_' . $addressBook['owner'];
$addressBook['displayname'] = $addressBook['displayname'];
}
$userAddressBooks[] = array(
2013-08-04 04:40:52 +02:00
'id' => $backend->name . '::' . $addressBook['id'],
'uri' => $addressBook['uri'],
'principaluri' => 'principals/'.$addressBook['owner'],
'{DAV:}displayname' => $addressBook['displayname'],
'{' . \Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description'
=> $addressBook['description'],
'{http://calendarserver.org/ns/}getctag' => $addressBook['lastmodified'],
'{' . \Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' =>
new \Sabre_CardDAV_Property_SupportedAddressData(),
);
}
}
2011-08-06 22:32:06 +02:00
}
return $userAddressBooks;
2011-08-06 22:32:06 +02:00
}
/**
* Updates an addressbook's properties
*
* See Sabre_DAV_IProperties for a description of the mutations array, as
* well as the return value.
*
* @param mixed $addressbookid
* @param array $mutations
* @see Sabre_DAV_IProperties::updateProperties
* @return bool|array
*/
public function updateAddressBook($addressbookid, array $mutations) {
$changes = array();
2011-08-06 22:32:06 +02:00
foreach($mutations as $property=>$newvalue) {
switch($property) {
case '{DAV:}displayname' :
$changes['name'] = $newvalue;
2011-08-06 22:32:06 +02:00
break;
2013-03-05 11:08:04 +01:00
case '{' . \Sabre_CardDAV_Plugin::NS_CARDDAV
2012-08-02 05:02:36 +02:00
. '}addressbook-description' :
$changes['description'] = $newvalue;
2011-08-06 22:32:06 +02:00
break;
default :
// If any unsupported values were being updated, we must
// let the entire request fail.
return false;
}
}
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
return $backend->updateAddressBook($id, $changes);
2011-08-06 22:32:06 +02:00
}
/**
* Creates a new address book
*
* @param string $principaluri
2013-03-13 05:56:31 +01:00
* @param string $uri Just the 'basename' of the url.
2011-08-06 22:32:06 +02:00
* @param array $properties
* @return void
*/
2013-03-13 05:56:31 +01:00
public function createAddressBook($principaluri, $uri, array $properties) {
2011-08-06 22:32:06 +02:00
$properties = array();
2011-08-06 22:32:06 +02:00
foreach($properties as $property => $newvalue) {
2011-08-06 22:32:06 +02:00
switch($property) {
case '{DAV:}displayname' :
$properties['displayname'] = $newvalue;
2011-08-06 22:32:06 +02:00
break;
2013-03-05 11:08:04 +01:00
case '{' . \Sabre_CardDAV_Plugin::NS_CARDDAV
2012-08-02 05:02:36 +02:00
. '}addressbook-description' :
$properties['description'] = $newvalue;
2011-08-06 22:32:06 +02:00
break;
default :
2013-03-05 11:08:04 +01:00
throw new \Sabre_DAV_Exception_BadRequest('Unknown property: '
2012-08-02 05:02:36 +02:00
. $property);
2011-08-06 22:32:06 +02:00
}
}
$properties['uri'] = $uri;
$app = new Contacts\App();
$backend = $app->getBackend('local');
$backend->createAddressBook($properties);
2011-08-06 22:32:06 +02:00
}
/**
* Deletes an entire addressbook and all its contents
*
* @param mixed $addressbookid
2011-08-06 22:32:06 +02:00
* @return void
*/
public function deleteAddressBook($addressbookid) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$backend->deleteAddressBook($id);
2011-08-06 22:32:06 +02:00
}
/**
* Returns the last modified date if the backend supports it.
*
* @param mixed $addressbookid
* @return void
*/
public function lastModifiedAddressBook($addressbookid) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
return $backend->lastModifiedAddressBook($id);
}
2011-08-06 22:32:06 +02:00
/**
* Returns all cards for a specific addressbook id.
*
* @param mixed $addressbookid
* @return array
*/
public function getCards($addressbookid) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$contacts = $backend->getContacts($id);
2011-08-06 22:32:06 +02:00
$cards = array();
foreach($contacts as $contact) {
2011-08-06 22:32:06 +02:00
$cards[] = array(
'id' => $contact['id'],
//'carddata' => $i['carddata'],
'size' => strlen($contact['carddata']),
'etag' => '"' . md5($contact['carddata']) . '"',
'uri' => urlencode($contact['uri']),
'lastmodified' => $contact['lastmodified'] );
2011-08-06 22:32:06 +02:00
}
return $cards;
}
2011-08-06 22:32:06 +02:00
/**
* Returns a specfic card
*
* @param mixed $addressbookid
* @param string $carduri
* @return array
*/
public function getCard($addressbookid, $carduri) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
2014-01-22 22:22:58 +01:00
try {
$contact = $backend->getContact($id, array('uri' => urldecode($carduri)));
} catch(\Exception $e) {
throw new \Sabre_DAV_Exception_NotFound($e->getMessage());
}
if(is_array($contact) ) {
$contact['etag'] = '"' . md5($contact['carddata']) . '"';
return $contact;
}
throw new \Sabre_DAV_Exception('Error retrieving the card');
2011-08-06 22:32:06 +02:00
}
/**
* Creates a new card
*
2013-03-12 09:15:40 +01:00
* We don't return an Etag as the carddata can have been modified
* by Plugin::validate()
*
* @see Plugin::validate()
2011-08-06 22:32:06 +02:00
* @param mixed $addressbookid
* @param string $carduri
* @param string $carddata
2013-03-12 09:15:40 +01:00
* @return string|null
2011-08-06 22:32:06 +02:00
*/
public function createCard($addressbookid, $carduri, $carddata) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$backend->createContact($id, $carddata, array('uri' => $carduri));
2011-08-06 22:32:06 +02:00
}
/**
* Updates a card
*
* @param mixed $addressbookid
* @param string $carduri
* @param string $carddata
2013-03-12 09:15:40 +01:00
* @return null
2011-08-06 22:32:06 +02:00
*/
public function updateCard($addressbookid, $carduri, $carddata) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$backend->updateContact($id, array('uri' => $carduri,), $carddata);
2011-08-06 22:32:06 +02:00
}
/**
* Deletes a card
*
* @param mixed $addressbookid
* @param string $carduri
* @return bool
*/
public function deleteCard($addressbookid, $carduri) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
2013-10-03 18:09:19 +02:00
return $backend->deleteContact($id, array('uri' => $carduri));
2011-08-06 22:32:06 +02:00
}
/**
* @brief gets the userid from a principal path
* @param string $principaluri
* @return string
*/
public function userIDByPrincipal($principaluri) {
list(, $userid) = \Sabre_DAV_URLUtil::splitPath($principaluri);
return $userid;
}
2013-04-07 22:28:14 +02:00
/**
* Get the backend for an address book
*
* @param mixed $addressbookid
* @return array(string, \OCA\Contacts\Backend\AbstractBackend)
2013-04-07 22:28:14 +02:00
*/
public function getBackendForAddressBook($addressbookid) {
2013-09-04 20:25:23 +02:00
list($backendName, $id) = explode('::', $addressbookid);
$app = new Contacts\App();
$backend = $app->getBackend($backendName);
if($backend->name === $backendName && $backend->hasAddressBook($id)) {
return array($id, $backend);
}
throw new \Sabre_DAV_Exception_NotFound('Backend not found: ' . $addressbookid);
}
2011-08-06 22:32:06 +02:00
}