1
0

CalendarObject.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. */
  27. namespace OCA\DAV\CalDAV;
  28. use OCP\IL10N;
  29. use Sabre\VObject\Component;
  30. use Sabre\VObject\Property;
  31. use Sabre\VObject\Reader;
  32. class CalendarObject extends \Sabre\CalDAV\CalendarObject {
  33. /** @var IL10N */
  34. protected $l10n;
  35. /**
  36. * CalendarObject constructor.
  37. *
  38. * @param CalDavBackend $caldavBackend
  39. * @param IL10N $l10n
  40. * @param array $calendarInfo
  41. * @param array $objectData
  42. */
  43. public function __construct(CalDavBackend $caldavBackend, IL10N $l10n,
  44. array $calendarInfo,
  45. array $objectData) {
  46. parent::__construct($caldavBackend, $calendarInfo, $objectData);
  47. if ($this->isShared()) {
  48. unset($this->objectData['size']);
  49. }
  50. $this->l10n = $l10n;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function get() {
  56. $data = parent::get();
  57. if (!$this->isShared()) {
  58. return $data;
  59. }
  60. $vObject = Reader::read($data);
  61. // remove VAlarms if calendar is shared read-only
  62. if (!$this->canWrite()) {
  63. $this->removeVAlarms($vObject);
  64. }
  65. // shows as busy if event is declared confidential
  66. if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
  67. $this->createConfidentialObject($vObject);
  68. }
  69. return $vObject->serialize();
  70. }
  71. protected function isShared() {
  72. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  73. return false;
  74. }
  75. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  76. }
  77. /**
  78. * @param Component\VCalendar $vObject
  79. * @return void
  80. */
  81. private function createConfidentialObject(Component\VCalendar $vObject) {
  82. /** @var Component $vElement */
  83. $vElement = null;
  84. if (isset($vObject->VEVENT)) {
  85. $vElement = $vObject->VEVENT;
  86. }
  87. if (isset($vObject->VJOURNAL)) {
  88. $vElement = $vObject->VJOURNAL;
  89. }
  90. if (isset($vObject->VTODO)) {
  91. $vElement = $vObject->VTODO;
  92. }
  93. if (!is_null($vElement)) {
  94. foreach ($vElement->children() as &$property) {
  95. /** @var Property $property */
  96. switch ($property->name) {
  97. case 'CREATED':
  98. case 'DTSTART':
  99. case 'RRULE':
  100. case 'DURATION':
  101. case 'DTEND':
  102. case 'CLASS':
  103. case 'UID':
  104. break;
  105. case 'SUMMARY':
  106. $property->setValue($this->l10n->t('Busy'));
  107. break;
  108. default:
  109. $vElement->__unset($property->name);
  110. unset($property);
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * @param Component\VCalendar $vObject
  118. * @return void
  119. */
  120. private function removeVAlarms(Component\VCalendar $vObject) {
  121. $subcomponents = $vObject->getComponents();
  122. foreach ($subcomponents as $subcomponent) {
  123. unset($subcomponent->VALARM);
  124. }
  125. }
  126. /**
  127. * @return bool
  128. */
  129. private function canWrite() {
  130. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  131. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  132. }
  133. return true;
  134. }
  135. }