2015-11-30 18:59:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-11-30 18:59:41 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CardDAV;
|
|
|
|
|
2015-12-02 13:09:15 +03:00
|
|
|
use OCP\IImage;
|
2015-11-30 18:59:41 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use Sabre\VObject\Component\VCard;
|
|
|
|
use Sabre\VObject\Property\Text;
|
|
|
|
|
|
|
|
class Converter {
|
|
|
|
|
2015-12-02 16:25:07 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return VCard
|
|
|
|
*/
|
2015-11-30 18:59:41 +03:00
|
|
|
public function createCardFromUser(IUser $user) {
|
|
|
|
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$displayName = $user->getDisplayName();
|
|
|
|
$displayName = empty($displayName ) ? $uid : $displayName;
|
|
|
|
$emailAddress = $user->getEMailAddress();
|
|
|
|
$cloudId = $user->getCloudId();
|
2016-01-29 17:39:39 +03:00
|
|
|
$image = $this->getAvatarImage($user);
|
2015-11-30 18:59:41 +03:00
|
|
|
|
|
|
|
$vCard = new VCard();
|
2016-10-13 13:15:10 +03:00
|
|
|
$vCard->VERSION = '3.0';
|
|
|
|
$vCard->UID = $uid;
|
2015-11-30 18:59:41 +03:00
|
|
|
if (!empty($displayName)) {
|
2016-10-13 13:15:10 +03:00
|
|
|
$vCard->FN = $displayName;
|
|
|
|
$vCard->N = $this->splitFullName($displayName);
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|
|
|
|
if (!empty($emailAddress)) {
|
2015-12-01 00:28:36 +03:00
|
|
|
$vCard->add(new Text($vCard, 'EMAIL', $emailAddress, ['TYPE' => 'OTHER']));
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|
|
|
|
if (!empty($cloudId)) {
|
2016-10-13 13:15:10 +03:00
|
|
|
$vCard->CLOUD = $cloudId;
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|
|
|
|
if ($image) {
|
|
|
|
$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
|
|
|
|
}
|
|
|
|
$vCard->validate();
|
|
|
|
|
|
|
|
return $vCard;
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:25:07 +03:00
|
|
|
/**
|
|
|
|
* @param VCard $vCard
|
|
|
|
* @param IUser $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-11-30 18:59:41 +03:00
|
|
|
public function updateCard(VCard $vCard, IUser $user) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$displayName = $user->getDisplayName();
|
|
|
|
$displayName = empty($displayName ) ? $uid : $displayName;
|
|
|
|
$emailAddress = $user->getEMailAddress();
|
|
|
|
$cloudId = $user->getCloudId();
|
2016-01-29 17:39:39 +03:00
|
|
|
$image = $this->getAvatarImage($user);
|
2015-11-30 18:59:41 +03:00
|
|
|
|
|
|
|
$updated = false;
|
2015-12-02 13:09:15 +03:00
|
|
|
if($this->propertyNeedsUpdate($vCard, 'FN', $displayName)) {
|
2015-11-30 18:59:41 +03:00
|
|
|
$vCard->FN = new Text($vCard, 'FN', $displayName);
|
2015-12-01 00:28:36 +03:00
|
|
|
unset($vCard->N);
|
|
|
|
$vCard->add(new Text($vCard, 'N', $this->splitFullName($displayName)));
|
2015-11-30 18:59:41 +03:00
|
|
|
$updated = true;
|
|
|
|
}
|
2015-12-02 13:09:15 +03:00
|
|
|
if($this->propertyNeedsUpdate($vCard, 'EMAIL', $emailAddress)) {
|
2015-11-30 18:59:41 +03:00
|
|
|
$vCard->EMAIL = new Text($vCard, 'EMAIL', $emailAddress);
|
|
|
|
$updated = true;
|
|
|
|
}
|
2015-12-02 13:09:15 +03:00
|
|
|
if($this->propertyNeedsUpdate($vCard, 'CLOUD', $cloudId)) {
|
2015-11-30 18:59:41 +03:00
|
|
|
$vCard->CLOUD = new Text($vCard, 'CLOUD', $cloudId);
|
|
|
|
$updated = true;
|
|
|
|
}
|
2015-12-02 13:09:15 +03:00
|
|
|
|
|
|
|
if($this->propertyNeedsUpdate($vCard, 'PHOTO', $image)) {
|
2016-09-29 13:39:36 +03:00
|
|
|
unset($vCard->PHOTO);
|
2015-12-01 00:28:36 +03:00
|
|
|
$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
|
|
|
|
$updated = true;
|
|
|
|
}
|
2015-11-30 18:59:41 +03:00
|
|
|
|
|
|
|
if (empty($emailAddress) && !is_null($vCard->EMAIL)) {
|
|
|
|
unset($vCard->EMAIL);
|
|
|
|
$updated = true;
|
|
|
|
}
|
|
|
|
if (empty($cloudId) && !is_null($vCard->CLOUD)) {
|
|
|
|
unset($vCard->CLOUD);
|
|
|
|
$updated = true;
|
|
|
|
}
|
|
|
|
if (empty($image) && !is_null($vCard->PHOTO)) {
|
|
|
|
unset($vCard->PHOTO);
|
|
|
|
$updated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $updated;
|
|
|
|
}
|
2015-12-01 00:28:36 +03:00
|
|
|
|
2015-12-02 16:25:07 +03:00
|
|
|
/**
|
|
|
|
* @param VCard $vCard
|
|
|
|
* @param string $name
|
|
|
|
* @param string|IImage $newValue
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-12-02 13:09:15 +03:00
|
|
|
private function propertyNeedsUpdate(VCard $vCard, $name, $newValue) {
|
|
|
|
if (is_null($newValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$value = $vCard->__get($name);
|
|
|
|
if (!is_null($value)) {
|
|
|
|
$value = $value->getValue();
|
|
|
|
$newValue = $newValue instanceof IImage ? $newValue->data() : $newValue;
|
|
|
|
|
|
|
|
return $value !== $newValue;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:28:36 +03:00
|
|
|
/**
|
|
|
|
* @param string $fullName
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function splitFullName($fullName) {
|
|
|
|
// Very basic western style parsing. I'm not gonna implement
|
|
|
|
// https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
|
|
|
|
|
|
|
|
$elements = explode(' ', $fullName);
|
|
|
|
$result = ['', '', '', '', ''];
|
|
|
|
if (count($elements) > 2) {
|
|
|
|
$result[0] = implode(' ', array_slice($elements, count($elements)-1));
|
|
|
|
$result[1] = $elements[0];
|
|
|
|
$result[2] = implode(' ', array_slice($elements, 1, count($elements)-2));
|
|
|
|
} elseif (count($elements) === 2) {
|
|
|
|
$result[0] = $elements[1];
|
|
|
|
$result[1] = $elements[0];
|
|
|
|
} else {
|
|
|
|
$result[0] = $elements[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2015-12-02 13:09:15 +03:00
|
|
|
|
2016-01-29 17:39:39 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return null|IImage
|
|
|
|
*/
|
|
|
|
private function getAvatarImage(IUser $user) {
|
|
|
|
try {
|
|
|
|
$image = $user->getAvatarImage(-1);
|
|
|
|
return $image;
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|