ContactsManager.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  136. *
  137. * @return IAddressBook[]
  138. * @since 16.0.0
  139. */
  140. public function getUserAddressBooks(): array {
  141. $this->loadAddressBooks();
  142. return $this->addressBooks;
  143. }
  144. /**
  145. * removes all registered address book instances
  146. */
  147. public function clear() {
  148. $this->addressBooks = [];
  149. $this->addressBookLoaders = [];
  150. }
  151. /**
  152. * @var IAddressBook[] which holds all registered address books
  153. */
  154. private $addressBooks = [];
  155. /**
  156. * @var \Closure[] to call to load/register address books
  157. */
  158. private $addressBookLoaders = [];
  159. /**
  160. * In order to improve lazy loading a closure can be registered which will be called in case
  161. * address books are actually requested
  162. *
  163. * @param \Closure $callable
  164. */
  165. public function register(\Closure $callable) {
  166. $this->addressBookLoaders[] = $callable;
  167. }
  168. /**
  169. * Get (and load when needed) the address book for $key
  170. *
  171. * @param string $addressBookKey
  172. * @return IAddressBook
  173. */
  174. protected function getAddressBook($addressBookKey) {
  175. $this->loadAddressBooks();
  176. if (!array_key_exists($addressBookKey, $this->addressBooks)) {
  177. return null;
  178. }
  179. return $this->addressBooks[$addressBookKey];
  180. }
  181. /**
  182. * Load all address books registered with 'register'
  183. */
  184. protected function loadAddressBooks() {
  185. foreach ($this->addressBookLoaders as $callable) {
  186. $callable($this);
  187. }
  188. $this->addressBookLoaders = [];
  189. }
  190. }