Plugin.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program 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 License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\DAV\CardDAV;
  23. use OCA\DAV\CardDAV\Xml\Groups;
  24. use Sabre\DAV\INode;
  25. use Sabre\DAV\PropFind;
  26. use Sabre\DAV\Server;
  27. use Sabre\HTTP\URLUtil;
  28. class Plugin extends \Sabre\CardDAV\Plugin {
  29. function initialize(Server $server) {
  30. $server->on('propFind', [$this, 'propFind']);
  31. parent::initialize($server);
  32. }
  33. /**
  34. * Returns the addressbook home for a given principal
  35. *
  36. * @param string $principal
  37. * @return string
  38. */
  39. protected function getAddressbookHomeForPrincipal($principal) {
  40. if (strrpos($principal, 'principals/users', -strlen($principal)) !== false) {
  41. list(, $principalId) = URLUtil::splitPath($principal);
  42. return self::ADDRESSBOOK_ROOT . '/users/' . $principalId;
  43. }
  44. if (strrpos($principal, 'principals/groups', -strlen($principal)) !== false) {
  45. list(, $principalId) = URLUtil::splitPath($principal);
  46. return self::ADDRESSBOOK_ROOT . '/groups/' . $principalId;
  47. }
  48. if (strrpos($principal, 'principals/system', -strlen($principal)) !== false) {
  49. list(, $principalId) = URLUtil::splitPath($principal);
  50. return self::ADDRESSBOOK_ROOT . '/system/' . $principalId;
  51. }
  52. throw new \LogicException('This is not supposed to happen');
  53. }
  54. /**
  55. * Adds all CardDAV-specific properties
  56. *
  57. * @param PropFind $propFind
  58. * @param INode $node
  59. * @return void
  60. */
  61. function propFind(PropFind $propFind, INode $node) {
  62. $ns = '{http://owncloud.org/ns}';
  63. if ($node instanceof AddressBook) {
  64. $propFind->handle($ns . 'groups', function () use ($node) {
  65. return new Groups($node->getContactsGroups());
  66. });
  67. }
  68. }
  69. }