1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]> |
4
|
|
|
* @copyright Copyright (c) 2016, Joas Schilling <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Joas Schilling <[email protected]> |
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license GNU AGPL version 3 or any later version |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
namespace OCA\DAV\CalDAV\Schedule; |
26
|
|
|
|
27
|
|
|
use OCA\DAV\CalDAV\CalDavBackend; |
28
|
|
|
use OCA\DAV\CalDAV\CalendarHome; |
29
|
|
|
use Sabre\DAV\INode; |
30
|
|
|
use Sabre\DAV\PropFind; |
31
|
|
|
use Sabre\DAV\Server; |
32
|
|
|
use Sabre\DAV\Xml\Property\LocalHref; |
33
|
|
|
use Sabre\DAVACL\IPrincipal; |
34
|
|
|
|
35
|
|
|
class Plugin extends \Sabre\CalDAV\Schedule\Plugin { |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initializes the plugin |
39
|
|
|
* |
40
|
|
|
* @param Server $server |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
function initialize(Server $server) { |
|
|
|
|
44
|
|
|
parent::initialize($server); |
45
|
|
|
$server->on('propFind', [$this, 'propFindDefaultCalendarUrl'], 90); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This method handler is invoked during fetching of properties. |
50
|
|
|
* |
51
|
|
|
* We use this event to add calendar-auto-schedule-specific properties. |
52
|
|
|
* |
53
|
|
|
* @param PropFind $propFind |
54
|
|
|
* @param INode $node |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
function propFind(PropFind $propFind, INode $node) { |
|
|
|
|
58
|
|
|
// overwrite Sabre/Dav's implementation |
59
|
|
|
$propFind->handle('{' . self::NS_CALDAV . '}calendar-user-type', function() use ($node) { |
60
|
|
|
$calendarUserType = '{' . self::NS_CALDAV . '}calendar-user-type'; |
61
|
|
|
$props = $node->getProperties([$calendarUserType]); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if (isset($props[$calendarUserType])) { |
64
|
|
|
return $props[$calendarUserType]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return 'INDIVIDUAL'; |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
parent::propFind($propFind, $node); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns a list of addresses that are associated with a principal. |
75
|
|
|
* |
76
|
|
|
* @param string $principal |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function getAddressesForPrincipal($principal) { |
80
|
|
|
$result = parent::getAddressesForPrincipal($principal); |
81
|
|
|
|
82
|
|
|
if ($result === null) { |
|
|
|
|
83
|
|
|
$result = []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Always use the personal calendar as target for scheduled events |
91
|
|
|
* |
92
|
|
|
* @param PropFind $propFind |
93
|
|
|
* @param INode $node |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) { |
|
|
|
|
97
|
|
|
if ($node instanceof IPrincipal) { |
98
|
|
|
$propFind->handle('{' . self::NS_CALDAV . '}schedule-default-calendar-URL', function() use ($node) { |
99
|
|
|
/** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */ |
100
|
|
|
$caldavPlugin = $this->server->getPlugin('caldav'); |
101
|
|
|
$principalUrl = $node->getPrincipalUrl(); |
102
|
|
|
|
103
|
|
|
$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl); |
104
|
|
|
if (!$calendarHomePath) { |
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (strpos($principalUrl, 'principals/users') === 0) { |
109
|
|
|
$uri = CalDavBackend::PERSONAL_CALENDAR_URI; |
110
|
|
|
$displayname = CalDavBackend::PERSONAL_CALENDAR_NAME; |
111
|
|
|
} elseif (strpos($principalUrl, 'principals/calendar-resources') === 0 || |
112
|
|
|
strpos($principalUrl, 'principals/calendar-rooms') === 0) { |
113
|
|
|
$uri = CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI; |
114
|
|
|
$displayname = CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME; |
115
|
|
|
} else { |
116
|
|
|
// How did we end up here? |
117
|
|
|
// TODO - throw exception or just ignore? |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @var CalendarHome $calendarHome */ |
122
|
|
|
$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath); |
123
|
|
|
if (!$calendarHome->childExists($uri)) { |
124
|
|
|
$calendarHome->getCalDAVBackend()->createCalendar($principalUrl, $uri, [ |
125
|
|
|
'{DAV:}displayname' => $displayname, |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1); |
|
|
|
|
130
|
|
|
if (empty($result)) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return new LocalHref($result[0]['href']); |
135
|
|
|
}); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.