Converter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Sabre\VObject\Component\VCard;
  34. use Sabre\VObject\Property\Text;
  35. class Converter {
  36. /** @var IAccountManager */
  37. private $accountManager;
  38. public function __construct(IAccountManager $accountManager) {
  39. $this->accountManager = $accountManager;
  40. }
  41. public function createCardFromUser(IUser $user): ?VCard {
  42. $userProperties = $this->accountManager->getAccount($user)->getProperties();
  43. $uid = $user->getUID();
  44. $cloudId = $user->getCloudId();
  45. $image = $this->getAvatarImage($user);
  46. $vCard = new VCard();
  47. $vCard->VERSION = '3.0';
  48. $vCard->UID = $uid;
  49. $publish = false;
  50. foreach ($userProperties as $property) {
  51. $shareWithTrustedServers =
  52. $property->getScope() === IAccountManager::SCOPE_FEDERATED ||
  53. $property->getScope() === IAccountManager::SCOPE_PUBLISHED;
  54. $emptyValue = $property->getValue() === '';
  55. if ($shareWithTrustedServers && !$emptyValue) {
  56. $publish = true;
  57. switch ($property->getName()) {
  58. case IAccountManager::PROPERTY_DISPLAYNAME:
  59. $vCard->add(new Text($vCard, 'FN', $property->getValue()));
  60. $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue())));
  61. break;
  62. case IAccountManager::PROPERTY_AVATAR:
  63. if ($image !== null) {
  64. $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
  65. }
  66. break;
  67. case IAccountManager::PROPERTY_EMAIL:
  68. $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER']));
  69. break;
  70. case IAccountManager::PROPERTY_WEBSITE:
  71. $vCard->add(new Text($vCard, 'URL', $property->getValue()));
  72. break;
  73. case IAccountManager::PROPERTY_PHONE:
  74. $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER']));
  75. break;
  76. case IAccountManager::PROPERTY_ADDRESS:
  77. $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER']));
  78. break;
  79. case IAccountManager::PROPERTY_TWITTER:
  80. $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER']));
  81. break;
  82. }
  83. }
  84. }
  85. if ($publish && !empty($cloudId)) {
  86. $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
  87. $vCard->validate();
  88. return $vCard;
  89. }
  90. return null;
  91. }
  92. public function splitFullName(string $fullName): array {
  93. // Very basic western style parsing. I'm not gonna implement
  94. // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
  95. $elements = explode(' ', $fullName);
  96. $result = ['', '', '', '', ''];
  97. if (count($elements) > 2) {
  98. $result[0] = implode(' ', array_slice($elements, count($elements) - 1));
  99. $result[1] = $elements[0];
  100. $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
  101. } elseif (count($elements) === 2) {
  102. $result[0] = $elements[1];
  103. $result[1] = $elements[0];
  104. } else {
  105. $result[0] = $elements[0];
  106. }
  107. return $result;
  108. }
  109. private function getAvatarImage(IUser $user): ?IImage {
  110. try {
  111. return $user->getAvatarImage(-1);
  112. } catch (Exception $ex) {
  113. return null;
  114. }
  115. }
  116. }