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 15KB

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