ExternalCalendar.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\CalDAV\Integration;
  24. use Sabre\CalDAV;
  25. use Sabre\DAV;
  26. /**
  27. * Class ExternalCalendar
  28. *
  29. * @package OCA\DAV\CalDAV\Integration
  30. * @since 19.0.0
  31. */
  32. abstract class ExternalCalendar implements CalDAV\ICalendar, DAV\IProperties {
  33. /** @var string */
  34. private const PREFIX = 'app-generated';
  35. /**
  36. * @var string
  37. *
  38. * Double dash is a valid delimiter,
  39. * because it will always split the calendarURIs correctly:
  40. * - our prefix contains only one dash and won't be split
  41. * - appIds are not allowed to contain dashes as per spec:
  42. * > must contain only lowercase ASCII characters and underscore
  43. * - explode has a limit of three, so even if the app-generated
  44. * calendar uri has double dashes, it won't be split
  45. */
  46. private const DELIMITER = '--';
  47. /** @var string */
  48. private $appId;
  49. /** @var string */
  50. private $calendarUri;
  51. /**
  52. * ExternalCalendar constructor.
  53. *
  54. * @param string $appId
  55. * @param string $calendarUri
  56. */
  57. public function __construct(string $appId, string $calendarUri) {
  58. $this->appId = $appId;
  59. $this->calendarUri = $calendarUri;
  60. }
  61. /**
  62. * @inheritDoc
  63. */
  64. final public function getName() {
  65. return implode(self::DELIMITER, [
  66. self::PREFIX,
  67. $this->appId,
  68. $this->calendarUri,
  69. ]);
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. final public function setName($name) {
  75. throw new DAV\Exception\MethodNotAllowed('Renaming calendars is not yet supported');
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. final public function createDirectory($name) {
  81. throw new DAV\Exception\MethodNotAllowed('Creating collections in calendar objects is not allowed');
  82. }
  83. /**
  84. * Checks whether the calendar uri is app-generated
  85. *
  86. * @param string $calendarUri
  87. * @return bool
  88. */
  89. public static function isAppGeneratedCalendar(string $calendarUri):bool {
  90. return strpos($calendarUri, self::PREFIX) === 0 && substr_count($calendarUri, self::DELIMITER) >= 2;
  91. }
  92. /**
  93. * Splits an app-generated calendar-uri into appId and calendarUri
  94. *
  95. * @param string $calendarUri
  96. * @return array
  97. */
  98. public static function splitAppGeneratedCalendarUri(string $calendarUri):array {
  99. $array = array_slice(explode(self::DELIMITER, $calendarUri, 3), 1);
  100. // Check the array has expected amount of elements
  101. // and none of them is an empty string
  102. if (\count($array) !== 2 || \in_array('', $array, true)) {
  103. throw new \InvalidArgumentException('Provided calendar uri was not app-generated');
  104. }
  105. return $array;
  106. }
  107. /**
  108. * Checks whether a calendar-name, the user wants to create, violates
  109. * the reserved name for calendar uris
  110. *
  111. * @param string $calendarUri
  112. * @return bool
  113. */
  114. public static function doesViolateReservedName(string $calendarUri):bool {
  115. return strpos($calendarUri, self::PREFIX) === 0;
  116. }
  117. }