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.

DeletedCalendarObjectsCollection.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 function array_map;
  32. use function implode;
  33. use function preg_match;
  34. class DeletedCalendarObjectsCollection implements ICalendarObjectContainer {
  35. public const NAME = 'objects';
  36. /** @var CalDavBackend */
  37. protected $caldavBackend;
  38. /** @var mixed[] */
  39. private $principalInfo;
  40. public function __construct(CalDavBackend $caldavBackend,
  41. array $principalInfo) {
  42. $this->caldavBackend = $caldavBackend;
  43. $this->principalInfo = $principalInfo;
  44. }
  45. /**
  46. * @see \OCA\DAV\CalDAV\Trashbin\DeletedCalendarObjectsCollection::calendarQuery
  47. */
  48. public function getChildren() {
  49. throw new NotImplemented();
  50. }
  51. public function getChild($name) {
  52. if (!preg_match("/(\d+)\\.ics/", $name, $matches)) {
  53. throw new NotFound();
  54. }
  55. $data = $this->caldavBackend->getCalendarObjectById(
  56. $this->principalInfo['uri'],
  57. (int) $matches[1],
  58. );
  59. // If the object hasn't been deleted yet then we don't want to find it here
  60. if ($data === null) {
  61. throw new NotFound();
  62. }
  63. if (!isset($data['deleted_at'])) {
  64. throw new BadRequest('The calendar object you\'re trying to restore is not marked as deleted');
  65. }
  66. return new DeletedCalendarObject(
  67. $this->getRelativeObjectPath($data),
  68. $data,
  69. $this->principalInfo['uri'],
  70. $this->caldavBackend
  71. );
  72. }
  73. public function createFile($name, $data = null) {
  74. throw new Forbidden();
  75. }
  76. public function createDirectory($name) {
  77. throw new Forbidden();
  78. }
  79. public function childExists($name) {
  80. try {
  81. $this->getChild($name);
  82. } catch (NotFound $e) {
  83. return false;
  84. }
  85. return true;
  86. }
  87. public function delete() {
  88. throw new Forbidden();
  89. }
  90. public function getName(): string {
  91. return self::NAME;
  92. }
  93. public function setName($name) {
  94. throw new Forbidden();
  95. }
  96. public function getLastModified(): int {
  97. return 0;
  98. }
  99. public function calendarQuery(array $filters) {
  100. return array_map(function (array $calendarObjectInfo) {
  101. return $this->getRelativeObjectPath($calendarObjectInfo);
  102. }, $this->caldavBackend->getDeletedCalendarObjectsByPrincipal($this->principalInfo['uri']));
  103. }
  104. private function getRelativeObjectPath(array $calendarInfo): string {
  105. return implode(
  106. '.',
  107. [$calendarInfo['id'], 'ics'],
  108. );
  109. }
  110. }