Blame view

sources/apps/contacts/lib/backend/shared.php 3.6 KB
923852aa1   Kload   Official Owncloud...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  <?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;
  
  use OCA\Contacts;
  
  /**
   * Subclass this class for Cantacts backends
   */
  
  class Shared extends Database {
  
  	public $name = 'shared';
  	public $addressbooks = array();
  
  	/**
  	 * Returns the list of addressbooks for a specific user.
  	 *
  	 * @param string $principaluri
  	 * @return array
  	 */
  	public function getAddressBooksForUser(array $options = array()) {
  
  		// workaround for https://github.com/owncloud/core/issues/2814
  		$maybeSharedAddressBook = \OCP\Share::getItemsSharedWith(
  			'addressbook',
  			Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS
  		);
  		foreach($maybeSharedAddressBook as $sharedAddressbook) {
  			if(isset($sharedAddressbook['id'])) {
  				$this->addressbooks[] = $this->getAddressBook($sharedAddressbook['id']);
  			}
  		}
  
  		foreach($this->addressbooks as &$addressBook) {
  			$addressBook['backend'] = $this->name;
  		}
  		return $this->addressbooks;
  	}
  
  	/**
  	 * Returns a specific address book.
  	 *
  	 * @param string $addressbookid
  	 * @param mixed $id Contact ID
  	 * @return mixed
  	 */
  	public function getAddressBook($addressbookid, array $options = array()) {
  		$addressBook = \OCP\Share::getItemSharedWithBySource(
  			'addressbook',
  			$addressbookid,
  			Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS
  		);
  		// Not sure if I'm doing it wrongly, or if its supposed to return
  		// the info in an array?
  		$addressBook = (isset($addressBook['permissions']) ? $addressBook : $addressBook[0]);
  		$addressBook['backend'] = $this->name;
  		return $addressBook;
  	}
  
  	/**
  	 * 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, array $options = array()) {
  
  		$addressBook = $this->getAddressBook($addressbookid);
  		if(!$addressBook) {
  			throw new \Exception('Shared Address Book not found: ' . $addressbookid, 404);
  		}
  		$permissions = $addressBook['permissions'];
  
  		$cards = parent::getContacts($addressbookid, $options);
  
  		foreach($cards as &$card) {
  			$card['permissions'] = $permissions;
  		}
  
  		return $cards;
  	}
  
  	/**
  	 * 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, array $options = array()) {
  		$addressBook = $this->getAddressBook($addressbookid);
  		if(!$addressBook) {
  			throw new \Exception('Shared Address Book not found: ' . $addressbookid, 404);
  		}
  		$permissions = $addressBook['permissions'];
  
  		$card = parent::getContact($addressbookid, $id, $options);
  		$card['permissions'] = $permissions;
  		return $card;
  	}
  }