ctucx.git: tinyDAV

[php] Cal-/ CardDAV server with a simple web-GUI based on SabreDAV

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
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 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
<?php
declare(strict_types=1);

use Sabre\CardDAV;
use Sabre\DAV;

class SabreCardDAVJsonBackend extends Sabre\CardDAV\Backend\AbstractBackend implements Sabre\CardDAV\Backend\SyncSupport {
    protected $cardMgr;

    public function __construct(AddressbookCollectionManager $cardMgr) {
        $this->cardMgr = $cardMgr;
    }

    public function getAddressBooksForUser ($principalUri) {
        $addressBooks = [];

        foreach ($this->cardMgr->getCollections($principalUri) as $row) {
            $addressBooks[] = [
                'id'                                                      => $row['id'],
                'uri'                                                     => $row['uri'],
                'principaluri'                                            => $row['principaluri'],
                '{DAV:}displayname'                                       => $row['displayname'],
                '{'.CardDAV\Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'],
                '{http://calendarserver.org/ns/}getctag'                  => $row['synctoken'],
                '{http://sabredav.org/ns}sync-token'                      => $row['synctoken'],
            ];
        }

        return $addressBooks;
    }

    public function updateAddressBook ($addressBookId, \Sabre\DAV\PropPatch $propPatch) {
        $supportedProperties = [
            '{DAV:}displayname',
            '{'.CardDAV\Plugin::NS_CARDDAV.'}addressbook-description',
        ];

        $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) {
            $values = [];

            foreach ($mutations as $property => $newValue) {
                switch ($property) {
                    case '{DAV:}displayname':
                        $values['displayname'] = $newValue;
                        break;
                    case '{'.CardDAV\Plugin::NS_CARDDAV.'}addressbook-description':
                        $values['description'] = $newValue;
                        break;
                }
        	}

			$this->cardMgr->updateCollection($addressBookId, $values);
            return true;
        });
    }

    public function createAddressBook ($principalUri, $url, array $properties) {
        $displayname = null;
        $description = null;

        foreach ($properties as $property => $newValue) {
            switch ($property) {
                case '{DAV:}displayname':
                    $displayname = $newValue;
                    break;
                case '{'.CardDAV\Plugin::NS_CARDDAV.'}addressbook-description':
                    $description = $newValue;
                    break;
                default:
                    throw new DAV\Exception\BadRequest('Unknown property: '.$property);
            }
        }

        return $this->cardMgr->createCollection($principalUri, $url, $displayname, $description);
    }

    public function deleteAddressBook ($addressBookId) {
		$this->cardMgr->deleteCollection($addressBookId);
    }

    public function getCards ($addressbookId) {
    	$objects = $this->cardMgr->getObjects($addressbookId);

		foreach ($objects as $key => $object) {
			$objects[$key]['etag'] = '"'.$object['etag'].'"';
		}
    	
        return $objects;
    }

    public function getCard ($addressBookId, $cardUri) {
    	$object = $this->cardMgr->getObject($addressBookId, $cardUri);
		if (isset($object['etag'])) $object['etag'] = '"'.$object['etag'].'"';

    	return $object;
    }

    public function getMultipleCards ($addressBookId, array $uris) {
        $result = [];

		foreach ($uris as $uri) {
			$result[] = $this->getCard($addressBookId, $uri);
        }

        return $result;
    }

    public function createCard ($addressBookId, $uri, $cardData) {
		$etag = $this->cardMgr->createObject($addressBookId, $uri, $cardData);

        return '"'.$etag.'"';
    }

    public function updateCard ($addressBookId, $uri, $cardData) {
		$etag = $this->cardMgr->updateObject($addressBookId, $uri, $cardData);

        return '"'.$etag.'"';
    }

    public function deleteCard ($addressBookId, $uri) {
		return $this->cardMgr->deleteObject($addressBookId, $uri);
    }

    public function getChangesForAddressBook ($addressBookId, $syncToken, $syncLevel, $limit = null) {
		return $this->cardMgr->getChanges($addressBookId, $syncToken, $syncLevel);
    }
}