@@ -38,274 +38,274 @@ |
||
38 | 38 | */ |
39 | 39 | class Calendar extends \Sabre\CalDAV\Calendar implements IShareable { |
40 | 40 | |
41 | - public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) { |
|
42 | - parent::__construct($caldavBackend, $calendarInfo); |
|
43 | - |
|
44 | - if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
45 | - $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays'); |
|
46 | - } |
|
47 | - if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI && |
|
48 | - $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) { |
|
49 | - $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal'); |
|
50 | - } |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Updates the list of shares. |
|
55 | - * |
|
56 | - * The first array is a list of people that are to be added to the |
|
57 | - * resource. |
|
58 | - * |
|
59 | - * Every element in the add array has the following properties: |
|
60 | - * * href - A url. Usually a mailto: address |
|
61 | - * * commonName - Usually a first and last name, or false |
|
62 | - * * summary - A description of the share, can also be false |
|
63 | - * * readOnly - A boolean value |
|
64 | - * |
|
65 | - * Every element in the remove array is just the address string. |
|
66 | - * |
|
67 | - * @param array $add |
|
68 | - * @param array $remove |
|
69 | - * @return void |
|
70 | - * @throws Forbidden |
|
71 | - */ |
|
72 | - public function updateShares(array $add, array $remove) { |
|
73 | - if ($this->isShared()) { |
|
74 | - throw new Forbidden(); |
|
75 | - } |
|
76 | - $this->caldavBackend->updateShares($this, $add, $remove); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Returns the list of people whom this resource is shared with. |
|
81 | - * |
|
82 | - * Every element in this array should have the following properties: |
|
83 | - * * href - Often a mailto: address |
|
84 | - * * commonName - Optional, for example a first + last name |
|
85 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
86 | - * * readOnly - boolean |
|
87 | - * * summary - Optional, a description for the share |
|
88 | - * |
|
89 | - * @return array |
|
90 | - */ |
|
91 | - public function getShares() { |
|
92 | - if ($this->isShared()) { |
|
93 | - return []; |
|
94 | - } |
|
95 | - return $this->caldavBackend->getShares($this->getResourceId()); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return int |
|
100 | - */ |
|
101 | - public function getResourceId() { |
|
102 | - return $this->calendarInfo['id']; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - public function getPrincipalURI() { |
|
109 | - return $this->calendarInfo['principaluri']; |
|
110 | - } |
|
111 | - |
|
112 | - public function getACL() { |
|
113 | - $acl = [ |
|
114 | - [ |
|
115 | - 'privilege' => '{DAV:}read', |
|
116 | - 'principal' => $this->getOwner(), |
|
117 | - 'protected' => true, |
|
118 | - ]]; |
|
119 | - if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
120 | - $acl[] = [ |
|
121 | - 'privilege' => '{DAV:}write', |
|
122 | - 'principal' => $this->getOwner(), |
|
123 | - 'protected' => true, |
|
124 | - ]; |
|
125 | - } |
|
126 | - if ($this->getOwner() !== parent::getOwner()) { |
|
127 | - $acl[] = [ |
|
128 | - 'privilege' => '{DAV:}read', |
|
129 | - 'principal' => parent::getOwner(), |
|
130 | - 'protected' => true, |
|
131 | - ]; |
|
132 | - if ($this->canWrite()) { |
|
133 | - $acl[] = [ |
|
134 | - 'privilege' => '{DAV:}write', |
|
135 | - 'principal' => parent::getOwner(), |
|
136 | - 'protected' => true, |
|
137 | - ]; |
|
138 | - } |
|
139 | - } |
|
140 | - if ($this->isPublic()) { |
|
141 | - $acl[] = [ |
|
142 | - 'privilege' => '{DAV:}read', |
|
143 | - 'principal' => 'principals/system/public', |
|
144 | - 'protected' => true, |
|
145 | - ]; |
|
146 | - } |
|
147 | - |
|
148 | - $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl); |
|
149 | - |
|
150 | - if (!$this->isShared()) { |
|
151 | - return $acl; |
|
152 | - } |
|
153 | - |
|
154 | - $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public']; |
|
155 | - return array_filter($acl, function($rule) use ($allowedPrincipals) { |
|
156 | - return in_array($rule['principal'], $allowedPrincipals); |
|
157 | - }); |
|
158 | - } |
|
159 | - |
|
160 | - public function getChildACL() { |
|
161 | - return $this->getACL(); |
|
162 | - } |
|
163 | - |
|
164 | - public function getOwner() { |
|
165 | - if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
166 | - return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; |
|
167 | - } |
|
168 | - return parent::getOwner(); |
|
169 | - } |
|
170 | - |
|
171 | - public function delete() { |
|
172 | - if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) && |
|
173 | - $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) { |
|
174 | - $principal = 'principal:' . parent::getOwner(); |
|
175 | - $shares = $this->caldavBackend->getShares($this->getResourceId()); |
|
176 | - $shares = array_filter($shares, function($share) use ($principal){ |
|
177 | - return $share['href'] === $principal; |
|
178 | - }); |
|
179 | - if (empty($shares)) { |
|
180 | - throw new Forbidden(); |
|
181 | - } |
|
182 | - |
|
183 | - $this->caldavBackend->updateShares($this, [], [ |
|
184 | - 'href' => $principal |
|
185 | - ]); |
|
186 | - return; |
|
187 | - } |
|
188 | - parent::delete(); |
|
189 | - } |
|
190 | - |
|
191 | - public function propPatch(PropPatch $propPatch) { |
|
192 | - // parent::propPatch will only update calendars table |
|
193 | - // if calendar is shared, changes have to be made to the properties table |
|
194 | - if (!$this->isShared()) { |
|
195 | - parent::propPatch($propPatch); |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - public function getChild($name) { |
|
200 | - |
|
201 | - $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
202 | - |
|
203 | - if (!$obj) { |
|
204 | - throw new NotFound('Calendar object not found'); |
|
205 | - } |
|
206 | - |
|
207 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
208 | - throw new NotFound('Calendar object not found'); |
|
209 | - } |
|
210 | - |
|
211 | - $obj['acl'] = $this->getChildACL(); |
|
212 | - |
|
213 | - return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - public function getChildren() { |
|
218 | - |
|
219 | - $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']); |
|
220 | - $children = []; |
|
221 | - foreach ($objs as $obj) { |
|
222 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
223 | - continue; |
|
224 | - } |
|
225 | - $obj['acl'] = $this->getChildACL(); |
|
226 | - $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
227 | - } |
|
228 | - return $children; |
|
229 | - |
|
230 | - } |
|
231 | - |
|
232 | - public function getMultipleChildren(array $paths) { |
|
233 | - |
|
234 | - $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths); |
|
235 | - $children = []; |
|
236 | - foreach ($objs as $obj) { |
|
237 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
238 | - continue; |
|
239 | - } |
|
240 | - $obj['acl'] = $this->getChildACL(); |
|
241 | - $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
242 | - } |
|
243 | - return $children; |
|
244 | - |
|
245 | - } |
|
246 | - |
|
247 | - public function childExists($name) { |
|
248 | - $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
249 | - if (!$obj) { |
|
250 | - return false; |
|
251 | - } |
|
252 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - |
|
256 | - return true; |
|
257 | - } |
|
258 | - |
|
259 | - public function calendarQuery(array $filters) { |
|
260 | - |
|
261 | - $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters); |
|
262 | - if ($this->isShared()) { |
|
263 | - return array_filter($uris, function ($uri) { |
|
264 | - return $this->childExists($uri); |
|
265 | - }); |
|
266 | - } |
|
267 | - |
|
268 | - return $uris; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @param boolean $value |
|
273 | - * @return string|null |
|
274 | - */ |
|
275 | - public function setPublishStatus($value) { |
|
276 | - $publicUri = $this->caldavBackend->setPublishStatus($value, $this); |
|
277 | - $this->calendarInfo['publicuri'] = $publicUri; |
|
278 | - return $publicUri; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @return mixed $value |
|
283 | - */ |
|
284 | - public function getPublishStatus() { |
|
285 | - return $this->caldavBackend->getPublishStatus($this); |
|
286 | - } |
|
287 | - |
|
288 | - private function canWrite() { |
|
289 | - if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) { |
|
290 | - return !$this->calendarInfo['{http://owncloud.org/ns}read-only']; |
|
291 | - } |
|
292 | - return true; |
|
293 | - } |
|
294 | - |
|
295 | - private function isPublic() { |
|
296 | - return isset($this->calendarInfo['{http://owncloud.org/ns}public']); |
|
297 | - } |
|
298 | - |
|
299 | - protected function isShared() { |
|
300 | - if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
301 | - return false; |
|
302 | - } |
|
303 | - |
|
304 | - return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']; |
|
305 | - } |
|
306 | - |
|
307 | - public function isSubscription() { |
|
308 | - return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']); |
|
309 | - } |
|
41 | + public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) { |
|
42 | + parent::__construct($caldavBackend, $calendarInfo); |
|
43 | + |
|
44 | + if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
45 | + $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays'); |
|
46 | + } |
|
47 | + if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI && |
|
48 | + $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) { |
|
49 | + $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal'); |
|
50 | + } |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Updates the list of shares. |
|
55 | + * |
|
56 | + * The first array is a list of people that are to be added to the |
|
57 | + * resource. |
|
58 | + * |
|
59 | + * Every element in the add array has the following properties: |
|
60 | + * * href - A url. Usually a mailto: address |
|
61 | + * * commonName - Usually a first and last name, or false |
|
62 | + * * summary - A description of the share, can also be false |
|
63 | + * * readOnly - A boolean value |
|
64 | + * |
|
65 | + * Every element in the remove array is just the address string. |
|
66 | + * |
|
67 | + * @param array $add |
|
68 | + * @param array $remove |
|
69 | + * @return void |
|
70 | + * @throws Forbidden |
|
71 | + */ |
|
72 | + public function updateShares(array $add, array $remove) { |
|
73 | + if ($this->isShared()) { |
|
74 | + throw new Forbidden(); |
|
75 | + } |
|
76 | + $this->caldavBackend->updateShares($this, $add, $remove); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Returns the list of people whom this resource is shared with. |
|
81 | + * |
|
82 | + * Every element in this array should have the following properties: |
|
83 | + * * href - Often a mailto: address |
|
84 | + * * commonName - Optional, for example a first + last name |
|
85 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
86 | + * * readOnly - boolean |
|
87 | + * * summary - Optional, a description for the share |
|
88 | + * |
|
89 | + * @return array |
|
90 | + */ |
|
91 | + public function getShares() { |
|
92 | + if ($this->isShared()) { |
|
93 | + return []; |
|
94 | + } |
|
95 | + return $this->caldavBackend->getShares($this->getResourceId()); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return int |
|
100 | + */ |
|
101 | + public function getResourceId() { |
|
102 | + return $this->calendarInfo['id']; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + public function getPrincipalURI() { |
|
109 | + return $this->calendarInfo['principaluri']; |
|
110 | + } |
|
111 | + |
|
112 | + public function getACL() { |
|
113 | + $acl = [ |
|
114 | + [ |
|
115 | + 'privilege' => '{DAV:}read', |
|
116 | + 'principal' => $this->getOwner(), |
|
117 | + 'protected' => true, |
|
118 | + ]]; |
|
119 | + if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
120 | + $acl[] = [ |
|
121 | + 'privilege' => '{DAV:}write', |
|
122 | + 'principal' => $this->getOwner(), |
|
123 | + 'protected' => true, |
|
124 | + ]; |
|
125 | + } |
|
126 | + if ($this->getOwner() !== parent::getOwner()) { |
|
127 | + $acl[] = [ |
|
128 | + 'privilege' => '{DAV:}read', |
|
129 | + 'principal' => parent::getOwner(), |
|
130 | + 'protected' => true, |
|
131 | + ]; |
|
132 | + if ($this->canWrite()) { |
|
133 | + $acl[] = [ |
|
134 | + 'privilege' => '{DAV:}write', |
|
135 | + 'principal' => parent::getOwner(), |
|
136 | + 'protected' => true, |
|
137 | + ]; |
|
138 | + } |
|
139 | + } |
|
140 | + if ($this->isPublic()) { |
|
141 | + $acl[] = [ |
|
142 | + 'privilege' => '{DAV:}read', |
|
143 | + 'principal' => 'principals/system/public', |
|
144 | + 'protected' => true, |
|
145 | + ]; |
|
146 | + } |
|
147 | + |
|
148 | + $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl); |
|
149 | + |
|
150 | + if (!$this->isShared()) { |
|
151 | + return $acl; |
|
152 | + } |
|
153 | + |
|
154 | + $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public']; |
|
155 | + return array_filter($acl, function($rule) use ($allowedPrincipals) { |
|
156 | + return in_array($rule['principal'], $allowedPrincipals); |
|
157 | + }); |
|
158 | + } |
|
159 | + |
|
160 | + public function getChildACL() { |
|
161 | + return $this->getACL(); |
|
162 | + } |
|
163 | + |
|
164 | + public function getOwner() { |
|
165 | + if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
166 | + return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; |
|
167 | + } |
|
168 | + return parent::getOwner(); |
|
169 | + } |
|
170 | + |
|
171 | + public function delete() { |
|
172 | + if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) && |
|
173 | + $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) { |
|
174 | + $principal = 'principal:' . parent::getOwner(); |
|
175 | + $shares = $this->caldavBackend->getShares($this->getResourceId()); |
|
176 | + $shares = array_filter($shares, function($share) use ($principal){ |
|
177 | + return $share['href'] === $principal; |
|
178 | + }); |
|
179 | + if (empty($shares)) { |
|
180 | + throw new Forbidden(); |
|
181 | + } |
|
182 | + |
|
183 | + $this->caldavBackend->updateShares($this, [], [ |
|
184 | + 'href' => $principal |
|
185 | + ]); |
|
186 | + return; |
|
187 | + } |
|
188 | + parent::delete(); |
|
189 | + } |
|
190 | + |
|
191 | + public function propPatch(PropPatch $propPatch) { |
|
192 | + // parent::propPatch will only update calendars table |
|
193 | + // if calendar is shared, changes have to be made to the properties table |
|
194 | + if (!$this->isShared()) { |
|
195 | + parent::propPatch($propPatch); |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + public function getChild($name) { |
|
200 | + |
|
201 | + $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
202 | + |
|
203 | + if (!$obj) { |
|
204 | + throw new NotFound('Calendar object not found'); |
|
205 | + } |
|
206 | + |
|
207 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
208 | + throw new NotFound('Calendar object not found'); |
|
209 | + } |
|
210 | + |
|
211 | + $obj['acl'] = $this->getChildACL(); |
|
212 | + |
|
213 | + return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + public function getChildren() { |
|
218 | + |
|
219 | + $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']); |
|
220 | + $children = []; |
|
221 | + foreach ($objs as $obj) { |
|
222 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
223 | + continue; |
|
224 | + } |
|
225 | + $obj['acl'] = $this->getChildACL(); |
|
226 | + $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
227 | + } |
|
228 | + return $children; |
|
229 | + |
|
230 | + } |
|
231 | + |
|
232 | + public function getMultipleChildren(array $paths) { |
|
233 | + |
|
234 | + $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths); |
|
235 | + $children = []; |
|
236 | + foreach ($objs as $obj) { |
|
237 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
238 | + continue; |
|
239 | + } |
|
240 | + $obj['acl'] = $this->getChildACL(); |
|
241 | + $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
242 | + } |
|
243 | + return $children; |
|
244 | + |
|
245 | + } |
|
246 | + |
|
247 | + public function childExists($name) { |
|
248 | + $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
249 | + if (!$obj) { |
|
250 | + return false; |
|
251 | + } |
|
252 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + |
|
256 | + return true; |
|
257 | + } |
|
258 | + |
|
259 | + public function calendarQuery(array $filters) { |
|
260 | + |
|
261 | + $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters); |
|
262 | + if ($this->isShared()) { |
|
263 | + return array_filter($uris, function ($uri) { |
|
264 | + return $this->childExists($uri); |
|
265 | + }); |
|
266 | + } |
|
267 | + |
|
268 | + return $uris; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @param boolean $value |
|
273 | + * @return string|null |
|
274 | + */ |
|
275 | + public function setPublishStatus($value) { |
|
276 | + $publicUri = $this->caldavBackend->setPublishStatus($value, $this); |
|
277 | + $this->calendarInfo['publicuri'] = $publicUri; |
|
278 | + return $publicUri; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @return mixed $value |
|
283 | + */ |
|
284 | + public function getPublishStatus() { |
|
285 | + return $this->caldavBackend->getPublishStatus($this); |
|
286 | + } |
|
287 | + |
|
288 | + private function canWrite() { |
|
289 | + if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) { |
|
290 | + return !$this->calendarInfo['{http://owncloud.org/ns}read-only']; |
|
291 | + } |
|
292 | + return true; |
|
293 | + } |
|
294 | + |
|
295 | + private function isPublic() { |
|
296 | + return isset($this->calendarInfo['{http://owncloud.org/ns}public']); |
|
297 | + } |
|
298 | + |
|
299 | + protected function isShared() { |
|
300 | + if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
301 | + return false; |
|
302 | + } |
|
303 | + |
|
304 | + return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']; |
|
305 | + } |
|
306 | + |
|
307 | + public function isSubscription() { |
|
308 | + return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']); |
|
309 | + } |
|
310 | 310 | |
311 | 311 | } |