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

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