1
0

DeletedCalendarObjectsCollection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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\Trashbin;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use Sabre\CalDAV\ICalendarObjectContainer;
  27. use Sabre\DAV\Exception\BadRequest;
  28. use Sabre\DAV\Exception\Forbidden;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\Exception\NotImplemented;
  31. use Sabre\DAVACL\ACLTrait;
  32. use Sabre\DAVACL\IACL;
  33. use function array_map;
  34. use function implode;
  35. use function preg_match;
  36. class DeletedCalendarObjectsCollection implements ICalendarObjectContainer, IACL {
  37. use ACLTrait;
  38. public const NAME = 'objects';
  39. /** @var CalDavBackend */
  40. protected $caldavBackend;
  41. /** @var mixed[] */
  42. private $principalInfo;
  43. public function __construct(CalDavBackend $caldavBackend,
  44. array $principalInfo) {
  45. $this->caldavBackend = $caldavBackend;
  46. $this->principalInfo = $principalInfo;
  47. }
  48. /**
  49. * @see \OCA\DAV\CalDAV\Trashbin\DeletedCalendarObjectsCollection::calendarQuery
  50. */
  51. public function getChildren() {
  52. throw new NotImplemented();
  53. }
  54. public function getChild($name) {
  55. if (!preg_match("/(\d+)\\.ics/", $name, $matches)) {
  56. throw new NotFound();
  57. }
  58. $data = $this->caldavBackend->getCalendarObjectById(
  59. $this->principalInfo['uri'],
  60. (int) $matches[1],
  61. );
  62. // If the object hasn't been deleted yet then we don't want to find it here
  63. if ($data === null) {
  64. throw new NotFound();
  65. }
  66. if (!isset($data['deleted_at'])) {
  67. throw new BadRequest('The calendar object you\'re trying to restore is not marked as deleted');
  68. }
  69. return new DeletedCalendarObject(
  70. $this->getRelativeObjectPath($data),
  71. $data,
  72. $this->principalInfo['uri'],
  73. $this->caldavBackend
  74. );
  75. }
  76. public function createFile($name, $data = null) {
  77. throw new Forbidden();
  78. }
  79. public function createDirectory($name) {
  80. throw new Forbidden();
  81. }
  82. public function childExists($name) {
  83. try {
  84. $this->getChild($name);
  85. } catch (NotFound $e) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. public function delete() {
  91. throw new Forbidden();
  92. }
  93. public function getName(): string {
  94. return self::NAME;
  95. }
  96. public function setName($name) {
  97. throw new Forbidden();
  98. }
  99. public function getLastModified(): int {
  100. return 0;
  101. }
  102. public function calendarQuery(array $filters) {
  103. return array_map(function (array $calendarObjectInfo) {
  104. return $this->getRelativeObjectPath($calendarObjectInfo);
  105. }, $this->caldavBackend->getDeletedCalendarObjectsByPrincipal($this->principalInfo['uri']));
  106. }
  107. private function getRelativeObjectPath(array $calendarInfo): string {
  108. return implode(
  109. '.',
  110. [$calendarInfo['id'], 'ics'],
  111. );
  112. }
  113. public function getOwner() {
  114. return $this->principalInfo['uri'];
  115. }
  116. public function getACL(): array {
  117. return [
  118. [
  119. 'privilege' => '{DAV:}read',
  120. 'principal' => $this->getOwner(),
  121. 'protected' => true,
  122. ],
  123. [
  124. 'privilege' => '{DAV:}unbind',
  125. 'principal' => '{DAV:}owner',
  126. 'protected' => true,
  127. ]
  128. ];
  129. }
  130. }