ContactsManager.php 7.0 KB

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