Blame view

sources/3rdparty/sabre/dav/lib/Sabre/CardDAV/Plugin.php 21.9 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
3
4
5
  namespace Sabre\CardDAV;
  
  use Sabre\DAV;
  use Sabre\DAVACL;
03e52840d   Kload   Init
6
7
8
9
10
11
12
  use Sabre\VObject;
  
  /**
   * CardDAV plugin
   *
   * The CardDAV plugin adds CardDAV functionality to the WebDAV server
   *
6d9380f96   Cédric Dupont   Update sources OC...
13
14
15
   * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
   * @license http://sabre.io/license/ Modified BSD License
03e52840d   Kload   Init
16
   */
6d9380f96   Cédric Dupont   Update sources OC...
17
  class Plugin extends DAV\ServerPlugin {
03e52840d   Kload   Init
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
      /**
       * Url to the addressbooks
       */
      const ADDRESSBOOK_ROOT = 'addressbooks';
  
      /**
       * xml namespace for CardDAV elements
       */
      const NS_CARDDAV = 'urn:ietf:params:xml:ns:carddav';
  
      /**
       * Add urls to this property to have them automatically exposed as
       * 'directories' to the user.
       *
       * @var array
       */
      public $directories = array();
  
      /**
       * Server class
       *
6d9380f96   Cédric Dupont   Update sources OC...
40
       * @var Sabre\DAV\Server
03e52840d   Kload   Init
41
42
43
44
45
46
       */
      protected $server;
  
      /**
       * Initializes the plugin
       *
6d9380f96   Cédric Dupont   Update sources OC...
47
       * @param DAV\Server $server
03e52840d   Kload   Init
48
49
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
50
      public function initialize(DAV\Server $server) {
03e52840d   Kload   Init
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  
          /* Events */
          $server->subscribeEvent('beforeGetProperties', array($this, 'beforeGetProperties'));
          $server->subscribeEvent('afterGetProperties',  array($this, 'afterGetProperties'));
          $server->subscribeEvent('updateProperties', array($this, 'updateProperties'));
          $server->subscribeEvent('report', array($this,'report'));
          $server->subscribeEvent('onHTMLActionsPanel', array($this,'htmlActionsPanel'));
          $server->subscribeEvent('onBrowserPostAction', array($this,'browserPostAction'));
          $server->subscribeEvent('beforeWriteContent', array($this, 'beforeWriteContent'));
          $server->subscribeEvent('beforeCreateFile', array($this, 'beforeCreateFile'));
  
          /* Namespaces */
          $server->xmlNamespaces[self::NS_CARDDAV] = 'card';
  
          /* Mapping Interfaces to {DAV:}resourcetype values */
6d9380f96   Cédric Dupont   Update sources OC...
66
67
          $server->resourceTypeMapping['Sabre\\CardDAV\\IAddressBook'] = '{' . self::NS_CARDDAV . '}addressbook';
          $server->resourceTypeMapping['Sabre\\CardDAV\\IDirectory'] = '{' . self::NS_CARDDAV . '}directory';
03e52840d   Kload   Init
68
69
70
71
72
73
  
          /* Adding properties that may never be changed */
          $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}supported-address-data';
          $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}max-resource-size';
          $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}addressbook-home-set';
          $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}supported-collation-set';
6d9380f96   Cédric Dupont   Update sources OC...
74
          $server->propertyMap['{http://calendarserver.org/ns/}me-card'] = 'Sabre\\DAV\\Property\\Href';
03e52840d   Kload   Init
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
  
          $this->server = $server;
  
      }
  
      /**
       * Returns a list of supported features.
       *
       * This is used in the DAV: header in the OPTIONS and PROPFIND requests.
       *
       * @return array
       */
      public function getFeatures() {
  
          return array('addressbook');
  
      }
  
      /**
       * Returns a list of reports this plugin supports.
       *
       * This will be used in the {DAV:}supported-report-set property.
       * Note that you still need to subscribe to the 'report' event to actually
       * implement them
       *
       * @param string $uri
       * @return array
       */
      public function getSupportedReportSet($uri) {
  
          $node = $this->server->tree->getNodeForPath($uri);
6d9380f96   Cédric Dupont   Update sources OC...
106
          if ($node instanceof IAddressBook || $node instanceof ICard) {
03e52840d   Kload   Init
107
108
109
110
111
112
113
114
115
116
117
118
119
120
              return array(
                   '{' . self::NS_CARDDAV . '}addressbook-multiget',
                   '{' . self::NS_CARDDAV . '}addressbook-query',
              );
          }
          return array();
  
      }
  
  
      /**
       * Adds all CardDAV-specific properties
       *
       * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
121
       * @param DAV\INode $node
03e52840d   Kload   Init
122
123
124
125
       * @param array $requestedProperties
       * @param array $returnedProperties
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
126
      public function beforeGetProperties($path, DAV\INode $node, array &$requestedProperties, array &$returnedProperties) {
03e52840d   Kload   Init
127

6d9380f96   Cédric Dupont   Update sources OC...
128
          if ($node instanceof DAVACL\IPrincipal) {
03e52840d   Kload   Init
129
130
131
132
133
134
135
  
              // calendar-home-set property
              $addHome = '{' . self::NS_CARDDAV . '}addressbook-home-set';
              if (in_array($addHome,$requestedProperties)) {
                  $principalId = $node->getName();
                  $addressbookHomePath = self::ADDRESSBOOK_ROOT . '/' . $principalId . '/';
                  unset($requestedProperties[array_search($addHome, $requestedProperties)]);
6d9380f96   Cédric Dupont   Update sources OC...
136
                  $returnedProperties[200][$addHome] = new DAV\Property\Href($addressbookHomePath);
03e52840d   Kload   Init
137
138
139
140
141
              }
  
              $directories = '{' . self::NS_CARDDAV . '}directory-gateway';
              if ($this->directories && in_array($directories, $requestedProperties)) {
                  unset($requestedProperties[array_search($directories, $requestedProperties)]);
6d9380f96   Cédric Dupont   Update sources OC...
142
                  $returnedProperties[200][$directories] = new DAV\Property\HrefList($this->directories);
03e52840d   Kload   Init
143
144
145
              }
  
          }
6d9380f96   Cédric Dupont   Update sources OC...
146
          if ($node instanceof ICard) {
03e52840d   Kload   Init
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  
              // The address-data property is not supposed to be a 'real'
              // property, but in large chunks of the spec it does act as such.
              // Therefore we simply expose it as a property.
              $addressDataProp = '{' . self::NS_CARDDAV . '}address-data';
              if (in_array($addressDataProp, $requestedProperties)) {
                  unset($requestedProperties[$addressDataProp]);
                  $val = $node->get();
                  if (is_resource($val))
                      $val = stream_get_contents($val);
  
                  $returnedProperties[200][$addressDataProp] = $val;
  
              }
          }
6d9380f96   Cédric Dupont   Update sources OC...
162
          if ($node instanceof UserAddressBooks) {
03e52840d   Kload   Init
163
164
165
166
167
168
  
              $meCardProp = '{http://calendarserver.org/ns/}me-card';
              if (in_array($meCardProp, $requestedProperties)) {
  
                  $props = $this->server->getProperties($node->getOwner(), array('{http://sabredav.org/ns}vcard-url'));
                  if (isset($props['{http://sabredav.org/ns}vcard-url'])) {
6d9380f96   Cédric Dupont   Update sources OC...
169
                      $returnedProperties[200][$meCardProp] = new DAV\Property\Href(
03e52840d   Kload   Init
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
                          $props['{http://sabredav.org/ns}vcard-url']
                      );
                      $pos = array_search($meCardProp, $requestedProperties);
                      unset($requestedProperties[$pos]);
  
                  }
  
              }
  
          }
  
      }
  
      /**
       * This event is triggered when a PROPPATCH method is executed
       *
       * @param array $mutations
       * @param array $result
6d9380f96   Cédric Dupont   Update sources OC...
188
       * @param DAV\INode $node
03e52840d   Kload   Init
189
190
       * @return bool
       */
6d9380f96   Cédric Dupont   Update sources OC...
191
      public function updateProperties(&$mutations, &$result, DAV\INode $node) {
03e52840d   Kload   Init
192

6d9380f96   Cédric Dupont   Update sources OC...
193
          if (!$node instanceof UserAddressBooks) {
03e52840d   Kload   Init
194
195
196
197
198
199
200
201
202
203
204
              return true;
          }
  
          $meCard = '{http://calendarserver.org/ns/}me-card';
  
          // The only property we care about
          if (!isset($mutations[$meCard]))
              return true;
  
          $value = $mutations[$meCard];
          unset($mutations[$meCard]);
6d9380f96   Cédric Dupont   Update sources OC...
205
          if ($value instanceof DAV\Property\IHref) {
03e52840d   Kload   Init
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
              $value = $value->getHref();
              $value = $this->server->calculateUri($value);
          } elseif (!is_null($value)) {
              $result[400][$meCard] = null;
              return false;
          }
  
          $innerResult = $this->server->updateProperties(
              $node->getOwner(),
              array(
                  '{http://sabredav.org/ns}vcard-url' => $value,
              )
          );
  
          $closureResult = false;
          foreach($innerResult as $status => $props) {
              if (is_array($props) && array_key_exists('{http://sabredav.org/ns}vcard-url', $props)) {
                  $result[$status][$meCard] = null;
                  $closureResult = ($status>=200 && $status<300);
              }
  
          }
  
          return $result;
  
      }
  
      /**
       * This functions handles REPORT requests specific to CardDAV
       *
       * @param string $reportName
6d9380f96   Cédric Dupont   Update sources OC...
237
       * @param \DOMNode $dom
03e52840d   Kload   Init
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
       * @return bool
       */
      public function report($reportName,$dom) {
  
          switch($reportName) {
              case '{'.self::NS_CARDDAV.'}addressbook-multiget' :
                  $this->addressbookMultiGetReport($dom);
                  return false;
              case '{'.self::NS_CARDDAV.'}addressbook-query' :
                  $this->addressBookQueryReport($dom);
                  return false;
              default :
                  return;
  
          }
  
  
      }
  
      /**
       * This function handles the addressbook-multiget REPORT.
       *
       * This report is used by the client to fetch the content of a series
       * of urls. Effectively avoiding a lot of redundant requests.
       *
6d9380f96   Cédric Dupont   Update sources OC...
263
       * @param \DOMNode $dom
03e52840d   Kload   Init
264
265
266
       * @return void
       */
      public function addressbookMultiGetReport($dom) {
6d9380f96   Cédric Dupont   Update sources OC...
267
          $properties = array_keys(DAV\XMLUtil::parseProperties($dom->firstChild));
03e52840d   Kload   Init
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
  
          $hrefElems = $dom->getElementsByTagNameNS('urn:DAV','href');
          $propertyList = array();
  
          foreach($hrefElems as $elem) {
  
              $uri = $this->server->calculateUri($elem->nodeValue);
              list($propertyList[]) = $this->server->getPropertiesForPath($uri,$properties);
  
          }
  
          $prefer = $this->server->getHTTPPRefer();
  
          $this->server->httpResponse->sendStatus(207);
          $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
          $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
          $this->server->httpResponse->sendBody($this->server->generateMultiStatus($propertyList, $prefer['return-minimal']));
  
      }
  
      /**
       * This method is triggered before a file gets updated with new content.
       *
       * This plugin uses this method to ensure that Card nodes receive valid
       * vcard data.
       *
       * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
295
       * @param DAV\IFile $node
03e52840d   Kload   Init
296
297
298
       * @param resource $data
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
299
      public function beforeWriteContent($path, DAV\IFile $node, &$data) {
03e52840d   Kload   Init
300

6d9380f96   Cédric Dupont   Update sources OC...
301
          if (!$node instanceof ICard)
03e52840d   Kload   Init
302
303
304
305
306
307
308
309
310
311
312
313
314
315
              return;
  
          $this->validateVCard($data);
  
      }
  
      /**
       * This method is triggered before a new file is created.
       *
       * This plugin uses this method to ensure that Card nodes receive valid
       * vcard data.
       *
       * @param string $path
       * @param resource $data
6d9380f96   Cédric Dupont   Update sources OC...
316
       * @param DAV\ICollection $parentNode
03e52840d   Kload   Init
317
318
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
319
      public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode) {
03e52840d   Kload   Init
320

6d9380f96   Cédric Dupont   Update sources OC...
321
          if (!$parentNode instanceof IAddressBook)
03e52840d   Kload   Init
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
              return;
  
          $this->validateVCard($data);
  
      }
  
      /**
       * Checks if the submitted iCalendar data is in fact, valid.
       *
       * An exception is thrown if it's not.
       *
       * @param resource|string $data
       * @return void
       */
      protected function validateVCard(&$data) {
  
          // If it's a stream, we convert it to a string first.
          if (is_resource($data)) {
              $data = stream_get_contents($data);
          }
  
          // Converting the data to unicode, if needed.
6d9380f96   Cédric Dupont   Update sources OC...
344
          $data = DAV\StringUtil::ensureUTF8($data);
03e52840d   Kload   Init
345
346
347
348
349
350
  
          try {
  
              $vobj = VObject\Reader::read($data);
  
          } catch (VObject\ParseException $e) {
6d9380f96   Cédric Dupont   Update sources OC...
351
              throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid vcard data. Parse error: ' . $e->getMessage());
03e52840d   Kload   Init
352
353
354
355
  
          }
  
          if ($vobj->name !== 'VCARD') {
6d9380f96   Cédric Dupont   Update sources OC...
356
              throw new DAV\Exception\UnsupportedMediaType('This collection can only support vcard objects.');
03e52840d   Kload   Init
357
358
359
          }
  
          if (!isset($vobj->UID)) {
6d9380f96   Cédric Dupont   Update sources OC...
360
361
362
              // No UID in vcards is invalid, but we'll just add it in anyway.
              $vobj->add('UID', DAV\UUIDUtil::getUUID());
              $data = $vobj->serialize();
03e52840d   Kload   Init
363
364
365
366
367
368
369
370
371
372
373
          }
  
      }
  
  
      /**
       * This function handles the addressbook-query REPORT
       *
       * This report is used by the client to filter an addressbook based on a
       * complex query.
       *
6d9380f96   Cédric Dupont   Update sources OC...
374
       * @param \DOMNode $dom
03e52840d   Kload   Init
375
376
377
       * @return void
       */
      protected function addressbookQueryReport($dom) {
6d9380f96   Cédric Dupont   Update sources OC...
378
          $query = new AddressBookQueryParser($dom);
03e52840d   Kload   Init
379
380
381
382
383
384
385
386
387
388
389
390
391
392
          $query->parse();
  
          $depth = $this->server->getHTTPDepth(0);
  
          if ($depth==0) {
              $candidateNodes = array(
                  $this->server->tree->getNodeForPath($this->server->getRequestUri())
              );
          } else {
              $candidateNodes = $this->server->tree->getChildren($this->server->getRequestUri());
          }
  
          $validNodes = array();
          foreach($candidateNodes as $node) {
6d9380f96   Cédric Dupont   Update sources OC...
393
              if (!$node instanceof ICard)
03e52840d   Kload   Init
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
                  continue;
  
              $blob = $node->get();
              if (is_resource($blob)) {
                  $blob = stream_get_contents($blob);
              }
  
              if (!$this->validateFilters($blob, $query->filters, $query->test)) {
                  continue;
              }
  
              $validNodes[] = $node;
  
              if ($query->limit && $query->limit <= count($validNodes)) {
                  // We hit the maximum number of items, we can stop now.
                  break;
              }
  
          }
  
          $result = array();
          foreach($validNodes as $validNode) {
  
              if ($depth==0) {
                  $href = $this->server->getRequestUri();
              } else {
                  $href = $this->server->getRequestUri() . '/' . $validNode->getName();
              }
  
              list($result[]) = $this->server->getPropertiesForPath($href, $query->requestedProperties, 0);
  
          }
  
          $prefer = $this->server->getHTTPPRefer();
  
          $this->server->httpResponse->sendStatus(207);
          $this->server->httpResponse->setHeader('Content-Type','application/xml; charset=utf-8');
          $this->server->httpResponse->setHeader('Vary','Brief,Prefer');
          $this->server->httpResponse->sendBody($this->server->generateMultiStatus($result, $prefer['return-minimal']));
  
      }
  
      /**
       * Validates if a vcard makes it throught a list of filters.
       *
       * @param string $vcardData
       * @param array $filters
       * @param string $test anyof or allof (which means OR or AND)
       * @return bool
       */
      public function validateFilters($vcardData, array $filters, $test) {
  
          $vcard = VObject\Reader::read($vcardData);
  
          if (!$filters) return true;
  
          foreach($filters as $filter) {
  
              $isDefined = isset($vcard->{$filter['name']});
              if ($filter['is-not-defined']) {
                  if ($isDefined) {
                      $success = false;
                  } else {
                      $success = true;
                  }
              } elseif ((!$filter['param-filters'] && !$filter['text-matches']) || !$isDefined) {
  
                  // We only need to check for existence
                  $success = $isDefined;
  
              } else {
  
                  $vProperties = $vcard->select($filter['name']);
  
                  $results = array();
                  if ($filter['param-filters']) {
                      $results[] = $this->validateParamFilters($vProperties, $filter['param-filters'], $filter['test']);
                  }
                  if ($filter['text-matches']) {
                      $texts = array();
                      foreach($vProperties as $vProperty)
6d9380f96   Cédric Dupont   Update sources OC...
475
                          $texts[] = $vProperty->getValue();
03e52840d   Kload   Init
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  
                      $results[] = $this->validateTextMatches($texts, $filter['text-matches'], $filter['test']);
                  }
  
                  if (count($results)===1) {
                      $success = $results[0];
                  } else {
                      if ($filter['test'] === 'anyof') {
                          $success = $results[0] || $results[1];
                      } else {
                          $success = $results[0] && $results[1];
                      }
                  }
  
              } // else
  
              // There are two conditions where we can already determine whether
              // or not this filter succeeds.
              if ($test==='anyof' && $success) {
                  return true;
              }
              if ($test==='allof' && !$success) {
                  return false;
              }
  
          } // foreach
  
          // If we got all the way here, it means we haven't been able to
          // determine early if the test failed or not.
          //
          // This implies for 'anyof' that the test failed, and for 'allof' that
          // we succeeded. Sounds weird, but makes sense.
          return $test==='allof';
  
      }
  
      /**
       * Validates if a param-filter can be applied to a specific property.
       *
       * @todo currently we're only validating the first parameter of the passed
       *       property. Any subsequence parameters with the same name are
       *       ignored.
       * @param array $vProperties
       * @param array $filters
       * @param string $test
       * @return bool
       */
      protected function validateParamFilters(array $vProperties, array $filters, $test) {
  
          foreach($filters as $filter) {
  
              $isDefined = false;
              foreach($vProperties as $vProperty) {
                  $isDefined = isset($vProperty[$filter['name']]);
                  if ($isDefined) break;
              }
  
              if ($filter['is-not-defined']) {
                  if ($isDefined) {
                      $success = false;
                  } else {
                      $success = true;
                  }
  
              // If there's no text-match, we can just check for existence
              } elseif (!$filter['text-match'] || !$isDefined) {
  
                  $success = $isDefined;
  
              } else {
  
                  $success = false;
                  foreach($vProperties as $vProperty) {
                      // If we got all the way here, we'll need to validate the
                      // text-match filter.
6d9380f96   Cédric Dupont   Update sources OC...
551
                      $success = DAV\StringUtil::textMatch($vProperty[$filter['name']]->getValue(), $filter['text-match']['value'], $filter['text-match']['collation'], $filter['text-match']['match-type']);
03e52840d   Kload   Init
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
                      if ($success) break;
                  }
                  if ($filter['text-match']['negate-condition']) {
                      $success = !$success;
                  }
  
              } // else
  
              // There are two conditions where we can already determine whether
              // or not this filter succeeds.
              if ($test==='anyof' && $success) {
                  return true;
              }
              if ($test==='allof' && !$success) {
                  return false;
              }
  
          }
  
          // If we got all the way here, it means we haven't been able to
          // determine early if the test failed or not.
          //
          // This implies for 'anyof' that the test failed, and for 'allof' that
          // we succeeded. Sounds weird, but makes sense.
          return $test==='allof';
  
      }
  
      /**
       * Validates if a text-filter can be applied to a specific property.
       *
       * @param array $texts
       * @param array $filters
       * @param string $test
       * @return bool
       */
      protected function validateTextMatches(array $texts, array $filters, $test) {
  
          foreach($filters as $filter) {
  
              $success = false;
              foreach($texts as $haystack) {
6d9380f96   Cédric Dupont   Update sources OC...
594
                  $success = DAV\StringUtil::textMatch($haystack, $filter['value'], $filter['collation'], $filter['match-type']);
03e52840d   Kload   Init
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
  
                  // Breaking on the first match
                  if ($success) break;
              }
              if ($filter['negate-condition']) {
                  $success = !$success;
              }
  
              if ($success && $test==='anyof')
                  return true;
  
              if (!$success && $test=='allof')
                  return false;
  
  
          }
  
          // If we got all the way here, it means we haven't been able to
          // determine early if the test failed or not.
          //
          // This implies for 'anyof' that the test failed, and for 'allof' that
          // we succeeded. Sounds weird, but makes sense.
          return $test==='allof';
  
      }
  
      /**
       * This event is triggered after webdav-properties have been retrieved.
       *
       * @return bool
       */
      public function afterGetProperties($uri, &$properties) {
  
          // If the request was made using the SOGO connector, we must rewrite
          // the content-type property. By default SabreDAV will send back
          // text/x-vcard; charset=utf-8, but for SOGO we must strip that last
          // part.
          if (!isset($properties[200]['{DAV:}getcontenttype']))
              return;
  
          if (strpos($this->server->httpRequest->getHeader('User-Agent'),'Thunderbird')===false) {
              return;
          }
  
          if (strpos($properties[200]['{DAV:}getcontenttype'],'text/x-vcard')===0) {
              $properties[200]['{DAV:}getcontenttype'] = 'text/x-vcard';
          }
  
      }
  
      /**
       * This method is used to generate HTML output for the
6d9380f96   Cédric Dupont   Update sources OC...
647
       * Sabre\DAV\Browser\Plugin. This allows us to generate an interface users
03e52840d   Kload   Init
648
649
       * can use to create new calendars.
       *
6d9380f96   Cédric Dupont   Update sources OC...
650
       * @param DAV\INode $node
03e52840d   Kload   Init
651
652
653
       * @param string $output
       * @return bool
       */
6d9380f96   Cédric Dupont   Update sources OC...
654
      public function htmlActionsPanel(DAV\INode $node, &$output) {
03e52840d   Kload   Init
655

6d9380f96   Cédric Dupont   Update sources OC...
656
          if (!$node instanceof UserAddressBooks)
03e52840d   Kload   Init
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
              return;
  
          $output.= '<tr><td colspan="2"><form method="post" action="">
              <h3>Create new address book</h3>
              <input type="hidden" name="sabreAction" value="mkaddressbook" />
              <label>Name (uri):</label> <input type="text" name="name" /><br />
              <label>Display name:</label> <input type="text" name="{DAV:}displayname" /><br />
              <input type="submit" value="create" />
              </form>
              </td></tr>';
  
          return false;
  
      }
  
      /**
       * This method allows us to intercept the 'mkcalendar' sabreAction. This
       * action enables the user to create new calendars from the browser plugin.
       *
       * @param string $uri
       * @param string $action
       * @param array $postVars
       * @return bool
       */
      public function browserPostAction($uri, $action, array $postVars) {
  
          if ($action!=='mkaddressbook')
              return;
  
          $resourceType = array('{DAV:}collection','{urn:ietf:params:xml:ns:carddav}addressbook');
          $properties = array();
          if (isset($postVars['{DAV:}displayname'])) {
              $properties['{DAV:}displayname'] = $postVars['{DAV:}displayname'];
          }
          $this->server->createCollection($uri . '/' . $postVars['name'],$resourceType,$properties);
          return false;
  
      }
  
  }