1<?php
2/**
3 * @copyright Copyright (c) 2016, ownCloud, Inc.
4 * @copyright Copyright (c) 2017, Georg Ehrke
5 * @copyright Copyright (c) 2020, Gary Kim <gary@garykim.dev>
6 *
7 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
8 * @author Gary Kim <gary@garykim.dev>
9 * @author Georg Ehrke <oc.list@georgehrke.com>
10 * @author Thomas Müller <thomas.mueller@tmit.eu>
11 *
12 * @license AGPL-3.0
13 *
14 * This code is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Affero General Public License, version 3,
16 * as published by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Affero General Public License for more details.
22 *
23 * You should have received a copy of the GNU Affero General Public License, version 3,
24 * along with this program. If not, see <http://www.gnu.org/licenses/>
25 *
26 */
27namespace OCA\DAV\CalDAV;
28
29use OCP\IL10N;
30use Sabre\VObject\Component;
31use Sabre\VObject\Property;
32use Sabre\VObject\Reader;
33
34class CalendarObject extends \Sabre\CalDAV\CalendarObject {
35
36	/** @var IL10N */
37	protected $l10n;
38
39	/**
40	 * CalendarObject constructor.
41	 *
42	 * @param CalDavBackend $caldavBackend
43	 * @param IL10N $l10n
44	 * @param array $calendarInfo
45	 * @param array $objectData
46	 */
47	public function __construct(CalDavBackend $caldavBackend, IL10N $l10n,
48								array $calendarInfo,
49								array $objectData) {
50		parent::__construct($caldavBackend, $calendarInfo, $objectData);
51
52		if ($this->isShared()) {
53			unset($this->objectData['size']);
54		}
55
56		$this->l10n = $l10n;
57	}
58
59	/**
60	 * @inheritdoc
61	 */
62	public function get() {
63		$data = parent::get();
64
65		if (!$this->isShared()) {
66			return $data;
67		}
68
69		$vObject = Reader::read($data);
70
71		// remove VAlarms if calendar is shared read-only
72		if (!$this->canWrite()) {
73			$this->removeVAlarms($vObject);
74		}
75
76		// shows as busy if event is declared confidential
77		if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
78			$this->createConfidentialObject($vObject);
79		}
80
81		return $vObject->serialize();
82	}
83
84	public function getId(): int {
85		return (int) $this->objectData['id'];
86	}
87
88	protected function isShared() {
89		if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
90			return false;
91		}
92
93		return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
94	}
95
96	/**
97	 * @param Component\VCalendar $vObject
98	 * @return void
99	 */
100	private function createConfidentialObject(Component\VCalendar $vObject) {
101		/** @var Component $vElement */
102		$vElement = null;
103		if (isset($vObject->VEVENT)) {
104			$vElement = $vObject->VEVENT;
105		}
106		if (isset($vObject->VJOURNAL)) {
107			$vElement = $vObject->VJOURNAL;
108		}
109		if (isset($vObject->VTODO)) {
110			$vElement = $vObject->VTODO;
111		}
112		if (!is_null($vElement)) {
113			foreach ($vElement->children() as &$property) {
114				/** @var Property $property */
115				switch ($property->name) {
116					case 'CREATED':
117					case 'DTSTART':
118					case 'RRULE':
119					case 'DURATION':
120					case 'DTEND':
121					case 'CLASS':
122					case 'UID':
123						break;
124					case 'SUMMARY':
125						$property->setValue($this->l10n->t('Busy'));
126						break;
127					default:
128						$vElement->__unset($property->name);
129						unset($property);
130						break;
131				}
132			}
133		}
134	}
135
136	/**
137	 * @param Component\VCalendar $vObject
138	 * @return void
139	 */
140	private function removeVAlarms(Component\VCalendar $vObject) {
141		$subcomponents = $vObject->getComponents();
142
143		foreach ($subcomponents as $subcomponent) {
144			unset($subcomponent->VALARM);
145		}
146	}
147
148	/**
149	 * @return bool
150	 */
151	private function canWrite() {
152		if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
153			return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
154		}
155		return true;
156	}
157}
158