Blame view

sources/apps/contacts/lib/carddav/backend.php 8.18 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
4
5
  <?php
  /**
   * ownCloud - Addressbook
   *
   * @author Jakob Sack
6d9380f96   Cédric Dupont   Update sources OC...
6
   * @author Thomas Tanghus
d1bafeea1   Kload   [fix] Upgrade to ...
7
   * @copyright 2011 Jakob Sack mail@jakobsack.de
6d9380f96   Cédric Dupont   Update sources OC...
8
   * @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net)
d1bafeea1   Kload   [fix] Upgrade to ...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   *
   * 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\CardDAV;
  
  use OCA\Contacts;
6d9380f96   Cédric Dupont   Update sources OC...
28
29
30
31
32
33
34
35
  /**
   * This class exchanges data between SabreDav and the Address book backends.
   *
   * Address book IDs are a combination of the backend name and the ID it has
   * in that backend. For your own address books it can be e.g 'local::1' for
   * an address book shared with you it could be 'shared::2' an so forth.
   */
  class Backend extends \Sabre\CardDAV\Backend\AbstractBackend {
d1bafeea1   Kload   [fix] Upgrade to ...
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
  
  	public function __construct($backends) {
  		$this->backends = $backends;
  	}
  
  	/**
  	 * 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();
  			
  			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(
  						'id'  => $backend->name . '::' . $addressBook['id'],
  						'uri' => $addressBook['uri'],
  						'principaluri' => 'principals/'.$addressBook['owner'],
  						'{DAV:}displayname' => $addressBook['displayname'],
6d9380f96   Cédric Dupont   Update sources OC...
66
  						'{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}addressbook-description'
d1bafeea1   Kload   [fix] Upgrade to ...
67
68
  								=> $addressBook['description'],
  						'{http://calendarserver.org/ns/}getctag' => $addressBook['lastmodified'],
6d9380f96   Cédric Dupont   Update sources OC...
69
70
  						'{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' =>
  							new \Sabre\CardDAV\Property\SupportedAddressData(),
d1bafeea1   Kload   [fix] Upgrade to ...
71
72
73
74
75
76
77
78
79
80
81
82
  					);
  				}
  			}
  		}
  
  		return $userAddressBooks;
  	}
  
  
  	/**
  	 * Updates an addressbook's properties
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
83
  	 * See \Sabre\DAV\IProperties for a description of the mutations array, as
d1bafeea1   Kload   [fix] Upgrade to ...
84
85
86
87
  	 * well as the return value.
  	 *
  	 * @param mixed $addressbookid
  	 * @param array $mutations
6d9380f96   Cédric Dupont   Update sources OC...
88
  	 * @see \Sabre\DAV\IProperties::updateProperties
d1bafeea1   Kload   [fix] Upgrade to ...
89
90
91
92
93
94
95
96
97
98
  	 * @return bool|array
  	 */
  	public function updateAddressBook($addressbookid, array $mutations) {
  		$changes = array();
  
  		foreach($mutations as $property=>$newvalue) {
  			switch($property) {
  				case '{DAV:}displayname' :
  					$changes['displayname'] = $newvalue;
  					break;
6d9380f96   Cédric Dupont   Update sources OC...
99
  				case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV
d1bafeea1   Kload   [fix] Upgrade to ...
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
  						. '}addressbook-description' :
  					$changes['description'] = $newvalue;
  					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);
  
  	}
  
  	/**
  	 * Creates a new address book
  	 *
  	 * @param string $principaluri
  	 * @param string $uri Just the 'basename' of the url.
  	 * @param array $properties
  	 * @return void
  	 */
  	public function createAddressBook($principaluri, $uri, array $properties) {
  
  		foreach($properties as $property => $newvalue) {
6d9380f96   Cédric Dupont   Update sources OC...
126

d1bafeea1   Kload   [fix] Upgrade to ...
127
128
129
130
  			switch($property) {
  				case '{DAV:}displayname' :
  					$properties['displayname'] = $newvalue;
  					break;
6d9380f96   Cédric Dupont   Update sources OC...
131
  				case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV
d1bafeea1   Kload   [fix] Upgrade to ...
132
133
134
135
  						. '}addressbook-description' :
  					$properties['description'] = $newvalue;
  					break;
  				default :
6d9380f96   Cédric Dupont   Update sources OC...
136
  					throw new \Sabre\DAV\Exception\BadRequest('Unknown property: '
d1bafeea1   Kload   [fix] Upgrade to ...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  						. $property);
  			}
  
  		}
  
  		$properties['uri'] = $uri;
  
  		$app = new Contacts\App();
  		$backend = $app->getBackend('local');
  
  		$backend->createAddressBook($properties);
  	}
  
  	/**
  	 * Deletes an entire addressbook and all its contents
  	 *
  	 * @param mixed $addressbookid
  	 * @return void
  	 */
  	public function deleteAddressBook($addressbookid) {
  		list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
  		$backend->deleteAddressBook($id);
  	}
  
  	/**
  	 * 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);
  	}
  
  	/**
  	 * 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);
  
  		$cards = array();
  		foreach($contacts as $contact) {
  			$cards[] = array(
  				'id' => $contact['id'],
  				//'carddata' => $i['carddata'],
  				'size' => strlen($contact['carddata']),
  				'etag' => '"' . md5($contact['carddata']) . '"',
  				'uri' => urlencode($contact['uri']),
  				'lastmodified' => $contact['lastmodified'] );
  		}
  
  		return $cards;
  	}
  
  	/**
  	 * Returns a specfic card
  	 *
  	 * @param mixed $addressbookid
  	 * @param string $carduri
  	 * @return array
  	 */
  	public function getCard($addressbookid, $carduri) {
  		list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
  		try {
  			$contact = $backend->getContact($id, array('uri' => urldecode($carduri)));
d1bafeea1   Kload   [fix] Upgrade to ...
207
  		} catch(\Exception $e) {
6d9380f96   Cédric Dupont   Update sources OC...
208
  			//throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
d1bafeea1   Kload   [fix] Upgrade to ...
209
210
211
  			\OCP\Util::writeLog('contacts', __METHOD__.', Exception: '. $e->getMessage(), \OCP\Util::DEBUG);
  			return false;
  		}
6d9380f96   Cédric Dupont   Update sources OC...
212
213
214
215
216
  		if(is_array($contact) ) {
  			$contact['etag'] = '"' . md5($contact['carddata']) . '"';
  			return $contact;
  		}
  		//throw new \Sabre\DAV\Exception('Error retrieving the card');
d1bafeea1   Kload   [fix] Upgrade to ...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  		return false;
  	}
  
  	/**
  	 * Creates a new card
  	 *
  	 * We don't return an Etag as the carddata can have been modified
  	 * by Plugin::validate()
  	 *
  	 * @see Plugin::validate()
  	 * @param mixed $addressbookid
  	 * @param string $carduri
  	 * @param string $carddata
  	 * @return string|null
  	 */
  	public function createCard($addressbookid, $carduri, $carddata) {
  		list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
  		$backend->createContact($id, $carddata, array('uri' => $carduri));
  	}
  
  	/**
  	 * Updates a card
  	 *
  	 * @param mixed $addressbookid
  	 * @param string $carduri
  	 * @param string $carddata
  	 * @return null
  	 */
  	public function updateCard($addressbookid, $carduri, $carddata) {
  		list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
  		$backend->updateContact($id, array('uri' => $carduri,), $carddata);
  	}
  
  	/**
  	 * Deletes a card
  	 *
  	 * @param mixed $addressbookid
  	 * @param string $carduri
  	 * @return bool
  	 */
  	public function deleteCard($addressbookid, $carduri) {
  		list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
  		return $backend->deleteContact($id, array('uri' => $carduri));
  	}
  
  	/**
  	 * @brief gets the userid from a principal path
  	 * @param string $principaluri
  	 * @return string
  	 */
  	public function userIDByPrincipal($principaluri) {
6d9380f96   Cédric Dupont   Update sources OC...
268
  		list(, $userid) = \Sabre\DAV\URLUtil::splitPath($principaluri);
d1bafeea1   Kload   [fix] Upgrade to ...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  		return $userid;
  	}
  
  	/**
  	 * Get the backend for an address book
  	 *
  	 * @param mixed $addressbookid
  	 * @return array(string, \OCA\Contacts\Backend\AbstractBackend)
  	 */
  	public function getBackendForAddressBook($addressbookid) {
  		list($backendName, $id) = explode('::', $addressbookid);
  		$app = new Contacts\App();
  		$backend = $app->getBackend($backendName);
  		if($backend->name === $backendName && $backend->hasAddressBook($id)) {
  			return array($id, $backend);
  		}
6d9380f96   Cédric Dupont   Update sources OC...
285
  		throw new \Sabre\DAV\Exception\NotFound('Backend not found: ' . $addressbookid);
d1bafeea1   Kload   [fix] Upgrade to ...
286
287
  	}
  }