@@ -40,165 +40,165 @@ |
||
40 | 40 | |
41 | 41 | class Application extends App { |
42 | 42 | |
43 | - /** |
|
44 | - * Application constructor. |
|
45 | - */ |
|
46 | - public function __construct() { |
|
47 | - parent::__construct('dav'); |
|
48 | - |
|
49 | - $container = $this->getContainer(); |
|
50 | - $server = $container->getServer(); |
|
51 | - |
|
52 | - $container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) { |
|
53 | - return new PhotoCache( |
|
54 | - $server->getAppDataDir('dav-photocache') |
|
55 | - ); |
|
56 | - }); |
|
57 | - |
|
58 | - /* |
|
43 | + /** |
|
44 | + * Application constructor. |
|
45 | + */ |
|
46 | + public function __construct() { |
|
47 | + parent::__construct('dav'); |
|
48 | + |
|
49 | + $container = $this->getContainer(); |
|
50 | + $server = $container->getServer(); |
|
51 | + |
|
52 | + $container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) { |
|
53 | + return new PhotoCache( |
|
54 | + $server->getAppDataDir('dav-photocache') |
|
55 | + ); |
|
56 | + }); |
|
57 | + |
|
58 | + /* |
|
59 | 59 | * Register capabilities |
60 | 60 | */ |
61 | - $container->registerCapability(Capabilities::class); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @param IManager $contactsManager |
|
66 | - * @param string $userID |
|
67 | - */ |
|
68 | - public function setupContactsProvider(IManager $contactsManager, $userID) { |
|
69 | - /** @var ContactsManager $cm */ |
|
70 | - $cm = $this->getContainer()->query(ContactsManager::class); |
|
71 | - $urlGenerator = $this->getContainer()->getServer()->getURLGenerator(); |
|
72 | - $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator); |
|
73 | - } |
|
74 | - |
|
75 | - public function registerHooks() { |
|
76 | - /** @var HookManager $hm */ |
|
77 | - $hm = $this->getContainer()->query(HookManager::class); |
|
78 | - $hm->setup(); |
|
79 | - |
|
80 | - $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
81 | - |
|
82 | - // first time login event setup |
|
83 | - $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) { |
|
84 | - if ($event instanceof GenericEvent) { |
|
85 | - $hm->firstLogin($event->getSubject()); |
|
86 | - } |
|
87 | - }); |
|
88 | - |
|
89 | - // carddav/caldav sync event setup |
|
90 | - $listener = function($event) { |
|
91 | - if ($event instanceof GenericEvent) { |
|
92 | - /** @var BirthdayService $b */ |
|
93 | - $b = $this->getContainer()->query(BirthdayService::class); |
|
94 | - $b->onCardChanged( |
|
95 | - $event->getArgument('addressBookId'), |
|
96 | - $event->getArgument('cardUri'), |
|
97 | - $event->getArgument('cardData') |
|
98 | - ); |
|
99 | - } |
|
100 | - }; |
|
101 | - |
|
102 | - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener); |
|
103 | - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener); |
|
104 | - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) { |
|
105 | - if ($event instanceof GenericEvent) { |
|
106 | - /** @var BirthdayService $b */ |
|
107 | - $b = $this->getContainer()->query(BirthdayService::class); |
|
108 | - $b->onCardDeleted( |
|
109 | - $event->getArgument('addressBookId'), |
|
110 | - $event->getArgument('cardUri') |
|
111 | - ); |
|
112 | - } |
|
113 | - }); |
|
114 | - |
|
115 | - $clearPhotoCache = function($event) { |
|
116 | - if ($event instanceof GenericEvent) { |
|
117 | - /** @var PhotoCache $p */ |
|
118 | - $p = $this->getContainer()->query(PhotoCache::class); |
|
119 | - $p->delete( |
|
120 | - $event->getArgument('addressBookId'), |
|
121 | - $event->getArgument('cardUri') |
|
122 | - ); |
|
123 | - } |
|
124 | - }; |
|
125 | - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $clearPhotoCache); |
|
126 | - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', $clearPhotoCache); |
|
127 | - |
|
128 | - $dispatcher->addListener('OC\AccountManager::userUpdated', function(GenericEvent $event) { |
|
129 | - $user = $event->getSubject(); |
|
130 | - $syncService = $this->getContainer()->query(SyncService::class); |
|
131 | - $syncService->updateUser($user); |
|
132 | - }); |
|
133 | - |
|
134 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', function(GenericEvent $event) { |
|
135 | - /** @var Backend $backend */ |
|
136 | - $backend = $this->getContainer()->query(Backend::class); |
|
137 | - $backend->onCalendarAdd( |
|
138 | - $event->getArgument('calendarData') |
|
139 | - ); |
|
140 | - }); |
|
141 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', function(GenericEvent $event) { |
|
142 | - /** @var Backend $backend */ |
|
143 | - $backend = $this->getContainer()->query(Backend::class); |
|
144 | - $backend->onCalendarUpdate( |
|
145 | - $event->getArgument('calendarData'), |
|
146 | - $event->getArgument('shares'), |
|
147 | - $event->getArgument('propertyMutations') |
|
148 | - ); |
|
149 | - }); |
|
150 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', function(GenericEvent $event) { |
|
151 | - /** @var Backend $backend */ |
|
152 | - $backend = $this->getContainer()->query(Backend::class); |
|
153 | - $backend->onCalendarDelete( |
|
154 | - $event->getArgument('calendarData'), |
|
155 | - $event->getArgument('shares') |
|
156 | - ); |
|
157 | - }); |
|
158 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function(GenericEvent $event) { |
|
159 | - /** @var Backend $backend */ |
|
160 | - $backend = $this->getContainer()->query(Backend::class); |
|
161 | - $backend->onCalendarUpdateShares( |
|
162 | - $event->getArgument('calendarData'), |
|
163 | - $event->getArgument('shares'), |
|
164 | - $event->getArgument('add'), |
|
165 | - $event->getArgument('remove') |
|
166 | - ); |
|
167 | - }); |
|
168 | - |
|
169 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', function(GenericEvent $event) { |
|
170 | - /** @var Backend $backend */ |
|
171 | - $backend = $this->getContainer()->query(Backend::class); |
|
172 | - $backend->onCalendarPublication( |
|
173 | - $event->getArgument('calendarData'), |
|
174 | - $event->getArgument('public') |
|
175 | - ); |
|
176 | - }); |
|
177 | - |
|
178 | - $listener = function(GenericEvent $event, $eventName) { |
|
179 | - /** @var Backend $backend */ |
|
180 | - $backend = $this->getContainer()->query(Backend::class); |
|
181 | - |
|
182 | - $subject = Event::SUBJECT_OBJECT_ADD; |
|
183 | - if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') { |
|
184 | - $subject = Event::SUBJECT_OBJECT_UPDATE; |
|
185 | - } else if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') { |
|
186 | - $subject = Event::SUBJECT_OBJECT_DELETE; |
|
187 | - } |
|
188 | - $backend->onTouchCalendarObject( |
|
189 | - $subject, |
|
190 | - $event->getArgument('calendarData'), |
|
191 | - $event->getArgument('shares'), |
|
192 | - $event->getArgument('objectData') |
|
193 | - ); |
|
194 | - }; |
|
195 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener); |
|
196 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener); |
|
197 | - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener); |
|
198 | - } |
|
199 | - |
|
200 | - public function getSyncService() { |
|
201 | - return $this->getContainer()->query(SyncService::class); |
|
202 | - } |
|
61 | + $container->registerCapability(Capabilities::class); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @param IManager $contactsManager |
|
66 | + * @param string $userID |
|
67 | + */ |
|
68 | + public function setupContactsProvider(IManager $contactsManager, $userID) { |
|
69 | + /** @var ContactsManager $cm */ |
|
70 | + $cm = $this->getContainer()->query(ContactsManager::class); |
|
71 | + $urlGenerator = $this->getContainer()->getServer()->getURLGenerator(); |
|
72 | + $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator); |
|
73 | + } |
|
74 | + |
|
75 | + public function registerHooks() { |
|
76 | + /** @var HookManager $hm */ |
|
77 | + $hm = $this->getContainer()->query(HookManager::class); |
|
78 | + $hm->setup(); |
|
79 | + |
|
80 | + $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
81 | + |
|
82 | + // first time login event setup |
|
83 | + $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) { |
|
84 | + if ($event instanceof GenericEvent) { |
|
85 | + $hm->firstLogin($event->getSubject()); |
|
86 | + } |
|
87 | + }); |
|
88 | + |
|
89 | + // carddav/caldav sync event setup |
|
90 | + $listener = function($event) { |
|
91 | + if ($event instanceof GenericEvent) { |
|
92 | + /** @var BirthdayService $b */ |
|
93 | + $b = $this->getContainer()->query(BirthdayService::class); |
|
94 | + $b->onCardChanged( |
|
95 | + $event->getArgument('addressBookId'), |
|
96 | + $event->getArgument('cardUri'), |
|
97 | + $event->getArgument('cardData') |
|
98 | + ); |
|
99 | + } |
|
100 | + }; |
|
101 | + |
|
102 | + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener); |
|
103 | + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener); |
|
104 | + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) { |
|
105 | + if ($event instanceof GenericEvent) { |
|
106 | + /** @var BirthdayService $b */ |
|
107 | + $b = $this->getContainer()->query(BirthdayService::class); |
|
108 | + $b->onCardDeleted( |
|
109 | + $event->getArgument('addressBookId'), |
|
110 | + $event->getArgument('cardUri') |
|
111 | + ); |
|
112 | + } |
|
113 | + }); |
|
114 | + |
|
115 | + $clearPhotoCache = function($event) { |
|
116 | + if ($event instanceof GenericEvent) { |
|
117 | + /** @var PhotoCache $p */ |
|
118 | + $p = $this->getContainer()->query(PhotoCache::class); |
|
119 | + $p->delete( |
|
120 | + $event->getArgument('addressBookId'), |
|
121 | + $event->getArgument('cardUri') |
|
122 | + ); |
|
123 | + } |
|
124 | + }; |
|
125 | + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $clearPhotoCache); |
|
126 | + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', $clearPhotoCache); |
|
127 | + |
|
128 | + $dispatcher->addListener('OC\AccountManager::userUpdated', function(GenericEvent $event) { |
|
129 | + $user = $event->getSubject(); |
|
130 | + $syncService = $this->getContainer()->query(SyncService::class); |
|
131 | + $syncService->updateUser($user); |
|
132 | + }); |
|
133 | + |
|
134 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', function(GenericEvent $event) { |
|
135 | + /** @var Backend $backend */ |
|
136 | + $backend = $this->getContainer()->query(Backend::class); |
|
137 | + $backend->onCalendarAdd( |
|
138 | + $event->getArgument('calendarData') |
|
139 | + ); |
|
140 | + }); |
|
141 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', function(GenericEvent $event) { |
|
142 | + /** @var Backend $backend */ |
|
143 | + $backend = $this->getContainer()->query(Backend::class); |
|
144 | + $backend->onCalendarUpdate( |
|
145 | + $event->getArgument('calendarData'), |
|
146 | + $event->getArgument('shares'), |
|
147 | + $event->getArgument('propertyMutations') |
|
148 | + ); |
|
149 | + }); |
|
150 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', function(GenericEvent $event) { |
|
151 | + /** @var Backend $backend */ |
|
152 | + $backend = $this->getContainer()->query(Backend::class); |
|
153 | + $backend->onCalendarDelete( |
|
154 | + $event->getArgument('calendarData'), |
|
155 | + $event->getArgument('shares') |
|
156 | + ); |
|
157 | + }); |
|
158 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function(GenericEvent $event) { |
|
159 | + /** @var Backend $backend */ |
|
160 | + $backend = $this->getContainer()->query(Backend::class); |
|
161 | + $backend->onCalendarUpdateShares( |
|
162 | + $event->getArgument('calendarData'), |
|
163 | + $event->getArgument('shares'), |
|
164 | + $event->getArgument('add'), |
|
165 | + $event->getArgument('remove') |
|
166 | + ); |
|
167 | + }); |
|
168 | + |
|
169 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', function(GenericEvent $event) { |
|
170 | + /** @var Backend $backend */ |
|
171 | + $backend = $this->getContainer()->query(Backend::class); |
|
172 | + $backend->onCalendarPublication( |
|
173 | + $event->getArgument('calendarData'), |
|
174 | + $event->getArgument('public') |
|
175 | + ); |
|
176 | + }); |
|
177 | + |
|
178 | + $listener = function(GenericEvent $event, $eventName) { |
|
179 | + /** @var Backend $backend */ |
|
180 | + $backend = $this->getContainer()->query(Backend::class); |
|
181 | + |
|
182 | + $subject = Event::SUBJECT_OBJECT_ADD; |
|
183 | + if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') { |
|
184 | + $subject = Event::SUBJECT_OBJECT_UPDATE; |
|
185 | + } else if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') { |
|
186 | + $subject = Event::SUBJECT_OBJECT_DELETE; |
|
187 | + } |
|
188 | + $backend->onTouchCalendarObject( |
|
189 | + $subject, |
|
190 | + $event->getArgument('calendarData'), |
|
191 | + $event->getArgument('shares'), |
|
192 | + $event->getArgument('objectData') |
|
193 | + ); |
|
194 | + }; |
|
195 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener); |
|
196 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener); |
|
197 | + $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener); |
|
198 | + } |
|
199 | + |
|
200 | + public function getSyncService() { |
|
201 | + return $this->getContainer()->query(SyncService::class); |
|
202 | + } |
|
203 | 203 | |
204 | 204 | } |
@@ -39,443 +39,443 @@ |
||
39 | 39 | */ |
40 | 40 | class Backend { |
41 | 41 | |
42 | - /** @var IActivityManager */ |
|
43 | - protected $activityManager; |
|
44 | - |
|
45 | - /** @var IGroupManager */ |
|
46 | - protected $groupManager; |
|
47 | - |
|
48 | - /** @var IUserSession */ |
|
49 | - protected $userSession; |
|
50 | - |
|
51 | - /** |
|
52 | - * @param IActivityManager $activityManager |
|
53 | - * @param IGroupManager $groupManager |
|
54 | - * @param IUserSession $userSession |
|
55 | - */ |
|
56 | - public function __construct(IActivityManager $activityManager, IGroupManager $groupManager, IUserSession $userSession) { |
|
57 | - $this->activityManager = $activityManager; |
|
58 | - $this->groupManager = $groupManager; |
|
59 | - $this->userSession = $userSession; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Creates activities when a calendar was creates |
|
64 | - * |
|
65 | - * @param array $calendarData |
|
66 | - */ |
|
67 | - public function onCalendarAdd(array $calendarData) { |
|
68 | - $this->triggerCalendarActivity(Calendar::SUBJECT_ADD, $calendarData); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Creates activities when a calendar was updated |
|
73 | - * |
|
74 | - * @param array $calendarData |
|
75 | - * @param array $shares |
|
76 | - * @param array $properties |
|
77 | - */ |
|
78 | - public function onCalendarUpdate(array $calendarData, array $shares, array $properties) { |
|
79 | - $this->triggerCalendarActivity(Calendar::SUBJECT_UPDATE, $calendarData, $shares, $properties); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Creates activities when a calendar was deleted |
|
84 | - * |
|
85 | - * @param array $calendarData |
|
86 | - * @param array $shares |
|
87 | - */ |
|
88 | - public function onCalendarDelete(array $calendarData, array $shares) { |
|
89 | - $this->triggerCalendarActivity(Calendar::SUBJECT_DELETE, $calendarData, $shares); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Creates activities when a calendar was (un)published |
|
94 | - * |
|
95 | - * @param array $calendarData |
|
96 | - * @param bool $publishStatus |
|
97 | - */ |
|
98 | - public function onCalendarPublication(array $calendarData, $publishStatus) { |
|
99 | - $this->triggerCalendarActivity($publishStatus ? Calendar::SUBJECT_PUBLISH : Calendar::SUBJECT_UNPUBLISH, $calendarData); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Creates activities for all related users when a calendar was touched |
|
104 | - * |
|
105 | - * @param string $action |
|
106 | - * @param array $calendarData |
|
107 | - * @param array $shares |
|
108 | - * @param array $changedProperties |
|
109 | - */ |
|
110 | - protected function triggerCalendarActivity($action, array $calendarData, array $shares = [], array $changedProperties = []) { |
|
111 | - if (!isset($calendarData['principaluri'])) { |
|
112 | - return; |
|
113 | - } |
|
114 | - |
|
115 | - $principal = explode('/', $calendarData['principaluri']); |
|
116 | - $owner = array_pop($principal); |
|
117 | - |
|
118 | - $currentUser = $this->userSession->getUser(); |
|
119 | - if ($currentUser instanceof IUser) { |
|
120 | - $currentUser = $currentUser->getUID(); |
|
121 | - } else { |
|
122 | - $currentUser = $owner; |
|
123 | - } |
|
124 | - |
|
125 | - $event = $this->activityManager->generateEvent(); |
|
126 | - $event->setApp('dav') |
|
127 | - ->setObject('calendar', (int) $calendarData['id']) |
|
128 | - ->setType('calendar') |
|
129 | - ->setAuthor($currentUser); |
|
130 | - |
|
131 | - $changedVisibleInformation = array_intersect([ |
|
132 | - '{DAV:}displayname', |
|
133 | - '{http://apple.com/ns/ical/}calendar-color' |
|
134 | - ], array_keys($changedProperties)); |
|
135 | - |
|
136 | - if (empty($shares) || ($action === Calendar::SUBJECT_UPDATE && empty($changedVisibleInformation))) { |
|
137 | - $users = [$owner]; |
|
138 | - } else { |
|
139 | - $users = $this->getUsersForShares($shares); |
|
140 | - $users[] = $owner; |
|
141 | - } |
|
142 | - |
|
143 | - foreach ($users as $user) { |
|
144 | - $event->setAffectedUser($user) |
|
145 | - ->setSubject( |
|
146 | - $user === $currentUser ? $action . '_self' : $action, |
|
147 | - [ |
|
148 | - $currentUser, |
|
149 | - $calendarData['{DAV:}displayname'], |
|
150 | - ] |
|
151 | - ); |
|
152 | - $this->activityManager->publish($event); |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Creates activities for all related users when a calendar was (un-)shared |
|
158 | - * |
|
159 | - * @param array $calendarData |
|
160 | - * @param array $shares |
|
161 | - * @param array $add |
|
162 | - * @param array $remove |
|
163 | - */ |
|
164 | - public function onCalendarUpdateShares(array $calendarData, array $shares, array $add, array $remove) { |
|
165 | - $principal = explode('/', $calendarData['principaluri']); |
|
166 | - $owner = $principal[2]; |
|
167 | - |
|
168 | - $currentUser = $this->userSession->getUser(); |
|
169 | - if ($currentUser instanceof IUser) { |
|
170 | - $currentUser = $currentUser->getUID(); |
|
171 | - } else { |
|
172 | - $currentUser = $owner; |
|
173 | - } |
|
174 | - |
|
175 | - $event = $this->activityManager->generateEvent(); |
|
176 | - $event->setApp('dav') |
|
177 | - ->setObject('calendar', (int) $calendarData['id']) |
|
178 | - ->setType('calendar') |
|
179 | - ->setAuthor($currentUser); |
|
180 | - |
|
181 | - foreach ($remove as $principal) { |
|
182 | - // principal:principals/users/test |
|
183 | - $parts = explode(':', $principal, 2); |
|
184 | - if ($parts[0] !== 'principal') { |
|
185 | - continue; |
|
186 | - } |
|
187 | - $principal = explode('/', $parts[1]); |
|
188 | - |
|
189 | - if ($principal[1] === 'users') { |
|
190 | - $this->triggerActivityUser( |
|
191 | - $principal[2], |
|
192 | - $event, |
|
193 | - $calendarData, |
|
194 | - Calendar::SUBJECT_UNSHARE_USER, |
|
195 | - Calendar::SUBJECT_DELETE . '_self' |
|
196 | - ); |
|
197 | - |
|
198 | - if ($owner !== $principal[2]) { |
|
199 | - $parameters = [ |
|
200 | - $principal[2], |
|
201 | - $calendarData['{DAV:}displayname'], |
|
202 | - ]; |
|
203 | - |
|
204 | - if ($owner === $event->getAuthor()) { |
|
205 | - $subject = Calendar::SUBJECT_UNSHARE_USER . '_you'; |
|
206 | - } else if ($principal[2] === $event->getAuthor()) { |
|
207 | - $subject = Calendar::SUBJECT_UNSHARE_USER . '_self'; |
|
208 | - } else { |
|
209 | - $event->setAffectedUser($event->getAuthor()) |
|
210 | - ->setSubject(Calendar::SUBJECT_UNSHARE_USER . '_you', $parameters); |
|
211 | - $this->activityManager->publish($event); |
|
212 | - |
|
213 | - $subject = Calendar::SUBJECT_UNSHARE_USER . '_by'; |
|
214 | - $parameters[] = $event->getAuthor(); |
|
215 | - } |
|
216 | - |
|
217 | - $event->setAffectedUser($owner) |
|
218 | - ->setSubject($subject, $parameters); |
|
219 | - $this->activityManager->publish($event); |
|
220 | - } |
|
221 | - } else if ($principal[1] === 'groups') { |
|
222 | - $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_UNSHARE_USER); |
|
223 | - |
|
224 | - $parameters = [ |
|
225 | - $principal[2], |
|
226 | - $calendarData['{DAV:}displayname'], |
|
227 | - ]; |
|
228 | - |
|
229 | - if ($owner === $event->getAuthor()) { |
|
230 | - $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_you'; |
|
231 | - } else { |
|
232 | - $event->setAffectedUser($event->getAuthor()) |
|
233 | - ->setSubject(Calendar::SUBJECT_UNSHARE_GROUP . '_you', $parameters); |
|
234 | - $this->activityManager->publish($event); |
|
235 | - |
|
236 | - $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_by'; |
|
237 | - $parameters[] = $event->getAuthor(); |
|
238 | - } |
|
239 | - |
|
240 | - $event->setAffectedUser($owner) |
|
241 | - ->setSubject($subject, $parameters); |
|
242 | - $this->activityManager->publish($event); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - foreach ($add as $share) { |
|
247 | - if ($this->isAlreadyShared($share['href'], $shares)) { |
|
248 | - continue; |
|
249 | - } |
|
250 | - |
|
251 | - // principal:principals/users/test |
|
252 | - $parts = explode(':', $share['href'], 2); |
|
253 | - if ($parts[0] !== 'principal') { |
|
254 | - continue; |
|
255 | - } |
|
256 | - $principal = explode('/', $parts[1]); |
|
257 | - |
|
258 | - if ($principal[1] === 'users') { |
|
259 | - $this->triggerActivityUser($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER); |
|
260 | - |
|
261 | - if ($owner !== $principal[2]) { |
|
262 | - $parameters = [ |
|
263 | - $principal[2], |
|
264 | - $calendarData['{DAV:}displayname'], |
|
265 | - ]; |
|
266 | - |
|
267 | - if ($owner === $event->getAuthor()) { |
|
268 | - $subject = Calendar::SUBJECT_SHARE_USER . '_you'; |
|
269 | - } else { |
|
270 | - $event->setAffectedUser($event->getAuthor()) |
|
271 | - ->setSubject(Calendar::SUBJECT_SHARE_USER . '_you', $parameters); |
|
272 | - $this->activityManager->publish($event); |
|
273 | - |
|
274 | - $subject = Calendar::SUBJECT_SHARE_USER . '_by'; |
|
275 | - $parameters[] = $event->getAuthor(); |
|
276 | - } |
|
277 | - |
|
278 | - $event->setAffectedUser($owner) |
|
279 | - ->setSubject($subject, $parameters); |
|
280 | - $this->activityManager->publish($event); |
|
281 | - } |
|
282 | - } else if ($principal[1] === 'groups') { |
|
283 | - $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER); |
|
284 | - |
|
285 | - $parameters = [ |
|
286 | - $principal[2], |
|
287 | - $calendarData['{DAV:}displayname'], |
|
288 | - ]; |
|
289 | - |
|
290 | - if ($owner === $event->getAuthor()) { |
|
291 | - $subject = Calendar::SUBJECT_SHARE_GROUP . '_you'; |
|
292 | - } else { |
|
293 | - $event->setAffectedUser($event->getAuthor()) |
|
294 | - ->setSubject(Calendar::SUBJECT_SHARE_GROUP . '_you', $parameters); |
|
295 | - $this->activityManager->publish($event); |
|
296 | - |
|
297 | - $subject = Calendar::SUBJECT_SHARE_GROUP . '_by'; |
|
298 | - $parameters[] = $event->getAuthor(); |
|
299 | - } |
|
300 | - |
|
301 | - $event->setAffectedUser($owner) |
|
302 | - ->setSubject($subject, $parameters); |
|
303 | - $this->activityManager->publish($event); |
|
304 | - } |
|
305 | - } |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Checks if a calendar is already shared with a principal |
|
310 | - * |
|
311 | - * @param string $principal |
|
312 | - * @param array[] $shares |
|
313 | - * @return bool |
|
314 | - */ |
|
315 | - protected function isAlreadyShared($principal, $shares) { |
|
316 | - foreach ($shares as $share) { |
|
317 | - if ($principal === $share['href']) { |
|
318 | - return true; |
|
319 | - } |
|
320 | - } |
|
321 | - |
|
322 | - return false; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Creates the given activity for all members of the given group |
|
327 | - * |
|
328 | - * @param string $gid |
|
329 | - * @param IEvent $event |
|
330 | - * @param array $properties |
|
331 | - * @param string $subject |
|
332 | - */ |
|
333 | - protected function triggerActivityGroup($gid, IEvent $event, array $properties, $subject) { |
|
334 | - $group = $this->groupManager->get($gid); |
|
335 | - |
|
336 | - if ($group instanceof IGroup) { |
|
337 | - foreach ($group->getUsers() as $user) { |
|
338 | - // Exclude current user |
|
339 | - if ($user->getUID() !== $event->getAuthor()) { |
|
340 | - $this->triggerActivityUser($user->getUID(), $event, $properties, $subject); |
|
341 | - } |
|
342 | - } |
|
343 | - } |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Creates the given activity for the given user |
|
348 | - * |
|
349 | - * @param string $user |
|
350 | - * @param IEvent $event |
|
351 | - * @param array $properties |
|
352 | - * @param string $subject |
|
353 | - * @param string $subjectSelf |
|
354 | - */ |
|
355 | - protected function triggerActivityUser($user, IEvent $event, array $properties, $subject, $subjectSelf = '') { |
|
356 | - $event->setAffectedUser($user) |
|
357 | - ->setSubject( |
|
358 | - $user === $event->getAuthor() && $subjectSelf ? $subjectSelf : $subject, |
|
359 | - [ |
|
360 | - $event->getAuthor(), |
|
361 | - $properties['{DAV:}displayname'], |
|
362 | - ] |
|
363 | - ); |
|
364 | - |
|
365 | - $this->activityManager->publish($event); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Creates activities when a calendar object was created/updated/deleted |
|
370 | - * |
|
371 | - * @param string $action |
|
372 | - * @param array $calendarData |
|
373 | - * @param array $shares |
|
374 | - * @param array $objectData |
|
375 | - */ |
|
376 | - public function onTouchCalendarObject($action, array $calendarData, array $shares, array $objectData) { |
|
377 | - if (!isset($calendarData['principaluri'])) { |
|
378 | - return; |
|
379 | - } |
|
380 | - |
|
381 | - $principal = explode('/', $calendarData['principaluri']); |
|
382 | - $owner = array_pop($principal); |
|
383 | - |
|
384 | - $currentUser = $this->userSession->getUser(); |
|
385 | - if ($currentUser instanceof IUser) { |
|
386 | - $currentUser = $currentUser->getUID(); |
|
387 | - } else { |
|
388 | - $currentUser = $owner; |
|
389 | - } |
|
390 | - |
|
391 | - $object = $this->getObjectNameAndType($objectData); |
|
392 | - $action = $action . '_' . $object['type']; |
|
393 | - |
|
394 | - if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'COMPLETED') { |
|
395 | - $action .= '_completed'; |
|
396 | - } else if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'NEEDS-ACTION') { |
|
397 | - $action .= '_needs_action'; |
|
398 | - } |
|
399 | - |
|
400 | - $event = $this->activityManager->generateEvent(); |
|
401 | - $event->setApp('dav') |
|
402 | - ->setObject('calendar', (int) $calendarData['id']) |
|
403 | - ->setType($object['type'] === 'event' ? 'calendar_event' : 'calendar_todo') |
|
404 | - ->setAuthor($currentUser); |
|
405 | - |
|
406 | - $users = $this->getUsersForShares($shares); |
|
407 | - $users[] = $owner; |
|
408 | - |
|
409 | - foreach ($users as $user) { |
|
410 | - $event->setAffectedUser($user) |
|
411 | - ->setSubject( |
|
412 | - $user === $currentUser ? $action . '_self' : $action, |
|
413 | - [ |
|
414 | - $currentUser, |
|
415 | - $calendarData['{DAV:}displayname'], |
|
416 | - [ |
|
417 | - 'id' => $object['id'], |
|
418 | - 'name' => $object['name'], |
|
419 | - ], |
|
420 | - ] |
|
421 | - ); |
|
422 | - $this->activityManager->publish($event); |
|
423 | - } |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * @param array $objectData |
|
428 | - * @return string[]|bool |
|
429 | - */ |
|
430 | - protected function getObjectNameAndType(array $objectData) { |
|
431 | - $vObject = Reader::read($objectData['calendardata']); |
|
432 | - $component = $componentType = null; |
|
433 | - foreach($vObject->getComponents() as $component) { |
|
434 | - if (in_array($component->name, ['VEVENT', 'VTODO'])) { |
|
435 | - $componentType = $component->name; |
|
436 | - break; |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - if (!$componentType) { |
|
441 | - // Calendar objects must have a VEVENT or VTODO component |
|
442 | - return false; |
|
443 | - } |
|
444 | - |
|
445 | - if ($componentType === 'VEVENT') { |
|
446 | - return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'event']; |
|
447 | - } |
|
448 | - return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'todo', 'status' => (string) $component->STATUS]; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Get all users that have access to a given calendar |
|
453 | - * |
|
454 | - * @param array $shares |
|
455 | - * @return string[] |
|
456 | - */ |
|
457 | - protected function getUsersForShares(array $shares) { |
|
458 | - $users = $groups = []; |
|
459 | - foreach ($shares as $share) { |
|
460 | - $prinical = explode('/', $share['{http://owncloud.org/ns}principal']); |
|
461 | - if ($prinical[1] === 'users') { |
|
462 | - $users[] = $prinical[2]; |
|
463 | - } else if ($prinical[1] === 'groups') { |
|
464 | - $groups[] = $prinical[2]; |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - if (!empty($groups)) { |
|
469 | - foreach ($groups as $gid) { |
|
470 | - $group = $this->groupManager->get($gid); |
|
471 | - if ($group instanceof IGroup) { |
|
472 | - foreach ($group->getUsers() as $user) { |
|
473 | - $users[] = $user->getUID(); |
|
474 | - } |
|
475 | - } |
|
476 | - } |
|
477 | - } |
|
478 | - |
|
479 | - return array_unique($users); |
|
480 | - } |
|
42 | + /** @var IActivityManager */ |
|
43 | + protected $activityManager; |
|
44 | + |
|
45 | + /** @var IGroupManager */ |
|
46 | + protected $groupManager; |
|
47 | + |
|
48 | + /** @var IUserSession */ |
|
49 | + protected $userSession; |
|
50 | + |
|
51 | + /** |
|
52 | + * @param IActivityManager $activityManager |
|
53 | + * @param IGroupManager $groupManager |
|
54 | + * @param IUserSession $userSession |
|
55 | + */ |
|
56 | + public function __construct(IActivityManager $activityManager, IGroupManager $groupManager, IUserSession $userSession) { |
|
57 | + $this->activityManager = $activityManager; |
|
58 | + $this->groupManager = $groupManager; |
|
59 | + $this->userSession = $userSession; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Creates activities when a calendar was creates |
|
64 | + * |
|
65 | + * @param array $calendarData |
|
66 | + */ |
|
67 | + public function onCalendarAdd(array $calendarData) { |
|
68 | + $this->triggerCalendarActivity(Calendar::SUBJECT_ADD, $calendarData); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Creates activities when a calendar was updated |
|
73 | + * |
|
74 | + * @param array $calendarData |
|
75 | + * @param array $shares |
|
76 | + * @param array $properties |
|
77 | + */ |
|
78 | + public function onCalendarUpdate(array $calendarData, array $shares, array $properties) { |
|
79 | + $this->triggerCalendarActivity(Calendar::SUBJECT_UPDATE, $calendarData, $shares, $properties); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Creates activities when a calendar was deleted |
|
84 | + * |
|
85 | + * @param array $calendarData |
|
86 | + * @param array $shares |
|
87 | + */ |
|
88 | + public function onCalendarDelete(array $calendarData, array $shares) { |
|
89 | + $this->triggerCalendarActivity(Calendar::SUBJECT_DELETE, $calendarData, $shares); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Creates activities when a calendar was (un)published |
|
94 | + * |
|
95 | + * @param array $calendarData |
|
96 | + * @param bool $publishStatus |
|
97 | + */ |
|
98 | + public function onCalendarPublication(array $calendarData, $publishStatus) { |
|
99 | + $this->triggerCalendarActivity($publishStatus ? Calendar::SUBJECT_PUBLISH : Calendar::SUBJECT_UNPUBLISH, $calendarData); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Creates activities for all related users when a calendar was touched |
|
104 | + * |
|
105 | + * @param string $action |
|
106 | + * @param array $calendarData |
|
107 | + * @param array $shares |
|
108 | + * @param array $changedProperties |
|
109 | + */ |
|
110 | + protected function triggerCalendarActivity($action, array $calendarData, array $shares = [], array $changedProperties = []) { |
|
111 | + if (!isset($calendarData['principaluri'])) { |
|
112 | + return; |
|
113 | + } |
|
114 | + |
|
115 | + $principal = explode('/', $calendarData['principaluri']); |
|
116 | + $owner = array_pop($principal); |
|
117 | + |
|
118 | + $currentUser = $this->userSession->getUser(); |
|
119 | + if ($currentUser instanceof IUser) { |
|
120 | + $currentUser = $currentUser->getUID(); |
|
121 | + } else { |
|
122 | + $currentUser = $owner; |
|
123 | + } |
|
124 | + |
|
125 | + $event = $this->activityManager->generateEvent(); |
|
126 | + $event->setApp('dav') |
|
127 | + ->setObject('calendar', (int) $calendarData['id']) |
|
128 | + ->setType('calendar') |
|
129 | + ->setAuthor($currentUser); |
|
130 | + |
|
131 | + $changedVisibleInformation = array_intersect([ |
|
132 | + '{DAV:}displayname', |
|
133 | + '{http://apple.com/ns/ical/}calendar-color' |
|
134 | + ], array_keys($changedProperties)); |
|
135 | + |
|
136 | + if (empty($shares) || ($action === Calendar::SUBJECT_UPDATE && empty($changedVisibleInformation))) { |
|
137 | + $users = [$owner]; |
|
138 | + } else { |
|
139 | + $users = $this->getUsersForShares($shares); |
|
140 | + $users[] = $owner; |
|
141 | + } |
|
142 | + |
|
143 | + foreach ($users as $user) { |
|
144 | + $event->setAffectedUser($user) |
|
145 | + ->setSubject( |
|
146 | + $user === $currentUser ? $action . '_self' : $action, |
|
147 | + [ |
|
148 | + $currentUser, |
|
149 | + $calendarData['{DAV:}displayname'], |
|
150 | + ] |
|
151 | + ); |
|
152 | + $this->activityManager->publish($event); |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Creates activities for all related users when a calendar was (un-)shared |
|
158 | + * |
|
159 | + * @param array $calendarData |
|
160 | + * @param array $shares |
|
161 | + * @param array $add |
|
162 | + * @param array $remove |
|
163 | + */ |
|
164 | + public function onCalendarUpdateShares(array $calendarData, array $shares, array $add, array $remove) { |
|
165 | + $principal = explode('/', $calendarData['principaluri']); |
|
166 | + $owner = $principal[2]; |
|
167 | + |
|
168 | + $currentUser = $this->userSession->getUser(); |
|
169 | + if ($currentUser instanceof IUser) { |
|
170 | + $currentUser = $currentUser->getUID(); |
|
171 | + } else { |
|
172 | + $currentUser = $owner; |
|
173 | + } |
|
174 | + |
|
175 | + $event = $this->activityManager->generateEvent(); |
|
176 | + $event->setApp('dav') |
|
177 | + ->setObject('calendar', (int) $calendarData['id']) |
|
178 | + ->setType('calendar') |
|
179 | + ->setAuthor($currentUser); |
|
180 | + |
|
181 | + foreach ($remove as $principal) { |
|
182 | + // principal:principals/users/test |
|
183 | + $parts = explode(':', $principal, 2); |
|
184 | + if ($parts[0] !== 'principal') { |
|
185 | + continue; |
|
186 | + } |
|
187 | + $principal = explode('/', $parts[1]); |
|
188 | + |
|
189 | + if ($principal[1] === 'users') { |
|
190 | + $this->triggerActivityUser( |
|
191 | + $principal[2], |
|
192 | + $event, |
|
193 | + $calendarData, |
|
194 | + Calendar::SUBJECT_UNSHARE_USER, |
|
195 | + Calendar::SUBJECT_DELETE . '_self' |
|
196 | + ); |
|
197 | + |
|
198 | + if ($owner !== $principal[2]) { |
|
199 | + $parameters = [ |
|
200 | + $principal[2], |
|
201 | + $calendarData['{DAV:}displayname'], |
|
202 | + ]; |
|
203 | + |
|
204 | + if ($owner === $event->getAuthor()) { |
|
205 | + $subject = Calendar::SUBJECT_UNSHARE_USER . '_you'; |
|
206 | + } else if ($principal[2] === $event->getAuthor()) { |
|
207 | + $subject = Calendar::SUBJECT_UNSHARE_USER . '_self'; |
|
208 | + } else { |
|
209 | + $event->setAffectedUser($event->getAuthor()) |
|
210 | + ->setSubject(Calendar::SUBJECT_UNSHARE_USER . '_you', $parameters); |
|
211 | + $this->activityManager->publish($event); |
|
212 | + |
|
213 | + $subject = Calendar::SUBJECT_UNSHARE_USER . '_by'; |
|
214 | + $parameters[] = $event->getAuthor(); |
|
215 | + } |
|
216 | + |
|
217 | + $event->setAffectedUser($owner) |
|
218 | + ->setSubject($subject, $parameters); |
|
219 | + $this->activityManager->publish($event); |
|
220 | + } |
|
221 | + } else if ($principal[1] === 'groups') { |
|
222 | + $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_UNSHARE_USER); |
|
223 | + |
|
224 | + $parameters = [ |
|
225 | + $principal[2], |
|
226 | + $calendarData['{DAV:}displayname'], |
|
227 | + ]; |
|
228 | + |
|
229 | + if ($owner === $event->getAuthor()) { |
|
230 | + $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_you'; |
|
231 | + } else { |
|
232 | + $event->setAffectedUser($event->getAuthor()) |
|
233 | + ->setSubject(Calendar::SUBJECT_UNSHARE_GROUP . '_you', $parameters); |
|
234 | + $this->activityManager->publish($event); |
|
235 | + |
|
236 | + $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_by'; |
|
237 | + $parameters[] = $event->getAuthor(); |
|
238 | + } |
|
239 | + |
|
240 | + $event->setAffectedUser($owner) |
|
241 | + ->setSubject($subject, $parameters); |
|
242 | + $this->activityManager->publish($event); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + foreach ($add as $share) { |
|
247 | + if ($this->isAlreadyShared($share['href'], $shares)) { |
|
248 | + continue; |
|
249 | + } |
|
250 | + |
|
251 | + // principal:principals/users/test |
|
252 | + $parts = explode(':', $share['href'], 2); |
|
253 | + if ($parts[0] !== 'principal') { |
|
254 | + continue; |
|
255 | + } |
|
256 | + $principal = explode('/', $parts[1]); |
|
257 | + |
|
258 | + if ($principal[1] === 'users') { |
|
259 | + $this->triggerActivityUser($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER); |
|
260 | + |
|
261 | + if ($owner !== $principal[2]) { |
|
262 | + $parameters = [ |
|
263 | + $principal[2], |
|
264 | + $calendarData['{DAV:}displayname'], |
|
265 | + ]; |
|
266 | + |
|
267 | + if ($owner === $event->getAuthor()) { |
|
268 | + $subject = Calendar::SUBJECT_SHARE_USER . '_you'; |
|
269 | + } else { |
|
270 | + $event->setAffectedUser($event->getAuthor()) |
|
271 | + ->setSubject(Calendar::SUBJECT_SHARE_USER . '_you', $parameters); |
|
272 | + $this->activityManager->publish($event); |
|
273 | + |
|
274 | + $subject = Calendar::SUBJECT_SHARE_USER . '_by'; |
|
275 | + $parameters[] = $event->getAuthor(); |
|
276 | + } |
|
277 | + |
|
278 | + $event->setAffectedUser($owner) |
|
279 | + ->setSubject($subject, $parameters); |
|
280 | + $this->activityManager->publish($event); |
|
281 | + } |
|
282 | + } else if ($principal[1] === 'groups') { |
|
283 | + $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER); |
|
284 | + |
|
285 | + $parameters = [ |
|
286 | + $principal[2], |
|
287 | + $calendarData['{DAV:}displayname'], |
|
288 | + ]; |
|
289 | + |
|
290 | + if ($owner === $event->getAuthor()) { |
|
291 | + $subject = Calendar::SUBJECT_SHARE_GROUP . '_you'; |
|
292 | + } else { |
|
293 | + $event->setAffectedUser($event->getAuthor()) |
|
294 | + ->setSubject(Calendar::SUBJECT_SHARE_GROUP . '_you', $parameters); |
|
295 | + $this->activityManager->publish($event); |
|
296 | + |
|
297 | + $subject = Calendar::SUBJECT_SHARE_GROUP . '_by'; |
|
298 | + $parameters[] = $event->getAuthor(); |
|
299 | + } |
|
300 | + |
|
301 | + $event->setAffectedUser($owner) |
|
302 | + ->setSubject($subject, $parameters); |
|
303 | + $this->activityManager->publish($event); |
|
304 | + } |
|
305 | + } |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Checks if a calendar is already shared with a principal |
|
310 | + * |
|
311 | + * @param string $principal |
|
312 | + * @param array[] $shares |
|
313 | + * @return bool |
|
314 | + */ |
|
315 | + protected function isAlreadyShared($principal, $shares) { |
|
316 | + foreach ($shares as $share) { |
|
317 | + if ($principal === $share['href']) { |
|
318 | + return true; |
|
319 | + } |
|
320 | + } |
|
321 | + |
|
322 | + return false; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Creates the given activity for all members of the given group |
|
327 | + * |
|
328 | + * @param string $gid |
|
329 | + * @param IEvent $event |
|
330 | + * @param array $properties |
|
331 | + * @param string $subject |
|
332 | + */ |
|
333 | + protected function triggerActivityGroup($gid, IEvent $event, array $properties, $subject) { |
|
334 | + $group = $this->groupManager->get($gid); |
|
335 | + |
|
336 | + if ($group instanceof IGroup) { |
|
337 | + foreach ($group->getUsers() as $user) { |
|
338 | + // Exclude current user |
|
339 | + if ($user->getUID() !== $event->getAuthor()) { |
|
340 | + $this->triggerActivityUser($user->getUID(), $event, $properties, $subject); |
|
341 | + } |
|
342 | + } |
|
343 | + } |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Creates the given activity for the given user |
|
348 | + * |
|
349 | + * @param string $user |
|
350 | + * @param IEvent $event |
|
351 | + * @param array $properties |
|
352 | + * @param string $subject |
|
353 | + * @param string $subjectSelf |
|
354 | + */ |
|
355 | + protected function triggerActivityUser($user, IEvent $event, array $properties, $subject, $subjectSelf = '') { |
|
356 | + $event->setAffectedUser($user) |
|
357 | + ->setSubject( |
|
358 | + $user === $event->getAuthor() && $subjectSelf ? $subjectSelf : $subject, |
|
359 | + [ |
|
360 | + $event->getAuthor(), |
|
361 | + $properties['{DAV:}displayname'], |
|
362 | + ] |
|
363 | + ); |
|
364 | + |
|
365 | + $this->activityManager->publish($event); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Creates activities when a calendar object was created/updated/deleted |
|
370 | + * |
|
371 | + * @param string $action |
|
372 | + * @param array $calendarData |
|
373 | + * @param array $shares |
|
374 | + * @param array $objectData |
|
375 | + */ |
|
376 | + public function onTouchCalendarObject($action, array $calendarData, array $shares, array $objectData) { |
|
377 | + if (!isset($calendarData['principaluri'])) { |
|
378 | + return; |
|
379 | + } |
|
380 | + |
|
381 | + $principal = explode('/', $calendarData['principaluri']); |
|
382 | + $owner = array_pop($principal); |
|
383 | + |
|
384 | + $currentUser = $this->userSession->getUser(); |
|
385 | + if ($currentUser instanceof IUser) { |
|
386 | + $currentUser = $currentUser->getUID(); |
|
387 | + } else { |
|
388 | + $currentUser = $owner; |
|
389 | + } |
|
390 | + |
|
391 | + $object = $this->getObjectNameAndType($objectData); |
|
392 | + $action = $action . '_' . $object['type']; |
|
393 | + |
|
394 | + if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'COMPLETED') { |
|
395 | + $action .= '_completed'; |
|
396 | + } else if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'NEEDS-ACTION') { |
|
397 | + $action .= '_needs_action'; |
|
398 | + } |
|
399 | + |
|
400 | + $event = $this->activityManager->generateEvent(); |
|
401 | + $event->setApp('dav') |
|
402 | + ->setObject('calendar', (int) $calendarData['id']) |
|
403 | + ->setType($object['type'] === 'event' ? 'calendar_event' : 'calendar_todo') |
|
404 | + ->setAuthor($currentUser); |
|
405 | + |
|
406 | + $users = $this->getUsersForShares($shares); |
|
407 | + $users[] = $owner; |
|
408 | + |
|
409 | + foreach ($users as $user) { |
|
410 | + $event->setAffectedUser($user) |
|
411 | + ->setSubject( |
|
412 | + $user === $currentUser ? $action . '_self' : $action, |
|
413 | + [ |
|
414 | + $currentUser, |
|
415 | + $calendarData['{DAV:}displayname'], |
|
416 | + [ |
|
417 | + 'id' => $object['id'], |
|
418 | + 'name' => $object['name'], |
|
419 | + ], |
|
420 | + ] |
|
421 | + ); |
|
422 | + $this->activityManager->publish($event); |
|
423 | + } |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * @param array $objectData |
|
428 | + * @return string[]|bool |
|
429 | + */ |
|
430 | + protected function getObjectNameAndType(array $objectData) { |
|
431 | + $vObject = Reader::read($objectData['calendardata']); |
|
432 | + $component = $componentType = null; |
|
433 | + foreach($vObject->getComponents() as $component) { |
|
434 | + if (in_array($component->name, ['VEVENT', 'VTODO'])) { |
|
435 | + $componentType = $component->name; |
|
436 | + break; |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + if (!$componentType) { |
|
441 | + // Calendar objects must have a VEVENT or VTODO component |
|
442 | + return false; |
|
443 | + } |
|
444 | + |
|
445 | + if ($componentType === 'VEVENT') { |
|
446 | + return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'event']; |
|
447 | + } |
|
448 | + return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'todo', 'status' => (string) $component->STATUS]; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Get all users that have access to a given calendar |
|
453 | + * |
|
454 | + * @param array $shares |
|
455 | + * @return string[] |
|
456 | + */ |
|
457 | + protected function getUsersForShares(array $shares) { |
|
458 | + $users = $groups = []; |
|
459 | + foreach ($shares as $share) { |
|
460 | + $prinical = explode('/', $share['{http://owncloud.org/ns}principal']); |
|
461 | + if ($prinical[1] === 'users') { |
|
462 | + $users[] = $prinical[2]; |
|
463 | + } else if ($prinical[1] === 'groups') { |
|
464 | + $groups[] = $prinical[2]; |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + if (!empty($groups)) { |
|
469 | + foreach ($groups as $gid) { |
|
470 | + $group = $this->groupManager->get($gid); |
|
471 | + if ($group instanceof IGroup) { |
|
472 | + foreach ($group->getUsers() as $user) { |
|
473 | + $users[] = $user->getUID(); |
|
474 | + } |
|
475 | + } |
|
476 | + } |
|
477 | + } |
|
478 | + |
|
479 | + return array_unique($users); |
|
480 | + } |
|
481 | 481 | } |
@@ -31,182 +31,182 @@ |
||
31 | 31 | |
32 | 32 | class Calendar extends Base { |
33 | 33 | |
34 | - const SUBJECT_ADD = 'calendar_add'; |
|
35 | - const SUBJECT_UPDATE = 'calendar_update'; |
|
36 | - const SUBJECT_DELETE = 'calendar_delete'; |
|
37 | - const SUBJECT_PUBLISH = 'calendar_publish'; |
|
38 | - const SUBJECT_UNPUBLISH = 'calendar_unpublish'; |
|
39 | - const SUBJECT_SHARE_USER = 'calendar_user_share'; |
|
40 | - const SUBJECT_SHARE_GROUP = 'calendar_group_share'; |
|
41 | - const SUBJECT_UNSHARE_USER = 'calendar_user_unshare'; |
|
42 | - const SUBJECT_UNSHARE_GROUP = 'calendar_group_unshare'; |
|
43 | - |
|
44 | - /** @var IFactory */ |
|
45 | - protected $languageFactory; |
|
46 | - |
|
47 | - /** @var IL10N */ |
|
48 | - protected $l; |
|
49 | - |
|
50 | - /** @var IURLGenerator */ |
|
51 | - protected $url; |
|
52 | - |
|
53 | - /** @var IManager */ |
|
54 | - protected $activityManager; |
|
55 | - |
|
56 | - /** @var IEventMerger */ |
|
57 | - protected $eventMerger; |
|
58 | - |
|
59 | - /** |
|
60 | - * @param IFactory $languageFactory |
|
61 | - * @param IURLGenerator $url |
|
62 | - * @param IManager $activityManager |
|
63 | - * @param IUserManager $userManager |
|
64 | - * @param IEventMerger $eventMerger |
|
65 | - */ |
|
66 | - public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) { |
|
67 | - parent::__construct($userManager); |
|
68 | - $this->languageFactory = $languageFactory; |
|
69 | - $this->url = $url; |
|
70 | - $this->activityManager = $activityManager; |
|
71 | - $this->eventMerger = $eventMerger; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param string $language |
|
76 | - * @param IEvent $event |
|
77 | - * @param IEvent|null $previousEvent |
|
78 | - * @return IEvent |
|
79 | - * @throws \InvalidArgumentException |
|
80 | - * @since 11.0.0 |
|
81 | - */ |
|
82 | - public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
83 | - if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar') { |
|
84 | - throw new \InvalidArgumentException(); |
|
85 | - } |
|
86 | - |
|
87 | - $this->l = $this->languageFactory->get('dav', $language); |
|
88 | - |
|
89 | - if ($this->activityManager->getRequirePNG()) { |
|
90 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png'))); |
|
91 | - } else { |
|
92 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.svg'))); |
|
93 | - } |
|
94 | - |
|
95 | - if ($event->getSubject() === self::SUBJECT_ADD) { |
|
96 | - $subject = $this->l->t('{actor} created calendar {calendar}'); |
|
97 | - } else if ($event->getSubject() === self::SUBJECT_ADD . '_self') { |
|
98 | - $subject = $this->l->t('You created calendar {calendar}'); |
|
99 | - } else if ($event->getSubject() === self::SUBJECT_DELETE) { |
|
100 | - $subject = $this->l->t('{actor} deleted calendar {calendar}'); |
|
101 | - } else if ($event->getSubject() === self::SUBJECT_DELETE . '_self') { |
|
102 | - $subject = $this->l->t('You deleted calendar {calendar}'); |
|
103 | - } else if ($event->getSubject() === self::SUBJECT_UPDATE) { |
|
104 | - $subject = $this->l->t('{actor} updated calendar {calendar}'); |
|
105 | - } else if ($event->getSubject() === self::SUBJECT_UPDATE . '_self') { |
|
106 | - $subject = $this->l->t('You updated calendar {calendar}'); |
|
107 | - |
|
108 | - } else if ($event->getSubject() === self::SUBJECT_PUBLISH . '_self') { |
|
109 | - $subject = $this->l->t('You published calendar {calendar}'); |
|
110 | - } else if ($event->getSubject() === self::SUBJECT_UNPUBLISH . '_self') { |
|
111 | - $subject = $this->l->t('You unpublished calendar {calendar}'); |
|
112 | - |
|
113 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_USER) { |
|
114 | - $subject = $this->l->t('{actor} shared calendar {calendar} with you'); |
|
115 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') { |
|
116 | - $subject = $this->l->t('You shared calendar {calendar} with {user}'); |
|
117 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') { |
|
118 | - $subject = $this->l->t('{actor} shared calendar {calendar} with {user}'); |
|
119 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER) { |
|
120 | - $subject = $this->l->t('{actor} unshared calendar {calendar} from you'); |
|
121 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') { |
|
122 | - $subject = $this->l->t('You unshared calendar {calendar} from {user}'); |
|
123 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') { |
|
124 | - $subject = $this->l->t('{actor} unshared calendar {calendar} from {user}'); |
|
125 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') { |
|
126 | - $subject = $this->l->t('{actor} unshared calendar {calendar} from themselves'); |
|
127 | - |
|
128 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') { |
|
129 | - $subject = $this->l->t('You shared calendar {calendar} with group {group}'); |
|
130 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') { |
|
131 | - $subject = $this->l->t('{actor} shared calendar {calendar} with group {group}'); |
|
132 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') { |
|
133 | - $subject = $this->l->t('You unshared calendar {calendar} from group {group}'); |
|
134 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') { |
|
135 | - $subject = $this->l->t('{actor} unshared calendar {calendar} from group {group}'); |
|
136 | - } else { |
|
137 | - throw new \InvalidArgumentException(); |
|
138 | - } |
|
139 | - |
|
140 | - $parsedParameters = $this->getParameters($event); |
|
141 | - $this->setSubjects($event, $subject, $parsedParameters); |
|
142 | - |
|
143 | - $event = $this->eventMerger->mergeEvents('calendar', $event, $previousEvent); |
|
144 | - |
|
145 | - if ($event->getChildEvent() === null) { |
|
146 | - if (isset($parsedParameters['user'])) { |
|
147 | - // Couldn't group by calendar, maybe we can group by users |
|
148 | - $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent); |
|
149 | - } else if (isset($parsedParameters['group'])) { |
|
150 | - // Couldn't group by calendar, maybe we can group by groups |
|
151 | - $event = $this->eventMerger->mergeEvents('group', $event, $previousEvent); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - return $event; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param IEvent $event |
|
160 | - * @return array |
|
161 | - */ |
|
162 | - protected function getParameters(IEvent $event) { |
|
163 | - $subject = $event->getSubject(); |
|
164 | - $parameters = $event->getSubjectParameters(); |
|
165 | - |
|
166 | - switch ($subject) { |
|
167 | - case self::SUBJECT_ADD: |
|
168 | - case self::SUBJECT_ADD . '_self': |
|
169 | - case self::SUBJECT_DELETE: |
|
170 | - case self::SUBJECT_DELETE . '_self': |
|
171 | - case self::SUBJECT_UPDATE: |
|
172 | - case self::SUBJECT_UPDATE . '_self': |
|
173 | - case self::SUBJECT_PUBLISH . '_self': |
|
174 | - case self::SUBJECT_UNPUBLISH . '_self': |
|
175 | - case self::SUBJECT_SHARE_USER: |
|
176 | - case self::SUBJECT_UNSHARE_USER: |
|
177 | - case self::SUBJECT_UNSHARE_USER . '_self': |
|
178 | - return [ |
|
179 | - 'actor' => $this->generateUserParameter($parameters[0]), |
|
180 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
181 | - ]; |
|
182 | - case self::SUBJECT_SHARE_USER . '_you': |
|
183 | - case self::SUBJECT_UNSHARE_USER . '_you': |
|
184 | - return [ |
|
185 | - 'user' => $this->generateUserParameter($parameters[0]), |
|
186 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
187 | - ]; |
|
188 | - case self::SUBJECT_SHARE_USER . '_by': |
|
189 | - case self::SUBJECT_UNSHARE_USER . '_by': |
|
190 | - return [ |
|
191 | - 'user' => $this->generateUserParameter($parameters[0]), |
|
192 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
193 | - 'actor' => $this->generateUserParameter($parameters[2]), |
|
194 | - ]; |
|
195 | - case self::SUBJECT_SHARE_GROUP . '_you': |
|
196 | - case self::SUBJECT_UNSHARE_GROUP . '_you': |
|
197 | - return [ |
|
198 | - 'group' => $this->generateGroupParameter($parameters[0]), |
|
199 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
200 | - ]; |
|
201 | - case self::SUBJECT_SHARE_GROUP . '_by': |
|
202 | - case self::SUBJECT_UNSHARE_GROUP . '_by': |
|
203 | - return [ |
|
204 | - 'group' => $this->generateGroupParameter($parameters[0]), |
|
205 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
206 | - 'actor' => $this->generateUserParameter($parameters[2]), |
|
207 | - ]; |
|
208 | - } |
|
209 | - |
|
210 | - throw new \InvalidArgumentException(); |
|
211 | - } |
|
34 | + const SUBJECT_ADD = 'calendar_add'; |
|
35 | + const SUBJECT_UPDATE = 'calendar_update'; |
|
36 | + const SUBJECT_DELETE = 'calendar_delete'; |
|
37 | + const SUBJECT_PUBLISH = 'calendar_publish'; |
|
38 | + const SUBJECT_UNPUBLISH = 'calendar_unpublish'; |
|
39 | + const SUBJECT_SHARE_USER = 'calendar_user_share'; |
|
40 | + const SUBJECT_SHARE_GROUP = 'calendar_group_share'; |
|
41 | + const SUBJECT_UNSHARE_USER = 'calendar_user_unshare'; |
|
42 | + const SUBJECT_UNSHARE_GROUP = 'calendar_group_unshare'; |
|
43 | + |
|
44 | + /** @var IFactory */ |
|
45 | + protected $languageFactory; |
|
46 | + |
|
47 | + /** @var IL10N */ |
|
48 | + protected $l; |
|
49 | + |
|
50 | + /** @var IURLGenerator */ |
|
51 | + protected $url; |
|
52 | + |
|
53 | + /** @var IManager */ |
|
54 | + protected $activityManager; |
|
55 | + |
|
56 | + /** @var IEventMerger */ |
|
57 | + protected $eventMerger; |
|
58 | + |
|
59 | + /** |
|
60 | + * @param IFactory $languageFactory |
|
61 | + * @param IURLGenerator $url |
|
62 | + * @param IManager $activityManager |
|
63 | + * @param IUserManager $userManager |
|
64 | + * @param IEventMerger $eventMerger |
|
65 | + */ |
|
66 | + public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) { |
|
67 | + parent::__construct($userManager); |
|
68 | + $this->languageFactory = $languageFactory; |
|
69 | + $this->url = $url; |
|
70 | + $this->activityManager = $activityManager; |
|
71 | + $this->eventMerger = $eventMerger; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param string $language |
|
76 | + * @param IEvent $event |
|
77 | + * @param IEvent|null $previousEvent |
|
78 | + * @return IEvent |
|
79 | + * @throws \InvalidArgumentException |
|
80 | + * @since 11.0.0 |
|
81 | + */ |
|
82 | + public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
83 | + if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar') { |
|
84 | + throw new \InvalidArgumentException(); |
|
85 | + } |
|
86 | + |
|
87 | + $this->l = $this->languageFactory->get('dav', $language); |
|
88 | + |
|
89 | + if ($this->activityManager->getRequirePNG()) { |
|
90 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png'))); |
|
91 | + } else { |
|
92 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.svg'))); |
|
93 | + } |
|
94 | + |
|
95 | + if ($event->getSubject() === self::SUBJECT_ADD) { |
|
96 | + $subject = $this->l->t('{actor} created calendar {calendar}'); |
|
97 | + } else if ($event->getSubject() === self::SUBJECT_ADD . '_self') { |
|
98 | + $subject = $this->l->t('You created calendar {calendar}'); |
|
99 | + } else if ($event->getSubject() === self::SUBJECT_DELETE) { |
|
100 | + $subject = $this->l->t('{actor} deleted calendar {calendar}'); |
|
101 | + } else if ($event->getSubject() === self::SUBJECT_DELETE . '_self') { |
|
102 | + $subject = $this->l->t('You deleted calendar {calendar}'); |
|
103 | + } else if ($event->getSubject() === self::SUBJECT_UPDATE) { |
|
104 | + $subject = $this->l->t('{actor} updated calendar {calendar}'); |
|
105 | + } else if ($event->getSubject() === self::SUBJECT_UPDATE . '_self') { |
|
106 | + $subject = $this->l->t('You updated calendar {calendar}'); |
|
107 | + |
|
108 | + } else if ($event->getSubject() === self::SUBJECT_PUBLISH . '_self') { |
|
109 | + $subject = $this->l->t('You published calendar {calendar}'); |
|
110 | + } else if ($event->getSubject() === self::SUBJECT_UNPUBLISH . '_self') { |
|
111 | + $subject = $this->l->t('You unpublished calendar {calendar}'); |
|
112 | + |
|
113 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_USER) { |
|
114 | + $subject = $this->l->t('{actor} shared calendar {calendar} with you'); |
|
115 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') { |
|
116 | + $subject = $this->l->t('You shared calendar {calendar} with {user}'); |
|
117 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') { |
|
118 | + $subject = $this->l->t('{actor} shared calendar {calendar} with {user}'); |
|
119 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER) { |
|
120 | + $subject = $this->l->t('{actor} unshared calendar {calendar} from you'); |
|
121 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') { |
|
122 | + $subject = $this->l->t('You unshared calendar {calendar} from {user}'); |
|
123 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') { |
|
124 | + $subject = $this->l->t('{actor} unshared calendar {calendar} from {user}'); |
|
125 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') { |
|
126 | + $subject = $this->l->t('{actor} unshared calendar {calendar} from themselves'); |
|
127 | + |
|
128 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') { |
|
129 | + $subject = $this->l->t('You shared calendar {calendar} with group {group}'); |
|
130 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') { |
|
131 | + $subject = $this->l->t('{actor} shared calendar {calendar} with group {group}'); |
|
132 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') { |
|
133 | + $subject = $this->l->t('You unshared calendar {calendar} from group {group}'); |
|
134 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') { |
|
135 | + $subject = $this->l->t('{actor} unshared calendar {calendar} from group {group}'); |
|
136 | + } else { |
|
137 | + throw new \InvalidArgumentException(); |
|
138 | + } |
|
139 | + |
|
140 | + $parsedParameters = $this->getParameters($event); |
|
141 | + $this->setSubjects($event, $subject, $parsedParameters); |
|
142 | + |
|
143 | + $event = $this->eventMerger->mergeEvents('calendar', $event, $previousEvent); |
|
144 | + |
|
145 | + if ($event->getChildEvent() === null) { |
|
146 | + if (isset($parsedParameters['user'])) { |
|
147 | + // Couldn't group by calendar, maybe we can group by users |
|
148 | + $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent); |
|
149 | + } else if (isset($parsedParameters['group'])) { |
|
150 | + // Couldn't group by calendar, maybe we can group by groups |
|
151 | + $event = $this->eventMerger->mergeEvents('group', $event, $previousEvent); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + return $event; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param IEvent $event |
|
160 | + * @return array |
|
161 | + */ |
|
162 | + protected function getParameters(IEvent $event) { |
|
163 | + $subject = $event->getSubject(); |
|
164 | + $parameters = $event->getSubjectParameters(); |
|
165 | + |
|
166 | + switch ($subject) { |
|
167 | + case self::SUBJECT_ADD: |
|
168 | + case self::SUBJECT_ADD . '_self': |
|
169 | + case self::SUBJECT_DELETE: |
|
170 | + case self::SUBJECT_DELETE . '_self': |
|
171 | + case self::SUBJECT_UPDATE: |
|
172 | + case self::SUBJECT_UPDATE . '_self': |
|
173 | + case self::SUBJECT_PUBLISH . '_self': |
|
174 | + case self::SUBJECT_UNPUBLISH . '_self': |
|
175 | + case self::SUBJECT_SHARE_USER: |
|
176 | + case self::SUBJECT_UNSHARE_USER: |
|
177 | + case self::SUBJECT_UNSHARE_USER . '_self': |
|
178 | + return [ |
|
179 | + 'actor' => $this->generateUserParameter($parameters[0]), |
|
180 | + 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
181 | + ]; |
|
182 | + case self::SUBJECT_SHARE_USER . '_you': |
|
183 | + case self::SUBJECT_UNSHARE_USER . '_you': |
|
184 | + return [ |
|
185 | + 'user' => $this->generateUserParameter($parameters[0]), |
|
186 | + 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
187 | + ]; |
|
188 | + case self::SUBJECT_SHARE_USER . '_by': |
|
189 | + case self::SUBJECT_UNSHARE_USER . '_by': |
|
190 | + return [ |
|
191 | + 'user' => $this->generateUserParameter($parameters[0]), |
|
192 | + 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
193 | + 'actor' => $this->generateUserParameter($parameters[2]), |
|
194 | + ]; |
|
195 | + case self::SUBJECT_SHARE_GROUP . '_you': |
|
196 | + case self::SUBJECT_UNSHARE_GROUP . '_you': |
|
197 | + return [ |
|
198 | + 'group' => $this->generateGroupParameter($parameters[0]), |
|
199 | + 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
200 | + ]; |
|
201 | + case self::SUBJECT_SHARE_GROUP . '_by': |
|
202 | + case self::SUBJECT_UNSHARE_GROUP . '_by': |
|
203 | + return [ |
|
204 | + 'group' => $this->generateGroupParameter($parameters[0]), |
|
205 | + 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
206 | + 'actor' => $this->generateUserParameter($parameters[2]), |
|
207 | + ]; |
|
208 | + } |
|
209 | + |
|
210 | + throw new \InvalidArgumentException(); |
|
211 | + } |
|
212 | 212 | } |
@@ -94,44 +94,44 @@ discard block |
||
94 | 94 | |
95 | 95 | if ($event->getSubject() === self::SUBJECT_ADD) { |
96 | 96 | $subject = $this->l->t('{actor} created calendar {calendar}'); |
97 | - } else if ($event->getSubject() === self::SUBJECT_ADD . '_self') { |
|
97 | + } else if ($event->getSubject() === self::SUBJECT_ADD.'_self') { |
|
98 | 98 | $subject = $this->l->t('You created calendar {calendar}'); |
99 | 99 | } else if ($event->getSubject() === self::SUBJECT_DELETE) { |
100 | 100 | $subject = $this->l->t('{actor} deleted calendar {calendar}'); |
101 | - } else if ($event->getSubject() === self::SUBJECT_DELETE . '_self') { |
|
101 | + } else if ($event->getSubject() === self::SUBJECT_DELETE.'_self') { |
|
102 | 102 | $subject = $this->l->t('You deleted calendar {calendar}'); |
103 | 103 | } else if ($event->getSubject() === self::SUBJECT_UPDATE) { |
104 | 104 | $subject = $this->l->t('{actor} updated calendar {calendar}'); |
105 | - } else if ($event->getSubject() === self::SUBJECT_UPDATE . '_self') { |
|
105 | + } else if ($event->getSubject() === self::SUBJECT_UPDATE.'_self') { |
|
106 | 106 | $subject = $this->l->t('You updated calendar {calendar}'); |
107 | 107 | |
108 | - } else if ($event->getSubject() === self::SUBJECT_PUBLISH . '_self') { |
|
108 | + } else if ($event->getSubject() === self::SUBJECT_PUBLISH.'_self') { |
|
109 | 109 | $subject = $this->l->t('You published calendar {calendar}'); |
110 | - } else if ($event->getSubject() === self::SUBJECT_UNPUBLISH . '_self') { |
|
110 | + } else if ($event->getSubject() === self::SUBJECT_UNPUBLISH.'_self') { |
|
111 | 111 | $subject = $this->l->t('You unpublished calendar {calendar}'); |
112 | 112 | |
113 | 113 | } else if ($event->getSubject() === self::SUBJECT_SHARE_USER) { |
114 | 114 | $subject = $this->l->t('{actor} shared calendar {calendar} with you'); |
115 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') { |
|
115 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_USER.'_you') { |
|
116 | 116 | $subject = $this->l->t('You shared calendar {calendar} with {user}'); |
117 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') { |
|
117 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_USER.'_by') { |
|
118 | 118 | $subject = $this->l->t('{actor} shared calendar {calendar} with {user}'); |
119 | 119 | } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER) { |
120 | 120 | $subject = $this->l->t('{actor} unshared calendar {calendar} from you'); |
121 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') { |
|
121 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER.'_you') { |
|
122 | 122 | $subject = $this->l->t('You unshared calendar {calendar} from {user}'); |
123 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') { |
|
123 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER.'_by') { |
|
124 | 124 | $subject = $this->l->t('{actor} unshared calendar {calendar} from {user}'); |
125 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') { |
|
125 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER.'_self') { |
|
126 | 126 | $subject = $this->l->t('{actor} unshared calendar {calendar} from themselves'); |
127 | 127 | |
128 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') { |
|
128 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP.'_you') { |
|
129 | 129 | $subject = $this->l->t('You shared calendar {calendar} with group {group}'); |
130 | - } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') { |
|
130 | + } else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP.'_by') { |
|
131 | 131 | $subject = $this->l->t('{actor} shared calendar {calendar} with group {group}'); |
132 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') { |
|
132 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP.'_you') { |
|
133 | 133 | $subject = $this->l->t('You unshared calendar {calendar} from group {group}'); |
134 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') { |
|
134 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP.'_by') { |
|
135 | 135 | $subject = $this->l->t('{actor} unshared calendar {calendar} from group {group}'); |
136 | 136 | } else { |
137 | 137 | throw new \InvalidArgumentException(); |
@@ -165,44 +165,44 @@ discard block |
||
165 | 165 | |
166 | 166 | switch ($subject) { |
167 | 167 | case self::SUBJECT_ADD: |
168 | - case self::SUBJECT_ADD . '_self': |
|
168 | + case self::SUBJECT_ADD.'_self': |
|
169 | 169 | case self::SUBJECT_DELETE: |
170 | - case self::SUBJECT_DELETE . '_self': |
|
170 | + case self::SUBJECT_DELETE.'_self': |
|
171 | 171 | case self::SUBJECT_UPDATE: |
172 | - case self::SUBJECT_UPDATE . '_self': |
|
173 | - case self::SUBJECT_PUBLISH . '_self': |
|
174 | - case self::SUBJECT_UNPUBLISH . '_self': |
|
172 | + case self::SUBJECT_UPDATE.'_self': |
|
173 | + case self::SUBJECT_PUBLISH.'_self': |
|
174 | + case self::SUBJECT_UNPUBLISH.'_self': |
|
175 | 175 | case self::SUBJECT_SHARE_USER: |
176 | 176 | case self::SUBJECT_UNSHARE_USER: |
177 | - case self::SUBJECT_UNSHARE_USER . '_self': |
|
177 | + case self::SUBJECT_UNSHARE_USER.'_self': |
|
178 | 178 | return [ |
179 | 179 | 'actor' => $this->generateUserParameter($parameters[0]), |
180 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
180 | + 'calendar' => $this->generateCalendarParameter((int) $event->getObjectId(), $parameters[1]), |
|
181 | 181 | ]; |
182 | - case self::SUBJECT_SHARE_USER . '_you': |
|
183 | - case self::SUBJECT_UNSHARE_USER . '_you': |
|
182 | + case self::SUBJECT_SHARE_USER.'_you': |
|
183 | + case self::SUBJECT_UNSHARE_USER.'_you': |
|
184 | 184 | return [ |
185 | 185 | 'user' => $this->generateUserParameter($parameters[0]), |
186 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
186 | + 'calendar' => $this->generateCalendarParameter((int) $event->getObjectId(), $parameters[1]), |
|
187 | 187 | ]; |
188 | - case self::SUBJECT_SHARE_USER . '_by': |
|
189 | - case self::SUBJECT_UNSHARE_USER . '_by': |
|
188 | + case self::SUBJECT_SHARE_USER.'_by': |
|
189 | + case self::SUBJECT_UNSHARE_USER.'_by': |
|
190 | 190 | return [ |
191 | 191 | 'user' => $this->generateUserParameter($parameters[0]), |
192 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
192 | + 'calendar' => $this->generateCalendarParameter((int) $event->getObjectId(), $parameters[1]), |
|
193 | 193 | 'actor' => $this->generateUserParameter($parameters[2]), |
194 | 194 | ]; |
195 | - case self::SUBJECT_SHARE_GROUP . '_you': |
|
196 | - case self::SUBJECT_UNSHARE_GROUP . '_you': |
|
195 | + case self::SUBJECT_SHARE_GROUP.'_you': |
|
196 | + case self::SUBJECT_UNSHARE_GROUP.'_you': |
|
197 | 197 | return [ |
198 | 198 | 'group' => $this->generateGroupParameter($parameters[0]), |
199 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
199 | + 'calendar' => $this->generateCalendarParameter((int) $event->getObjectId(), $parameters[1]), |
|
200 | 200 | ]; |
201 | - case self::SUBJECT_SHARE_GROUP . '_by': |
|
202 | - case self::SUBJECT_UNSHARE_GROUP . '_by': |
|
201 | + case self::SUBJECT_SHARE_GROUP.'_by': |
|
202 | + case self::SUBJECT_UNSHARE_GROUP.'_by': |
|
203 | 203 | return [ |
204 | 204 | 'group' => $this->generateGroupParameter($parameters[0]), |
205 | - 'calendar' => $this->generateCalendarParameter((int)$event->getObjectId(), $parameters[1]), |
|
205 | + 'calendar' => $this->generateCalendarParameter((int) $event->getObjectId(), $parameters[1]), |
|
206 | 206 | 'actor' => $this->generateUserParameter($parameters[2]), |
207 | 207 | ]; |
208 | 208 | } |
@@ -63,2079 +63,2079 @@ |
||
63 | 63 | */ |
64 | 64 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
65 | 65 | |
66 | - const PERSONAL_CALENDAR_URI = 'personal'; |
|
67 | - const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
68 | - |
|
69 | - /** |
|
70 | - * We need to specify a max date, because we need to stop *somewhere* |
|
71 | - * |
|
72 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
73 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
74 | - * in 2038-01-19 to avoid problems when the date is converted |
|
75 | - * to a unix timestamp. |
|
76 | - */ |
|
77 | - const MAX_DATE = '2038-01-01'; |
|
78 | - |
|
79 | - const ACCESS_PUBLIC = 4; |
|
80 | - const CLASSIFICATION_PUBLIC = 0; |
|
81 | - const CLASSIFICATION_PRIVATE = 1; |
|
82 | - const CLASSIFICATION_CONFIDENTIAL = 2; |
|
83 | - |
|
84 | - /** |
|
85 | - * List of CalDAV properties, and how they map to database field names |
|
86 | - * Add your own properties by simply adding on to this array. |
|
87 | - * |
|
88 | - * Note that only string-based properties are supported here. |
|
89 | - * |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - public $propertyMap = [ |
|
93 | - '{DAV:}displayname' => 'displayname', |
|
94 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
95 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
96 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
97 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
98 | - ]; |
|
99 | - |
|
100 | - /** |
|
101 | - * List of subscription properties, and how they map to database field names. |
|
102 | - * |
|
103 | - * @var array |
|
104 | - */ |
|
105 | - public $subscriptionPropertyMap = [ |
|
106 | - '{DAV:}displayname' => 'displayname', |
|
107 | - '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
108 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
109 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
110 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
111 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
112 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
113 | - ]; |
|
114 | - |
|
115 | - /** @var array properties to index */ |
|
116 | - public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
|
117 | - 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
|
118 | - 'ORGANIZER']; |
|
119 | - |
|
120 | - /** @var array parameters to index */ |
|
121 | - public static $indexParameters = [ |
|
122 | - 'ATTENDEE' => ['CN'], |
|
123 | - 'ORGANIZER' => ['CN'], |
|
124 | - ]; |
|
125 | - |
|
126 | - /** |
|
127 | - * @var string[] Map of uid => display name |
|
128 | - */ |
|
129 | - protected $userDisplayNames; |
|
130 | - |
|
131 | - /** @var IDBConnection */ |
|
132 | - private $db; |
|
133 | - |
|
134 | - /** @var Backend */ |
|
135 | - private $sharingBackend; |
|
136 | - |
|
137 | - /** @var Principal */ |
|
138 | - private $principalBackend; |
|
139 | - |
|
140 | - /** @var IUserManager */ |
|
141 | - private $userManager; |
|
142 | - |
|
143 | - /** @var ISecureRandom */ |
|
144 | - private $random; |
|
145 | - |
|
146 | - /** @var EventDispatcherInterface */ |
|
147 | - private $dispatcher; |
|
148 | - |
|
149 | - /** @var bool */ |
|
150 | - private $legacyEndpoint; |
|
151 | - |
|
152 | - /** @var string */ |
|
153 | - private $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
154 | - |
|
155 | - /** |
|
156 | - * CalDavBackend constructor. |
|
157 | - * |
|
158 | - * @param IDBConnection $db |
|
159 | - * @param Principal $principalBackend |
|
160 | - * @param IUserManager $userManager |
|
161 | - * @param ISecureRandom $random |
|
162 | - * @param EventDispatcherInterface $dispatcher |
|
163 | - * @param bool $legacyEndpoint |
|
164 | - */ |
|
165 | - public function __construct(IDBConnection $db, |
|
166 | - Principal $principalBackend, |
|
167 | - IUserManager $userManager, |
|
168 | - ISecureRandom $random, |
|
169 | - EventDispatcherInterface $dispatcher, |
|
170 | - $legacyEndpoint = false) { |
|
171 | - $this->db = $db; |
|
172 | - $this->principalBackend = $principalBackend; |
|
173 | - $this->userManager = $userManager; |
|
174 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
175 | - $this->random = $random; |
|
176 | - $this->dispatcher = $dispatcher; |
|
177 | - $this->legacyEndpoint = $legacyEndpoint; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Return the number of calendars for a principal |
|
182 | - * |
|
183 | - * By default this excludes the automatically generated birthday calendar |
|
184 | - * |
|
185 | - * @param $principalUri |
|
186 | - * @param bool $excludeBirthday |
|
187 | - * @return int |
|
188 | - */ |
|
189 | - public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
190 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
191 | - $query = $this->db->getQueryBuilder(); |
|
192 | - $query->select($query->createFunction('COUNT(*)')) |
|
193 | - ->from('calendars') |
|
194 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
195 | - |
|
196 | - if ($excludeBirthday) { |
|
197 | - $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
198 | - } |
|
199 | - |
|
200 | - return (int)$query->execute()->fetchColumn(); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Returns a list of calendars for a principal. |
|
205 | - * |
|
206 | - * Every project is an array with the following keys: |
|
207 | - * * id, a unique id that will be used by other functions to modify the |
|
208 | - * calendar. This can be the same as the uri or a database key. |
|
209 | - * * uri, which the basename of the uri with which the calendar is |
|
210 | - * accessed. |
|
211 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
212 | - * principalUri passed to this method. |
|
213 | - * |
|
214 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
215 | - * common one is '{DAV:}displayname'. |
|
216 | - * |
|
217 | - * Many clients also require: |
|
218 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
219 | - * For this property, you can just return an instance of |
|
220 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
221 | - * |
|
222 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
223 | - * ACL will automatically be put in read-only mode. |
|
224 | - * |
|
225 | - * @param string $principalUri |
|
226 | - * @return array |
|
227 | - */ |
|
228 | - function getCalendarsForUser($principalUri) { |
|
229 | - $principalUriOriginal = $principalUri; |
|
230 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
231 | - $fields = array_values($this->propertyMap); |
|
232 | - $fields[] = 'id'; |
|
233 | - $fields[] = 'uri'; |
|
234 | - $fields[] = 'synctoken'; |
|
235 | - $fields[] = 'components'; |
|
236 | - $fields[] = 'principaluri'; |
|
237 | - $fields[] = 'transparent'; |
|
238 | - |
|
239 | - // Making fields a comma-delimited list |
|
240 | - $query = $this->db->getQueryBuilder(); |
|
241 | - $query->select($fields)->from('calendars') |
|
242 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
243 | - ->orderBy('calendarorder', 'ASC'); |
|
244 | - $stmt = $query->execute(); |
|
245 | - |
|
246 | - $calendars = []; |
|
247 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
248 | - |
|
249 | - $components = []; |
|
250 | - if ($row['components']) { |
|
251 | - $components = explode(',',$row['components']); |
|
252 | - } |
|
253 | - |
|
254 | - $calendar = [ |
|
255 | - 'id' => $row['id'], |
|
256 | - 'uri' => $row['uri'], |
|
257 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
258 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
259 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
260 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
261 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
262 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
263 | - ]; |
|
264 | - |
|
265 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
266 | - $calendar[$xmlName] = $row[$dbName]; |
|
267 | - } |
|
268 | - |
|
269 | - $this->addOwnerPrincipal($calendar); |
|
270 | - |
|
271 | - if (!isset($calendars[$calendar['id']])) { |
|
272 | - $calendars[$calendar['id']] = $calendar; |
|
273 | - } |
|
274 | - } |
|
275 | - |
|
276 | - $stmt->closeCursor(); |
|
277 | - |
|
278 | - // query for shared calendars |
|
279 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
280 | - $principals = array_map(function($principal) { |
|
281 | - return urldecode($principal); |
|
282 | - }, $principals); |
|
283 | - $principals[]= $principalUri; |
|
284 | - |
|
285 | - $fields = array_values($this->propertyMap); |
|
286 | - $fields[] = 'a.id'; |
|
287 | - $fields[] = 'a.uri'; |
|
288 | - $fields[] = 'a.synctoken'; |
|
289 | - $fields[] = 'a.components'; |
|
290 | - $fields[] = 'a.principaluri'; |
|
291 | - $fields[] = 'a.transparent'; |
|
292 | - $fields[] = 's.access'; |
|
293 | - $query = $this->db->getQueryBuilder(); |
|
294 | - $result = $query->select($fields) |
|
295 | - ->from('dav_shares', 's') |
|
296 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
297 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
298 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
299 | - ->setParameter('type', 'calendar') |
|
300 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
301 | - ->execute(); |
|
302 | - |
|
303 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
304 | - while($row = $result->fetch()) { |
|
305 | - if ($row['principaluri'] === $principalUri) { |
|
306 | - continue; |
|
307 | - } |
|
308 | - |
|
309 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
310 | - if (isset($calendars[$row['id']])) { |
|
311 | - if ($readOnly) { |
|
312 | - // New share can not have more permissions then the old one. |
|
313 | - continue; |
|
314 | - } |
|
315 | - if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
316 | - $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
317 | - // Old share is already read-write, no more permissions can be gained |
|
318 | - continue; |
|
319 | - } |
|
320 | - } |
|
321 | - |
|
322 | - list(, $name) = Uri\split($row['principaluri']); |
|
323 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
324 | - $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
325 | - $components = []; |
|
326 | - if ($row['components']) { |
|
327 | - $components = explode(',',$row['components']); |
|
328 | - } |
|
329 | - $calendar = [ |
|
330 | - 'id' => $row['id'], |
|
331 | - 'uri' => $uri, |
|
332 | - 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
333 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
334 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
335 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
336 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
337 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
338 | - $readOnlyPropertyName => $readOnly, |
|
339 | - ]; |
|
340 | - |
|
341 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
342 | - $calendar[$xmlName] = $row[$dbName]; |
|
343 | - } |
|
344 | - |
|
345 | - $this->addOwnerPrincipal($calendar); |
|
346 | - |
|
347 | - $calendars[$calendar['id']] = $calendar; |
|
348 | - } |
|
349 | - $result->closeCursor(); |
|
350 | - |
|
351 | - return array_values($calendars); |
|
352 | - } |
|
353 | - |
|
354 | - public function getUsersOwnCalendars($principalUri) { |
|
355 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
356 | - $fields = array_values($this->propertyMap); |
|
357 | - $fields[] = 'id'; |
|
358 | - $fields[] = 'uri'; |
|
359 | - $fields[] = 'synctoken'; |
|
360 | - $fields[] = 'components'; |
|
361 | - $fields[] = 'principaluri'; |
|
362 | - $fields[] = 'transparent'; |
|
363 | - // Making fields a comma-delimited list |
|
364 | - $query = $this->db->getQueryBuilder(); |
|
365 | - $query->select($fields)->from('calendars') |
|
366 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
367 | - ->orderBy('calendarorder', 'ASC'); |
|
368 | - $stmt = $query->execute(); |
|
369 | - $calendars = []; |
|
370 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
371 | - $components = []; |
|
372 | - if ($row['components']) { |
|
373 | - $components = explode(',',$row['components']); |
|
374 | - } |
|
375 | - $calendar = [ |
|
376 | - 'id' => $row['id'], |
|
377 | - 'uri' => $row['uri'], |
|
378 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
379 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
380 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
381 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
382 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
383 | - ]; |
|
384 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
385 | - $calendar[$xmlName] = $row[$dbName]; |
|
386 | - } |
|
387 | - |
|
388 | - $this->addOwnerPrincipal($calendar); |
|
389 | - |
|
390 | - if (!isset($calendars[$calendar['id']])) { |
|
391 | - $calendars[$calendar['id']] = $calendar; |
|
392 | - } |
|
393 | - } |
|
394 | - $stmt->closeCursor(); |
|
395 | - return array_values($calendars); |
|
396 | - } |
|
397 | - |
|
398 | - |
|
399 | - private function getUserDisplayName($uid) { |
|
400 | - if (!isset($this->userDisplayNames[$uid])) { |
|
401 | - $user = $this->userManager->get($uid); |
|
402 | - |
|
403 | - if ($user instanceof IUser) { |
|
404 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
405 | - } else { |
|
406 | - $this->userDisplayNames[$uid] = $uid; |
|
407 | - } |
|
408 | - } |
|
409 | - |
|
410 | - return $this->userDisplayNames[$uid]; |
|
411 | - } |
|
66 | + const PERSONAL_CALENDAR_URI = 'personal'; |
|
67 | + const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
68 | + |
|
69 | + /** |
|
70 | + * We need to specify a max date, because we need to stop *somewhere* |
|
71 | + * |
|
72 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
73 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
74 | + * in 2038-01-19 to avoid problems when the date is converted |
|
75 | + * to a unix timestamp. |
|
76 | + */ |
|
77 | + const MAX_DATE = '2038-01-01'; |
|
78 | + |
|
79 | + const ACCESS_PUBLIC = 4; |
|
80 | + const CLASSIFICATION_PUBLIC = 0; |
|
81 | + const CLASSIFICATION_PRIVATE = 1; |
|
82 | + const CLASSIFICATION_CONFIDENTIAL = 2; |
|
83 | + |
|
84 | + /** |
|
85 | + * List of CalDAV properties, and how they map to database field names |
|
86 | + * Add your own properties by simply adding on to this array. |
|
87 | + * |
|
88 | + * Note that only string-based properties are supported here. |
|
89 | + * |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + public $propertyMap = [ |
|
93 | + '{DAV:}displayname' => 'displayname', |
|
94 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
95 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
96 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
97 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
98 | + ]; |
|
99 | + |
|
100 | + /** |
|
101 | + * List of subscription properties, and how they map to database field names. |
|
102 | + * |
|
103 | + * @var array |
|
104 | + */ |
|
105 | + public $subscriptionPropertyMap = [ |
|
106 | + '{DAV:}displayname' => 'displayname', |
|
107 | + '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
108 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
109 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
110 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
111 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
112 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
113 | + ]; |
|
114 | + |
|
115 | + /** @var array properties to index */ |
|
116 | + public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
|
117 | + 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
|
118 | + 'ORGANIZER']; |
|
119 | + |
|
120 | + /** @var array parameters to index */ |
|
121 | + public static $indexParameters = [ |
|
122 | + 'ATTENDEE' => ['CN'], |
|
123 | + 'ORGANIZER' => ['CN'], |
|
124 | + ]; |
|
125 | + |
|
126 | + /** |
|
127 | + * @var string[] Map of uid => display name |
|
128 | + */ |
|
129 | + protected $userDisplayNames; |
|
130 | + |
|
131 | + /** @var IDBConnection */ |
|
132 | + private $db; |
|
133 | + |
|
134 | + /** @var Backend */ |
|
135 | + private $sharingBackend; |
|
136 | + |
|
137 | + /** @var Principal */ |
|
138 | + private $principalBackend; |
|
139 | + |
|
140 | + /** @var IUserManager */ |
|
141 | + private $userManager; |
|
142 | + |
|
143 | + /** @var ISecureRandom */ |
|
144 | + private $random; |
|
145 | + |
|
146 | + /** @var EventDispatcherInterface */ |
|
147 | + private $dispatcher; |
|
148 | + |
|
149 | + /** @var bool */ |
|
150 | + private $legacyEndpoint; |
|
151 | + |
|
152 | + /** @var string */ |
|
153 | + private $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
154 | + |
|
155 | + /** |
|
156 | + * CalDavBackend constructor. |
|
157 | + * |
|
158 | + * @param IDBConnection $db |
|
159 | + * @param Principal $principalBackend |
|
160 | + * @param IUserManager $userManager |
|
161 | + * @param ISecureRandom $random |
|
162 | + * @param EventDispatcherInterface $dispatcher |
|
163 | + * @param bool $legacyEndpoint |
|
164 | + */ |
|
165 | + public function __construct(IDBConnection $db, |
|
166 | + Principal $principalBackend, |
|
167 | + IUserManager $userManager, |
|
168 | + ISecureRandom $random, |
|
169 | + EventDispatcherInterface $dispatcher, |
|
170 | + $legacyEndpoint = false) { |
|
171 | + $this->db = $db; |
|
172 | + $this->principalBackend = $principalBackend; |
|
173 | + $this->userManager = $userManager; |
|
174 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
175 | + $this->random = $random; |
|
176 | + $this->dispatcher = $dispatcher; |
|
177 | + $this->legacyEndpoint = $legacyEndpoint; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Return the number of calendars for a principal |
|
182 | + * |
|
183 | + * By default this excludes the automatically generated birthday calendar |
|
184 | + * |
|
185 | + * @param $principalUri |
|
186 | + * @param bool $excludeBirthday |
|
187 | + * @return int |
|
188 | + */ |
|
189 | + public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
190 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
191 | + $query = $this->db->getQueryBuilder(); |
|
192 | + $query->select($query->createFunction('COUNT(*)')) |
|
193 | + ->from('calendars') |
|
194 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
195 | + |
|
196 | + if ($excludeBirthday) { |
|
197 | + $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
198 | + } |
|
199 | + |
|
200 | + return (int)$query->execute()->fetchColumn(); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Returns a list of calendars for a principal. |
|
205 | + * |
|
206 | + * Every project is an array with the following keys: |
|
207 | + * * id, a unique id that will be used by other functions to modify the |
|
208 | + * calendar. This can be the same as the uri or a database key. |
|
209 | + * * uri, which the basename of the uri with which the calendar is |
|
210 | + * accessed. |
|
211 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
212 | + * principalUri passed to this method. |
|
213 | + * |
|
214 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
215 | + * common one is '{DAV:}displayname'. |
|
216 | + * |
|
217 | + * Many clients also require: |
|
218 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
219 | + * For this property, you can just return an instance of |
|
220 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
221 | + * |
|
222 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
223 | + * ACL will automatically be put in read-only mode. |
|
224 | + * |
|
225 | + * @param string $principalUri |
|
226 | + * @return array |
|
227 | + */ |
|
228 | + function getCalendarsForUser($principalUri) { |
|
229 | + $principalUriOriginal = $principalUri; |
|
230 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
231 | + $fields = array_values($this->propertyMap); |
|
232 | + $fields[] = 'id'; |
|
233 | + $fields[] = 'uri'; |
|
234 | + $fields[] = 'synctoken'; |
|
235 | + $fields[] = 'components'; |
|
236 | + $fields[] = 'principaluri'; |
|
237 | + $fields[] = 'transparent'; |
|
238 | + |
|
239 | + // Making fields a comma-delimited list |
|
240 | + $query = $this->db->getQueryBuilder(); |
|
241 | + $query->select($fields)->from('calendars') |
|
242 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
243 | + ->orderBy('calendarorder', 'ASC'); |
|
244 | + $stmt = $query->execute(); |
|
245 | + |
|
246 | + $calendars = []; |
|
247 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
248 | + |
|
249 | + $components = []; |
|
250 | + if ($row['components']) { |
|
251 | + $components = explode(',',$row['components']); |
|
252 | + } |
|
253 | + |
|
254 | + $calendar = [ |
|
255 | + 'id' => $row['id'], |
|
256 | + 'uri' => $row['uri'], |
|
257 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
258 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
259 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
260 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
261 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
262 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
263 | + ]; |
|
264 | + |
|
265 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
266 | + $calendar[$xmlName] = $row[$dbName]; |
|
267 | + } |
|
268 | + |
|
269 | + $this->addOwnerPrincipal($calendar); |
|
270 | + |
|
271 | + if (!isset($calendars[$calendar['id']])) { |
|
272 | + $calendars[$calendar['id']] = $calendar; |
|
273 | + } |
|
274 | + } |
|
275 | + |
|
276 | + $stmt->closeCursor(); |
|
277 | + |
|
278 | + // query for shared calendars |
|
279 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
280 | + $principals = array_map(function($principal) { |
|
281 | + return urldecode($principal); |
|
282 | + }, $principals); |
|
283 | + $principals[]= $principalUri; |
|
284 | + |
|
285 | + $fields = array_values($this->propertyMap); |
|
286 | + $fields[] = 'a.id'; |
|
287 | + $fields[] = 'a.uri'; |
|
288 | + $fields[] = 'a.synctoken'; |
|
289 | + $fields[] = 'a.components'; |
|
290 | + $fields[] = 'a.principaluri'; |
|
291 | + $fields[] = 'a.transparent'; |
|
292 | + $fields[] = 's.access'; |
|
293 | + $query = $this->db->getQueryBuilder(); |
|
294 | + $result = $query->select($fields) |
|
295 | + ->from('dav_shares', 's') |
|
296 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
297 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
298 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
299 | + ->setParameter('type', 'calendar') |
|
300 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
301 | + ->execute(); |
|
302 | + |
|
303 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
304 | + while($row = $result->fetch()) { |
|
305 | + if ($row['principaluri'] === $principalUri) { |
|
306 | + continue; |
|
307 | + } |
|
308 | + |
|
309 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
310 | + if (isset($calendars[$row['id']])) { |
|
311 | + if ($readOnly) { |
|
312 | + // New share can not have more permissions then the old one. |
|
313 | + continue; |
|
314 | + } |
|
315 | + if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
316 | + $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
317 | + // Old share is already read-write, no more permissions can be gained |
|
318 | + continue; |
|
319 | + } |
|
320 | + } |
|
321 | + |
|
322 | + list(, $name) = Uri\split($row['principaluri']); |
|
323 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
324 | + $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
325 | + $components = []; |
|
326 | + if ($row['components']) { |
|
327 | + $components = explode(',',$row['components']); |
|
328 | + } |
|
329 | + $calendar = [ |
|
330 | + 'id' => $row['id'], |
|
331 | + 'uri' => $uri, |
|
332 | + 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
333 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
334 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
335 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
336 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
337 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
338 | + $readOnlyPropertyName => $readOnly, |
|
339 | + ]; |
|
340 | + |
|
341 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
342 | + $calendar[$xmlName] = $row[$dbName]; |
|
343 | + } |
|
344 | + |
|
345 | + $this->addOwnerPrincipal($calendar); |
|
346 | + |
|
347 | + $calendars[$calendar['id']] = $calendar; |
|
348 | + } |
|
349 | + $result->closeCursor(); |
|
350 | + |
|
351 | + return array_values($calendars); |
|
352 | + } |
|
353 | + |
|
354 | + public function getUsersOwnCalendars($principalUri) { |
|
355 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
356 | + $fields = array_values($this->propertyMap); |
|
357 | + $fields[] = 'id'; |
|
358 | + $fields[] = 'uri'; |
|
359 | + $fields[] = 'synctoken'; |
|
360 | + $fields[] = 'components'; |
|
361 | + $fields[] = 'principaluri'; |
|
362 | + $fields[] = 'transparent'; |
|
363 | + // Making fields a comma-delimited list |
|
364 | + $query = $this->db->getQueryBuilder(); |
|
365 | + $query->select($fields)->from('calendars') |
|
366 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
367 | + ->orderBy('calendarorder', 'ASC'); |
|
368 | + $stmt = $query->execute(); |
|
369 | + $calendars = []; |
|
370 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
371 | + $components = []; |
|
372 | + if ($row['components']) { |
|
373 | + $components = explode(',',$row['components']); |
|
374 | + } |
|
375 | + $calendar = [ |
|
376 | + 'id' => $row['id'], |
|
377 | + 'uri' => $row['uri'], |
|
378 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
379 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
380 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
381 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
382 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
383 | + ]; |
|
384 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
385 | + $calendar[$xmlName] = $row[$dbName]; |
|
386 | + } |
|
387 | + |
|
388 | + $this->addOwnerPrincipal($calendar); |
|
389 | + |
|
390 | + if (!isset($calendars[$calendar['id']])) { |
|
391 | + $calendars[$calendar['id']] = $calendar; |
|
392 | + } |
|
393 | + } |
|
394 | + $stmt->closeCursor(); |
|
395 | + return array_values($calendars); |
|
396 | + } |
|
397 | + |
|
398 | + |
|
399 | + private function getUserDisplayName($uid) { |
|
400 | + if (!isset($this->userDisplayNames[$uid])) { |
|
401 | + $user = $this->userManager->get($uid); |
|
402 | + |
|
403 | + if ($user instanceof IUser) { |
|
404 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
405 | + } else { |
|
406 | + $this->userDisplayNames[$uid] = $uid; |
|
407 | + } |
|
408 | + } |
|
409 | + |
|
410 | + return $this->userDisplayNames[$uid]; |
|
411 | + } |
|
412 | 412 | |
413 | - /** |
|
414 | - * @return array |
|
415 | - */ |
|
416 | - public function getPublicCalendars() { |
|
417 | - $fields = array_values($this->propertyMap); |
|
418 | - $fields[] = 'a.id'; |
|
419 | - $fields[] = 'a.uri'; |
|
420 | - $fields[] = 'a.synctoken'; |
|
421 | - $fields[] = 'a.components'; |
|
422 | - $fields[] = 'a.principaluri'; |
|
423 | - $fields[] = 'a.transparent'; |
|
424 | - $fields[] = 's.access'; |
|
425 | - $fields[] = 's.publicuri'; |
|
426 | - $calendars = []; |
|
427 | - $query = $this->db->getQueryBuilder(); |
|
428 | - $result = $query->select($fields) |
|
429 | - ->from('dav_shares', 's') |
|
430 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
431 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
432 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
433 | - ->execute(); |
|
434 | - |
|
435 | - while($row = $result->fetch()) { |
|
436 | - list(, $name) = Uri\split($row['principaluri']); |
|
437 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
438 | - $components = []; |
|
439 | - if ($row['components']) { |
|
440 | - $components = explode(',',$row['components']); |
|
441 | - } |
|
442 | - $calendar = [ |
|
443 | - 'id' => $row['id'], |
|
444 | - 'uri' => $row['publicuri'], |
|
445 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
446 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
447 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
448 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
449 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
450 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
451 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
452 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
453 | - ]; |
|
454 | - |
|
455 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
456 | - $calendar[$xmlName] = $row[$dbName]; |
|
457 | - } |
|
458 | - |
|
459 | - $this->addOwnerPrincipal($calendar); |
|
460 | - |
|
461 | - if (!isset($calendars[$calendar['id']])) { |
|
462 | - $calendars[$calendar['id']] = $calendar; |
|
463 | - } |
|
464 | - } |
|
465 | - $result->closeCursor(); |
|
466 | - |
|
467 | - return array_values($calendars); |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * @param string $uri |
|
472 | - * @return array |
|
473 | - * @throws NotFound |
|
474 | - */ |
|
475 | - public function getPublicCalendar($uri) { |
|
476 | - $fields = array_values($this->propertyMap); |
|
477 | - $fields[] = 'a.id'; |
|
478 | - $fields[] = 'a.uri'; |
|
479 | - $fields[] = 'a.synctoken'; |
|
480 | - $fields[] = 'a.components'; |
|
481 | - $fields[] = 'a.principaluri'; |
|
482 | - $fields[] = 'a.transparent'; |
|
483 | - $fields[] = 's.access'; |
|
484 | - $fields[] = 's.publicuri'; |
|
485 | - $query = $this->db->getQueryBuilder(); |
|
486 | - $result = $query->select($fields) |
|
487 | - ->from('dav_shares', 's') |
|
488 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
489 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
490 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
491 | - ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
492 | - ->execute(); |
|
493 | - |
|
494 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
495 | - |
|
496 | - $result->closeCursor(); |
|
497 | - |
|
498 | - if ($row === false) { |
|
499 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
500 | - } |
|
501 | - |
|
502 | - list(, $name) = Uri\split($row['principaluri']); |
|
503 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
504 | - $components = []; |
|
505 | - if ($row['components']) { |
|
506 | - $components = explode(',',$row['components']); |
|
507 | - } |
|
508 | - $calendar = [ |
|
509 | - 'id' => $row['id'], |
|
510 | - 'uri' => $row['publicuri'], |
|
511 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
512 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
513 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
514 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
515 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
516 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
517 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
518 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
519 | - ]; |
|
520 | - |
|
521 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
522 | - $calendar[$xmlName] = $row[$dbName]; |
|
523 | - } |
|
524 | - |
|
525 | - $this->addOwnerPrincipal($calendar); |
|
526 | - |
|
527 | - return $calendar; |
|
528 | - |
|
529 | - } |
|
530 | - |
|
531 | - /** |
|
532 | - * @param string $principal |
|
533 | - * @param string $uri |
|
534 | - * @return array|null |
|
535 | - */ |
|
536 | - public function getCalendarByUri($principal, $uri) { |
|
537 | - $fields = array_values($this->propertyMap); |
|
538 | - $fields[] = 'id'; |
|
539 | - $fields[] = 'uri'; |
|
540 | - $fields[] = 'synctoken'; |
|
541 | - $fields[] = 'components'; |
|
542 | - $fields[] = 'principaluri'; |
|
543 | - $fields[] = 'transparent'; |
|
544 | - |
|
545 | - // Making fields a comma-delimited list |
|
546 | - $query = $this->db->getQueryBuilder(); |
|
547 | - $query->select($fields)->from('calendars') |
|
548 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
549 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
550 | - ->setMaxResults(1); |
|
551 | - $stmt = $query->execute(); |
|
552 | - |
|
553 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
554 | - $stmt->closeCursor(); |
|
555 | - if ($row === false) { |
|
556 | - return null; |
|
557 | - } |
|
558 | - |
|
559 | - $components = []; |
|
560 | - if ($row['components']) { |
|
561 | - $components = explode(',',$row['components']); |
|
562 | - } |
|
563 | - |
|
564 | - $calendar = [ |
|
565 | - 'id' => $row['id'], |
|
566 | - 'uri' => $row['uri'], |
|
567 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
568 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
569 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
570 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
571 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
572 | - ]; |
|
573 | - |
|
574 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
575 | - $calendar[$xmlName] = $row[$dbName]; |
|
576 | - } |
|
577 | - |
|
578 | - $this->addOwnerPrincipal($calendar); |
|
579 | - |
|
580 | - return $calendar; |
|
581 | - } |
|
582 | - |
|
583 | - public function getCalendarById($calendarId) { |
|
584 | - $fields = array_values($this->propertyMap); |
|
585 | - $fields[] = 'id'; |
|
586 | - $fields[] = 'uri'; |
|
587 | - $fields[] = 'synctoken'; |
|
588 | - $fields[] = 'components'; |
|
589 | - $fields[] = 'principaluri'; |
|
590 | - $fields[] = 'transparent'; |
|
591 | - |
|
592 | - // Making fields a comma-delimited list |
|
593 | - $query = $this->db->getQueryBuilder(); |
|
594 | - $query->select($fields)->from('calendars') |
|
595 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
596 | - ->setMaxResults(1); |
|
597 | - $stmt = $query->execute(); |
|
598 | - |
|
599 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
600 | - $stmt->closeCursor(); |
|
601 | - if ($row === false) { |
|
602 | - return null; |
|
603 | - } |
|
604 | - |
|
605 | - $components = []; |
|
606 | - if ($row['components']) { |
|
607 | - $components = explode(',',$row['components']); |
|
608 | - } |
|
609 | - |
|
610 | - $calendar = [ |
|
611 | - 'id' => $row['id'], |
|
612 | - 'uri' => $row['uri'], |
|
613 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
614 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
615 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
616 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
617 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
618 | - ]; |
|
619 | - |
|
620 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
621 | - $calendar[$xmlName] = $row[$dbName]; |
|
622 | - } |
|
623 | - |
|
624 | - $this->addOwnerPrincipal($calendar); |
|
625 | - |
|
626 | - return $calendar; |
|
627 | - } |
|
628 | - |
|
629 | - /** |
|
630 | - * Creates a new calendar for a principal. |
|
631 | - * |
|
632 | - * If the creation was a success, an id must be returned that can be used to reference |
|
633 | - * this calendar in other methods, such as updateCalendar. |
|
634 | - * |
|
635 | - * @param string $principalUri |
|
636 | - * @param string $calendarUri |
|
637 | - * @param array $properties |
|
638 | - * @return int |
|
639 | - * @suppress SqlInjectionChecker |
|
640 | - */ |
|
641 | - function createCalendar($principalUri, $calendarUri, array $properties) { |
|
642 | - $values = [ |
|
643 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
644 | - 'uri' => $calendarUri, |
|
645 | - 'synctoken' => 1, |
|
646 | - 'transparent' => 0, |
|
647 | - 'components' => 'VEVENT,VTODO', |
|
648 | - 'displayname' => $calendarUri |
|
649 | - ]; |
|
650 | - |
|
651 | - // Default value |
|
652 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
653 | - if (isset($properties[$sccs])) { |
|
654 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
655 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
656 | - } |
|
657 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
658 | - } |
|
659 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
660 | - if (isset($properties[$transp])) { |
|
661 | - $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
662 | - } |
|
663 | - |
|
664 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
665 | - if (isset($properties[$xmlName])) { |
|
666 | - $values[$dbName] = $properties[$xmlName]; |
|
667 | - } |
|
668 | - } |
|
669 | - |
|
670 | - $query = $this->db->getQueryBuilder(); |
|
671 | - $query->insert('calendars'); |
|
672 | - foreach($values as $column => $value) { |
|
673 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
674 | - } |
|
675 | - $query->execute(); |
|
676 | - $calendarId = $query->getLastInsertId(); |
|
677 | - |
|
678 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( |
|
679 | - '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', |
|
680 | - [ |
|
681 | - 'calendarId' => $calendarId, |
|
682 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
683 | - ])); |
|
684 | - |
|
685 | - return $calendarId; |
|
686 | - } |
|
687 | - |
|
688 | - /** |
|
689 | - * Updates properties for a calendar. |
|
690 | - * |
|
691 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
692 | - * To do the actual updates, you must tell this object which properties |
|
693 | - * you're going to process with the handle() method. |
|
694 | - * |
|
695 | - * Calling the handle method is like telling the PropPatch object "I |
|
696 | - * promise I can handle updating this property". |
|
697 | - * |
|
698 | - * Read the PropPatch documentation for more info and examples. |
|
699 | - * |
|
700 | - * @param mixed $calendarId |
|
701 | - * @param PropPatch $propPatch |
|
702 | - * @return void |
|
703 | - */ |
|
704 | - function updateCalendar($calendarId, PropPatch $propPatch) { |
|
705 | - $supportedProperties = array_keys($this->propertyMap); |
|
706 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
707 | - |
|
708 | - /** |
|
709 | - * @suppress SqlInjectionChecker |
|
710 | - */ |
|
711 | - $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
712 | - $newValues = []; |
|
713 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
714 | - |
|
715 | - switch ($propertyName) { |
|
716 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
717 | - $fieldName = 'transparent'; |
|
718 | - $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
719 | - break; |
|
720 | - default : |
|
721 | - $fieldName = $this->propertyMap[$propertyName]; |
|
722 | - $newValues[$fieldName] = $propertyValue; |
|
723 | - break; |
|
724 | - } |
|
725 | - |
|
726 | - } |
|
727 | - $query = $this->db->getQueryBuilder(); |
|
728 | - $query->update('calendars'); |
|
729 | - foreach ($newValues as $fieldName => $value) { |
|
730 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
731 | - } |
|
732 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
733 | - $query->execute(); |
|
734 | - |
|
735 | - $this->addChange($calendarId, "", 2); |
|
736 | - |
|
737 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( |
|
738 | - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', |
|
739 | - [ |
|
740 | - 'calendarId' => $calendarId, |
|
741 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
742 | - 'shares' => $this->getShares($calendarId), |
|
743 | - 'propertyMutations' => $mutations, |
|
744 | - ])); |
|
745 | - |
|
746 | - return true; |
|
747 | - }); |
|
748 | - } |
|
749 | - |
|
750 | - /** |
|
751 | - * Delete a calendar and all it's objects |
|
752 | - * |
|
753 | - * @param mixed $calendarId |
|
754 | - * @return void |
|
755 | - */ |
|
756 | - function deleteCalendar($calendarId) { |
|
757 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent( |
|
758 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', |
|
759 | - [ |
|
760 | - 'calendarId' => $calendarId, |
|
761 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
762 | - 'shares' => $this->getShares($calendarId), |
|
763 | - ])); |
|
764 | - |
|
765 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
766 | - $stmt->execute([$calendarId]); |
|
767 | - |
|
768 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
769 | - $stmt->execute([$calendarId]); |
|
770 | - |
|
771 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
772 | - $stmt->execute([$calendarId]); |
|
773 | - |
|
774 | - $this->sharingBackend->deleteAllShares($calendarId); |
|
775 | - |
|
776 | - $query = $this->db->getQueryBuilder(); |
|
777 | - $query->delete($this->dbObjectPropertiesTable) |
|
778 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
779 | - ->execute(); |
|
780 | - } |
|
781 | - |
|
782 | - /** |
|
783 | - * Delete all of an user's shares |
|
784 | - * |
|
785 | - * @param string $principaluri |
|
786 | - * @return void |
|
787 | - */ |
|
788 | - function deleteAllSharesByUser($principaluri) { |
|
789 | - $this->sharingBackend->deleteAllSharesByUser($principaluri); |
|
790 | - } |
|
791 | - |
|
792 | - /** |
|
793 | - * Returns all calendar objects within a calendar. |
|
794 | - * |
|
795 | - * Every item contains an array with the following keys: |
|
796 | - * * calendardata - The iCalendar-compatible calendar data |
|
797 | - * * uri - a unique key which will be used to construct the uri. This can |
|
798 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
799 | - * good idea. This is only the basename, or filename, not the full |
|
800 | - * path. |
|
801 | - * * lastmodified - a timestamp of the last modification time |
|
802 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
803 | - * '"abcdef"') |
|
804 | - * * size - The size of the calendar objects, in bytes. |
|
805 | - * * component - optional, a string containing the type of object, such |
|
806 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
807 | - * the Content-Type header. |
|
808 | - * |
|
809 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
810 | - * speed reasons. |
|
811 | - * |
|
812 | - * The calendardata is also optional. If it's not returned |
|
813 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
814 | - * calendardata. |
|
815 | - * |
|
816 | - * If neither etag or size are specified, the calendardata will be |
|
817 | - * used/fetched to determine these numbers. If both are specified the |
|
818 | - * amount of times this is needed is reduced by a great degree. |
|
819 | - * |
|
820 | - * @param mixed $calendarId |
|
821 | - * @return array |
|
822 | - */ |
|
823 | - function getCalendarObjects($calendarId) { |
|
824 | - $query = $this->db->getQueryBuilder(); |
|
825 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
826 | - ->from('calendarobjects') |
|
827 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
828 | - $stmt = $query->execute(); |
|
829 | - |
|
830 | - $result = []; |
|
831 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
832 | - $result[] = [ |
|
833 | - 'id' => $row['id'], |
|
834 | - 'uri' => $row['uri'], |
|
835 | - 'lastmodified' => $row['lastmodified'], |
|
836 | - 'etag' => '"' . $row['etag'] . '"', |
|
837 | - 'calendarid' => $row['calendarid'], |
|
838 | - 'size' => (int)$row['size'], |
|
839 | - 'component' => strtolower($row['componenttype']), |
|
840 | - 'classification'=> (int)$row['classification'] |
|
841 | - ]; |
|
842 | - } |
|
843 | - |
|
844 | - return $result; |
|
845 | - } |
|
846 | - |
|
847 | - /** |
|
848 | - * Returns information from a single calendar object, based on it's object |
|
849 | - * uri. |
|
850 | - * |
|
851 | - * The object uri is only the basename, or filename and not a full path. |
|
852 | - * |
|
853 | - * The returned array must have the same keys as getCalendarObjects. The |
|
854 | - * 'calendardata' object is required here though, while it's not required |
|
855 | - * for getCalendarObjects. |
|
856 | - * |
|
857 | - * This method must return null if the object did not exist. |
|
858 | - * |
|
859 | - * @param mixed $calendarId |
|
860 | - * @param string $objectUri |
|
861 | - * @return array|null |
|
862 | - */ |
|
863 | - function getCalendarObject($calendarId, $objectUri) { |
|
864 | - |
|
865 | - $query = $this->db->getQueryBuilder(); |
|
866 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
867 | - ->from('calendarobjects') |
|
868 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
869 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
870 | - $stmt = $query->execute(); |
|
871 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
872 | - |
|
873 | - if(!$row) return null; |
|
874 | - |
|
875 | - return [ |
|
876 | - 'id' => $row['id'], |
|
877 | - 'uri' => $row['uri'], |
|
878 | - 'lastmodified' => $row['lastmodified'], |
|
879 | - 'etag' => '"' . $row['etag'] . '"', |
|
880 | - 'calendarid' => $row['calendarid'], |
|
881 | - 'size' => (int)$row['size'], |
|
882 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
883 | - 'component' => strtolower($row['componenttype']), |
|
884 | - 'classification'=> (int)$row['classification'] |
|
885 | - ]; |
|
886 | - } |
|
887 | - |
|
888 | - /** |
|
889 | - * Returns a list of calendar objects. |
|
890 | - * |
|
891 | - * This method should work identical to getCalendarObject, but instead |
|
892 | - * return all the calendar objects in the list as an array. |
|
893 | - * |
|
894 | - * If the backend supports this, it may allow for some speed-ups. |
|
895 | - * |
|
896 | - * @param mixed $calendarId |
|
897 | - * @param string[] $uris |
|
898 | - * @return array |
|
899 | - */ |
|
900 | - function getMultipleCalendarObjects($calendarId, array $uris) { |
|
901 | - if (empty($uris)) { |
|
902 | - return []; |
|
903 | - } |
|
904 | - |
|
905 | - $chunks = array_chunk($uris, 100); |
|
906 | - $objects = []; |
|
907 | - |
|
908 | - $query = $this->db->getQueryBuilder(); |
|
909 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
910 | - ->from('calendarobjects') |
|
911 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
912 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
913 | - |
|
914 | - foreach ($chunks as $uris) { |
|
915 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
916 | - $result = $query->execute(); |
|
917 | - |
|
918 | - while ($row = $result->fetch()) { |
|
919 | - $objects[] = [ |
|
920 | - 'id' => $row['id'], |
|
921 | - 'uri' => $row['uri'], |
|
922 | - 'lastmodified' => $row['lastmodified'], |
|
923 | - 'etag' => '"' . $row['etag'] . '"', |
|
924 | - 'calendarid' => $row['calendarid'], |
|
925 | - 'size' => (int)$row['size'], |
|
926 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
927 | - 'component' => strtolower($row['componenttype']), |
|
928 | - 'classification' => (int)$row['classification'] |
|
929 | - ]; |
|
930 | - } |
|
931 | - $result->closeCursor(); |
|
932 | - } |
|
933 | - return $objects; |
|
934 | - } |
|
935 | - |
|
936 | - /** |
|
937 | - * Creates a new calendar object. |
|
938 | - * |
|
939 | - * The object uri is only the basename, or filename and not a full path. |
|
940 | - * |
|
941 | - * It is possible return an etag from this function, which will be used in |
|
942 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
943 | - * by double-quotes. |
|
944 | - * |
|
945 | - * However, you should only really return this ETag if you don't mangle the |
|
946 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
947 | - * the exact same as this request body, you should omit the ETag. |
|
948 | - * |
|
949 | - * @param mixed $calendarId |
|
950 | - * @param string $objectUri |
|
951 | - * @param string $calendarData |
|
952 | - * @return string |
|
953 | - */ |
|
954 | - function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
955 | - $extraData = $this->getDenormalizedData($calendarData); |
|
956 | - |
|
957 | - $query = $this->db->getQueryBuilder(); |
|
958 | - $query->insert('calendarobjects') |
|
959 | - ->values([ |
|
960 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
961 | - 'uri' => $query->createNamedParameter($objectUri), |
|
962 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
963 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
964 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
965 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
966 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
967 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
968 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
969 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
970 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
971 | - ]) |
|
972 | - ->execute(); |
|
973 | - |
|
974 | - $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
975 | - |
|
976 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent( |
|
977 | - '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', |
|
978 | - [ |
|
979 | - 'calendarId' => $calendarId, |
|
980 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
981 | - 'shares' => $this->getShares($calendarId), |
|
982 | - 'objectData' => $this->getCalendarObject($calendarId, $objectUri), |
|
983 | - ] |
|
984 | - )); |
|
985 | - $this->addChange($calendarId, $objectUri, 1); |
|
986 | - |
|
987 | - return '"' . $extraData['etag'] . '"'; |
|
988 | - } |
|
989 | - |
|
990 | - /** |
|
991 | - * Updates an existing calendarobject, based on it's uri. |
|
992 | - * |
|
993 | - * The object uri is only the basename, or filename and not a full path. |
|
994 | - * |
|
995 | - * It is possible return an etag from this function, which will be used in |
|
996 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
997 | - * by double-quotes. |
|
998 | - * |
|
999 | - * However, you should only really return this ETag if you don't mangle the |
|
1000 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
1001 | - * the exact same as this request body, you should omit the ETag. |
|
1002 | - * |
|
1003 | - * @param mixed $calendarId |
|
1004 | - * @param string $objectUri |
|
1005 | - * @param string $calendarData |
|
1006 | - * @return string |
|
1007 | - */ |
|
1008 | - function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
1009 | - $extraData = $this->getDenormalizedData($calendarData); |
|
1010 | - |
|
1011 | - $query = $this->db->getQueryBuilder(); |
|
1012 | - $query->update('calendarobjects') |
|
1013 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
1014 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
1015 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
1016 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
1017 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
1018 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
1019 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
1020 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
1021 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
1022 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
1023 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1024 | - ->execute(); |
|
1025 | - |
|
1026 | - $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
1027 | - |
|
1028 | - $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1029 | - if (is_array($data)) { |
|
1030 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent( |
|
1031 | - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', |
|
1032 | - [ |
|
1033 | - 'calendarId' => $calendarId, |
|
1034 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1035 | - 'shares' => $this->getShares($calendarId), |
|
1036 | - 'objectData' => $data, |
|
1037 | - ] |
|
1038 | - )); |
|
1039 | - } |
|
1040 | - $this->addChange($calendarId, $objectUri, 2); |
|
1041 | - |
|
1042 | - return '"' . $extraData['etag'] . '"'; |
|
1043 | - } |
|
1044 | - |
|
1045 | - /** |
|
1046 | - * @param int $calendarObjectId |
|
1047 | - * @param int $classification |
|
1048 | - */ |
|
1049 | - public function setClassification($calendarObjectId, $classification) { |
|
1050 | - if (!in_array($classification, [ |
|
1051 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
1052 | - ])) { |
|
1053 | - throw new \InvalidArgumentException(); |
|
1054 | - } |
|
1055 | - $query = $this->db->getQueryBuilder(); |
|
1056 | - $query->update('calendarobjects') |
|
1057 | - ->set('classification', $query->createNamedParameter($classification)) |
|
1058 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
1059 | - ->execute(); |
|
1060 | - } |
|
1061 | - |
|
1062 | - /** |
|
1063 | - * Deletes an existing calendar object. |
|
1064 | - * |
|
1065 | - * The object uri is only the basename, or filename and not a full path. |
|
1066 | - * |
|
1067 | - * @param mixed $calendarId |
|
1068 | - * @param string $objectUri |
|
1069 | - * @return void |
|
1070 | - */ |
|
1071 | - function deleteCalendarObject($calendarId, $objectUri) { |
|
1072 | - $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1073 | - if (is_array($data)) { |
|
1074 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( |
|
1075 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', |
|
1076 | - [ |
|
1077 | - 'calendarId' => $calendarId, |
|
1078 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1079 | - 'shares' => $this->getShares($calendarId), |
|
1080 | - 'objectData' => $data, |
|
1081 | - ] |
|
1082 | - )); |
|
1083 | - } |
|
1084 | - |
|
1085 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
1086 | - $stmt->execute([$calendarId, $objectUri]); |
|
1087 | - |
|
1088 | - $this->purgeProperties($calendarId, $data['id']); |
|
1089 | - |
|
1090 | - $this->addChange($calendarId, $objectUri, 3); |
|
1091 | - } |
|
1092 | - |
|
1093 | - /** |
|
1094 | - * Performs a calendar-query on the contents of this calendar. |
|
1095 | - * |
|
1096 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
1097 | - * calendar-query it is possible for a client to request a specific set of |
|
1098 | - * object, based on contents of iCalendar properties, date-ranges and |
|
1099 | - * iCalendar component types (VTODO, VEVENT). |
|
1100 | - * |
|
1101 | - * This method should just return a list of (relative) urls that match this |
|
1102 | - * query. |
|
1103 | - * |
|
1104 | - * The list of filters are specified as an array. The exact array is |
|
1105 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
1106 | - * |
|
1107 | - * Note that it is extremely likely that getCalendarObject for every path |
|
1108 | - * returned from this method will be called almost immediately after. You |
|
1109 | - * may want to anticipate this to speed up these requests. |
|
1110 | - * |
|
1111 | - * This method provides a default implementation, which parses *all* the |
|
1112 | - * iCalendar objects in the specified calendar. |
|
1113 | - * |
|
1114 | - * This default may well be good enough for personal use, and calendars |
|
1115 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
1116 | - * or high loads, you are strongly advised to optimize certain paths. |
|
1117 | - * |
|
1118 | - * The best way to do so is override this method and to optimize |
|
1119 | - * specifically for 'common filters'. |
|
1120 | - * |
|
1121 | - * Requests that are extremely common are: |
|
1122 | - * * requests for just VEVENTS |
|
1123 | - * * requests for just VTODO |
|
1124 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
1125 | - * |
|
1126 | - * ..and combinations of these requests. It may not be worth it to try to |
|
1127 | - * handle every possible situation and just rely on the (relatively |
|
1128 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
1129 | - * |
|
1130 | - * Note that especially time-range-filters may be difficult to parse. A |
|
1131 | - * time-range filter specified on a VEVENT must for instance also handle |
|
1132 | - * recurrence rules correctly. |
|
1133 | - * A good example of how to interprete all these filters can also simply |
|
1134 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
1135 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
1136 | - * to think of. |
|
1137 | - * |
|
1138 | - * @param mixed $calendarId |
|
1139 | - * @param array $filters |
|
1140 | - * @return array |
|
1141 | - */ |
|
1142 | - function calendarQuery($calendarId, array $filters) { |
|
1143 | - $componentType = null; |
|
1144 | - $requirePostFilter = true; |
|
1145 | - $timeRange = null; |
|
1146 | - |
|
1147 | - // if no filters were specified, we don't need to filter after a query |
|
1148 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
1149 | - $requirePostFilter = false; |
|
1150 | - } |
|
1151 | - |
|
1152 | - // Figuring out if there's a component filter |
|
1153 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
1154 | - $componentType = $filters['comp-filters'][0]['name']; |
|
1155 | - |
|
1156 | - // Checking if we need post-filters |
|
1157 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
1158 | - $requirePostFilter = false; |
|
1159 | - } |
|
1160 | - // There was a time-range filter |
|
1161 | - if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
1162 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
1163 | - |
|
1164 | - // If start time OR the end time is not specified, we can do a |
|
1165 | - // 100% accurate mysql query. |
|
1166 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
1167 | - $requirePostFilter = false; |
|
1168 | - } |
|
1169 | - } |
|
1170 | - |
|
1171 | - } |
|
1172 | - $columns = ['uri']; |
|
1173 | - if ($requirePostFilter) { |
|
1174 | - $columns = ['uri', 'calendardata']; |
|
1175 | - } |
|
1176 | - $query = $this->db->getQueryBuilder(); |
|
1177 | - $query->select($columns) |
|
1178 | - ->from('calendarobjects') |
|
1179 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
1180 | - |
|
1181 | - if ($componentType) { |
|
1182 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
1183 | - } |
|
1184 | - |
|
1185 | - if ($timeRange && $timeRange['start']) { |
|
1186 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
1187 | - } |
|
1188 | - if ($timeRange && $timeRange['end']) { |
|
1189 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
1190 | - } |
|
1191 | - |
|
1192 | - $stmt = $query->execute(); |
|
1193 | - |
|
1194 | - $result = []; |
|
1195 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1196 | - if ($requirePostFilter) { |
|
1197 | - if (!$this->validateFilterForObject($row, $filters)) { |
|
1198 | - continue; |
|
1199 | - } |
|
1200 | - } |
|
1201 | - $result[] = $row['uri']; |
|
1202 | - } |
|
1203 | - |
|
1204 | - return $result; |
|
1205 | - } |
|
1206 | - |
|
1207 | - /** |
|
1208 | - * custom Nextcloud search extension for CalDAV |
|
1209 | - * |
|
1210 | - * @param string $principalUri |
|
1211 | - * @param array $filters |
|
1212 | - * @param integer|null $limit |
|
1213 | - * @param integer|null $offset |
|
1214 | - * @return array |
|
1215 | - */ |
|
1216 | - public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
1217 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
1218 | - $ownCalendars = []; |
|
1219 | - $sharedCalendars = []; |
|
1220 | - |
|
1221 | - $uriMapper = []; |
|
1222 | - |
|
1223 | - foreach($calendars as $calendar) { |
|
1224 | - if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
1225 | - $ownCalendars[] = $calendar['id']; |
|
1226 | - } else { |
|
1227 | - $sharedCalendars[] = $calendar['id']; |
|
1228 | - } |
|
1229 | - $uriMapper[$calendar['id']] = $calendar['uri']; |
|
1230 | - } |
|
1231 | - if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
1232 | - return []; |
|
1233 | - } |
|
1234 | - |
|
1235 | - $query = $this->db->getQueryBuilder(); |
|
1236 | - // Calendar id expressions |
|
1237 | - $calendarExpressions = []; |
|
1238 | - foreach($ownCalendars as $id) { |
|
1239 | - $calendarExpressions[] = $query->expr() |
|
1240 | - ->eq('c.calendarid', $query->createNamedParameter($id)); |
|
1241 | - } |
|
1242 | - foreach($sharedCalendars as $id) { |
|
1243 | - $calendarExpressions[] = $query->expr()->andX( |
|
1244 | - $query->expr()->eq('c.calendarid', |
|
1245 | - $query->createNamedParameter($id)), |
|
1246 | - $query->expr()->eq('c.classification', |
|
1247 | - $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)) |
|
1248 | - ); |
|
1249 | - } |
|
1250 | - |
|
1251 | - if (count($calendarExpressions) === 1) { |
|
1252 | - $calExpr = $calendarExpressions[0]; |
|
1253 | - } else { |
|
1254 | - $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
1255 | - } |
|
1256 | - |
|
1257 | - // Component expressions |
|
1258 | - $compExpressions = []; |
|
1259 | - foreach($filters['comps'] as $comp) { |
|
1260 | - $compExpressions[] = $query->expr() |
|
1261 | - ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
1262 | - } |
|
1263 | - |
|
1264 | - if (count($compExpressions) === 1) { |
|
1265 | - $compExpr = $compExpressions[0]; |
|
1266 | - } else { |
|
1267 | - $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
1268 | - } |
|
1269 | - |
|
1270 | - if (!isset($filters['props'])) { |
|
1271 | - $filters['props'] = []; |
|
1272 | - } |
|
1273 | - if (!isset($filters['params'])) { |
|
1274 | - $filters['params'] = []; |
|
1275 | - } |
|
1276 | - |
|
1277 | - $propParamExpressions = []; |
|
1278 | - foreach($filters['props'] as $prop) { |
|
1279 | - $propParamExpressions[] = $query->expr()->andX( |
|
1280 | - $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
1281 | - $query->expr()->isNull('i.parameter') |
|
1282 | - ); |
|
1283 | - } |
|
1284 | - foreach($filters['params'] as $param) { |
|
1285 | - $propParamExpressions[] = $query->expr()->andX( |
|
1286 | - $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
1287 | - $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
1288 | - ); |
|
1289 | - } |
|
1290 | - |
|
1291 | - if (count($propParamExpressions) === 1) { |
|
1292 | - $propParamExpr = $propParamExpressions[0]; |
|
1293 | - } else { |
|
1294 | - $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
1295 | - } |
|
1296 | - |
|
1297 | - $query->select(['c.calendarid', 'c.uri']) |
|
1298 | - ->from($this->dbObjectPropertiesTable, 'i') |
|
1299 | - ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
1300 | - ->where($calExpr) |
|
1301 | - ->andWhere($compExpr) |
|
1302 | - ->andWhere($propParamExpr) |
|
1303 | - ->andWhere($query->expr()->iLike('i.value', |
|
1304 | - $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))); |
|
1305 | - |
|
1306 | - if ($offset) { |
|
1307 | - $query->setFirstResult($offset); |
|
1308 | - } |
|
1309 | - if ($limit) { |
|
1310 | - $query->setMaxResults($limit); |
|
1311 | - } |
|
1312 | - |
|
1313 | - $stmt = $query->execute(); |
|
1314 | - |
|
1315 | - $result = []; |
|
1316 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1317 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
1318 | - if (!in_array($path, $result)) { |
|
1319 | - $result[] = $path; |
|
1320 | - } |
|
1321 | - } |
|
1322 | - |
|
1323 | - return $result; |
|
1324 | - } |
|
1325 | - |
|
1326 | - /** |
|
1327 | - * Searches through all of a users calendars and calendar objects to find |
|
1328 | - * an object with a specific UID. |
|
1329 | - * |
|
1330 | - * This method should return the path to this object, relative to the |
|
1331 | - * calendar home, so this path usually only contains two parts: |
|
1332 | - * |
|
1333 | - * calendarpath/objectpath.ics |
|
1334 | - * |
|
1335 | - * If the uid is not found, return null. |
|
1336 | - * |
|
1337 | - * This method should only consider * objects that the principal owns, so |
|
1338 | - * any calendars owned by other principals that also appear in this |
|
1339 | - * collection should be ignored. |
|
1340 | - * |
|
1341 | - * @param string $principalUri |
|
1342 | - * @param string $uid |
|
1343 | - * @return string|null |
|
1344 | - */ |
|
1345 | - function getCalendarObjectByUID($principalUri, $uid) { |
|
1346 | - |
|
1347 | - $query = $this->db->getQueryBuilder(); |
|
1348 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
1349 | - ->from('calendarobjects', 'co') |
|
1350 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
1351 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
1352 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
1353 | - |
|
1354 | - $stmt = $query->execute(); |
|
1355 | - |
|
1356 | - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1357 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
1358 | - } |
|
1359 | - |
|
1360 | - return null; |
|
1361 | - } |
|
1362 | - |
|
1363 | - /** |
|
1364 | - * The getChanges method returns all the changes that have happened, since |
|
1365 | - * the specified syncToken in the specified calendar. |
|
1366 | - * |
|
1367 | - * This function should return an array, such as the following: |
|
1368 | - * |
|
1369 | - * [ |
|
1370 | - * 'syncToken' => 'The current synctoken', |
|
1371 | - * 'added' => [ |
|
1372 | - * 'new.txt', |
|
1373 | - * ], |
|
1374 | - * 'modified' => [ |
|
1375 | - * 'modified.txt', |
|
1376 | - * ], |
|
1377 | - * 'deleted' => [ |
|
1378 | - * 'foo.php.bak', |
|
1379 | - * 'old.txt' |
|
1380 | - * ] |
|
1381 | - * ); |
|
1382 | - * |
|
1383 | - * The returned syncToken property should reflect the *current* syncToken |
|
1384 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
1385 | - * property This is * needed here too, to ensure the operation is atomic. |
|
1386 | - * |
|
1387 | - * If the $syncToken argument is specified as null, this is an initial |
|
1388 | - * sync, and all members should be reported. |
|
1389 | - * |
|
1390 | - * The modified property is an array of nodenames that have changed since |
|
1391 | - * the last token. |
|
1392 | - * |
|
1393 | - * The deleted property is an array with nodenames, that have been deleted |
|
1394 | - * from collection. |
|
1395 | - * |
|
1396 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
1397 | - * 1, you only have to report changes that happened only directly in |
|
1398 | - * immediate descendants. If it's 2, it should also include changes from |
|
1399 | - * the nodes below the child collections. (grandchildren) |
|
1400 | - * |
|
1401 | - * The $limit argument allows a client to specify how many results should |
|
1402 | - * be returned at most. If the limit is not specified, it should be treated |
|
1403 | - * as infinite. |
|
1404 | - * |
|
1405 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
1406 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
1407 | - * |
|
1408 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
1409 | - * return null. |
|
1410 | - * |
|
1411 | - * The limit is 'suggestive'. You are free to ignore it. |
|
1412 | - * |
|
1413 | - * @param string $calendarId |
|
1414 | - * @param string $syncToken |
|
1415 | - * @param int $syncLevel |
|
1416 | - * @param int $limit |
|
1417 | - * @return array |
|
1418 | - */ |
|
1419 | - function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
1420 | - // Current synctoken |
|
1421 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1422 | - $stmt->execute([ $calendarId ]); |
|
1423 | - $currentToken = $stmt->fetchColumn(0); |
|
1424 | - |
|
1425 | - if (is_null($currentToken)) { |
|
1426 | - return null; |
|
1427 | - } |
|
1428 | - |
|
1429 | - $result = [ |
|
1430 | - 'syncToken' => $currentToken, |
|
1431 | - 'added' => [], |
|
1432 | - 'modified' => [], |
|
1433 | - 'deleted' => [], |
|
1434 | - ]; |
|
1435 | - |
|
1436 | - if ($syncToken) { |
|
1437 | - |
|
1438 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
1439 | - if ($limit>0) { |
|
1440 | - $query.= " LIMIT " . (int)$limit; |
|
1441 | - } |
|
1442 | - |
|
1443 | - // Fetching all changes |
|
1444 | - $stmt = $this->db->prepare($query); |
|
1445 | - $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
1446 | - |
|
1447 | - $changes = []; |
|
1448 | - |
|
1449 | - // This loop ensures that any duplicates are overwritten, only the |
|
1450 | - // last change on a node is relevant. |
|
1451 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1452 | - |
|
1453 | - $changes[$row['uri']] = $row['operation']; |
|
1454 | - |
|
1455 | - } |
|
1456 | - |
|
1457 | - foreach($changes as $uri => $operation) { |
|
1458 | - |
|
1459 | - switch($operation) { |
|
1460 | - case 1 : |
|
1461 | - $result['added'][] = $uri; |
|
1462 | - break; |
|
1463 | - case 2 : |
|
1464 | - $result['modified'][] = $uri; |
|
1465 | - break; |
|
1466 | - case 3 : |
|
1467 | - $result['deleted'][] = $uri; |
|
1468 | - break; |
|
1469 | - } |
|
1470 | - |
|
1471 | - } |
|
1472 | - } else { |
|
1473 | - // No synctoken supplied, this is the initial sync. |
|
1474 | - $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
1475 | - $stmt = $this->db->prepare($query); |
|
1476 | - $stmt->execute([$calendarId]); |
|
1477 | - |
|
1478 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
1479 | - } |
|
1480 | - return $result; |
|
1481 | - |
|
1482 | - } |
|
1483 | - |
|
1484 | - /** |
|
1485 | - * Returns a list of subscriptions for a principal. |
|
1486 | - * |
|
1487 | - * Every subscription is an array with the following keys: |
|
1488 | - * * id, a unique id that will be used by other functions to modify the |
|
1489 | - * subscription. This can be the same as the uri or a database key. |
|
1490 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
1491 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
1492 | - * principalUri passed to this method. |
|
1493 | - * |
|
1494 | - * Furthermore, all the subscription info must be returned too: |
|
1495 | - * |
|
1496 | - * 1. {DAV:}displayname |
|
1497 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
1498 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
1499 | - * should not be stripped). |
|
1500 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
1501 | - * should not be stripped). |
|
1502 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
1503 | - * attachments should not be stripped). |
|
1504 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
1505 | - * Sabre\DAV\Property\Href). |
|
1506 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
1507 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
1508 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
1509 | - * (should just be an instance of |
|
1510 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
1511 | - * default components). |
|
1512 | - * |
|
1513 | - * @param string $principalUri |
|
1514 | - * @return array |
|
1515 | - */ |
|
1516 | - function getSubscriptionsForUser($principalUri) { |
|
1517 | - $fields = array_values($this->subscriptionPropertyMap); |
|
1518 | - $fields[] = 'id'; |
|
1519 | - $fields[] = 'uri'; |
|
1520 | - $fields[] = 'source'; |
|
1521 | - $fields[] = 'principaluri'; |
|
1522 | - $fields[] = 'lastmodified'; |
|
1523 | - |
|
1524 | - $query = $this->db->getQueryBuilder(); |
|
1525 | - $query->select($fields) |
|
1526 | - ->from('calendarsubscriptions') |
|
1527 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1528 | - ->orderBy('calendarorder', 'asc'); |
|
1529 | - $stmt =$query->execute(); |
|
1530 | - |
|
1531 | - $subscriptions = []; |
|
1532 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1533 | - |
|
1534 | - $subscription = [ |
|
1535 | - 'id' => $row['id'], |
|
1536 | - 'uri' => $row['uri'], |
|
1537 | - 'principaluri' => $row['principaluri'], |
|
1538 | - 'source' => $row['source'], |
|
1539 | - 'lastmodified' => $row['lastmodified'], |
|
1540 | - |
|
1541 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
1542 | - ]; |
|
1543 | - |
|
1544 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1545 | - if (!is_null($row[$dbName])) { |
|
1546 | - $subscription[$xmlName] = $row[$dbName]; |
|
1547 | - } |
|
1548 | - } |
|
1549 | - |
|
1550 | - $subscriptions[] = $subscription; |
|
1551 | - |
|
1552 | - } |
|
1553 | - |
|
1554 | - return $subscriptions; |
|
1555 | - } |
|
1556 | - |
|
1557 | - /** |
|
1558 | - * Creates a new subscription for a principal. |
|
1559 | - * |
|
1560 | - * If the creation was a success, an id must be returned that can be used to reference |
|
1561 | - * this subscription in other methods, such as updateSubscription. |
|
1562 | - * |
|
1563 | - * @param string $principalUri |
|
1564 | - * @param string $uri |
|
1565 | - * @param array $properties |
|
1566 | - * @return mixed |
|
1567 | - */ |
|
1568 | - function createSubscription($principalUri, $uri, array $properties) { |
|
1569 | - |
|
1570 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
1571 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
1572 | - } |
|
1573 | - |
|
1574 | - $values = [ |
|
1575 | - 'principaluri' => $principalUri, |
|
1576 | - 'uri' => $uri, |
|
1577 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
1578 | - 'lastmodified' => time(), |
|
1579 | - ]; |
|
1580 | - |
|
1581 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
1582 | - |
|
1583 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1584 | - if (array_key_exists($xmlName, $properties)) { |
|
1585 | - $values[$dbName] = $properties[$xmlName]; |
|
1586 | - if (in_array($dbName, $propertiesBoolean)) { |
|
1587 | - $values[$dbName] = true; |
|
1588 | - } |
|
1589 | - } |
|
1590 | - } |
|
1591 | - |
|
1592 | - $valuesToInsert = array(); |
|
1593 | - |
|
1594 | - $query = $this->db->getQueryBuilder(); |
|
1595 | - |
|
1596 | - foreach (array_keys($values) as $name) { |
|
1597 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
1598 | - } |
|
1599 | - |
|
1600 | - $query->insert('calendarsubscriptions') |
|
1601 | - ->values($valuesToInsert) |
|
1602 | - ->execute(); |
|
1603 | - |
|
1604 | - return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
1605 | - } |
|
1606 | - |
|
1607 | - /** |
|
1608 | - * Updates a subscription |
|
1609 | - * |
|
1610 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
1611 | - * To do the actual updates, you must tell this object which properties |
|
1612 | - * you're going to process with the handle() method. |
|
1613 | - * |
|
1614 | - * Calling the handle method is like telling the PropPatch object "I |
|
1615 | - * promise I can handle updating this property". |
|
1616 | - * |
|
1617 | - * Read the PropPatch documentation for more info and examples. |
|
1618 | - * |
|
1619 | - * @param mixed $subscriptionId |
|
1620 | - * @param PropPatch $propPatch |
|
1621 | - * @return void |
|
1622 | - */ |
|
1623 | - function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
1624 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
1625 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
1626 | - |
|
1627 | - /** |
|
1628 | - * @suppress SqlInjectionChecker |
|
1629 | - */ |
|
1630 | - $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
1631 | - |
|
1632 | - $newValues = []; |
|
1633 | - |
|
1634 | - foreach($mutations as $propertyName=>$propertyValue) { |
|
1635 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
1636 | - $newValues['source'] = $propertyValue->getHref(); |
|
1637 | - } else { |
|
1638 | - $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
1639 | - $newValues[$fieldName] = $propertyValue; |
|
1640 | - } |
|
1641 | - } |
|
1642 | - |
|
1643 | - $query = $this->db->getQueryBuilder(); |
|
1644 | - $query->update('calendarsubscriptions') |
|
1645 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
1646 | - foreach($newValues as $fieldName=>$value) { |
|
1647 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
1648 | - } |
|
1649 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1650 | - ->execute(); |
|
1651 | - |
|
1652 | - return true; |
|
1653 | - |
|
1654 | - }); |
|
1655 | - } |
|
1656 | - |
|
1657 | - /** |
|
1658 | - * Deletes a subscription. |
|
1659 | - * |
|
1660 | - * @param mixed $subscriptionId |
|
1661 | - * @return void |
|
1662 | - */ |
|
1663 | - function deleteSubscription($subscriptionId) { |
|
1664 | - $query = $this->db->getQueryBuilder(); |
|
1665 | - $query->delete('calendarsubscriptions') |
|
1666 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1667 | - ->execute(); |
|
1668 | - } |
|
1669 | - |
|
1670 | - /** |
|
1671 | - * Returns a single scheduling object for the inbox collection. |
|
1672 | - * |
|
1673 | - * The returned array should contain the following elements: |
|
1674 | - * * uri - A unique basename for the object. This will be used to |
|
1675 | - * construct a full uri. |
|
1676 | - * * calendardata - The iCalendar object |
|
1677 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
1678 | - * timestamp, or a PHP DateTime object. |
|
1679 | - * * etag - A unique token that must change if the object changed. |
|
1680 | - * * size - The size of the object, in bytes. |
|
1681 | - * |
|
1682 | - * @param string $principalUri |
|
1683 | - * @param string $objectUri |
|
1684 | - * @return array |
|
1685 | - */ |
|
1686 | - function getSchedulingObject($principalUri, $objectUri) { |
|
1687 | - $query = $this->db->getQueryBuilder(); |
|
1688 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1689 | - ->from('schedulingobjects') |
|
1690 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1691 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1692 | - ->execute(); |
|
1693 | - |
|
1694 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
1695 | - |
|
1696 | - if(!$row) { |
|
1697 | - return null; |
|
1698 | - } |
|
1699 | - |
|
1700 | - return [ |
|
1701 | - 'uri' => $row['uri'], |
|
1702 | - 'calendardata' => $row['calendardata'], |
|
1703 | - 'lastmodified' => $row['lastmodified'], |
|
1704 | - 'etag' => '"' . $row['etag'] . '"', |
|
1705 | - 'size' => (int)$row['size'], |
|
1706 | - ]; |
|
1707 | - } |
|
1708 | - |
|
1709 | - /** |
|
1710 | - * Returns all scheduling objects for the inbox collection. |
|
1711 | - * |
|
1712 | - * These objects should be returned as an array. Every item in the array |
|
1713 | - * should follow the same structure as returned from getSchedulingObject. |
|
1714 | - * |
|
1715 | - * The main difference is that 'calendardata' is optional. |
|
1716 | - * |
|
1717 | - * @param string $principalUri |
|
1718 | - * @return array |
|
1719 | - */ |
|
1720 | - function getSchedulingObjects($principalUri) { |
|
1721 | - $query = $this->db->getQueryBuilder(); |
|
1722 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1723 | - ->from('schedulingobjects') |
|
1724 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1725 | - ->execute(); |
|
1726 | - |
|
1727 | - $result = []; |
|
1728 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
1729 | - $result[] = [ |
|
1730 | - 'calendardata' => $row['calendardata'], |
|
1731 | - 'uri' => $row['uri'], |
|
1732 | - 'lastmodified' => $row['lastmodified'], |
|
1733 | - 'etag' => '"' . $row['etag'] . '"', |
|
1734 | - 'size' => (int)$row['size'], |
|
1735 | - ]; |
|
1736 | - } |
|
1737 | - |
|
1738 | - return $result; |
|
1739 | - } |
|
1740 | - |
|
1741 | - /** |
|
1742 | - * Deletes a scheduling object from the inbox collection. |
|
1743 | - * |
|
1744 | - * @param string $principalUri |
|
1745 | - * @param string $objectUri |
|
1746 | - * @return void |
|
1747 | - */ |
|
1748 | - function deleteSchedulingObject($principalUri, $objectUri) { |
|
1749 | - $query = $this->db->getQueryBuilder(); |
|
1750 | - $query->delete('schedulingobjects') |
|
1751 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1752 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1753 | - ->execute(); |
|
1754 | - } |
|
1755 | - |
|
1756 | - /** |
|
1757 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
1758 | - * |
|
1759 | - * @param string $principalUri |
|
1760 | - * @param string $objectUri |
|
1761 | - * @param string $objectData |
|
1762 | - * @return void |
|
1763 | - */ |
|
1764 | - function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
1765 | - $query = $this->db->getQueryBuilder(); |
|
1766 | - $query->insert('schedulingobjects') |
|
1767 | - ->values([ |
|
1768 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
1769 | - 'calendardata' => $query->createNamedParameter($objectData), |
|
1770 | - 'uri' => $query->createNamedParameter($objectUri), |
|
1771 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
1772 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
1773 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
1774 | - ]) |
|
1775 | - ->execute(); |
|
1776 | - } |
|
1777 | - |
|
1778 | - /** |
|
1779 | - * Adds a change record to the calendarchanges table. |
|
1780 | - * |
|
1781 | - * @param mixed $calendarId |
|
1782 | - * @param string $objectUri |
|
1783 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
1784 | - * @return void |
|
1785 | - */ |
|
1786 | - protected function addChange($calendarId, $objectUri, $operation) { |
|
1787 | - |
|
1788 | - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1789 | - $stmt->execute([ |
|
1790 | - $objectUri, |
|
1791 | - $calendarId, |
|
1792 | - $operation, |
|
1793 | - $calendarId |
|
1794 | - ]); |
|
1795 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
1796 | - $stmt->execute([ |
|
1797 | - $calendarId |
|
1798 | - ]); |
|
1799 | - |
|
1800 | - } |
|
1801 | - |
|
1802 | - /** |
|
1803 | - * Parses some information from calendar objects, used for optimized |
|
1804 | - * calendar-queries. |
|
1805 | - * |
|
1806 | - * Returns an array with the following keys: |
|
1807 | - * * etag - An md5 checksum of the object without the quotes. |
|
1808 | - * * size - Size of the object in bytes |
|
1809 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
1810 | - * * firstOccurence |
|
1811 | - * * lastOccurence |
|
1812 | - * * uid - value of the UID property |
|
1813 | - * |
|
1814 | - * @param string $calendarData |
|
1815 | - * @return array |
|
1816 | - */ |
|
1817 | - public function getDenormalizedData($calendarData) { |
|
1818 | - |
|
1819 | - $vObject = Reader::read($calendarData); |
|
1820 | - $componentType = null; |
|
1821 | - $component = null; |
|
1822 | - $firstOccurrence = null; |
|
1823 | - $lastOccurrence = null; |
|
1824 | - $uid = null; |
|
1825 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
1826 | - foreach($vObject->getComponents() as $component) { |
|
1827 | - if ($component->name!=='VTIMEZONE') { |
|
1828 | - $componentType = $component->name; |
|
1829 | - $uid = (string)$component->UID; |
|
1830 | - break; |
|
1831 | - } |
|
1832 | - } |
|
1833 | - if (!$componentType) { |
|
1834 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
1835 | - } |
|
1836 | - if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
1837 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
1838 | - // Finding the last occurrence is a bit harder |
|
1839 | - if (!isset($component->RRULE)) { |
|
1840 | - if (isset($component->DTEND)) { |
|
1841 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
1842 | - } elseif (isset($component->DURATION)) { |
|
1843 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
1844 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
1845 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
1846 | - } elseif (!$component->DTSTART->hasTime()) { |
|
1847 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
1848 | - $endDate->modify('+1 day'); |
|
1849 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
1850 | - } else { |
|
1851 | - $lastOccurrence = $firstOccurrence; |
|
1852 | - } |
|
1853 | - } else { |
|
1854 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
1855 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
1856 | - if ($it->isInfinite()) { |
|
1857 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
1858 | - } else { |
|
1859 | - $end = $it->getDtEnd(); |
|
1860 | - while($it->valid() && $end < $maxDate) { |
|
1861 | - $end = $it->getDtEnd(); |
|
1862 | - $it->next(); |
|
1863 | - |
|
1864 | - } |
|
1865 | - $lastOccurrence = $end->getTimestamp(); |
|
1866 | - } |
|
1867 | - |
|
1868 | - } |
|
1869 | - } |
|
1870 | - |
|
1871 | - if ($component->CLASS) { |
|
1872 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
1873 | - switch ($component->CLASS->getValue()) { |
|
1874 | - case 'PUBLIC': |
|
1875 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
1876 | - break; |
|
1877 | - case 'CONFIDENTIAL': |
|
1878 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
1879 | - break; |
|
1880 | - } |
|
1881 | - } |
|
1882 | - return [ |
|
1883 | - 'etag' => md5($calendarData), |
|
1884 | - 'size' => strlen($calendarData), |
|
1885 | - 'componentType' => $componentType, |
|
1886 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
1887 | - 'lastOccurence' => $lastOccurrence, |
|
1888 | - 'uid' => $uid, |
|
1889 | - 'classification' => $classification |
|
1890 | - ]; |
|
1891 | - |
|
1892 | - } |
|
1893 | - |
|
1894 | - private function readBlob($cardData) { |
|
1895 | - if (is_resource($cardData)) { |
|
1896 | - return stream_get_contents($cardData); |
|
1897 | - } |
|
1898 | - |
|
1899 | - return $cardData; |
|
1900 | - } |
|
1901 | - |
|
1902 | - /** |
|
1903 | - * @param IShareable $shareable |
|
1904 | - * @param array $add |
|
1905 | - * @param array $remove |
|
1906 | - */ |
|
1907 | - public function updateShares($shareable, $add, $remove) { |
|
1908 | - $calendarId = $shareable->getResourceId(); |
|
1909 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent( |
|
1910 | - '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
1911 | - [ |
|
1912 | - 'calendarId' => $calendarId, |
|
1913 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1914 | - 'shares' => $this->getShares($calendarId), |
|
1915 | - 'add' => $add, |
|
1916 | - 'remove' => $remove, |
|
1917 | - ])); |
|
1918 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
1919 | - } |
|
1920 | - |
|
1921 | - /** |
|
1922 | - * @param int $resourceId |
|
1923 | - * @return array |
|
1924 | - */ |
|
1925 | - public function getShares($resourceId) { |
|
1926 | - return $this->sharingBackend->getShares($resourceId); |
|
1927 | - } |
|
1928 | - |
|
1929 | - /** |
|
1930 | - * @param boolean $value |
|
1931 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
1932 | - * @return string|null |
|
1933 | - */ |
|
1934 | - public function setPublishStatus($value, $calendar) { |
|
1935 | - |
|
1936 | - $calendarId = $calendar->getResourceId(); |
|
1937 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent( |
|
1938 | - '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
1939 | - [ |
|
1940 | - 'calendarId' => $calendarId, |
|
1941 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1942 | - 'public' => $value, |
|
1943 | - ])); |
|
1944 | - |
|
1945 | - $query = $this->db->getQueryBuilder(); |
|
1946 | - if ($value) { |
|
1947 | - $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
1948 | - $query->insert('dav_shares') |
|
1949 | - ->values([ |
|
1950 | - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
1951 | - 'type' => $query->createNamedParameter('calendar'), |
|
1952 | - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
1953 | - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
1954 | - 'publicuri' => $query->createNamedParameter($publicUri) |
|
1955 | - ]); |
|
1956 | - $query->execute(); |
|
1957 | - return $publicUri; |
|
1958 | - } |
|
1959 | - $query->delete('dav_shares') |
|
1960 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
1961 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
1962 | - $query->execute(); |
|
1963 | - return null; |
|
1964 | - } |
|
1965 | - |
|
1966 | - /** |
|
1967 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
1968 | - * @return mixed |
|
1969 | - */ |
|
1970 | - public function getPublishStatus($calendar) { |
|
1971 | - $query = $this->db->getQueryBuilder(); |
|
1972 | - $result = $query->select('publicuri') |
|
1973 | - ->from('dav_shares') |
|
1974 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
1975 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
1976 | - ->execute(); |
|
1977 | - |
|
1978 | - $row = $result->fetch(); |
|
1979 | - $result->closeCursor(); |
|
1980 | - return $row ? reset($row) : false; |
|
1981 | - } |
|
1982 | - |
|
1983 | - /** |
|
1984 | - * @param int $resourceId |
|
1985 | - * @param array $acl |
|
1986 | - * @return array |
|
1987 | - */ |
|
1988 | - public function applyShareAcl($resourceId, $acl) { |
|
1989 | - return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
1990 | - } |
|
1991 | - |
|
1992 | - |
|
1993 | - |
|
1994 | - /** |
|
1995 | - * update properties table |
|
1996 | - * |
|
1997 | - * @param int $calendarId |
|
1998 | - * @param string $objectUri |
|
1999 | - * @param string $calendarData |
|
2000 | - */ |
|
2001 | - public function updateProperties($calendarId, $objectUri, $calendarData) { |
|
2002 | - $objectId = $this->getCalendarObjectId($calendarId, $objectUri); |
|
2003 | - |
|
2004 | - try { |
|
2005 | - $vCalendar = $this->readCalendarData($calendarData); |
|
2006 | - } catch (\Exception $ex) { |
|
2007 | - return; |
|
2008 | - } |
|
2009 | - |
|
2010 | - $this->purgeProperties($calendarId, $objectId); |
|
2011 | - |
|
2012 | - $query = $this->db->getQueryBuilder(); |
|
2013 | - $query->insert($this->dbObjectPropertiesTable) |
|
2014 | - ->values( |
|
2015 | - [ |
|
2016 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
2017 | - 'objectid' => $query->createNamedParameter($objectId), |
|
2018 | - 'name' => $query->createParameter('name'), |
|
2019 | - 'parameter' => $query->createParameter('parameter'), |
|
2020 | - 'value' => $query->createParameter('value'), |
|
2021 | - ] |
|
2022 | - ); |
|
2023 | - |
|
2024 | - $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
2025 | - foreach ($vCalendar->getComponents() as $component) { |
|
2026 | - if (!in_array($component->name, $indexComponents)) { |
|
2027 | - continue; |
|
2028 | - } |
|
2029 | - |
|
2030 | - foreach ($component->children() as $property) { |
|
2031 | - if (in_array($property->name, self::$indexProperties)) { |
|
2032 | - $value = $property->getValue(); |
|
2033 | - // is this a shitty db? |
|
2034 | - if (!$this->db->supports4ByteText()) { |
|
2035 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2036 | - } |
|
2037 | - $value = substr($value, 0, 254); |
|
2038 | - |
|
2039 | - $query->setParameter('name', $property->name); |
|
2040 | - $query->setParameter('parameter', null); |
|
2041 | - $query->setParameter('value', $value); |
|
2042 | - $query->execute(); |
|
2043 | - } |
|
2044 | - |
|
2045 | - if (in_array($property->name, array_keys(self::$indexParameters))) { |
|
2046 | - $parameters = $property->parameters(); |
|
2047 | - $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
2048 | - |
|
2049 | - foreach ($parameters as $key => $value) { |
|
2050 | - if (in_array($key, $indexedParametersForProperty)) { |
|
2051 | - // is this a shitty db? |
|
2052 | - if ($this->db->supports4ByteText()) { |
|
2053 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2054 | - } |
|
2055 | - $value = substr($value, 0, 254); |
|
2056 | - |
|
2057 | - $query->setParameter('name', $property->name); |
|
2058 | - $query->setParameter('parameter', substr($key, 0, 254)); |
|
2059 | - $query->setParameter('value', substr($value, 0, 254)); |
|
2060 | - $query->execute(); |
|
2061 | - } |
|
2062 | - } |
|
2063 | - } |
|
2064 | - } |
|
2065 | - } |
|
2066 | - } |
|
2067 | - |
|
2068 | - /** |
|
2069 | - * read VCalendar data into a VCalendar object |
|
2070 | - * |
|
2071 | - * @param string $objectData |
|
2072 | - * @return VCalendar |
|
2073 | - */ |
|
2074 | - protected function readCalendarData($objectData) { |
|
2075 | - return Reader::read($objectData); |
|
2076 | - } |
|
2077 | - |
|
2078 | - /** |
|
2079 | - * delete all properties from a given calendar object |
|
2080 | - * |
|
2081 | - * @param int $calendarId |
|
2082 | - * @param int $objectId |
|
2083 | - */ |
|
2084 | - protected function purgeProperties($calendarId, $objectId) { |
|
2085 | - $query = $this->db->getQueryBuilder(); |
|
2086 | - $query->delete($this->dbObjectPropertiesTable) |
|
2087 | - ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
2088 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2089 | - $query->execute(); |
|
2090 | - } |
|
2091 | - |
|
2092 | - /** |
|
2093 | - * get ID from a given calendar object |
|
2094 | - * |
|
2095 | - * @param int $calendarId |
|
2096 | - * @param string $uri |
|
2097 | - * @return int |
|
2098 | - */ |
|
2099 | - protected function getCalendarObjectId($calendarId, $uri) { |
|
2100 | - $query = $this->db->getQueryBuilder(); |
|
2101 | - $query->select('id')->from('calendarobjects') |
|
2102 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
2103 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2104 | - |
|
2105 | - $result = $query->execute(); |
|
2106 | - $objectIds = $result->fetch(); |
|
2107 | - $result->closeCursor(); |
|
2108 | - |
|
2109 | - if (!isset($objectIds['id'])) { |
|
2110 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
2111 | - } |
|
2112 | - |
|
2113 | - return (int)$objectIds['id']; |
|
2114 | - } |
|
2115 | - |
|
2116 | - private function convertPrincipal($principalUri, $toV2) { |
|
2117 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
2118 | - list(, $name) = Uri\split($principalUri); |
|
2119 | - if ($toV2 === true) { |
|
2120 | - return "principals/users/$name"; |
|
2121 | - } |
|
2122 | - return "principals/$name"; |
|
2123 | - } |
|
2124 | - return $principalUri; |
|
2125 | - } |
|
2126 | - |
|
2127 | - private function addOwnerPrincipal(&$calendarInfo) { |
|
2128 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
2129 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
2130 | - if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
2131 | - $uri = $calendarInfo[$ownerPrincipalKey]; |
|
2132 | - } else { |
|
2133 | - $uri = $calendarInfo['principaluri']; |
|
2134 | - } |
|
2135 | - |
|
2136 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
2137 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
2138 | - $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
2139 | - } |
|
2140 | - } |
|
413 | + /** |
|
414 | + * @return array |
|
415 | + */ |
|
416 | + public function getPublicCalendars() { |
|
417 | + $fields = array_values($this->propertyMap); |
|
418 | + $fields[] = 'a.id'; |
|
419 | + $fields[] = 'a.uri'; |
|
420 | + $fields[] = 'a.synctoken'; |
|
421 | + $fields[] = 'a.components'; |
|
422 | + $fields[] = 'a.principaluri'; |
|
423 | + $fields[] = 'a.transparent'; |
|
424 | + $fields[] = 's.access'; |
|
425 | + $fields[] = 's.publicuri'; |
|
426 | + $calendars = []; |
|
427 | + $query = $this->db->getQueryBuilder(); |
|
428 | + $result = $query->select($fields) |
|
429 | + ->from('dav_shares', 's') |
|
430 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
431 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
432 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
433 | + ->execute(); |
|
434 | + |
|
435 | + while($row = $result->fetch()) { |
|
436 | + list(, $name) = Uri\split($row['principaluri']); |
|
437 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
438 | + $components = []; |
|
439 | + if ($row['components']) { |
|
440 | + $components = explode(',',$row['components']); |
|
441 | + } |
|
442 | + $calendar = [ |
|
443 | + 'id' => $row['id'], |
|
444 | + 'uri' => $row['publicuri'], |
|
445 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
446 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
447 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
448 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
449 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
450 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
451 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
452 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
453 | + ]; |
|
454 | + |
|
455 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
456 | + $calendar[$xmlName] = $row[$dbName]; |
|
457 | + } |
|
458 | + |
|
459 | + $this->addOwnerPrincipal($calendar); |
|
460 | + |
|
461 | + if (!isset($calendars[$calendar['id']])) { |
|
462 | + $calendars[$calendar['id']] = $calendar; |
|
463 | + } |
|
464 | + } |
|
465 | + $result->closeCursor(); |
|
466 | + |
|
467 | + return array_values($calendars); |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * @param string $uri |
|
472 | + * @return array |
|
473 | + * @throws NotFound |
|
474 | + */ |
|
475 | + public function getPublicCalendar($uri) { |
|
476 | + $fields = array_values($this->propertyMap); |
|
477 | + $fields[] = 'a.id'; |
|
478 | + $fields[] = 'a.uri'; |
|
479 | + $fields[] = 'a.synctoken'; |
|
480 | + $fields[] = 'a.components'; |
|
481 | + $fields[] = 'a.principaluri'; |
|
482 | + $fields[] = 'a.transparent'; |
|
483 | + $fields[] = 's.access'; |
|
484 | + $fields[] = 's.publicuri'; |
|
485 | + $query = $this->db->getQueryBuilder(); |
|
486 | + $result = $query->select($fields) |
|
487 | + ->from('dav_shares', 's') |
|
488 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
489 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
490 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
491 | + ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
492 | + ->execute(); |
|
493 | + |
|
494 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
495 | + |
|
496 | + $result->closeCursor(); |
|
497 | + |
|
498 | + if ($row === false) { |
|
499 | + throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
500 | + } |
|
501 | + |
|
502 | + list(, $name) = Uri\split($row['principaluri']); |
|
503 | + $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
504 | + $components = []; |
|
505 | + if ($row['components']) { |
|
506 | + $components = explode(',',$row['components']); |
|
507 | + } |
|
508 | + $calendar = [ |
|
509 | + 'id' => $row['id'], |
|
510 | + 'uri' => $row['publicuri'], |
|
511 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
512 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
513 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
514 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
515 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
516 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
517 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
518 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
519 | + ]; |
|
520 | + |
|
521 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
522 | + $calendar[$xmlName] = $row[$dbName]; |
|
523 | + } |
|
524 | + |
|
525 | + $this->addOwnerPrincipal($calendar); |
|
526 | + |
|
527 | + return $calendar; |
|
528 | + |
|
529 | + } |
|
530 | + |
|
531 | + /** |
|
532 | + * @param string $principal |
|
533 | + * @param string $uri |
|
534 | + * @return array|null |
|
535 | + */ |
|
536 | + public function getCalendarByUri($principal, $uri) { |
|
537 | + $fields = array_values($this->propertyMap); |
|
538 | + $fields[] = 'id'; |
|
539 | + $fields[] = 'uri'; |
|
540 | + $fields[] = 'synctoken'; |
|
541 | + $fields[] = 'components'; |
|
542 | + $fields[] = 'principaluri'; |
|
543 | + $fields[] = 'transparent'; |
|
544 | + |
|
545 | + // Making fields a comma-delimited list |
|
546 | + $query = $this->db->getQueryBuilder(); |
|
547 | + $query->select($fields)->from('calendars') |
|
548 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
549 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
550 | + ->setMaxResults(1); |
|
551 | + $stmt = $query->execute(); |
|
552 | + |
|
553 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
554 | + $stmt->closeCursor(); |
|
555 | + if ($row === false) { |
|
556 | + return null; |
|
557 | + } |
|
558 | + |
|
559 | + $components = []; |
|
560 | + if ($row['components']) { |
|
561 | + $components = explode(',',$row['components']); |
|
562 | + } |
|
563 | + |
|
564 | + $calendar = [ |
|
565 | + 'id' => $row['id'], |
|
566 | + 'uri' => $row['uri'], |
|
567 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
568 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
569 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
570 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
571 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
572 | + ]; |
|
573 | + |
|
574 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
575 | + $calendar[$xmlName] = $row[$dbName]; |
|
576 | + } |
|
577 | + |
|
578 | + $this->addOwnerPrincipal($calendar); |
|
579 | + |
|
580 | + return $calendar; |
|
581 | + } |
|
582 | + |
|
583 | + public function getCalendarById($calendarId) { |
|
584 | + $fields = array_values($this->propertyMap); |
|
585 | + $fields[] = 'id'; |
|
586 | + $fields[] = 'uri'; |
|
587 | + $fields[] = 'synctoken'; |
|
588 | + $fields[] = 'components'; |
|
589 | + $fields[] = 'principaluri'; |
|
590 | + $fields[] = 'transparent'; |
|
591 | + |
|
592 | + // Making fields a comma-delimited list |
|
593 | + $query = $this->db->getQueryBuilder(); |
|
594 | + $query->select($fields)->from('calendars') |
|
595 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
596 | + ->setMaxResults(1); |
|
597 | + $stmt = $query->execute(); |
|
598 | + |
|
599 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
600 | + $stmt->closeCursor(); |
|
601 | + if ($row === false) { |
|
602 | + return null; |
|
603 | + } |
|
604 | + |
|
605 | + $components = []; |
|
606 | + if ($row['components']) { |
|
607 | + $components = explode(',',$row['components']); |
|
608 | + } |
|
609 | + |
|
610 | + $calendar = [ |
|
611 | + 'id' => $row['id'], |
|
612 | + 'uri' => $row['uri'], |
|
613 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
614 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
615 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
616 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
617 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
618 | + ]; |
|
619 | + |
|
620 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
621 | + $calendar[$xmlName] = $row[$dbName]; |
|
622 | + } |
|
623 | + |
|
624 | + $this->addOwnerPrincipal($calendar); |
|
625 | + |
|
626 | + return $calendar; |
|
627 | + } |
|
628 | + |
|
629 | + /** |
|
630 | + * Creates a new calendar for a principal. |
|
631 | + * |
|
632 | + * If the creation was a success, an id must be returned that can be used to reference |
|
633 | + * this calendar in other methods, such as updateCalendar. |
|
634 | + * |
|
635 | + * @param string $principalUri |
|
636 | + * @param string $calendarUri |
|
637 | + * @param array $properties |
|
638 | + * @return int |
|
639 | + * @suppress SqlInjectionChecker |
|
640 | + */ |
|
641 | + function createCalendar($principalUri, $calendarUri, array $properties) { |
|
642 | + $values = [ |
|
643 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
644 | + 'uri' => $calendarUri, |
|
645 | + 'synctoken' => 1, |
|
646 | + 'transparent' => 0, |
|
647 | + 'components' => 'VEVENT,VTODO', |
|
648 | + 'displayname' => $calendarUri |
|
649 | + ]; |
|
650 | + |
|
651 | + // Default value |
|
652 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
653 | + if (isset($properties[$sccs])) { |
|
654 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
655 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
656 | + } |
|
657 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
658 | + } |
|
659 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
660 | + if (isset($properties[$transp])) { |
|
661 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
662 | + } |
|
663 | + |
|
664 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
665 | + if (isset($properties[$xmlName])) { |
|
666 | + $values[$dbName] = $properties[$xmlName]; |
|
667 | + } |
|
668 | + } |
|
669 | + |
|
670 | + $query = $this->db->getQueryBuilder(); |
|
671 | + $query->insert('calendars'); |
|
672 | + foreach($values as $column => $value) { |
|
673 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
674 | + } |
|
675 | + $query->execute(); |
|
676 | + $calendarId = $query->getLastInsertId(); |
|
677 | + |
|
678 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( |
|
679 | + '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', |
|
680 | + [ |
|
681 | + 'calendarId' => $calendarId, |
|
682 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
683 | + ])); |
|
684 | + |
|
685 | + return $calendarId; |
|
686 | + } |
|
687 | + |
|
688 | + /** |
|
689 | + * Updates properties for a calendar. |
|
690 | + * |
|
691 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
692 | + * To do the actual updates, you must tell this object which properties |
|
693 | + * you're going to process with the handle() method. |
|
694 | + * |
|
695 | + * Calling the handle method is like telling the PropPatch object "I |
|
696 | + * promise I can handle updating this property". |
|
697 | + * |
|
698 | + * Read the PropPatch documentation for more info and examples. |
|
699 | + * |
|
700 | + * @param mixed $calendarId |
|
701 | + * @param PropPatch $propPatch |
|
702 | + * @return void |
|
703 | + */ |
|
704 | + function updateCalendar($calendarId, PropPatch $propPatch) { |
|
705 | + $supportedProperties = array_keys($this->propertyMap); |
|
706 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
707 | + |
|
708 | + /** |
|
709 | + * @suppress SqlInjectionChecker |
|
710 | + */ |
|
711 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
712 | + $newValues = []; |
|
713 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
714 | + |
|
715 | + switch ($propertyName) { |
|
716 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
717 | + $fieldName = 'transparent'; |
|
718 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
719 | + break; |
|
720 | + default : |
|
721 | + $fieldName = $this->propertyMap[$propertyName]; |
|
722 | + $newValues[$fieldName] = $propertyValue; |
|
723 | + break; |
|
724 | + } |
|
725 | + |
|
726 | + } |
|
727 | + $query = $this->db->getQueryBuilder(); |
|
728 | + $query->update('calendars'); |
|
729 | + foreach ($newValues as $fieldName => $value) { |
|
730 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
731 | + } |
|
732 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
733 | + $query->execute(); |
|
734 | + |
|
735 | + $this->addChange($calendarId, "", 2); |
|
736 | + |
|
737 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( |
|
738 | + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', |
|
739 | + [ |
|
740 | + 'calendarId' => $calendarId, |
|
741 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
742 | + 'shares' => $this->getShares($calendarId), |
|
743 | + 'propertyMutations' => $mutations, |
|
744 | + ])); |
|
745 | + |
|
746 | + return true; |
|
747 | + }); |
|
748 | + } |
|
749 | + |
|
750 | + /** |
|
751 | + * Delete a calendar and all it's objects |
|
752 | + * |
|
753 | + * @param mixed $calendarId |
|
754 | + * @return void |
|
755 | + */ |
|
756 | + function deleteCalendar($calendarId) { |
|
757 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent( |
|
758 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', |
|
759 | + [ |
|
760 | + 'calendarId' => $calendarId, |
|
761 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
762 | + 'shares' => $this->getShares($calendarId), |
|
763 | + ])); |
|
764 | + |
|
765 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
766 | + $stmt->execute([$calendarId]); |
|
767 | + |
|
768 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
769 | + $stmt->execute([$calendarId]); |
|
770 | + |
|
771 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
772 | + $stmt->execute([$calendarId]); |
|
773 | + |
|
774 | + $this->sharingBackend->deleteAllShares($calendarId); |
|
775 | + |
|
776 | + $query = $this->db->getQueryBuilder(); |
|
777 | + $query->delete($this->dbObjectPropertiesTable) |
|
778 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
779 | + ->execute(); |
|
780 | + } |
|
781 | + |
|
782 | + /** |
|
783 | + * Delete all of an user's shares |
|
784 | + * |
|
785 | + * @param string $principaluri |
|
786 | + * @return void |
|
787 | + */ |
|
788 | + function deleteAllSharesByUser($principaluri) { |
|
789 | + $this->sharingBackend->deleteAllSharesByUser($principaluri); |
|
790 | + } |
|
791 | + |
|
792 | + /** |
|
793 | + * Returns all calendar objects within a calendar. |
|
794 | + * |
|
795 | + * Every item contains an array with the following keys: |
|
796 | + * * calendardata - The iCalendar-compatible calendar data |
|
797 | + * * uri - a unique key which will be used to construct the uri. This can |
|
798 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
799 | + * good idea. This is only the basename, or filename, not the full |
|
800 | + * path. |
|
801 | + * * lastmodified - a timestamp of the last modification time |
|
802 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
803 | + * '"abcdef"') |
|
804 | + * * size - The size of the calendar objects, in bytes. |
|
805 | + * * component - optional, a string containing the type of object, such |
|
806 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
807 | + * the Content-Type header. |
|
808 | + * |
|
809 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
810 | + * speed reasons. |
|
811 | + * |
|
812 | + * The calendardata is also optional. If it's not returned |
|
813 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
814 | + * calendardata. |
|
815 | + * |
|
816 | + * If neither etag or size are specified, the calendardata will be |
|
817 | + * used/fetched to determine these numbers. If both are specified the |
|
818 | + * amount of times this is needed is reduced by a great degree. |
|
819 | + * |
|
820 | + * @param mixed $calendarId |
|
821 | + * @return array |
|
822 | + */ |
|
823 | + function getCalendarObjects($calendarId) { |
|
824 | + $query = $this->db->getQueryBuilder(); |
|
825 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
826 | + ->from('calendarobjects') |
|
827 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
828 | + $stmt = $query->execute(); |
|
829 | + |
|
830 | + $result = []; |
|
831 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
832 | + $result[] = [ |
|
833 | + 'id' => $row['id'], |
|
834 | + 'uri' => $row['uri'], |
|
835 | + 'lastmodified' => $row['lastmodified'], |
|
836 | + 'etag' => '"' . $row['etag'] . '"', |
|
837 | + 'calendarid' => $row['calendarid'], |
|
838 | + 'size' => (int)$row['size'], |
|
839 | + 'component' => strtolower($row['componenttype']), |
|
840 | + 'classification'=> (int)$row['classification'] |
|
841 | + ]; |
|
842 | + } |
|
843 | + |
|
844 | + return $result; |
|
845 | + } |
|
846 | + |
|
847 | + /** |
|
848 | + * Returns information from a single calendar object, based on it's object |
|
849 | + * uri. |
|
850 | + * |
|
851 | + * The object uri is only the basename, or filename and not a full path. |
|
852 | + * |
|
853 | + * The returned array must have the same keys as getCalendarObjects. The |
|
854 | + * 'calendardata' object is required here though, while it's not required |
|
855 | + * for getCalendarObjects. |
|
856 | + * |
|
857 | + * This method must return null if the object did not exist. |
|
858 | + * |
|
859 | + * @param mixed $calendarId |
|
860 | + * @param string $objectUri |
|
861 | + * @return array|null |
|
862 | + */ |
|
863 | + function getCalendarObject($calendarId, $objectUri) { |
|
864 | + |
|
865 | + $query = $this->db->getQueryBuilder(); |
|
866 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
867 | + ->from('calendarobjects') |
|
868 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
869 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
870 | + $stmt = $query->execute(); |
|
871 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
872 | + |
|
873 | + if(!$row) return null; |
|
874 | + |
|
875 | + return [ |
|
876 | + 'id' => $row['id'], |
|
877 | + 'uri' => $row['uri'], |
|
878 | + 'lastmodified' => $row['lastmodified'], |
|
879 | + 'etag' => '"' . $row['etag'] . '"', |
|
880 | + 'calendarid' => $row['calendarid'], |
|
881 | + 'size' => (int)$row['size'], |
|
882 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
883 | + 'component' => strtolower($row['componenttype']), |
|
884 | + 'classification'=> (int)$row['classification'] |
|
885 | + ]; |
|
886 | + } |
|
887 | + |
|
888 | + /** |
|
889 | + * Returns a list of calendar objects. |
|
890 | + * |
|
891 | + * This method should work identical to getCalendarObject, but instead |
|
892 | + * return all the calendar objects in the list as an array. |
|
893 | + * |
|
894 | + * If the backend supports this, it may allow for some speed-ups. |
|
895 | + * |
|
896 | + * @param mixed $calendarId |
|
897 | + * @param string[] $uris |
|
898 | + * @return array |
|
899 | + */ |
|
900 | + function getMultipleCalendarObjects($calendarId, array $uris) { |
|
901 | + if (empty($uris)) { |
|
902 | + return []; |
|
903 | + } |
|
904 | + |
|
905 | + $chunks = array_chunk($uris, 100); |
|
906 | + $objects = []; |
|
907 | + |
|
908 | + $query = $this->db->getQueryBuilder(); |
|
909 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
910 | + ->from('calendarobjects') |
|
911 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
912 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
913 | + |
|
914 | + foreach ($chunks as $uris) { |
|
915 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
916 | + $result = $query->execute(); |
|
917 | + |
|
918 | + while ($row = $result->fetch()) { |
|
919 | + $objects[] = [ |
|
920 | + 'id' => $row['id'], |
|
921 | + 'uri' => $row['uri'], |
|
922 | + 'lastmodified' => $row['lastmodified'], |
|
923 | + 'etag' => '"' . $row['etag'] . '"', |
|
924 | + 'calendarid' => $row['calendarid'], |
|
925 | + 'size' => (int)$row['size'], |
|
926 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
927 | + 'component' => strtolower($row['componenttype']), |
|
928 | + 'classification' => (int)$row['classification'] |
|
929 | + ]; |
|
930 | + } |
|
931 | + $result->closeCursor(); |
|
932 | + } |
|
933 | + return $objects; |
|
934 | + } |
|
935 | + |
|
936 | + /** |
|
937 | + * Creates a new calendar object. |
|
938 | + * |
|
939 | + * The object uri is only the basename, or filename and not a full path. |
|
940 | + * |
|
941 | + * It is possible return an etag from this function, which will be used in |
|
942 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
943 | + * by double-quotes. |
|
944 | + * |
|
945 | + * However, you should only really return this ETag if you don't mangle the |
|
946 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
947 | + * the exact same as this request body, you should omit the ETag. |
|
948 | + * |
|
949 | + * @param mixed $calendarId |
|
950 | + * @param string $objectUri |
|
951 | + * @param string $calendarData |
|
952 | + * @return string |
|
953 | + */ |
|
954 | + function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
955 | + $extraData = $this->getDenormalizedData($calendarData); |
|
956 | + |
|
957 | + $query = $this->db->getQueryBuilder(); |
|
958 | + $query->insert('calendarobjects') |
|
959 | + ->values([ |
|
960 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
961 | + 'uri' => $query->createNamedParameter($objectUri), |
|
962 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
963 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
964 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
965 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
966 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
967 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
968 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
969 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
970 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
971 | + ]) |
|
972 | + ->execute(); |
|
973 | + |
|
974 | + $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
975 | + |
|
976 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent( |
|
977 | + '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', |
|
978 | + [ |
|
979 | + 'calendarId' => $calendarId, |
|
980 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
981 | + 'shares' => $this->getShares($calendarId), |
|
982 | + 'objectData' => $this->getCalendarObject($calendarId, $objectUri), |
|
983 | + ] |
|
984 | + )); |
|
985 | + $this->addChange($calendarId, $objectUri, 1); |
|
986 | + |
|
987 | + return '"' . $extraData['etag'] . '"'; |
|
988 | + } |
|
989 | + |
|
990 | + /** |
|
991 | + * Updates an existing calendarobject, based on it's uri. |
|
992 | + * |
|
993 | + * The object uri is only the basename, or filename and not a full path. |
|
994 | + * |
|
995 | + * It is possible return an etag from this function, which will be used in |
|
996 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
997 | + * by double-quotes. |
|
998 | + * |
|
999 | + * However, you should only really return this ETag if you don't mangle the |
|
1000 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
1001 | + * the exact same as this request body, you should omit the ETag. |
|
1002 | + * |
|
1003 | + * @param mixed $calendarId |
|
1004 | + * @param string $objectUri |
|
1005 | + * @param string $calendarData |
|
1006 | + * @return string |
|
1007 | + */ |
|
1008 | + function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
1009 | + $extraData = $this->getDenormalizedData($calendarData); |
|
1010 | + |
|
1011 | + $query = $this->db->getQueryBuilder(); |
|
1012 | + $query->update('calendarobjects') |
|
1013 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
1014 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
1015 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
1016 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
1017 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
1018 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
1019 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
1020 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
1021 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
1022 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
1023 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1024 | + ->execute(); |
|
1025 | + |
|
1026 | + $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
1027 | + |
|
1028 | + $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1029 | + if (is_array($data)) { |
|
1030 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent( |
|
1031 | + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', |
|
1032 | + [ |
|
1033 | + 'calendarId' => $calendarId, |
|
1034 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1035 | + 'shares' => $this->getShares($calendarId), |
|
1036 | + 'objectData' => $data, |
|
1037 | + ] |
|
1038 | + )); |
|
1039 | + } |
|
1040 | + $this->addChange($calendarId, $objectUri, 2); |
|
1041 | + |
|
1042 | + return '"' . $extraData['etag'] . '"'; |
|
1043 | + } |
|
1044 | + |
|
1045 | + /** |
|
1046 | + * @param int $calendarObjectId |
|
1047 | + * @param int $classification |
|
1048 | + */ |
|
1049 | + public function setClassification($calendarObjectId, $classification) { |
|
1050 | + if (!in_array($classification, [ |
|
1051 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
1052 | + ])) { |
|
1053 | + throw new \InvalidArgumentException(); |
|
1054 | + } |
|
1055 | + $query = $this->db->getQueryBuilder(); |
|
1056 | + $query->update('calendarobjects') |
|
1057 | + ->set('classification', $query->createNamedParameter($classification)) |
|
1058 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
1059 | + ->execute(); |
|
1060 | + } |
|
1061 | + |
|
1062 | + /** |
|
1063 | + * Deletes an existing calendar object. |
|
1064 | + * |
|
1065 | + * The object uri is only the basename, or filename and not a full path. |
|
1066 | + * |
|
1067 | + * @param mixed $calendarId |
|
1068 | + * @param string $objectUri |
|
1069 | + * @return void |
|
1070 | + */ |
|
1071 | + function deleteCalendarObject($calendarId, $objectUri) { |
|
1072 | + $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1073 | + if (is_array($data)) { |
|
1074 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( |
|
1075 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', |
|
1076 | + [ |
|
1077 | + 'calendarId' => $calendarId, |
|
1078 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1079 | + 'shares' => $this->getShares($calendarId), |
|
1080 | + 'objectData' => $data, |
|
1081 | + ] |
|
1082 | + )); |
|
1083 | + } |
|
1084 | + |
|
1085 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
1086 | + $stmt->execute([$calendarId, $objectUri]); |
|
1087 | + |
|
1088 | + $this->purgeProperties($calendarId, $data['id']); |
|
1089 | + |
|
1090 | + $this->addChange($calendarId, $objectUri, 3); |
|
1091 | + } |
|
1092 | + |
|
1093 | + /** |
|
1094 | + * Performs a calendar-query on the contents of this calendar. |
|
1095 | + * |
|
1096 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
1097 | + * calendar-query it is possible for a client to request a specific set of |
|
1098 | + * object, based on contents of iCalendar properties, date-ranges and |
|
1099 | + * iCalendar component types (VTODO, VEVENT). |
|
1100 | + * |
|
1101 | + * This method should just return a list of (relative) urls that match this |
|
1102 | + * query. |
|
1103 | + * |
|
1104 | + * The list of filters are specified as an array. The exact array is |
|
1105 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
1106 | + * |
|
1107 | + * Note that it is extremely likely that getCalendarObject for every path |
|
1108 | + * returned from this method will be called almost immediately after. You |
|
1109 | + * may want to anticipate this to speed up these requests. |
|
1110 | + * |
|
1111 | + * This method provides a default implementation, which parses *all* the |
|
1112 | + * iCalendar objects in the specified calendar. |
|
1113 | + * |
|
1114 | + * This default may well be good enough for personal use, and calendars |
|
1115 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
1116 | + * or high loads, you are strongly advised to optimize certain paths. |
|
1117 | + * |
|
1118 | + * The best way to do so is override this method and to optimize |
|
1119 | + * specifically for 'common filters'. |
|
1120 | + * |
|
1121 | + * Requests that are extremely common are: |
|
1122 | + * * requests for just VEVENTS |
|
1123 | + * * requests for just VTODO |
|
1124 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
1125 | + * |
|
1126 | + * ..and combinations of these requests. It may not be worth it to try to |
|
1127 | + * handle every possible situation and just rely on the (relatively |
|
1128 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
1129 | + * |
|
1130 | + * Note that especially time-range-filters may be difficult to parse. A |
|
1131 | + * time-range filter specified on a VEVENT must for instance also handle |
|
1132 | + * recurrence rules correctly. |
|
1133 | + * A good example of how to interprete all these filters can also simply |
|
1134 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
1135 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
1136 | + * to think of. |
|
1137 | + * |
|
1138 | + * @param mixed $calendarId |
|
1139 | + * @param array $filters |
|
1140 | + * @return array |
|
1141 | + */ |
|
1142 | + function calendarQuery($calendarId, array $filters) { |
|
1143 | + $componentType = null; |
|
1144 | + $requirePostFilter = true; |
|
1145 | + $timeRange = null; |
|
1146 | + |
|
1147 | + // if no filters were specified, we don't need to filter after a query |
|
1148 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
1149 | + $requirePostFilter = false; |
|
1150 | + } |
|
1151 | + |
|
1152 | + // Figuring out if there's a component filter |
|
1153 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
1154 | + $componentType = $filters['comp-filters'][0]['name']; |
|
1155 | + |
|
1156 | + // Checking if we need post-filters |
|
1157 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
1158 | + $requirePostFilter = false; |
|
1159 | + } |
|
1160 | + // There was a time-range filter |
|
1161 | + if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
1162 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
1163 | + |
|
1164 | + // If start time OR the end time is not specified, we can do a |
|
1165 | + // 100% accurate mysql query. |
|
1166 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
1167 | + $requirePostFilter = false; |
|
1168 | + } |
|
1169 | + } |
|
1170 | + |
|
1171 | + } |
|
1172 | + $columns = ['uri']; |
|
1173 | + if ($requirePostFilter) { |
|
1174 | + $columns = ['uri', 'calendardata']; |
|
1175 | + } |
|
1176 | + $query = $this->db->getQueryBuilder(); |
|
1177 | + $query->select($columns) |
|
1178 | + ->from('calendarobjects') |
|
1179 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
1180 | + |
|
1181 | + if ($componentType) { |
|
1182 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
1183 | + } |
|
1184 | + |
|
1185 | + if ($timeRange && $timeRange['start']) { |
|
1186 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
1187 | + } |
|
1188 | + if ($timeRange && $timeRange['end']) { |
|
1189 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
1190 | + } |
|
1191 | + |
|
1192 | + $stmt = $query->execute(); |
|
1193 | + |
|
1194 | + $result = []; |
|
1195 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1196 | + if ($requirePostFilter) { |
|
1197 | + if (!$this->validateFilterForObject($row, $filters)) { |
|
1198 | + continue; |
|
1199 | + } |
|
1200 | + } |
|
1201 | + $result[] = $row['uri']; |
|
1202 | + } |
|
1203 | + |
|
1204 | + return $result; |
|
1205 | + } |
|
1206 | + |
|
1207 | + /** |
|
1208 | + * custom Nextcloud search extension for CalDAV |
|
1209 | + * |
|
1210 | + * @param string $principalUri |
|
1211 | + * @param array $filters |
|
1212 | + * @param integer|null $limit |
|
1213 | + * @param integer|null $offset |
|
1214 | + * @return array |
|
1215 | + */ |
|
1216 | + public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
1217 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
1218 | + $ownCalendars = []; |
|
1219 | + $sharedCalendars = []; |
|
1220 | + |
|
1221 | + $uriMapper = []; |
|
1222 | + |
|
1223 | + foreach($calendars as $calendar) { |
|
1224 | + if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
1225 | + $ownCalendars[] = $calendar['id']; |
|
1226 | + } else { |
|
1227 | + $sharedCalendars[] = $calendar['id']; |
|
1228 | + } |
|
1229 | + $uriMapper[$calendar['id']] = $calendar['uri']; |
|
1230 | + } |
|
1231 | + if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
1232 | + return []; |
|
1233 | + } |
|
1234 | + |
|
1235 | + $query = $this->db->getQueryBuilder(); |
|
1236 | + // Calendar id expressions |
|
1237 | + $calendarExpressions = []; |
|
1238 | + foreach($ownCalendars as $id) { |
|
1239 | + $calendarExpressions[] = $query->expr() |
|
1240 | + ->eq('c.calendarid', $query->createNamedParameter($id)); |
|
1241 | + } |
|
1242 | + foreach($sharedCalendars as $id) { |
|
1243 | + $calendarExpressions[] = $query->expr()->andX( |
|
1244 | + $query->expr()->eq('c.calendarid', |
|
1245 | + $query->createNamedParameter($id)), |
|
1246 | + $query->expr()->eq('c.classification', |
|
1247 | + $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)) |
|
1248 | + ); |
|
1249 | + } |
|
1250 | + |
|
1251 | + if (count($calendarExpressions) === 1) { |
|
1252 | + $calExpr = $calendarExpressions[0]; |
|
1253 | + } else { |
|
1254 | + $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
1255 | + } |
|
1256 | + |
|
1257 | + // Component expressions |
|
1258 | + $compExpressions = []; |
|
1259 | + foreach($filters['comps'] as $comp) { |
|
1260 | + $compExpressions[] = $query->expr() |
|
1261 | + ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
1262 | + } |
|
1263 | + |
|
1264 | + if (count($compExpressions) === 1) { |
|
1265 | + $compExpr = $compExpressions[0]; |
|
1266 | + } else { |
|
1267 | + $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
1268 | + } |
|
1269 | + |
|
1270 | + if (!isset($filters['props'])) { |
|
1271 | + $filters['props'] = []; |
|
1272 | + } |
|
1273 | + if (!isset($filters['params'])) { |
|
1274 | + $filters['params'] = []; |
|
1275 | + } |
|
1276 | + |
|
1277 | + $propParamExpressions = []; |
|
1278 | + foreach($filters['props'] as $prop) { |
|
1279 | + $propParamExpressions[] = $query->expr()->andX( |
|
1280 | + $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
1281 | + $query->expr()->isNull('i.parameter') |
|
1282 | + ); |
|
1283 | + } |
|
1284 | + foreach($filters['params'] as $param) { |
|
1285 | + $propParamExpressions[] = $query->expr()->andX( |
|
1286 | + $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
1287 | + $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
1288 | + ); |
|
1289 | + } |
|
1290 | + |
|
1291 | + if (count($propParamExpressions) === 1) { |
|
1292 | + $propParamExpr = $propParamExpressions[0]; |
|
1293 | + } else { |
|
1294 | + $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
1295 | + } |
|
1296 | + |
|
1297 | + $query->select(['c.calendarid', 'c.uri']) |
|
1298 | + ->from($this->dbObjectPropertiesTable, 'i') |
|
1299 | + ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
1300 | + ->where($calExpr) |
|
1301 | + ->andWhere($compExpr) |
|
1302 | + ->andWhere($propParamExpr) |
|
1303 | + ->andWhere($query->expr()->iLike('i.value', |
|
1304 | + $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))); |
|
1305 | + |
|
1306 | + if ($offset) { |
|
1307 | + $query->setFirstResult($offset); |
|
1308 | + } |
|
1309 | + if ($limit) { |
|
1310 | + $query->setMaxResults($limit); |
|
1311 | + } |
|
1312 | + |
|
1313 | + $stmt = $query->execute(); |
|
1314 | + |
|
1315 | + $result = []; |
|
1316 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1317 | + $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
1318 | + if (!in_array($path, $result)) { |
|
1319 | + $result[] = $path; |
|
1320 | + } |
|
1321 | + } |
|
1322 | + |
|
1323 | + return $result; |
|
1324 | + } |
|
1325 | + |
|
1326 | + /** |
|
1327 | + * Searches through all of a users calendars and calendar objects to find |
|
1328 | + * an object with a specific UID. |
|
1329 | + * |
|
1330 | + * This method should return the path to this object, relative to the |
|
1331 | + * calendar home, so this path usually only contains two parts: |
|
1332 | + * |
|
1333 | + * calendarpath/objectpath.ics |
|
1334 | + * |
|
1335 | + * If the uid is not found, return null. |
|
1336 | + * |
|
1337 | + * This method should only consider * objects that the principal owns, so |
|
1338 | + * any calendars owned by other principals that also appear in this |
|
1339 | + * collection should be ignored. |
|
1340 | + * |
|
1341 | + * @param string $principalUri |
|
1342 | + * @param string $uid |
|
1343 | + * @return string|null |
|
1344 | + */ |
|
1345 | + function getCalendarObjectByUID($principalUri, $uid) { |
|
1346 | + |
|
1347 | + $query = $this->db->getQueryBuilder(); |
|
1348 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
1349 | + ->from('calendarobjects', 'co') |
|
1350 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
1351 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
1352 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
1353 | + |
|
1354 | + $stmt = $query->execute(); |
|
1355 | + |
|
1356 | + if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1357 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
1358 | + } |
|
1359 | + |
|
1360 | + return null; |
|
1361 | + } |
|
1362 | + |
|
1363 | + /** |
|
1364 | + * The getChanges method returns all the changes that have happened, since |
|
1365 | + * the specified syncToken in the specified calendar. |
|
1366 | + * |
|
1367 | + * This function should return an array, such as the following: |
|
1368 | + * |
|
1369 | + * [ |
|
1370 | + * 'syncToken' => 'The current synctoken', |
|
1371 | + * 'added' => [ |
|
1372 | + * 'new.txt', |
|
1373 | + * ], |
|
1374 | + * 'modified' => [ |
|
1375 | + * 'modified.txt', |
|
1376 | + * ], |
|
1377 | + * 'deleted' => [ |
|
1378 | + * 'foo.php.bak', |
|
1379 | + * 'old.txt' |
|
1380 | + * ] |
|
1381 | + * ); |
|
1382 | + * |
|
1383 | + * The returned syncToken property should reflect the *current* syncToken |
|
1384 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
1385 | + * property This is * needed here too, to ensure the operation is atomic. |
|
1386 | + * |
|
1387 | + * If the $syncToken argument is specified as null, this is an initial |
|
1388 | + * sync, and all members should be reported. |
|
1389 | + * |
|
1390 | + * The modified property is an array of nodenames that have changed since |
|
1391 | + * the last token. |
|
1392 | + * |
|
1393 | + * The deleted property is an array with nodenames, that have been deleted |
|
1394 | + * from collection. |
|
1395 | + * |
|
1396 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
1397 | + * 1, you only have to report changes that happened only directly in |
|
1398 | + * immediate descendants. If it's 2, it should also include changes from |
|
1399 | + * the nodes below the child collections. (grandchildren) |
|
1400 | + * |
|
1401 | + * The $limit argument allows a client to specify how many results should |
|
1402 | + * be returned at most. If the limit is not specified, it should be treated |
|
1403 | + * as infinite. |
|
1404 | + * |
|
1405 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
1406 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
1407 | + * |
|
1408 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
1409 | + * return null. |
|
1410 | + * |
|
1411 | + * The limit is 'suggestive'. You are free to ignore it. |
|
1412 | + * |
|
1413 | + * @param string $calendarId |
|
1414 | + * @param string $syncToken |
|
1415 | + * @param int $syncLevel |
|
1416 | + * @param int $limit |
|
1417 | + * @return array |
|
1418 | + */ |
|
1419 | + function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
1420 | + // Current synctoken |
|
1421 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1422 | + $stmt->execute([ $calendarId ]); |
|
1423 | + $currentToken = $stmt->fetchColumn(0); |
|
1424 | + |
|
1425 | + if (is_null($currentToken)) { |
|
1426 | + return null; |
|
1427 | + } |
|
1428 | + |
|
1429 | + $result = [ |
|
1430 | + 'syncToken' => $currentToken, |
|
1431 | + 'added' => [], |
|
1432 | + 'modified' => [], |
|
1433 | + 'deleted' => [], |
|
1434 | + ]; |
|
1435 | + |
|
1436 | + if ($syncToken) { |
|
1437 | + |
|
1438 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
1439 | + if ($limit>0) { |
|
1440 | + $query.= " LIMIT " . (int)$limit; |
|
1441 | + } |
|
1442 | + |
|
1443 | + // Fetching all changes |
|
1444 | + $stmt = $this->db->prepare($query); |
|
1445 | + $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
1446 | + |
|
1447 | + $changes = []; |
|
1448 | + |
|
1449 | + // This loop ensures that any duplicates are overwritten, only the |
|
1450 | + // last change on a node is relevant. |
|
1451 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1452 | + |
|
1453 | + $changes[$row['uri']] = $row['operation']; |
|
1454 | + |
|
1455 | + } |
|
1456 | + |
|
1457 | + foreach($changes as $uri => $operation) { |
|
1458 | + |
|
1459 | + switch($operation) { |
|
1460 | + case 1 : |
|
1461 | + $result['added'][] = $uri; |
|
1462 | + break; |
|
1463 | + case 2 : |
|
1464 | + $result['modified'][] = $uri; |
|
1465 | + break; |
|
1466 | + case 3 : |
|
1467 | + $result['deleted'][] = $uri; |
|
1468 | + break; |
|
1469 | + } |
|
1470 | + |
|
1471 | + } |
|
1472 | + } else { |
|
1473 | + // No synctoken supplied, this is the initial sync. |
|
1474 | + $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
1475 | + $stmt = $this->db->prepare($query); |
|
1476 | + $stmt->execute([$calendarId]); |
|
1477 | + |
|
1478 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
1479 | + } |
|
1480 | + return $result; |
|
1481 | + |
|
1482 | + } |
|
1483 | + |
|
1484 | + /** |
|
1485 | + * Returns a list of subscriptions for a principal. |
|
1486 | + * |
|
1487 | + * Every subscription is an array with the following keys: |
|
1488 | + * * id, a unique id that will be used by other functions to modify the |
|
1489 | + * subscription. This can be the same as the uri or a database key. |
|
1490 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
1491 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
1492 | + * principalUri passed to this method. |
|
1493 | + * |
|
1494 | + * Furthermore, all the subscription info must be returned too: |
|
1495 | + * |
|
1496 | + * 1. {DAV:}displayname |
|
1497 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
1498 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
1499 | + * should not be stripped). |
|
1500 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
1501 | + * should not be stripped). |
|
1502 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
1503 | + * attachments should not be stripped). |
|
1504 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
1505 | + * Sabre\DAV\Property\Href). |
|
1506 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
1507 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
1508 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
1509 | + * (should just be an instance of |
|
1510 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
1511 | + * default components). |
|
1512 | + * |
|
1513 | + * @param string $principalUri |
|
1514 | + * @return array |
|
1515 | + */ |
|
1516 | + function getSubscriptionsForUser($principalUri) { |
|
1517 | + $fields = array_values($this->subscriptionPropertyMap); |
|
1518 | + $fields[] = 'id'; |
|
1519 | + $fields[] = 'uri'; |
|
1520 | + $fields[] = 'source'; |
|
1521 | + $fields[] = 'principaluri'; |
|
1522 | + $fields[] = 'lastmodified'; |
|
1523 | + |
|
1524 | + $query = $this->db->getQueryBuilder(); |
|
1525 | + $query->select($fields) |
|
1526 | + ->from('calendarsubscriptions') |
|
1527 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1528 | + ->orderBy('calendarorder', 'asc'); |
|
1529 | + $stmt =$query->execute(); |
|
1530 | + |
|
1531 | + $subscriptions = []; |
|
1532 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1533 | + |
|
1534 | + $subscription = [ |
|
1535 | + 'id' => $row['id'], |
|
1536 | + 'uri' => $row['uri'], |
|
1537 | + 'principaluri' => $row['principaluri'], |
|
1538 | + 'source' => $row['source'], |
|
1539 | + 'lastmodified' => $row['lastmodified'], |
|
1540 | + |
|
1541 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
1542 | + ]; |
|
1543 | + |
|
1544 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1545 | + if (!is_null($row[$dbName])) { |
|
1546 | + $subscription[$xmlName] = $row[$dbName]; |
|
1547 | + } |
|
1548 | + } |
|
1549 | + |
|
1550 | + $subscriptions[] = $subscription; |
|
1551 | + |
|
1552 | + } |
|
1553 | + |
|
1554 | + return $subscriptions; |
|
1555 | + } |
|
1556 | + |
|
1557 | + /** |
|
1558 | + * Creates a new subscription for a principal. |
|
1559 | + * |
|
1560 | + * If the creation was a success, an id must be returned that can be used to reference |
|
1561 | + * this subscription in other methods, such as updateSubscription. |
|
1562 | + * |
|
1563 | + * @param string $principalUri |
|
1564 | + * @param string $uri |
|
1565 | + * @param array $properties |
|
1566 | + * @return mixed |
|
1567 | + */ |
|
1568 | + function createSubscription($principalUri, $uri, array $properties) { |
|
1569 | + |
|
1570 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
1571 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
1572 | + } |
|
1573 | + |
|
1574 | + $values = [ |
|
1575 | + 'principaluri' => $principalUri, |
|
1576 | + 'uri' => $uri, |
|
1577 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
1578 | + 'lastmodified' => time(), |
|
1579 | + ]; |
|
1580 | + |
|
1581 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
1582 | + |
|
1583 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1584 | + if (array_key_exists($xmlName, $properties)) { |
|
1585 | + $values[$dbName] = $properties[$xmlName]; |
|
1586 | + if (in_array($dbName, $propertiesBoolean)) { |
|
1587 | + $values[$dbName] = true; |
|
1588 | + } |
|
1589 | + } |
|
1590 | + } |
|
1591 | + |
|
1592 | + $valuesToInsert = array(); |
|
1593 | + |
|
1594 | + $query = $this->db->getQueryBuilder(); |
|
1595 | + |
|
1596 | + foreach (array_keys($values) as $name) { |
|
1597 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
1598 | + } |
|
1599 | + |
|
1600 | + $query->insert('calendarsubscriptions') |
|
1601 | + ->values($valuesToInsert) |
|
1602 | + ->execute(); |
|
1603 | + |
|
1604 | + return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
1605 | + } |
|
1606 | + |
|
1607 | + /** |
|
1608 | + * Updates a subscription |
|
1609 | + * |
|
1610 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
1611 | + * To do the actual updates, you must tell this object which properties |
|
1612 | + * you're going to process with the handle() method. |
|
1613 | + * |
|
1614 | + * Calling the handle method is like telling the PropPatch object "I |
|
1615 | + * promise I can handle updating this property". |
|
1616 | + * |
|
1617 | + * Read the PropPatch documentation for more info and examples. |
|
1618 | + * |
|
1619 | + * @param mixed $subscriptionId |
|
1620 | + * @param PropPatch $propPatch |
|
1621 | + * @return void |
|
1622 | + */ |
|
1623 | + function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
1624 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
1625 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
1626 | + |
|
1627 | + /** |
|
1628 | + * @suppress SqlInjectionChecker |
|
1629 | + */ |
|
1630 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
1631 | + |
|
1632 | + $newValues = []; |
|
1633 | + |
|
1634 | + foreach($mutations as $propertyName=>$propertyValue) { |
|
1635 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
1636 | + $newValues['source'] = $propertyValue->getHref(); |
|
1637 | + } else { |
|
1638 | + $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
1639 | + $newValues[$fieldName] = $propertyValue; |
|
1640 | + } |
|
1641 | + } |
|
1642 | + |
|
1643 | + $query = $this->db->getQueryBuilder(); |
|
1644 | + $query->update('calendarsubscriptions') |
|
1645 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
1646 | + foreach($newValues as $fieldName=>$value) { |
|
1647 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
1648 | + } |
|
1649 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1650 | + ->execute(); |
|
1651 | + |
|
1652 | + return true; |
|
1653 | + |
|
1654 | + }); |
|
1655 | + } |
|
1656 | + |
|
1657 | + /** |
|
1658 | + * Deletes a subscription. |
|
1659 | + * |
|
1660 | + * @param mixed $subscriptionId |
|
1661 | + * @return void |
|
1662 | + */ |
|
1663 | + function deleteSubscription($subscriptionId) { |
|
1664 | + $query = $this->db->getQueryBuilder(); |
|
1665 | + $query->delete('calendarsubscriptions') |
|
1666 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
1667 | + ->execute(); |
|
1668 | + } |
|
1669 | + |
|
1670 | + /** |
|
1671 | + * Returns a single scheduling object for the inbox collection. |
|
1672 | + * |
|
1673 | + * The returned array should contain the following elements: |
|
1674 | + * * uri - A unique basename for the object. This will be used to |
|
1675 | + * construct a full uri. |
|
1676 | + * * calendardata - The iCalendar object |
|
1677 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
1678 | + * timestamp, or a PHP DateTime object. |
|
1679 | + * * etag - A unique token that must change if the object changed. |
|
1680 | + * * size - The size of the object, in bytes. |
|
1681 | + * |
|
1682 | + * @param string $principalUri |
|
1683 | + * @param string $objectUri |
|
1684 | + * @return array |
|
1685 | + */ |
|
1686 | + function getSchedulingObject($principalUri, $objectUri) { |
|
1687 | + $query = $this->db->getQueryBuilder(); |
|
1688 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1689 | + ->from('schedulingobjects') |
|
1690 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1691 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1692 | + ->execute(); |
|
1693 | + |
|
1694 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
1695 | + |
|
1696 | + if(!$row) { |
|
1697 | + return null; |
|
1698 | + } |
|
1699 | + |
|
1700 | + return [ |
|
1701 | + 'uri' => $row['uri'], |
|
1702 | + 'calendardata' => $row['calendardata'], |
|
1703 | + 'lastmodified' => $row['lastmodified'], |
|
1704 | + 'etag' => '"' . $row['etag'] . '"', |
|
1705 | + 'size' => (int)$row['size'], |
|
1706 | + ]; |
|
1707 | + } |
|
1708 | + |
|
1709 | + /** |
|
1710 | + * Returns all scheduling objects for the inbox collection. |
|
1711 | + * |
|
1712 | + * These objects should be returned as an array. Every item in the array |
|
1713 | + * should follow the same structure as returned from getSchedulingObject. |
|
1714 | + * |
|
1715 | + * The main difference is that 'calendardata' is optional. |
|
1716 | + * |
|
1717 | + * @param string $principalUri |
|
1718 | + * @return array |
|
1719 | + */ |
|
1720 | + function getSchedulingObjects($principalUri) { |
|
1721 | + $query = $this->db->getQueryBuilder(); |
|
1722 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
1723 | + ->from('schedulingobjects') |
|
1724 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1725 | + ->execute(); |
|
1726 | + |
|
1727 | + $result = []; |
|
1728 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
1729 | + $result[] = [ |
|
1730 | + 'calendardata' => $row['calendardata'], |
|
1731 | + 'uri' => $row['uri'], |
|
1732 | + 'lastmodified' => $row['lastmodified'], |
|
1733 | + 'etag' => '"' . $row['etag'] . '"', |
|
1734 | + 'size' => (int)$row['size'], |
|
1735 | + ]; |
|
1736 | + } |
|
1737 | + |
|
1738 | + return $result; |
|
1739 | + } |
|
1740 | + |
|
1741 | + /** |
|
1742 | + * Deletes a scheduling object from the inbox collection. |
|
1743 | + * |
|
1744 | + * @param string $principalUri |
|
1745 | + * @param string $objectUri |
|
1746 | + * @return void |
|
1747 | + */ |
|
1748 | + function deleteSchedulingObject($principalUri, $objectUri) { |
|
1749 | + $query = $this->db->getQueryBuilder(); |
|
1750 | + $query->delete('schedulingobjects') |
|
1751 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1752 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1753 | + ->execute(); |
|
1754 | + } |
|
1755 | + |
|
1756 | + /** |
|
1757 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
1758 | + * |
|
1759 | + * @param string $principalUri |
|
1760 | + * @param string $objectUri |
|
1761 | + * @param string $objectData |
|
1762 | + * @return void |
|
1763 | + */ |
|
1764 | + function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
1765 | + $query = $this->db->getQueryBuilder(); |
|
1766 | + $query->insert('schedulingobjects') |
|
1767 | + ->values([ |
|
1768 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
1769 | + 'calendardata' => $query->createNamedParameter($objectData), |
|
1770 | + 'uri' => $query->createNamedParameter($objectUri), |
|
1771 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
1772 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
1773 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
1774 | + ]) |
|
1775 | + ->execute(); |
|
1776 | + } |
|
1777 | + |
|
1778 | + /** |
|
1779 | + * Adds a change record to the calendarchanges table. |
|
1780 | + * |
|
1781 | + * @param mixed $calendarId |
|
1782 | + * @param string $objectUri |
|
1783 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
1784 | + * @return void |
|
1785 | + */ |
|
1786 | + protected function addChange($calendarId, $objectUri, $operation) { |
|
1787 | + |
|
1788 | + $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1789 | + $stmt->execute([ |
|
1790 | + $objectUri, |
|
1791 | + $calendarId, |
|
1792 | + $operation, |
|
1793 | + $calendarId |
|
1794 | + ]); |
|
1795 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
1796 | + $stmt->execute([ |
|
1797 | + $calendarId |
|
1798 | + ]); |
|
1799 | + |
|
1800 | + } |
|
1801 | + |
|
1802 | + /** |
|
1803 | + * Parses some information from calendar objects, used for optimized |
|
1804 | + * calendar-queries. |
|
1805 | + * |
|
1806 | + * Returns an array with the following keys: |
|
1807 | + * * etag - An md5 checksum of the object without the quotes. |
|
1808 | + * * size - Size of the object in bytes |
|
1809 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
1810 | + * * firstOccurence |
|
1811 | + * * lastOccurence |
|
1812 | + * * uid - value of the UID property |
|
1813 | + * |
|
1814 | + * @param string $calendarData |
|
1815 | + * @return array |
|
1816 | + */ |
|
1817 | + public function getDenormalizedData($calendarData) { |
|
1818 | + |
|
1819 | + $vObject = Reader::read($calendarData); |
|
1820 | + $componentType = null; |
|
1821 | + $component = null; |
|
1822 | + $firstOccurrence = null; |
|
1823 | + $lastOccurrence = null; |
|
1824 | + $uid = null; |
|
1825 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
1826 | + foreach($vObject->getComponents() as $component) { |
|
1827 | + if ($component->name!=='VTIMEZONE') { |
|
1828 | + $componentType = $component->name; |
|
1829 | + $uid = (string)$component->UID; |
|
1830 | + break; |
|
1831 | + } |
|
1832 | + } |
|
1833 | + if (!$componentType) { |
|
1834 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
1835 | + } |
|
1836 | + if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
1837 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
1838 | + // Finding the last occurrence is a bit harder |
|
1839 | + if (!isset($component->RRULE)) { |
|
1840 | + if (isset($component->DTEND)) { |
|
1841 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
1842 | + } elseif (isset($component->DURATION)) { |
|
1843 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
1844 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
1845 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
1846 | + } elseif (!$component->DTSTART->hasTime()) { |
|
1847 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
1848 | + $endDate->modify('+1 day'); |
|
1849 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
1850 | + } else { |
|
1851 | + $lastOccurrence = $firstOccurrence; |
|
1852 | + } |
|
1853 | + } else { |
|
1854 | + $it = new EventIterator($vObject, (string)$component->UID); |
|
1855 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
1856 | + if ($it->isInfinite()) { |
|
1857 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
1858 | + } else { |
|
1859 | + $end = $it->getDtEnd(); |
|
1860 | + while($it->valid() && $end < $maxDate) { |
|
1861 | + $end = $it->getDtEnd(); |
|
1862 | + $it->next(); |
|
1863 | + |
|
1864 | + } |
|
1865 | + $lastOccurrence = $end->getTimestamp(); |
|
1866 | + } |
|
1867 | + |
|
1868 | + } |
|
1869 | + } |
|
1870 | + |
|
1871 | + if ($component->CLASS) { |
|
1872 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
1873 | + switch ($component->CLASS->getValue()) { |
|
1874 | + case 'PUBLIC': |
|
1875 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
1876 | + break; |
|
1877 | + case 'CONFIDENTIAL': |
|
1878 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
1879 | + break; |
|
1880 | + } |
|
1881 | + } |
|
1882 | + return [ |
|
1883 | + 'etag' => md5($calendarData), |
|
1884 | + 'size' => strlen($calendarData), |
|
1885 | + 'componentType' => $componentType, |
|
1886 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
1887 | + 'lastOccurence' => $lastOccurrence, |
|
1888 | + 'uid' => $uid, |
|
1889 | + 'classification' => $classification |
|
1890 | + ]; |
|
1891 | + |
|
1892 | + } |
|
1893 | + |
|
1894 | + private function readBlob($cardData) { |
|
1895 | + if (is_resource($cardData)) { |
|
1896 | + return stream_get_contents($cardData); |
|
1897 | + } |
|
1898 | + |
|
1899 | + return $cardData; |
|
1900 | + } |
|
1901 | + |
|
1902 | + /** |
|
1903 | + * @param IShareable $shareable |
|
1904 | + * @param array $add |
|
1905 | + * @param array $remove |
|
1906 | + */ |
|
1907 | + public function updateShares($shareable, $add, $remove) { |
|
1908 | + $calendarId = $shareable->getResourceId(); |
|
1909 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent( |
|
1910 | + '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
1911 | + [ |
|
1912 | + 'calendarId' => $calendarId, |
|
1913 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1914 | + 'shares' => $this->getShares($calendarId), |
|
1915 | + 'add' => $add, |
|
1916 | + 'remove' => $remove, |
|
1917 | + ])); |
|
1918 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
1919 | + } |
|
1920 | + |
|
1921 | + /** |
|
1922 | + * @param int $resourceId |
|
1923 | + * @return array |
|
1924 | + */ |
|
1925 | + public function getShares($resourceId) { |
|
1926 | + return $this->sharingBackend->getShares($resourceId); |
|
1927 | + } |
|
1928 | + |
|
1929 | + /** |
|
1930 | + * @param boolean $value |
|
1931 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
1932 | + * @return string|null |
|
1933 | + */ |
|
1934 | + public function setPublishStatus($value, $calendar) { |
|
1935 | + |
|
1936 | + $calendarId = $calendar->getResourceId(); |
|
1937 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent( |
|
1938 | + '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
1939 | + [ |
|
1940 | + 'calendarId' => $calendarId, |
|
1941 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1942 | + 'public' => $value, |
|
1943 | + ])); |
|
1944 | + |
|
1945 | + $query = $this->db->getQueryBuilder(); |
|
1946 | + if ($value) { |
|
1947 | + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
1948 | + $query->insert('dav_shares') |
|
1949 | + ->values([ |
|
1950 | + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
1951 | + 'type' => $query->createNamedParameter('calendar'), |
|
1952 | + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
1953 | + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
1954 | + 'publicuri' => $query->createNamedParameter($publicUri) |
|
1955 | + ]); |
|
1956 | + $query->execute(); |
|
1957 | + return $publicUri; |
|
1958 | + } |
|
1959 | + $query->delete('dav_shares') |
|
1960 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
1961 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
1962 | + $query->execute(); |
|
1963 | + return null; |
|
1964 | + } |
|
1965 | + |
|
1966 | + /** |
|
1967 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
1968 | + * @return mixed |
|
1969 | + */ |
|
1970 | + public function getPublishStatus($calendar) { |
|
1971 | + $query = $this->db->getQueryBuilder(); |
|
1972 | + $result = $query->select('publicuri') |
|
1973 | + ->from('dav_shares') |
|
1974 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
1975 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
1976 | + ->execute(); |
|
1977 | + |
|
1978 | + $row = $result->fetch(); |
|
1979 | + $result->closeCursor(); |
|
1980 | + return $row ? reset($row) : false; |
|
1981 | + } |
|
1982 | + |
|
1983 | + /** |
|
1984 | + * @param int $resourceId |
|
1985 | + * @param array $acl |
|
1986 | + * @return array |
|
1987 | + */ |
|
1988 | + public function applyShareAcl($resourceId, $acl) { |
|
1989 | + return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
1990 | + } |
|
1991 | + |
|
1992 | + |
|
1993 | + |
|
1994 | + /** |
|
1995 | + * update properties table |
|
1996 | + * |
|
1997 | + * @param int $calendarId |
|
1998 | + * @param string $objectUri |
|
1999 | + * @param string $calendarData |
|
2000 | + */ |
|
2001 | + public function updateProperties($calendarId, $objectUri, $calendarData) { |
|
2002 | + $objectId = $this->getCalendarObjectId($calendarId, $objectUri); |
|
2003 | + |
|
2004 | + try { |
|
2005 | + $vCalendar = $this->readCalendarData($calendarData); |
|
2006 | + } catch (\Exception $ex) { |
|
2007 | + return; |
|
2008 | + } |
|
2009 | + |
|
2010 | + $this->purgeProperties($calendarId, $objectId); |
|
2011 | + |
|
2012 | + $query = $this->db->getQueryBuilder(); |
|
2013 | + $query->insert($this->dbObjectPropertiesTable) |
|
2014 | + ->values( |
|
2015 | + [ |
|
2016 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
2017 | + 'objectid' => $query->createNamedParameter($objectId), |
|
2018 | + 'name' => $query->createParameter('name'), |
|
2019 | + 'parameter' => $query->createParameter('parameter'), |
|
2020 | + 'value' => $query->createParameter('value'), |
|
2021 | + ] |
|
2022 | + ); |
|
2023 | + |
|
2024 | + $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
2025 | + foreach ($vCalendar->getComponents() as $component) { |
|
2026 | + if (!in_array($component->name, $indexComponents)) { |
|
2027 | + continue; |
|
2028 | + } |
|
2029 | + |
|
2030 | + foreach ($component->children() as $property) { |
|
2031 | + if (in_array($property->name, self::$indexProperties)) { |
|
2032 | + $value = $property->getValue(); |
|
2033 | + // is this a shitty db? |
|
2034 | + if (!$this->db->supports4ByteText()) { |
|
2035 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2036 | + } |
|
2037 | + $value = substr($value, 0, 254); |
|
2038 | + |
|
2039 | + $query->setParameter('name', $property->name); |
|
2040 | + $query->setParameter('parameter', null); |
|
2041 | + $query->setParameter('value', $value); |
|
2042 | + $query->execute(); |
|
2043 | + } |
|
2044 | + |
|
2045 | + if (in_array($property->name, array_keys(self::$indexParameters))) { |
|
2046 | + $parameters = $property->parameters(); |
|
2047 | + $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
2048 | + |
|
2049 | + foreach ($parameters as $key => $value) { |
|
2050 | + if (in_array($key, $indexedParametersForProperty)) { |
|
2051 | + // is this a shitty db? |
|
2052 | + if ($this->db->supports4ByteText()) { |
|
2053 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2054 | + } |
|
2055 | + $value = substr($value, 0, 254); |
|
2056 | + |
|
2057 | + $query->setParameter('name', $property->name); |
|
2058 | + $query->setParameter('parameter', substr($key, 0, 254)); |
|
2059 | + $query->setParameter('value', substr($value, 0, 254)); |
|
2060 | + $query->execute(); |
|
2061 | + } |
|
2062 | + } |
|
2063 | + } |
|
2064 | + } |
|
2065 | + } |
|
2066 | + } |
|
2067 | + |
|
2068 | + /** |
|
2069 | + * read VCalendar data into a VCalendar object |
|
2070 | + * |
|
2071 | + * @param string $objectData |
|
2072 | + * @return VCalendar |
|
2073 | + */ |
|
2074 | + protected function readCalendarData($objectData) { |
|
2075 | + return Reader::read($objectData); |
|
2076 | + } |
|
2077 | + |
|
2078 | + /** |
|
2079 | + * delete all properties from a given calendar object |
|
2080 | + * |
|
2081 | + * @param int $calendarId |
|
2082 | + * @param int $objectId |
|
2083 | + */ |
|
2084 | + protected function purgeProperties($calendarId, $objectId) { |
|
2085 | + $query = $this->db->getQueryBuilder(); |
|
2086 | + $query->delete($this->dbObjectPropertiesTable) |
|
2087 | + ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
2088 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2089 | + $query->execute(); |
|
2090 | + } |
|
2091 | + |
|
2092 | + /** |
|
2093 | + * get ID from a given calendar object |
|
2094 | + * |
|
2095 | + * @param int $calendarId |
|
2096 | + * @param string $uri |
|
2097 | + * @return int |
|
2098 | + */ |
|
2099 | + protected function getCalendarObjectId($calendarId, $uri) { |
|
2100 | + $query = $this->db->getQueryBuilder(); |
|
2101 | + $query->select('id')->from('calendarobjects') |
|
2102 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
2103 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2104 | + |
|
2105 | + $result = $query->execute(); |
|
2106 | + $objectIds = $result->fetch(); |
|
2107 | + $result->closeCursor(); |
|
2108 | + |
|
2109 | + if (!isset($objectIds['id'])) { |
|
2110 | + throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
2111 | + } |
|
2112 | + |
|
2113 | + return (int)$objectIds['id']; |
|
2114 | + } |
|
2115 | + |
|
2116 | + private function convertPrincipal($principalUri, $toV2) { |
|
2117 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
2118 | + list(, $name) = Uri\split($principalUri); |
|
2119 | + if ($toV2 === true) { |
|
2120 | + return "principals/users/$name"; |
|
2121 | + } |
|
2122 | + return "principals/$name"; |
|
2123 | + } |
|
2124 | + return $principalUri; |
|
2125 | + } |
|
2126 | + |
|
2127 | + private function addOwnerPrincipal(&$calendarInfo) { |
|
2128 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
2129 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
2130 | + if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
2131 | + $uri = $calendarInfo[$ownerPrincipalKey]; |
|
2132 | + } else { |
|
2133 | + $uri = $calendarInfo['principaluri']; |
|
2134 | + } |
|
2135 | + |
|
2136 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
2137 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
2138 | + $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
2139 | + } |
|
2140 | + } |
|
2141 | 2141 | } |