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.

532 lines
15 KiB

  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 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. // Users for share can return the owner itself if the calendar is published
  383. foreach (array_unique($users) as $user) {
  384. if ($classification === CalDavBackend::CLASSIFICATION_PRIVATE && $user !== $owner) {
  385. // Private events are only shown to the owner
  386. continue;
  387. }
  388. $params = [
  389. 'actor' => $event->getAuthor(),
  390. 'calendar' => [
  391. 'id' => (int) $calendarData['id'],
  392. 'uri' => $calendarData['uri'],
  393. 'name' => $calendarData['{DAV:}displayname'],
  394. ],
  395. 'object' => [
  396. 'id' => $object['id'],
  397. 'name' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner ? 'Busy' : $object['name'],
  398. 'classified' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner,
  399. ],
  400. ];
  401. if ($object['type'] === 'event' && strpos($action, Event::SUBJECT_OBJECT_DELETE) === false && $this->appManager->isEnabledForUser('calendar')) {
  402. $params['object']['link']['object_uri'] = $objectData['uri'];
  403. $params['object']['link']['calendar_uri'] = $calendarData['uri'];
  404. $params['object']['link']['owner'] = $owner;
  405. }
  406. $event->setAffectedUser($user)
  407. ->setSubject(
  408. $user === $currentUser ? $action . '_self' : $action,
  409. $params
  410. );
  411. $this->activityManager->publish($event);
  412. }
  413. }
  414. /**
  415. * @param array $objectData
  416. * @return string[]|bool
  417. */
  418. protected function getObjectNameAndType(array $objectData) {
  419. $vObject = Reader::read($objectData['calendardata']);
  420. $component = $componentType = null;
  421. foreach ($vObject->getComponents() as $component) {
  422. if (in_array($component->name, ['VEVENT', 'VTODO'])) {
  423. $componentType = $component->name;
  424. break;
  425. }
  426. }
  427. if (!$componentType) {
  428. // Calendar objects must have a VEVENT or VTODO component
  429. return false;
  430. }
  431. if ($componentType === 'VEVENT') {
  432. return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'event'];
  433. }
  434. return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'todo', 'status' => (string) $component->STATUS];
  435. }
  436. /**
  437. * Get all users that have access to a given calendar
  438. *
  439. * @param array $shares
  440. * @return string[]
  441. */
  442. protected function getUsersForShares(array $shares) {
  443. $users = $groups = [];
  444. foreach ($shares as $share) {
  445. $principal = explode('/', $share['{http://owncloud.org/ns}principal']);
  446. if ($principal[1] === 'users') {
  447. $users[] = $principal[2];
  448. } elseif ($principal[1] === 'groups') {
  449. $groups[] = $principal[2];
  450. }
  451. }
  452. if (!empty($groups)) {
  453. foreach ($groups as $gid) {
  454. $group = $this->groupManager->get($gid);
  455. if ($group instanceof IGroup) {
  456. foreach ($group->getUsers() as $user) {
  457. $users[] = $user->getUID();
  458. }
  459. }
  460. }
  461. }
  462. return array_unique($users);
  463. }
  464. }