CalendarObject.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV;
  25. use Sabre\VObject\Component;
  26. use Sabre\VObject\Property;
  27. use Sabre\VObject\Reader;
  28. class CalendarObject extends \Sabre\CalDAV\CalendarObject {
  29. /**
  30. * CalendarObject constructor.
  31. *
  32. * @param CalDavBackend $caldavBackend
  33. * @param array $calendarInfo
  34. * @param array $objectData
  35. */
  36. public function __construct(CalDavBackend $caldavBackend, array $calendarInfo,
  37. array $objectData) {
  38. parent::__construct($caldavBackend, $calendarInfo, $objectData);
  39. if ($this->isShared()) {
  40. unset($this->objectData['size']);
  41. }
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. function get() {
  47. $data = parent::get();
  48. if (!$this->isShared()) {
  49. return $data;
  50. }
  51. $vObject = Reader::read($data);
  52. // remove VAlarms if calendar is shared read-only
  53. if (!$this->canWrite()) {
  54. $this->removeVAlarms($vObject);
  55. }
  56. // shows as busy if event is declared confidential
  57. if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
  58. $this->createConfidentialObject($vObject);
  59. }
  60. return $vObject->serialize();
  61. }
  62. protected function isShared() {
  63. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  64. return false;
  65. }
  66. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  67. }
  68. /**
  69. * @param Component\VCalendar $vObject
  70. * @return void
  71. */
  72. private static function createConfidentialObject(Component\VCalendar $vObject) {
  73. /** @var Component $vElement */
  74. $vElement = null;
  75. if(isset($vObject->VEVENT)) {
  76. $vElement = $vObject->VEVENT;
  77. }
  78. if(isset($vObject->VJOURNAL)) {
  79. $vElement = $vObject->VJOURNAL;
  80. }
  81. if(isset($vObject->VTODO)) {
  82. $vElement = $vObject->VTODO;
  83. }
  84. if(!is_null($vElement)) {
  85. foreach ($vElement->children() as &$property) {
  86. /** @var Property $property */
  87. switch($property->name) {
  88. case 'CREATED':
  89. case 'DTSTART':
  90. case 'RRULE':
  91. case 'DURATION':
  92. case 'DTEND':
  93. case 'CLASS':
  94. case 'UID':
  95. break;
  96. case 'SUMMARY':
  97. $property->setValue('Busy');
  98. break;
  99. default:
  100. $vElement->__unset($property->name);
  101. unset($property);
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * @param Component\VCalendar $vObject
  109. * @return void
  110. */
  111. private function removeVAlarms(Component\VCalendar $vObject) {
  112. $subcomponents = $vObject->getComponents();
  113. foreach($subcomponents as $subcomponent) {
  114. unset($subcomponent->VALARM);
  115. }
  116. }
  117. /**
  118. * @return bool
  119. */
  120. private function canWrite() {
  121. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  122. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  123. }
  124. return true;
  125. }
  126. }