Converter.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // structured prop: https://www.rfc-editor.org/rfc/rfc6350.html#section-6.3.1
  86. // post office box;extended address;street address;locality;region;postal code;country
  87. $vCard->add(
  88. new Text(
  89. $vCard,
  90. 'ADR',
  91. [ '', '', '', $property->getValue(), '', '', '' ],
  92. [
  93. 'TYPE' => 'OTHER',
  94. 'X-NC-SCOPE' => $scope,
  95. ]
  96. )
  97. );
  98. break;
  99. case IAccountManager::PROPERTY_TWITTER:
  100. $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER', 'X-NC-SCOPE' => $scope]));
  101. break;
  102. case IAccountManager::PROPERTY_ORGANISATION:
  103. $vCard->add(new Text($vCard, 'ORG', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  104. break;
  105. case IAccountManager::PROPERTY_ROLE:
  106. $vCard->add(new Text($vCard, 'TITLE', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  107. break;
  108. }
  109. }
  110. // Local properties
  111. $managers = $user->getManagerUids();
  112. // X-MANAGERSNAME only allows a single value, so we take the first manager
  113. if (isset($managers[0])) {
  114. $displayName = $this->userManager->getDisplayName($managers[0]);
  115. // Only set the manager if a user object is found
  116. if ($displayName !== null) {
  117. $vCard->add(new Text($vCard, 'X-MANAGERSNAME', $displayName, [
  118. 'uid' => $managers[0],
  119. 'X-NC-SCOPE' => IAccountManager::SCOPE_LOCAL,
  120. ]));
  121. }
  122. }
  123. if ($publish && !empty($cloudId)) {
  124. $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
  125. $vCard->validate();
  126. return $vCard;
  127. }
  128. return null;
  129. }
  130. public function splitFullName(string $fullName): array {
  131. // Very basic western style parsing. I'm not gonna implement
  132. // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
  133. $elements = explode(' ', $fullName);
  134. $result = ['', '', '', '', ''];
  135. if (count($elements) > 2) {
  136. $result[0] = implode(' ', array_slice($elements, count($elements) - 1));
  137. $result[1] = $elements[0];
  138. $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
  139. } elseif (count($elements) === 2) {
  140. $result[0] = $elements[1];
  141. $result[1] = $elements[0];
  142. } else {
  143. $result[0] = $elements[0];
  144. }
  145. return $result;
  146. }
  147. private function getAvatarImage(IUser $user): ?IImage {
  148. try {
  149. return $user->getAvatarImage(512);
  150. } catch (Exception $ex) {
  151. return null;
  152. }
  153. }
  154. }