|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Thomas Müller <[email protected]> |
|
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
|
|
|
|
|
23
|
|
|
namespace OCA\DAV\CardDAV; |
|
24
|
|
|
|
|
25
|
|
|
use OC\Accounts\AccountManager; |
|
26
|
|
|
use OCP\IImage; |
|
27
|
|
|
use OCP\IUser; |
|
28
|
|
|
use Sabre\VObject\Component\VCard; |
|
29
|
|
|
use Sabre\VObject\Property\Text; |
|
30
|
|
|
|
|
31
|
|
|
class Converter { |
|
32
|
|
|
|
|
33
|
|
|
/** @var AccountManager */ |
|
34
|
|
|
private $accountManager; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Converter constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param AccountManager $accountManager |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(AccountManager $accountManager) { |
|
42
|
|
|
$this->accountManager = $accountManager; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param IUser $user |
|
47
|
|
|
* @return VCard|null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function createCardFromUser(IUser $user) { |
|
50
|
|
|
|
|
51
|
|
|
$userData = $this->accountManager->getUser($user); |
|
52
|
|
|
|
|
53
|
|
|
$uid = $user->getUID(); |
|
54
|
|
|
$cloudId = $user->getCloudId(); |
|
55
|
|
|
$image = $this->getAvatarImage($user); |
|
56
|
|
|
|
|
57
|
|
|
$vCard = new VCard(); |
|
58
|
|
|
$vCard->add(new Text($vCard, 'UID', $uid)); |
|
59
|
|
|
|
|
60
|
|
|
$publish = false; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($userData as $property => $value) { |
|
63
|
|
|
if ($value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
64
|
|
|
$value['scope'] === AccountManager::VISIBILITY_PUBLIC |
|
65
|
|
|
) { |
|
66
|
|
|
$publish = true; |
|
67
|
|
|
switch ($property) { |
|
68
|
|
|
case AccountManager::PROPERTY_DISPLAYNAME: |
|
69
|
|
|
$vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
70
|
|
|
$vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
71
|
|
|
break; |
|
72
|
|
|
case AccountManager::PROPERTY_AVATAR: |
|
73
|
|
|
if ($image !== null) { |
|
74
|
|
|
$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
75
|
|
|
} |
|
76
|
|
|
break; |
|
77
|
|
View Code Duplication |
case AccountManager::PROPERTY_EMAIL: |
|
|
|
|
|
|
78
|
|
|
$vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
79
|
|
|
break; |
|
80
|
|
|
case AccountManager::PROPERTY_WEBSITE: |
|
81
|
|
|
$vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
82
|
|
|
break; |
|
83
|
|
View Code Duplication |
case AccountManager::PROPERTY_PHONE: |
|
|
|
|
|
|
84
|
|
|
$vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
85
|
|
|
break; |
|
86
|
|
View Code Duplication |
case AccountManager::PROPERTY_ADDRESS: |
|
|
|
|
|
|
87
|
|
|
$vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
88
|
|
|
break; |
|
89
|
|
|
case AccountManager::PROPERTY_TWITTER: |
|
90
|
|
|
$vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($publish && !empty($cloudId)) { |
|
97
|
|
|
$vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
98
|
|
|
$vCard->validate(); |
|
99
|
|
|
return $vCard; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $fullName |
|
107
|
|
|
* @return string[] |
|
108
|
|
|
*/ |
|
109
|
|
|
public function splitFullName($fullName) { |
|
110
|
|
|
// Very basic western style parsing. I'm not gonna implement |
|
111
|
|
|
// https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
112
|
|
|
|
|
113
|
|
|
$elements = explode(' ', $fullName); |
|
114
|
|
|
$result = ['', '', '', '', '']; |
|
115
|
|
|
if (count($elements) > 2) { |
|
116
|
|
|
$result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
117
|
|
|
$result[1] = $elements[0]; |
|
118
|
|
|
$result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
119
|
|
|
} elseif (count($elements) === 2) { |
|
120
|
|
|
$result[0] = $elements[1]; |
|
121
|
|
|
$result[1] = $elements[0]; |
|
122
|
|
|
} else { |
|
123
|
|
|
$result[0] = $elements[0]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $result; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param IUser $user |
|
131
|
|
|
* @return null|IImage |
|
132
|
|
|
*/ |
|
133
|
|
|
private function getAvatarImage(IUser $user) { |
|
134
|
|
|
try { |
|
135
|
|
|
$image = $user->getAvatarImage(-1); |
|
136
|
|
|
return $image; |
|
137
|
|
|
} catch (\Exception $ex) { |
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.