]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/dav_carddav_backend_private.inc.php
Update t() calls
[friendica-addons.git] / dav / common / dav_carddav_backend_private.inc.php
1 <?php
2
3 use Friendica\Core\L10n;
4
5 class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Common
6 {
7
8         /**
9          * @var null|Sabre_CardDAV_Backend_Std
10          */
11         private static $instance = null;
12
13         /**
14          * @static
15          * @return Sabre_CardDAV_Backend_Std
16          */
17         public static function getInstance() {
18                 if (self::$instance == null) {
19                         self::$instance = new Sabre_CardDAV_Backend_Std();
20                 }
21                 return self::$instance;
22         }
23
24
25         /**
26          * Sets up the object
27          */
28         public function __construct()
29         {
30
31         }
32
33
34         /**
35          * @return int
36          */
37         public function getNamespace()
38         {
39                 return CARDDAV_NAMESPACE_PRIVATE;
40         }
41
42         /**
43          * @static
44          * @return string
45          */
46         public static function getBackendTypeName()
47         {
48                 return L10n::t("Private Addressbooks");
49         }
50
51         /**
52          * Returns the list of addressbooks for a specific user.
53          *
54          * @param string $principalUri
55          * @return array
56          */
57         public function getAddressBooksForUser($principalUri)
58         {
59                 $n = dav_compat_principal2namespace($principalUri);
60                 if ($n["namespace"] != $this->getNamespace()) return array();
61
62                 $addressBooks = array();
63
64                 $books = q("SELECT * FROM %s%saddressbooks WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($n["namespace"]), IntVal($n["namespace_id"]));
65                 foreach ($books as $row) {
66                         if (in_array($row["uri"], $GLOBALS["CARDDAV_PRIVATE_SYSTEM_ADDRESSBOOKS"])) continue;
67
68                         $addressBooks[] = array(
69                                 'id'                                                                => $row['id'],
70                                 'uri'                                                               => $row['uri'],
71                                 'principaluri'                                                      => $principalUri,
72                                 '{DAV:}displayname'                                                 => $row['displayname'],
73                                 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
74                                 '{http://calendarserver.org/ns/}getctag'                            => $row['ctag'],
75                                 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data'  =>
76                                 new Sabre_CardDAV_Property_SupportedAddressData(),
77                         );
78                 }
79
80                 return $addressBooks;
81
82         }
83
84
85         /**
86          * Creates a new address book
87          *
88          * @param string $principalUri
89          * @param string $url Just the 'basename' of the url.
90          * @param array $properties
91          * @throws Sabre_DAV_Exception_BadRequest
92          * @return void
93          */
94         public function createAddressBook($principalUri, $url, array $properties)
95         {
96                 $uid = dav_compat_principal2uid($principalUri);
97
98                 $values = array(
99                         'displayname'  => null,
100                         'description'  => null,
101                         'principaluri' => $principalUri,
102                         'uri'          => $url,
103                 );
104
105                 foreach ($properties as $property=> $newValue) {
106
107                         switch ($property) {
108                                 case '{DAV:}displayname' :
109                                         $values['displayname'] = $newValue;
110                                         break;
111                                 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
112                                         $values['description'] = $newValue;
113                                         break;
114                                 default :
115                                         throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
116                         }
117
118                 }
119
120                 q("INSERT INTO %s%saddressbooks (`uri`, `displayname`, `description`, `namespace`, `namespace_id`, `ctag`) VALUES ('%s', '%s', '%s', %d, %d, 1)",
121                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), CARDDAV_NAMESPACE_PRIVATE, IntVal($uid)
122                 );
123         }
124
125         /**
126          * Deletes an entire addressbook and all its contents
127          *
128          * @param int $addressBookId
129          * @throws Sabre_DAV_Exception_Forbidden
130          * @return void
131          */
132         public function deleteAddressBook($addressBookId)
133         {
134                 q("DELETE FROM %s%saddressbookobjects WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
135                 q("DELETE FROM %s%saddressbooks WHERE `addressbook_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
136         }
137
138         /**
139          * Returns all cards for a specific addressbook id.
140          *
141          * This method should return the following properties for each card:
142          *   * carddata - raw vcard data
143          *   * uri - Some unique url
144          *   * lastmodified - A unix timestamp
145          *
146          * It's recommended to also return the following properties:
147          *   * etag - A unique etag. This must change every time the card changes.
148          *   * size - The size of the card in bytes.
149          *
150          * If these last two properties are provided, less time will be spent
151          * calculating them. If they are specified, you can also ommit carddata.
152          * This may speed up certain requests, especially with large cards.
153          *
154          * @param string $addressbookId
155          * @return array
156          */
157         public function getCards($addressbookId)
158         {
159                 $r = q('SELECT `id`, `carddata`, `uri`, `lastmodified`, `etag`, `size`, `contact` FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `manually_deleted` = 0',
160                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId)
161                 );
162                 if ($r) return $r;
163                 return array();
164         }
165
166         /**
167          * Returns a specfic card.
168          *
169          * The same set of properties must be returned as with getCards. The only
170          * exception is that 'carddata' is absolutely required.
171          *
172          * @param mixed $addressBookId
173          * @param string $cardUri
174          * @throws Sabre_DAV_Exception_NotFound
175          * @return array
176          */
177         public function getCard($addressBookId, $cardUri)
178         {
179                 $x = q("SELECT `id`, `carddata`, `uri`, `lastmodified`, `etag`, `size` FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `uri` = '%s'",
180                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri));
181                 if (count($x) == 0) throw new Sabre_DAV_Exception_NotFound();
182                 return $x[0];
183         }
184
185         /**
186          * Creates a new card.
187          *
188          * The addressbook id will be passed as the first argument. This is the
189          * same id as it is returned from the getAddressbooksForUser method.
190          *
191          * The cardUri is a base uri, and doesn't include the full path. The
192          * cardData argument is the vcard body, and is passed as a string.
193          *
194          * It is possible to return an ETag from this method. This ETag is for the
195          * newly created resource, and must be enclosed with double quotes (that
196          * is, the string itself must contain the double quotes).
197          *
198          * You should only return the ETag if you store the carddata as-is. If a
199          * subsequent GET request on the same card does not have the same body,
200          * byte-by-byte and you did return an ETag here, clients tend to get
201          * confused.
202          *
203          * If you don't return an ETag, you can just return null.
204          *
205          * @param string $addressBookId
206          * @param string $cardUri
207          * @param string $cardData
208          * @throws Sabre_DAV_Exception_Forbidden
209          * @return string
210          */
211         public function createCard($addressBookId, $cardUri, $cardData)
212         {
213                 $etag = md5($cardData);
214                 q("INSERT INTO %s%saddressbookobjects (`carddata`, `uri`, `lastmodified`, `addressbook_id`, `etag`, `size`) VALUES ('%s', '%s', NOW(), %d, '%s', %d)",
215                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($cardUri), IntVal($addressBookId), dbesc($etag), strlen($cardData)
216                 );
217
218                 q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
219
220                 return '"' . $etag . '"';
221
222         }
223
224         /**
225          * Updates a card.
226          *
227          * The addressbook id will be passed as the first argument. This is the
228          * same id as it is returned from the getAddressbooksForUser method.
229          *
230          * The cardUri is a base uri, and doesn't include the full path. The
231          * cardData argument is the vcard body, and is passed as a string.
232          *
233          * It is possible to return an ETag from this method. This ETag should
234          * match that of the updated resource, and must be enclosed with double
235          * quotes (that is: the string itself must contain the actual quotes).
236          *
237          * You should only return the ETag if you store the carddata as-is. If a
238          * subsequent GET request on the same card does not have the same body,
239          * byte-by-byte and you did return an ETag here, clients tend to get
240          * confused.
241          *
242          * If you don't return an ETag, you can just return null.
243          *
244          * @param string $addressBookId
245          * @param string $cardUri
246          * @param string $cardData
247          * @throws Sabre_DAV_Exception_Forbidden
248          * @return string|null
249          */
250         public function updateCard($addressBookId, $cardUri, $cardData)
251         {
252                 $etag = md5($cardData);
253                 q("UPDATE %s%saddressbookobjects SET `carddata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d, `manually_edited` = 1 WHERE `uri` = '%s' AND `addressbook_id` = %d",
254                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($etag), strlen($cardData), dbesc($cardUri), IntVal($addressBookId)
255                 );
256
257                 q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
258
259                 return '"' . $etag . '"';
260         }
261
262         /**
263          * Deletes a card
264          *
265          * @param string $addressBookId
266          * @param string $cardUri
267          * @throws Sabre_DAV_Exception_Forbidden
268          * @return bool
269          */
270         public function deleteCard($addressBookId, $cardUri)
271         {
272                 q("DELETE FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri));
273                 q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
274
275                 return true;
276         }
277 }