You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Backend.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\CalDAV\Activity;
  27. use OCA\DAV\CalDAV\Activity\Provider\Calendar;
  28. use OCA\DAV\CalDAV\Activity\Provider\Event;
  29. use OCA\DAV\CalDAV\CalDavBackend;
  30. use OCP\Activity\IEvent;
  31. use OCP\Activity\IManager as IActivityManager;
  32. use OCP\App\IAppManager;
  33. use OCP\IGroup;
  34. use OCP\IGroupManager;
  35. use OCP\IUser;
  36. use OCP\IUserSession;
  37. use Sabre\VObject\Reader;
  38. /**
  39. * Class Backend
  40. *
  41. * @package OCA\DAV\CalDAV\Activity
  42. */
  43. class Backend {
  44. /** @var IActivityManager */
  45. protected $activityManager;
  46. /** @var IGroupManager */
  47. protected $groupManager;
  48. /** @var IUserSession */
  49. protected $userSession;
  50. /** @var IAppManager */
  51. protected $appManager;
  52. public function __construct(IActivityManager $activityManager, IGroupManager $groupManager, IUserSession $userSession, IAppManager $appManager) {
  53. $this->activityManager = $activityManager;
  54. $this->groupManager = $groupManager;
  55. $this->userSession = $userSession;
  56. $this->appManager = $appManager;
  57. }
  58. /**
  59. * Creates activities when a calendar was creates
  60. *
  61. * @param array $calendarData
  62. */
  63. public function onCalendarAdd(array $calendarData) {
  64. $this->triggerCalendarActivity(Calendar::SUBJECT_ADD, $calendarData);
  65. }
  66. /**
  67. * Creates activities when a calendar was updated
  68. *
  69. * @param array $calendarData
  70. * @param array $shares
  71. * @param array $properties
  72. */
  73. public function onCalendarUpdate(array $calendarData, array $shares, array $properties) {
  74. $this->triggerCalendarActivity(Calendar::SUBJECT_UPDATE, $calendarData, $shares, $properties);
  75. }
  76. /**
  77. * Creates activities when a calendar was moved to trash
  78. *
  79. * @param array $calendarData
  80. * @param array $shares
  81. */
  82. public function onCalendarMovedToTrash(array $calendarData, array $shares): void {
  83. $this->triggerCalendarActivity(Calendar::SUBJECT_MOVE_TO_TRASH, $calendarData, $shares);
  84. }
  85. /**
  86. * Creates activities when a calendar was restored
  87. *
  88. * @param array $calendarData
  89. * @param array $shares
  90. */
  91. public function onCalendarRestored(array $calendarData, array $shares): void {
  92. $this->triggerCalendarActivity(Calendar::SUBJECT_RESTORE, $calendarData, $shares);
  93. }
  94. /**
  95. * Creates activities when a calendar was deleted
  96. *
  97. * @param array $calendarData
  98. * @param array $shares
  99. */
  100. public function onCalendarDelete(array $calendarData, array $shares): void {
  101. $this->triggerCalendarActivity(Calendar::SUBJECT_DELETE, $calendarData, $shares);
  102. }
  103. /**
  104. * Creates activities when a calendar was (un)published
  105. *
  106. * @param array $calendarData
  107. * @param bool $publishStatus
  108. */
  109. public function onCalendarPublication(array $calendarData, $publishStatus) {
  110. $this->triggerCalendarActivity($publishStatus ? Calendar::SUBJECT_PUBLISH : Calendar::SUBJECT_UNPUBLISH, $calendarData);
  111. }
  112. /**
  113. * Creates activities for all related users when a calendar was touched
  114. *
  115. * @param string $action
  116. * @param array $calendarData
  117. * @param array $shares
  118. * @param array $changedProperties
  119. */
  120. protected function triggerCalendarActivity($action, array $calendarData, array $shares = [], array $changedProperties = []) {
  121. if (!isset($calendarData['principaluri'])) {
  122. return;
  123. }
  124. $principal = explode('/', $calendarData['principaluri']);
  125. $owner = array_pop($principal);
  126. $currentUser = $this->userSession->getUser();
  127. if ($currentUser instanceof IUser) {
  128. $currentUser = $currentUser->getUID();
  129. } else {
  130. $currentUser = $owner;
  131. }
  132. $event = $this->activityManager->generateEvent();
  133. $event->setApp('dav')
  134. ->setObject('calendar', (int) $calendarData['id'])
  135. ->setType('calendar')
  136. ->setAuthor($currentUser);
  137. $changedVisibleInformation = array_intersect([
  138. '{DAV:}displayname',
  139. '{http://apple.com/ns/ical/}calendar-color'
  140. ], array_keys($changedProperties));
  141. if (empty($shares) || ($action === Calendar::SUBJECT_UPDATE && empty($changedVisibleInformation))) {
  142. $users = [$owner];
  143. } else {
  144. $users = $this->getUsersForShares($shares);
  145. $users[] = $owner;
  146. }
  147. foreach ($users as $user) {
  148. $event->setAffectedUser($user)
  149. ->setSubject(
  150. $user === $currentUser ? $action . '_self' : $action,
  151. [
  152. 'actor' => $currentUser,
  153. 'calendar' => [
  154. 'id' => (int) $calendarData['id'],
  155. 'uri' => $calendarData['uri'],
  156. 'name' => $calendarData['{DAV:}displayname'],
  157. ],
  158. ]
  159. );
  160. $this->activityManager->publish($event);
  161. }
  162. }
  163. /**
  164. * Creates activities for all related users when a calendar was (un-)shared
  165. *
  166. * @param array $calendarData
  167. * @param array $shares
  168. * @param array $add
  169. * @param array $remove
  170. */
  171. public function onCalendarUpdateShares(array $calendarData, array $shares, array $add, array $remove) {
  172. $principal = explode('/', $calendarData['principaluri']);
  173. $owner = $principal[2];
  174. $currentUser = $this->userSession->getUser();
  175. if ($currentUser instanceof IUser) {
  176. $currentUser = $currentUser->getUID();
  177. } else {
  178. $currentUser = $owner;
  179. }
  180. $event = $this->activityManager->generateEvent();
  181. $event->setApp('dav')
  182. ->setObject('calendar', (int) $calendarData['id'])
  183. ->setType('calendar')
  184. ->setAuthor($currentUser);
  185. foreach ($remove as $principal) {
  186. // principal:principals/users/test
  187. $parts = explode(':', $principal, 2);
  188. if ($parts[0] !== 'principal') {
  189. continue;
  190. }
  191. $principal = explode('/', $parts[1]);
  192. if ($principal[1] === 'users') {
  193. $this->triggerActivityUser(
  194. $principal[2],
  195. $event,
  196. $calendarData,
  197. Calendar::SUBJECT_UNSHARE_USER,
  198. Calendar::SUBJECT_DELETE . '_self'
  199. );
  200. if ($owner !== $principal[2]) {
  201. $parameters = [
  202. 'actor' => $event->getAuthor(),
  203. 'calendar' => [
  204. 'id' => (int) $calendarData['id'],
  205. 'uri' => $calendarData['uri'],
  206. 'name' => $calendarData['{DAV:}displayname'],
  207. ],
  208. 'user' => $principal[2],
  209. ];
  210. if ($owner === $event->getAuthor()) {
  211. $subject = Calendar::SUBJECT_UNSHARE_USER . '_you';
  212. } elseif ($principal[2] === $event->getAuthor()) {
  213. $subject = Calendar::SUBJECT_UNSHARE_USER . '_self';
  214. } else {
  215. $event->setAffectedUser($event->getAuthor())
  216. ->setSubject(Calendar::SUBJECT_UNSHARE_USER . '_you', $parameters);
  217. $this->activityManager->publish($event);
  218. $subject = Calendar::SUBJECT_UNSHARE_USER . '_by';
  219. }
  220. $event->setAffectedUser($owner)
  221. ->setSubject($subject, $parameters);
  222. $this->activityManager->publish($event);
  223. }
  224. } elseif ($principal[1] === 'groups') {
  225. $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_UNSHARE_USER);
  226. $parameters = [
  227. 'actor' => $event->getAuthor(),
  228. 'calendar' => [
  229. 'id' => (int) $calendarData['id'],
  230. 'uri' => $calendarData['uri'],
  231. 'name' => $calendarData['{DAV:}displayname'],
  232. ],
  233. 'group' => $principal[2],
  234. ];
  235. if ($owner === $event->getAuthor()) {
  236. $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_you';
  237. } else {
  238. $event->setAffectedUser($event->getAuthor())
  239. ->setSubject(Calendar::SUBJECT_UNSHARE_GROUP . '_you', $parameters);
  240. $this->activityManager->publish($event);
  241. $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_by';
  242. }
  243. $event->setAffectedUser($owner)
  244. ->setSubject($subject, $parameters);
  245. $this->activityManager->publish($event);
  246. }
  247. }
  248. foreach ($add as $share) {
  249. if ($this->isAlreadyShared($share['href'], $shares)) {
  250. continue;
  251. }
  252. // principal:principals/users/test
  253. $parts = explode(':', $share['href'], 2);
  254. if ($parts[0] !== 'principal') {
  255. continue;
  256. }
  257. $principal = explode('/', $parts[1]);
  258. if ($principal[1] === 'users') {
  259. $this->triggerActivityUser($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER);
  260. if ($owner !== $principal[2]) {
  261. $parameters = [
  262. 'actor' => $event->getAuthor(),
  263. 'calendar' => [
  264. 'id' => (int) $calendarData['id'],
  265. 'uri' => $calendarData['uri'],
  266. 'name' => $calendarData['{DAV:}displayname'],
  267. ],
  268. 'user' => $principal[2],
  269. ];
  270. if ($owner === $event->getAuthor()) {
  271. $subject = Calendar::SUBJECT_SHARE_USER . '_you';
  272. } else {
  273. $event->setAffectedUser($event->getAuthor())
  274. ->setSubject(Calendar::SUBJECT_SHARE_USER . '_you', $parameters);
  275. $this->activityManager->publish($event);
  276. $subject = Calendar::SUBJECT_SHARE_USER . '_by';
  277. }
  278. $event->setAffectedUser($owner)
  279. ->setSubject($subject, $parameters);
  280. $this->activityManager->publish($event);
  281. }
  282. } elseif ($principal[1] === 'groups') {
  283. $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER);
  284. $parameters = [
  285. 'actor' => $event->getAuthor(),
  286. 'calendar' => [
  287. 'id' => (int) $calendarData['id'],
  288. 'uri' => $calendarData['uri'],
  289. 'name' => $calendarData['{DAV:}displayname'],
  290. ],
  291. 'group' => $principal[2],
  292. ];
  293. if ($owner === $event->getAuthor()) {
  294. $subject = Calendar::SUBJECT_SHARE_GROUP . '_you';
  295. } else {
  296. $event->setAffectedUser($event->getAuthor())
  297. ->setSubject(Calendar::SUBJECT_SHARE_GROUP . '_you', $parameters);
  298. $this->activityManager->publish($event);
  299. $subject = Calendar::SUBJECT_SHARE_GROUP . '_by';
  300. }
  301. $event->setAffectedUser($owner)
  302. ->setSubject($subject, $parameters);
  303. $this->activityManager->publish($event);
  304. }
  305. }
  306. }
  307. /**
  308. * Checks if a calendar is already shared with a principal
  309. *
  310. * @param string $principal
  311. * @param array[] $shares
  312. * @return bool
  313. */
  314. protected function isAlreadyShared($principal, $shares) {
  315. foreach ($shares as $share) {
  316. if ($principal === $share['href']) {
  317. return true;
  318. }
  319. }
  320. return false;
  321. }
  322. /**
  323. * Creates the given activity for all members of the given group
  324. *
  325. * @param string $gid
  326. * @param IEvent $event
  327. * @param array $properties
  328. * @param string $subject
  329. */
  330. protected function triggerActivityGroup($gid, IEvent $event, array $properties, $subject) {
  331. $group = $this->groupManager->get($gid);
  332. if ($group instanceof IGroup) {
  333. foreach ($group->getUsers() as $user) {
  334. // Exclude current user
  335. if ($user->getUID() !== $event->getAuthor()) {
  336. $this->triggerActivityUser($user->getUID(), $event, $properties, $subject);
  337. }
  338. }
  339. }
  340. }
  341. /**
  342. * Creates the given activity for the given user
  343. *
  344. * @param string $user
  345. * @param IEvent $event
  346. * @param array $properties
  347. * @param string $subject
  348. * @param string $subjectSelf
  349. */
  350. protected function triggerActivityUser($user, IEvent $event, array $properties, $subject, $subjectSelf = '') {
  351. $event->setAffectedUser($user)
  352. ->setSubject(
  353. $user === $event->getAuthor() && $subjectSelf ? $subjectSelf : $subject,
  354. [
  355. 'actor' => $event->getAuthor(),
  356. 'calendar' => [
  357. 'id' => (int) $properties['id'],
  358. 'uri' => $properties['uri'],
  359. 'name' => $properties['{DAV:}displayname'],
  360. ],
  361. ]
  362. );
  363. $this->activityManager->publish($event);
  364. }
  365. /**
  366. * Creates activities when a calendar object was created/updated/deleted
  367. *
  368. * @param string $action
  369. * @param array $calendarData
  370. * @param array $shares
  371. * @param array $objectData
  372. */
  373. public function onTouchCalendarObject($action, array $calendarData, array $shares, array $objectData) {
  374. if (!isset($calendarData['principaluri'])) {
  375. return;
  376. }
  377. $principal = explode('/', $calendarData['principaluri']);
  378. $owner = array_pop($principal);
  379. $currentUser = $this->userSession->getUser();
  380. if ($currentUser instanceof IUser) {
  381. $currentUser = $currentUser->getUID();
  382. } else {
  383. $currentUser = $owner;
  384. }
  385. $classification = $objectData['classification'] ?? CalDavBackend::CLASSIFICATION_PUBLIC;
  386. $object = $this->getObjectNameAndType($objectData);
  387. $action = $action . '_' . $object['type'];
  388. if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'COMPLETED') {
  389. $action .= '_completed';
  390. } elseif ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'NEEDS-ACTION') {
  391. $action .= '_needs_action';
  392. }
  393. $event = $this->activityManager->generateEvent();
  394. $event->setApp('dav')
  395. ->setObject('calendar', (int) $calendarData['id'])
  396. ->setType($object['type'] === 'event' ? 'calendar_event' : 'calendar_todo')
  397. ->setAuthor($currentUser);
  398. $users = $this->getUsersForShares($shares);
  399. $users[] = $owner;
  400. // Users for share can return the owner itself if the calendar is published
  401. foreach (array_unique($users) as $user) {
  402. if ($classification === CalDavBackend::CLASSIFICATION_PRIVATE && $user !== $owner) {
  403. // Private events are only shown to the owner
  404. continue;
  405. }
  406. $params = [
  407. 'actor' => $event->getAuthor(),
  408. 'calendar' => [
  409. 'id' => (int) $calendarData['id'],
  410. 'uri' => $calendarData['uri'],
  411. 'name' => $calendarData['{DAV:}displayname'],
  412. ],
  413. 'object' => [
  414. 'id' => $object['id'],
  415. 'name' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner ? 'Busy' : $object['name'],
  416. 'classified' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner,
  417. ],
  418. ];
  419. if ($object['type'] === 'event' && strpos($action, Event::SUBJECT_OBJECT_DELETE) === false && $this->appManager->isEnabledForUser('calendar')) {
  420. $params['object']['link']['object_uri'] = $objectData['uri'];
  421. $params['object']['link']['calendar_uri'] = $calendarData['uri'];
  422. $params['object']['link']['owner'] = $owner;
  423. }
  424. $event->setAffectedUser($user)
  425. ->setSubject(
  426. $user === $currentUser ? $action . '_self' : $action,
  427. $params
  428. );
  429. $this->activityManager->publish($event);
  430. }
  431. }
  432. /**
  433. * @param array $objectData
  434. * @return string[]|bool
  435. */
  436. protected function getObjectNameAndType(array $objectData) {
  437. $vObject = Reader::read($objectData['calendardata']);
  438. $component = $componentType = null;
  439. foreach ($vObject->getComponents() as $component) {
  440. if (in_array($component->name, ['VEVENT', 'VTODO'])) {
  441. $componentType = $component->name;
  442. break;
  443. }
  444. }
  445. if (!$componentType) {
  446. // Calendar objects must have a VEVENT or VTODO component
  447. return false;
  448. }
  449. if ($componentType === 'VEVENT') {
  450. return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'event'];
  451. }
  452. return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'todo', 'status' => (string) $component->STATUS];
  453. }
  454. /**
  455. * Get all users that have access to a given calendar
  456. *
  457. * @param array $shares
  458. * @return string[]
  459. */
  460. protected function getUsersForShares(array $shares) {
  461. $users = $groups = [];
  462. foreach ($shares as $share) {
  463. $principal = explode('/', $share['{http://owncloud.org/ns}principal']);
  464. if ($principal[1] === 'users') {
  465. $users[] = $principal[2];
  466. } elseif ($principal[1] === 'groups') {
  467. $groups[] = $principal[2];
  468. }
  469. }
  470. if (!empty($groups)) {
  471. foreach ($groups as $gid) {
  472. $group = $this->groupManager->get($gid);
  473. if ($group instanceof IGroup) {
  474. foreach ($group->getUsers() as $user) {
  475. $users[] = $user->getUID();
  476. }
  477. }
  478. }
  479. }
  480. return array_unique($users);
  481. }
  482. }