@@ -34,45 +34,45 @@ |
||
34 | 34 | |
35 | 35 | class AddServerMiddleware extends Middleware { |
36 | 36 | |
37 | - /** @var string */ |
|
38 | - protected $appName; |
|
37 | + /** @var string */ |
|
38 | + protected $appName; |
|
39 | 39 | |
40 | - /** @var IL10N */ |
|
41 | - protected $l; |
|
40 | + /** @var IL10N */ |
|
41 | + protected $l; |
|
42 | 42 | |
43 | - /** @var ILogger */ |
|
44 | - protected $logger; |
|
43 | + /** @var ILogger */ |
|
44 | + protected $logger; |
|
45 | 45 | |
46 | - public function __construct($appName, IL10N $l, ILogger $logger) { |
|
47 | - $this->appName = $appName; |
|
48 | - $this->l = $l; |
|
49 | - $this->logger = $logger; |
|
50 | - } |
|
46 | + public function __construct($appName, IL10N $l, ILogger $logger) { |
|
47 | + $this->appName = $appName; |
|
48 | + $this->l = $l; |
|
49 | + $this->logger = $logger; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Log error message and return a response which can be displayed to the user |
|
54 | - * |
|
55 | - * @param \OCP\AppFramework\Controller $controller |
|
56 | - * @param string $methodName |
|
57 | - * @param \Exception $exception |
|
58 | - * @return JSONResponse |
|
59 | - */ |
|
60 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
61 | - if (($controller instanceof SettingsController) === false) { |
|
62 | - throw $exception; |
|
63 | - } |
|
64 | - $this->logger->error($exception->getMessage(), ['app' => $this->appName]); |
|
65 | - if ($exception instanceof HintException) { |
|
66 | - $message = $exception->getHint(); |
|
67 | - } else { |
|
68 | - $message = $exception->getMessage(); |
|
69 | - } |
|
52 | + /** |
|
53 | + * Log error message and return a response which can be displayed to the user |
|
54 | + * |
|
55 | + * @param \OCP\AppFramework\Controller $controller |
|
56 | + * @param string $methodName |
|
57 | + * @param \Exception $exception |
|
58 | + * @return JSONResponse |
|
59 | + */ |
|
60 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
61 | + if (($controller instanceof SettingsController) === false) { |
|
62 | + throw $exception; |
|
63 | + } |
|
64 | + $this->logger->error($exception->getMessage(), ['app' => $this->appName]); |
|
65 | + if ($exception instanceof HintException) { |
|
66 | + $message = $exception->getHint(); |
|
67 | + } else { |
|
68 | + $message = $exception->getMessage(); |
|
69 | + } |
|
70 | 70 | |
71 | - return new JSONResponse( |
|
72 | - ['message' => $message], |
|
73 | - Http::STATUS_BAD_REQUEST |
|
74 | - ); |
|
71 | + return new JSONResponse( |
|
72 | + ['message' => $message], |
|
73 | + Http::STATUS_BAD_REQUEST |
|
74 | + ); |
|
75 | 75 | |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | 78 | } |
@@ -119,9 +119,9 @@ |
||
119 | 119 | $elements = explode(' ', $fullName); |
120 | 120 | $result = ['', '', '', '', '']; |
121 | 121 | if (count($elements) > 2) { |
122 | - $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
122 | + $result[0] = implode(' ', array_slice($elements, count($elements) - 1)); |
|
123 | 123 | $result[1] = $elements[0]; |
124 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
124 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2)); |
|
125 | 125 | } elseif (count($elements) === 2) { |
126 | 126 | $result[0] = $elements[1]; |
127 | 127 | $result[1] = $elements[0]; |
@@ -30,120 +30,120 @@ |
||
30 | 30 | |
31 | 31 | class Converter { |
32 | 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->VERSION = '3.0'; |
|
59 | - $vCard->UID = $uid; |
|
60 | - |
|
61 | - $publish = false; |
|
62 | - |
|
63 | - foreach ($userData as $property => $value) { |
|
64 | - |
|
65 | - $shareWithTrustedServers = |
|
66 | - $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
67 | - $value['scope'] === AccountManager::VISIBILITY_PUBLIC; |
|
68 | - |
|
69 | - $emptyValue = !isset($value['value']) || $value['value'] === ''; |
|
70 | - $noImage = $image === null; |
|
71 | - |
|
72 | - if ($shareWithTrustedServers && (!$emptyValue || !$noImage)) { |
|
73 | - $publish = true; |
|
74 | - switch ($property) { |
|
75 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
76 | - $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
77 | - $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
78 | - break; |
|
79 | - case AccountManager::PROPERTY_AVATAR: |
|
80 | - if ($image !== null) { |
|
81 | - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
82 | - } |
|
83 | - break; |
|
84 | - case AccountManager::PROPERTY_EMAIL: |
|
85 | - $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
86 | - break; |
|
87 | - case AccountManager::PROPERTY_WEBSITE: |
|
88 | - $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
89 | - break; |
|
90 | - case AccountManager::PROPERTY_PHONE: |
|
91 | - $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
92 | - break; |
|
93 | - case AccountManager::PROPERTY_ADDRESS: |
|
94 | - $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
95 | - break; |
|
96 | - case AccountManager::PROPERTY_TWITTER: |
|
97 | - $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
98 | - break; |
|
99 | - } |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - if ($publish && !empty($cloudId)) { |
|
104 | - $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
105 | - $vCard->validate(); |
|
106 | - return $vCard; |
|
107 | - } |
|
108 | - |
|
109 | - return null; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param string $fullName |
|
114 | - * @return string[] |
|
115 | - */ |
|
116 | - public function splitFullName($fullName) { |
|
117 | - // Very basic western style parsing. I'm not gonna implement |
|
118 | - // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
119 | - |
|
120 | - $elements = explode(' ', $fullName); |
|
121 | - $result = ['', '', '', '', '']; |
|
122 | - if (count($elements) > 2) { |
|
123 | - $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
124 | - $result[1] = $elements[0]; |
|
125 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
126 | - } elseif (count($elements) === 2) { |
|
127 | - $result[0] = $elements[1]; |
|
128 | - $result[1] = $elements[0]; |
|
129 | - } else { |
|
130 | - $result[0] = $elements[0]; |
|
131 | - } |
|
132 | - |
|
133 | - return $result; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param IUser $user |
|
138 | - * @return null|IImage |
|
139 | - */ |
|
140 | - private function getAvatarImage(IUser $user) { |
|
141 | - try { |
|
142 | - $image = $user->getAvatarImage(-1); |
|
143 | - return $image; |
|
144 | - } catch (\Exception $ex) { |
|
145 | - return null; |
|
146 | - } |
|
147 | - } |
|
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->VERSION = '3.0'; |
|
59 | + $vCard->UID = $uid; |
|
60 | + |
|
61 | + $publish = false; |
|
62 | + |
|
63 | + foreach ($userData as $property => $value) { |
|
64 | + |
|
65 | + $shareWithTrustedServers = |
|
66 | + $value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY || |
|
67 | + $value['scope'] === AccountManager::VISIBILITY_PUBLIC; |
|
68 | + |
|
69 | + $emptyValue = !isset($value['value']) || $value['value'] === ''; |
|
70 | + $noImage = $image === null; |
|
71 | + |
|
72 | + if ($shareWithTrustedServers && (!$emptyValue || !$noImage)) { |
|
73 | + $publish = true; |
|
74 | + switch ($property) { |
|
75 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
76 | + $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
77 | + $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
78 | + break; |
|
79 | + case AccountManager::PROPERTY_AVATAR: |
|
80 | + if ($image !== null) { |
|
81 | + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
82 | + } |
|
83 | + break; |
|
84 | + case AccountManager::PROPERTY_EMAIL: |
|
85 | + $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
86 | + break; |
|
87 | + case AccountManager::PROPERTY_WEBSITE: |
|
88 | + $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
89 | + break; |
|
90 | + case AccountManager::PROPERTY_PHONE: |
|
91 | + $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
92 | + break; |
|
93 | + case AccountManager::PROPERTY_ADDRESS: |
|
94 | + $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
95 | + break; |
|
96 | + case AccountManager::PROPERTY_TWITTER: |
|
97 | + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
98 | + break; |
|
99 | + } |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + if ($publish && !empty($cloudId)) { |
|
104 | + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
105 | + $vCard->validate(); |
|
106 | + return $vCard; |
|
107 | + } |
|
108 | + |
|
109 | + return null; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param string $fullName |
|
114 | + * @return string[] |
|
115 | + */ |
|
116 | + public function splitFullName($fullName) { |
|
117 | + // Very basic western style parsing. I'm not gonna implement |
|
118 | + // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
119 | + |
|
120 | + $elements = explode(' ', $fullName); |
|
121 | + $result = ['', '', '', '', '']; |
|
122 | + if (count($elements) > 2) { |
|
123 | + $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
124 | + $result[1] = $elements[0]; |
|
125 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
126 | + } elseif (count($elements) === 2) { |
|
127 | + $result[0] = $elements[1]; |
|
128 | + $result[1] = $elements[0]; |
|
129 | + } else { |
|
130 | + $result[0] = $elements[0]; |
|
131 | + } |
|
132 | + |
|
133 | + return $result; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param IUser $user |
|
138 | + * @return null|IImage |
|
139 | + */ |
|
140 | + private function getAvatarImage(IUser $user) { |
|
141 | + try { |
|
142 | + $image = $user->getAvatarImage(-1); |
|
143 | + return $image; |
|
144 | + } catch (\Exception $ex) { |
|
145 | + return null; |
|
146 | + } |
|
147 | + } |
|
148 | 148 | |
149 | 149 | } |