1
0

ExternalAddressBook.php 3.3 KB

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