ContactsManager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Tobia De Koninck <tobia@ledfan.be>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC;
  29. use OCP\Constants;
  30. use OCP\Contacts\IManager;
  31. use OCP\IAddressBook;
  32. class ContactsManager implements IManager {
  33. /**
  34. * This function is used to search and find contacts within the users address books.
  35. * In case $pattern is empty all contacts will be returned.
  36. *
  37. * @param string $pattern which should match within the $searchProperties
  38. * @param array $searchProperties defines the properties within the query pattern should match
  39. * @param array $options = array() to define the search behavior
  40. * - 'escape_like_param' - If set to false wildcards _ and % are not escaped
  41. * - 'limit' - Set a numeric limit for the search results
  42. * - 'offset' - Set the offset for the limited search results
  43. * - 'enumeration' - (since 23.0.0) Whether user enumeration on system address book is allowed
  44. * - 'fullmatch' - (since 23.0.0) Whether matching on full detail in system address book is allowed
  45. * - 'strict_search' - (since 23.0.0) Whether the search pattern is full string or partial search
  46. * @psalm-param array{escape_like_param?: bool, limit?: int, offset?: int, enumeration?: bool, fullmatch?: bool, strict_search?: bool} $options
  47. * @return array an array of contacts which are arrays of key-value-pairs
  48. */
  49. public function search($pattern, $searchProperties = [], $options = []) {
  50. $this->loadAddressBooks();
  51. $result = [];
  52. foreach ($this->addressBooks as $addressBook) {
  53. $searchOptions = $options;
  54. $strictSearch = array_key_exists('strict_search', $options) && $options['strict_search'] === true;
  55. if ($addressBook->isSystemAddressBook()) {
  56. $fullMatch = !\array_key_exists('fullmatch', $options) || $options['fullmatch'] !== false;
  57. if (!$fullMatch) {
  58. // Neither full match is allowed, so skip the system address book
  59. continue;
  60. }
  61. if ($strictSearch) {
  62. $searchOptions['wildcard'] = false;
  63. } else {
  64. $searchOptions['wildcard'] = !\array_key_exists('enumeration', $options) || $options['enumeration'] !== false;
  65. }
  66. } else {
  67. $searchOptions['wildcard'] = !$strictSearch;
  68. }
  69. $r = $addressBook->search($pattern, $searchProperties, $searchOptions);
  70. $contacts = [];
  71. foreach ($r as $c) {
  72. $c['addressbook-key'] = $addressBook->getKey();
  73. $contacts[] = $c;
  74. }
  75. $result = array_merge($result, $contacts);
  76. }
  77. return $result;
  78. }
  79. /**
  80. * This function can be used to delete the contact identified by the given id
  81. *
  82. * @param int $id the unique identifier to a contact
  83. * @param string $address_book_key identifier of the address book in which the contact shall be deleted
  84. * @return bool successful or not
  85. */
  86. public function delete($id, $address_book_key) {
  87. $addressBook = $this->getAddressBook($address_book_key);
  88. if (!$addressBook) {
  89. return null;
  90. }
  91. if ($addressBook->getPermissions() & Constants::PERMISSION_DELETE) {
  92. return $addressBook->delete($id);
  93. }
  94. return null;
  95. }
  96. /**
  97. * This function is used to create a new contact if 'id' is not given or not present.
  98. * Otherwise the contact will be updated by replacing the entire data set.
  99. *
  100. * @param array $properties this array if key-value-pairs defines a contact
  101. * @param string $address_book_key identifier of the address book in which the contact shall be created or updated
  102. * @return array representing the contact just created or updated
  103. */
  104. public function createOrUpdate($properties, $address_book_key) {
  105. $addressBook = $this->getAddressBook($address_book_key);
  106. if (!$addressBook) {
  107. return null;
  108. }
  109. if ($addressBook->getPermissions() & Constants::PERMISSION_CREATE) {
  110. return $addressBook->createOrUpdate($properties);
  111. }
  112. return null;
  113. }
  114. /**
  115. * Check if contacts are available (e.g. contacts app enabled)
  116. *
  117. * @return bool true if enabled, false if not
  118. */
  119. public function isEnabled() {
  120. return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
  121. }
  122. /**
  123. * @param IAddressBook $addressBook
  124. */
  125. public function registerAddressBook(IAddressBook $addressBook) {
  126. $this->addressBooks[$addressBook->getKey()] = $addressBook;
  127. }
  128. /**
  129. * @param IAddressBook $addressBook
  130. */
  131. public function unregisterAddressBook(IAddressBook $addressBook) {
  132. unset($this->addressBooks[$addressBook->getKey()]);
  133. }
  134. /**
  135. * Return a list of the user's addressbooks display names
  136. * ! The addressBook displayName are not unique, please use getUserAddressBooks
  137. *
  138. * @return IAddressBook[]
  139. * @since 6.0.0
  140. * @deprecated 16.0.0 - Use `$this->getUserAddressBooks()` instead
  141. */
  142. public function getAddressBooks() {
  143. $this->loadAddressBooks();
  144. $result = [];
  145. foreach ($this->addressBooks as $addressBook) {
  146. $result[$addressBook->getKey()] = $addressBook->getDisplayName();
  147. }
  148. return $result;
  149. }
  150. /**
  151. * Return a list of the user's addressbooks
  152. *
  153. * @return IAddressBook[]
  154. * @since 16.0.0
  155. */
  156. public function getUserAddressBooks(): array {
  157. $this->loadAddressBooks();
  158. return $this->addressBooks;
  159. }
  160. /**
  161. * removes all registered address book instances
  162. */
  163. public function clear() {
  164. $this->addressBooks = [];
  165. $this->addressBookLoaders = [];
  166. }
  167. /**
  168. * @var IAddressBook[] which holds all registered address books
  169. */
  170. private $addressBooks = [];
  171. /**
  172. * @var \Closure[] to call to load/register address books
  173. */
  174. private $addressBookLoaders = [];
  175. /**
  176. * In order to improve lazy loading a closure can be registered which will be called in case
  177. * address books are actually requested
  178. *
  179. * @param \Closure $callable
  180. */
  181. public function register(\Closure $callable) {
  182. $this->addressBookLoaders[] = $callable;
  183. }
  184. /**
  185. * Get (and load when needed) the address book for $key
  186. *
  187. * @param string $addressBookKey
  188. * @return IAddressBook
  189. */
  190. protected function getAddressBook($addressBookKey) {
  191. $this->loadAddressBooks();
  192. if (!array_key_exists($addressBookKey, $this->addressBooks)) {
  193. return null;
  194. }
  195. return $this->addressBooks[$addressBookKey];
  196. }
  197. /**
  198. * Load all address books registered with 'register'
  199. */
  200. protected function loadAddressBooks() {
  201. foreach ($this->addressBookLoaders as $callable) {
  202. $callable($this);
  203. }
  204. $this->addressBookLoaders = [];
  205. }
  206. }