You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Converter.php 4.3KB

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