Converter.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 OCP\IImage;
  24. use OCP\IUser;
  25. use Sabre\VObject\Component\VCard;
  26. use Sabre\VObject\Property\Text;
  27. class Converter {
  28. /**
  29. * @param IUser $user
  30. * @return VCard
  31. */
  32. public function createCardFromUser(IUser $user) {
  33. $uid = $user->getUID();
  34. $displayName = $user->getDisplayName();
  35. $displayName = empty($displayName ) ? $uid : $displayName;
  36. $emailAddress = $user->getEMailAddress();
  37. $cloudId = $user->getCloudId();
  38. $image = $this->getAvatarImage($user);
  39. $vCard = new VCard();
  40. $vCard->add(new Text($vCard, 'UID', $uid));
  41. if (!empty($displayName)) {
  42. $vCard->add(new Text($vCard, 'FN', $displayName));
  43. $vCard->add(new Text($vCard, 'N', $this->splitFullName($displayName)));
  44. }
  45. if (!empty($emailAddress)) {
  46. $vCard->add(new Text($vCard, 'EMAIL', $emailAddress, ['TYPE' => 'OTHER']));
  47. }
  48. if (!empty($cloudId)) {
  49. $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
  50. }
  51. if ($image) {
  52. $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
  53. }
  54. $vCard->validate();
  55. return $vCard;
  56. }
  57. /**
  58. * @param VCard $vCard
  59. * @param IUser $user
  60. * @return bool
  61. */
  62. public function updateCard(VCard $vCard, IUser $user) {
  63. $uid = $user->getUID();
  64. $displayName = $user->getDisplayName();
  65. $displayName = empty($displayName ) ? $uid : $displayName;
  66. $emailAddress = $user->getEMailAddress();
  67. $cloudId = $user->getCloudId();
  68. $image = $this->getAvatarImage($user);
  69. $updated = false;
  70. if($this->propertyNeedsUpdate($vCard, 'FN', $displayName)) {
  71. $vCard->FN = new Text($vCard, 'FN', $displayName);
  72. unset($vCard->N);
  73. $vCard->add(new Text($vCard, 'N', $this->splitFullName($displayName)));
  74. $updated = true;
  75. }
  76. if($this->propertyNeedsUpdate($vCard, 'EMAIL', $emailAddress)) {
  77. $vCard->EMAIL = new Text($vCard, 'EMAIL', $emailAddress);
  78. $updated = true;
  79. }
  80. if($this->propertyNeedsUpdate($vCard, 'CLOUD', $cloudId)) {
  81. $vCard->CLOUD = new Text($vCard, 'CLOUD', $cloudId);
  82. $updated = true;
  83. }
  84. if($this->propertyNeedsUpdate($vCard, 'PHOTO', $image)) {
  85. $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
  86. $updated = true;
  87. }
  88. if (empty($emailAddress) && !is_null($vCard->EMAIL)) {
  89. unset($vCard->EMAIL);
  90. $updated = true;
  91. }
  92. if (empty($cloudId) && !is_null($vCard->CLOUD)) {
  93. unset($vCard->CLOUD);
  94. $updated = true;
  95. }
  96. if (empty($image) && !is_null($vCard->PHOTO)) {
  97. unset($vCard->PHOTO);
  98. $updated = true;
  99. }
  100. return $updated;
  101. }
  102. /**
  103. * @param VCard $vCard
  104. * @param string $name
  105. * @param string|IImage $newValue
  106. * @return bool
  107. */
  108. private function propertyNeedsUpdate(VCard $vCard, $name, $newValue) {
  109. if (is_null($newValue)) {
  110. return false;
  111. }
  112. $value = $vCard->__get($name);
  113. if (!is_null($value)) {
  114. $value = $value->getValue();
  115. $newValue = $newValue instanceof IImage ? $newValue->data() : $newValue;
  116. return $value !== $newValue;
  117. }
  118. return true;
  119. }
  120. /**
  121. * @param string $fullName
  122. * @return string[]
  123. */
  124. public function splitFullName($fullName) {
  125. // Very basic western style parsing. I'm not gonna implement
  126. // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
  127. $elements = explode(' ', $fullName);
  128. $result = ['', '', '', '', ''];
  129. if (count($elements) > 2) {
  130. $result[0] = implode(' ', array_slice($elements, count($elements)-1));
  131. $result[1] = $elements[0];
  132. $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2));
  133. } elseif (count($elements) === 2) {
  134. $result[0] = $elements[1];
  135. $result[1] = $elements[0];
  136. } else {
  137. $result[0] = $elements[0];
  138. }
  139. return $result;
  140. }
  141. /**
  142. * @param IUser $user
  143. * @return null|IImage
  144. */
  145. private function getAvatarImage(IUser $user) {
  146. try {
  147. $image = $user->getAvatarImage(-1);
  148. return $image;
  149. } catch (\Exception $ex) {
  150. return null;
  151. }
  152. }
  153. }