Converter.php 3.9 KB

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