1
0

ContactsManager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * - 'types' boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array
  41. * example: ['id' => 5, 'FN' => 'Thomas Tanghus', 'EMAIL' => ['type => 'HOME', 'value' => 'g@h.i']]
  42. * - 'escape_like_param' - If set to false wildcards _ and % are not escaped
  43. * - 'limit' - Set a numeric limit for the search results
  44. * - 'offset' - Set the offset for the limited search results
  45. * - 'enumeration' - (since 23.0.0) Whether user enumeration on system address book is allowed
  46. * - 'fullmatch' - (since 23.0.0) Whether matching on full detail in system address book is allowed
  47. * - 'strict_search' - (since 23.0.0) Whether the search pattern is full string or partial search
  48. * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, enumeration?: bool, fullmatch?: bool, strict_search?: bool} $options
  49. * @return array an array of contacts which are arrays of key-value-pairs
  50. */
  51. public function search($pattern, $searchProperties = [], $options = []) {
  52. $this->loadAddressBooks();
  53. $result = [];
  54. foreach ($this->addressBooks as $addressBook) {
  55. $searchOptions = $options;
  56. $strictSearch = array_key_exists('strict_search', $options) && $options['strict_search'] === true;
  57. if ($addressBook->isSystemAddressBook()) {
  58. $fullMatch = !\array_key_exists('fullmatch', $options) || $options['fullmatch'] !== false;
  59. if (!$fullMatch) {
  60. // Neither full match is allowed, so skip the system address book
  61. continue;
  62. }
  63. if ($strictSearch) {
  64. $searchOptions['wildcard'] = false;
  65. } else {
  66. $searchOptions['wildcard'] = !\array_key_exists('enumeration', $options) || $options['enumeration'] !== false;
  67. }
  68. } else {
  69. $searchOptions['wildcard'] = !$strictSearch;
  70. }
  71. $r = $addressBook->search($pattern, $searchProperties, $searchOptions);
  72. $contacts = [];
  73. foreach ($r as $c) {
  74. $c['addressbook-key'] = $addressBook->getKey();
  75. $contacts[] = $c;
  76. }
  77. $result = array_merge($result, $contacts);
  78. }
  79. return $result;
  80. }
  81. /**
  82. * This function can be used to delete the contact identified by the given id
  83. *
  84. * @param int $id the unique identifier to a contact
  85. * @param string $address_book_key identifier of the address book in which the contact shall be deleted
  86. * @return bool successful or not
  87. */
  88. public function delete($id, $address_book_key) {
  89. $addressBook = $this->getAddressBook($address_book_key);
  90. if (!$addressBook) {
  91. return null;
  92. }
  93. if ($addressBook->getPermissions() & Constants::PERMISSION_DELETE) {
  94. return $addressBook->delete($id);
  95. }
  96. return null;
  97. }
  98. /**
  99. * This function is used to create a new contact if 'id' is not given or not present.
  100. * Otherwise the contact will be updated by replacing the entire data set.
  101. *
  102. * @param array $properties this array if key-value-pairs defines a contact
  103. * @param string $address_book_key identifier of the address book in which the contact shall be created or updated
  104. * @return array representing the contact just created or updated
  105. */
  106. public function createOrUpdate($properties, $address_book_key) {
  107. $addressBook = $this->getAddressBook($address_book_key);
  108. if (!$addressBook) {
  109. return null;
  110. }
  111. if ($addressBook->getPermissions() & Constants::PERMISSION_CREATE) {
  112. return $addressBook->createOrUpdate($properties);
  113. }
  114. return null;
  115. }
  116. /**
  117. * Check if contacts are available (e.g. contacts app enabled)
  118. *
  119. * @return bool true if enabled, false if not
  120. */
  121. public function isEnabled() {
  122. return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
  123. }
  124. /**
  125. * @param IAddressBook $addressBook
  126. */
  127. public function registerAddressBook(IAddressBook $addressBook) {
  128. $this->addressBooks[$addressBook->getKey()] = $addressBook;
  129. }
  130. /**
  131. * @param IAddressBook $addressBook
  132. */
  133. public function unregisterAddressBook(IAddressBook $addressBook) {
  134. unset($this->addressBooks[$addressBook->getKey()]);
  135. }
  136. /**
  137. * Return a list of the user's addressbooks
  138. *
  139. * @return IAddressBook[]
  140. * @since 16.0.0
  141. */
  142. public function getUserAddressBooks(): array {
  143. $this->loadAddressBooks();
  144. return $this->addressBooks;
  145. }
  146. /**
  147. * removes all registered address book instances
  148. */
  149. public function clear() {
  150. $this->addressBooks = [];
  151. $this->addressBookLoaders = [];
  152. }
  153. /**
  154. * @var IAddressBook[] which holds all registered address books
  155. */
  156. private $addressBooks = [];
  157. /**
  158. * @var \Closure[] to call to load/register address books
  159. */
  160. private $addressBookLoaders = [];
  161. /**
  162. * In order to improve lazy loading a closure can be registered which will be called in case
  163. * address books are actually requested
  164. *
  165. * @param \Closure $callable
  166. */
  167. public function register(\Closure $callable) {
  168. $this->addressBookLoaders[] = $callable;
  169. }
  170. /**
  171. * Get (and load when needed) the address book for $key
  172. *
  173. * @param string $addressBookKey
  174. * @return IAddressBook
  175. */
  176. protected function getAddressBook($addressBookKey) {
  177. $this->loadAddressBooks();
  178. if (!array_key_exists($addressBookKey, $this->addressBooks)) {
  179. return null;
  180. }
  181. return $this->addressBooks[$addressBookKey];
  182. }
  183. /**
  184. * Load all address books registered with 'register'
  185. */
  186. protected function loadAddressBooks() {
  187. foreach ($this->addressBookLoaders as $callable) {
  188. $callable($this);
  189. }
  190. $this->addressBookLoaders = [];
  191. }
  192. }