Converter.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use Sabre\VObject\Component\VCard;
  36. use Sabre\VObject\Property\Text;
  37. class Converter {
  38. /** @var IURLGenerator */
  39. private $urlGenerator;
  40. /** @var IAccountManager */
  41. private $accountManager;
  42. private IUserManager $userManager;
  43. public function __construct(IAccountManager $accountManager,
  44. IUserManager $userManager, IURLGenerator $urlGenerator) {
  45. $this->accountManager = $accountManager;
  46. $this->userManager = $userManager;
  47. $this->urlGenerator = $urlGenerator;
  48. }
  49. public function createCardFromUser(IUser $user): ?VCard {
  50. $userProperties = $this->accountManager->getAccount($user)->getAllProperties();
  51. $uid = $user->getUID();
  52. $cloudId = $user->getCloudId();
  53. $image = $this->getAvatarImage($user);
  54. $vCard = new VCard();
  55. $vCard->VERSION = '3.0';
  56. $vCard->UID = $uid;
  57. $publish = false;
  58. foreach ($userProperties as $property) {
  59. if ($property->getName() !== IAccountManager::PROPERTY_AVATAR && empty($property->getValue())) {
  60. continue;
  61. }
  62. $scope = $property->getScope();
  63. // Do not write private data to the system address book at all
  64. if ($scope === IAccountManager::SCOPE_PRIVATE || empty($scope)) {
  65. continue;
  66. }
  67. $publish = true;
  68. switch ($property->getName()) {
  69. case IAccountManager::PROPERTY_DISPLAYNAME:
  70. $vCard->add(new Text($vCard, 'FN', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  71. $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()), ['X-NC-SCOPE' => $scope]));
  72. break;
  73. case IAccountManager::PROPERTY_AVATAR:
  74. if ($image !== null) {
  75. $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType(), ['X-NC-SCOPE' => $scope]]);
  76. }
  77. break;
  78. case IAccountManager::COLLECTION_EMAIL:
  79. case IAccountManager::PROPERTY_EMAIL:
  80. $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
  81. break;
  82. case IAccountManager::PROPERTY_WEBSITE:
  83. $vCard->add(new Text($vCard, 'URL', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  84. break;
  85. case IAccountManager::PROPERTY_PROFILE_ENABLED:
  86. if ($property->getValue()) {
  87. $vCard->add(
  88. new Text(
  89. $vCard,
  90. 'X-SOCIALPROFILE',
  91. $this->urlGenerator->linkToRouteAbsolute('core.ProfilePage.index', ['targetUserId' => $user->getUID()]),
  92. [
  93. 'TYPE' => 'NEXTCLOUD',
  94. 'X-NC-SCOPE' => IAccountManager::SCOPE_PUBLISHED
  95. ]
  96. )
  97. );
  98. }
  99. break;
  100. case IAccountManager::PROPERTY_PHONE:
  101. $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'VOICE', 'X-NC-SCOPE' => $scope]));
  102. break;
  103. case IAccountManager::PROPERTY_ADDRESS:
  104. // structured prop: https://www.rfc-editor.org/rfc/rfc6350.html#section-6.3.1
  105. // post office box;extended address;street address;locality;region;postal code;country
  106. $vCard->add(
  107. new Text(
  108. $vCard,
  109. 'ADR',
  110. [ '', '', '', $property->getValue(), '', '', '' ],
  111. [
  112. 'TYPE' => 'OTHER',
  113. 'X-NC-SCOPE' => $scope,
  114. ]
  115. )
  116. );
  117. break;
  118. case IAccountManager::PROPERTY_TWITTER:
  119. $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER', 'X-NC-SCOPE' => $scope]));
  120. break;
  121. case IAccountManager::PROPERTY_ORGANISATION:
  122. $vCard->add(new Text($vCard, 'ORG', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  123. break;
  124. case IAccountManager::PROPERTY_ROLE:
  125. $vCard->add(new Text($vCard, 'TITLE', $property->getValue(), ['X-NC-SCOPE' => $scope]));
  126. break;
  127. }
  128. }
  129. // Local properties
  130. $managers = $user->getManagerUids();
  131. // X-MANAGERSNAME only allows a single value, so we take the first manager
  132. if (isset($managers[0])) {
  133. $displayName = $this->userManager->getDisplayName($managers[0]);
  134. // Only set the manager if a user object is found
  135. if ($displayName !== null) {
  136. $vCard->add(new Text($vCard, 'X-MANAGERSNAME', $displayName, [
  137. 'uid' => $managers[0],
  138. 'X-NC-SCOPE' => IAccountManager::SCOPE_LOCAL,
  139. ]));
  140. }
  141. }
  142. if ($publish && !empty($cloudId)) {
  143. $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
  144. $vCard->validate();
  145. return $vCard;
  146. }
  147. return null;
  148. }
  149. public function splitFullName(string $fullName): array {
  150. // Very basic western style parsing. I'm not gonna implement
  151. // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
  152. $elements = explode(' ', $fullName);
  153. $result = ['', '', '', '', ''];
  154. if (count($elements) > 2) {
  155. $result[0] = implode(' ', array_slice($elements, count($elements) - 1));
  156. $result[1] = $elements[0];
  157. $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
  158. } elseif (count($elements) === 2) {
  159. $result[0] = $elements[1];
  160. $result[1] = $elements[0];
  161. } else {
  162. $result[0] = $elements[0];
  163. }
  164. return $result;
  165. }
  166. private function getAvatarImage(IUser $user): ?IImage {
  167. try {
  168. return $user->getAvatarImage(512);
  169. } catch (Exception $ex) {
  170. return null;
  171. }
  172. }
  173. }