@@ -31,59 +31,59 @@ discard block |
||
31 | 31 | $app->registerHooks(); |
32 | 32 | |
33 | 33 | \OC::$server->registerService('CardDAVSyncService', function() use ($app) { |
34 | - return $app->getSyncService(); |
|
34 | + return $app->getSyncService(); |
|
35 | 35 | }); |
36 | 36 | |
37 | 37 | $eventDispatcher = \OC::$server->getEventDispatcher(); |
38 | 38 | |
39 | 39 | $eventDispatcher->addListener('OCP\Federation\TrustedServerEvent::remove', |
40 | - function(GenericEvent $event) use ($app) { |
|
41 | - /** @var CardDavBackend $cardDavBackend */ |
|
42 | - $cardDavBackend = $app->getContainer()->query(CardDavBackend::class); |
|
43 | - $addressBookUri = $event->getSubject(); |
|
44 | - $addressBook = $cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri); |
|
45 | - if (!is_null($addressBook)) { |
|
46 | - $cardDavBackend->deleteAddressBook($addressBook['id']); |
|
47 | - } |
|
48 | - } |
|
40 | + function(GenericEvent $event) use ($app) { |
|
41 | + /** @var CardDavBackend $cardDavBackend */ |
|
42 | + $cardDavBackend = $app->getContainer()->query(CardDavBackend::class); |
|
43 | + $addressBookUri = $event->getSubject(); |
|
44 | + $addressBook = $cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri); |
|
45 | + if (!is_null($addressBook)) { |
|
46 | + $cardDavBackend->deleteAddressBook($addressBook['id']); |
|
47 | + } |
|
48 | + } |
|
49 | 49 | ); |
50 | 50 | |
51 | 51 | $eventDispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', |
52 | - function(GenericEvent $event) use ($app) { |
|
53 | - $jobList = $app->getContainer()->getServer()->getJobList(); |
|
54 | - $subscriptionData = $event->getArgument('subscriptionData'); |
|
52 | + function(GenericEvent $event) use ($app) { |
|
53 | + $jobList = $app->getContainer()->getServer()->getJobList(); |
|
54 | + $subscriptionData = $event->getArgument('subscriptionData'); |
|
55 | 55 | |
56 | - $jobList->add(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [ |
|
57 | - 'principaluri' => $subscriptionData['principaluri'], |
|
58 | - 'uri' => $subscriptionData['uri'] |
|
59 | - ]); |
|
60 | - } |
|
56 | + $jobList->add(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [ |
|
57 | + 'principaluri' => $subscriptionData['principaluri'], |
|
58 | + 'uri' => $subscriptionData['uri'] |
|
59 | + ]); |
|
60 | + } |
|
61 | 61 | ); |
62 | 62 | |
63 | 63 | $eventDispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', |
64 | - function(GenericEvent $event) use ($app) { |
|
65 | - $jobList = $app->getContainer()->getServer()->getJobList(); |
|
66 | - $subscriptionData = $event->getArgument('subscriptionData'); |
|
64 | + function(GenericEvent $event) use ($app) { |
|
65 | + $jobList = $app->getContainer()->getServer()->getJobList(); |
|
66 | + $subscriptionData = $event->getArgument('subscriptionData'); |
|
67 | 67 | |
68 | - $jobList->remove(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [ |
|
69 | - 'principaluri' => $subscriptionData['principaluri'], |
|
70 | - 'uri' => $subscriptionData['uri'] |
|
71 | - ]); |
|
68 | + $jobList->remove(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [ |
|
69 | + 'principaluri' => $subscriptionData['principaluri'], |
|
70 | + 'uri' => $subscriptionData['uri'] |
|
71 | + ]); |
|
72 | 72 | |
73 | - /** @var \OCA\DAV\CalDAV\CalDavBackend $calDavBackend */ |
|
74 | - $calDavBackend = $app->getContainer()->query(\OCA\DAV\CalDAV\CalDavBackend::class); |
|
75 | - $calDavBackend->purgeAllCachedEventsForSubscription($subscriptionData['id']); |
|
76 | - } |
|
73 | + /** @var \OCA\DAV\CalDAV\CalDavBackend $calDavBackend */ |
|
74 | + $calDavBackend = $app->getContainer()->query(\OCA\DAV\CalDAV\CalDavBackend::class); |
|
75 | + $calDavBackend->purgeAllCachedEventsForSubscription($subscriptionData['id']); |
|
76 | + } |
|
77 | 77 | ); |
78 | 78 | |
79 | 79 | $eventHandler = function() use ($app) { |
80 | - try { |
|
81 | - $job = $app->getContainer()->query(\OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob::class); |
|
82 | - $job->run([]); |
|
83 | - $app->getContainer()->getServer()->getJobList()->setLastRun($job); |
|
84 | - } catch(\Exception $ex) { |
|
85 | - $app->getContainer()->getServer()->getLogger()->logException($ex); |
|
86 | - } |
|
80 | + try { |
|
81 | + $job = $app->getContainer()->query(\OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob::class); |
|
82 | + $job->run([]); |
|
83 | + $app->getContainer()->getServer()->getJobList()->setLastRun($job); |
|
84 | + } catch(\Exception $ex) { |
|
85 | + $app->getContainer()->getServer()->getLogger()->logException($ex); |
|
86 | + } |
|
87 | 87 | }; |
88 | 88 | |
89 | 89 | $eventDispatcher->addListener('\OCP\Calendar\Resource\ForceRefreshEvent', $eventHandler); |
@@ -91,18 +91,18 @@ discard block |
||
91 | 91 | |
92 | 92 | $cm = \OC::$server->getContactsManager(); |
93 | 93 | $cm->register(function() use ($cm, $app) { |
94 | - $user = \OC::$server->getUserSession()->getUser(); |
|
95 | - if (!is_null($user)) { |
|
96 | - $app->setupContactsProvider($cm, $user->getUID()); |
|
97 | - } else { |
|
98 | - $app->setupSystemContactsProvider($cm); |
|
99 | - } |
|
94 | + $user = \OC::$server->getUserSession()->getUser(); |
|
95 | + if (!is_null($user)) { |
|
96 | + $app->setupContactsProvider($cm, $user->getUID()); |
|
97 | + } else { |
|
98 | + $app->setupSystemContactsProvider($cm); |
|
99 | + } |
|
100 | 100 | }); |
101 | 101 | |
102 | 102 | $calendarManager = \OC::$server->getCalendarManager(); |
103 | 103 | $calendarManager->register(function() use ($calendarManager, $app) { |
104 | - $user = \OC::$server->getUserSession()->getUser(); |
|
105 | - if ($user !== null) { |
|
106 | - $app->setupCalendarProvider($calendarManager, $user->getUID()); |
|
107 | - } |
|
104 | + $user = \OC::$server->getUserSession()->getUser(); |
|
105 | + if ($user !== null) { |
|
106 | + $app->setupCalendarProvider($calendarManager, $user->getUID()); |
|
107 | + } |
|
108 | 108 | }); |
@@ -36,115 +36,115 @@ |
||
36 | 36 | |
37 | 37 | class Plugin extends ServerPlugin { |
38 | 38 | |
39 | - /** |
|
40 | - * list of regular expressions for calendar user agents, |
|
41 | - * that do not support subscriptions on their own |
|
42 | - * |
|
43 | - * @var string[] |
|
44 | - */ |
|
45 | - const ENABLE_FOR_CLIENTS = [ |
|
46 | - // TODO - fill me |
|
47 | - ]; |
|
39 | + /** |
|
40 | + * list of regular expressions for calendar user agents, |
|
41 | + * that do not support subscriptions on their own |
|
42 | + * |
|
43 | + * @var string[] |
|
44 | + */ |
|
45 | + const ENABLE_FOR_CLIENTS = [ |
|
46 | + // TODO - fill me |
|
47 | + ]; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - private $enabled=false; |
|
49 | + /** |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + private $enabled=false; |
|
53 | 53 | |
54 | - /** |
|
55 | - * @var Server |
|
56 | - */ |
|
57 | - private $server; |
|
54 | + /** |
|
55 | + * @var Server |
|
56 | + */ |
|
57 | + private $server; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Plugin constructor. |
|
61 | - * |
|
62 | - * @param IRequest $request |
|
63 | - */ |
|
64 | - public function __construct(IRequest $request) { |
|
65 | - if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) { |
|
66 | - $this->enabled = true; |
|
67 | - } |
|
59 | + /** |
|
60 | + * Plugin constructor. |
|
61 | + * |
|
62 | + * @param IRequest $request |
|
63 | + */ |
|
64 | + public function __construct(IRequest $request) { |
|
65 | + if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) { |
|
66 | + $this->enabled = true; |
|
67 | + } |
|
68 | 68 | |
69 | - $magicHeader = $request->getHeader('X-NC-CALDAV-WEBCAL-CACHING'); |
|
70 | - if ($magicHeader === 'ON') { |
|
71 | - $this->enabled = true; |
|
72 | - } |
|
73 | - } |
|
69 | + $magicHeader = $request->getHeader('X-NC-CALDAV-WEBCAL-CACHING'); |
|
70 | + if ($magicHeader === 'ON') { |
|
71 | + $this->enabled = true; |
|
72 | + } |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * This initializes the plugin. |
|
77 | - * |
|
78 | - * This function is called by Sabre\DAV\Server, after |
|
79 | - * addPlugin is called. |
|
80 | - * |
|
81 | - * This method should set up the required event subscriptions. |
|
82 | - * |
|
83 | - * @param Server $server |
|
84 | - */ |
|
85 | - public function initialize(Server $server) { |
|
86 | - $this->server = $server; |
|
87 | - $server->on('beforeMethod', [$this, 'beforeMethod']); |
|
88 | - } |
|
75 | + /** |
|
76 | + * This initializes the plugin. |
|
77 | + * |
|
78 | + * This function is called by Sabre\DAV\Server, after |
|
79 | + * addPlugin is called. |
|
80 | + * |
|
81 | + * This method should set up the required event subscriptions. |
|
82 | + * |
|
83 | + * @param Server $server |
|
84 | + */ |
|
85 | + public function initialize(Server $server) { |
|
86 | + $this->server = $server; |
|
87 | + $server->on('beforeMethod', [$this, 'beforeMethod']); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @param RequestInterface $request |
|
92 | - * @param ResponseInterface $response |
|
93 | - */ |
|
94 | - public function beforeMethod(RequestInterface $request, ResponseInterface $response) { |
|
95 | - if (!$this->enabled) { |
|
96 | - return; |
|
97 | - } |
|
90 | + /** |
|
91 | + * @param RequestInterface $request |
|
92 | + * @param ResponseInterface $response |
|
93 | + */ |
|
94 | + public function beforeMethod(RequestInterface $request, ResponseInterface $response) { |
|
95 | + if (!$this->enabled) { |
|
96 | + return; |
|
97 | + } |
|
98 | 98 | |
99 | - $path = $request->getPath(); |
|
100 | - $pathParts = explode('/', ltrim($path, '/')); |
|
101 | - if (\count($pathParts) < 2) { |
|
102 | - return; |
|
103 | - } |
|
99 | + $path = $request->getPath(); |
|
100 | + $pathParts = explode('/', ltrim($path, '/')); |
|
101 | + if (\count($pathParts) < 2) { |
|
102 | + return; |
|
103 | + } |
|
104 | 104 | |
105 | - // $calendarHomePath will look like: calendars/username |
|
106 | - $calendarHomePath = $pathParts[0] . '/' . $pathParts[1]; |
|
107 | - try { |
|
108 | - $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath); |
|
109 | - if (!($calendarHome instanceof CalendarHome)) { |
|
110 | - //how did we end up here? |
|
111 | - return; |
|
112 | - } |
|
105 | + // $calendarHomePath will look like: calendars/username |
|
106 | + $calendarHomePath = $pathParts[0] . '/' . $pathParts[1]; |
|
107 | + try { |
|
108 | + $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath); |
|
109 | + if (!($calendarHome instanceof CalendarHome)) { |
|
110 | + //how did we end up here? |
|
111 | + return; |
|
112 | + } |
|
113 | 113 | |
114 | - $calendarHome->enableCachedSubscriptionsForThisRequest(); |
|
115 | - } catch(NotFound $ex) { |
|
116 | - return; |
|
117 | - } |
|
118 | - } |
|
114 | + $calendarHome->enableCachedSubscriptionsForThisRequest(); |
|
115 | + } catch(NotFound $ex) { |
|
116 | + return; |
|
117 | + } |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - public function isCachingEnabledForThisRequest():bool { |
|
124 | - return $this->enabled; |
|
125 | - } |
|
120 | + /** |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + public function isCachingEnabledForThisRequest():bool { |
|
124 | + return $this->enabled; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * This method should return a list of server-features. |
|
129 | - * |
|
130 | - * This is for example 'versioning' and is added to the DAV: header |
|
131 | - * in an OPTIONS response. |
|
132 | - * |
|
133 | - * @return string[] |
|
134 | - */ |
|
135 | - public function getFeatures():array { |
|
136 | - return ['nc-calendar-webcal-cache']; |
|
137 | - } |
|
127 | + /** |
|
128 | + * This method should return a list of server-features. |
|
129 | + * |
|
130 | + * This is for example 'versioning' and is added to the DAV: header |
|
131 | + * in an OPTIONS response. |
|
132 | + * |
|
133 | + * @return string[] |
|
134 | + */ |
|
135 | + public function getFeatures():array { |
|
136 | + return ['nc-calendar-webcal-cache']; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Returns a plugin name. |
|
141 | - * |
|
142 | - * Using this name other plugins will be able to access other plugins |
|
143 | - * using Sabre\DAV\Server::getPlugin |
|
144 | - * |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - public function getPluginName():string { |
|
148 | - return 'nc-calendar-webcal-cache'; |
|
149 | - } |
|
139 | + /** |
|
140 | + * Returns a plugin name. |
|
141 | + * |
|
142 | + * Using this name other plugins will be able to access other plugins |
|
143 | + * using Sabre\DAV\Server::getPlugin |
|
144 | + * |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + public function getPluginName():string { |
|
148 | + return 'nc-calendar-webcal-cache'; |
|
149 | + } |
|
150 | 150 | } |
@@ -36,140 +36,140 @@ |
||
36 | 36 | */ |
37 | 37 | class CachedSubscription extends \Sabre\CalDAV\Calendar { |
38 | 38 | |
39 | - /** |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function getPrincipalURI():string { |
|
43 | - return $this->calendarInfo['principaluri']; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @return array |
|
48 | - */ |
|
49 | - public function getACL():array { |
|
50 | - return [ |
|
51 | - [ |
|
52 | - 'privilege' => '{DAV:}read', |
|
53 | - 'principal' => $this->getOwner(), |
|
54 | - 'protected' => true, |
|
55 | - ] |
|
56 | - ]; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @return array |
|
61 | - */ |
|
62 | - public function getChildACL():array { |
|
63 | - return $this->getACL(); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @return null|string |
|
68 | - */ |
|
69 | - public function getOwner() { |
|
70 | - if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
71 | - return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; |
|
72 | - } |
|
73 | - return parent::getOwner(); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * |
|
78 | - */ |
|
79 | - public function delete() { |
|
80 | - $this->caldavBackend->deleteSubscription($this->calendarInfo['id']); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param PropPatch $propPatch |
|
85 | - */ |
|
86 | - public function propPatch(PropPatch $propPatch) { |
|
87 | - $this->caldavBackend->updateSubscription($this->calendarInfo['id'], $propPatch); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $name |
|
92 | - * @return CalendarObject|\Sabre\CalDAV\ICalendarObject |
|
93 | - * @throws NotFound |
|
94 | - */ |
|
95 | - public function getChild($name) { |
|
96 | - $obj = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $name); |
|
97 | - if (!$obj) { |
|
98 | - throw new NotFound('Calendar object not found'); |
|
99 | - } |
|
100 | - |
|
101 | - $obj['acl'] = $this->getChildACL(); |
|
102 | - return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
103 | - |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function getChildren():array { |
|
110 | - $objs = $this->caldavBackend->getCachedCalendarObjects($this->calendarInfo['id']); |
|
111 | - |
|
112 | - $children = []; |
|
113 | - foreach($objs as $obj) { |
|
114 | - $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
115 | - } |
|
116 | - |
|
117 | - return $children; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @param array $paths |
|
122 | - * @return array |
|
123 | - */ |
|
124 | - public function getMultipleChildren(array $paths):array { |
|
125 | - $objs = $this->caldavBackend->getMultipleCachedCalendarObjects($this->calendarInfo['id'], $paths); |
|
126 | - |
|
127 | - $children = []; |
|
128 | - foreach($objs as $obj) { |
|
129 | - $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
130 | - } |
|
131 | - |
|
132 | - return $children; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * @param string $name |
|
137 | - * @param null $calendarData |
|
138 | - * @return null|string|void |
|
139 | - * @throws MethodNotAllowed |
|
140 | - */ |
|
141 | - public function createFile($name, $calendarData = null) { |
|
142 | - throw new MethodNotAllowed('Creating objects in cached subscription is not allowed'); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $name |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function childExists($name):bool { |
|
150 | - $obj = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $name); |
|
151 | - if (!$obj) { |
|
152 | - return false; |
|
153 | - } |
|
154 | - |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param array $filters |
|
160 | - * @return array |
|
161 | - */ |
|
162 | - public function calendarQuery(array $filters):array { |
|
163 | - return $this->caldavBackend->cachedCalendarQuery($this->calendarInfo['id'], $filters); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * CachedSubscriptions don't support sync-tokens for now |
|
168 | - * Clients will have to do a multi get etag for now |
|
169 | - * |
|
170 | - * @return null |
|
171 | - */ |
|
172 | - public function getSyncToken() { |
|
173 | - return null; |
|
174 | - } |
|
39 | + /** |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function getPrincipalURI():string { |
|
43 | + return $this->calendarInfo['principaluri']; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @return array |
|
48 | + */ |
|
49 | + public function getACL():array { |
|
50 | + return [ |
|
51 | + [ |
|
52 | + 'privilege' => '{DAV:}read', |
|
53 | + 'principal' => $this->getOwner(), |
|
54 | + 'protected' => true, |
|
55 | + ] |
|
56 | + ]; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @return array |
|
61 | + */ |
|
62 | + public function getChildACL():array { |
|
63 | + return $this->getACL(); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @return null|string |
|
68 | + */ |
|
69 | + public function getOwner() { |
|
70 | + if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
71 | + return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; |
|
72 | + } |
|
73 | + return parent::getOwner(); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * |
|
78 | + */ |
|
79 | + public function delete() { |
|
80 | + $this->caldavBackend->deleteSubscription($this->calendarInfo['id']); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param PropPatch $propPatch |
|
85 | + */ |
|
86 | + public function propPatch(PropPatch $propPatch) { |
|
87 | + $this->caldavBackend->updateSubscription($this->calendarInfo['id'], $propPatch); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $name |
|
92 | + * @return CalendarObject|\Sabre\CalDAV\ICalendarObject |
|
93 | + * @throws NotFound |
|
94 | + */ |
|
95 | + public function getChild($name) { |
|
96 | + $obj = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $name); |
|
97 | + if (!$obj) { |
|
98 | + throw new NotFound('Calendar object not found'); |
|
99 | + } |
|
100 | + |
|
101 | + $obj['acl'] = $this->getChildACL(); |
|
102 | + return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
103 | + |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function getChildren():array { |
|
110 | + $objs = $this->caldavBackend->getCachedCalendarObjects($this->calendarInfo['id']); |
|
111 | + |
|
112 | + $children = []; |
|
113 | + foreach($objs as $obj) { |
|
114 | + $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
115 | + } |
|
116 | + |
|
117 | + return $children; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @param array $paths |
|
122 | + * @return array |
|
123 | + */ |
|
124 | + public function getMultipleChildren(array $paths):array { |
|
125 | + $objs = $this->caldavBackend->getMultipleCachedCalendarObjects($this->calendarInfo['id'], $paths); |
|
126 | + |
|
127 | + $children = []; |
|
128 | + foreach($objs as $obj) { |
|
129 | + $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj); |
|
130 | + } |
|
131 | + |
|
132 | + return $children; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * @param string $name |
|
137 | + * @param null $calendarData |
|
138 | + * @return null|string|void |
|
139 | + * @throws MethodNotAllowed |
|
140 | + */ |
|
141 | + public function createFile($name, $calendarData = null) { |
|
142 | + throw new MethodNotAllowed('Creating objects in cached subscription is not allowed'); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $name |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function childExists($name):bool { |
|
150 | + $obj = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $name); |
|
151 | + if (!$obj) { |
|
152 | + return false; |
|
153 | + } |
|
154 | + |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param array $filters |
|
160 | + * @return array |
|
161 | + */ |
|
162 | + public function calendarQuery(array $filters):array { |
|
163 | + return $this->caldavBackend->cachedCalendarQuery($this->calendarInfo['id'], $filters); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * CachedSubscriptions don't support sync-tokens for now |
|
168 | + * Clients will have to do a multi get etag for now |
|
169 | + * |
|
170 | + * @return null |
|
171 | + */ |
|
172 | + public function getSyncToken() { |
|
173 | + return null; |
|
174 | + } |
|
175 | 175 | } |
@@ -37,127 +37,127 @@ |
||
37 | 37 | |
38 | 38 | class CalendarHome extends \Sabre\CalDAV\CalendarHome { |
39 | 39 | |
40 | - /** @var \OCP\IL10N */ |
|
41 | - private $l10n; |
|
42 | - |
|
43 | - /** @var \OCP\IConfig */ |
|
44 | - private $config; |
|
45 | - |
|
46 | - /** @var bool */ |
|
47 | - private $returnCachedSubscriptions=false; |
|
48 | - |
|
49 | - public function __construct(BackendInterface $caldavBackend, $principalInfo) { |
|
50 | - parent::__construct($caldavBackend, $principalInfo); |
|
51 | - $this->l10n = \OC::$server->getL10N('dav'); |
|
52 | - $this->config = \OC::$server->getConfig(); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return BackendInterface |
|
57 | - */ |
|
58 | - public function getCalDAVBackend() { |
|
59 | - return $this->caldavBackend; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @inheritdoc |
|
64 | - */ |
|
65 | - function createExtendedCollection($name, MkCol $mkCol) { |
|
66 | - $reservedNames = [BirthdayService::BIRTHDAY_CALENDAR_URI]; |
|
67 | - |
|
68 | - if (in_array($name, $reservedNames)) { |
|
69 | - throw new MethodNotAllowed('The resource you tried to create has a reserved name'); |
|
70 | - } |
|
71 | - |
|
72 | - parent::createExtendedCollection($name, $mkCol); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritdoc |
|
77 | - */ |
|
78 | - function getChildren() { |
|
79 | - $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']); |
|
80 | - $objects = []; |
|
81 | - foreach ($calendars as $calendar) { |
|
82 | - $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
83 | - } |
|
84 | - |
|
85 | - if ($this->caldavBackend instanceof SchedulingSupport) { |
|
86 | - $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
87 | - $objects[] = new Outbox($this->principalInfo['uri']); |
|
88 | - } |
|
89 | - |
|
90 | - // We're adding a notifications node, if it's supported by the backend. |
|
91 | - if ($this->caldavBackend instanceof NotificationSupport) { |
|
92 | - $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
93 | - } |
|
94 | - |
|
95 | - // If the backend supports subscriptions, we'll add those as well, |
|
96 | - if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
97 | - foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
98 | - if ($this->returnCachedSubscriptions) { |
|
99 | - $objects[] = new CachedSubscription($this->caldavBackend, $subscription); |
|
100 | - } else { |
|
101 | - $objects[] = new Subscription($this->caldavBackend, $subscription); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - return $objects; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @inheritdoc |
|
111 | - */ |
|
112 | - function getChild($name) { |
|
113 | - // Special nodes |
|
114 | - if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
115 | - return new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
116 | - } |
|
117 | - if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
118 | - return new Outbox($this->principalInfo['uri']); |
|
119 | - } |
|
120 | - if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) { |
|
121 | - return new \Sabre\CalDAv\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
122 | - } |
|
123 | - |
|
124 | - // Calendars |
|
125 | - foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) { |
|
126 | - if ($calendar['uri'] === $name) { |
|
127 | - return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
132 | - foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
133 | - if ($subscription['uri'] === $name) { |
|
134 | - if ($this->returnCachedSubscriptions) { |
|
135 | - return new CachedSubscription($this->caldavBackend, $subscription); |
|
136 | - } |
|
137 | - |
|
138 | - return new Subscription($this->caldavBackend, $subscription); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - } |
|
143 | - |
|
144 | - throw new NotFound('Node with name \'' . $name . '\' could not be found'); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param array $filters |
|
149 | - * @param integer|null $limit |
|
150 | - * @param integer|null $offset |
|
151 | - */ |
|
152 | - function calendarSearch(array $filters, $limit=null, $offset=null) { |
|
153 | - $principalUri = $this->principalInfo['uri']; |
|
154 | - return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * |
|
159 | - */ |
|
160 | - public function enableCachedSubscriptionsForThisRequest() { |
|
161 | - $this->returnCachedSubscriptions = true; |
|
162 | - } |
|
40 | + /** @var \OCP\IL10N */ |
|
41 | + private $l10n; |
|
42 | + |
|
43 | + /** @var \OCP\IConfig */ |
|
44 | + private $config; |
|
45 | + |
|
46 | + /** @var bool */ |
|
47 | + private $returnCachedSubscriptions=false; |
|
48 | + |
|
49 | + public function __construct(BackendInterface $caldavBackend, $principalInfo) { |
|
50 | + parent::__construct($caldavBackend, $principalInfo); |
|
51 | + $this->l10n = \OC::$server->getL10N('dav'); |
|
52 | + $this->config = \OC::$server->getConfig(); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return BackendInterface |
|
57 | + */ |
|
58 | + public function getCalDAVBackend() { |
|
59 | + return $this->caldavBackend; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @inheritdoc |
|
64 | + */ |
|
65 | + function createExtendedCollection($name, MkCol $mkCol) { |
|
66 | + $reservedNames = [BirthdayService::BIRTHDAY_CALENDAR_URI]; |
|
67 | + |
|
68 | + if (in_array($name, $reservedNames)) { |
|
69 | + throw new MethodNotAllowed('The resource you tried to create has a reserved name'); |
|
70 | + } |
|
71 | + |
|
72 | + parent::createExtendedCollection($name, $mkCol); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritdoc |
|
77 | + */ |
|
78 | + function getChildren() { |
|
79 | + $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']); |
|
80 | + $objects = []; |
|
81 | + foreach ($calendars as $calendar) { |
|
82 | + $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
83 | + } |
|
84 | + |
|
85 | + if ($this->caldavBackend instanceof SchedulingSupport) { |
|
86 | + $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
87 | + $objects[] = new Outbox($this->principalInfo['uri']); |
|
88 | + } |
|
89 | + |
|
90 | + // We're adding a notifications node, if it's supported by the backend. |
|
91 | + if ($this->caldavBackend instanceof NotificationSupport) { |
|
92 | + $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
93 | + } |
|
94 | + |
|
95 | + // If the backend supports subscriptions, we'll add those as well, |
|
96 | + if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
97 | + foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
98 | + if ($this->returnCachedSubscriptions) { |
|
99 | + $objects[] = new CachedSubscription($this->caldavBackend, $subscription); |
|
100 | + } else { |
|
101 | + $objects[] = new Subscription($this->caldavBackend, $subscription); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + return $objects; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @inheritdoc |
|
111 | + */ |
|
112 | + function getChild($name) { |
|
113 | + // Special nodes |
|
114 | + if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
115 | + return new Inbox($this->caldavBackend, $this->principalInfo['uri']); |
|
116 | + } |
|
117 | + if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) { |
|
118 | + return new Outbox($this->principalInfo['uri']); |
|
119 | + } |
|
120 | + if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) { |
|
121 | + return new \Sabre\CalDAv\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']); |
|
122 | + } |
|
123 | + |
|
124 | + // Calendars |
|
125 | + foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) { |
|
126 | + if ($calendar['uri'] === $name) { |
|
127 | + return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + if ($this->caldavBackend instanceof SubscriptionSupport) { |
|
132 | + foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { |
|
133 | + if ($subscription['uri'] === $name) { |
|
134 | + if ($this->returnCachedSubscriptions) { |
|
135 | + return new CachedSubscription($this->caldavBackend, $subscription); |
|
136 | + } |
|
137 | + |
|
138 | + return new Subscription($this->caldavBackend, $subscription); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + } |
|
143 | + |
|
144 | + throw new NotFound('Node with name \'' . $name . '\' could not be found'); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param array $filters |
|
149 | + * @param integer|null $limit |
|
150 | + * @param integer|null $offset |
|
151 | + */ |
|
152 | + function calendarSearch(array $filters, $limit=null, $offset=null) { |
|
153 | + $principalUri = $this->principalInfo['uri']; |
|
154 | + return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * |
|
159 | + */ |
|
160 | + public function enableCachedSubscriptionsForThisRequest() { |
|
161 | + $this->returnCachedSubscriptions = true; |
|
162 | + } |
|
163 | 163 | } |
@@ -33,32 +33,32 @@ |
||
33 | 33 | */ |
34 | 34 | class CachedSubscriptionObject extends \Sabre\CalDAV\CalendarObject { |
35 | 35 | |
36 | - /** |
|
37 | - * @inheritdoc |
|
38 | - */ |
|
39 | - public function get() { |
|
40 | - // Pre-populating the 'calendardata' is optional, if we don't have it |
|
41 | - // already we fetch it from the backend. |
|
42 | - if (!isset($this->objectData['calendardata'])) { |
|
43 | - $this->objectData = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $this->objectData['uri']); |
|
44 | - } |
|
36 | + /** |
|
37 | + * @inheritdoc |
|
38 | + */ |
|
39 | + public function get() { |
|
40 | + // Pre-populating the 'calendardata' is optional, if we don't have it |
|
41 | + // already we fetch it from the backend. |
|
42 | + if (!isset($this->objectData['calendardata'])) { |
|
43 | + $this->objectData = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $this->objectData['uri']); |
|
44 | + } |
|
45 | 45 | |
46 | - return $this->objectData['calendardata']; |
|
47 | - } |
|
46 | + return $this->objectData['calendardata']; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param resource|string $calendarData |
|
51 | - * @return string|void |
|
52 | - * @throws MethodNotAllowed |
|
53 | - */ |
|
54 | - public function put($calendarData) { |
|
55 | - throw new MethodNotAllowed('Creating objects in cached subscription is not allowed'); |
|
56 | - } |
|
49 | + /** |
|
50 | + * @param resource|string $calendarData |
|
51 | + * @return string|void |
|
52 | + * @throws MethodNotAllowed |
|
53 | + */ |
|
54 | + public function put($calendarData) { |
|
55 | + throw new MethodNotAllowed('Creating objects in cached subscription is not allowed'); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @throws MethodNotAllowed |
|
60 | - */ |
|
61 | - public function delete() { |
|
62 | - throw new MethodNotAllowed('Deleting objects in cached subscription is not allowed'); |
|
63 | - } |
|
58 | + /** |
|
59 | + * @throws MethodNotAllowed |
|
60 | + */ |
|
61 | + public function delete() { |
|
62 | + throw new MethodNotAllowed('Deleting objects in cached subscription is not allowed'); |
|
63 | + } |
|
64 | 64 | } |
@@ -66,244 +66,244 @@ |
||
66 | 66 | |
67 | 67 | class Server { |
68 | 68 | |
69 | - /** @var IRequest */ |
|
70 | - private $request; |
|
69 | + /** @var IRequest */ |
|
70 | + private $request; |
|
71 | 71 | |
72 | - /** @var string */ |
|
73 | - private $baseUri; |
|
72 | + /** @var string */ |
|
73 | + private $baseUri; |
|
74 | 74 | |
75 | - /** @var Connector\Sabre\Server */ |
|
76 | - public $server; |
|
75 | + /** @var Connector\Sabre\Server */ |
|
76 | + public $server; |
|
77 | 77 | |
78 | - public function __construct(IRequest $request, $baseUri) { |
|
79 | - $this->request = $request; |
|
80 | - $this->baseUri = $baseUri; |
|
81 | - $logger = \OC::$server->getLogger(); |
|
82 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
78 | + public function __construct(IRequest $request, $baseUri) { |
|
79 | + $this->request = $request; |
|
80 | + $this->baseUri = $baseUri; |
|
81 | + $logger = \OC::$server->getLogger(); |
|
82 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
83 | 83 | |
84 | - $root = new RootCollection(); |
|
85 | - $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
|
84 | + $root = new RootCollection(); |
|
85 | + $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
|
86 | 86 | |
87 | - // Add maintenance plugin |
|
88 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
87 | + // Add maintenance plugin |
|
88 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
89 | 89 | |
90 | - // Backends |
|
91 | - $authBackend = new Auth( |
|
92 | - \OC::$server->getSession(), |
|
93 | - \OC::$server->getUserSession(), |
|
94 | - \OC::$server->getRequest(), |
|
95 | - \OC::$server->getTwoFactorAuthManager(), |
|
96 | - \OC::$server->getBruteForceThrottler() |
|
97 | - ); |
|
90 | + // Backends |
|
91 | + $authBackend = new Auth( |
|
92 | + \OC::$server->getSession(), |
|
93 | + \OC::$server->getUserSession(), |
|
94 | + \OC::$server->getRequest(), |
|
95 | + \OC::$server->getTwoFactorAuthManager(), |
|
96 | + \OC::$server->getBruteForceThrottler() |
|
97 | + ); |
|
98 | 98 | |
99 | - // Set URL explicitly due to reverse-proxy situations |
|
100 | - $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
101 | - $this->server->setBaseUri($this->baseUri); |
|
99 | + // Set URL explicitly due to reverse-proxy situations |
|
100 | + $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
101 | + $this->server->setBaseUri($this->baseUri); |
|
102 | 102 | |
103 | - $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
104 | - $this->server->addPlugin(new AnonymousOptionsPlugin()); |
|
105 | - $authPlugin = new Plugin(); |
|
106 | - $authPlugin->addBackend(new PublicAuth()); |
|
107 | - $this->server->addPlugin($authPlugin); |
|
103 | + $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
104 | + $this->server->addPlugin(new AnonymousOptionsPlugin()); |
|
105 | + $authPlugin = new Plugin(); |
|
106 | + $authPlugin->addBackend(new PublicAuth()); |
|
107 | + $this->server->addPlugin($authPlugin); |
|
108 | 108 | |
109 | - // allow setup of additional auth backends |
|
110 | - $event = new SabrePluginEvent($this->server); |
|
111 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
109 | + // allow setup of additional auth backends |
|
110 | + $event = new SabrePluginEvent($this->server); |
|
111 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
112 | 112 | |
113 | - $bearerAuthBackend = new BearerAuth( |
|
114 | - \OC::$server->getUserSession(), |
|
115 | - \OC::$server->getSession(), |
|
116 | - \OC::$server->getRequest() |
|
117 | - ); |
|
118 | - $authPlugin->addBackend($bearerAuthBackend); |
|
119 | - // because we are throwing exceptions this plugin has to be the last one |
|
120 | - $authPlugin->addBackend($authBackend); |
|
113 | + $bearerAuthBackend = new BearerAuth( |
|
114 | + \OC::$server->getUserSession(), |
|
115 | + \OC::$server->getSession(), |
|
116 | + \OC::$server->getRequest() |
|
117 | + ); |
|
118 | + $authPlugin->addBackend($bearerAuthBackend); |
|
119 | + // because we are throwing exceptions this plugin has to be the last one |
|
120 | + $authPlugin->addBackend($authBackend); |
|
121 | 121 | |
122 | - // debugging |
|
123 | - if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
124 | - $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
125 | - } else { |
|
126 | - $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
127 | - } |
|
122 | + // debugging |
|
123 | + if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
124 | + $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
125 | + } else { |
|
126 | + $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
127 | + } |
|
128 | 128 | |
129 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
130 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
131 | - $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
129 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
130 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
131 | + $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
132 | 132 | |
133 | - // acl |
|
134 | - $acl = new DavAclPlugin(); |
|
135 | - $acl->principalCollectionSet = [ |
|
136 | - 'principals/users', 'principals/groups', |
|
137 | - 'principals/calendar-resources', |
|
138 | - 'principals/calendar-rooms', |
|
139 | - ]; |
|
140 | - $acl->defaultUsernamePath = 'principals/users'; |
|
141 | - $this->server->addPlugin($acl); |
|
133 | + // acl |
|
134 | + $acl = new DavAclPlugin(); |
|
135 | + $acl->principalCollectionSet = [ |
|
136 | + 'principals/users', 'principals/groups', |
|
137 | + 'principals/calendar-resources', |
|
138 | + 'principals/calendar-rooms', |
|
139 | + ]; |
|
140 | + $acl->defaultUsernamePath = 'principals/users'; |
|
141 | + $this->server->addPlugin($acl); |
|
142 | 142 | |
143 | - // calendar plugins |
|
144 | - if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) { |
|
145 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
146 | - $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
147 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
148 | - if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { |
|
149 | - $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
150 | - } |
|
151 | - $webcalCachingPlugin = new CalDAV\WebcalCaching\Plugin($request); |
|
152 | - $this->server->addPlugin($webcalCachingPlugin); |
|
153 | - if (!$webcalCachingPlugin->isCachingEnabledForThisRequest()) { |
|
154 | - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
155 | - } |
|
156 | - $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
157 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
158 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
159 | - \OC::$server->getConfig(), |
|
160 | - \OC::$server->getURLGenerator() |
|
161 | - )); |
|
162 | - } |
|
143 | + // calendar plugins |
|
144 | + if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) { |
|
145 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
146 | + $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
147 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
148 | + if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { |
|
149 | + $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
150 | + } |
|
151 | + $webcalCachingPlugin = new CalDAV\WebcalCaching\Plugin($request); |
|
152 | + $this->server->addPlugin($webcalCachingPlugin); |
|
153 | + if (!$webcalCachingPlugin->isCachingEnabledForThisRequest()) { |
|
154 | + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
155 | + } |
|
156 | + $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
157 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
158 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
159 | + \OC::$server->getConfig(), |
|
160 | + \OC::$server->getURLGenerator() |
|
161 | + )); |
|
162 | + } |
|
163 | 163 | |
164 | - // addressbook plugins |
|
165 | - if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { |
|
166 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
167 | - $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
168 | - $this->server->addPlugin(new VCFExportPlugin()); |
|
169 | - $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
|
170 | - } |
|
164 | + // addressbook plugins |
|
165 | + if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { |
|
166 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
167 | + $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
168 | + $this->server->addPlugin(new VCFExportPlugin()); |
|
169 | + $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
|
170 | + } |
|
171 | 171 | |
172 | - // system tags plugins |
|
173 | - $this->server->addPlugin(new SystemTagPlugin( |
|
174 | - \OC::$server->getSystemTagManager(), |
|
175 | - \OC::$server->getGroupManager(), |
|
176 | - \OC::$server->getUserSession() |
|
177 | - )); |
|
172 | + // system tags plugins |
|
173 | + $this->server->addPlugin(new SystemTagPlugin( |
|
174 | + \OC::$server->getSystemTagManager(), |
|
175 | + \OC::$server->getGroupManager(), |
|
176 | + \OC::$server->getUserSession() |
|
177 | + )); |
|
178 | 178 | |
179 | - // comments plugin |
|
180 | - $this->server->addPlugin(new CommentsPlugin( |
|
181 | - \OC::$server->getCommentsManager(), |
|
182 | - \OC::$server->getUserSession() |
|
183 | - )); |
|
179 | + // comments plugin |
|
180 | + $this->server->addPlugin(new CommentsPlugin( |
|
181 | + \OC::$server->getCommentsManager(), |
|
182 | + \OC::$server->getUserSession() |
|
183 | + )); |
|
184 | 184 | |
185 | - $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
186 | - $this->server->addPlugin(new ChunkingPlugin()); |
|
185 | + $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
186 | + $this->server->addPlugin(new ChunkingPlugin()); |
|
187 | 187 | |
188 | - // allow setup of additional plugins |
|
189 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
|
188 | + // allow setup of additional plugins |
|
189 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
|
190 | 190 | |
191 | - // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
192 | - // we do not provide locking we emulate it using a fake locking plugin. |
|
193 | - if($request->isUserAgent([ |
|
194 | - '/WebDAVFS/', |
|
195 | - '/OneNote/', |
|
196 | - '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
197 | - ])) { |
|
198 | - $this->server->addPlugin(new FakeLockerPlugin()); |
|
199 | - } |
|
191 | + // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
192 | + // we do not provide locking we emulate it using a fake locking plugin. |
|
193 | + if($request->isUserAgent([ |
|
194 | + '/WebDAVFS/', |
|
195 | + '/OneNote/', |
|
196 | + '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
197 | + ])) { |
|
198 | + $this->server->addPlugin(new FakeLockerPlugin()); |
|
199 | + } |
|
200 | 200 | |
201 | - if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
202 | - $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
203 | - } |
|
201 | + if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
202 | + $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
203 | + } |
|
204 | 204 | |
205 | - $lazySearchBackend = new LazySearchBackend(); |
|
206 | - $this->server->addPlugin(new SearchPlugin($lazySearchBackend)); |
|
205 | + $lazySearchBackend = new LazySearchBackend(); |
|
206 | + $this->server->addPlugin(new SearchPlugin($lazySearchBackend)); |
|
207 | 207 | |
208 | - // wait with registering these until auth is handled and the filesystem is setup |
|
209 | - $this->server->on('beforeMethod', function () use ($root, $lazySearchBackend) { |
|
210 | - // custom properties plugin must be the last one |
|
211 | - $userSession = \OC::$server->getUserSession(); |
|
212 | - $user = $userSession->getUser(); |
|
213 | - if ($user !== null) { |
|
214 | - $view = \OC\Files\Filesystem::getView(); |
|
215 | - $this->server->addPlugin( |
|
216 | - new FilesPlugin( |
|
217 | - $this->server->tree, |
|
218 | - \OC::$server->getConfig(), |
|
219 | - $this->request, |
|
220 | - \OC::$server->getPreviewManager(), |
|
221 | - false, |
|
222 | - !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
223 | - ) |
|
224 | - ); |
|
208 | + // wait with registering these until auth is handled and the filesystem is setup |
|
209 | + $this->server->on('beforeMethod', function () use ($root, $lazySearchBackend) { |
|
210 | + // custom properties plugin must be the last one |
|
211 | + $userSession = \OC::$server->getUserSession(); |
|
212 | + $user = $userSession->getUser(); |
|
213 | + if ($user !== null) { |
|
214 | + $view = \OC\Files\Filesystem::getView(); |
|
215 | + $this->server->addPlugin( |
|
216 | + new FilesPlugin( |
|
217 | + $this->server->tree, |
|
218 | + \OC::$server->getConfig(), |
|
219 | + $this->request, |
|
220 | + \OC::$server->getPreviewManager(), |
|
221 | + false, |
|
222 | + !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
223 | + ) |
|
224 | + ); |
|
225 | 225 | |
226 | - $this->server->addPlugin( |
|
227 | - new \Sabre\DAV\PropertyStorage\Plugin( |
|
228 | - new CustomPropertiesBackend( |
|
229 | - $this->server->tree, |
|
230 | - \OC::$server->getDatabaseConnection(), |
|
231 | - \OC::$server->getUserSession()->getUser() |
|
232 | - ) |
|
233 | - ) |
|
234 | - ); |
|
235 | - if ($view !== null) { |
|
236 | - $this->server->addPlugin( |
|
237 | - new QuotaPlugin($view, false)); |
|
238 | - } |
|
239 | - $this->server->addPlugin( |
|
240 | - new TagsPlugin( |
|
241 | - $this->server->tree, \OC::$server->getTagManager() |
|
242 | - ) |
|
243 | - ); |
|
244 | - // TODO: switch to LazyUserFolder |
|
245 | - $userFolder = \OC::$server->getUserFolder(); |
|
246 | - $this->server->addPlugin(new SharesPlugin( |
|
247 | - $this->server->tree, |
|
248 | - $userSession, |
|
249 | - $userFolder, |
|
250 | - \OC::$server->getShareManager() |
|
251 | - )); |
|
252 | - $this->server->addPlugin(new CommentPropertiesPlugin( |
|
253 | - \OC::$server->getCommentsManager(), |
|
254 | - $userSession |
|
255 | - )); |
|
256 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
257 | - if ($view !== null) { |
|
258 | - $this->server->addPlugin(new FilesReportPlugin( |
|
259 | - $this->server->tree, |
|
260 | - $view, |
|
261 | - \OC::$server->getSystemTagManager(), |
|
262 | - \OC::$server->getSystemTagObjectMapper(), |
|
263 | - \OC::$server->getTagManager(), |
|
264 | - $userSession, |
|
265 | - \OC::$server->getGroupManager(), |
|
266 | - $userFolder |
|
267 | - )); |
|
268 | - $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend( |
|
269 | - $this->server->tree, |
|
270 | - $user, |
|
271 | - \OC::$server->getRootFolder(), |
|
272 | - \OC::$server->getShareManager(), |
|
273 | - $view |
|
274 | - )); |
|
275 | - } |
|
276 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin( |
|
277 | - \OC::$server->getConfig(), |
|
278 | - \OC::$server->query(BirthdayService::class) |
|
279 | - )); |
|
280 | - } |
|
226 | + $this->server->addPlugin( |
|
227 | + new \Sabre\DAV\PropertyStorage\Plugin( |
|
228 | + new CustomPropertiesBackend( |
|
229 | + $this->server->tree, |
|
230 | + \OC::$server->getDatabaseConnection(), |
|
231 | + \OC::$server->getUserSession()->getUser() |
|
232 | + ) |
|
233 | + ) |
|
234 | + ); |
|
235 | + if ($view !== null) { |
|
236 | + $this->server->addPlugin( |
|
237 | + new QuotaPlugin($view, false)); |
|
238 | + } |
|
239 | + $this->server->addPlugin( |
|
240 | + new TagsPlugin( |
|
241 | + $this->server->tree, \OC::$server->getTagManager() |
|
242 | + ) |
|
243 | + ); |
|
244 | + // TODO: switch to LazyUserFolder |
|
245 | + $userFolder = \OC::$server->getUserFolder(); |
|
246 | + $this->server->addPlugin(new SharesPlugin( |
|
247 | + $this->server->tree, |
|
248 | + $userSession, |
|
249 | + $userFolder, |
|
250 | + \OC::$server->getShareManager() |
|
251 | + )); |
|
252 | + $this->server->addPlugin(new CommentPropertiesPlugin( |
|
253 | + \OC::$server->getCommentsManager(), |
|
254 | + $userSession |
|
255 | + )); |
|
256 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
257 | + if ($view !== null) { |
|
258 | + $this->server->addPlugin(new FilesReportPlugin( |
|
259 | + $this->server->tree, |
|
260 | + $view, |
|
261 | + \OC::$server->getSystemTagManager(), |
|
262 | + \OC::$server->getSystemTagObjectMapper(), |
|
263 | + \OC::$server->getTagManager(), |
|
264 | + $userSession, |
|
265 | + \OC::$server->getGroupManager(), |
|
266 | + $userFolder |
|
267 | + )); |
|
268 | + $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend( |
|
269 | + $this->server->tree, |
|
270 | + $user, |
|
271 | + \OC::$server->getRootFolder(), |
|
272 | + \OC::$server->getShareManager(), |
|
273 | + $view |
|
274 | + )); |
|
275 | + } |
|
276 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin( |
|
277 | + \OC::$server->getConfig(), |
|
278 | + \OC::$server->query(BirthdayService::class) |
|
279 | + )); |
|
280 | + } |
|
281 | 281 | |
282 | - // register plugins from apps |
|
283 | - $pluginManager = new PluginManager( |
|
284 | - \OC::$server, |
|
285 | - \OC::$server->getAppManager() |
|
286 | - ); |
|
287 | - foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
|
288 | - $this->server->addPlugin($appPlugin); |
|
289 | - } |
|
290 | - foreach ($pluginManager->getAppCollections() as $appCollection) { |
|
291 | - $root->addChild($appCollection); |
|
292 | - } |
|
293 | - }); |
|
294 | - } |
|
282 | + // register plugins from apps |
|
283 | + $pluginManager = new PluginManager( |
|
284 | + \OC::$server, |
|
285 | + \OC::$server->getAppManager() |
|
286 | + ); |
|
287 | + foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
|
288 | + $this->server->addPlugin($appPlugin); |
|
289 | + } |
|
290 | + foreach ($pluginManager->getAppCollections() as $appCollection) { |
|
291 | + $root->addChild($appCollection); |
|
292 | + } |
|
293 | + }); |
|
294 | + } |
|
295 | 295 | |
296 | - public function exec() { |
|
297 | - $this->server->exec(); |
|
298 | - } |
|
296 | + public function exec() { |
|
297 | + $this->server->exec(); |
|
298 | + } |
|
299 | 299 | |
300 | - private function requestIsForSubtree(array $subTrees): bool { |
|
301 | - foreach ($subTrees as $subTree) { |
|
302 | - $subTree = trim($subTree, ' /'); |
|
303 | - if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) { |
|
304 | - return true; |
|
305 | - } |
|
306 | - } |
|
307 | - return false; |
|
308 | - } |
|
300 | + private function requestIsForSubtree(array $subTrees): bool { |
|
301 | + foreach ($subTrees as $subTree) { |
|
302 | + $subTree = trim($subTree, ' /'); |
|
303 | + if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) { |
|
304 | + return true; |
|
305 | + } |
|
306 | + } |
|
307 | + return false; |
|
308 | + } |
|
309 | 309 | } |
@@ -32,52 +32,52 @@ |
||
32 | 32 | |
33 | 33 | class RefreshWebcalJobRegistrar implements IRepairStep { |
34 | 34 | |
35 | - /** @var IDBConnection */ |
|
36 | - private $connection; |
|
35 | + /** @var IDBConnection */ |
|
36 | + private $connection; |
|
37 | 37 | |
38 | - /** @var IJobList */ |
|
39 | - private $jobList; |
|
38 | + /** @var IJobList */ |
|
39 | + private $jobList; |
|
40 | 40 | |
41 | - /** |
|
42 | - * FixBirthdayCalendarComponent constructor. |
|
43 | - * |
|
44 | - * @param IDBConnection $connection |
|
45 | - * @param IJobList $jobList |
|
46 | - */ |
|
47 | - public function __construct(IDBConnection $connection, IJobList $jobList) { |
|
48 | - $this->connection = $connection; |
|
49 | - $this->jobList = $jobList; |
|
50 | - } |
|
41 | + /** |
|
42 | + * FixBirthdayCalendarComponent constructor. |
|
43 | + * |
|
44 | + * @param IDBConnection $connection |
|
45 | + * @param IJobList $jobList |
|
46 | + */ |
|
47 | + public function __construct(IDBConnection $connection, IJobList $jobList) { |
|
48 | + $this->connection = $connection; |
|
49 | + $this->jobList = $jobList; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @inheritdoc |
|
54 | - */ |
|
55 | - public function getName() { |
|
56 | - return 'Registering background jobs to update cache for webcal calendars'; |
|
57 | - } |
|
52 | + /** |
|
53 | + * @inheritdoc |
|
54 | + */ |
|
55 | + public function getName() { |
|
56 | + return 'Registering background jobs to update cache for webcal calendars'; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @inheritdoc |
|
61 | - */ |
|
62 | - public function run(IOutput $output) { |
|
63 | - $query = $this->connection->getQueryBuilder(); |
|
64 | - $query->select(['principaluri', 'uri']) |
|
65 | - ->from('calendarsubscriptions'); |
|
66 | - $stmt = $query->execute(); |
|
59 | + /** |
|
60 | + * @inheritdoc |
|
61 | + */ |
|
62 | + public function run(IOutput $output) { |
|
63 | + $query = $this->connection->getQueryBuilder(); |
|
64 | + $query->select(['principaluri', 'uri']) |
|
65 | + ->from('calendarsubscriptions'); |
|
66 | + $stmt = $query->execute(); |
|
67 | 67 | |
68 | - $count = 0; |
|
69 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
70 | - $args = [ |
|
71 | - 'principaluri' => $row['principaluri'], |
|
72 | - 'uri' => $row['uri'], |
|
73 | - ]; |
|
68 | + $count = 0; |
|
69 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
70 | + $args = [ |
|
71 | + 'principaluri' => $row['principaluri'], |
|
72 | + 'uri' => $row['uri'], |
|
73 | + ]; |
|
74 | 74 | |
75 | - if (!$this->jobList->has(RefreshWebcalJob::class, $args)) { |
|
76 | - $this->jobList->add(RefreshWebcalJob::class, $args); |
|
77 | - $count++; |
|
78 | - } |
|
79 | - } |
|
75 | + if (!$this->jobList->has(RefreshWebcalJob::class, $args)) { |
|
76 | + $this->jobList->add(RefreshWebcalJob::class, $args); |
|
77 | + $count++; |
|
78 | + } |
|
79 | + } |
|
80 | 80 | |
81 | - $output->info("Added $count background jobs to update webcal calendars"); |
|
82 | - } |
|
81 | + $output->info("Added $count background jobs to update webcal calendars"); |
|
82 | + } |
|
83 | 83 | } |
@@ -31,78 +31,78 @@ |
||
31 | 31 | |
32 | 32 | class Version1006Date20180628111625 extends SimpleMigrationStep { |
33 | 33 | |
34 | - /** |
|
35 | - * @param IOutput $output |
|
36 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
37 | - * @param array $options |
|
38 | - * @return null|ISchemaWrapper |
|
39 | - */ |
|
40 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
41 | - /** @var ISchemaWrapper $schema */ |
|
42 | - $schema = $schemaClosure(); |
|
34 | + /** |
|
35 | + * @param IOutput $output |
|
36 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
37 | + * @param array $options |
|
38 | + * @return null|ISchemaWrapper |
|
39 | + */ |
|
40 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
41 | + /** @var ISchemaWrapper $schema */ |
|
42 | + $schema = $schemaClosure(); |
|
43 | 43 | |
44 | - if (!$schema->hasTable('calendarsubscrobjects')) { |
|
45 | - $table = $schema->createTable('calendarsubscrobjects'); |
|
46 | - $table->addColumn('id', Type::BIGINT, [ |
|
47 | - 'autoincrement' => true, |
|
48 | - 'notnull' => true, |
|
49 | - 'length' => 11, |
|
50 | - 'unsigned' => true, |
|
51 | - ]); |
|
52 | - $table->addColumn('calendardata', Type::BLOB, [ |
|
53 | - 'notnull' => false, |
|
54 | - ]); |
|
55 | - $table->addColumn('uri', Type::STRING, [ |
|
56 | - 'notnull' => false, |
|
57 | - 'length' => 255, |
|
58 | - ]); |
|
59 | - $table->addColumn('subscriptionid', Type::BIGINT, [ |
|
60 | - 'notnull' => true, |
|
61 | - 'length' => 11, |
|
62 | - 'unsigned' => true, |
|
63 | - ]); |
|
64 | - $table->addColumn('lastmodified', Type::INTEGER, [ |
|
65 | - 'notnull' => false, |
|
66 | - 'length' => 10, |
|
67 | - 'unsigned' => true, |
|
68 | - ]); |
|
69 | - $table->addColumn('etag', Type::STRING, [ |
|
70 | - 'notnull' => false, |
|
71 | - 'length' => 32, |
|
72 | - ]); |
|
73 | - $table->addColumn('size', Type::BIGINT, [ |
|
74 | - 'notnull' => true, |
|
75 | - 'length' => 11, |
|
76 | - 'unsigned' => true, |
|
77 | - ]); |
|
78 | - $table->addColumn('componenttype', Type::STRING, [ |
|
79 | - 'notnull' => false, |
|
80 | - 'length' => 8, |
|
81 | - ]); |
|
82 | - $table->addColumn('firstoccurence', Type::BIGINT, [ |
|
83 | - 'notnull' => false, |
|
84 | - 'length' => 11, |
|
85 | - 'unsigned' => true, |
|
86 | - ]); |
|
87 | - $table->addColumn('lastoccurence', Type::BIGINT, [ |
|
88 | - 'notnull' => false, |
|
89 | - 'length' => 11, |
|
90 | - 'unsigned' => true, |
|
91 | - ]); |
|
92 | - $table->addColumn('uid', Type::STRING, [ |
|
93 | - 'notnull' => false, |
|
94 | - 'length' => 255, |
|
95 | - ]); |
|
96 | - $table->addColumn('classification', Type::INTEGER, [ |
|
97 | - 'notnull' => false, |
|
98 | - 'default' => 0, |
|
99 | - ]); |
|
100 | - $table->setPrimaryKey(['id']); |
|
101 | - $table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index'); |
|
102 | - } |
|
44 | + if (!$schema->hasTable('calendarsubscrobjects')) { |
|
45 | + $table = $schema->createTable('calendarsubscrobjects'); |
|
46 | + $table->addColumn('id', Type::BIGINT, [ |
|
47 | + 'autoincrement' => true, |
|
48 | + 'notnull' => true, |
|
49 | + 'length' => 11, |
|
50 | + 'unsigned' => true, |
|
51 | + ]); |
|
52 | + $table->addColumn('calendardata', Type::BLOB, [ |
|
53 | + 'notnull' => false, |
|
54 | + ]); |
|
55 | + $table->addColumn('uri', Type::STRING, [ |
|
56 | + 'notnull' => false, |
|
57 | + 'length' => 255, |
|
58 | + ]); |
|
59 | + $table->addColumn('subscriptionid', Type::BIGINT, [ |
|
60 | + 'notnull' => true, |
|
61 | + 'length' => 11, |
|
62 | + 'unsigned' => true, |
|
63 | + ]); |
|
64 | + $table->addColumn('lastmodified', Type::INTEGER, [ |
|
65 | + 'notnull' => false, |
|
66 | + 'length' => 10, |
|
67 | + 'unsigned' => true, |
|
68 | + ]); |
|
69 | + $table->addColumn('etag', Type::STRING, [ |
|
70 | + 'notnull' => false, |
|
71 | + 'length' => 32, |
|
72 | + ]); |
|
73 | + $table->addColumn('size', Type::BIGINT, [ |
|
74 | + 'notnull' => true, |
|
75 | + 'length' => 11, |
|
76 | + 'unsigned' => true, |
|
77 | + ]); |
|
78 | + $table->addColumn('componenttype', Type::STRING, [ |
|
79 | + 'notnull' => false, |
|
80 | + 'length' => 8, |
|
81 | + ]); |
|
82 | + $table->addColumn('firstoccurence', Type::BIGINT, [ |
|
83 | + 'notnull' => false, |
|
84 | + 'length' => 11, |
|
85 | + 'unsigned' => true, |
|
86 | + ]); |
|
87 | + $table->addColumn('lastoccurence', Type::BIGINT, [ |
|
88 | + 'notnull' => false, |
|
89 | + 'length' => 11, |
|
90 | + 'unsigned' => true, |
|
91 | + ]); |
|
92 | + $table->addColumn('uid', Type::STRING, [ |
|
93 | + 'notnull' => false, |
|
94 | + 'length' => 255, |
|
95 | + ]); |
|
96 | + $table->addColumn('classification', Type::INTEGER, [ |
|
97 | + 'notnull' => false, |
|
98 | + 'default' => 0, |
|
99 | + ]); |
|
100 | + $table->setPrimaryKey(['id']); |
|
101 | + $table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index'); |
|
102 | + } |
|
103 | 103 | |
104 | - return $schema; |
|
105 | - } |
|
104 | + return $schema; |
|
105 | + } |
|
106 | 106 | } |
107 | 107 | |
108 | 108 |
@@ -73,2517 +73,2517 @@ |
||
73 | 73 | */ |
74 | 74 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
75 | 75 | |
76 | - const PERSONAL_CALENDAR_URI = 'personal'; |
|
77 | - const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
78 | - |
|
79 | - const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
80 | - const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
81 | - |
|
82 | - /** |
|
83 | - * We need to specify a max date, because we need to stop *somewhere* |
|
84 | - * |
|
85 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
86 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
87 | - * in 2038-01-19 to avoid problems when the date is converted |
|
88 | - * to a unix timestamp. |
|
89 | - */ |
|
90 | - const MAX_DATE = '2038-01-01'; |
|
91 | - |
|
92 | - const ACCESS_PUBLIC = 4; |
|
93 | - const CLASSIFICATION_PUBLIC = 0; |
|
94 | - const CLASSIFICATION_PRIVATE = 1; |
|
95 | - const CLASSIFICATION_CONFIDENTIAL = 2; |
|
96 | - |
|
97 | - /** |
|
98 | - * List of CalDAV properties, and how they map to database field names |
|
99 | - * Add your own properties by simply adding on to this array. |
|
100 | - * |
|
101 | - * Note that only string-based properties are supported here. |
|
102 | - * |
|
103 | - * @var array |
|
104 | - */ |
|
105 | - public $propertyMap = [ |
|
106 | - '{DAV:}displayname' => 'displayname', |
|
107 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
108 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
109 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
110 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
111 | - ]; |
|
112 | - |
|
113 | - /** |
|
114 | - * List of subscription properties, and how they map to database field names. |
|
115 | - * |
|
116 | - * @var array |
|
117 | - */ |
|
118 | - public $subscriptionPropertyMap = [ |
|
119 | - '{DAV:}displayname' => 'displayname', |
|
120 | - '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
121 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
122 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
123 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
124 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
125 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
126 | - ]; |
|
127 | - |
|
128 | - /** @var array properties to index */ |
|
129 | - public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
|
130 | - 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
|
131 | - 'ORGANIZER']; |
|
132 | - |
|
133 | - /** @var array parameters to index */ |
|
134 | - public static $indexParameters = [ |
|
135 | - 'ATTENDEE' => ['CN'], |
|
136 | - 'ORGANIZER' => ['CN'], |
|
137 | - ]; |
|
138 | - |
|
139 | - /** |
|
140 | - * @var string[] Map of uid => display name |
|
141 | - */ |
|
142 | - protected $userDisplayNames; |
|
143 | - |
|
144 | - /** @var IDBConnection */ |
|
145 | - private $db; |
|
146 | - |
|
147 | - /** @var Backend */ |
|
148 | - private $sharingBackend; |
|
149 | - |
|
150 | - /** @var Principal */ |
|
151 | - private $principalBackend; |
|
152 | - |
|
153 | - /** @var IUserManager */ |
|
154 | - private $userManager; |
|
155 | - |
|
156 | - /** @var ISecureRandom */ |
|
157 | - private $random; |
|
158 | - |
|
159 | - /** @var ILogger */ |
|
160 | - private $logger; |
|
161 | - |
|
162 | - /** @var EventDispatcherInterface */ |
|
163 | - private $dispatcher; |
|
164 | - |
|
165 | - /** @var bool */ |
|
166 | - private $legacyEndpoint; |
|
167 | - |
|
168 | - /** @var string */ |
|
169 | - private $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
170 | - |
|
171 | - /** |
|
172 | - * CalDavBackend constructor. |
|
173 | - * |
|
174 | - * @param IDBConnection $db |
|
175 | - * @param Principal $principalBackend |
|
176 | - * @param IUserManager $userManager |
|
177 | - * @param IGroupManager $groupManager |
|
178 | - * @param ISecureRandom $random |
|
179 | - * @param ILogger $logger |
|
180 | - * @param EventDispatcherInterface $dispatcher |
|
181 | - * @param bool $legacyEndpoint |
|
182 | - */ |
|
183 | - public function __construct(IDBConnection $db, |
|
184 | - Principal $principalBackend, |
|
185 | - IUserManager $userManager, |
|
186 | - IGroupManager $groupManager, |
|
187 | - ISecureRandom $random, |
|
188 | - ILogger $logger, |
|
189 | - EventDispatcherInterface $dispatcher, |
|
190 | - $legacyEndpoint = false) { |
|
191 | - $this->db = $db; |
|
192 | - $this->principalBackend = $principalBackend; |
|
193 | - $this->userManager = $userManager; |
|
194 | - $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
|
195 | - $this->random = $random; |
|
196 | - $this->logger = $logger; |
|
197 | - $this->dispatcher = $dispatcher; |
|
198 | - $this->legacyEndpoint = $legacyEndpoint; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Return the number of calendars for a principal |
|
203 | - * |
|
204 | - * By default this excludes the automatically generated birthday calendar |
|
205 | - * |
|
206 | - * @param $principalUri |
|
207 | - * @param bool $excludeBirthday |
|
208 | - * @return int |
|
209 | - */ |
|
210 | - public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
211 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
212 | - $query = $this->db->getQueryBuilder(); |
|
213 | - $query->select($query->createFunction('COUNT(*)')) |
|
214 | - ->from('calendars') |
|
215 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
216 | - |
|
217 | - if ($excludeBirthday) { |
|
218 | - $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
219 | - } |
|
220 | - |
|
221 | - return (int)$query->execute()->fetchColumn(); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Returns a list of calendars for a principal. |
|
226 | - * |
|
227 | - * Every project is an array with the following keys: |
|
228 | - * * id, a unique id that will be used by other functions to modify the |
|
229 | - * calendar. This can be the same as the uri or a database key. |
|
230 | - * * uri, which the basename of the uri with which the calendar is |
|
231 | - * accessed. |
|
232 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
233 | - * principalUri passed to this method. |
|
234 | - * |
|
235 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
236 | - * common one is '{DAV:}displayname'. |
|
237 | - * |
|
238 | - * Many clients also require: |
|
239 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
240 | - * For this property, you can just return an instance of |
|
241 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
242 | - * |
|
243 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
244 | - * ACL will automatically be put in read-only mode. |
|
245 | - * |
|
246 | - * @param string $principalUri |
|
247 | - * @return array |
|
248 | - */ |
|
249 | - function getCalendarsForUser($principalUri) { |
|
250 | - $principalUriOriginal = $principalUri; |
|
251 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
252 | - $fields = array_values($this->propertyMap); |
|
253 | - $fields[] = 'id'; |
|
254 | - $fields[] = 'uri'; |
|
255 | - $fields[] = 'synctoken'; |
|
256 | - $fields[] = 'components'; |
|
257 | - $fields[] = 'principaluri'; |
|
258 | - $fields[] = 'transparent'; |
|
259 | - |
|
260 | - // Making fields a comma-delimited list |
|
261 | - $query = $this->db->getQueryBuilder(); |
|
262 | - $query->select($fields)->from('calendars') |
|
263 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
264 | - ->orderBy('calendarorder', 'ASC'); |
|
265 | - $stmt = $query->execute(); |
|
266 | - |
|
267 | - $calendars = []; |
|
268 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
269 | - |
|
270 | - $components = []; |
|
271 | - if ($row['components']) { |
|
272 | - $components = explode(',',$row['components']); |
|
273 | - } |
|
274 | - |
|
275 | - $calendar = [ |
|
276 | - 'id' => $row['id'], |
|
277 | - 'uri' => $row['uri'], |
|
278 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
279 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
280 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
281 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
282 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
283 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
284 | - ]; |
|
285 | - |
|
286 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
287 | - $calendar[$xmlName] = $row[$dbName]; |
|
288 | - } |
|
289 | - |
|
290 | - $this->addOwnerPrincipal($calendar); |
|
291 | - |
|
292 | - if (!isset($calendars[$calendar['id']])) { |
|
293 | - $calendars[$calendar['id']] = $calendar; |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - $stmt->closeCursor(); |
|
298 | - |
|
299 | - // query for shared calendars |
|
300 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
301 | - $principals = array_map(function($principal) { |
|
302 | - return urldecode($principal); |
|
303 | - }, $principals); |
|
304 | - $principals[]= $principalUri; |
|
305 | - |
|
306 | - $fields = array_values($this->propertyMap); |
|
307 | - $fields[] = 'a.id'; |
|
308 | - $fields[] = 'a.uri'; |
|
309 | - $fields[] = 'a.synctoken'; |
|
310 | - $fields[] = 'a.components'; |
|
311 | - $fields[] = 'a.principaluri'; |
|
312 | - $fields[] = 'a.transparent'; |
|
313 | - $fields[] = 's.access'; |
|
314 | - $query = $this->db->getQueryBuilder(); |
|
315 | - $result = $query->select($fields) |
|
316 | - ->from('dav_shares', 's') |
|
317 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
318 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
319 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
320 | - ->setParameter('type', 'calendar') |
|
321 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
322 | - ->execute(); |
|
323 | - |
|
324 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
325 | - while($row = $result->fetch()) { |
|
326 | - if ($row['principaluri'] === $principalUri) { |
|
327 | - continue; |
|
328 | - } |
|
329 | - |
|
330 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
331 | - if (isset($calendars[$row['id']])) { |
|
332 | - if ($readOnly) { |
|
333 | - // New share can not have more permissions then the old one. |
|
334 | - continue; |
|
335 | - } |
|
336 | - if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
337 | - $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
338 | - // Old share is already read-write, no more permissions can be gained |
|
339 | - continue; |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - list(, $name) = Uri\split($row['principaluri']); |
|
344 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
345 | - $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
346 | - $components = []; |
|
347 | - if ($row['components']) { |
|
348 | - $components = explode(',',$row['components']); |
|
349 | - } |
|
350 | - $calendar = [ |
|
351 | - 'id' => $row['id'], |
|
352 | - 'uri' => $uri, |
|
353 | - 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
354 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
355 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
356 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
357 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
358 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
359 | - $readOnlyPropertyName => $readOnly, |
|
360 | - ]; |
|
361 | - |
|
362 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
363 | - $calendar[$xmlName] = $row[$dbName]; |
|
364 | - } |
|
365 | - |
|
366 | - $this->addOwnerPrincipal($calendar); |
|
367 | - |
|
368 | - $calendars[$calendar['id']] = $calendar; |
|
369 | - } |
|
370 | - $result->closeCursor(); |
|
371 | - |
|
372 | - return array_values($calendars); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * @param $principalUri |
|
377 | - * @return array |
|
378 | - */ |
|
379 | - public function getUsersOwnCalendars($principalUri) { |
|
380 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
381 | - $fields = array_values($this->propertyMap); |
|
382 | - $fields[] = 'id'; |
|
383 | - $fields[] = 'uri'; |
|
384 | - $fields[] = 'synctoken'; |
|
385 | - $fields[] = 'components'; |
|
386 | - $fields[] = 'principaluri'; |
|
387 | - $fields[] = 'transparent'; |
|
388 | - // Making fields a comma-delimited list |
|
389 | - $query = $this->db->getQueryBuilder(); |
|
390 | - $query->select($fields)->from('calendars') |
|
391 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
392 | - ->orderBy('calendarorder', 'ASC'); |
|
393 | - $stmt = $query->execute(); |
|
394 | - $calendars = []; |
|
395 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
396 | - $components = []; |
|
397 | - if ($row['components']) { |
|
398 | - $components = explode(',',$row['components']); |
|
399 | - } |
|
400 | - $calendar = [ |
|
401 | - 'id' => $row['id'], |
|
402 | - 'uri' => $row['uri'], |
|
403 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
404 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
405 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
406 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
407 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
408 | - ]; |
|
409 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
410 | - $calendar[$xmlName] = $row[$dbName]; |
|
411 | - } |
|
412 | - |
|
413 | - $this->addOwnerPrincipal($calendar); |
|
414 | - |
|
415 | - if (!isset($calendars[$calendar['id']])) { |
|
416 | - $calendars[$calendar['id']] = $calendar; |
|
417 | - } |
|
418 | - } |
|
419 | - $stmt->closeCursor(); |
|
420 | - return array_values($calendars); |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * @param $uid |
|
426 | - * @return string |
|
427 | - */ |
|
428 | - private function getUserDisplayName($uid) { |
|
429 | - if (!isset($this->userDisplayNames[$uid])) { |
|
430 | - $user = $this->userManager->get($uid); |
|
431 | - |
|
432 | - if ($user instanceof IUser) { |
|
433 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
434 | - } else { |
|
435 | - $this->userDisplayNames[$uid] = $uid; |
|
436 | - } |
|
437 | - } |
|
438 | - |
|
439 | - return $this->userDisplayNames[$uid]; |
|
440 | - } |
|
76 | + const PERSONAL_CALENDAR_URI = 'personal'; |
|
77 | + const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
78 | + |
|
79 | + const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
80 | + const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
81 | + |
|
82 | + /** |
|
83 | + * We need to specify a max date, because we need to stop *somewhere* |
|
84 | + * |
|
85 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
86 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
87 | + * in 2038-01-19 to avoid problems when the date is converted |
|
88 | + * to a unix timestamp. |
|
89 | + */ |
|
90 | + const MAX_DATE = '2038-01-01'; |
|
91 | + |
|
92 | + const ACCESS_PUBLIC = 4; |
|
93 | + const CLASSIFICATION_PUBLIC = 0; |
|
94 | + const CLASSIFICATION_PRIVATE = 1; |
|
95 | + const CLASSIFICATION_CONFIDENTIAL = 2; |
|
96 | + |
|
97 | + /** |
|
98 | + * List of CalDAV properties, and how they map to database field names |
|
99 | + * Add your own properties by simply adding on to this array. |
|
100 | + * |
|
101 | + * Note that only string-based properties are supported here. |
|
102 | + * |
|
103 | + * @var array |
|
104 | + */ |
|
105 | + public $propertyMap = [ |
|
106 | + '{DAV:}displayname' => 'displayname', |
|
107 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
108 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
109 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
110 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
111 | + ]; |
|
112 | + |
|
113 | + /** |
|
114 | + * List of subscription properties, and how they map to database field names. |
|
115 | + * |
|
116 | + * @var array |
|
117 | + */ |
|
118 | + public $subscriptionPropertyMap = [ |
|
119 | + '{DAV:}displayname' => 'displayname', |
|
120 | + '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
121 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
122 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
123 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
124 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
125 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
126 | + ]; |
|
127 | + |
|
128 | + /** @var array properties to index */ |
|
129 | + public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
|
130 | + 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
|
131 | + 'ORGANIZER']; |
|
132 | + |
|
133 | + /** @var array parameters to index */ |
|
134 | + public static $indexParameters = [ |
|
135 | + 'ATTENDEE' => ['CN'], |
|
136 | + 'ORGANIZER' => ['CN'], |
|
137 | + ]; |
|
138 | + |
|
139 | + /** |
|
140 | + * @var string[] Map of uid => display name |
|
141 | + */ |
|
142 | + protected $userDisplayNames; |
|
143 | + |
|
144 | + /** @var IDBConnection */ |
|
145 | + private $db; |
|
146 | + |
|
147 | + /** @var Backend */ |
|
148 | + private $sharingBackend; |
|
149 | + |
|
150 | + /** @var Principal */ |
|
151 | + private $principalBackend; |
|
152 | + |
|
153 | + /** @var IUserManager */ |
|
154 | + private $userManager; |
|
155 | + |
|
156 | + /** @var ISecureRandom */ |
|
157 | + private $random; |
|
158 | + |
|
159 | + /** @var ILogger */ |
|
160 | + private $logger; |
|
161 | + |
|
162 | + /** @var EventDispatcherInterface */ |
|
163 | + private $dispatcher; |
|
164 | + |
|
165 | + /** @var bool */ |
|
166 | + private $legacyEndpoint; |
|
167 | + |
|
168 | + /** @var string */ |
|
169 | + private $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
170 | + |
|
171 | + /** |
|
172 | + * CalDavBackend constructor. |
|
173 | + * |
|
174 | + * @param IDBConnection $db |
|
175 | + * @param Principal $principalBackend |
|
176 | + * @param IUserManager $userManager |
|
177 | + * @param IGroupManager $groupManager |
|
178 | + * @param ISecureRandom $random |
|
179 | + * @param ILogger $logger |
|
180 | + * @param EventDispatcherInterface $dispatcher |
|
181 | + * @param bool $legacyEndpoint |
|
182 | + */ |
|
183 | + public function __construct(IDBConnection $db, |
|
184 | + Principal $principalBackend, |
|
185 | + IUserManager $userManager, |
|
186 | + IGroupManager $groupManager, |
|
187 | + ISecureRandom $random, |
|
188 | + ILogger $logger, |
|
189 | + EventDispatcherInterface $dispatcher, |
|
190 | + $legacyEndpoint = false) { |
|
191 | + $this->db = $db; |
|
192 | + $this->principalBackend = $principalBackend; |
|
193 | + $this->userManager = $userManager; |
|
194 | + $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
|
195 | + $this->random = $random; |
|
196 | + $this->logger = $logger; |
|
197 | + $this->dispatcher = $dispatcher; |
|
198 | + $this->legacyEndpoint = $legacyEndpoint; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Return the number of calendars for a principal |
|
203 | + * |
|
204 | + * By default this excludes the automatically generated birthday calendar |
|
205 | + * |
|
206 | + * @param $principalUri |
|
207 | + * @param bool $excludeBirthday |
|
208 | + * @return int |
|
209 | + */ |
|
210 | + public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
211 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
212 | + $query = $this->db->getQueryBuilder(); |
|
213 | + $query->select($query->createFunction('COUNT(*)')) |
|
214 | + ->from('calendars') |
|
215 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
216 | + |
|
217 | + if ($excludeBirthday) { |
|
218 | + $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
219 | + } |
|
220 | + |
|
221 | + return (int)$query->execute()->fetchColumn(); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Returns a list of calendars for a principal. |
|
226 | + * |
|
227 | + * Every project is an array with the following keys: |
|
228 | + * * id, a unique id that will be used by other functions to modify the |
|
229 | + * calendar. This can be the same as the uri or a database key. |
|
230 | + * * uri, which the basename of the uri with which the calendar is |
|
231 | + * accessed. |
|
232 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
233 | + * principalUri passed to this method. |
|
234 | + * |
|
235 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
236 | + * common one is '{DAV:}displayname'. |
|
237 | + * |
|
238 | + * Many clients also require: |
|
239 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
240 | + * For this property, you can just return an instance of |
|
241 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
242 | + * |
|
243 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
244 | + * ACL will automatically be put in read-only mode. |
|
245 | + * |
|
246 | + * @param string $principalUri |
|
247 | + * @return array |
|
248 | + */ |
|
249 | + function getCalendarsForUser($principalUri) { |
|
250 | + $principalUriOriginal = $principalUri; |
|
251 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
252 | + $fields = array_values($this->propertyMap); |
|
253 | + $fields[] = 'id'; |
|
254 | + $fields[] = 'uri'; |
|
255 | + $fields[] = 'synctoken'; |
|
256 | + $fields[] = 'components'; |
|
257 | + $fields[] = 'principaluri'; |
|
258 | + $fields[] = 'transparent'; |
|
259 | + |
|
260 | + // Making fields a comma-delimited list |
|
261 | + $query = $this->db->getQueryBuilder(); |
|
262 | + $query->select($fields)->from('calendars') |
|
263 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
264 | + ->orderBy('calendarorder', 'ASC'); |
|
265 | + $stmt = $query->execute(); |
|
266 | + |
|
267 | + $calendars = []; |
|
268 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
269 | + |
|
270 | + $components = []; |
|
271 | + if ($row['components']) { |
|
272 | + $components = explode(',',$row['components']); |
|
273 | + } |
|
274 | + |
|
275 | + $calendar = [ |
|
276 | + 'id' => $row['id'], |
|
277 | + 'uri' => $row['uri'], |
|
278 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
279 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
280 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
281 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
282 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
283 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
284 | + ]; |
|
285 | + |
|
286 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
287 | + $calendar[$xmlName] = $row[$dbName]; |
|
288 | + } |
|
289 | + |
|
290 | + $this->addOwnerPrincipal($calendar); |
|
291 | + |
|
292 | + if (!isset($calendars[$calendar['id']])) { |
|
293 | + $calendars[$calendar['id']] = $calendar; |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + $stmt->closeCursor(); |
|
298 | + |
|
299 | + // query for shared calendars |
|
300 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
301 | + $principals = array_map(function($principal) { |
|
302 | + return urldecode($principal); |
|
303 | + }, $principals); |
|
304 | + $principals[]= $principalUri; |
|
305 | + |
|
306 | + $fields = array_values($this->propertyMap); |
|
307 | + $fields[] = 'a.id'; |
|
308 | + $fields[] = 'a.uri'; |
|
309 | + $fields[] = 'a.synctoken'; |
|
310 | + $fields[] = 'a.components'; |
|
311 | + $fields[] = 'a.principaluri'; |
|
312 | + $fields[] = 'a.transparent'; |
|
313 | + $fields[] = 's.access'; |
|
314 | + $query = $this->db->getQueryBuilder(); |
|
315 | + $result = $query->select($fields) |
|
316 | + ->from('dav_shares', 's') |
|
317 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
318 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
319 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
320 | + ->setParameter('type', 'calendar') |
|
321 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
322 | + ->execute(); |
|
323 | + |
|
324 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
325 | + while($row = $result->fetch()) { |
|
326 | + if ($row['principaluri'] === $principalUri) { |
|
327 | + continue; |
|
328 | + } |
|
329 | + |
|
330 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
331 | + if (isset($calendars[$row['id']])) { |
|
332 | + if ($readOnly) { |
|
333 | + // New share can not have more permissions then the old one. |
|
334 | + continue; |
|
335 | + } |
|
336 | + if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
337 | + $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
338 | + // Old share is already read-write, no more permissions can be gained |
|
339 | + continue; |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + list(, $name) = Uri\split($row['principaluri']); |
|
344 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
345 | + $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
346 | + $components = []; |
|
347 | + if ($row['components']) { |
|
348 | + $components = explode(',',$row['components']); |
|
349 | + } |
|
350 | + $calendar = [ |
|
351 | + 'id' => $row['id'], |
|
352 | + 'uri' => $uri, |
|
353 | + 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
354 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
355 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
356 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
357 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
358 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
359 | + $readOnlyPropertyName => $readOnly, |
|
360 | + ]; |
|
361 | + |
|
362 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
363 | + $calendar[$xmlName] = $row[$dbName]; |
|
364 | + } |
|
365 | + |
|
366 | + $this->addOwnerPrincipal($calendar); |
|
367 | + |
|
368 | + $calendars[$calendar['id']] = $calendar; |
|
369 | + } |
|
370 | + $result->closeCursor(); |
|
371 | + |
|
372 | + return array_values($calendars); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * @param $principalUri |
|
377 | + * @return array |
|
378 | + */ |
|
379 | + public function getUsersOwnCalendars($principalUri) { |
|
380 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
381 | + $fields = array_values($this->propertyMap); |
|
382 | + $fields[] = 'id'; |
|
383 | + $fields[] = 'uri'; |
|
384 | + $fields[] = 'synctoken'; |
|
385 | + $fields[] = 'components'; |
|
386 | + $fields[] = 'principaluri'; |
|
387 | + $fields[] = 'transparent'; |
|
388 | + // Making fields a comma-delimited list |
|
389 | + $query = $this->db->getQueryBuilder(); |
|
390 | + $query->select($fields)->from('calendars') |
|
391 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
392 | + ->orderBy('calendarorder', 'ASC'); |
|
393 | + $stmt = $query->execute(); |
|
394 | + $calendars = []; |
|
395 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
396 | + $components = []; |
|
397 | + if ($row['components']) { |
|
398 | + $components = explode(',',$row['components']); |
|
399 | + } |
|
400 | + $calendar = [ |
|
401 | + 'id' => $row['id'], |
|
402 | + 'uri' => $row['uri'], |
|
403 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
404 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
405 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
406 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
407 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
408 | + ]; |
|
409 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
410 | + $calendar[$xmlName] = $row[$dbName]; |
|
411 | + } |
|
412 | + |
|
413 | + $this->addOwnerPrincipal($calendar); |
|
414 | + |
|
415 | + if (!isset($calendars[$calendar['id']])) { |
|
416 | + $calendars[$calendar['id']] = $calendar; |
|
417 | + } |
|
418 | + } |
|
419 | + $stmt->closeCursor(); |
|
420 | + return array_values($calendars); |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * @param $uid |
|
426 | + * @return string |
|
427 | + */ |
|
428 | + private function getUserDisplayName($uid) { |
|
429 | + if (!isset($this->userDisplayNames[$uid])) { |
|
430 | + $user = $this->userManager->get($uid); |
|
431 | + |
|
432 | + if ($user instanceof IUser) { |
|
433 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
434 | + } else { |
|
435 | + $this->userDisplayNames[$uid] = $uid; |
|
436 | + } |
|
437 | + } |
|
438 | + |
|
439 | + return $this->userDisplayNames[$uid]; |
|
440 | + } |
|
441 | 441 | |
442 | - /** |
|
443 | - * @return array |
|
444 | - */ |
|
445 | - public function getPublicCalendars() { |
|
446 | - $fields = array_values($this->propertyMap); |
|
447 | - $fields[] = 'a.id'; |
|
448 | - $fields[] = 'a.uri'; |
|
449 | - $fields[] = 'a.synctoken'; |
|
450 | - $fields[] = 'a.components'; |
|
451 | - $fields[] = 'a.principaluri'; |
|
452 | - $fields[] = 'a.transparent'; |
|
453 | - $fields[] = 's.access'; |
|
454 | - $fields[] = 's.publicuri'; |
|
455 | - $calendars = []; |
|
456 | - $query = $this->db->getQueryBuilder(); |
|
457 | - $result = $query->select($fields) |
|
458 | - ->from('dav_shares', 's') |
|
459 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
460 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
461 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
462 | - ->execute(); |
|
463 | - |
|
464 | - while($row = $result->fetch()) { |
|
465 | - list(, $name) = Uri\split($row['principaluri']); |
|
466 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
467 | - $components = []; |
|
468 | - if ($row['components']) { |
|
469 | - $components = explode(',',$row['components']); |
|
470 | - } |
|
471 | - $calendar = [ |
|
472 | - 'id' => $row['id'], |
|
473 | - 'uri' => $row['publicuri'], |
|
474 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
475 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
476 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
477 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
478 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
479 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
480 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
481 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
482 | - ]; |
|
483 | - |
|
484 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
485 | - $calendar[$xmlName] = $row[$dbName]; |
|
486 | - } |
|
487 | - |
|
488 | - $this->addOwnerPrincipal($calendar); |
|
489 | - |
|
490 | - if (!isset($calendars[$calendar['id']])) { |
|
491 | - $calendars[$calendar['id']] = $calendar; |
|
492 | - } |
|
493 | - } |
|
494 | - $result->closeCursor(); |
|
495 | - |
|
496 | - return array_values($calendars); |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * @param string $uri |
|
501 | - * @return array |
|
502 | - * @throws NotFound |
|
503 | - */ |
|
504 | - public function getPublicCalendar($uri) { |
|
505 | - $fields = array_values($this->propertyMap); |
|
506 | - $fields[] = 'a.id'; |
|
507 | - $fields[] = 'a.uri'; |
|
508 | - $fields[] = 'a.synctoken'; |
|
509 | - $fields[] = 'a.components'; |
|
510 | - $fields[] = 'a.principaluri'; |
|
511 | - $fields[] = 'a.transparent'; |
|
512 | - $fields[] = 's.access'; |
|
513 | - $fields[] = 's.publicuri'; |
|
514 | - $query = $this->db->getQueryBuilder(); |
|
515 | - $result = $query->select($fields) |
|
516 | - ->from('dav_shares', 's') |
|
517 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
518 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
519 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
520 | - ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
521 | - ->execute(); |
|
522 | - |
|
523 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
524 | - |
|
525 | - $result->closeCursor(); |
|
526 | - |
|
527 | - if ($row === false) { |
|
528 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
529 | - } |
|
530 | - |
|
531 | - list(, $name) = Uri\split($row['principaluri']); |
|
532 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
533 | - $components = []; |
|
534 | - if ($row['components']) { |
|
535 | - $components = explode(',',$row['components']); |
|
536 | - } |
|
537 | - $calendar = [ |
|
538 | - 'id' => $row['id'], |
|
539 | - 'uri' => $row['publicuri'], |
|
540 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
541 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
542 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
543 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
544 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
545 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
546 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
547 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
548 | - ]; |
|
549 | - |
|
550 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
551 | - $calendar[$xmlName] = $row[$dbName]; |
|
552 | - } |
|
553 | - |
|
554 | - $this->addOwnerPrincipal($calendar); |
|
555 | - |
|
556 | - return $calendar; |
|
557 | - |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * @param string $principal |
|
562 | - * @param string $uri |
|
563 | - * @return array|null |
|
564 | - */ |
|
565 | - public function getCalendarByUri($principal, $uri) { |
|
566 | - $fields = array_values($this->propertyMap); |
|
567 | - $fields[] = 'id'; |
|
568 | - $fields[] = 'uri'; |
|
569 | - $fields[] = 'synctoken'; |
|
570 | - $fields[] = 'components'; |
|
571 | - $fields[] = 'principaluri'; |
|
572 | - $fields[] = 'transparent'; |
|
573 | - |
|
574 | - // Making fields a comma-delimited list |
|
575 | - $query = $this->db->getQueryBuilder(); |
|
576 | - $query->select($fields)->from('calendars') |
|
577 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
578 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
579 | - ->setMaxResults(1); |
|
580 | - $stmt = $query->execute(); |
|
581 | - |
|
582 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
583 | - $stmt->closeCursor(); |
|
584 | - if ($row === false) { |
|
585 | - return null; |
|
586 | - } |
|
587 | - |
|
588 | - $components = []; |
|
589 | - if ($row['components']) { |
|
590 | - $components = explode(',',$row['components']); |
|
591 | - } |
|
592 | - |
|
593 | - $calendar = [ |
|
594 | - 'id' => $row['id'], |
|
595 | - 'uri' => $row['uri'], |
|
596 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
597 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
598 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
599 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
600 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
601 | - ]; |
|
602 | - |
|
603 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
604 | - $calendar[$xmlName] = $row[$dbName]; |
|
605 | - } |
|
606 | - |
|
607 | - $this->addOwnerPrincipal($calendar); |
|
608 | - |
|
609 | - return $calendar; |
|
610 | - } |
|
611 | - |
|
612 | - /** |
|
613 | - * @param $calendarId |
|
614 | - * @return array|null |
|
615 | - */ |
|
616 | - public function getCalendarById($calendarId) { |
|
617 | - $fields = array_values($this->propertyMap); |
|
618 | - $fields[] = 'id'; |
|
619 | - $fields[] = 'uri'; |
|
620 | - $fields[] = 'synctoken'; |
|
621 | - $fields[] = 'components'; |
|
622 | - $fields[] = 'principaluri'; |
|
623 | - $fields[] = 'transparent'; |
|
624 | - |
|
625 | - // Making fields a comma-delimited list |
|
626 | - $query = $this->db->getQueryBuilder(); |
|
627 | - $query->select($fields)->from('calendars') |
|
628 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
629 | - ->setMaxResults(1); |
|
630 | - $stmt = $query->execute(); |
|
631 | - |
|
632 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
633 | - $stmt->closeCursor(); |
|
634 | - if ($row === false) { |
|
635 | - return null; |
|
636 | - } |
|
637 | - |
|
638 | - $components = []; |
|
639 | - if ($row['components']) { |
|
640 | - $components = explode(',',$row['components']); |
|
641 | - } |
|
642 | - |
|
643 | - $calendar = [ |
|
644 | - 'id' => $row['id'], |
|
645 | - 'uri' => $row['uri'], |
|
646 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
647 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
648 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
649 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
650 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
651 | - ]; |
|
652 | - |
|
653 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
654 | - $calendar[$xmlName] = $row[$dbName]; |
|
655 | - } |
|
656 | - |
|
657 | - $this->addOwnerPrincipal($calendar); |
|
658 | - |
|
659 | - return $calendar; |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * @param $subscriptionId |
|
664 | - */ |
|
665 | - public function getSubscriptionById($subscriptionId) { |
|
666 | - $fields = array_values($this->subscriptionPropertyMap); |
|
667 | - $fields[] = 'id'; |
|
668 | - $fields[] = 'uri'; |
|
669 | - $fields[] = 'source'; |
|
670 | - $fields[] = 'principaluri'; |
|
671 | - $fields[] = 'lastmodified'; |
|
672 | - |
|
673 | - $query = $this->db->getQueryBuilder(); |
|
674 | - $query->select($fields) |
|
675 | - ->from('calendarsubscriptions') |
|
676 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
677 | - ->orderBy('calendarorder', 'asc'); |
|
678 | - $stmt =$query->execute(); |
|
679 | - |
|
680 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
681 | - $stmt->closeCursor(); |
|
682 | - if ($row === false) { |
|
683 | - return null; |
|
684 | - } |
|
685 | - |
|
686 | - $subscription = [ |
|
687 | - 'id' => $row['id'], |
|
688 | - 'uri' => $row['uri'], |
|
689 | - 'principaluri' => $row['principaluri'], |
|
690 | - 'source' => $row['source'], |
|
691 | - 'lastmodified' => $row['lastmodified'], |
|
692 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
693 | - ]; |
|
694 | - |
|
695 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
696 | - if (!is_null($row[$dbName])) { |
|
697 | - $subscription[$xmlName] = $row[$dbName]; |
|
698 | - } |
|
699 | - } |
|
700 | - |
|
701 | - return $subscription; |
|
702 | - } |
|
703 | - |
|
704 | - /** |
|
705 | - * Creates a new calendar for a principal. |
|
706 | - * |
|
707 | - * If the creation was a success, an id must be returned that can be used to reference |
|
708 | - * this calendar in other methods, such as updateCalendar. |
|
709 | - * |
|
710 | - * @param string $principalUri |
|
711 | - * @param string $calendarUri |
|
712 | - * @param array $properties |
|
713 | - * @return int |
|
714 | - * @suppress SqlInjectionChecker |
|
715 | - */ |
|
716 | - function createCalendar($principalUri, $calendarUri, array $properties) { |
|
717 | - $values = [ |
|
718 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
719 | - 'uri' => $calendarUri, |
|
720 | - 'synctoken' => 1, |
|
721 | - 'transparent' => 0, |
|
722 | - 'components' => 'VEVENT,VTODO', |
|
723 | - 'displayname' => $calendarUri |
|
724 | - ]; |
|
725 | - |
|
726 | - // Default value |
|
727 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
728 | - if (isset($properties[$sccs])) { |
|
729 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
730 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
731 | - } |
|
732 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
733 | - } |
|
734 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
735 | - if (isset($properties[$transp])) { |
|
736 | - $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
737 | - } |
|
738 | - |
|
739 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
740 | - if (isset($properties[$xmlName])) { |
|
741 | - $values[$dbName] = $properties[$xmlName]; |
|
742 | - } |
|
743 | - } |
|
744 | - |
|
745 | - $query = $this->db->getQueryBuilder(); |
|
746 | - $query->insert('calendars'); |
|
747 | - foreach($values as $column => $value) { |
|
748 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
749 | - } |
|
750 | - $query->execute(); |
|
751 | - $calendarId = $query->getLastInsertId(); |
|
752 | - |
|
753 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( |
|
754 | - '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', |
|
755 | - [ |
|
756 | - 'calendarId' => $calendarId, |
|
757 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
758 | - ])); |
|
759 | - |
|
760 | - return $calendarId; |
|
761 | - } |
|
762 | - |
|
763 | - /** |
|
764 | - * Updates properties for a calendar. |
|
765 | - * |
|
766 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
767 | - * To do the actual updates, you must tell this object which properties |
|
768 | - * you're going to process with the handle() method. |
|
769 | - * |
|
770 | - * Calling the handle method is like telling the PropPatch object "I |
|
771 | - * promise I can handle updating this property". |
|
772 | - * |
|
773 | - * Read the PropPatch documentation for more info and examples. |
|
774 | - * |
|
775 | - * @param mixed $calendarId |
|
776 | - * @param PropPatch $propPatch |
|
777 | - * @return void |
|
778 | - */ |
|
779 | - function updateCalendar($calendarId, PropPatch $propPatch) { |
|
780 | - $supportedProperties = array_keys($this->propertyMap); |
|
781 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
782 | - |
|
783 | - /** |
|
784 | - * @suppress SqlInjectionChecker |
|
785 | - */ |
|
786 | - $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
787 | - $newValues = []; |
|
788 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
789 | - |
|
790 | - switch ($propertyName) { |
|
791 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
792 | - $fieldName = 'transparent'; |
|
793 | - $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
794 | - break; |
|
795 | - default : |
|
796 | - $fieldName = $this->propertyMap[$propertyName]; |
|
797 | - $newValues[$fieldName] = $propertyValue; |
|
798 | - break; |
|
799 | - } |
|
800 | - |
|
801 | - } |
|
802 | - $query = $this->db->getQueryBuilder(); |
|
803 | - $query->update('calendars'); |
|
804 | - foreach ($newValues as $fieldName => $value) { |
|
805 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
806 | - } |
|
807 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
808 | - $query->execute(); |
|
809 | - |
|
810 | - $this->addChange($calendarId, "", 2); |
|
811 | - |
|
812 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( |
|
813 | - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', |
|
814 | - [ |
|
815 | - 'calendarId' => $calendarId, |
|
816 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
817 | - 'shares' => $this->getShares($calendarId), |
|
818 | - 'propertyMutations' => $mutations, |
|
819 | - ])); |
|
820 | - |
|
821 | - return true; |
|
822 | - }); |
|
823 | - } |
|
824 | - |
|
825 | - /** |
|
826 | - * Delete a calendar and all it's objects |
|
827 | - * |
|
828 | - * @param mixed $calendarId |
|
829 | - * @return void |
|
830 | - */ |
|
831 | - function deleteCalendar($calendarId) { |
|
832 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent( |
|
833 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', |
|
834 | - [ |
|
835 | - 'calendarId' => $calendarId, |
|
836 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
837 | - 'shares' => $this->getShares($calendarId), |
|
838 | - ])); |
|
839 | - |
|
840 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
841 | - $stmt->execute([$calendarId]); |
|
842 | - |
|
843 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
844 | - $stmt->execute([$calendarId]); |
|
845 | - |
|
846 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
847 | - $stmt->execute([$calendarId]); |
|
848 | - |
|
849 | - $this->sharingBackend->deleteAllShares($calendarId); |
|
850 | - |
|
851 | - $query = $this->db->getQueryBuilder(); |
|
852 | - $query->delete($this->dbObjectPropertiesTable) |
|
853 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
854 | - ->execute(); |
|
855 | - } |
|
856 | - |
|
857 | - /** |
|
858 | - * Delete all of an user's shares |
|
859 | - * |
|
860 | - * @param string $principaluri |
|
861 | - * @return void |
|
862 | - */ |
|
863 | - function deleteAllSharesByUser($principaluri) { |
|
864 | - $this->sharingBackend->deleteAllSharesByUser($principaluri); |
|
865 | - } |
|
866 | - |
|
867 | - /** |
|
868 | - * Returns all calendar objects within a calendar. |
|
869 | - * |
|
870 | - * Every item contains an array with the following keys: |
|
871 | - * * calendardata - The iCalendar-compatible calendar data |
|
872 | - * * uri - a unique key which will be used to construct the uri. This can |
|
873 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
874 | - * good idea. This is only the basename, or filename, not the full |
|
875 | - * path. |
|
876 | - * * lastmodified - a timestamp of the last modification time |
|
877 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
878 | - * '"abcdef"') |
|
879 | - * * size - The size of the calendar objects, in bytes. |
|
880 | - * * component - optional, a string containing the type of object, such |
|
881 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
882 | - * the Content-Type header. |
|
883 | - * |
|
884 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
885 | - * speed reasons. |
|
886 | - * |
|
887 | - * The calendardata is also optional. If it's not returned |
|
888 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
889 | - * calendardata. |
|
890 | - * |
|
891 | - * If neither etag or size are specified, the calendardata will be |
|
892 | - * used/fetched to determine these numbers. If both are specified the |
|
893 | - * amount of times this is needed is reduced by a great degree. |
|
894 | - * |
|
895 | - * @param mixed $calendarId |
|
896 | - * @return array |
|
897 | - */ |
|
898 | - function getCalendarObjects($calendarId):array { |
|
899 | - return $this->commonGetCalendarObjects('calendarobjects', 'calendarid', $calendarId); |
|
900 | - } |
|
901 | - |
|
902 | - /** |
|
903 | - * @param mixed $subscriptionId |
|
904 | - * @return array |
|
905 | - */ |
|
906 | - public function getCachedCalendarObjects($subscriptionId):array { |
|
907 | - return $this->commonGetCalendarObjects('calendarsubscrobjects', 'subscriptionid', $subscriptionId); |
|
908 | - } |
|
909 | - |
|
910 | - /** |
|
911 | - * @param string $tablename |
|
912 | - * @param string $selector |
|
913 | - * @param mixed $id |
|
914 | - * @return array |
|
915 | - */ |
|
916 | - private function commonGetCalendarObjects(string $tablename, string $selector, $id):array { |
|
917 | - $query = $this->db->getQueryBuilder(); |
|
918 | - $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'componenttype', 'classification']) |
|
919 | - ->from($tablename) |
|
920 | - ->where($query->expr()->eq($selector, $query->createNamedParameter($id))); |
|
921 | - $stmt = $query->execute(); |
|
922 | - |
|
923 | - $result = []; |
|
924 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
925 | - $result[] = [ |
|
926 | - 'id' => $row['id'], |
|
927 | - 'uri' => $row['uri'], |
|
928 | - 'lastmodified' => $row['lastmodified'], |
|
929 | - 'etag' => '"' . $row['etag'] . '"', |
|
930 | - 'calendarid' => $row[$selector], |
|
931 | - 'size' => (int)$row['size'], |
|
932 | - 'component' => strtolower($row['componenttype']), |
|
933 | - 'classification'=> (int)$row['classification'] |
|
934 | - ]; |
|
935 | - } |
|
936 | - |
|
937 | - return $result; |
|
938 | - } |
|
939 | - |
|
940 | - /** |
|
941 | - * Returns information from a single calendar object, based on it's object |
|
942 | - * uri. |
|
943 | - * |
|
944 | - * The object uri is only the basename, or filename and not a full path. |
|
945 | - * |
|
946 | - * The returned array must have the same keys as getCalendarObjects. The |
|
947 | - * 'calendardata' object is required here though, while it's not required |
|
948 | - * for getCalendarObjects. |
|
949 | - * |
|
950 | - * This method must return null if the object did not exist. |
|
951 | - * |
|
952 | - * @param mixed $calendarId |
|
953 | - * @param string $objectUri |
|
954 | - * @return array|null |
|
955 | - */ |
|
956 | - function getCalendarObject($calendarId, $objectUri) { |
|
957 | - return $this->commonGetCalendarObject('calendarobjects', 'calendarid', $calendarId, $objectUri); |
|
958 | - } |
|
959 | - |
|
960 | - /** |
|
961 | - * @param $subscriptionId |
|
962 | - * @param $objectUri |
|
963 | - * @return array |
|
964 | - */ |
|
965 | - public function getCachedCalendarObject($subscriptionId, $objectUri) { |
|
966 | - return $this->commonGetCalendarObject('calendarsubscrobjects', 'subscriptionid', $subscriptionId, $objectUri); |
|
967 | - } |
|
968 | - |
|
969 | - /** |
|
970 | - * @param string $tablename |
|
971 | - * @param string $selector |
|
972 | - * @param $id |
|
973 | - * @param $objectUri |
|
974 | - * @return array |
|
975 | - */ |
|
976 | - private function commonGetCalendarObject(string $tablename, string $selector, $id, $objectUri):array { |
|
977 | - $query = $this->db->getQueryBuilder(); |
|
978 | - $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'calendardata', 'componenttype', 'classification']) |
|
979 | - ->from($tablename) |
|
980 | - ->where($query->expr()->eq($selector, $query->createNamedParameter($id))) |
|
981 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
982 | - $stmt = $query->execute(); |
|
983 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
984 | - |
|
985 | - if(!$row) { |
|
986 | - return null; |
|
987 | - } |
|
988 | - |
|
989 | - return [ |
|
990 | - 'id' => $row['id'], |
|
991 | - 'uri' => $row['uri'], |
|
992 | - 'lastmodified' => $row['lastmodified'], |
|
993 | - 'etag' => '"' . $row['etag'] . '"', |
|
994 | - 'calendarid' => $row[$selector], |
|
995 | - 'size' => (int)$row['size'], |
|
996 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
997 | - 'component' => strtolower($row['componenttype']), |
|
998 | - 'classification'=> (int)$row['classification'] |
|
999 | - ]; |
|
1000 | - } |
|
1001 | - |
|
1002 | - /** |
|
1003 | - * Returns a list of calendar objects. |
|
1004 | - * |
|
1005 | - * This method should work identical to getCalendarObject, but instead |
|
1006 | - * return all the calendar objects in the list as an array. |
|
1007 | - * |
|
1008 | - * If the backend supports this, it may allow for some speed-ups. |
|
1009 | - * |
|
1010 | - * @param mixed $calendarId |
|
1011 | - * @param string[] $uris |
|
1012 | - * @return array |
|
1013 | - */ |
|
1014 | - function getMultipleCalendarObjects($calendarId, array $uris) { |
|
1015 | - return $this->commonGetMultipleCachedCalendarObjects('calendarobjects', 'calendarid', $calendarId, $uris); |
|
1016 | - } |
|
1017 | - |
|
1018 | - /** |
|
1019 | - * @param $subscripionId |
|
1020 | - * @param array $uris |
|
1021 | - * @return array |
|
1022 | - */ |
|
1023 | - function getMultipleCachedCalendarObjects($subscripionId, array $uris) { |
|
1024 | - return $this->commonGetMultipleCachedCalendarObjects('calendarsubscrobjects', 'subscriptionid', $subscripionId, $uris); |
|
1025 | - } |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * @param string $tablename |
|
1029 | - * @param string $selector |
|
1030 | - * @param $id |
|
1031 | - * @param array $uris |
|
1032 | - * @return array |
|
1033 | - */ |
|
1034 | - private function commonGetMultipleCachedCalendarObjects(string $tablename, string $selector, $id, array $uris) { |
|
1035 | - if (empty($uris)) { |
|
1036 | - return []; |
|
1037 | - } |
|
1038 | - |
|
1039 | - $chunks = array_chunk($uris, 100); |
|
1040 | - $objects = []; |
|
1041 | - |
|
1042 | - $query = $this->db->getQueryBuilder(); |
|
1043 | - $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'calendardata', 'componenttype', 'classification']) |
|
1044 | - ->from($tablename) |
|
1045 | - ->where($query->expr()->eq($selector, $query->createNamedParameter($id))) |
|
1046 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
1047 | - |
|
1048 | - foreach ($chunks as $uris) { |
|
1049 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
1050 | - $result = $query->execute(); |
|
1051 | - |
|
1052 | - while ($row = $result->fetch()) { |
|
1053 | - $objects[] = [ |
|
1054 | - 'id' => $row['id'], |
|
1055 | - 'uri' => $row['uri'], |
|
1056 | - 'lastmodified' => $row['lastmodified'], |
|
1057 | - 'etag' => '"' . $row['etag'] . '"', |
|
1058 | - 'calendarid' => $row[$selector], |
|
1059 | - 'size' => (int)$row['size'], |
|
1060 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
1061 | - 'component' => strtolower($row['componenttype']), |
|
1062 | - 'classification' => (int)$row['classification'] |
|
1063 | - ]; |
|
1064 | - } |
|
1065 | - $result->closeCursor(); |
|
1066 | - } |
|
1067 | - |
|
1068 | - return $objects; |
|
1069 | - } |
|
1070 | - |
|
1071 | - /** |
|
1072 | - * Creates a new calendar object. |
|
1073 | - * |
|
1074 | - * The object uri is only the basename, or filename and not a full path. |
|
1075 | - * |
|
1076 | - * It is possible return an etag from this function, which will be used in |
|
1077 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
1078 | - * by double-quotes. |
|
1079 | - * |
|
1080 | - * However, you should only really return this ETag if you don't mangle the |
|
1081 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
1082 | - * the exact same as this request body, you should omit the ETag. |
|
1083 | - * |
|
1084 | - * @param mixed $calendarId |
|
1085 | - * @param string $objectUri |
|
1086 | - * @param string $calendarData |
|
1087 | - * @return string |
|
1088 | - */ |
|
1089 | - function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
1090 | - $extraData = $this->getDenormalizedData($calendarData); |
|
1091 | - |
|
1092 | - $q = $this->db->getQueryBuilder(); |
|
1093 | - $q->select($q->createFunction('COUNT(*)')) |
|
1094 | - ->from('calendarobjects') |
|
1095 | - ->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId))) |
|
1096 | - ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid']))); |
|
1097 | - |
|
1098 | - $result = $q->execute(); |
|
1099 | - $count = (int) $result->fetchColumn(); |
|
1100 | - $result->closeCursor(); |
|
1101 | - |
|
1102 | - if ($count !== 0) { |
|
1103 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
1104 | - } |
|
1105 | - |
|
1106 | - $query = $this->db->getQueryBuilder(); |
|
1107 | - $query->insert('calendarobjects') |
|
1108 | - ->values([ |
|
1109 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
1110 | - 'uri' => $query->createNamedParameter($objectUri), |
|
1111 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
1112 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
1113 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
1114 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
1115 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
1116 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
1117 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
1118 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
1119 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
1120 | - ]) |
|
1121 | - ->execute(); |
|
1122 | - |
|
1123 | - $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
1124 | - |
|
1125 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent( |
|
1126 | - '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', |
|
1127 | - [ |
|
1128 | - 'calendarId' => $calendarId, |
|
1129 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1130 | - 'shares' => $this->getShares($calendarId), |
|
1131 | - 'objectData' => $this->getCalendarObject($calendarId, $objectUri), |
|
1132 | - ] |
|
1133 | - )); |
|
1134 | - $this->addChange($calendarId, $objectUri, 1); |
|
1135 | - |
|
1136 | - return '"' . $extraData['etag'] . '"'; |
|
1137 | - } |
|
1138 | - |
|
1139 | - /** |
|
1140 | - * Updates an existing calendarobject, based on it's uri. |
|
1141 | - * |
|
1142 | - * The object uri is only the basename, or filename and not a full path. |
|
1143 | - * |
|
1144 | - * It is possible return an etag from this function, which will be used in |
|
1145 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
1146 | - * by double-quotes. |
|
1147 | - * |
|
1148 | - * However, you should only really return this ETag if you don't mangle the |
|
1149 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
1150 | - * the exact same as this request body, you should omit the ETag. |
|
1151 | - * |
|
1152 | - * @param mixed $calendarId |
|
1153 | - * @param string $objectUri |
|
1154 | - * @param string $calendarData |
|
1155 | - * @return string |
|
1156 | - */ |
|
1157 | - function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
1158 | - $extraData = $this->getDenormalizedData($calendarData); |
|
1159 | - |
|
1160 | - $query = $this->db->getQueryBuilder(); |
|
1161 | - $query->update('calendarobjects') |
|
1162 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
1163 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
1164 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
1165 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
1166 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
1167 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
1168 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
1169 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
1170 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
1171 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
1172 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1173 | - ->execute(); |
|
1174 | - |
|
1175 | - $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
1176 | - |
|
1177 | - $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1178 | - if (is_array($data)) { |
|
1179 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent( |
|
1180 | - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', |
|
1181 | - [ |
|
1182 | - 'calendarId' => $calendarId, |
|
1183 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1184 | - 'shares' => $this->getShares($calendarId), |
|
1185 | - 'objectData' => $data, |
|
1186 | - ] |
|
1187 | - )); |
|
1188 | - } |
|
1189 | - $this->addChange($calendarId, $objectUri, 2); |
|
1190 | - |
|
1191 | - return '"' . $extraData['etag'] . '"'; |
|
1192 | - } |
|
1193 | - |
|
1194 | - /** |
|
1195 | - * @param int $calendarObjectId |
|
1196 | - * @param int $classification |
|
1197 | - */ |
|
1198 | - public function setClassification($calendarObjectId, $classification) { |
|
1199 | - if (!in_array($classification, [ |
|
1200 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
1201 | - ])) { |
|
1202 | - throw new \InvalidArgumentException(); |
|
1203 | - } |
|
1204 | - $query = $this->db->getQueryBuilder(); |
|
1205 | - $query->update('calendarobjects') |
|
1206 | - ->set('classification', $query->createNamedParameter($classification)) |
|
1207 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
1208 | - ->execute(); |
|
1209 | - } |
|
1210 | - |
|
1211 | - /** |
|
1212 | - * Deletes an existing calendar object. |
|
1213 | - * |
|
1214 | - * The object uri is only the basename, or filename and not a full path. |
|
1215 | - * |
|
1216 | - * @param mixed $calendarId |
|
1217 | - * @param string $objectUri |
|
1218 | - * @return void |
|
1219 | - */ |
|
1220 | - function deleteCalendarObject($calendarId, $objectUri) { |
|
1221 | - $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1222 | - if (is_array($data)) { |
|
1223 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( |
|
1224 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', |
|
1225 | - [ |
|
1226 | - 'calendarId' => $calendarId, |
|
1227 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
1228 | - 'shares' => $this->getShares($calendarId), |
|
1229 | - 'objectData' => $data, |
|
1230 | - ] |
|
1231 | - )); |
|
1232 | - } |
|
1233 | - |
|
1234 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
1235 | - $stmt->execute([$calendarId, $objectUri]); |
|
1236 | - |
|
1237 | - $this->purgeProperties($calendarId, $data['id']); |
|
1238 | - |
|
1239 | - $this->addChange($calendarId, $objectUri, 3); |
|
1240 | - } |
|
1241 | - |
|
1242 | - /** |
|
1243 | - * Performs a calendar-query on the contents of this calendar. |
|
1244 | - * |
|
1245 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
1246 | - * calendar-query it is possible for a client to request a specific set of |
|
1247 | - * object, based on contents of iCalendar properties, date-ranges and |
|
1248 | - * iCalendar component types (VTODO, VEVENT). |
|
1249 | - * |
|
1250 | - * This method should just return a list of (relative) urls that match this |
|
1251 | - * query. |
|
1252 | - * |
|
1253 | - * The list of filters are specified as an array. The exact array is |
|
1254 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
1255 | - * |
|
1256 | - * Note that it is extremely likely that getCalendarObject for every path |
|
1257 | - * returned from this method will be called almost immediately after. You |
|
1258 | - * may want to anticipate this to speed up these requests. |
|
1259 | - * |
|
1260 | - * This method provides a default implementation, which parses *all* the |
|
1261 | - * iCalendar objects in the specified calendar. |
|
1262 | - * |
|
1263 | - * This default may well be good enough for personal use, and calendars |
|
1264 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
1265 | - * or high loads, you are strongly advised to optimize certain paths. |
|
1266 | - * |
|
1267 | - * The best way to do so is override this method and to optimize |
|
1268 | - * specifically for 'common filters'. |
|
1269 | - * |
|
1270 | - * Requests that are extremely common are: |
|
1271 | - * * requests for just VEVENTS |
|
1272 | - * * requests for just VTODO |
|
1273 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
1274 | - * |
|
1275 | - * ..and combinations of these requests. It may not be worth it to try to |
|
1276 | - * handle every possible situation and just rely on the (relatively |
|
1277 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
1278 | - * |
|
1279 | - * Note that especially time-range-filters may be difficult to parse. A |
|
1280 | - * time-range filter specified on a VEVENT must for instance also handle |
|
1281 | - * recurrence rules correctly. |
|
1282 | - * A good example of how to interprete all these filters can also simply |
|
1283 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
1284 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
1285 | - * to think of. |
|
1286 | - * |
|
1287 | - * @param mixed $calendarId |
|
1288 | - * @param array $filters |
|
1289 | - * @return array |
|
1290 | - */ |
|
1291 | - function calendarQuery($calendarId, array $filters):array { |
|
1292 | - return $this->commonCalendarQuery('calendarobjects', 'calendarid', $calendarId, $filters); |
|
1293 | - } |
|
1294 | - |
|
1295 | - /** |
|
1296 | - * @param $subscriptionId |
|
1297 | - * @param array $filters |
|
1298 | - * @return array |
|
1299 | - */ |
|
1300 | - public function cachedCalendarQuery($subscriptionId, array $filters):array { |
|
1301 | - return $this->commonCalendarQuery('calendarsubscrobjects', 'subscriptionid', $subscriptionId, $filters); |
|
1302 | - } |
|
1303 | - |
|
1304 | - /** |
|
1305 | - * @param string $tablename |
|
1306 | - * @param string $selector |
|
1307 | - * @param $id |
|
1308 | - * @param array $filters |
|
1309 | - * @return array |
|
1310 | - */ |
|
1311 | - private function commonCalendarQuery(string $tablename, string $selector, $id, array $filters) { |
|
1312 | - $componentType = null; |
|
1313 | - $requirePostFilter = true; |
|
1314 | - $timeRange = null; |
|
1315 | - |
|
1316 | - // if no filters were specified, we don't need to filter after a query |
|
1317 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
1318 | - $requirePostFilter = false; |
|
1319 | - } |
|
1320 | - |
|
1321 | - // Figuring out if there's a component filter |
|
1322 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
1323 | - $componentType = $filters['comp-filters'][0]['name']; |
|
1324 | - |
|
1325 | - // Checking if we need post-filters |
|
1326 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
1327 | - $requirePostFilter = false; |
|
1328 | - } |
|
1329 | - // There was a time-range filter |
|
1330 | - if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
1331 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
1332 | - |
|
1333 | - // If start time OR the end time is not specified, we can do a |
|
1334 | - // 100% accurate mysql query. |
|
1335 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
1336 | - $requirePostFilter = false; |
|
1337 | - } |
|
1338 | - } |
|
1339 | - |
|
1340 | - } |
|
1341 | - $columns = ['uri']; |
|
1342 | - if ($requirePostFilter) { |
|
1343 | - $columns = ['uri', 'calendardata']; |
|
1344 | - } |
|
1345 | - $query = $this->db->getQueryBuilder(); |
|
1346 | - $query->select($columns) |
|
1347 | - ->from($tablename) |
|
1348 | - ->where($query->expr()->eq($selector, $query->createNamedParameter($id))); |
|
1349 | - |
|
1350 | - if ($componentType) { |
|
1351 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
1352 | - } |
|
1353 | - |
|
1354 | - if ($timeRange && $timeRange['start']) { |
|
1355 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
1356 | - } |
|
1357 | - if ($timeRange && $timeRange['end']) { |
|
1358 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
1359 | - } |
|
1360 | - |
|
1361 | - $stmt = $query->execute(); |
|
1362 | - |
|
1363 | - $result = []; |
|
1364 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1365 | - if ($requirePostFilter) { |
|
1366 | - // validateFilterForObject will parse the calendar data |
|
1367 | - // catch parsing errors |
|
1368 | - try { |
|
1369 | - $matches = $this->validateFilterForObject($row, $filters); |
|
1370 | - } catch(ParseException $ex) { |
|
1371 | - $this->logger->logException($ex, [ |
|
1372 | - 'app' => 'dav', |
|
1373 | - 'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri'] |
|
1374 | - ]); |
|
1375 | - continue; |
|
1376 | - } catch (InvalidDataException $ex) { |
|
1377 | - $this->logger->logException($ex, [ |
|
1378 | - 'app' => 'dav', |
|
1379 | - 'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri'] |
|
1380 | - ]); |
|
1381 | - continue; |
|
1382 | - } |
|
1383 | - |
|
1384 | - if (!$matches) { |
|
1385 | - continue; |
|
1386 | - } |
|
1387 | - } |
|
1388 | - $result[] = $row['uri']; |
|
1389 | - } |
|
1390 | - |
|
1391 | - return $result; |
|
1392 | - } |
|
1393 | - |
|
1394 | - /** |
|
1395 | - * custom Nextcloud search extension for CalDAV |
|
1396 | - * |
|
1397 | - * @param string $principalUri |
|
1398 | - * @param array $filters |
|
1399 | - * @param integer|null $limit |
|
1400 | - * @param integer|null $offset |
|
1401 | - * @return array |
|
1402 | - */ |
|
1403 | - public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
1404 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
1405 | - $ownCalendars = []; |
|
1406 | - $sharedCalendars = []; |
|
1407 | - |
|
1408 | - $uriMapper = []; |
|
1409 | - |
|
1410 | - foreach($calendars as $calendar) { |
|
1411 | - if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
1412 | - $ownCalendars[] = $calendar['id']; |
|
1413 | - } else { |
|
1414 | - $sharedCalendars[] = $calendar['id']; |
|
1415 | - } |
|
1416 | - $uriMapper[$calendar['id']] = $calendar['uri']; |
|
1417 | - } |
|
1418 | - if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
1419 | - return []; |
|
1420 | - } |
|
1421 | - |
|
1422 | - $query = $this->db->getQueryBuilder(); |
|
1423 | - // Calendar id expressions |
|
1424 | - $calendarExpressions = []; |
|
1425 | - foreach($ownCalendars as $id) { |
|
1426 | - $calendarExpressions[] = $query->expr() |
|
1427 | - ->eq('c.calendarid', $query->createNamedParameter($id)); |
|
1428 | - } |
|
1429 | - foreach($sharedCalendars as $id) { |
|
1430 | - $calendarExpressions[] = $query->expr()->andX( |
|
1431 | - $query->expr()->eq('c.calendarid', |
|
1432 | - $query->createNamedParameter($id)), |
|
1433 | - $query->expr()->eq('c.classification', |
|
1434 | - $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)) |
|
1435 | - ); |
|
1436 | - } |
|
1437 | - |
|
1438 | - if (count($calendarExpressions) === 1) { |
|
1439 | - $calExpr = $calendarExpressions[0]; |
|
1440 | - } else { |
|
1441 | - $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
1442 | - } |
|
1443 | - |
|
1444 | - // Component expressions |
|
1445 | - $compExpressions = []; |
|
1446 | - foreach($filters['comps'] as $comp) { |
|
1447 | - $compExpressions[] = $query->expr() |
|
1448 | - ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
1449 | - } |
|
1450 | - |
|
1451 | - if (count($compExpressions) === 1) { |
|
1452 | - $compExpr = $compExpressions[0]; |
|
1453 | - } else { |
|
1454 | - $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
1455 | - } |
|
1456 | - |
|
1457 | - if (!isset($filters['props'])) { |
|
1458 | - $filters['props'] = []; |
|
1459 | - } |
|
1460 | - if (!isset($filters['params'])) { |
|
1461 | - $filters['params'] = []; |
|
1462 | - } |
|
1463 | - |
|
1464 | - $propParamExpressions = []; |
|
1465 | - foreach($filters['props'] as $prop) { |
|
1466 | - $propParamExpressions[] = $query->expr()->andX( |
|
1467 | - $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
1468 | - $query->expr()->isNull('i.parameter') |
|
1469 | - ); |
|
1470 | - } |
|
1471 | - foreach($filters['params'] as $param) { |
|
1472 | - $propParamExpressions[] = $query->expr()->andX( |
|
1473 | - $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
1474 | - $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
1475 | - ); |
|
1476 | - } |
|
1477 | - |
|
1478 | - if (count($propParamExpressions) === 1) { |
|
1479 | - $propParamExpr = $propParamExpressions[0]; |
|
1480 | - } else { |
|
1481 | - $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
1482 | - } |
|
1483 | - |
|
1484 | - $query->select(['c.calendarid', 'c.uri']) |
|
1485 | - ->from($this->dbObjectPropertiesTable, 'i') |
|
1486 | - ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
1487 | - ->where($calExpr) |
|
1488 | - ->andWhere($compExpr) |
|
1489 | - ->andWhere($propParamExpr) |
|
1490 | - ->andWhere($query->expr()->iLike('i.value', |
|
1491 | - $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))); |
|
1492 | - |
|
1493 | - if ($offset) { |
|
1494 | - $query->setFirstResult($offset); |
|
1495 | - } |
|
1496 | - if ($limit) { |
|
1497 | - $query->setMaxResults($limit); |
|
1498 | - } |
|
1499 | - |
|
1500 | - $stmt = $query->execute(); |
|
1501 | - |
|
1502 | - $result = []; |
|
1503 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1504 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
1505 | - if (!in_array($path, $result)) { |
|
1506 | - $result[] = $path; |
|
1507 | - } |
|
1508 | - } |
|
1509 | - |
|
1510 | - return $result; |
|
1511 | - } |
|
1512 | - |
|
1513 | - /** |
|
1514 | - * used for Nextcloud's calendar API |
|
1515 | - * |
|
1516 | - * @param array $calendarInfo |
|
1517 | - * @param string $pattern |
|
1518 | - * @param array $searchProperties |
|
1519 | - * @param array $options |
|
1520 | - * @param integer|null $limit |
|
1521 | - * @param integer|null $offset |
|
1522 | - * |
|
1523 | - * @return array |
|
1524 | - */ |
|
1525 | - public function search(array $calendarInfo, $pattern, array $searchProperties, |
|
1526 | - array $options, $limit, $offset) { |
|
1527 | - $outerQuery = $this->db->getQueryBuilder(); |
|
1528 | - $innerQuery = $this->db->getQueryBuilder(); |
|
1529 | - |
|
1530 | - $innerQuery->selectDistinct('op.objectid') |
|
1531 | - ->from($this->dbObjectPropertiesTable, 'op') |
|
1532 | - ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
1533 | - $outerQuery->createNamedParameter($calendarInfo['id']))); |
|
1534 | - |
|
1535 | - // only return public items for shared calendars for now |
|
1536 | - if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
1537 | - $innerQuery->andWhere($innerQuery->expr()->eq('c.classification', |
|
1538 | - $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
1539 | - } |
|
1540 | - |
|
1541 | - $or = $innerQuery->expr()->orX(); |
|
1542 | - foreach($searchProperties as $searchProperty) { |
|
1543 | - $or->add($innerQuery->expr()->eq('op.name', |
|
1544 | - $outerQuery->createNamedParameter($searchProperty))); |
|
1545 | - } |
|
1546 | - $innerQuery->andWhere($or); |
|
1547 | - |
|
1548 | - if ($pattern !== '') { |
|
1549 | - $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
1550 | - $outerQuery->createNamedParameter('%' . |
|
1551 | - $this->db->escapeLikeParameter($pattern) . '%'))); |
|
1552 | - } |
|
1553 | - |
|
1554 | - $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
1555 | - ->from('calendarobjects', 'c'); |
|
1556 | - |
|
1557 | - if (isset($options['timerange'])) { |
|
1558 | - if (isset($options['timerange']['start'])) { |
|
1559 | - $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence', |
|
1560 | - $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp))); |
|
1561 | - |
|
1562 | - } |
|
1563 | - if (isset($options['timerange']['end'])) { |
|
1564 | - $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence', |
|
1565 | - $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp))); |
|
1566 | - } |
|
1567 | - } |
|
1568 | - |
|
1569 | - if (isset($options['types'])) { |
|
1570 | - $or = $outerQuery->expr()->orX(); |
|
1571 | - foreach($options['types'] as $type) { |
|
1572 | - $or->add($outerQuery->expr()->eq('componenttype', |
|
1573 | - $outerQuery->createNamedParameter($type))); |
|
1574 | - } |
|
1575 | - $outerQuery->andWhere($or); |
|
1576 | - } |
|
1577 | - |
|
1578 | - $outerQuery->andWhere($outerQuery->expr()->in('c.id', |
|
1579 | - $outerQuery->createFunction($innerQuery->getSQL()))); |
|
1580 | - |
|
1581 | - if ($offset) { |
|
1582 | - $outerQuery->setFirstResult($offset); |
|
1583 | - } |
|
1584 | - if ($limit) { |
|
1585 | - $outerQuery->setMaxResults($limit); |
|
1586 | - } |
|
1587 | - |
|
1588 | - $result = $outerQuery->execute(); |
|
1589 | - $calendarObjects = $result->fetchAll(); |
|
1590 | - |
|
1591 | - return array_map(function($o) { |
|
1592 | - $calendarData = Reader::read($o['calendardata']); |
|
1593 | - $comps = $calendarData->getComponents(); |
|
1594 | - $objects = []; |
|
1595 | - $timezones = []; |
|
1596 | - foreach($comps as $comp) { |
|
1597 | - if ($comp instanceof VTimeZone) { |
|
1598 | - $timezones[] = $comp; |
|
1599 | - } else { |
|
1600 | - $objects[] = $comp; |
|
1601 | - } |
|
1602 | - } |
|
1603 | - |
|
1604 | - return [ |
|
1605 | - 'id' => $o['id'], |
|
1606 | - 'type' => $o['componenttype'], |
|
1607 | - 'uid' => $o['uid'], |
|
1608 | - 'uri' => $o['uri'], |
|
1609 | - 'objects' => array_map(function($c) { |
|
1610 | - return $this->transformSearchData($c); |
|
1611 | - }, $objects), |
|
1612 | - 'timezones' => array_map(function($c) { |
|
1613 | - return $this->transformSearchData($c); |
|
1614 | - }, $timezones), |
|
1615 | - ]; |
|
1616 | - }, $calendarObjects); |
|
1617 | - } |
|
1618 | - |
|
1619 | - /** |
|
1620 | - * @param Component $comp |
|
1621 | - * @return array |
|
1622 | - */ |
|
1623 | - private function transformSearchData(Component $comp) { |
|
1624 | - $data = []; |
|
1625 | - /** @var Component[] $subComponents */ |
|
1626 | - $subComponents = $comp->getComponents(); |
|
1627 | - /** @var Property[] $properties */ |
|
1628 | - $properties = array_filter($comp->children(), function($c) { |
|
1629 | - return $c instanceof Property; |
|
1630 | - }); |
|
1631 | - $validationRules = $comp->getValidationRules(); |
|
1632 | - |
|
1633 | - foreach($subComponents as $subComponent) { |
|
1634 | - $name = $subComponent->name; |
|
1635 | - if (!isset($data[$name])) { |
|
1636 | - $data[$name] = []; |
|
1637 | - } |
|
1638 | - $data[$name][] = $this->transformSearchData($subComponent); |
|
1639 | - } |
|
1640 | - |
|
1641 | - foreach($properties as $property) { |
|
1642 | - $name = $property->name; |
|
1643 | - if (!isset($validationRules[$name])) { |
|
1644 | - $validationRules[$name] = '*'; |
|
1645 | - } |
|
1646 | - |
|
1647 | - $rule = $validationRules[$property->name]; |
|
1648 | - if ($rule === '+' || $rule === '*') { // multiple |
|
1649 | - if (!isset($data[$name])) { |
|
1650 | - $data[$name] = []; |
|
1651 | - } |
|
1652 | - |
|
1653 | - $data[$name][] = $this->transformSearchProperty($property); |
|
1654 | - } else { // once |
|
1655 | - $data[$name] = $this->transformSearchProperty($property); |
|
1656 | - } |
|
1657 | - } |
|
1658 | - |
|
1659 | - return $data; |
|
1660 | - } |
|
1661 | - |
|
1662 | - /** |
|
1663 | - * @param Property $prop |
|
1664 | - * @return array |
|
1665 | - */ |
|
1666 | - private function transformSearchProperty(Property $prop) { |
|
1667 | - // No need to check Date, as it extends DateTime |
|
1668 | - if ($prop instanceof Property\ICalendar\DateTime) { |
|
1669 | - $value = $prop->getDateTime(); |
|
1670 | - } else { |
|
1671 | - $value = $prop->getValue(); |
|
1672 | - } |
|
1673 | - |
|
1674 | - return [ |
|
1675 | - $value, |
|
1676 | - $prop->parameters() |
|
1677 | - ]; |
|
1678 | - } |
|
1679 | - |
|
1680 | - /** |
|
1681 | - * Searches through all of a users calendars and calendar objects to find |
|
1682 | - * an object with a specific UID. |
|
1683 | - * |
|
1684 | - * This method should return the path to this object, relative to the |
|
1685 | - * calendar home, so this path usually only contains two parts: |
|
1686 | - * |
|
1687 | - * calendarpath/objectpath.ics |
|
1688 | - * |
|
1689 | - * If the uid is not found, return null. |
|
1690 | - * |
|
1691 | - * This method should only consider * objects that the principal owns, so |
|
1692 | - * any calendars owned by other principals that also appear in this |
|
1693 | - * collection should be ignored. |
|
1694 | - * |
|
1695 | - * @param string $principalUri |
|
1696 | - * @param string $uid |
|
1697 | - * @return string|null |
|
1698 | - */ |
|
1699 | - function getCalendarObjectByUID($principalUri, $uid) { |
|
1700 | - |
|
1701 | - $query = $this->db->getQueryBuilder(); |
|
1702 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
1703 | - ->from('calendarobjects', 'co') |
|
1704 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
1705 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
1706 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
1707 | - |
|
1708 | - $stmt = $query->execute(); |
|
1709 | - |
|
1710 | - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1711 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
1712 | - } |
|
1713 | - |
|
1714 | - return null; |
|
1715 | - } |
|
1716 | - |
|
1717 | - /** |
|
1718 | - * The getChanges method returns all the changes that have happened, since |
|
1719 | - * the specified syncToken in the specified calendar. |
|
1720 | - * |
|
1721 | - * This function should return an array, such as the following: |
|
1722 | - * |
|
1723 | - * [ |
|
1724 | - * 'syncToken' => 'The current synctoken', |
|
1725 | - * 'added' => [ |
|
1726 | - * 'new.txt', |
|
1727 | - * ], |
|
1728 | - * 'modified' => [ |
|
1729 | - * 'modified.txt', |
|
1730 | - * ], |
|
1731 | - * 'deleted' => [ |
|
1732 | - * 'foo.php.bak', |
|
1733 | - * 'old.txt' |
|
1734 | - * ] |
|
1735 | - * ); |
|
1736 | - * |
|
1737 | - * The returned syncToken property should reflect the *current* syncToken |
|
1738 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
1739 | - * property This is * needed here too, to ensure the operation is atomic. |
|
1740 | - * |
|
1741 | - * If the $syncToken argument is specified as null, this is an initial |
|
1742 | - * sync, and all members should be reported. |
|
1743 | - * |
|
1744 | - * The modified property is an array of nodenames that have changed since |
|
1745 | - * the last token. |
|
1746 | - * |
|
1747 | - * The deleted property is an array with nodenames, that have been deleted |
|
1748 | - * from collection. |
|
1749 | - * |
|
1750 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
1751 | - * 1, you only have to report changes that happened only directly in |
|
1752 | - * immediate descendants. If it's 2, it should also include changes from |
|
1753 | - * the nodes below the child collections. (grandchildren) |
|
1754 | - * |
|
1755 | - * The $limit argument allows a client to specify how many results should |
|
1756 | - * be returned at most. If the limit is not specified, it should be treated |
|
1757 | - * as infinite. |
|
1758 | - * |
|
1759 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
1760 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
1761 | - * |
|
1762 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
1763 | - * return null. |
|
1764 | - * |
|
1765 | - * The limit is 'suggestive'. You are free to ignore it. |
|
1766 | - * |
|
1767 | - * @param string $calendarId |
|
1768 | - * @param string $syncToken |
|
1769 | - * @param int $syncLevel |
|
1770 | - * @param int $limit |
|
1771 | - * @return array |
|
1772 | - */ |
|
1773 | - function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
1774 | - // Current synctoken |
|
1775 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1776 | - $stmt->execute([ $calendarId ]); |
|
1777 | - $currentToken = $stmt->fetchColumn(0); |
|
1778 | - |
|
1779 | - if (is_null($currentToken)) { |
|
1780 | - return null; |
|
1781 | - } |
|
1782 | - |
|
1783 | - $result = [ |
|
1784 | - 'syncToken' => $currentToken, |
|
1785 | - 'added' => [], |
|
1786 | - 'modified' => [], |
|
1787 | - 'deleted' => [], |
|
1788 | - ]; |
|
1789 | - |
|
1790 | - if ($syncToken) { |
|
1791 | - |
|
1792 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
1793 | - if ($limit>0) { |
|
1794 | - $query.= " LIMIT " . (int)$limit; |
|
1795 | - } |
|
1796 | - |
|
1797 | - // Fetching all changes |
|
1798 | - $stmt = $this->db->prepare($query); |
|
1799 | - $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
1800 | - |
|
1801 | - $changes = []; |
|
1802 | - |
|
1803 | - // This loop ensures that any duplicates are overwritten, only the |
|
1804 | - // last change on a node is relevant. |
|
1805 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1806 | - |
|
1807 | - $changes[$row['uri']] = $row['operation']; |
|
1808 | - |
|
1809 | - } |
|
1810 | - |
|
1811 | - foreach($changes as $uri => $operation) { |
|
1812 | - |
|
1813 | - switch($operation) { |
|
1814 | - case 1 : |
|
1815 | - $result['added'][] = $uri; |
|
1816 | - break; |
|
1817 | - case 2 : |
|
1818 | - $result['modified'][] = $uri; |
|
1819 | - break; |
|
1820 | - case 3 : |
|
1821 | - $result['deleted'][] = $uri; |
|
1822 | - break; |
|
1823 | - } |
|
1824 | - |
|
1825 | - } |
|
1826 | - } else { |
|
1827 | - // No synctoken supplied, this is the initial sync. |
|
1828 | - $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
1829 | - $stmt = $this->db->prepare($query); |
|
1830 | - $stmt->execute([$calendarId]); |
|
1831 | - |
|
1832 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
1833 | - } |
|
1834 | - return $result; |
|
1835 | - |
|
1836 | - } |
|
1837 | - |
|
1838 | - /** |
|
1839 | - * Returns a list of subscriptions for a principal. |
|
1840 | - * |
|
1841 | - * Every subscription is an array with the following keys: |
|
1842 | - * * id, a unique id that will be used by other functions to modify the |
|
1843 | - * subscription. This can be the same as the uri or a database key. |
|
1844 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
1845 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
1846 | - * principalUri passed to this method. |
|
1847 | - * |
|
1848 | - * Furthermore, all the subscription info must be returned too: |
|
1849 | - * |
|
1850 | - * 1. {DAV:}displayname |
|
1851 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
1852 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
1853 | - * should not be stripped). |
|
1854 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
1855 | - * should not be stripped). |
|
1856 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
1857 | - * attachments should not be stripped). |
|
1858 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
1859 | - * Sabre\DAV\Property\Href). |
|
1860 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
1861 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
1862 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
1863 | - * (should just be an instance of |
|
1864 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
1865 | - * default components). |
|
1866 | - * |
|
1867 | - * @param string $principalUri |
|
1868 | - * @return array |
|
1869 | - */ |
|
1870 | - function getSubscriptionsForUser($principalUri) { |
|
1871 | - $fields = array_values($this->subscriptionPropertyMap); |
|
1872 | - $fields[] = 'id'; |
|
1873 | - $fields[] = 'uri'; |
|
1874 | - $fields[] = 'source'; |
|
1875 | - $fields[] = 'principaluri'; |
|
1876 | - $fields[] = 'lastmodified'; |
|
1877 | - |
|
1878 | - $query = $this->db->getQueryBuilder(); |
|
1879 | - $query->select($fields) |
|
1880 | - ->from('calendarsubscriptions') |
|
1881 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1882 | - ->orderBy('calendarorder', 'asc'); |
|
1883 | - $stmt =$query->execute(); |
|
1884 | - |
|
1885 | - $subscriptions = []; |
|
1886 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1887 | - |
|
1888 | - $subscription = [ |
|
1889 | - 'id' => $row['id'], |
|
1890 | - 'uri' => $row['uri'], |
|
1891 | - 'principaluri' => $row['principaluri'], |
|
1892 | - 'source' => $row['source'], |
|
1893 | - 'lastmodified' => $row['lastmodified'], |
|
1894 | - |
|
1895 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
1896 | - ]; |
|
1897 | - |
|
1898 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1899 | - if (!is_null($row[$dbName])) { |
|
1900 | - $subscription[$xmlName] = $row[$dbName]; |
|
1901 | - } |
|
1902 | - } |
|
1903 | - |
|
1904 | - $subscriptions[] = $subscription; |
|
1905 | - |
|
1906 | - } |
|
1907 | - |
|
1908 | - return $subscriptions; |
|
1909 | - } |
|
1910 | - |
|
1911 | - /** |
|
1912 | - * Creates a new subscription for a principal. |
|
1913 | - * |
|
1914 | - * If the creation was a success, an id must be returned that can be used to reference |
|
1915 | - * this subscription in other methods, such as updateSubscription. |
|
1916 | - * |
|
1917 | - * @param string $principalUri |
|
1918 | - * @param string $uri |
|
1919 | - * @param array $properties |
|
1920 | - * @return mixed |
|
1921 | - */ |
|
1922 | - function createSubscription($principalUri, $uri, array $properties) { |
|
1923 | - |
|
1924 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
1925 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
1926 | - } |
|
1927 | - |
|
1928 | - $values = [ |
|
1929 | - 'principaluri' => $principalUri, |
|
1930 | - 'uri' => $uri, |
|
1931 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
1932 | - 'lastmodified' => time(), |
|
1933 | - ]; |
|
1934 | - |
|
1935 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
1936 | - |
|
1937 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1938 | - if (array_key_exists($xmlName, $properties)) { |
|
1939 | - $values[$dbName] = $properties[$xmlName]; |
|
1940 | - if (in_array($dbName, $propertiesBoolean)) { |
|
1941 | - $values[$dbName] = true; |
|
1942 | - } |
|
1943 | - } |
|
1944 | - } |
|
1945 | - |
|
1946 | - $valuesToInsert = array(); |
|
1947 | - |
|
1948 | - $query = $this->db->getQueryBuilder(); |
|
1949 | - |
|
1950 | - foreach (array_keys($values) as $name) { |
|
1951 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
1952 | - } |
|
1953 | - |
|
1954 | - $query->insert('calendarsubscriptions') |
|
1955 | - ->values($valuesToInsert) |
|
1956 | - ->execute(); |
|
1957 | - |
|
1958 | - $subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
1959 | - |
|
1960 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent( |
|
1961 | - '\OCA\DAV\CalDAV\CalDavBackend::createSubscription', |
|
1962 | - [ |
|
1963 | - 'subscriptionId' => $subscriptionId, |
|
1964 | - 'subscriptionData' => $this->getSubscriptionById($subscriptionId), |
|
1965 | - ])); |
|
1966 | - |
|
1967 | - return $subscriptionId; |
|
1968 | - } |
|
1969 | - |
|
1970 | - /** |
|
1971 | - * Updates a subscription |
|
1972 | - * |
|
1973 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
1974 | - * To do the actual updates, you must tell this object which properties |
|
1975 | - * you're going to process with the handle() method. |
|
1976 | - * |
|
1977 | - * Calling the handle method is like telling the PropPatch object "I |
|
1978 | - * promise I can handle updating this property". |
|
1979 | - * |
|
1980 | - * Read the PropPatch documentation for more info and examples. |
|
1981 | - * |
|
1982 | - * @param mixed $subscriptionId |
|
1983 | - * @param PropPatch $propPatch |
|
1984 | - * @return void |
|
1985 | - */ |
|
1986 | - function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
1987 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
1988 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
1989 | - |
|
1990 | - /** |
|
1991 | - * @suppress SqlInjectionChecker |
|
1992 | - */ |
|
1993 | - $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
1994 | - |
|
1995 | - $newValues = []; |
|
1996 | - |
|
1997 | - foreach($mutations as $propertyName=>$propertyValue) { |
|
1998 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
1999 | - $newValues['source'] = $propertyValue->getHref(); |
|
2000 | - } else { |
|
2001 | - $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
2002 | - $newValues[$fieldName] = $propertyValue; |
|
2003 | - } |
|
2004 | - } |
|
2005 | - |
|
2006 | - $query = $this->db->getQueryBuilder(); |
|
2007 | - $query->update('calendarsubscriptions') |
|
2008 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
2009 | - foreach($newValues as $fieldName=>$value) { |
|
2010 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
2011 | - } |
|
2012 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
2013 | - ->execute(); |
|
2014 | - |
|
2015 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent( |
|
2016 | - '\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', |
|
2017 | - [ |
|
2018 | - 'subscriptionId' => $subscriptionId, |
|
2019 | - 'subscriptionData' => $this->getSubscriptionById($subscriptionId), |
|
2020 | - 'propertyMutations' => $mutations, |
|
2021 | - ])); |
|
2022 | - |
|
2023 | - return true; |
|
2024 | - |
|
2025 | - }); |
|
2026 | - } |
|
2027 | - |
|
2028 | - /** |
|
2029 | - * Deletes a subscription. |
|
2030 | - * |
|
2031 | - * @param mixed $subscriptionId |
|
2032 | - * @return void |
|
2033 | - */ |
|
2034 | - function deleteSubscription($subscriptionId) { |
|
2035 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent( |
|
2036 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', |
|
2037 | - [ |
|
2038 | - 'subscriptionId' => $subscriptionId, |
|
2039 | - 'subscriptionData' => $this->getSubscriptionById($subscriptionId), |
|
2040 | - ])); |
|
2041 | - |
|
2042 | - $query = $this->db->getQueryBuilder(); |
|
2043 | - $query->delete('calendarsubscriptions') |
|
2044 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
2045 | - ->execute(); |
|
2046 | - } |
|
2047 | - |
|
2048 | - /** |
|
2049 | - * Returns a single scheduling object for the inbox collection. |
|
2050 | - * |
|
2051 | - * The returned array should contain the following elements: |
|
2052 | - * * uri - A unique basename for the object. This will be used to |
|
2053 | - * construct a full uri. |
|
2054 | - * * calendardata - The iCalendar object |
|
2055 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
2056 | - * timestamp, or a PHP DateTime object. |
|
2057 | - * * etag - A unique token that must change if the object changed. |
|
2058 | - * * size - The size of the object, in bytes. |
|
2059 | - * |
|
2060 | - * @param string $principalUri |
|
2061 | - * @param string $objectUri |
|
2062 | - * @return array |
|
2063 | - */ |
|
2064 | - function getSchedulingObject($principalUri, $objectUri) { |
|
2065 | - $query = $this->db->getQueryBuilder(); |
|
2066 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
2067 | - ->from('schedulingobjects') |
|
2068 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
2069 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
2070 | - ->execute(); |
|
2071 | - |
|
2072 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
2073 | - |
|
2074 | - if(!$row) { |
|
2075 | - return null; |
|
2076 | - } |
|
2077 | - |
|
2078 | - return [ |
|
2079 | - 'uri' => $row['uri'], |
|
2080 | - 'calendardata' => $row['calendardata'], |
|
2081 | - 'lastmodified' => $row['lastmodified'], |
|
2082 | - 'etag' => '"' . $row['etag'] . '"', |
|
2083 | - 'size' => (int)$row['size'], |
|
2084 | - ]; |
|
2085 | - } |
|
2086 | - |
|
2087 | - /** |
|
2088 | - * Returns all scheduling objects for the inbox collection. |
|
2089 | - * |
|
2090 | - * These objects should be returned as an array. Every item in the array |
|
2091 | - * should follow the same structure as returned from getSchedulingObject. |
|
2092 | - * |
|
2093 | - * The main difference is that 'calendardata' is optional. |
|
2094 | - * |
|
2095 | - * @param string $principalUri |
|
2096 | - * @return array |
|
2097 | - */ |
|
2098 | - function getSchedulingObjects($principalUri) { |
|
2099 | - $query = $this->db->getQueryBuilder(); |
|
2100 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
2101 | - ->from('schedulingobjects') |
|
2102 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
2103 | - ->execute(); |
|
2104 | - |
|
2105 | - $result = []; |
|
2106 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
2107 | - $result[] = [ |
|
2108 | - 'calendardata' => $row['calendardata'], |
|
2109 | - 'uri' => $row['uri'], |
|
2110 | - 'lastmodified' => $row['lastmodified'], |
|
2111 | - 'etag' => '"' . $row['etag'] . '"', |
|
2112 | - 'size' => (int)$row['size'], |
|
2113 | - ]; |
|
2114 | - } |
|
2115 | - |
|
2116 | - return $result; |
|
2117 | - } |
|
2118 | - |
|
2119 | - /** |
|
2120 | - * Deletes a scheduling object from the inbox collection. |
|
2121 | - * |
|
2122 | - * @param string $principalUri |
|
2123 | - * @param string $objectUri |
|
2124 | - * @return void |
|
2125 | - */ |
|
2126 | - function deleteSchedulingObject($principalUri, $objectUri) { |
|
2127 | - $query = $this->db->getQueryBuilder(); |
|
2128 | - $query->delete('schedulingobjects') |
|
2129 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
2130 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
2131 | - ->execute(); |
|
2132 | - } |
|
2133 | - |
|
2134 | - /** |
|
2135 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
2136 | - * |
|
2137 | - * @param string $principalUri |
|
2138 | - * @param string $objectUri |
|
2139 | - * @param string $objectData |
|
2140 | - * @return void |
|
2141 | - */ |
|
2142 | - function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
2143 | - $query = $this->db->getQueryBuilder(); |
|
2144 | - $query->insert('schedulingobjects') |
|
2145 | - ->values([ |
|
2146 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
2147 | - 'calendardata' => $query->createNamedParameter($objectData), |
|
2148 | - 'uri' => $query->createNamedParameter($objectUri), |
|
2149 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
2150 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
2151 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
2152 | - ]) |
|
2153 | - ->execute(); |
|
2154 | - } |
|
2155 | - |
|
2156 | - /** |
|
2157 | - * Adds a change record to the calendarchanges table. |
|
2158 | - * |
|
2159 | - * @param mixed $calendarId |
|
2160 | - * @param string $objectUri |
|
2161 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
2162 | - * @return void |
|
2163 | - */ |
|
2164 | - protected function addChange($calendarId, $objectUri, $operation) { |
|
2165 | - |
|
2166 | - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
2167 | - $stmt->execute([ |
|
2168 | - $objectUri, |
|
2169 | - $calendarId, |
|
2170 | - $operation, |
|
2171 | - $calendarId |
|
2172 | - ]); |
|
2173 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
2174 | - $stmt->execute([ |
|
2175 | - $calendarId |
|
2176 | - ]); |
|
2177 | - |
|
2178 | - } |
|
2179 | - |
|
2180 | - /** |
|
2181 | - * Parses some information from calendar objects, used for optimized |
|
2182 | - * calendar-queries. |
|
2183 | - * |
|
2184 | - * Returns an array with the following keys: |
|
2185 | - * * etag - An md5 checksum of the object without the quotes. |
|
2186 | - * * size - Size of the object in bytes |
|
2187 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
2188 | - * * firstOccurence |
|
2189 | - * * lastOccurence |
|
2190 | - * * uid - value of the UID property |
|
2191 | - * |
|
2192 | - * @param string $calendarData |
|
2193 | - * @return array |
|
2194 | - */ |
|
2195 | - public function getDenormalizedData($calendarData) { |
|
2196 | - |
|
2197 | - $vObject = Reader::read($calendarData); |
|
2198 | - $componentType = null; |
|
2199 | - $component = null; |
|
2200 | - $firstOccurrence = null; |
|
2201 | - $lastOccurrence = null; |
|
2202 | - $uid = null; |
|
2203 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
2204 | - foreach($vObject->getComponents() as $component) { |
|
2205 | - if ($component->name!=='VTIMEZONE') { |
|
2206 | - $componentType = $component->name; |
|
2207 | - $uid = (string)$component->UID; |
|
2208 | - break; |
|
2209 | - } |
|
2210 | - } |
|
2211 | - if (!$componentType) { |
|
2212 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
2213 | - } |
|
2214 | - if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
2215 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
2216 | - // Finding the last occurrence is a bit harder |
|
2217 | - if (!isset($component->RRULE)) { |
|
2218 | - if (isset($component->DTEND)) { |
|
2219 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
2220 | - } elseif (isset($component->DURATION)) { |
|
2221 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
2222 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
2223 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
2224 | - } elseif (!$component->DTSTART->hasTime()) { |
|
2225 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
2226 | - $endDate->modify('+1 day'); |
|
2227 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
2228 | - } else { |
|
2229 | - $lastOccurrence = $firstOccurrence; |
|
2230 | - } |
|
2231 | - } else { |
|
2232 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
2233 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
2234 | - if ($it->isInfinite()) { |
|
2235 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
2236 | - } else { |
|
2237 | - $end = $it->getDtEnd(); |
|
2238 | - while($it->valid() && $end < $maxDate) { |
|
2239 | - $end = $it->getDtEnd(); |
|
2240 | - $it->next(); |
|
2241 | - |
|
2242 | - } |
|
2243 | - $lastOccurrence = $end->getTimestamp(); |
|
2244 | - } |
|
2245 | - |
|
2246 | - } |
|
2247 | - } |
|
2248 | - |
|
2249 | - if ($component->CLASS) { |
|
2250 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
2251 | - switch ($component->CLASS->getValue()) { |
|
2252 | - case 'PUBLIC': |
|
2253 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
2254 | - break; |
|
2255 | - case 'CONFIDENTIAL': |
|
2256 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
2257 | - break; |
|
2258 | - } |
|
2259 | - } |
|
2260 | - return [ |
|
2261 | - 'etag' => md5($calendarData), |
|
2262 | - 'size' => strlen($calendarData), |
|
2263 | - 'componentType' => $componentType, |
|
2264 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
2265 | - 'lastOccurence' => $lastOccurrence, |
|
2266 | - 'uid' => $uid, |
|
2267 | - 'classification' => $classification |
|
2268 | - ]; |
|
2269 | - |
|
2270 | - } |
|
2271 | - |
|
2272 | - /** |
|
2273 | - * @param $cardData |
|
2274 | - * @return bool|string |
|
2275 | - */ |
|
2276 | - private function readBlob($cardData) { |
|
2277 | - if (is_resource($cardData)) { |
|
2278 | - return stream_get_contents($cardData); |
|
2279 | - } |
|
2280 | - |
|
2281 | - return $cardData; |
|
2282 | - } |
|
2283 | - |
|
2284 | - /** |
|
2285 | - * @param IShareable $shareable |
|
2286 | - * @param array $add |
|
2287 | - * @param array $remove |
|
2288 | - */ |
|
2289 | - public function updateShares($shareable, $add, $remove) { |
|
2290 | - $calendarId = $shareable->getResourceId(); |
|
2291 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent( |
|
2292 | - '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
2293 | - [ |
|
2294 | - 'calendarId' => $calendarId, |
|
2295 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
2296 | - 'shares' => $this->getShares($calendarId), |
|
2297 | - 'add' => $add, |
|
2298 | - 'remove' => $remove, |
|
2299 | - ])); |
|
2300 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
2301 | - } |
|
2302 | - |
|
2303 | - /** |
|
2304 | - * @param int $resourceId |
|
2305 | - * @return array |
|
2306 | - */ |
|
2307 | - public function getShares($resourceId) { |
|
2308 | - return $this->sharingBackend->getShares($resourceId); |
|
2309 | - } |
|
2310 | - |
|
2311 | - /** |
|
2312 | - * @param boolean $value |
|
2313 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
2314 | - * @return string|null |
|
2315 | - */ |
|
2316 | - public function setPublishStatus($value, $calendar) { |
|
2317 | - |
|
2318 | - $calendarId = $calendar->getResourceId(); |
|
2319 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent( |
|
2320 | - '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
2321 | - [ |
|
2322 | - 'calendarId' => $calendarId, |
|
2323 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
2324 | - 'public' => $value, |
|
2325 | - ])); |
|
2326 | - |
|
2327 | - $query = $this->db->getQueryBuilder(); |
|
2328 | - if ($value) { |
|
2329 | - $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
2330 | - $query->insert('dav_shares') |
|
2331 | - ->values([ |
|
2332 | - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
2333 | - 'type' => $query->createNamedParameter('calendar'), |
|
2334 | - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
2335 | - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
2336 | - 'publicuri' => $query->createNamedParameter($publicUri) |
|
2337 | - ]); |
|
2338 | - $query->execute(); |
|
2339 | - return $publicUri; |
|
2340 | - } |
|
2341 | - $query->delete('dav_shares') |
|
2342 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
2343 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
2344 | - $query->execute(); |
|
2345 | - return null; |
|
2346 | - } |
|
2347 | - |
|
2348 | - /** |
|
2349 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
2350 | - * @return mixed |
|
2351 | - */ |
|
2352 | - public function getPublishStatus($calendar) { |
|
2353 | - $query = $this->db->getQueryBuilder(); |
|
2354 | - $result = $query->select('publicuri') |
|
2355 | - ->from('dav_shares') |
|
2356 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
2357 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
2358 | - ->execute(); |
|
2359 | - |
|
2360 | - $row = $result->fetch(); |
|
2361 | - $result->closeCursor(); |
|
2362 | - return $row ? reset($row) : false; |
|
2363 | - } |
|
2364 | - |
|
2365 | - /** |
|
2366 | - * @param int $resourceId |
|
2367 | - * @param array $acl |
|
2368 | - * @return array |
|
2369 | - */ |
|
2370 | - public function applyShareAcl($resourceId, $acl) { |
|
2371 | - return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
2372 | - } |
|
2373 | - |
|
2374 | - |
|
2375 | - |
|
2376 | - /** |
|
2377 | - * update properties table |
|
2378 | - * |
|
2379 | - * @param int $calendarId |
|
2380 | - * @param string $objectUri |
|
2381 | - * @param string $calendarData |
|
2382 | - */ |
|
2383 | - public function updateProperties($calendarId, $objectUri, $calendarData) { |
|
2384 | - $objectId = $this->getCalendarObjectId($calendarId, $objectUri); |
|
2385 | - |
|
2386 | - try { |
|
2387 | - $vCalendar = $this->readCalendarData($calendarData); |
|
2388 | - } catch (\Exception $ex) { |
|
2389 | - return; |
|
2390 | - } |
|
2391 | - |
|
2392 | - $this->purgeProperties($calendarId, $objectId); |
|
2393 | - |
|
2394 | - $query = $this->db->getQueryBuilder(); |
|
2395 | - $query->insert($this->dbObjectPropertiesTable) |
|
2396 | - ->values( |
|
2397 | - [ |
|
2398 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
2399 | - 'objectid' => $query->createNamedParameter($objectId), |
|
2400 | - 'name' => $query->createParameter('name'), |
|
2401 | - 'parameter' => $query->createParameter('parameter'), |
|
2402 | - 'value' => $query->createParameter('value'), |
|
2403 | - ] |
|
2404 | - ); |
|
2405 | - |
|
2406 | - $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
2407 | - foreach ($vCalendar->getComponents() as $component) { |
|
2408 | - if (!in_array($component->name, $indexComponents)) { |
|
2409 | - continue; |
|
2410 | - } |
|
2411 | - |
|
2412 | - foreach ($component->children() as $property) { |
|
2413 | - if (in_array($property->name, self::$indexProperties)) { |
|
2414 | - $value = $property->getValue(); |
|
2415 | - // is this a shitty db? |
|
2416 | - if (!$this->db->supports4ByteText()) { |
|
2417 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2418 | - } |
|
2419 | - $value = mb_substr($value, 0, 254); |
|
2420 | - |
|
2421 | - $query->setParameter('name', $property->name); |
|
2422 | - $query->setParameter('parameter', null); |
|
2423 | - $query->setParameter('value', $value); |
|
2424 | - $query->execute(); |
|
2425 | - } |
|
2426 | - |
|
2427 | - if (array_key_exists($property->name, self::$indexParameters)) { |
|
2428 | - $parameters = $property->parameters(); |
|
2429 | - $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
2430 | - |
|
2431 | - foreach ($parameters as $key => $value) { |
|
2432 | - if (in_array($key, $indexedParametersForProperty)) { |
|
2433 | - // is this a shitty db? |
|
2434 | - if ($this->db->supports4ByteText()) { |
|
2435 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2436 | - } |
|
2437 | - $value = mb_substr($value, 0, 254); |
|
2438 | - |
|
2439 | - $query->setParameter('name', $property->name); |
|
2440 | - $query->setParameter('parameter', substr($key, 0, 254)); |
|
2441 | - $query->setParameter('value', substr($value, 0, 254)); |
|
2442 | - $query->execute(); |
|
2443 | - } |
|
2444 | - } |
|
2445 | - } |
|
2446 | - } |
|
2447 | - } |
|
2448 | - } |
|
2449 | - |
|
2450 | - /** |
|
2451 | - * deletes all birthday calendars |
|
2452 | - */ |
|
2453 | - public function deleteAllBirthdayCalendars() { |
|
2454 | - $query = $this->db->getQueryBuilder(); |
|
2455 | - $result = $query->select(['id'])->from('calendars') |
|
2456 | - ->where($query->expr()->eq('uri', |
|
2457 | - $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
2458 | - ->execute(); |
|
2459 | - |
|
2460 | - $ids = $result->fetchAll(); |
|
2461 | - foreach($ids as $id) { |
|
2462 | - $this->deleteCalendar($id['id']); |
|
2463 | - } |
|
2464 | - } |
|
2465 | - |
|
2466 | - /** |
|
2467 | - * @param $subscriptionId |
|
2468 | - */ |
|
2469 | - public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
2470 | - $query = $this->db->getQueryBuilder(); |
|
2471 | - $query->delete('calendarsubscrobjects') |
|
2472 | - ->where($query->expr()->eq('subscriptionid', $query->createNamedParameter($subscriptionId))); |
|
2473 | - $query->execute(); |
|
2474 | - } |
|
2475 | - |
|
2476 | - /** |
|
2477 | - * @param $subscriptionId |
|
2478 | - * @param $objectUri |
|
2479 | - * @param $calendarData |
|
2480 | - * @return string |
|
2481 | - * @throws DAV\Exception\BadRequest |
|
2482 | - */ |
|
2483 | - public function addCachedEvent($subscriptionId, $objectUri, $calendarData) { |
|
2484 | - $extraData = $this->getDenormalizedData($calendarData); |
|
2485 | - |
|
2486 | - $query = $this->db->getQueryBuilder(); |
|
2487 | - $query->insert('calendarsubscrobjects') |
|
2488 | - ->values([ |
|
2489 | - 'subscriptionid' => $query->createNamedParameter($subscriptionId), |
|
2490 | - 'uri' => $query->createNamedParameter($objectUri), |
|
2491 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
2492 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
2493 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
2494 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
2495 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
2496 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
2497 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
2498 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
2499 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
2500 | - ]) |
|
2501 | - ->execute(); |
|
2502 | - } |
|
2503 | - |
|
2504 | - /** |
|
2505 | - * read VCalendar data into a VCalendar object |
|
2506 | - * |
|
2507 | - * @param string $objectData |
|
2508 | - * @return VCalendar |
|
2509 | - */ |
|
2510 | - protected function readCalendarData($objectData) { |
|
2511 | - return Reader::read($objectData); |
|
2512 | - } |
|
2513 | - |
|
2514 | - /** |
|
2515 | - * delete all properties from a given calendar object |
|
2516 | - * |
|
2517 | - * @param int $calendarId |
|
2518 | - * @param int $objectId |
|
2519 | - */ |
|
2520 | - protected function purgeProperties($calendarId, $objectId) { |
|
2521 | - $query = $this->db->getQueryBuilder(); |
|
2522 | - $query->delete($this->dbObjectPropertiesTable) |
|
2523 | - ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
2524 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2525 | - $query->execute(); |
|
2526 | - } |
|
2527 | - |
|
2528 | - /** |
|
2529 | - * get ID from a given calendar object |
|
2530 | - * |
|
2531 | - * @param int $calendarId |
|
2532 | - * @param string $uri |
|
2533 | - * @return int |
|
2534 | - */ |
|
2535 | - protected function getCalendarObjectId($calendarId, $uri) { |
|
2536 | - $query = $this->db->getQueryBuilder(); |
|
2537 | - $query->select('id')->from('calendarobjects') |
|
2538 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
2539 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2540 | - |
|
2541 | - $result = $query->execute(); |
|
2542 | - $objectIds = $result->fetch(); |
|
2543 | - $result->closeCursor(); |
|
2544 | - |
|
2545 | - if (!isset($objectIds['id'])) { |
|
2546 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
2547 | - } |
|
2548 | - |
|
2549 | - return (int)$objectIds['id']; |
|
2550 | - } |
|
2551 | - |
|
2552 | - /** |
|
2553 | - * return legacy endpoint principal name to new principal name |
|
2554 | - * |
|
2555 | - * @param $principalUri |
|
2556 | - * @param $toV2 |
|
2557 | - * @return string |
|
2558 | - */ |
|
2559 | - private function convertPrincipal($principalUri, $toV2) { |
|
2560 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
2561 | - list(, $name) = Uri\split($principalUri); |
|
2562 | - if ($toV2 === true) { |
|
2563 | - return "principals/users/$name"; |
|
2564 | - } |
|
2565 | - return "principals/$name"; |
|
2566 | - } |
|
2567 | - return $principalUri; |
|
2568 | - } |
|
2569 | - |
|
2570 | - /** |
|
2571 | - * adds information about an owner to the calendar data |
|
2572 | - * |
|
2573 | - * @param $calendarInfo |
|
2574 | - */ |
|
2575 | - private function addOwnerPrincipal(&$calendarInfo) { |
|
2576 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
2577 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
2578 | - if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
2579 | - $uri = $calendarInfo[$ownerPrincipalKey]; |
|
2580 | - } else { |
|
2581 | - $uri = $calendarInfo['principaluri']; |
|
2582 | - } |
|
2583 | - |
|
2584 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
2585 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
2586 | - $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
2587 | - } |
|
2588 | - } |
|
442 | + /** |
|
443 | + * @return array |
|
444 | + */ |
|
445 | + public function getPublicCalendars() { |
|
446 | + $fields = array_values($this->propertyMap); |
|
447 | + $fields[] = 'a.id'; |
|
448 | + $fields[] = 'a.uri'; |
|
449 | + $fields[] = 'a.synctoken'; |
|
450 | + $fields[] = 'a.components'; |
|
451 | + $fields[] = 'a.principaluri'; |
|
452 | + $fields[] = 'a.transparent'; |
|
453 | + $fields[] = 's.access'; |
|
454 | + $fields[] = 's.publicuri'; |
|
455 | + $calendars = []; |
|
456 | + $query = $this->db->getQueryBuilder(); |
|
457 | + $result = $query->select($fields) |
|
458 | + ->from('dav_shares', 's') |
|
459 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
460 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
461 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
462 | + ->execute(); |
|
463 | + |
|
464 | + while($row = $result->fetch()) { |
|
465 | + list(, $name) = Uri\split($row['principaluri']); |
|
466 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
467 | + $components = []; |
|
468 | + if ($row['components']) { |
|
469 | + $components = explode(',',$row['components']); |
|
470 | + } |
|
471 | + $calendar = [ |
|
472 | + 'id' => $row['id'], |
|
473 | + 'uri' => $row['publicuri'], |
|
474 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
475 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
476 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
477 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
478 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
479 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
480 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
481 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
482 | + ]; |
|
483 | + |
|
484 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
485 | + $calendar[$xmlName] = $row[$dbName]; |
|
486 | + } |
|
487 | + |
|
488 | + $this->addOwnerPrincipal($calendar); |
|
489 | + |
|
490 | + if (!isset($calendars[$calendar['id']])) { |
|
491 | + $calendars[$calendar['id']] = $calendar; |
|
492 | + } |
|
493 | + } |
|
494 | + $result->closeCursor(); |
|
495 | + |
|
496 | + return array_values($calendars); |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * @param string $uri |
|
501 | + * @return array |
|
502 | + * @throws NotFound |
|
503 | + */ |
|
504 | + public function getPublicCalendar($uri) { |
|
505 | + $fields = array_values($this->propertyMap); |
|
506 | + $fields[] = 'a.id'; |
|
507 | + $fields[] = 'a.uri'; |
|
508 | + $fields[] = 'a.synctoken'; |
|
509 | + $fields[] = 'a.components'; |
|
510 | + $fields[] = 'a.principaluri'; |
|
511 | + $fields[] = 'a.transparent'; |
|
512 | + $fields[] = 's.access'; |
|
513 | + $fields[] = 's.publicuri'; |
|
514 | + $query = $this->db->getQueryBuilder(); |
|
515 | + $result = $query->select($fields) |
|
516 | + ->from('dav_shares', 's') |
|
517 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
518 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
519 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
520 | + ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
521 | + ->execute(); |
|
522 | + |
|
523 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
524 | + |
|
525 | + $result->closeCursor(); |
|
526 | + |
|
527 | + if ($row === false) { |
|
528 | + throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
529 | + } |
|
530 | + |
|
531 | + list(, $name) = Uri\split($row['principaluri']); |
|
532 | + $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
533 | + $components = []; |
|
534 | + if ($row['components']) { |
|
535 | + $components = explode(',',$row['components']); |
|
536 | + } |
|
537 | + $calendar = [ |
|
538 | + 'id' => $row['id'], |
|
539 | + 'uri' => $row['publicuri'], |
|
540 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
541 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
542 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
543 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
544 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
545 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
546 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
547 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
548 | + ]; |
|
549 | + |
|
550 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
551 | + $calendar[$xmlName] = $row[$dbName]; |
|
552 | + } |
|
553 | + |
|
554 | + $this->addOwnerPrincipal($calendar); |
|
555 | + |
|
556 | + return $calendar; |
|
557 | + |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * @param string $principal |
|
562 | + * @param string $uri |
|
563 | + * @return array|null |
|
564 | + */ |
|
565 | + public function getCalendarByUri($principal, $uri) { |
|
566 | + $fields = array_values($this->propertyMap); |
|
567 | + $fields[] = 'id'; |
|
568 | + $fields[] = 'uri'; |
|
569 | + $fields[] = 'synctoken'; |
|
570 | + $fields[] = 'components'; |
|
571 | + $fields[] = 'principaluri'; |
|
572 | + $fields[] = 'transparent'; |
|
573 | + |
|
574 | + // Making fields a comma-delimited list |
|
575 | + $query = $this->db->getQueryBuilder(); |
|
576 | + $query->select($fields)->from('calendars') |
|
577 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
578 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
579 | + ->setMaxResults(1); |
|
580 | + $stmt = $query->execute(); |
|
581 | + |
|
582 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
583 | + $stmt->closeCursor(); |
|
584 | + if ($row === false) { |
|
585 | + return null; |
|
586 | + } |
|
587 | + |
|
588 | + $components = []; |
|
589 | + if ($row['components']) { |
|
590 | + $components = explode(',',$row['components']); |
|
591 | + } |
|
592 | + |
|
593 | + $calendar = [ |
|
594 | + 'id' => $row['id'], |
|
595 | + 'uri' => $row['uri'], |
|
596 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
597 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
598 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
599 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
600 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
601 | + ]; |
|
602 | + |
|
603 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
604 | + $calendar[$xmlName] = $row[$dbName]; |
|
605 | + } |
|
606 | + |
|
607 | + $this->addOwnerPrincipal($calendar); |
|
608 | + |
|
609 | + return $calendar; |
|
610 | + } |
|
611 | + |
|
612 | + /** |
|
613 | + * @param $calendarId |
|
614 | + * @return array|null |
|
615 | + */ |
|
616 | + public function getCalendarById($calendarId) { |
|
617 | + $fields = array_values($this->propertyMap); |
|
618 | + $fields[] = 'id'; |
|
619 | + $fields[] = 'uri'; |
|
620 | + $fields[] = 'synctoken'; |
|
621 | + $fields[] = 'components'; |
|
622 | + $fields[] = 'principaluri'; |
|
623 | + $fields[] = 'transparent'; |
|
624 | + |
|
625 | + // Making fields a comma-delimited list |
|
626 | + $query = $this->db->getQueryBuilder(); |
|
627 | + $query->select($fields)->from('calendars') |
|
628 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
629 | + ->setMaxResults(1); |
|
630 | + $stmt = $query->execute(); |
|
631 | + |
|
632 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
633 | + $stmt->closeCursor(); |
|
634 | + if ($row === false) { |
|
635 | + return null; |
|
636 | + } |
|
637 | + |
|
638 | + $components = []; |
|
639 | + if ($row['components']) { |
|
640 | + $components = explode(',',$row['components']); |
|
641 | + } |
|
642 | + |
|
643 | + $calendar = [ |
|
644 | + 'id' => $row['id'], |
|
645 | + 'uri' => $row['uri'], |
|
646 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
647 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
648 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
649 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
650 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
651 | + ]; |
|
652 | + |
|
653 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
654 | + $calendar[$xmlName] = $row[$dbName]; |
|
655 | + } |
|
656 | + |
|
657 | + $this->addOwnerPrincipal($calendar); |
|
658 | + |
|
659 | + return $calendar; |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * @param $subscriptionId |
|
664 | + */ |
|
665 | + public function getSubscriptionById($subscriptionId) { |
|
666 | + $fields = array_values($this->subscriptionPropertyMap); |
|
667 | + $fields[] = 'id'; |
|
668 | + $fields[] = 'uri'; |
|
669 | + $fields[] = 'source'; |
|
670 | + $fields[] = 'principaluri'; |
|
671 | + $fields[] = 'lastmodified'; |
|
672 | + |
|
673 | + $query = $this->db->getQueryBuilder(); |
|
674 | + $query->select($fields) |
|
675 | + ->from('calendarsubscriptions') |
|
676 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
677 | + ->orderBy('calendarorder', 'asc'); |
|
678 | + $stmt =$query->execute(); |
|
679 | + |
|
680 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
681 | + $stmt->closeCursor(); |
|
682 | + if ($row === false) { |
|
683 | + return null; |
|
684 | + } |
|
685 | + |
|
686 | + $subscription = [ |
|
687 | + 'id' => $row['id'], |
|
688 | + 'uri' => $row['uri'], |
|
689 | + 'principaluri' => $row['principaluri'], |
|
690 | + 'source' => $row['source'], |
|
691 | + 'lastmodified' => $row['lastmodified'], |
|
692 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
693 | + ]; |
|
694 | + |
|
695 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
696 | + if (!is_null($row[$dbName])) { |
|
697 | + $subscription[$xmlName] = $row[$dbName]; |
|
698 | + } |
|
699 | + } |
|
700 | + |
|
701 | + return $subscription; |
|
702 | + } |
|
703 | + |
|
704 | + /** |
|
705 | + * Creates a new calendar for a principal. |
|
706 | + * |
|
707 | + * If the creation was a success, an id must be returned that can be used to reference |
|
708 | + * this calendar in other methods, such as updateCalendar. |
|
709 | + * |
|
710 | + * @param string $principalUri |
|
711 | + * @param string $calendarUri |
|
712 | + * @param array $properties |
|
713 | + * @return int |
|
714 | + * @suppress SqlInjectionChecker |
|
715 | + */ |
|
716 | + function createCalendar($principalUri, $calendarUri, array $properties) { |
|
717 | + $values = [ |
|
718 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
719 | + 'uri' => $calendarUri, |
|
720 | + 'synctoken' => 1, |
|
721 | + 'transparent' => 0, |
|
722 | + 'components' => 'VEVENT,VTODO', |
|
723 | + 'displayname' => $calendarUri |
|
724 | + ]; |
|
725 | + |
|
726 | + // Default value |
|
727 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
728 | + if (isset($properties[$sccs])) { |
|
729 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
730 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
731 | + } |
|
732 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
733 | + } |
|
734 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
735 | + if (isset($properties[$transp])) { |
|
736 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
737 | + } |
|
738 | + |
|
739 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
740 | + if (isset($properties[$xmlName])) { |
|
741 | + $values[$dbName] = $properties[$xmlName]; |
|
742 | + } |
|
743 | + } |
|
744 | + |
|
745 | + $query = $this->db->getQueryBuilder(); |
|
746 | + $query->insert('calendars'); |
|
747 | + foreach($values as $column => $value) { |
|
748 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
749 | + } |
|
750 | + $query->execute(); |
|
751 | + $calendarId = $query->getLastInsertId(); |
|
752 | + |
|
753 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( |
|
754 | + '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', |
|
755 | + [ |
|
756 | + 'calendarId' => $calendarId, |
|
757 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
758 | + ])); |
|
759 | + |
|
760 | + return $calendarId; |
|
761 | + } |
|
762 | + |
|
763 | + /** |
|
764 | + * Updates properties for a calendar. |
|
765 | + * |
|
766 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
767 | + * To do the actual updates, you must tell this object which properties |
|
768 | + * you're going to process with the handle() method. |
|
769 | + * |
|
770 | + * Calling the handle method is like telling the PropPatch object "I |
|
771 | + * promise I can handle updating this property". |
|
772 | + * |
|
773 | + * Read the PropPatch documentation for more info and examples. |
|
774 | + * |
|
775 | + * @param mixed $calendarId |
|
776 | + * @param PropPatch $propPatch |
|
777 | + * @return void |
|
778 | + */ |
|
779 | + function updateCalendar($calendarId, PropPatch $propPatch) { |
|
780 | + $supportedProperties = array_keys($this->propertyMap); |
|
781 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
782 | + |
|
783 | + /** |
|
784 | + * @suppress SqlInjectionChecker |
|
785 | + */ |
|
786 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
787 | + $newValues = []; |
|
788 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
789 | + |
|
790 | + switch ($propertyName) { |
|
791 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
792 | + $fieldName = 'transparent'; |
|
793 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
794 | + break; |
|
795 | + default : |
|
796 | + $fieldName = $this->propertyMap[$propertyName]; |
|
797 | + $newValues[$fieldName] = $propertyValue; |
|
798 | + break; |
|
799 | + } |
|
800 | + |
|
801 | + } |
|
802 | + $query = $this->db->getQueryBuilder(); |
|
803 | + $query->update('calendars'); |
|
804 | + foreach ($newValues as $fieldName => $value) { |
|
805 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
806 | + } |
|
807 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
808 | + $query->execute(); |
|
809 | + |
|
810 | + $this->addChange($calendarId, "", 2); |
|
811 | + |
|
812 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( |
|
813 | + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', |
|
814 | + [ |
|
815 | + 'calendarId' => $calendarId, |
|
816 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
817 | + 'shares' => $this->getShares($calendarId), |
|
818 | + 'propertyMutations' => $mutations, |
|
819 | + ])); |
|
820 | + |
|
821 | + return true; |
|
822 | + }); |
|
823 | + } |
|
824 | + |
|
825 | + /** |
|
826 | + * Delete a calendar and all it's objects |
|
827 | + * |
|
828 | + * @param mixed $calendarId |
|
829 | + * @return void |
|
830 | + */ |
|
831 | + function deleteCalendar($calendarId) { |
|
832 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent( |
|
833 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', |
|
834 | + [ |
|
835 | + 'calendarId' => $calendarId, |
|
836 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
837 | + 'shares' => $this->getShares($calendarId), |
|
838 | + ])); |
|
839 | + |
|
840 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
841 | + $stmt->execute([$calendarId]); |
|
842 | + |
|
843 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
844 | + $stmt->execute([$calendarId]); |
|
845 | + |
|
846 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
847 | + $stmt->execute([$calendarId]); |
|
848 | + |
|
849 | + $this->sharingBackend->deleteAllShares($calendarId); |
|
850 | + |
|
851 | + $query = $this->db->getQueryBuilder(); |
|
852 | + $query->delete($this->dbObjectPropertiesTable) |
|
853 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
854 | + ->execute(); |
|
855 | + } |
|
856 | + |
|
857 | + /** |
|
858 | + * Delete all of an user's shares |
|
859 | + * |
|
860 | + * @param string $principaluri |
|
861 | + * @return void |
|
862 | + */ |
|
863 | + function deleteAllSharesByUser($principaluri) { |
|
864 | + $this->sharingBackend->deleteAllSharesByUser($principaluri); |
|
865 | + } |
|
866 | + |
|
867 | + /** |
|
868 | + * Returns all calendar objects within a calendar. |
|
869 | + * |
|
870 | + * Every item contains an array with the following keys: |
|
871 | + * * calendardata - The iCalendar-compatible calendar data |
|
872 | + * * uri - a unique key which will be used to construct the uri. This can |
|
873 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
874 | + * good idea. This is only the basename, or filename, not the full |
|
875 | + * path. |
|
876 | + * * lastmodified - a timestamp of the last modification time |
|
877 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
878 | + * '"abcdef"') |
|
879 | + * * size - The size of the calendar objects, in bytes. |
|
880 | + * * component - optional, a string containing the type of object, such |
|
881 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
882 | + * the Content-Type header. |
|
883 | + * |
|
884 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
885 | + * speed reasons. |
|
886 | + * |
|
887 | + * The calendardata is also optional. If it's not returned |
|
888 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
889 | + * calendardata. |
|
890 | + * |
|
891 | + * If neither etag or size are specified, the calendardata will be |
|
892 | + * used/fetched to determine these numbers. If both are specified the |
|
893 | + * amount of times this is needed is reduced by a great degree. |
|
894 | + * |
|
895 | + * @param mixed $calendarId |
|
896 | + * @return array |
|
897 | + */ |
|
898 | + function getCalendarObjects($calendarId):array { |
|
899 | + return $this->commonGetCalendarObjects('calendarobjects', 'calendarid', $calendarId); |
|
900 | + } |
|
901 | + |
|
902 | + /** |
|
903 | + * @param mixed $subscriptionId |
|
904 | + * @return array |
|
905 | + */ |
|
906 | + public function getCachedCalendarObjects($subscriptionId):array { |
|
907 | + return $this->commonGetCalendarObjects('calendarsubscrobjects', 'subscriptionid', $subscriptionId); |
|
908 | + } |
|
909 | + |
|
910 | + /** |
|
911 | + * @param string $tablename |
|
912 | + * @param string $selector |
|
913 | + * @param mixed $id |
|
914 | + * @return array |
|
915 | + */ |
|
916 | + private function commonGetCalendarObjects(string $tablename, string $selector, $id):array { |
|
917 | + $query = $this->db->getQueryBuilder(); |
|
918 | + $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'componenttype', 'classification']) |
|
919 | + ->from($tablename) |
|
920 | + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))); |
|
921 | + $stmt = $query->execute(); |
|
922 | + |
|
923 | + $result = []; |
|
924 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
925 | + $result[] = [ |
|
926 | + 'id' => $row['id'], |
|
927 | + 'uri' => $row['uri'], |
|
928 | + 'lastmodified' => $row['lastmodified'], |
|
929 | + 'etag' => '"' . $row['etag'] . '"', |
|
930 | + 'calendarid' => $row[$selector], |
|
931 | + 'size' => (int)$row['size'], |
|
932 | + 'component' => strtolower($row['componenttype']), |
|
933 | + 'classification'=> (int)$row['classification'] |
|
934 | + ]; |
|
935 | + } |
|
936 | + |
|
937 | + return $result; |
|
938 | + } |
|
939 | + |
|
940 | + /** |
|
941 | + * Returns information from a single calendar object, based on it's object |
|
942 | + * uri. |
|
943 | + * |
|
944 | + * The object uri is only the basename, or filename and not a full path. |
|
945 | + * |
|
946 | + * The returned array must have the same keys as getCalendarObjects. The |
|
947 | + * 'calendardata' object is required here though, while it's not required |
|
948 | + * for getCalendarObjects. |
|
949 | + * |
|
950 | + * This method must return null if the object did not exist. |
|
951 | + * |
|
952 | + * @param mixed $calendarId |
|
953 | + * @param string $objectUri |
|
954 | + * @return array|null |
|
955 | + */ |
|
956 | + function getCalendarObject($calendarId, $objectUri) { |
|
957 | + return $this->commonGetCalendarObject('calendarobjects', 'calendarid', $calendarId, $objectUri); |
|
958 | + } |
|
959 | + |
|
960 | + /** |
|
961 | + * @param $subscriptionId |
|
962 | + * @param $objectUri |
|
963 | + * @return array |
|
964 | + */ |
|
965 | + public function getCachedCalendarObject($subscriptionId, $objectUri) { |
|
966 | + return $this->commonGetCalendarObject('calendarsubscrobjects', 'subscriptionid', $subscriptionId, $objectUri); |
|
967 | + } |
|
968 | + |
|
969 | + /** |
|
970 | + * @param string $tablename |
|
971 | + * @param string $selector |
|
972 | + * @param $id |
|
973 | + * @param $objectUri |
|
974 | + * @return array |
|
975 | + */ |
|
976 | + private function commonGetCalendarObject(string $tablename, string $selector, $id, $objectUri):array { |
|
977 | + $query = $this->db->getQueryBuilder(); |
|
978 | + $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'calendardata', 'componenttype', 'classification']) |
|
979 | + ->from($tablename) |
|
980 | + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))) |
|
981 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
982 | + $stmt = $query->execute(); |
|
983 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
984 | + |
|
985 | + if(!$row) { |
|
986 | + return null; |
|
987 | + } |
|
988 | + |
|
989 | + return [ |
|
990 | + 'id' => $row['id'], |
|
991 | + 'uri' => $row['uri'], |
|
992 | + 'lastmodified' => $row['lastmodified'], |
|
993 | + 'etag' => '"' . $row['etag'] . '"', |
|
994 | + 'calendarid' => $row[$selector], |
|
995 | + 'size' => (int)$row['size'], |
|
996 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
997 | + 'component' => strtolower($row['componenttype']), |
|
998 | + 'classification'=> (int)$row['classification'] |
|
999 | + ]; |
|
1000 | + } |
|
1001 | + |
|
1002 | + /** |
|
1003 | + * Returns a list of calendar objects. |
|
1004 | + * |
|
1005 | + * This method should work identical to getCalendarObject, but instead |
|
1006 | + * return all the calendar objects in the list as an array. |
|
1007 | + * |
|
1008 | + * If the backend supports this, it may allow for some speed-ups. |
|
1009 | + * |
|
1010 | + * @param mixed $calendarId |
|
1011 | + * @param string[] $uris |
|
1012 | + * @return array |
|
1013 | + */ |
|
1014 | + function getMultipleCalendarObjects($calendarId, array $uris) { |
|
1015 | + return $this->commonGetMultipleCachedCalendarObjects('calendarobjects', 'calendarid', $calendarId, $uris); |
|
1016 | + } |
|
1017 | + |
|
1018 | + /** |
|
1019 | + * @param $subscripionId |
|
1020 | + * @param array $uris |
|
1021 | + * @return array |
|
1022 | + */ |
|
1023 | + function getMultipleCachedCalendarObjects($subscripionId, array $uris) { |
|
1024 | + return $this->commonGetMultipleCachedCalendarObjects('calendarsubscrobjects', 'subscriptionid', $subscripionId, $uris); |
|
1025 | + } |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * @param string $tablename |
|
1029 | + * @param string $selector |
|
1030 | + * @param $id |
|
1031 | + * @param array $uris |
|
1032 | + * @return array |
|
1033 | + */ |
|
1034 | + private function commonGetMultipleCachedCalendarObjects(string $tablename, string $selector, $id, array $uris) { |
|
1035 | + if (empty($uris)) { |
|
1036 | + return []; |
|
1037 | + } |
|
1038 | + |
|
1039 | + $chunks = array_chunk($uris, 100); |
|
1040 | + $objects = []; |
|
1041 | + |
|
1042 | + $query = $this->db->getQueryBuilder(); |
|
1043 | + $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'calendardata', 'componenttype', 'classification']) |
|
1044 | + ->from($tablename) |
|
1045 | + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))) |
|
1046 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
1047 | + |
|
1048 | + foreach ($chunks as $uris) { |
|
1049 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
1050 | + $result = $query->execute(); |
|
1051 | + |
|
1052 | + while ($row = $result->fetch()) { |
|
1053 | + $objects[] = [ |
|
1054 | + 'id' => $row['id'], |
|
1055 | + 'uri' => $row['uri'], |
|
1056 | + 'lastmodified' => $row['lastmodified'], |
|
1057 | + 'etag' => '"' . $row['etag'] . '"', |
|
1058 | + 'calendarid' => $row[$selector], |
|
1059 | + 'size' => (int)$row['size'], |
|
1060 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
1061 | + 'component' => strtolower($row['componenttype']), |
|
1062 | + 'classification' => (int)$row['classification'] |
|
1063 | + ]; |
|
1064 | + } |
|
1065 | + $result->closeCursor(); |
|
1066 | + } |
|
1067 | + |
|
1068 | + return $objects; |
|
1069 | + } |
|
1070 | + |
|
1071 | + /** |
|
1072 | + * Creates a new calendar object. |
|
1073 | + * |
|
1074 | + * The object uri is only the basename, or filename and not a full path. |
|
1075 | + * |
|
1076 | + * It is possible return an etag from this function, which will be used in |
|
1077 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
1078 | + * by double-quotes. |
|
1079 | + * |
|
1080 | + * However, you should only really return this ETag if you don't mangle the |
|
1081 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
1082 | + * the exact same as this request body, you should omit the ETag. |
|
1083 | + * |
|
1084 | + * @param mixed $calendarId |
|
1085 | + * @param string $objectUri |
|
1086 | + * @param string $calendarData |
|
1087 | + * @return string |
|
1088 | + */ |
|
1089 | + function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
1090 | + $extraData = $this->getDenormalizedData($calendarData); |
|
1091 | + |
|
1092 | + $q = $this->db->getQueryBuilder(); |
|
1093 | + $q->select($q->createFunction('COUNT(*)')) |
|
1094 | + ->from('calendarobjects') |
|
1095 | + ->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId))) |
|
1096 | + ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid']))); |
|
1097 | + |
|
1098 | + $result = $q->execute(); |
|
1099 | + $count = (int) $result->fetchColumn(); |
|
1100 | + $result->closeCursor(); |
|
1101 | + |
|
1102 | + if ($count !== 0) { |
|
1103 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
1104 | + } |
|
1105 | + |
|
1106 | + $query = $this->db->getQueryBuilder(); |
|
1107 | + $query->insert('calendarobjects') |
|
1108 | + ->values([ |
|
1109 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
1110 | + 'uri' => $query->createNamedParameter($objectUri), |
|
1111 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
1112 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
1113 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
1114 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
1115 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
1116 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
1117 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
1118 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
1119 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
1120 | + ]) |
|
1121 | + ->execute(); |
|
1122 | + |
|
1123 | + $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
1124 | + |
|
1125 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent( |
|
1126 | + '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', |
|
1127 | + [ |
|
1128 | + 'calendarId' => $calendarId, |
|
1129 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1130 | + 'shares' => $this->getShares($calendarId), |
|
1131 | + 'objectData' => $this->getCalendarObject($calendarId, $objectUri), |
|
1132 | + ] |
|
1133 | + )); |
|
1134 | + $this->addChange($calendarId, $objectUri, 1); |
|
1135 | + |
|
1136 | + return '"' . $extraData['etag'] . '"'; |
|
1137 | + } |
|
1138 | + |
|
1139 | + /** |
|
1140 | + * Updates an existing calendarobject, based on it's uri. |
|
1141 | + * |
|
1142 | + * The object uri is only the basename, or filename and not a full path. |
|
1143 | + * |
|
1144 | + * It is possible return an etag from this function, which will be used in |
|
1145 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
1146 | + * by double-quotes. |
|
1147 | + * |
|
1148 | + * However, you should only really return this ETag if you don't mangle the |
|
1149 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
1150 | + * the exact same as this request body, you should omit the ETag. |
|
1151 | + * |
|
1152 | + * @param mixed $calendarId |
|
1153 | + * @param string $objectUri |
|
1154 | + * @param string $calendarData |
|
1155 | + * @return string |
|
1156 | + */ |
|
1157 | + function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
1158 | + $extraData = $this->getDenormalizedData($calendarData); |
|
1159 | + |
|
1160 | + $query = $this->db->getQueryBuilder(); |
|
1161 | + $query->update('calendarobjects') |
|
1162 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
1163 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
1164 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
1165 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
1166 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
1167 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
1168 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
1169 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
1170 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
1171 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
1172 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
1173 | + ->execute(); |
|
1174 | + |
|
1175 | + $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
1176 | + |
|
1177 | + $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1178 | + if (is_array($data)) { |
|
1179 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent( |
|
1180 | + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', |
|
1181 | + [ |
|
1182 | + 'calendarId' => $calendarId, |
|
1183 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1184 | + 'shares' => $this->getShares($calendarId), |
|
1185 | + 'objectData' => $data, |
|
1186 | + ] |
|
1187 | + )); |
|
1188 | + } |
|
1189 | + $this->addChange($calendarId, $objectUri, 2); |
|
1190 | + |
|
1191 | + return '"' . $extraData['etag'] . '"'; |
|
1192 | + } |
|
1193 | + |
|
1194 | + /** |
|
1195 | + * @param int $calendarObjectId |
|
1196 | + * @param int $classification |
|
1197 | + */ |
|
1198 | + public function setClassification($calendarObjectId, $classification) { |
|
1199 | + if (!in_array($classification, [ |
|
1200 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
1201 | + ])) { |
|
1202 | + throw new \InvalidArgumentException(); |
|
1203 | + } |
|
1204 | + $query = $this->db->getQueryBuilder(); |
|
1205 | + $query->update('calendarobjects') |
|
1206 | + ->set('classification', $query->createNamedParameter($classification)) |
|
1207 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
1208 | + ->execute(); |
|
1209 | + } |
|
1210 | + |
|
1211 | + /** |
|
1212 | + * Deletes an existing calendar object. |
|
1213 | + * |
|
1214 | + * The object uri is only the basename, or filename and not a full path. |
|
1215 | + * |
|
1216 | + * @param mixed $calendarId |
|
1217 | + * @param string $objectUri |
|
1218 | + * @return void |
|
1219 | + */ |
|
1220 | + function deleteCalendarObject($calendarId, $objectUri) { |
|
1221 | + $data = $this->getCalendarObject($calendarId, $objectUri); |
|
1222 | + if (is_array($data)) { |
|
1223 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( |
|
1224 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', |
|
1225 | + [ |
|
1226 | + 'calendarId' => $calendarId, |
|
1227 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
1228 | + 'shares' => $this->getShares($calendarId), |
|
1229 | + 'objectData' => $data, |
|
1230 | + ] |
|
1231 | + )); |
|
1232 | + } |
|
1233 | + |
|
1234 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
1235 | + $stmt->execute([$calendarId, $objectUri]); |
|
1236 | + |
|
1237 | + $this->purgeProperties($calendarId, $data['id']); |
|
1238 | + |
|
1239 | + $this->addChange($calendarId, $objectUri, 3); |
|
1240 | + } |
|
1241 | + |
|
1242 | + /** |
|
1243 | + * Performs a calendar-query on the contents of this calendar. |
|
1244 | + * |
|
1245 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
1246 | + * calendar-query it is possible for a client to request a specific set of |
|
1247 | + * object, based on contents of iCalendar properties, date-ranges and |
|
1248 | + * iCalendar component types (VTODO, VEVENT). |
|
1249 | + * |
|
1250 | + * This method should just return a list of (relative) urls that match this |
|
1251 | + * query. |
|
1252 | + * |
|
1253 | + * The list of filters are specified as an array. The exact array is |
|
1254 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
1255 | + * |
|
1256 | + * Note that it is extremely likely that getCalendarObject for every path |
|
1257 | + * returned from this method will be called almost immediately after. You |
|
1258 | + * may want to anticipate this to speed up these requests. |
|
1259 | + * |
|
1260 | + * This method provides a default implementation, which parses *all* the |
|
1261 | + * iCalendar objects in the specified calendar. |
|
1262 | + * |
|
1263 | + * This default may well be good enough for personal use, and calendars |
|
1264 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
1265 | + * or high loads, you are strongly advised to optimize certain paths. |
|
1266 | + * |
|
1267 | + * The best way to do so is override this method and to optimize |
|
1268 | + * specifically for 'common filters'. |
|
1269 | + * |
|
1270 | + * Requests that are extremely common are: |
|
1271 | + * * requests for just VEVENTS |
|
1272 | + * * requests for just VTODO |
|
1273 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
1274 | + * |
|
1275 | + * ..and combinations of these requests. It may not be worth it to try to |
|
1276 | + * handle every possible situation and just rely on the (relatively |
|
1277 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
1278 | + * |
|
1279 | + * Note that especially time-range-filters may be difficult to parse. A |
|
1280 | + * time-range filter specified on a VEVENT must for instance also handle |
|
1281 | + * recurrence rules correctly. |
|
1282 | + * A good example of how to interprete all these filters can also simply |
|
1283 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
1284 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
1285 | + * to think of. |
|
1286 | + * |
|
1287 | + * @param mixed $calendarId |
|
1288 | + * @param array $filters |
|
1289 | + * @return array |
|
1290 | + */ |
|
1291 | + function calendarQuery($calendarId, array $filters):array { |
|
1292 | + return $this->commonCalendarQuery('calendarobjects', 'calendarid', $calendarId, $filters); |
|
1293 | + } |
|
1294 | + |
|
1295 | + /** |
|
1296 | + * @param $subscriptionId |
|
1297 | + * @param array $filters |
|
1298 | + * @return array |
|
1299 | + */ |
|
1300 | + public function cachedCalendarQuery($subscriptionId, array $filters):array { |
|
1301 | + return $this->commonCalendarQuery('calendarsubscrobjects', 'subscriptionid', $subscriptionId, $filters); |
|
1302 | + } |
|
1303 | + |
|
1304 | + /** |
|
1305 | + * @param string $tablename |
|
1306 | + * @param string $selector |
|
1307 | + * @param $id |
|
1308 | + * @param array $filters |
|
1309 | + * @return array |
|
1310 | + */ |
|
1311 | + private function commonCalendarQuery(string $tablename, string $selector, $id, array $filters) { |
|
1312 | + $componentType = null; |
|
1313 | + $requirePostFilter = true; |
|
1314 | + $timeRange = null; |
|
1315 | + |
|
1316 | + // if no filters were specified, we don't need to filter after a query |
|
1317 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
1318 | + $requirePostFilter = false; |
|
1319 | + } |
|
1320 | + |
|
1321 | + // Figuring out if there's a component filter |
|
1322 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
1323 | + $componentType = $filters['comp-filters'][0]['name']; |
|
1324 | + |
|
1325 | + // Checking if we need post-filters |
|
1326 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
1327 | + $requirePostFilter = false; |
|
1328 | + } |
|
1329 | + // There was a time-range filter |
|
1330 | + if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
1331 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
1332 | + |
|
1333 | + // If start time OR the end time is not specified, we can do a |
|
1334 | + // 100% accurate mysql query. |
|
1335 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
1336 | + $requirePostFilter = false; |
|
1337 | + } |
|
1338 | + } |
|
1339 | + |
|
1340 | + } |
|
1341 | + $columns = ['uri']; |
|
1342 | + if ($requirePostFilter) { |
|
1343 | + $columns = ['uri', 'calendardata']; |
|
1344 | + } |
|
1345 | + $query = $this->db->getQueryBuilder(); |
|
1346 | + $query->select($columns) |
|
1347 | + ->from($tablename) |
|
1348 | + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))); |
|
1349 | + |
|
1350 | + if ($componentType) { |
|
1351 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
1352 | + } |
|
1353 | + |
|
1354 | + if ($timeRange && $timeRange['start']) { |
|
1355 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
1356 | + } |
|
1357 | + if ($timeRange && $timeRange['end']) { |
|
1358 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
1359 | + } |
|
1360 | + |
|
1361 | + $stmt = $query->execute(); |
|
1362 | + |
|
1363 | + $result = []; |
|
1364 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1365 | + if ($requirePostFilter) { |
|
1366 | + // validateFilterForObject will parse the calendar data |
|
1367 | + // catch parsing errors |
|
1368 | + try { |
|
1369 | + $matches = $this->validateFilterForObject($row, $filters); |
|
1370 | + } catch(ParseException $ex) { |
|
1371 | + $this->logger->logException($ex, [ |
|
1372 | + 'app' => 'dav', |
|
1373 | + 'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri'] |
|
1374 | + ]); |
|
1375 | + continue; |
|
1376 | + } catch (InvalidDataException $ex) { |
|
1377 | + $this->logger->logException($ex, [ |
|
1378 | + 'app' => 'dav', |
|
1379 | + 'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri'] |
|
1380 | + ]); |
|
1381 | + continue; |
|
1382 | + } |
|
1383 | + |
|
1384 | + if (!$matches) { |
|
1385 | + continue; |
|
1386 | + } |
|
1387 | + } |
|
1388 | + $result[] = $row['uri']; |
|
1389 | + } |
|
1390 | + |
|
1391 | + return $result; |
|
1392 | + } |
|
1393 | + |
|
1394 | + /** |
|
1395 | + * custom Nextcloud search extension for CalDAV |
|
1396 | + * |
|
1397 | + * @param string $principalUri |
|
1398 | + * @param array $filters |
|
1399 | + * @param integer|null $limit |
|
1400 | + * @param integer|null $offset |
|
1401 | + * @return array |
|
1402 | + */ |
|
1403 | + public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
1404 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
1405 | + $ownCalendars = []; |
|
1406 | + $sharedCalendars = []; |
|
1407 | + |
|
1408 | + $uriMapper = []; |
|
1409 | + |
|
1410 | + foreach($calendars as $calendar) { |
|
1411 | + if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
1412 | + $ownCalendars[] = $calendar['id']; |
|
1413 | + } else { |
|
1414 | + $sharedCalendars[] = $calendar['id']; |
|
1415 | + } |
|
1416 | + $uriMapper[$calendar['id']] = $calendar['uri']; |
|
1417 | + } |
|
1418 | + if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
1419 | + return []; |
|
1420 | + } |
|
1421 | + |
|
1422 | + $query = $this->db->getQueryBuilder(); |
|
1423 | + // Calendar id expressions |
|
1424 | + $calendarExpressions = []; |
|
1425 | + foreach($ownCalendars as $id) { |
|
1426 | + $calendarExpressions[] = $query->expr() |
|
1427 | + ->eq('c.calendarid', $query->createNamedParameter($id)); |
|
1428 | + } |
|
1429 | + foreach($sharedCalendars as $id) { |
|
1430 | + $calendarExpressions[] = $query->expr()->andX( |
|
1431 | + $query->expr()->eq('c.calendarid', |
|
1432 | + $query->createNamedParameter($id)), |
|
1433 | + $query->expr()->eq('c.classification', |
|
1434 | + $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)) |
|
1435 | + ); |
|
1436 | + } |
|
1437 | + |
|
1438 | + if (count($calendarExpressions) === 1) { |
|
1439 | + $calExpr = $calendarExpressions[0]; |
|
1440 | + } else { |
|
1441 | + $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
1442 | + } |
|
1443 | + |
|
1444 | + // Component expressions |
|
1445 | + $compExpressions = []; |
|
1446 | + foreach($filters['comps'] as $comp) { |
|
1447 | + $compExpressions[] = $query->expr() |
|
1448 | + ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
1449 | + } |
|
1450 | + |
|
1451 | + if (count($compExpressions) === 1) { |
|
1452 | + $compExpr = $compExpressions[0]; |
|
1453 | + } else { |
|
1454 | + $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
1455 | + } |
|
1456 | + |
|
1457 | + if (!isset($filters['props'])) { |
|
1458 | + $filters['props'] = []; |
|
1459 | + } |
|
1460 | + if (!isset($filters['params'])) { |
|
1461 | + $filters['params'] = []; |
|
1462 | + } |
|
1463 | + |
|
1464 | + $propParamExpressions = []; |
|
1465 | + foreach($filters['props'] as $prop) { |
|
1466 | + $propParamExpressions[] = $query->expr()->andX( |
|
1467 | + $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
1468 | + $query->expr()->isNull('i.parameter') |
|
1469 | + ); |
|
1470 | + } |
|
1471 | + foreach($filters['params'] as $param) { |
|
1472 | + $propParamExpressions[] = $query->expr()->andX( |
|
1473 | + $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
1474 | + $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
1475 | + ); |
|
1476 | + } |
|
1477 | + |
|
1478 | + if (count($propParamExpressions) === 1) { |
|
1479 | + $propParamExpr = $propParamExpressions[0]; |
|
1480 | + } else { |
|
1481 | + $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
1482 | + } |
|
1483 | + |
|
1484 | + $query->select(['c.calendarid', 'c.uri']) |
|
1485 | + ->from($this->dbObjectPropertiesTable, 'i') |
|
1486 | + ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
1487 | + ->where($calExpr) |
|
1488 | + ->andWhere($compExpr) |
|
1489 | + ->andWhere($propParamExpr) |
|
1490 | + ->andWhere($query->expr()->iLike('i.value', |
|
1491 | + $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))); |
|
1492 | + |
|
1493 | + if ($offset) { |
|
1494 | + $query->setFirstResult($offset); |
|
1495 | + } |
|
1496 | + if ($limit) { |
|
1497 | + $query->setMaxResults($limit); |
|
1498 | + } |
|
1499 | + |
|
1500 | + $stmt = $query->execute(); |
|
1501 | + |
|
1502 | + $result = []; |
|
1503 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1504 | + $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
1505 | + if (!in_array($path, $result)) { |
|
1506 | + $result[] = $path; |
|
1507 | + } |
|
1508 | + } |
|
1509 | + |
|
1510 | + return $result; |
|
1511 | + } |
|
1512 | + |
|
1513 | + /** |
|
1514 | + * used for Nextcloud's calendar API |
|
1515 | + * |
|
1516 | + * @param array $calendarInfo |
|
1517 | + * @param string $pattern |
|
1518 | + * @param array $searchProperties |
|
1519 | + * @param array $options |
|
1520 | + * @param integer|null $limit |
|
1521 | + * @param integer|null $offset |
|
1522 | + * |
|
1523 | + * @return array |
|
1524 | + */ |
|
1525 | + public function search(array $calendarInfo, $pattern, array $searchProperties, |
|
1526 | + array $options, $limit, $offset) { |
|
1527 | + $outerQuery = $this->db->getQueryBuilder(); |
|
1528 | + $innerQuery = $this->db->getQueryBuilder(); |
|
1529 | + |
|
1530 | + $innerQuery->selectDistinct('op.objectid') |
|
1531 | + ->from($this->dbObjectPropertiesTable, 'op') |
|
1532 | + ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
1533 | + $outerQuery->createNamedParameter($calendarInfo['id']))); |
|
1534 | + |
|
1535 | + // only return public items for shared calendars for now |
|
1536 | + if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
1537 | + $innerQuery->andWhere($innerQuery->expr()->eq('c.classification', |
|
1538 | + $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
1539 | + } |
|
1540 | + |
|
1541 | + $or = $innerQuery->expr()->orX(); |
|
1542 | + foreach($searchProperties as $searchProperty) { |
|
1543 | + $or->add($innerQuery->expr()->eq('op.name', |
|
1544 | + $outerQuery->createNamedParameter($searchProperty))); |
|
1545 | + } |
|
1546 | + $innerQuery->andWhere($or); |
|
1547 | + |
|
1548 | + if ($pattern !== '') { |
|
1549 | + $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
1550 | + $outerQuery->createNamedParameter('%' . |
|
1551 | + $this->db->escapeLikeParameter($pattern) . '%'))); |
|
1552 | + } |
|
1553 | + |
|
1554 | + $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
1555 | + ->from('calendarobjects', 'c'); |
|
1556 | + |
|
1557 | + if (isset($options['timerange'])) { |
|
1558 | + if (isset($options['timerange']['start'])) { |
|
1559 | + $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence', |
|
1560 | + $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp))); |
|
1561 | + |
|
1562 | + } |
|
1563 | + if (isset($options['timerange']['end'])) { |
|
1564 | + $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence', |
|
1565 | + $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp))); |
|
1566 | + } |
|
1567 | + } |
|
1568 | + |
|
1569 | + if (isset($options['types'])) { |
|
1570 | + $or = $outerQuery->expr()->orX(); |
|
1571 | + foreach($options['types'] as $type) { |
|
1572 | + $or->add($outerQuery->expr()->eq('componenttype', |
|
1573 | + $outerQuery->createNamedParameter($type))); |
|
1574 | + } |
|
1575 | + $outerQuery->andWhere($or); |
|
1576 | + } |
|
1577 | + |
|
1578 | + $outerQuery->andWhere($outerQuery->expr()->in('c.id', |
|
1579 | + $outerQuery->createFunction($innerQuery->getSQL()))); |
|
1580 | + |
|
1581 | + if ($offset) { |
|
1582 | + $outerQuery->setFirstResult($offset); |
|
1583 | + } |
|
1584 | + if ($limit) { |
|
1585 | + $outerQuery->setMaxResults($limit); |
|
1586 | + } |
|
1587 | + |
|
1588 | + $result = $outerQuery->execute(); |
|
1589 | + $calendarObjects = $result->fetchAll(); |
|
1590 | + |
|
1591 | + return array_map(function($o) { |
|
1592 | + $calendarData = Reader::read($o['calendardata']); |
|
1593 | + $comps = $calendarData->getComponents(); |
|
1594 | + $objects = []; |
|
1595 | + $timezones = []; |
|
1596 | + foreach($comps as $comp) { |
|
1597 | + if ($comp instanceof VTimeZone) { |
|
1598 | + $timezones[] = $comp; |
|
1599 | + } else { |
|
1600 | + $objects[] = $comp; |
|
1601 | + } |
|
1602 | + } |
|
1603 | + |
|
1604 | + return [ |
|
1605 | + 'id' => $o['id'], |
|
1606 | + 'type' => $o['componenttype'], |
|
1607 | + 'uid' => $o['uid'], |
|
1608 | + 'uri' => $o['uri'], |
|
1609 | + 'objects' => array_map(function($c) { |
|
1610 | + return $this->transformSearchData($c); |
|
1611 | + }, $objects), |
|
1612 | + 'timezones' => array_map(function($c) { |
|
1613 | + return $this->transformSearchData($c); |
|
1614 | + }, $timezones), |
|
1615 | + ]; |
|
1616 | + }, $calendarObjects); |
|
1617 | + } |
|
1618 | + |
|
1619 | + /** |
|
1620 | + * @param Component $comp |
|
1621 | + * @return array |
|
1622 | + */ |
|
1623 | + private function transformSearchData(Component $comp) { |
|
1624 | + $data = []; |
|
1625 | + /** @var Component[] $subComponents */ |
|
1626 | + $subComponents = $comp->getComponents(); |
|
1627 | + /** @var Property[] $properties */ |
|
1628 | + $properties = array_filter($comp->children(), function($c) { |
|
1629 | + return $c instanceof Property; |
|
1630 | + }); |
|
1631 | + $validationRules = $comp->getValidationRules(); |
|
1632 | + |
|
1633 | + foreach($subComponents as $subComponent) { |
|
1634 | + $name = $subComponent->name; |
|
1635 | + if (!isset($data[$name])) { |
|
1636 | + $data[$name] = []; |
|
1637 | + } |
|
1638 | + $data[$name][] = $this->transformSearchData($subComponent); |
|
1639 | + } |
|
1640 | + |
|
1641 | + foreach($properties as $property) { |
|
1642 | + $name = $property->name; |
|
1643 | + if (!isset($validationRules[$name])) { |
|
1644 | + $validationRules[$name] = '*'; |
|
1645 | + } |
|
1646 | + |
|
1647 | + $rule = $validationRules[$property->name]; |
|
1648 | + if ($rule === '+' || $rule === '*') { // multiple |
|
1649 | + if (!isset($data[$name])) { |
|
1650 | + $data[$name] = []; |
|
1651 | + } |
|
1652 | + |
|
1653 | + $data[$name][] = $this->transformSearchProperty($property); |
|
1654 | + } else { // once |
|
1655 | + $data[$name] = $this->transformSearchProperty($property); |
|
1656 | + } |
|
1657 | + } |
|
1658 | + |
|
1659 | + return $data; |
|
1660 | + } |
|
1661 | + |
|
1662 | + /** |
|
1663 | + * @param Property $prop |
|
1664 | + * @return array |
|
1665 | + */ |
|
1666 | + private function transformSearchProperty(Property $prop) { |
|
1667 | + // No need to check Date, as it extends DateTime |
|
1668 | + if ($prop instanceof Property\ICalendar\DateTime) { |
|
1669 | + $value = $prop->getDateTime(); |
|
1670 | + } else { |
|
1671 | + $value = $prop->getValue(); |
|
1672 | + } |
|
1673 | + |
|
1674 | + return [ |
|
1675 | + $value, |
|
1676 | + $prop->parameters() |
|
1677 | + ]; |
|
1678 | + } |
|
1679 | + |
|
1680 | + /** |
|
1681 | + * Searches through all of a users calendars and calendar objects to find |
|
1682 | + * an object with a specific UID. |
|
1683 | + * |
|
1684 | + * This method should return the path to this object, relative to the |
|
1685 | + * calendar home, so this path usually only contains two parts: |
|
1686 | + * |
|
1687 | + * calendarpath/objectpath.ics |
|
1688 | + * |
|
1689 | + * If the uid is not found, return null. |
|
1690 | + * |
|
1691 | + * This method should only consider * objects that the principal owns, so |
|
1692 | + * any calendars owned by other principals that also appear in this |
|
1693 | + * collection should be ignored. |
|
1694 | + * |
|
1695 | + * @param string $principalUri |
|
1696 | + * @param string $uid |
|
1697 | + * @return string|null |
|
1698 | + */ |
|
1699 | + function getCalendarObjectByUID($principalUri, $uid) { |
|
1700 | + |
|
1701 | + $query = $this->db->getQueryBuilder(); |
|
1702 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
1703 | + ->from('calendarobjects', 'co') |
|
1704 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
1705 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
1706 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
1707 | + |
|
1708 | + $stmt = $query->execute(); |
|
1709 | + |
|
1710 | + if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1711 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
1712 | + } |
|
1713 | + |
|
1714 | + return null; |
|
1715 | + } |
|
1716 | + |
|
1717 | + /** |
|
1718 | + * The getChanges method returns all the changes that have happened, since |
|
1719 | + * the specified syncToken in the specified calendar. |
|
1720 | + * |
|
1721 | + * This function should return an array, such as the following: |
|
1722 | + * |
|
1723 | + * [ |
|
1724 | + * 'syncToken' => 'The current synctoken', |
|
1725 | + * 'added' => [ |
|
1726 | + * 'new.txt', |
|
1727 | + * ], |
|
1728 | + * 'modified' => [ |
|
1729 | + * 'modified.txt', |
|
1730 | + * ], |
|
1731 | + * 'deleted' => [ |
|
1732 | + * 'foo.php.bak', |
|
1733 | + * 'old.txt' |
|
1734 | + * ] |
|
1735 | + * ); |
|
1736 | + * |
|
1737 | + * The returned syncToken property should reflect the *current* syncToken |
|
1738 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
1739 | + * property This is * needed here too, to ensure the operation is atomic. |
|
1740 | + * |
|
1741 | + * If the $syncToken argument is specified as null, this is an initial |
|
1742 | + * sync, and all members should be reported. |
|
1743 | + * |
|
1744 | + * The modified property is an array of nodenames that have changed since |
|
1745 | + * the last token. |
|
1746 | + * |
|
1747 | + * The deleted property is an array with nodenames, that have been deleted |
|
1748 | + * from collection. |
|
1749 | + * |
|
1750 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
1751 | + * 1, you only have to report changes that happened only directly in |
|
1752 | + * immediate descendants. If it's 2, it should also include changes from |
|
1753 | + * the nodes below the child collections. (grandchildren) |
|
1754 | + * |
|
1755 | + * The $limit argument allows a client to specify how many results should |
|
1756 | + * be returned at most. If the limit is not specified, it should be treated |
|
1757 | + * as infinite. |
|
1758 | + * |
|
1759 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
1760 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
1761 | + * |
|
1762 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
1763 | + * return null. |
|
1764 | + * |
|
1765 | + * The limit is 'suggestive'. You are free to ignore it. |
|
1766 | + * |
|
1767 | + * @param string $calendarId |
|
1768 | + * @param string $syncToken |
|
1769 | + * @param int $syncLevel |
|
1770 | + * @param int $limit |
|
1771 | + * @return array |
|
1772 | + */ |
|
1773 | + function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
1774 | + // Current synctoken |
|
1775 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
1776 | + $stmt->execute([ $calendarId ]); |
|
1777 | + $currentToken = $stmt->fetchColumn(0); |
|
1778 | + |
|
1779 | + if (is_null($currentToken)) { |
|
1780 | + return null; |
|
1781 | + } |
|
1782 | + |
|
1783 | + $result = [ |
|
1784 | + 'syncToken' => $currentToken, |
|
1785 | + 'added' => [], |
|
1786 | + 'modified' => [], |
|
1787 | + 'deleted' => [], |
|
1788 | + ]; |
|
1789 | + |
|
1790 | + if ($syncToken) { |
|
1791 | + |
|
1792 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
1793 | + if ($limit>0) { |
|
1794 | + $query.= " LIMIT " . (int)$limit; |
|
1795 | + } |
|
1796 | + |
|
1797 | + // Fetching all changes |
|
1798 | + $stmt = $this->db->prepare($query); |
|
1799 | + $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
1800 | + |
|
1801 | + $changes = []; |
|
1802 | + |
|
1803 | + // This loop ensures that any duplicates are overwritten, only the |
|
1804 | + // last change on a node is relevant. |
|
1805 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1806 | + |
|
1807 | + $changes[$row['uri']] = $row['operation']; |
|
1808 | + |
|
1809 | + } |
|
1810 | + |
|
1811 | + foreach($changes as $uri => $operation) { |
|
1812 | + |
|
1813 | + switch($operation) { |
|
1814 | + case 1 : |
|
1815 | + $result['added'][] = $uri; |
|
1816 | + break; |
|
1817 | + case 2 : |
|
1818 | + $result['modified'][] = $uri; |
|
1819 | + break; |
|
1820 | + case 3 : |
|
1821 | + $result['deleted'][] = $uri; |
|
1822 | + break; |
|
1823 | + } |
|
1824 | + |
|
1825 | + } |
|
1826 | + } else { |
|
1827 | + // No synctoken supplied, this is the initial sync. |
|
1828 | + $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
1829 | + $stmt = $this->db->prepare($query); |
|
1830 | + $stmt->execute([$calendarId]); |
|
1831 | + |
|
1832 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
1833 | + } |
|
1834 | + return $result; |
|
1835 | + |
|
1836 | + } |
|
1837 | + |
|
1838 | + /** |
|
1839 | + * Returns a list of subscriptions for a principal. |
|
1840 | + * |
|
1841 | + * Every subscription is an array with the following keys: |
|
1842 | + * * id, a unique id that will be used by other functions to modify the |
|
1843 | + * subscription. This can be the same as the uri or a database key. |
|
1844 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
1845 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
1846 | + * principalUri passed to this method. |
|
1847 | + * |
|
1848 | + * Furthermore, all the subscription info must be returned too: |
|
1849 | + * |
|
1850 | + * 1. {DAV:}displayname |
|
1851 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
1852 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
1853 | + * should not be stripped). |
|
1854 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
1855 | + * should not be stripped). |
|
1856 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
1857 | + * attachments should not be stripped). |
|
1858 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
1859 | + * Sabre\DAV\Property\Href). |
|
1860 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
1861 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
1862 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
1863 | + * (should just be an instance of |
|
1864 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
1865 | + * default components). |
|
1866 | + * |
|
1867 | + * @param string $principalUri |
|
1868 | + * @return array |
|
1869 | + */ |
|
1870 | + function getSubscriptionsForUser($principalUri) { |
|
1871 | + $fields = array_values($this->subscriptionPropertyMap); |
|
1872 | + $fields[] = 'id'; |
|
1873 | + $fields[] = 'uri'; |
|
1874 | + $fields[] = 'source'; |
|
1875 | + $fields[] = 'principaluri'; |
|
1876 | + $fields[] = 'lastmodified'; |
|
1877 | + |
|
1878 | + $query = $this->db->getQueryBuilder(); |
|
1879 | + $query->select($fields) |
|
1880 | + ->from('calendarsubscriptions') |
|
1881 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
1882 | + ->orderBy('calendarorder', 'asc'); |
|
1883 | + $stmt =$query->execute(); |
|
1884 | + |
|
1885 | + $subscriptions = []; |
|
1886 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
1887 | + |
|
1888 | + $subscription = [ |
|
1889 | + 'id' => $row['id'], |
|
1890 | + 'uri' => $row['uri'], |
|
1891 | + 'principaluri' => $row['principaluri'], |
|
1892 | + 'source' => $row['source'], |
|
1893 | + 'lastmodified' => $row['lastmodified'], |
|
1894 | + |
|
1895 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
1896 | + ]; |
|
1897 | + |
|
1898 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1899 | + if (!is_null($row[$dbName])) { |
|
1900 | + $subscription[$xmlName] = $row[$dbName]; |
|
1901 | + } |
|
1902 | + } |
|
1903 | + |
|
1904 | + $subscriptions[] = $subscription; |
|
1905 | + |
|
1906 | + } |
|
1907 | + |
|
1908 | + return $subscriptions; |
|
1909 | + } |
|
1910 | + |
|
1911 | + /** |
|
1912 | + * Creates a new subscription for a principal. |
|
1913 | + * |
|
1914 | + * If the creation was a success, an id must be returned that can be used to reference |
|
1915 | + * this subscription in other methods, such as updateSubscription. |
|
1916 | + * |
|
1917 | + * @param string $principalUri |
|
1918 | + * @param string $uri |
|
1919 | + * @param array $properties |
|
1920 | + * @return mixed |
|
1921 | + */ |
|
1922 | + function createSubscription($principalUri, $uri, array $properties) { |
|
1923 | + |
|
1924 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
1925 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
1926 | + } |
|
1927 | + |
|
1928 | + $values = [ |
|
1929 | + 'principaluri' => $principalUri, |
|
1930 | + 'uri' => $uri, |
|
1931 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
1932 | + 'lastmodified' => time(), |
|
1933 | + ]; |
|
1934 | + |
|
1935 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
1936 | + |
|
1937 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
1938 | + if (array_key_exists($xmlName, $properties)) { |
|
1939 | + $values[$dbName] = $properties[$xmlName]; |
|
1940 | + if (in_array($dbName, $propertiesBoolean)) { |
|
1941 | + $values[$dbName] = true; |
|
1942 | + } |
|
1943 | + } |
|
1944 | + } |
|
1945 | + |
|
1946 | + $valuesToInsert = array(); |
|
1947 | + |
|
1948 | + $query = $this->db->getQueryBuilder(); |
|
1949 | + |
|
1950 | + foreach (array_keys($values) as $name) { |
|
1951 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
1952 | + } |
|
1953 | + |
|
1954 | + $query->insert('calendarsubscriptions') |
|
1955 | + ->values($valuesToInsert) |
|
1956 | + ->execute(); |
|
1957 | + |
|
1958 | + $subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
1959 | + |
|
1960 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent( |
|
1961 | + '\OCA\DAV\CalDAV\CalDavBackend::createSubscription', |
|
1962 | + [ |
|
1963 | + 'subscriptionId' => $subscriptionId, |
|
1964 | + 'subscriptionData' => $this->getSubscriptionById($subscriptionId), |
|
1965 | + ])); |
|
1966 | + |
|
1967 | + return $subscriptionId; |
|
1968 | + } |
|
1969 | + |
|
1970 | + /** |
|
1971 | + * Updates a subscription |
|
1972 | + * |
|
1973 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
1974 | + * To do the actual updates, you must tell this object which properties |
|
1975 | + * you're going to process with the handle() method. |
|
1976 | + * |
|
1977 | + * Calling the handle method is like telling the PropPatch object "I |
|
1978 | + * promise I can handle updating this property". |
|
1979 | + * |
|
1980 | + * Read the PropPatch documentation for more info and examples. |
|
1981 | + * |
|
1982 | + * @param mixed $subscriptionId |
|
1983 | + * @param PropPatch $propPatch |
|
1984 | + * @return void |
|
1985 | + */ |
|
1986 | + function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
1987 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
1988 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
1989 | + |
|
1990 | + /** |
|
1991 | + * @suppress SqlInjectionChecker |
|
1992 | + */ |
|
1993 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
1994 | + |
|
1995 | + $newValues = []; |
|
1996 | + |
|
1997 | + foreach($mutations as $propertyName=>$propertyValue) { |
|
1998 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
1999 | + $newValues['source'] = $propertyValue->getHref(); |
|
2000 | + } else { |
|
2001 | + $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
2002 | + $newValues[$fieldName] = $propertyValue; |
|
2003 | + } |
|
2004 | + } |
|
2005 | + |
|
2006 | + $query = $this->db->getQueryBuilder(); |
|
2007 | + $query->update('calendarsubscriptions') |
|
2008 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
2009 | + foreach($newValues as $fieldName=>$value) { |
|
2010 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
2011 | + } |
|
2012 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
2013 | + ->execute(); |
|
2014 | + |
|
2015 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent( |
|
2016 | + '\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', |
|
2017 | + [ |
|
2018 | + 'subscriptionId' => $subscriptionId, |
|
2019 | + 'subscriptionData' => $this->getSubscriptionById($subscriptionId), |
|
2020 | + 'propertyMutations' => $mutations, |
|
2021 | + ])); |
|
2022 | + |
|
2023 | + return true; |
|
2024 | + |
|
2025 | + }); |
|
2026 | + } |
|
2027 | + |
|
2028 | + /** |
|
2029 | + * Deletes a subscription. |
|
2030 | + * |
|
2031 | + * @param mixed $subscriptionId |
|
2032 | + * @return void |
|
2033 | + */ |
|
2034 | + function deleteSubscription($subscriptionId) { |
|
2035 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent( |
|
2036 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', |
|
2037 | + [ |
|
2038 | + 'subscriptionId' => $subscriptionId, |
|
2039 | + 'subscriptionData' => $this->getSubscriptionById($subscriptionId), |
|
2040 | + ])); |
|
2041 | + |
|
2042 | + $query = $this->db->getQueryBuilder(); |
|
2043 | + $query->delete('calendarsubscriptions') |
|
2044 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
2045 | + ->execute(); |
|
2046 | + } |
|
2047 | + |
|
2048 | + /** |
|
2049 | + * Returns a single scheduling object for the inbox collection. |
|
2050 | + * |
|
2051 | + * The returned array should contain the following elements: |
|
2052 | + * * uri - A unique basename for the object. This will be used to |
|
2053 | + * construct a full uri. |
|
2054 | + * * calendardata - The iCalendar object |
|
2055 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
2056 | + * timestamp, or a PHP DateTime object. |
|
2057 | + * * etag - A unique token that must change if the object changed. |
|
2058 | + * * size - The size of the object, in bytes. |
|
2059 | + * |
|
2060 | + * @param string $principalUri |
|
2061 | + * @param string $objectUri |
|
2062 | + * @return array |
|
2063 | + */ |
|
2064 | + function getSchedulingObject($principalUri, $objectUri) { |
|
2065 | + $query = $this->db->getQueryBuilder(); |
|
2066 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
2067 | + ->from('schedulingobjects') |
|
2068 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
2069 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
2070 | + ->execute(); |
|
2071 | + |
|
2072 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
2073 | + |
|
2074 | + if(!$row) { |
|
2075 | + return null; |
|
2076 | + } |
|
2077 | + |
|
2078 | + return [ |
|
2079 | + 'uri' => $row['uri'], |
|
2080 | + 'calendardata' => $row['calendardata'], |
|
2081 | + 'lastmodified' => $row['lastmodified'], |
|
2082 | + 'etag' => '"' . $row['etag'] . '"', |
|
2083 | + 'size' => (int)$row['size'], |
|
2084 | + ]; |
|
2085 | + } |
|
2086 | + |
|
2087 | + /** |
|
2088 | + * Returns all scheduling objects for the inbox collection. |
|
2089 | + * |
|
2090 | + * These objects should be returned as an array. Every item in the array |
|
2091 | + * should follow the same structure as returned from getSchedulingObject. |
|
2092 | + * |
|
2093 | + * The main difference is that 'calendardata' is optional. |
|
2094 | + * |
|
2095 | + * @param string $principalUri |
|
2096 | + * @return array |
|
2097 | + */ |
|
2098 | + function getSchedulingObjects($principalUri) { |
|
2099 | + $query = $this->db->getQueryBuilder(); |
|
2100 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
2101 | + ->from('schedulingobjects') |
|
2102 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
2103 | + ->execute(); |
|
2104 | + |
|
2105 | + $result = []; |
|
2106 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
2107 | + $result[] = [ |
|
2108 | + 'calendardata' => $row['calendardata'], |
|
2109 | + 'uri' => $row['uri'], |
|
2110 | + 'lastmodified' => $row['lastmodified'], |
|
2111 | + 'etag' => '"' . $row['etag'] . '"', |
|
2112 | + 'size' => (int)$row['size'], |
|
2113 | + ]; |
|
2114 | + } |
|
2115 | + |
|
2116 | + return $result; |
|
2117 | + } |
|
2118 | + |
|
2119 | + /** |
|
2120 | + * Deletes a scheduling object from the inbox collection. |
|
2121 | + * |
|
2122 | + * @param string $principalUri |
|
2123 | + * @param string $objectUri |
|
2124 | + * @return void |
|
2125 | + */ |
|
2126 | + function deleteSchedulingObject($principalUri, $objectUri) { |
|
2127 | + $query = $this->db->getQueryBuilder(); |
|
2128 | + $query->delete('schedulingobjects') |
|
2129 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
2130 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
2131 | + ->execute(); |
|
2132 | + } |
|
2133 | + |
|
2134 | + /** |
|
2135 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
2136 | + * |
|
2137 | + * @param string $principalUri |
|
2138 | + * @param string $objectUri |
|
2139 | + * @param string $objectData |
|
2140 | + * @return void |
|
2141 | + */ |
|
2142 | + function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
2143 | + $query = $this->db->getQueryBuilder(); |
|
2144 | + $query->insert('schedulingobjects') |
|
2145 | + ->values([ |
|
2146 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
2147 | + 'calendardata' => $query->createNamedParameter($objectData), |
|
2148 | + 'uri' => $query->createNamedParameter($objectUri), |
|
2149 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
2150 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
2151 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
2152 | + ]) |
|
2153 | + ->execute(); |
|
2154 | + } |
|
2155 | + |
|
2156 | + /** |
|
2157 | + * Adds a change record to the calendarchanges table. |
|
2158 | + * |
|
2159 | + * @param mixed $calendarId |
|
2160 | + * @param string $objectUri |
|
2161 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
2162 | + * @return void |
|
2163 | + */ |
|
2164 | + protected function addChange($calendarId, $objectUri, $operation) { |
|
2165 | + |
|
2166 | + $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
2167 | + $stmt->execute([ |
|
2168 | + $objectUri, |
|
2169 | + $calendarId, |
|
2170 | + $operation, |
|
2171 | + $calendarId |
|
2172 | + ]); |
|
2173 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
2174 | + $stmt->execute([ |
|
2175 | + $calendarId |
|
2176 | + ]); |
|
2177 | + |
|
2178 | + } |
|
2179 | + |
|
2180 | + /** |
|
2181 | + * Parses some information from calendar objects, used for optimized |
|
2182 | + * calendar-queries. |
|
2183 | + * |
|
2184 | + * Returns an array with the following keys: |
|
2185 | + * * etag - An md5 checksum of the object without the quotes. |
|
2186 | + * * size - Size of the object in bytes |
|
2187 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
2188 | + * * firstOccurence |
|
2189 | + * * lastOccurence |
|
2190 | + * * uid - value of the UID property |
|
2191 | + * |
|
2192 | + * @param string $calendarData |
|
2193 | + * @return array |
|
2194 | + */ |
|
2195 | + public function getDenormalizedData($calendarData) { |
|
2196 | + |
|
2197 | + $vObject = Reader::read($calendarData); |
|
2198 | + $componentType = null; |
|
2199 | + $component = null; |
|
2200 | + $firstOccurrence = null; |
|
2201 | + $lastOccurrence = null; |
|
2202 | + $uid = null; |
|
2203 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
2204 | + foreach($vObject->getComponents() as $component) { |
|
2205 | + if ($component->name!=='VTIMEZONE') { |
|
2206 | + $componentType = $component->name; |
|
2207 | + $uid = (string)$component->UID; |
|
2208 | + break; |
|
2209 | + } |
|
2210 | + } |
|
2211 | + if (!$componentType) { |
|
2212 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
2213 | + } |
|
2214 | + if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
2215 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
2216 | + // Finding the last occurrence is a bit harder |
|
2217 | + if (!isset($component->RRULE)) { |
|
2218 | + if (isset($component->DTEND)) { |
|
2219 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
2220 | + } elseif (isset($component->DURATION)) { |
|
2221 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
2222 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
2223 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
2224 | + } elseif (!$component->DTSTART->hasTime()) { |
|
2225 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
2226 | + $endDate->modify('+1 day'); |
|
2227 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
2228 | + } else { |
|
2229 | + $lastOccurrence = $firstOccurrence; |
|
2230 | + } |
|
2231 | + } else { |
|
2232 | + $it = new EventIterator($vObject, (string)$component->UID); |
|
2233 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
2234 | + if ($it->isInfinite()) { |
|
2235 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
2236 | + } else { |
|
2237 | + $end = $it->getDtEnd(); |
|
2238 | + while($it->valid() && $end < $maxDate) { |
|
2239 | + $end = $it->getDtEnd(); |
|
2240 | + $it->next(); |
|
2241 | + |
|
2242 | + } |
|
2243 | + $lastOccurrence = $end->getTimestamp(); |
|
2244 | + } |
|
2245 | + |
|
2246 | + } |
|
2247 | + } |
|
2248 | + |
|
2249 | + if ($component->CLASS) { |
|
2250 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
2251 | + switch ($component->CLASS->getValue()) { |
|
2252 | + case 'PUBLIC': |
|
2253 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
2254 | + break; |
|
2255 | + case 'CONFIDENTIAL': |
|
2256 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
2257 | + break; |
|
2258 | + } |
|
2259 | + } |
|
2260 | + return [ |
|
2261 | + 'etag' => md5($calendarData), |
|
2262 | + 'size' => strlen($calendarData), |
|
2263 | + 'componentType' => $componentType, |
|
2264 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
2265 | + 'lastOccurence' => $lastOccurrence, |
|
2266 | + 'uid' => $uid, |
|
2267 | + 'classification' => $classification |
|
2268 | + ]; |
|
2269 | + |
|
2270 | + } |
|
2271 | + |
|
2272 | + /** |
|
2273 | + * @param $cardData |
|
2274 | + * @return bool|string |
|
2275 | + */ |
|
2276 | + private function readBlob($cardData) { |
|
2277 | + if (is_resource($cardData)) { |
|
2278 | + return stream_get_contents($cardData); |
|
2279 | + } |
|
2280 | + |
|
2281 | + return $cardData; |
|
2282 | + } |
|
2283 | + |
|
2284 | + /** |
|
2285 | + * @param IShareable $shareable |
|
2286 | + * @param array $add |
|
2287 | + * @param array $remove |
|
2288 | + */ |
|
2289 | + public function updateShares($shareable, $add, $remove) { |
|
2290 | + $calendarId = $shareable->getResourceId(); |
|
2291 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent( |
|
2292 | + '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
2293 | + [ |
|
2294 | + 'calendarId' => $calendarId, |
|
2295 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
2296 | + 'shares' => $this->getShares($calendarId), |
|
2297 | + 'add' => $add, |
|
2298 | + 'remove' => $remove, |
|
2299 | + ])); |
|
2300 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
2301 | + } |
|
2302 | + |
|
2303 | + /** |
|
2304 | + * @param int $resourceId |
|
2305 | + * @return array |
|
2306 | + */ |
|
2307 | + public function getShares($resourceId) { |
|
2308 | + return $this->sharingBackend->getShares($resourceId); |
|
2309 | + } |
|
2310 | + |
|
2311 | + /** |
|
2312 | + * @param boolean $value |
|
2313 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
2314 | + * @return string|null |
|
2315 | + */ |
|
2316 | + public function setPublishStatus($value, $calendar) { |
|
2317 | + |
|
2318 | + $calendarId = $calendar->getResourceId(); |
|
2319 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent( |
|
2320 | + '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
2321 | + [ |
|
2322 | + 'calendarId' => $calendarId, |
|
2323 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
2324 | + 'public' => $value, |
|
2325 | + ])); |
|
2326 | + |
|
2327 | + $query = $this->db->getQueryBuilder(); |
|
2328 | + if ($value) { |
|
2329 | + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
2330 | + $query->insert('dav_shares') |
|
2331 | + ->values([ |
|
2332 | + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
2333 | + 'type' => $query->createNamedParameter('calendar'), |
|
2334 | + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
2335 | + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
2336 | + 'publicuri' => $query->createNamedParameter($publicUri) |
|
2337 | + ]); |
|
2338 | + $query->execute(); |
|
2339 | + return $publicUri; |
|
2340 | + } |
|
2341 | + $query->delete('dav_shares') |
|
2342 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
2343 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
2344 | + $query->execute(); |
|
2345 | + return null; |
|
2346 | + } |
|
2347 | + |
|
2348 | + /** |
|
2349 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
2350 | + * @return mixed |
|
2351 | + */ |
|
2352 | + public function getPublishStatus($calendar) { |
|
2353 | + $query = $this->db->getQueryBuilder(); |
|
2354 | + $result = $query->select('publicuri') |
|
2355 | + ->from('dav_shares') |
|
2356 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
2357 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
2358 | + ->execute(); |
|
2359 | + |
|
2360 | + $row = $result->fetch(); |
|
2361 | + $result->closeCursor(); |
|
2362 | + return $row ? reset($row) : false; |
|
2363 | + } |
|
2364 | + |
|
2365 | + /** |
|
2366 | + * @param int $resourceId |
|
2367 | + * @param array $acl |
|
2368 | + * @return array |
|
2369 | + */ |
|
2370 | + public function applyShareAcl($resourceId, $acl) { |
|
2371 | + return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
2372 | + } |
|
2373 | + |
|
2374 | + |
|
2375 | + |
|
2376 | + /** |
|
2377 | + * update properties table |
|
2378 | + * |
|
2379 | + * @param int $calendarId |
|
2380 | + * @param string $objectUri |
|
2381 | + * @param string $calendarData |
|
2382 | + */ |
|
2383 | + public function updateProperties($calendarId, $objectUri, $calendarData) { |
|
2384 | + $objectId = $this->getCalendarObjectId($calendarId, $objectUri); |
|
2385 | + |
|
2386 | + try { |
|
2387 | + $vCalendar = $this->readCalendarData($calendarData); |
|
2388 | + } catch (\Exception $ex) { |
|
2389 | + return; |
|
2390 | + } |
|
2391 | + |
|
2392 | + $this->purgeProperties($calendarId, $objectId); |
|
2393 | + |
|
2394 | + $query = $this->db->getQueryBuilder(); |
|
2395 | + $query->insert($this->dbObjectPropertiesTable) |
|
2396 | + ->values( |
|
2397 | + [ |
|
2398 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
2399 | + 'objectid' => $query->createNamedParameter($objectId), |
|
2400 | + 'name' => $query->createParameter('name'), |
|
2401 | + 'parameter' => $query->createParameter('parameter'), |
|
2402 | + 'value' => $query->createParameter('value'), |
|
2403 | + ] |
|
2404 | + ); |
|
2405 | + |
|
2406 | + $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
2407 | + foreach ($vCalendar->getComponents() as $component) { |
|
2408 | + if (!in_array($component->name, $indexComponents)) { |
|
2409 | + continue; |
|
2410 | + } |
|
2411 | + |
|
2412 | + foreach ($component->children() as $property) { |
|
2413 | + if (in_array($property->name, self::$indexProperties)) { |
|
2414 | + $value = $property->getValue(); |
|
2415 | + // is this a shitty db? |
|
2416 | + if (!$this->db->supports4ByteText()) { |
|
2417 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2418 | + } |
|
2419 | + $value = mb_substr($value, 0, 254); |
|
2420 | + |
|
2421 | + $query->setParameter('name', $property->name); |
|
2422 | + $query->setParameter('parameter', null); |
|
2423 | + $query->setParameter('value', $value); |
|
2424 | + $query->execute(); |
|
2425 | + } |
|
2426 | + |
|
2427 | + if (array_key_exists($property->name, self::$indexParameters)) { |
|
2428 | + $parameters = $property->parameters(); |
|
2429 | + $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
2430 | + |
|
2431 | + foreach ($parameters as $key => $value) { |
|
2432 | + if (in_array($key, $indexedParametersForProperty)) { |
|
2433 | + // is this a shitty db? |
|
2434 | + if ($this->db->supports4ByteText()) { |
|
2435 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
2436 | + } |
|
2437 | + $value = mb_substr($value, 0, 254); |
|
2438 | + |
|
2439 | + $query->setParameter('name', $property->name); |
|
2440 | + $query->setParameter('parameter', substr($key, 0, 254)); |
|
2441 | + $query->setParameter('value', substr($value, 0, 254)); |
|
2442 | + $query->execute(); |
|
2443 | + } |
|
2444 | + } |
|
2445 | + } |
|
2446 | + } |
|
2447 | + } |
|
2448 | + } |
|
2449 | + |
|
2450 | + /** |
|
2451 | + * deletes all birthday calendars |
|
2452 | + */ |
|
2453 | + public function deleteAllBirthdayCalendars() { |
|
2454 | + $query = $this->db->getQueryBuilder(); |
|
2455 | + $result = $query->select(['id'])->from('calendars') |
|
2456 | + ->where($query->expr()->eq('uri', |
|
2457 | + $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
2458 | + ->execute(); |
|
2459 | + |
|
2460 | + $ids = $result->fetchAll(); |
|
2461 | + foreach($ids as $id) { |
|
2462 | + $this->deleteCalendar($id['id']); |
|
2463 | + } |
|
2464 | + } |
|
2465 | + |
|
2466 | + /** |
|
2467 | + * @param $subscriptionId |
|
2468 | + */ |
|
2469 | + public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
2470 | + $query = $this->db->getQueryBuilder(); |
|
2471 | + $query->delete('calendarsubscrobjects') |
|
2472 | + ->where($query->expr()->eq('subscriptionid', $query->createNamedParameter($subscriptionId))); |
|
2473 | + $query->execute(); |
|
2474 | + } |
|
2475 | + |
|
2476 | + /** |
|
2477 | + * @param $subscriptionId |
|
2478 | + * @param $objectUri |
|
2479 | + * @param $calendarData |
|
2480 | + * @return string |
|
2481 | + * @throws DAV\Exception\BadRequest |
|
2482 | + */ |
|
2483 | + public function addCachedEvent($subscriptionId, $objectUri, $calendarData) { |
|
2484 | + $extraData = $this->getDenormalizedData($calendarData); |
|
2485 | + |
|
2486 | + $query = $this->db->getQueryBuilder(); |
|
2487 | + $query->insert('calendarsubscrobjects') |
|
2488 | + ->values([ |
|
2489 | + 'subscriptionid' => $query->createNamedParameter($subscriptionId), |
|
2490 | + 'uri' => $query->createNamedParameter($objectUri), |
|
2491 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
2492 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
2493 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
2494 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
2495 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
2496 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
2497 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
2498 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
2499 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
2500 | + ]) |
|
2501 | + ->execute(); |
|
2502 | + } |
|
2503 | + |
|
2504 | + /** |
|
2505 | + * read VCalendar data into a VCalendar object |
|
2506 | + * |
|
2507 | + * @param string $objectData |
|
2508 | + * @return VCalendar |
|
2509 | + */ |
|
2510 | + protected function readCalendarData($objectData) { |
|
2511 | + return Reader::read($objectData); |
|
2512 | + } |
|
2513 | + |
|
2514 | + /** |
|
2515 | + * delete all properties from a given calendar object |
|
2516 | + * |
|
2517 | + * @param int $calendarId |
|
2518 | + * @param int $objectId |
|
2519 | + */ |
|
2520 | + protected function purgeProperties($calendarId, $objectId) { |
|
2521 | + $query = $this->db->getQueryBuilder(); |
|
2522 | + $query->delete($this->dbObjectPropertiesTable) |
|
2523 | + ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
2524 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2525 | + $query->execute(); |
|
2526 | + } |
|
2527 | + |
|
2528 | + /** |
|
2529 | + * get ID from a given calendar object |
|
2530 | + * |
|
2531 | + * @param int $calendarId |
|
2532 | + * @param string $uri |
|
2533 | + * @return int |
|
2534 | + */ |
|
2535 | + protected function getCalendarObjectId($calendarId, $uri) { |
|
2536 | + $query = $this->db->getQueryBuilder(); |
|
2537 | + $query->select('id')->from('calendarobjects') |
|
2538 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
2539 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
2540 | + |
|
2541 | + $result = $query->execute(); |
|
2542 | + $objectIds = $result->fetch(); |
|
2543 | + $result->closeCursor(); |
|
2544 | + |
|
2545 | + if (!isset($objectIds['id'])) { |
|
2546 | + throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
2547 | + } |
|
2548 | + |
|
2549 | + return (int)$objectIds['id']; |
|
2550 | + } |
|
2551 | + |
|
2552 | + /** |
|
2553 | + * return legacy endpoint principal name to new principal name |
|
2554 | + * |
|
2555 | + * @param $principalUri |
|
2556 | + * @param $toV2 |
|
2557 | + * @return string |
|
2558 | + */ |
|
2559 | + private function convertPrincipal($principalUri, $toV2) { |
|
2560 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
2561 | + list(, $name) = Uri\split($principalUri); |
|
2562 | + if ($toV2 === true) { |
|
2563 | + return "principals/users/$name"; |
|
2564 | + } |
|
2565 | + return "principals/$name"; |
|
2566 | + } |
|
2567 | + return $principalUri; |
|
2568 | + } |
|
2569 | + |
|
2570 | + /** |
|
2571 | + * adds information about an owner to the calendar data |
|
2572 | + * |
|
2573 | + * @param $calendarInfo |
|
2574 | + */ |
|
2575 | + private function addOwnerPrincipal(&$calendarInfo) { |
|
2576 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
2577 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
2578 | + if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
2579 | + $uri = $calendarInfo[$ownerPrincipalKey]; |
|
2580 | + } else { |
|
2581 | + $uri = $calendarInfo['principaluri']; |
|
2582 | + } |
|
2583 | + |
|
2584 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
2585 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
2586 | + $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
2587 | + } |
|
2588 | + } |
|
2589 | 2589 | } |