Converter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\CardDAV;
  24. use OC\Accounts\AccountManager;
  25. use OCP\IImage;
  26. use OCP\IUser;
  27. use Sabre\VObject\Component\VCard;
  28. use Sabre\VObject\Property\Text;
  29. class Converter {
  30. /** @var AccountManager */
  31. private $accountManager;
  32. /**
  33. * Converter constructor.
  34. *
  35. * @param AccountManager $accountManager
  36. */
  37. public function __construct(AccountManager $accountManager) {
  38. $this->accountManager = $accountManager;
  39. }
  40. /**
  41. * @param IUser $user
  42. * @return VCard|null
  43. */
  44. public function createCardFromUser(IUser $user) {
  45. $userData = $this->accountManager->getUser($user);
  46. $uid = $user->getUID();
  47. $cloudId = $user->getCloudId();
  48. $image = $this->getAvatarImage($user);
  49. $vCard = new VCard();
  50. $vCard->VERSION = '3.0';
  51. $vCard->UID = $uid;
  52. $publish = false;
  53. if ($image !== null && isset($userData[AccountManager::PROPERTY_AVATAR])) {
  54. $userData[AccountManager::PROPERTY_AVATAR]['value'] = true;
  55. }
  56. foreach ($userData as $property => $value) {
  57. $shareWithTrustedServers =
  58. $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY ||
  59. $value['scope'] === AccountManager::VISIBILITY_PUBLIC;
  60. $emptyValue = !isset($value['value']) || $value['value'] === '';
  61. if ($shareWithTrustedServers && !$emptyValue) {
  62. $publish = true;
  63. switch ($property) {
  64. case AccountManager::PROPERTY_DISPLAYNAME:
  65. $vCard->add(new Text($vCard, 'FN', $value['value']));
  66. $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value'])));
  67. break;
  68. case AccountManager::PROPERTY_AVATAR:
  69. if ($image !== null) {
  70. $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
  71. }
  72. break;
  73. case AccountManager::PROPERTY_EMAIL:
  74. $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER']));
  75. break;
  76. case AccountManager::PROPERTY_WEBSITE:
  77. $vCard->add(new Text($vCard, 'URL', $value['value']));
  78. break;
  79. case AccountManager::PROPERTY_PHONE:
  80. $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER']));
  81. break;
  82. case AccountManager::PROPERTY_ADDRESS:
  83. $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER']));
  84. break;
  85. case AccountManager::PROPERTY_TWITTER:
  86. $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER']));
  87. break;
  88. }
  89. }
  90. }
  91. if ($publish && !empty($cloudId)) {
  92. $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
  93. $vCard->validate();
  94. return $vCard;
  95. }
  96. return null;
  97. }
  98. /**
  99. * @param string $fullName
  100. * @return string[]
  101. */
  102. public function splitFullName($fullName) {
  103. // Very basic western style parsing. I'm not gonna implement
  104. // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
  105. $elements = explode(' ', $fullName);
  106. $result = ['', '', '', '', ''];
  107. if (count($elements) > 2) {
  108. $result[0] = implode(' ', array_slice($elements, count($elements)-1));
  109. $result[1] = $elements[0];
  110. $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2));
  111. } elseif (count($elements) === 2) {
  112. $result[0] = $elements[1];
  113. $result[1] = $elements[0];
  114. } else {
  115. $result[0] = $elements[0];
  116. }
  117. return $result;
  118. }
  119. /**
  120. * @param IUser $user
  121. * @return null|IImage
  122. */
  123. private function getAvatarImage(IUser $user) {
  124. try {
  125. return $user->getAvatarImage(-1);
  126. } catch (\Exception $ex) {
  127. return null;
  128. }
  129. }
  130. }