Converter.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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 OCA\DAV\CardDAV;
  29. use Exception;
  30. use OCP\Accounts\IAccountManager;
  31. use OCP\IImage;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use Sabre\VObject\Component\VCard;
  35. use Sabre\VObject\Property\Text;
  36. class Converter {
  37. /** @var IAccountManager */
  38. private $accountManager;
  39. private IUserManager $userManager;
  40. public function __construct(IAccountManager $accountManager,
  41. IUserManager $userManager) {
  42. $this->accountManager = $accountManager;
  43. $this->userManager = $userManager;
  44. }
  45. public function createCardFromUser(IUser $user): ?VCard {
  46. $userProperties = $this->accountManager->getAccount($user)->getAllProperties();
  47. $uid = $user->getUID();
  48. $cloudId = $user->getCloudId();
  49. $image = $this->getAvatarImage($user);
  50. $vCard = new VCard();
  51. $vCard->VERSION = '3.0';
  52. $vCard->UID = $uid;
  53. $publish = false;
  54. foreach ($userProperties as $property) {
  55. if ($property->getName() !== IAccountManager::PROPERTY_AVATAR && empty($property->getValue())) {
  56. continue;
  57. }
  58. $scope = $property->getScope();
  59. // Do not write private data to the system address book at all
  60. if ($scope === IAccountManager::SCOPE_PRIVATE || empty($scope)) {
  61. continue;
  62. }
  63. $publish = true;
  64. switch ($property->getName()) {
  65. case IAccountManager::PROPERTY_DISPLAYNAME:
  66. $vCard->add(new Text($vCard, 'FN', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  67. $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()), ['X-NC-SCOPE' => $scope]));
  68. break;
  69. case IAccountManager::PROPERTY_AVATAR:
  70. if ($image !== null) {
  71. $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType(), ['X-NC-SCOPE' => $scope]]);
  72. }
  73. break;
  74. case IAccountManager::COLLECTION_EMAIL:
  75. case IAccountManager::PROPERTY_EMAIL:
  76. $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
  77. break;
  78. case IAccountManager::PROPERTY_WEBSITE:
  79. $vCard->add(new Text($vCard, 'URL', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  80. break;
  81. case IAccountManager::PROPERTY_PHONE:
  82. $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'VOICE', 'X-NC-SCOPE' => $scope]));
  83. break;
  84. case IAccountManager::PROPERTY_ADDRESS:
  85. $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
  86. break;
  87. case IAccountManager::PROPERTY_TWITTER:
  88. $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER', 'X-NC-SCOPE' => $scope]));
  89. break;
  90. case IAccountManager::PROPERTY_ORGANISATION:
  91. $vCard->add(new Text($vCard, 'ORG', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  92. break;
  93. case IAccountManager::PROPERTY_ROLE:
  94. $vCard->add(new Text($vCard, 'TITLE', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  95. break;
  96. }
  97. }
  98. // Local properties
  99. $managers = $user->getManagerUids();
  100. // X-MANAGERSNAME only allows a single value, so we take the first manager
  101. if (isset($managers[0])) {
  102. $displayName = $this->userManager->getDisplayName($managers[0]);
  103. // Only set the manager if a user object is found
  104. if ($displayName !== null) {
  105. $vCard->add(new Text($vCard, 'X-MANAGERSNAME', $displayName, [
  106. 'uid' => $managers[0],
  107. 'X-NC-SCOPE' => IAccountManager::SCOPE_LOCAL,
  108. ]));
  109. }
  110. }
  111. if ($publish && !empty($cloudId)) {
  112. $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
  113. $vCard->validate();
  114. return $vCard;
  115. }
  116. return null;
  117. }
  118. public function splitFullName(string $fullName): array {
  119. // Very basic western style parsing. I'm not gonna implement
  120. // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
  121. $elements = explode(' ', $fullName);
  122. $result = ['', '', '', '', ''];
  123. if (count($elements) > 2) {
  124. $result[0] = implode(' ', array_slice($elements, count($elements) - 1));
  125. $result[1] = $elements[0];
  126. $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
  127. } elseif (count($elements) === 2) {
  128. $result[0] = $elements[1];
  129. $result[1] = $elements[0];
  130. } else {
  131. $result[0] = $elements[0];
  132. }
  133. return $result;
  134. }
  135. private function getAvatarImage(IUser $user): ?IImage {
  136. try {
  137. return $user->getAvatarImage(512);
  138. } catch (Exception $ex) {
  139. return null;
  140. }
  141. }
  142. }