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.

ExternalAddressBook.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  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\CardDAV\Integration;
  25. use Sabre\CardDAV\IAddressBook;
  26. use Sabre\DAV;
  27. /**
  28. * @since 19.0.0
  29. */
  30. abstract class ExternalAddressBook implements IAddressBook, DAV\IProperties {
  31. /** @var string */
  32. private const PREFIX = 'app-generated';
  33. /**
  34. * @var string
  35. *
  36. * Double dash is a valid delimiter,
  37. * because it will always split the URIs correctly:
  38. * - our prefix contains only one dash and won't be split
  39. * - appIds are not allowed to contain dashes as per spec:
  40. * > must contain only lowercase ASCII characters and underscore
  41. * - explode has a limit of three, so even if the app-generated
  42. * URI has double dashes, it won't be split
  43. */
  44. private const DELIMITER = '--';
  45. /** @var string */
  46. private $appId;
  47. /** @var string */
  48. private $uri;
  49. /**
  50. * @param string $appId
  51. * @param string $uri
  52. */
  53. public function __construct(string $appId, string $uri) {
  54. $this->appId = $appId;
  55. $this->uri = $uri;
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. final public function getName() {
  61. return implode(self::DELIMITER, [
  62. self::PREFIX,
  63. $this->appId,
  64. $this->uri,
  65. ]);
  66. }
  67. /**
  68. * @inheritDoc
  69. */
  70. final public function setName($name) {
  71. throw new DAV\Exception\MethodNotAllowed('Renaming address books is not yet supported');
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. final public function createDirectory($name) {
  77. throw new DAV\Exception\MethodNotAllowed('Creating collections in address book objects is not allowed');
  78. }
  79. /**
  80. * Checks whether the address book uri is app-generated
  81. *
  82. * @param string $uri
  83. *
  84. * @return bool
  85. */
  86. public static function isAppGeneratedAddressBook(string $uri): bool {
  87. return strpos($uri, self::PREFIX) === 0 && substr_count($uri, self::DELIMITER) >= 2;
  88. }
  89. /**
  90. * Splits an app-generated uri into appId and uri
  91. *
  92. * @param string $uri
  93. *
  94. * @return array
  95. */
  96. public static function splitAppGeneratedAddressBookUri(string $uri): array {
  97. $array = array_slice(explode(self::DELIMITER, $uri, 3), 1);
  98. // Check the array has expected amount of elements
  99. // and none of them is an empty string
  100. if (\count($array) !== 2 || \in_array('', $array, true)) {
  101. throw new \InvalidArgumentException('Provided address book uri was not app-generated');
  102. }
  103. return $array;
  104. }
  105. /**
  106. * Checks whether a address book name the user wants to create violates
  107. * the reserved name for URIs
  108. *
  109. * @param string $uri
  110. *
  111. * @return bool
  112. */
  113. public static function doesViolateReservedName(string $uri): bool {
  114. return strpos($uri, self::PREFIX) === 0;
  115. }
  116. }