2015-11-30 18:59:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-11-30 18:59:41 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CardDAV;
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
use OC\Accounts\AccountManager;
|
2020-12-01 16:33:22 +03:00
|
|
|
use OCP\Accounts\IAccountManager;
|
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 {
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
/** @var AccountManager */
|
|
|
|
private $accountManager;
|
|
|
|
|
2015-12-02 16:25:07 +03:00
|
|
|
/**
|
2016-11-11 16:36:17 +03:00
|
|
|
* Converter constructor.
|
|
|
|
*
|
|
|
|
* @param AccountManager $accountManager
|
2015-12-02 16:25:07 +03:00
|
|
|
*/
|
2016-11-11 16:36:17 +03:00
|
|
|
public function __construct(AccountManager $accountManager) {
|
|
|
|
$this->accountManager = $accountManager;
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|
|
|
|
|
2015-12-02 16:25:07 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
2016-11-11 16:36:17 +03:00
|
|
|
* @return VCard|null
|
2015-12-02 16:25:07 +03:00
|
|
|
*/
|
2016-11-11 16:36:17 +03:00
|
|
|
public function createCardFromUser(IUser $user) {
|
|
|
|
$userData = $this->accountManager->getUser($user);
|
|
|
|
|
2015-11-30 18:59:41 +03:00
|
|
|
$uid = $user->getUID();
|
|
|
|
$cloudId = $user->getCloudId();
|
2016-01-29 17:39:39 +03:00
|
|
|
$image = $this->getAvatarImage($user);
|
2015-11-30 18:59:41 +03:00
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard = new VCard();
|
2017-03-13 16:47:08 +03:00
|
|
|
$vCard->VERSION = '3.0';
|
|
|
|
$vCard->UID = $uid;
|
2016-11-11 16:36:17 +03:00
|
|
|
|
|
|
|
$publish = false;
|
|
|
|
|
2020-12-01 16:33:22 +03:00
|
|
|
if ($image !== null && isset($userData[IAccountManager::PROPERTY_AVATAR])) {
|
|
|
|
$userData[IAccountManager::PROPERTY_AVATAR]['value'] = true;
|
2017-11-29 17:24:08 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
foreach ($userData as $property => $value) {
|
2017-03-13 16:58:37 +03:00
|
|
|
$shareWithTrustedServers =
|
2021-03-23 16:47:10 +03:00
|
|
|
$value['scope'] === AccountManager::SCOPE_FEDERATED ||
|
|
|
|
$value['scope'] === AccountManager::SCOPE_PUBLISHED;
|
2017-03-13 16:58:37 +03:00
|
|
|
|
|
|
|
$emptyValue = !isset($value['value']) || $value['value'] === '';
|
|
|
|
|
2017-11-29 17:24:08 +03:00
|
|
|
if ($shareWithTrustedServers && !$emptyValue) {
|
2016-11-11 16:36:17 +03:00
|
|
|
$publish = true;
|
|
|
|
switch ($property) {
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_DISPLAYNAME:
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard->add(new Text($vCard, 'FN', $value['value']));
|
|
|
|
$vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value'])));
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_AVATAR:
|
2016-11-11 16:36:17 +03:00
|
|
|
if ($image !== null) {
|
2017-03-13 18:54:27 +03:00
|
|
|
$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
|
2016-11-11 16:36:17 +03:00
|
|
|
}
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_EMAIL:
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER']));
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_WEBSITE:
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard->add(new Text($vCard, 'URL', $value['value']));
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_PHONE:
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER']));
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_ADDRESS:
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER']));
|
|
|
|
break;
|
2020-12-01 16:33:22 +03:00
|
|
|
case IAccountManager::PROPERTY_TWITTER:
|
2016-11-11 16:36:17 +03:00
|
|
|
$vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER']));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
if ($publish && !empty($cloudId)) {
|
|
|
|
$vCard->add(new Text($vCard, 'CLOUD', $cloudId));
|
|
|
|
$vCard->validate();
|
|
|
|
return $vCard;
|
2015-12-02 13:09:15 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
return null;
|
2015-12-02 13:09:15 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-05 16:12:57 +03:00
|
|
|
$result[0] = implode(' ', array_slice($elements, count($elements) - 1));
|
2015-12-01 00:28:36 +03:00
|
|
|
$result[1] = $elements[0];
|
2020-10-05 16:12:57 +03:00
|
|
|
$result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
|
2015-12-01 00:28:36 +03:00
|
|
|
} 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 {
|
2018-01-26 02:02:03 +03:00
|
|
|
return $user->getAvatarImage(-1);
|
2016-01-29 17:39:39 +03:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-11-30 18:59:41 +03:00
|
|
|
}
|