/
calendarconnector.php
622 lines (499 loc) · 18.9 KB
/
calendarconnector.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?php
/**
* ownCloud - CalendarPlus
*
* @author Sebastian Doell
* @copyright 2015 sebastian doell sebastian@libasys.de
*
* Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
* Copyright (c) 2012 Georg Ehrke <georg@owncloud.com>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\CalendarPlus\Connector;
use OCA\CalendarPlus\AppInfo\Application;
use OCA\CalendarPlus\Db\CalendarDAO;
use OCA\CalendarPlus\Db\EventDAO;
use OCA\CalendarPlus\Share\Backend\Calendar as ShareCalendar;
use OCA\CalendarPlus\ActivityData;
use OCA\CalendarPlus\Share\ShareConnector;
use OCA\CalendarPlus\Service\ObjectParser;
class CalendarConnector {
private $userId;
private $db;
private $shareConnector;
public function __construct() {
$this -> db = \OC::$server -> getDb();
$this -> shareConnector = new ShareConnector();
}
/**
* @brief Returns the list of calendars for a principal (DAV term of user)
* @param string $principaluri
* @return array
*/
public function allCalendarsWherePrincipalURIIs($principaluri) {
$uid = $this -> extractUserID($principaluri);
$this -> setUserId($uid);
return $this -> all();
}
/**
* @brief finds an object by its DAV Data
* @param integer $cid Calendar id
* @param string $uri the uri ('filename')
* @return associative array || null
*/
public function findObjectWhereDAVDataIs($cid, $uri) {
$this -> setUserId(\OCP\User::getUser());
$eventDB = new EventDAO($this -> db, $this -> userId, null);
return $eventDB -> findWhereDAVDataIs($cid, $uri);
}
/**
* add object from DAV
* @param integer $cid Calendar id
* @param string $uri the uri ('filename')
* @param boolean $bLogActivity
* @return associative array || null
*/
public function addObject($calendarId, $objectUri, $calendarData, $bLogActivity = true) {
$this -> setUserId(\OCP\User::getUser());
$calendar = $this -> find($calendarId);
if ($calendar['userid'] !== $this -> userId) {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithBySourceCalendar($calendarId);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & $this -> shareConnector -> getCreateAccess())) {
throw new \Sabre\DAV\Exception\Forbidden('You do not have the permissions to add events to this calendar.');
}
}
$objectParser = new ObjectParser($this -> userId);
$object = $objectParser -> parse($calendarData);
list($type, $startdate, $enddate, $summary, $repeating, $uid, $isAlarm, $relatedTo) = $objectParser -> extractData($object);
//Thunderbird fix multiple categories
$vevent = $object->VEVENT;
if(isset($vevent->CATEGORIES) && count($vevent->CATEGORIES) > 1){
$sCat ='';
foreach($vevent->CATEGORIES as $key => $val){
if($sCat === ''){
$sCat .= $val;
}else{
$sCat .= ','.$val;
}
}
unset($vevent->CATEGORIES);
$vevent->CATEGORIES = $sCat;
$calendarData = $object->serialize();
}
$eventDB = new EventDAO($this -> db, $this -> userId, null);
$object_id = $eventDB -> add($calendarId, $type, $startdate, $enddate, $repeating, $summary, $calendarData, $objectUri, time(), $isAlarm, $uid, $relatedTo, 0, $this -> userId);
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$calendarDB -> touch($calendarId);
if ($repeating) {
$app = new Application();
$c = $app -> getContainer();
$repeatController = $c -> query('RepeatController');
$repeatController -> generateEventCache($object_id);
}
//\OCP\Util::emitHook('\OCA\CalendarPlus', 'addEvent', $object_id);
if($bLogActivity === true){
$linkTypeApp = 'calendarplus';
if ($type == 'VTODO') {
$linkTypeApp = 'tasksplus';
}
$link = \OC::$server -> getURLGenerator() -> linkToRoute($linkTypeApp . '.page.index') . '#' . urlencode($object_id);
//FIXME lang
$params = array('mode' => 'created', 'link' => $link, 'trans_type' => $type, 'summary' => $summary, 'cal_user' => $calendar['userid'], 'cal_displayname' => $calendar['displayname'], );
ActivityData::logEventActivity($params, true);
}
return $object_id;
}
/**
* edits an object with the data provided by sabredav
* @param integer $id calendar id
* @param string $uri the uri of the object
* @param string $data object
* @param boolean $bLogActivity
* @return boolean
*/
public function updateObject($cid, $uri, $data, $bLogActivity = true) {
$this -> setUserId(\OCP\User::getUser());
$oldobject = $this -> findObjectWhereDAVDataIs($cid, $uri);
$calendar = $this -> find($cid);
$objectParser = new ObjectParser($this -> userId);
$oldvobject = $objectParser -> parse($oldobject['calendardata']);
if ($calendar['userid'] !== $this -> userId) {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithBySourceCalendar($cid);
$sharedAccessClassPermissions = $objectParser -> getAccessClassPermissions($oldvobject);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & $this -> shareConnector -> getUpdateAccess()) || !($sharedAccessClassPermissions & $this -> shareConnector -> getUpdateAccess())) {
throw new \Sabre\DAV\Exception\Forbidden('You do not have the permissions to edit this event.');
}
}
$object = $objectParser -> parse($data);
list($type, $startdate, $enddate, $summary, $repeating, $uid, $isAlarm, $relatedTo) = $objectParser -> extractData($object);
//Thunderbird fix
$vevent = $object->VEVENT;
if(isset($vevent->CATEGORIES) && count($vevent->CATEGORIES) > 1){
$sCat ='';
foreach($vevent->CATEGORIES as $key => $val){
if($sCat === ''){
$sCat .= $val;
}else{
$sCat .= ','.$val;
}
}
unset($vevent->CATEGORIES);
$vevent->CATEGORIES = $sCat;
$data = $object->serialize();
}
$eventDB = new EventDAO($this -> db, $this -> userId, null);
$eventDB -> update($type, $startdate, $enddate, $repeating, $summary, $data, time(), $isAlarm, $uid, $relatedTo, $oldobject['id']);
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$calendarDB -> touch($oldobject['calendarid']);
if ($repeating) {
$app = new Application();
$c = $app -> getContainer();
$repeatController = $c -> query('RepeatController');
$repeatController -> updateEvent($oldobject['id']);
}
if($bLogActivity === true){
$linkTypeApp = 'calendarplus';
if ($type == 'VTODO') {
$linkTypeApp = 'tasksplus';
}
$link = \OC::$server -> getURLGenerator() -> linkToRoute($linkTypeApp . '.page.index') . '#' . urlencode($oldobject['calendarid']);
$params = array('mode' => 'edited', 'link' => $link, 'trans_type' => $type, 'summary' => $summary, 'cal_user' => $calendar['userid'], 'cal_displayname' => $calendar['displayname'], );
ActivityData::logEventActivity($params, true);
}
return true;
}
/**
* @brief deletes an object with the data provided by \Sabredav
* @param integer $cid calendar id
* @param string $uri the uri of the object
* @param boolean $bLogActivity
* @return boolean
*/
public function deleteObject($cid, $uri, $bLogActivity = true) {
$this -> setUserId(\OCP\User::getUser());
$calendar = $this -> find($cid);
if ($calendar['userid'] !== $this -> userId) {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithBySourceCalendar($cid);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & $this -> shareConnector -> getDeleteAccess())) {
throw new \Sabre\DAV\Exception\Forbidden('You do not have the permissions to delete this event.');
}
}
$oldobject = $this -> findObjectWhereDAVDataIs($cid, $uri);
$eventDB = new EventDAO($this -> db, $this -> userId, null);
$eventDB -> deleteEventFromDAV($cid, $uri);
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$calendarDB -> touch($cid);
if ($oldobject['repeating']) {
$app = new Application();
$c = $app -> getContainer();
$repeatController = $c -> query('RepeatController');
$repeatController -> cleanEvent($oldobject['id']);
}
//\OCP\Util::emitHook('\OCA\CalendarPlus', 'deleteEvent', $oldobject['id']);
if($bLogActivity === true){
$params = array('mode' => 'deleted', 'link' => '', 'trans_type' => $oldobject['objecttype'], 'summary' => $oldobject['summary'], 'cal_user' => $calendar['userid'], 'cal_displayname' => $calendar['displayname'], );
ActivityData::logEventActivity($params, true);
}
return true;
}
/**
* @brief finds an object by its DAV Data
* @param integer $cid Calendar id
* @param string $uri the uri ('filename')
* @return associative array || null
*/
public function allObjects($calendarId) {
$this -> setUserId(\OCP\User::getUser());
$eventDB = new EventDAO($this -> db, $this -> userId, null);
return $eventDB -> all($calendarId);
}
/**
* @NoAdminRequired
*
* @brief Returns an object
* @param integer $id
* @return associative array || null
*/
public function findObject($id) {
$this -> setUserId(\OCP\User::getUser());
$eventDB = new EventDAO($this -> db, $this -> userId, null);
return $eventDB -> find($id);
}
/**
* @NoAdminRequired
*
* @brief Returns true or false if event is an shared event
* @param integer $id
*
* @return associative array || null
*
*/
public function checkIfObjectIsShared($id) {
$this -> setUserId(\OCP\User::getUser());
$eventDB = new EventDAO($this -> db, $this -> userId, null);
return $eventDB -> checkSharedEvent($id);
}
/**
* @NoAdminRequired
*
* @brief Returns an object
* @param integer $id
* @return associative array || null
*/
public function deleteObjects($calendarid) {
$this -> setUserId(\OCP\User::getUser());
$eventDB = new EventDAO($this -> db, $this -> userId, null);
return $eventDB -> deleteAllObjectsFromCalendar($calendarid);
}
/**
* @brief returns the owner of an object
* @param integer $id event id
* @return string
*/
public function getowner($id) {
$event = $this -> findObject($id);
$calendar = $this -> find($event['calendarid']);
if ($calendar === false || is_array($calendar) === false) {
return null;
}
if (array_key_exists('userid', $calendar)) {
return $calendar['userid'];
} else {
return null;
}
}
/**
* @brief returns informations about a calendar
* @param int $id - id of the calendar
* @param bool $security - check access rights or not
* @param bool $shared - check if the user got access via sharing
* @return mixed - bool / array
*/
public function getCalendar($id, $security = true, $shared = false) {
if (!is_numeric($id)) {
return false;
}
$this -> setUserId(\OCP\User::getUser());
$calendar = $this -> find($id);
// FIXME: Correct arguments to just check for permissions
if ($security === true && $shared === false) {
if ($this -> userId === $calendar['userid']) {
return $calendar;
} else {
return false;
}
}
if ($security === true && $shared === true) {
if ($this -> shareConnector -> getItemSharedWithBySourceCalendar($id) || $this -> shareConnector -> getItemSharedWithByLinkCalendar($id, $calendar['userid'])) {
return $calendar;
}
}
return $calendar;
}
/**
* @brief Gets the data of one calendar
* @param integer $id
* @return associative array
*/
public function find($id) {
$this -> setUserId(\OCP\User::getUser());
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$calendarInfo = $calendarDB -> find($id);
if ($calendarInfo !== null) {
if ($calendarInfo['userid'] !== $this -> userId) {
$userExists = \OC::$server -> getUserManager() -> userExists($this -> userId);
if (!$userExists) {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithByLinkCalendar($id, $calendarInfo['userid']);
} else {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithBySourceCalendar($id);
}
if ((!$sharedCalendar || !(isset($sharedCalendar['permissions']) && $sharedCalendar['permissions'] & $this -> shareConnector -> getReadAccess()))) {
return $calendarInfo;
// I have to return the row so e.g. Object::getowner() works.
}
$calendarInfo['permissions'] = $sharedCalendar['permissions'];
} else {
$calendarInfo['permissions'] = $this -> shareConnector -> getAllAccess();
}
return $calendarInfo;
} else {
return null;
}
}
/**
* @brief Creates a new calendar from the data sabredav provides
* @param string $principaluri
* @param string $uri
* @param string $name
* @param string $components
* @param string $timezone
* @param integer $order
* @param string $color format: '#RRGGBB(AA)'
* @return insertid
*/
public function add($principaluri, $uri, $name, $components, $timezone, $order, $color, $transparent) {
$userid = $this -> extractUserID($principaluri);
$this -> setUserId($userid);
$all = $this -> all();
$uris = array();
foreach ($all as $i) {
$uris[] = $i['uri'];
}
$lastmodified = time();
$uri = $this -> createURI($name, $uris);
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$insertid = $calendarDB -> add($name, $uri, $order, $color, $timezone, $components, 0, '', 0);
if ($insertid !== null) {
\OCP\Util::emitHook('\OCA\CalendarPlus', 'addCalendar', $insertid);
return $insertid;
} else {
return null;
}
}
/**
* @brief Edits a calendar
* @param integer $id
* @param string $name Default: null
* @param string $components Default: null
* @param string $timezone Default: null
* @param integer $order Default: null
* @param string $color Default: null, format: '#RRGGBB(AA)'
* @param boolean $bLogActivity
* @return boolean
*
* Values not null will be set
*/
public function edit($id, $name = null, $components = null, $timezone = null, $order = null, $color = null, $transparent = null, $bLogActivity = true) {
// Need these ones for checking uri
$calendar = $this -> find($id);
if ($calendar['userid'] !== $this -> userId) {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithBySourceCalendar($id);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & $this -> shareConnector -> getUpdateAccess())) {
throw new \Sabre\DAV\Exception\Forbidden('You do not have the permissions to update this calendar.');
}
}
// Keep old stuff
if (is_null($name))
$name = $calendar['displayname'];
if (is_null($components))
$components = $calendar['components'];
if (is_null($timezone))
$timezone = $calendar['timezone'];
if (is_null($order))
$order = $calendar['calendarorder'];
if (is_null($color))
$color = $calendar['calendarcolor'];
if (is_null($transparent))
$transparent = $calendar['transparent'];
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$bUpdateCalendar = $calendarDB -> update($name, $order, $color, $timezone, $components, $transparent, $id);
if ($bUpdateCalendar === true) {
\OCP\Util::emitHook('\OCA\CalendarPlus', 'editCalendar', $id);
if($bLogActivity === true){
$link = \OC::$server -> getURLGenerator() -> linkToRoute('calendarplus.page.index');
$params = array('mode' => 'edited', 'link' => $link, 'trans_type' => '', 'summary' => $calendar['displayname'], 'cal_user' => $calendar['userid'], 'cal_displayname' => $calendar['displayname'], );
ActivityData::logEventActivity($params, false, true);
}
return true;
} else {
return null;
}
}
/**
* removes a calendar
* @param integer $id
* @param boolean $bLogActivity
* @return associative array
*/
public function delete($id, $bLogActivity = true) {
$calendar = $this -> find($id);
//\OCP\Util::writeLog('DAV', 'DEL ID-> '.$id, \OCP\Util::DEBUG);
$group = \OC::$server -> getGroupManager() -> get('admin');
$this -> setUserId(\OCP\User::getUser());
if ($calendar['userid'] !== $this -> userId && !$group -> inGroup($this -> userId)) {
$sharedCalendar = $this -> shareConnector -> getItemSharedWithBySourceCalendar($id);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & $this -> shareConnector -> getDeleteAccess())) {
throw new \Sabre\DAV\Exception\Forbidden('You do not have the permissions to delete this calendar.');
}
}
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$bDeleteCalendar = $calendarDB -> delete($id);
if ($bDeleteCalendar === true) {
\OCP\Util::emitHook('\OCA\CalendarPlus', 'deleteCalendar', $id);
$this -> deleteObjects($id);
$this -> shareConnector -> unshareAllCalendar($id);
$app = new Application();
$c = $app -> getContainer();
$repeatController = $c -> query('RepeatController');
$repeatController -> cleanCalendar($id);
$calendars = $this -> all(false, false);
if ((\OCP\USER::isLoggedIn() && count($calendars) === 0)) {
//self::addDefaultCalendars($user);
}
if($bLogActivity === true){
$link = \OC::$server -> getURLGenerator() -> linkToRoute('calendarplus.page.index');
$params = array('mode' => 'deleted', 'link' => $link, 'trans_type' => '', 'summary' => $calendar['displayname'], 'cal_user' => $this -> userId, 'cal_displayname' => $calendar['displayname'], );
ActivityData::logEventActivity($params, false, true);
}
return $bDeleteCalendar;
} else {
return $bDeleteCalendar;
}
}
/**
* @brief Returns the list of calendars for a specific user.
* @param string $uid User ID
* @param boolean $active Only return calendars with this $active state, default(=false) is don't care
* @param boolean $bSubscribe return calendars with this $issubscribe state, default(=true) is don't care
* @return array
*/
public function all($active = false, $bSubscribe = true) {
$calendarDB = new CalendarDAO($this -> db, $this -> userId);
$calendars = $calendarDB -> all($active, $bSubscribe);
$calendars = array_merge($calendars, $this -> shareConnector -> getItemsSharedWithCalendar());
\OCP\Util::emitHook('OCA\CalendarPlus', 'getCalendars', array('calendar' => &$calendars));
return $calendars;
}
private function setUserId($userid) {
$this -> userId = $userid;
}
/**
* @brief Creates a URI for Calendar
* @param string $name name of the calendar
* @param array $existing existing calendar URIs
* @return string uri
*/
private function createURI($name, $existing) {
$strip = array(' ', '/', '?', '&');
//these may break sync clients
$name = str_replace($strip, '', $name);
$name = strtolower($name);
$newname = $name;
$i = 1;
while (in_array($newname, $existing)) {
$newname = $name . $i;
$i = $i + 1;
}
return $newname;
}
/**
* @brief gets the userid from a principal path
* @return string
*/
public function extractUserID($principaluri) {
list($prefix, $userid) = \Sabre\DAV\URLUtil::splitPath($principaluri);
return $userid;
}
}