1<?php
2
3namespace Sabre\CalDAV\Subscriptions;
4
5use Sabre\DAV\INode;
6use Sabre\DAV\PropFind;
7use Sabre\DAV\Server;
8use Sabre\DAV\ServerPlugin;
9
10/**
11 * This plugin adds calendar-subscription support to your CalDAV server.
12 *
13 * Some clients support 'managed subscriptions' server-side. This is basically
14 * a list of subscription urls a user is using.
15 *
16 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
17 * @author Evert Pot (http://evertpot.com/)
18 * @license http://sabre.io/license/ Modified BSD License
19 */
20class Plugin extends ServerPlugin {
21
22    /**
23     * This initializes the plugin.
24     *
25     * This function is called by Sabre\DAV\Server, after
26     * addPlugin is called.
27     *
28     * This method should set up the required event subscriptions.
29     *
30     * @param Server $server
31     * @return void
32     */
33    function initialize(Server $server) {
34
35        $server->resourceTypeMapping['Sabre\\CalDAV\\Subscriptions\\ISubscription'] =
36            '{http://calendarserver.org/ns/}subscribed';
37
38        $server->xml->elementMap['{http://calendarserver.org/ns/}source'] =
39            'Sabre\\DAV\\Xml\\Property\\Href';
40
41        $server->on('propFind', [$this, 'propFind'], 150);
42
43    }
44
45    /**
46     * This method should return a list of server-features.
47     *
48     * This is for example 'versioning' and is added to the DAV: header
49     * in an OPTIONS response.
50     *
51     * @return array
52     */
53    function getFeatures() {
54
55        return ['calendarserver-subscribed'];
56
57    }
58
59    /**
60     * Triggered after properties have been fetched.
61     *
62     * @param PropFind $propFind
63     * @param INode $node
64     * @return void
65     */
66    function propFind(PropFind $propFind, INode $node) {
67
68        // There's a bunch of properties that must appear as a self-closing
69        // xml-element. This event handler ensures that this will be the case.
70        $props = [
71            '{http://calendarserver.org/ns/}subscribed-strip-alarms',
72            '{http://calendarserver.org/ns/}subscribed-strip-attachments',
73            '{http://calendarserver.org/ns/}subscribed-strip-todos',
74        ];
75
76        foreach ($props as $prop) {
77
78            if ($propFind->getStatus($prop) === 200) {
79                $propFind->set($prop, '', 200);
80            }
81
82        }
83
84    }
85
86    /**
87     * Returns a plugin name.
88     *
89     * Using this name other plugins will be able to access other plugins
90     * using \Sabre\DAV\Server::getPlugin
91     *
92     * @return string
93     */
94    function getPluginName() {
95
96        return 'subscriptions';
97
98    }
99
100    /**
101     * Returns a bunch of meta-data about the plugin.
102     *
103     * Providing this information is optional, and is mainly displayed by the
104     * Browser plugin.
105     *
106     * The description key in the returned array may contain html and will not
107     * be sanitized.
108     *
109     * @return array
110     */
111    function getPluginInfo() {
112
113        return [
114            'name'        => $this->getPluginName(),
115            'description' => 'This plugin allows users to store iCalendar subscriptions in their calendar-home.',
116            'link'        => null,
117        ];
118
119    }
120}
121