Blame view

sources/3rdparty/sabre/dav/lib/Sabre/CalDAV/SharingPlugin.php 17.5 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
3
4
  namespace Sabre\CalDAV;
  
  use Sabre\DAV;
03e52840d   Kload   Init
5
6
7
8
9
10
11
  /**
   * This plugin implements support for caldav sharing.
   *
   * This spec is defined at:
   * http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-sharing.txt
   *
   * See:
6d9380f96   Cédric Dupont   Update sources OC...
12
   * Sabre\CalDAV\Backend\SharingSupport for all the documentation.
03e52840d   Kload   Init
13
14
15
16
   *
   * Note: This feature is experimental, and may change in between different
   * SabreDAV versions.
   *
6d9380f96   Cédric Dupont   Update sources OC...
17
18
19
   * @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
20
   */
6d9380f96   Cédric Dupont   Update sources OC...
21
  class SharingPlugin extends DAV\ServerPlugin {
03e52840d   Kload   Init
22
23
24
25
26
27
28
29
30
31
32
33
34
  
      /**
       * These are the various status constants used by sharing-messages.
       */
      const STATUS_ACCEPTED = 1;
      const STATUS_DECLINED = 2;
      const STATUS_DELETED = 3;
      const STATUS_NORESPONSE = 4;
      const STATUS_INVALID = 5;
  
      /**
       * Reference to SabreDAV server object.
       *
6d9380f96   Cédric Dupont   Update sources OC...
35
       * @var Sabre\DAV\Server
03e52840d   Kload   Init
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
       */
      protected $server;
  
      /**
       * This method should return a list of server-features.
       *
       * This is for example 'versioning' and is added to the DAV: header
       * in an OPTIONS response.
       *
       * @return array
       */
      public function getFeatures() {
  
          return array('calendarserver-sharing');
  
      }
  
      /**
       * Returns a plugin name.
       *
       * Using this name other plugins will be able to access other plugins
6d9380f96   Cédric Dupont   Update sources OC...
57
       * using Sabre\DAV\Server::getPlugin
03e52840d   Kload   Init
58
59
60
61
62
63
64
65
66
67
68
69
       *
       * @return string
       */
      public function getPluginName() {
  
          return 'caldav-sharing';
  
      }
  
      /**
       * This initializes the plugin.
       *
6d9380f96   Cédric Dupont   Update sources OC...
70
       * This function is called by Sabre\DAV\Server, after
03e52840d   Kload   Init
71
72
73
74
       * addPlugin is called.
       *
       * This method should set up the required event subscriptions.
       *
6d9380f96   Cédric Dupont   Update sources OC...
75
       * @param DAV\Server $server
03e52840d   Kload   Init
76
77
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
78
      public function initialize(DAV\Server $server) {
03e52840d   Kload   Init
79
80
  
          $this->server = $server;
6d9380f96   Cédric Dupont   Update sources OC...
81
          $server->resourceTypeMapping['Sabre\\CalDAV\\ISharedCalendar'] = '{' . Plugin::NS_CALENDARSERVER . '}shared';
03e52840d   Kload   Init
82
83
84
  
          array_push(
              $this->server->protectedProperties,
6d9380f96   Cédric Dupont   Update sources OC...
85
86
87
              '{' . Plugin::NS_CALENDARSERVER . '}invite',
              '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes',
              '{' . Plugin::NS_CALENDARSERVER . '}shared-url'
03e52840d   Kload   Init
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
          );
  
          $this->server->subscribeEvent('beforeGetProperties', array($this, 'beforeGetProperties'));
          $this->server->subscribeEvent('afterGetProperties', array($this, 'afterGetProperties'));
          $this->server->subscribeEvent('updateProperties', array($this, 'updateProperties'));
          $this->server->subscribeEvent('unknownMethod', array($this,'unknownMethod'));
  
      }
  
      /**
       * This event is triggered when properties are requested for a certain
       * node.
       *
       * This allows us to inject any properties early.
       *
       * @param string $path
6d9380f96   Cédric Dupont   Update sources OC...
104
       * @param DAV\INode $node
03e52840d   Kload   Init
105
106
107
108
       * @param array $requestedProperties
       * @param array $returnedProperties
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
109
      public function beforeGetProperties($path, DAV\INode $node, &$requestedProperties, &$returnedProperties) {
03e52840d   Kload   Init
110

6d9380f96   Cédric Dupont   Update sources OC...
111
112
          if ($node instanceof IShareableCalendar) {
              if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}invite', $requestedProperties))!==false) {
03e52840d   Kload   Init
113
114
  
                  unset($requestedProperties[$index]);
6d9380f96   Cédric Dupont   Update sources OC...
115
116
                  $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}invite'] =
                      new Property\Invite(
03e52840d   Kload   Init
117
118
119
120
121
122
                          $node->getShares()
                      );
  
              }
  
          }
6d9380f96   Cédric Dupont   Update sources OC...
123
124
125
126
  
          if ($node instanceof ISharedCalendar) {
  
              if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}shared-url', $requestedProperties))!==false) {
03e52840d   Kload   Init
127
128
  
                  unset($requestedProperties[$index]);
6d9380f96   Cédric Dupont   Update sources OC...
129
130
                  $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}shared-url'] =
                      new DAV\Property\Href(
03e52840d   Kload   Init
131
132
133
134
                          $node->getSharedUrl()
                      );
  
              }
6d9380f96   Cédric Dupont   Update sources OC...
135
136
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
              // The 'invite' property is slightly different for the 'shared'
              // instance of the calendar, as it also contains the owner
              // information.
              if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}invite', $requestedProperties))!==false) {
  
                  unset($requestedProperties[$index]);
  
                  // Fetching owner information
                  $props = $this->server->getPropertiesForPath($node->getOwner(), array(
                      '{http://sabredav.org/ns}email-address',
                      '{DAV:}displayname',
                  ), 1);
  
                  $ownerInfo = array(
                      'href' => $node->getOwner(),
                  );
  
                  if (isset($props[0][200])) {
  
                      // We're mapping the internal webdav properties to the
                      // elements caldav-sharing expects.
                      if (isset($props[0][200]['{http://sabredav.org/ns}email-address'])) {
                          $ownerInfo['href'] = 'mailto:' . $props[0][200]['{http://sabredav.org/ns}email-address'];
                      }
                      if (isset($props[0][200]['{DAV:}displayname'])) {
                          $ownerInfo['commonName'] = $props[0][200]['{DAV:}displayname'];
                      }
  
                  }
  
                  $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}invite'] =
                      new Property\Invite(
                          $node->getShares(),
                          $ownerInfo
                      );
  
              }
03e52840d   Kload   Init
172
173
174
175
176
177
178
179
180
181
182
183
  
          }
  
      }
  
      /**
       * This method is triggered *after* all properties have been retrieved.
       * This allows us to inject the correct resourcetype for calendars that
       * have been shared.
       *
       * @param string $path
       * @param array $properties
6d9380f96   Cédric Dupont   Update sources OC...
184
       * @param DAV\INode $node
03e52840d   Kload   Init
185
186
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
187
      public function afterGetProperties($path, &$properties, DAV\INode $node) {
03e52840d   Kload   Init
188

6d9380f96   Cédric Dupont   Update sources OC...
189
          if ($node instanceof IShareableCalendar) {
03e52840d   Kload   Init
190
191
192
              if (isset($properties[200]['{DAV:}resourcetype'])) {
                  if (count($node->getShares())>0) {
                      $properties[200]['{DAV:}resourcetype']->add(
6d9380f96   Cédric Dupont   Update sources OC...
193
                          '{' . Plugin::NS_CALENDARSERVER . '}shared-owner'
03e52840d   Kload   Init
194
195
196
                      );
                  }
              }
6d9380f96   Cédric Dupont   Update sources OC...
197
              $propName = '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes';
03e52840d   Kload   Init
198
199
              if (array_key_exists($propName, $properties[404])) {
                  unset($properties[404][$propName]);
6d9380f96   Cédric Dupont   Update sources OC...
200
                  $properties[200][$propName] = new Property\AllowedSharingModes(true,false);
03e52840d   Kload   Init
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
              }
  
          }
  
      }
  
      /**
       * This method is trigged when a user attempts to update a node's
       * properties.
       *
       * A previous draft of the sharing spec stated that it was possible to use
       * PROPPATCH to remove 'shared-owner' from the resourcetype, thus unsharing
       * the calendar.
       *
       * Even though this is no longer in the current spec, we keep this around
       * because OS X 10.7 may still make use of this feature.
       *
       * @param array $mutations
       * @param array $result
6d9380f96   Cédric Dupont   Update sources OC...
220
       * @param DAV\INode $node
03e52840d   Kload   Init
221
222
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
223
      public function updateProperties(array &$mutations, array &$result, DAV\INode $node) {
03e52840d   Kload   Init
224

6d9380f96   Cédric Dupont   Update sources OC...
225
          if (!$node instanceof IShareableCalendar)
03e52840d   Kload   Init
226
227
228
229
230
231
232
              return;
  
          if (!isset($mutations['{DAV:}resourcetype'])) {
              return;
          }
  
          // Only doing something if shared-owner is indeed not in the list.
6d9380f96   Cédric Dupont   Update sources OC...
233
          if($mutations['{DAV:}resourcetype']->is('{' . Plugin::NS_CALENDARSERVER . '}shared-owner')) return;
03e52840d   Kload   Init
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
268
269
270
271
272
273
  
          $shares = $node->getShares();
          $remove = array();
          foreach($shares as $share) {
              $remove[] = $share['href'];
          }
          $node->updateShares(array(), $remove);
  
          // We're marking this update as 200 OK
          $result[200]['{DAV:}resourcetype'] = null;
  
          // Removing it from the mutations list
          unset($mutations['{DAV:}resourcetype']);
  
      }
  
      /**
       * This event is triggered when the server didn't know how to handle a
       * certain request.
       *
       * We intercept this to handle POST requests on calendars.
       *
       * @param string $method
       * @param string $uri
       * @return null|bool
       */
      public function unknownMethod($method, $uri) {
  
          if ($method!=='POST') {
              return;
          }
  
          // Only handling xml
          $contentType = $this->server->httpRequest->getHeader('Content-Type');
          if (strpos($contentType,'application/xml')===false && strpos($contentType,'text/xml')===false)
              return;
  
          // Making sure the node exists
          try {
              $node = $this->server->tree->getNodeForPath($uri);
6d9380f96   Cédric Dupont   Update sources OC...
274
          } catch (DAV\Exception\NotFound $e) {
03e52840d   Kload   Init
275
276
277
278
279
280
281
282
283
284
285
286
287
              return;
          }
  
          $requestBody = $this->server->httpRequest->getBody(true);
  
          // If this request handler could not deal with this POST request, it
          // will return 'null' and other plugins get a chance to handle the
          // request.
          //
          // However, we already requested the full body. This is a problem,
          // because a body can only be read once. This is why we preemptively
          // re-populated the request body with the existing data.
          $this->server->httpRequest->setBody($requestBody);
6d9380f96   Cédric Dupont   Update sources OC...
288
          $dom = DAV\XMLUtil::loadDOMDocument($requestBody);
03e52840d   Kload   Init
289

6d9380f96   Cédric Dupont   Update sources OC...
290
          $documentType = DAV\XMLUtil::toClarkNotation($dom->firstChild);
03e52840d   Kload   Init
291
292
293
294
295
  
          switch($documentType) {
  
              // Dealing with the 'share' document, which modified invitees on a
              // calendar.
6d9380f96   Cédric Dupont   Update sources OC...
296
              case '{' . Plugin::NS_CALENDARSERVER . '}share' :
03e52840d   Kload   Init
297
298
  
                  // We can only deal with IShareableCalendar objects
6d9380f96   Cédric Dupont   Update sources OC...
299
                  if (!$node instanceof IShareableCalendar) {
03e52840d   Kload   Init
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
                      return;
                  }
  
                  // Getting ACL info
                  $acl = $this->server->getPlugin('acl');
  
                  // If there's no ACL support, we allow everything
                  if ($acl) {
                      $acl->checkPrivileges($uri, '{DAV:}write');
                  }
  
                  $mutations = $this->parseShareRequest($dom);
  
                  $node->updateShares($mutations[0], $mutations[1]);
  
                  $this->server->httpResponse->sendStatus(200);
                  // Adding this because sending a response body may cause issues,
                  // and I wanted some type of indicator the response was handled.
                  $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
  
                  // Breaking the event chain
                  return false;
  
              // The invite-reply document is sent when the user replies to an
              // invitation of a calendar share.
6d9380f96   Cédric Dupont   Update sources OC...
325
              case '{'. Plugin::NS_CALENDARSERVER.'}invite-reply' :
03e52840d   Kload   Init
326
327
  
                  // This only works on the calendar-home-root node.
6d9380f96   Cédric Dupont   Update sources OC...
328
                  if (!$node instanceof UserCalendars) {
03e52840d   Kload   Init
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
                      return;
                  }
  
                  // Getting ACL info
                  $acl = $this->server->getPlugin('acl');
  
                  // If there's no ACL support, we allow everything
                  if ($acl) {
                      $acl->checkPrivileges($uri, '{DAV:}write');
                  }
  
                  $message = $this->parseInviteReplyRequest($dom);
  
                  $url = $node->shareReply(
                      $message['href'],
                      $message['status'],
                      $message['calendarUri'],
                      $message['inReplyTo'],
                      $message['summary']
                  );
  
                  $this->server->httpResponse->sendStatus(200);
                  // Adding this because sending a response body may cause issues,
                  // and I wanted some type of indicator the response was handled.
                  $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
  
                  if ($url) {
6d9380f96   Cédric Dupont   Update sources OC...
356
                      $dom = new \DOMDocument('1.0', 'UTF-8');
03e52840d   Kload   Init
357
358
359
360
361
362
363
364
                      $dom->formatOutput = true;
  
                      $root = $dom->createElement('cs:shared-as');
                      foreach($this->server->xmlNamespaces as $namespace => $prefix) {
                          $root->setAttribute('xmlns:' . $prefix, $namespace);
                      }
  
                      $dom->appendChild($root);
6d9380f96   Cédric Dupont   Update sources OC...
365
                      $href = new DAV\Property\Href($url);
03e52840d   Kload   Init
366
367
368
369
370
371
372
373
374
  
                      $href->serialize($this->server, $root);
                      $this->server->httpResponse->setHeader('Content-Type','application/xml');
                      $this->server->httpResponse->sendBody($dom->saveXML());
  
                  }
  
                  // Breaking the event chain
                  return false;
6d9380f96   Cédric Dupont   Update sources OC...
375
              case '{' . Plugin::NS_CALENDARSERVER . '}publish-calendar' :
03e52840d   Kload   Init
376
377
  
                  // We can only deal with IShareableCalendar objects
6d9380f96   Cédric Dupont   Update sources OC...
378
                  if (!$node instanceof IShareableCalendar) {
03e52840d   Kload   Init
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
                      return;
                  }
  
                  // Getting ACL info
                  $acl = $this->server->getPlugin('acl');
  
                  // If there's no ACL support, we allow everything
                  if ($acl) {
                      $acl->checkPrivileges($uri, '{DAV:}write');
                  }
  
                  $node->setPublishStatus(true);
  
                  // iCloud sends back the 202, so we will too.
                  $this->server->httpResponse->sendStatus(202);
  
                  // Adding this because sending a response body may cause issues,
                  // and I wanted some type of indicator the response was handled.
                  $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
  
                  // Breaking the event chain
                  return false;
6d9380f96   Cédric Dupont   Update sources OC...
401
              case '{' . Plugin::NS_CALENDARSERVER . '}unpublish-calendar' :
03e52840d   Kload   Init
402
403
  
                  // We can only deal with IShareableCalendar objects
6d9380f96   Cédric Dupont   Update sources OC...
404
                  if (!$node instanceof IShareableCalendar) {
03e52840d   Kload   Init
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
                      return;
                  }
  
                  // Getting ACL info
                  $acl = $this->server->getPlugin('acl');
  
                  // If there's no ACL support, we allow everything
                  if ($acl) {
                      $acl->checkPrivileges($uri, '{DAV:}write');
                  }
  
                  $node->setPublishStatus(false);
  
                  $this->server->httpResponse->sendStatus(200);
  
                  // Adding this because sending a response body may cause issues,
                  // and I wanted some type of indicator the response was handled.
                  $this->server->httpResponse->setHeader('X-Sabre-Status', 'everything-went-well');
  
                  // Breaking the event chain
                  return false;
  
          }
  
  
  
      }
  
      /**
       * Parses the 'share' POST request.
       *
       * This method returns an array, containing two arrays.
       * The first array is a list of new sharees. Every element is a struct
       * containing a:
       *   * href element. (usually a mailto: address)
       *   * commonName element (often a first and lastname, but can also be
       *     false)
       *   * readOnly (true or false)
       *   * summary (A description of the share, can also be false)
       *
       * The second array is a list of sharees that are to be removed. This is
       * just a simple array with 'hrefs'.
       *
6d9380f96   Cédric Dupont   Update sources OC...
448
       * @param \DOMDocument $dom
03e52840d   Kload   Init
449
450
       * @return array
       */
6d9380f96   Cédric Dupont   Update sources OC...
451
      protected function parseShareRequest(\DOMDocument $dom) {
03e52840d   Kload   Init
452
453
  
          $xpath = new \DOMXPath($dom);
6d9380f96   Cédric Dupont   Update sources OC...
454
          $xpath->registerNamespace('cs', Plugin::NS_CALENDARSERVER);
03e52840d   Kload   Init
455
          $xpath->registerNamespace('d', 'urn:DAV');
03e52840d   Kload   Init
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
          $set = array();
          $elems = $xpath->query('cs:set');
  
          for($i=0; $i < $elems->length; $i++) {
  
              $xset = $elems->item($i);
              $set[] = array(
                  'href' => $xpath->evaluate('string(d:href)', $xset),
                  'commonName' => $xpath->evaluate('string(cs:common-name)', $xset),
                  'summary' => $xpath->evaluate('string(cs:summary)', $xset),
                  'readOnly' => $xpath->evaluate('boolean(cs:read)', $xset)!==false
              );
  
          }
  
          $remove = array();
          $elems = $xpath->query('cs:remove');
  
          for($i=0; $i < $elems->length; $i++) {
  
              $xremove = $elems->item($i);
              $remove[] = $xpath->evaluate('string(d:href)', $xremove);
  
          }
  
          return array($set, $remove);
  
      }
  
      /**
       * Parses the 'invite-reply' POST request.
       *
       * This method returns an array, containing the following properties:
       *   * href - The sharee who is replying
       *   * status - One of the self::STATUS_* constants
       *   * calendarUri - The url of the shared calendar
       *   * inReplyTo - The unique id of the share invitation.
       *   * summary - Optional description of the reply.
       *
6d9380f96   Cédric Dupont   Update sources OC...
495
       * @param \DOMDocument $dom
03e52840d   Kload   Init
496
497
       * @return array
       */
6d9380f96   Cédric Dupont   Update sources OC...
498
      protected function parseInviteReplyRequest(\DOMDocument $dom) {
03e52840d   Kload   Init
499
500
  
          $xpath = new \DOMXPath($dom);
6d9380f96   Cédric Dupont   Update sources OC...
501
          $xpath->registerNamespace('cs', Plugin::NS_CALENDARSERVER);
03e52840d   Kload   Init
502
503
504
505
          $xpath->registerNamespace('d', 'urn:DAV');
  
          $hostHref = $xpath->evaluate('string(cs:hosturl/d:href)');
          if (!$hostHref) {
6d9380f96   Cédric Dupont   Update sources OC...
506
              throw new DAV\Exception\BadRequest('The {' . Plugin::NS_CALENDARSERVER . '}hosturl/{DAV:}href element is required');
03e52840d   Kload   Init
507
508
509
510
511
512
513
514
515
516
517
518
519
          }
  
          return array(
              'href' => $xpath->evaluate('string(d:href)'),
              'calendarUri' => $this->server->calculateUri($hostHref),
              'inReplyTo' => $xpath->evaluate('string(cs:in-reply-to)'),
              'summary' => $xpath->evaluate('string(cs:summary)'),
              'status' => $xpath->evaluate('boolean(cs:invite-accepted)')?self::STATUS_ACCEPTED:self::STATUS_DECLINED
          );
  
      }
  
  }