connector_sabre.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2011 Jakob Sack mail@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This CardDAV backend uses PDO to store addressbooks
  24. */
  25. class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
  26. /**
  27. * Returns the list of addressbooks for a specific user.
  28. *
  29. * @param string $principaluri
  30. * @return array
  31. */
  32. public function getAddressBooksForUser($principaluri) {
  33. $data = OC_Contacts_Addressbook::allWherePrincipalURIIs($principaluri);
  34. $addressbooks = array();
  35. foreach($data as $i) {
  36. $addressbooks[] = array(
  37. 'id' => $i['id'],
  38. 'uri' => $i['uri'],
  39. 'principaluri' => 'principals/'.$i['userid'],
  40. '{DAV:}displayname' => $i['displayname'],
  41. '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $i['description'],
  42. '{http://calendarserver.org/ns/}getctag' => $i['ctag'],
  43. );
  44. }
  45. return $addressbooks;
  46. }
  47. /**
  48. * Updates an addressbook's properties
  49. *
  50. * See Sabre_DAV_IProperties for a description of the mutations array, as
  51. * well as the return value.
  52. *
  53. * @param mixed $addressbookid
  54. * @param array $mutations
  55. * @see Sabre_DAV_IProperties::updateProperties
  56. * @return bool|array
  57. */
  58. public function updateAddressBook($addressbookid, array $mutations) {
  59. $name = null;
  60. $description = null;
  61. foreach($mutations as $property=>$newvalue) {
  62. switch($property) {
  63. case '{DAV:}displayname' :
  64. $name = $newvalue;
  65. break;
  66. case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
  67. $description = $newvalue;
  68. break;
  69. default :
  70. // If any unsupported values were being updated, we must
  71. // let the entire request fail.
  72. return false;
  73. }
  74. }
  75. OC_Contacts_Addressbook::edit($addressbookid,$name,$description);
  76. return true;
  77. }
  78. /**
  79. * Creates a new address book
  80. *
  81. * @param string $principaluri
  82. * @param string $url Just the 'basename' of the url.
  83. * @param array $properties
  84. * @return void
  85. */
  86. public function createAddressBook($principaluri, $url, array $properties) {
  87. $displayname = null;
  88. $description = null;
  89. foreach($properties as $property=>$newvalue) {
  90. switch($property) {
  91. case '{DAV:}displayname' :
  92. $displayname = $newvalue;
  93. break;
  94. case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
  95. $description = $newvalue;
  96. break;
  97. default :
  98. throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
  99. }
  100. }
  101. OC_Contacts_Addressbook::addFromDAVData($principaluri,$url,$name,$description);
  102. }
  103. /**
  104. * Deletes an entire addressbook and all its contents
  105. *
  106. * @param int $addressbookid
  107. * @return void
  108. */
  109. public function deleteAddressBook($addressbookid) {
  110. OC_Contacts_Addressbook::delete($addressbookid);
  111. }
  112. /**
  113. * Returns all cards for a specific addressbook id.
  114. *
  115. * @param mixed $addressbookid
  116. * @return array
  117. */
  118. public function getCards($addressbookid) {
  119. $data = OC_Contacts_VCard::all($addressbookid);
  120. $cards = array();
  121. foreach($data as $i){
  122. $cards[] = array(
  123. 'id' => $i['id'],
  124. 'carddata' => $i['carddata'],
  125. 'uri' => $i['uri'],
  126. 'lastmodified' => $i['lastmodified'] );
  127. }
  128. return $cards;
  129. }
  130. /**
  131. * Returns a specfic card
  132. *
  133. * @param mixed $addressbookid
  134. * @param string $carduri
  135. * @return array
  136. */
  137. public function getCard($addressbookid, $carduri) {
  138. return OC_Contacts_VCard::findWhereDAVDataIs($addressbookid,$carduri);
  139. }
  140. /**
  141. * Creates a new card
  142. *
  143. * @param mixed $addressbookid
  144. * @param string $carduri
  145. * @param string $carddata
  146. * @return bool
  147. */
  148. public function createCard($addressbookid, $carduri, $carddata) {
  149. OC_Contacts_VCard::addFromDAVData($addressbookid, $carduri, $carddata);
  150. return true;
  151. }
  152. /**
  153. * Updates a card
  154. *
  155. * @param mixed $addressbookid
  156. * @param string $carduri
  157. * @param string $carddata
  158. * @return bool
  159. */
  160. public function updateCard($addressbookid, $carduri, $carddata) {
  161. return OC_Contacts_VCard::editFromDAVData($addressbookid, $carduri, $carddata);
  162. }
  163. /**
  164. * Deletes a card
  165. *
  166. * @param mixed $addressbookid
  167. * @param string $carduri
  168. * @return bool
  169. */
  170. public function deleteCard($addressbookid, $carduri) {
  171. return OC_Contacts_VCard::deleteFromDAVData($addressbookid, $carduri);
  172. }
  173. }