2013-03-10 12:34:41 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Backend for Shared contacts
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\Backend;
|
|
|
|
|
2013-03-12 09:15:40 +01:00
|
|
|
use OCA\Contacts;
|
2013-03-10 12:34:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclass this class for Cantacts backends
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Shared extends Database {
|
|
|
|
|
2013-03-15 17:02:33 +01:00
|
|
|
public $name = 'shared';
|
2013-03-10 12:34:41 +01:00
|
|
|
public $addressbooks = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of addressbooks for a specific user.
|
|
|
|
*
|
|
|
|
* @param string $principaluri
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAddressBooksForUser($userid = null) {
|
|
|
|
$userid = $userid ? $userid : $this->userid;
|
|
|
|
|
2013-05-14 19:17:35 +02:00
|
|
|
// workaround for https://github.com/owncloud/core/issues/2814
|
|
|
|
$maybeSharedAddressBook = \OCP\Share::getItemsSharedWith(
|
2013-03-10 12:34:41 +01:00
|
|
|
'addressbook',
|
2013-04-25 01:03:05 +02:00
|
|
|
Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS
|
2013-03-10 12:34:41 +01:00
|
|
|
);
|
2013-05-14 19:17:35 +02:00
|
|
|
foreach($maybeSharedAddressBook as $sharedAddressbook) {
|
|
|
|
if(isset($sharedAddressbook['id'])) {
|
|
|
|
$this->addressbooks[] = $this->getAddressBook($sharedAddressbook['id']);
|
|
|
|
}
|
|
|
|
}
|
2013-03-10 12:34:41 +01:00
|
|
|
|
2013-05-03 04:59:26 +02:00
|
|
|
foreach($this->addressbooks as &$addressBook) {
|
|
|
|
$addressBook['backend'] = $this->name;
|
|
|
|
}
|
2013-03-10 12:34:41 +01:00
|
|
|
return $this->addressbooks;
|
|
|
|
}
|
|
|
|
|
2013-03-12 09:15:40 +01:00
|
|
|
/**
|
|
|
|
* Returns a specific address book.
|
|
|
|
*
|
|
|
|
* @param string $addressbookid
|
|
|
|
* @param mixed $id Contact ID
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getAddressBook($addressbookid) {
|
2013-05-03 04:59:26 +02:00
|
|
|
$addressBook = \OCP\Share::getItemSharedWithBySource(
|
2013-03-12 09:15:40 +01:00
|
|
|
'addressbook',
|
|
|
|
$addressbookid,
|
2013-04-25 01:03:05 +02:00
|
|
|
Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS
|
2013-03-12 09:15:40 +01:00
|
|
|
);
|
2013-03-12 20:00:22 +01:00
|
|
|
// Not sure if I'm doing it wrongly, or if its supposed to return
|
|
|
|
// the info in an array?
|
2013-05-03 04:59:26 +02:00
|
|
|
$addressBook = (isset($addressBook['permissions']) ? $addressBook : $addressBook[0]);
|
2013-05-02 20:41:26 +02:00
|
|
|
$addressBook['backend'] = $this->name;
|
2013-05-03 04:59:26 +02:00
|
|
|
return $addressBook;
|
2013-03-12 09:15:40 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 12:34:41 +01:00
|
|
|
/**
|
|
|
|
* Returns all contacts for a specific addressbook id.
|
|
|
|
*
|
|
|
|
* @param string $addressbookid
|
|
|
|
* @param bool $omitdata Don't fetch the entire carddata or vcard.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getContacts($addressbookid, $limit = null, $offset = null, $omitdata = false) {
|
2013-05-03 04:59:26 +02:00
|
|
|
|
|
|
|
$addressBook = $this->getAddressBook($addressbookid);
|
|
|
|
if(!$addressBook) {
|
|
|
|
throw new \Exception('Shared Address Book not found: ' . $addressbookid, 404);
|
2013-03-10 12:34:41 +01:00
|
|
|
}
|
2013-05-03 04:59:26 +02:00
|
|
|
$permissions = $addressBook['permissions'];
|
2013-03-10 12:34:41 +01:00
|
|
|
|
2013-05-03 04:59:26 +02:00
|
|
|
$cards = parent::getContacts($addressbookid, $limit, $offset, $omitdata);
|
|
|
|
|
|
|
|
foreach($cards as &$card) {
|
|
|
|
$card['permissions'] = $permissions;
|
2013-03-10 12:34:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $cards;
|
|
|
|
}
|
2013-03-12 09:15:40 +01:00
|
|
|
|
2013-05-03 04:59:26 +02:00
|
|
|
/**
|
|
|
|
* Returns a specific contact.
|
|
|
|
*
|
|
|
|
* The $id for Database and Shared backends can be an array containing
|
|
|
|
* either 'id' or 'uri' to be able to play seamlessly with the
|
|
|
|
* CardDAV backend.
|
|
|
|
* @see \Database\getContact
|
|
|
|
*
|
|
|
|
* @param string $addressbookid
|
|
|
|
* @param mixed $id Contact ID
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function getContact($addressbookid, $id, $noCollection = false) {
|
|
|
|
$addressBook = $this->getAddressBook($addressbookid);
|
|
|
|
if(!$addressBook) {
|
|
|
|
throw new \Exception('Shared Address Book not found: ' . $addressbookid, 404);
|
|
|
|
}
|
|
|
|
$permissions = $addressBook['permissions'];
|
|
|
|
|
|
|
|
$card = parent::getContact($addressbookid, $id, $noCollection);
|
|
|
|
$card['permissions'] = $permissions;
|
|
|
|
return $card;
|
|
|
|
}
|
2013-03-13 08:52:00 +01:00
|
|
|
}
|