diff --git a/apps/dav/command/syncsystemaddressbook.php b/apps/dav/command/syncsystemaddressbook.php index 01f4d08940..3139813fc4 100644 --- a/apps/dav/command/syncsystemaddressbook.php +++ b/apps/dav/command/syncsystemaddressbook.php @@ -70,20 +70,25 @@ class SyncSystemAddressBook extends Command { $name = $user->getBackendClassName(); $userId = $user->getUID(); $displayName = $user->getDisplayName(); + //$emailAddress = $user->getEMailAddress(); + $image = $user->getAvatarImage(-1); + $cardId = "$name:$userId.vcf"; $card = $this->backend->getCard($systemAddressBook['id'], $cardId); if ($card === false) { $vCard = new VCard(); - $vCard->add(new Text($vCard, 'UID', $user->getUID())); + $vCard->add(new Text($vCard, 'UID', $userId)); $vCard->add(new Text($vCard, 'FN', $displayName)); -// $vCard->add(new Text($vCard, 'EMAIL', $user->getEMailAddress())); - //$vCard->add(new Binary($vCard, 'PHOTO', $user->getAvatar())); +// $vCard->add(new Text($vCard, 'EMAIL', $emailAddress)); + if ($image) { + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); + } $vCard->validate(); $this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize()); } else { $updated = false; $vCard = Reader::read($card['carddata']); - if($vCard->FN !== $user->getDisplayName()) { + if($vCard->FN !== $displayName) { $vCard->FN = new Text($vCard, 'FN', $displayName); $updated = true; } diff --git a/lib/private/avatar.php b/lib/private/avatar.php index 872da35f94..37a813f3ff 100644 --- a/lib/private/avatar.php +++ b/lib/private/avatar.php @@ -79,7 +79,9 @@ class Avatar implements \OCP\IAvatar { /** @var File $node */ $node = $this->folder->get('avatar.' . $ext); $avatar->loadFromData($node->getContent()); - $avatar->resize($size); + if ($size > 0) { + $avatar->resize($size); + } $this->folder->newFile('avatar.' . $size . '.' . $ext)->putContent($avatar->data()); } return $avatar; diff --git a/lib/private/user/user.php b/lib/private/user/user.php index 2740b25d5d..023fc5f5a3 100644 --- a/lib/private/user/user.php +++ b/lib/private/user/user.php @@ -30,6 +30,8 @@ namespace OC\User; use OC\Hooks\Emitter; +use OCP\IAvatarManager; +use OCP\IImage; use OCP\IUser; use OCP\IConfig; @@ -74,17 +76,21 @@ class User implements IUser { */ private $config; + /** @var IAvatarManager */ + private $avatarManager; + /** * @param string $uid * @param \OC_User_Interface $backend * @param \OC\Hooks\Emitter $emitter * @param \OCP\IConfig $config */ - public function __construct($uid, $backend, $emitter = null, IConfig $config = null) { + public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $avatarManager = null) { $this->uid = $uid; $this->backend = $backend; $this->emitter = $emitter; $this->config = $config; + $this->avatarManager = $avatarManager; if ($this->config) { $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true'); $this->enabled = ($enabled === 'true'); @@ -93,6 +99,9 @@ class User implements IUser { $this->enabled = true; $this->lastLogin = \OC::$server->getConfig()->getUserValue($uid, 'login', 'lastLogin', 0); } + if (is_null($this->avatarManager)) { + $this->avatarManager = \OC::$server->getAvatarManager(); + } } /** @@ -316,4 +325,21 @@ class User implements IUser { public function getEMailAddress() { return $this->config->getUserValue($this->uid, 'settings', 'email'); } + + /** + * get the avatar image if it exists + * + * @param int $size + * @return IImage|null + * @since 9.0.0 + */ + public function getAvatarImage($size) { + $avatar = $this->avatarManager->getAvatar($this->uid); + $image = $avatar->get(-1); + if ($image) { + return $image; + } + + return null; + } } diff --git a/lib/public/iuser.php b/lib/public/iuser.php index 1e52cd5903..f6caef2ba1 100644 --- a/lib/public/iuser.php +++ b/lib/public/iuser.php @@ -152,4 +152,13 @@ interface IUser { * @since 9.0.0 */ public function getEMailAddress(); + + /** + * get the avatar image if it exists + * + * @param int $size + * @return IImage|null + * @since 9.0.0 + */ + public function getAvatarImage($size); } diff --git a/tests/lib/contacts/localadressbook.php b/tests/lib/contacts/localadressbook.php index 6bfcf3d890..5416832453 100644 --- a/tests/lib/contacts/localadressbook.php +++ b/tests/lib/contacts/localadressbook.php @@ -98,6 +98,8 @@ class SimpleUserForTesting implements IUser { } public function getEMailAddress() { - // TODO: Implement getEMailAddress() method. + } + + public function getAvatarImage($size) { } }