@@ -38,111 +38,111 @@ |
||
38 | 38 | |
39 | 39 | class Converter { |
40 | 40 | |
41 | - /** @var IAccountManager */ |
|
42 | - private $accountManager; |
|
43 | - |
|
44 | - public function __construct(IAccountManager $accountManager) { |
|
45 | - $this->accountManager = $accountManager; |
|
46 | - } |
|
47 | - |
|
48 | - public function createCardFromUser(IUser $user): ?VCard { |
|
49 | - $account = $this->accountManager->getAccount($user); |
|
50 | - $userProperties = $account->getProperties(); |
|
51 | - try { |
|
52 | - $additionalEmailsCollection = $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL); |
|
53 | - $userProperties = array_merge($userProperties, $additionalEmailsCollection->getProperties()); |
|
54 | - } catch (PropertyDoesNotExistException $e) { |
|
55 | - } |
|
56 | - |
|
57 | - $uid = $user->getUID(); |
|
58 | - $cloudId = $user->getCloudId(); |
|
59 | - $image = $this->getAvatarImage($user); |
|
60 | - |
|
61 | - $vCard = new VCard(); |
|
62 | - $vCard->VERSION = '3.0'; |
|
63 | - $vCard->UID = $uid; |
|
64 | - |
|
65 | - $publish = false; |
|
66 | - |
|
67 | - foreach ($userProperties as $property) { |
|
68 | - $shareWithTrustedServers = |
|
69 | - $property->getScope() === IAccountManager::SCOPE_FEDERATED || |
|
70 | - $property->getScope() === IAccountManager::SCOPE_PUBLISHED; |
|
71 | - |
|
72 | - $emptyValue = $property->getValue() === ''; |
|
73 | - |
|
74 | - if ($shareWithTrustedServers && !$emptyValue) { |
|
75 | - $publish = true; |
|
76 | - switch ($property->getName()) { |
|
77 | - case IAccountManager::PROPERTY_DISPLAYNAME: |
|
78 | - $vCard->add(new Text($vCard, 'FN', $property->getValue())); |
|
79 | - $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()))); |
|
80 | - break; |
|
81 | - case IAccountManager::PROPERTY_AVATAR: |
|
82 | - if ($image !== null) { |
|
83 | - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
84 | - } |
|
85 | - break; |
|
86 | - case IAccountManager::COLLECTION_EMAIL: |
|
87 | - case IAccountManager::PROPERTY_EMAIL: |
|
88 | - $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
89 | - break; |
|
90 | - case IAccountManager::PROPERTY_WEBSITE: |
|
91 | - $vCard->add(new Text($vCard, 'URL', $property->getValue())); |
|
92 | - break; |
|
93 | - case IAccountManager::PROPERTY_PHONE: |
|
94 | - $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
95 | - break; |
|
96 | - case IAccountManager::PROPERTY_ADDRESS: |
|
97 | - $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
98 | - break; |
|
99 | - case IAccountManager::PROPERTY_TWITTER: |
|
100 | - $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER'])); |
|
101 | - break; |
|
102 | - case IAccountManager::PROPERTY_ORGANISATION: |
|
103 | - $vCard->add(new Text($vCard, 'ORG', $property->getValue())); |
|
104 | - break; |
|
105 | - case IAccountManager::PROPERTY_ROLE: |
|
106 | - $vCard->add(new Text($vCard, 'TITLE', $property->getValue())); |
|
107 | - break; |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - if ($publish && !empty($cloudId)) { |
|
113 | - $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
114 | - $vCard->validate(); |
|
115 | - return $vCard; |
|
116 | - } |
|
117 | - |
|
118 | - return null; |
|
119 | - } |
|
120 | - |
|
121 | - public function splitFullName(string $fullName): array { |
|
122 | - // Very basic western style parsing. I'm not gonna implement |
|
123 | - // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
124 | - |
|
125 | - $elements = explode(' ', $fullName); |
|
126 | - $result = ['', '', '', '', '']; |
|
127 | - if (count($elements) > 2) { |
|
128 | - $result[0] = implode(' ', array_slice($elements, count($elements) - 1)); |
|
129 | - $result[1] = $elements[0]; |
|
130 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2)); |
|
131 | - } elseif (count($elements) === 2) { |
|
132 | - $result[0] = $elements[1]; |
|
133 | - $result[1] = $elements[0]; |
|
134 | - } else { |
|
135 | - $result[0] = $elements[0]; |
|
136 | - } |
|
137 | - |
|
138 | - return $result; |
|
139 | - } |
|
140 | - |
|
141 | - private function getAvatarImage(IUser $user): ?IImage { |
|
142 | - try { |
|
143 | - return $user->getAvatarImage(-1); |
|
144 | - } catch (Exception $ex) { |
|
145 | - return null; |
|
146 | - } |
|
147 | - } |
|
41 | + /** @var IAccountManager */ |
|
42 | + private $accountManager; |
|
43 | + |
|
44 | + public function __construct(IAccountManager $accountManager) { |
|
45 | + $this->accountManager = $accountManager; |
|
46 | + } |
|
47 | + |
|
48 | + public function createCardFromUser(IUser $user): ?VCard { |
|
49 | + $account = $this->accountManager->getAccount($user); |
|
50 | + $userProperties = $account->getProperties(); |
|
51 | + try { |
|
52 | + $additionalEmailsCollection = $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL); |
|
53 | + $userProperties = array_merge($userProperties, $additionalEmailsCollection->getProperties()); |
|
54 | + } catch (PropertyDoesNotExistException $e) { |
|
55 | + } |
|
56 | + |
|
57 | + $uid = $user->getUID(); |
|
58 | + $cloudId = $user->getCloudId(); |
|
59 | + $image = $this->getAvatarImage($user); |
|
60 | + |
|
61 | + $vCard = new VCard(); |
|
62 | + $vCard->VERSION = '3.0'; |
|
63 | + $vCard->UID = $uid; |
|
64 | + |
|
65 | + $publish = false; |
|
66 | + |
|
67 | + foreach ($userProperties as $property) { |
|
68 | + $shareWithTrustedServers = |
|
69 | + $property->getScope() === IAccountManager::SCOPE_FEDERATED || |
|
70 | + $property->getScope() === IAccountManager::SCOPE_PUBLISHED; |
|
71 | + |
|
72 | + $emptyValue = $property->getValue() === ''; |
|
73 | + |
|
74 | + if ($shareWithTrustedServers && !$emptyValue) { |
|
75 | + $publish = true; |
|
76 | + switch ($property->getName()) { |
|
77 | + case IAccountManager::PROPERTY_DISPLAYNAME: |
|
78 | + $vCard->add(new Text($vCard, 'FN', $property->getValue())); |
|
79 | + $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()))); |
|
80 | + break; |
|
81 | + case IAccountManager::PROPERTY_AVATAR: |
|
82 | + if ($image !== null) { |
|
83 | + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
84 | + } |
|
85 | + break; |
|
86 | + case IAccountManager::COLLECTION_EMAIL: |
|
87 | + case IAccountManager::PROPERTY_EMAIL: |
|
88 | + $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
89 | + break; |
|
90 | + case IAccountManager::PROPERTY_WEBSITE: |
|
91 | + $vCard->add(new Text($vCard, 'URL', $property->getValue())); |
|
92 | + break; |
|
93 | + case IAccountManager::PROPERTY_PHONE: |
|
94 | + $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
95 | + break; |
|
96 | + case IAccountManager::PROPERTY_ADDRESS: |
|
97 | + $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER'])); |
|
98 | + break; |
|
99 | + case IAccountManager::PROPERTY_TWITTER: |
|
100 | + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER'])); |
|
101 | + break; |
|
102 | + case IAccountManager::PROPERTY_ORGANISATION: |
|
103 | + $vCard->add(new Text($vCard, 'ORG', $property->getValue())); |
|
104 | + break; |
|
105 | + case IAccountManager::PROPERTY_ROLE: |
|
106 | + $vCard->add(new Text($vCard, 'TITLE', $property->getValue())); |
|
107 | + break; |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + if ($publish && !empty($cloudId)) { |
|
113 | + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
114 | + $vCard->validate(); |
|
115 | + return $vCard; |
|
116 | + } |
|
117 | + |
|
118 | + return null; |
|
119 | + } |
|
120 | + |
|
121 | + public function splitFullName(string $fullName): array { |
|
122 | + // Very basic western style parsing. I'm not gonna implement |
|
123 | + // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
124 | + |
|
125 | + $elements = explode(' ', $fullName); |
|
126 | + $result = ['', '', '', '', '']; |
|
127 | + if (count($elements) > 2) { |
|
128 | + $result[0] = implode(' ', array_slice($elements, count($elements) - 1)); |
|
129 | + $result[1] = $elements[0]; |
|
130 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2)); |
|
131 | + } elseif (count($elements) === 2) { |
|
132 | + $result[0] = $elements[1]; |
|
133 | + $result[1] = $elements[0]; |
|
134 | + } else { |
|
135 | + $result[0] = $elements[0]; |
|
136 | + } |
|
137 | + |
|
138 | + return $result; |
|
139 | + } |
|
140 | + |
|
141 | + private function getAvatarImage(IUser $user): ?IImage { |
|
142 | + try { |
|
143 | + return $user->getAvatarImage(-1); |
|
144 | + } catch (Exception $ex) { |
|
145 | + return null; |
|
146 | + } |
|
147 | + } |
|
148 | 148 | } |