@@ -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 | } |
@@ -30,119 +30,119 @@ |
||
| 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'] === '') || $image === null; |
|
| 70 | - |
|
| 71 | - if ($shareWithTrustedServers && !$emptyValue) { |
|
| 72 | - $publish = true; |
|
| 73 | - switch ($property) { |
|
| 74 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 75 | - $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
| 76 | - $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
| 77 | - break; |
|
| 78 | - case AccountManager::PROPERTY_AVATAR: |
|
| 79 | - if ($image !== null) { |
|
| 80 | - $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
| 81 | - } |
|
| 82 | - break; |
|
| 83 | - case AccountManager::PROPERTY_EMAIL: |
|
| 84 | - $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
| 85 | - break; |
|
| 86 | - case AccountManager::PROPERTY_WEBSITE: |
|
| 87 | - $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
| 88 | - break; |
|
| 89 | - case AccountManager::PROPERTY_PHONE: |
|
| 90 | - $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
| 91 | - break; |
|
| 92 | - case AccountManager::PROPERTY_ADDRESS: |
|
| 93 | - $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
| 94 | - break; |
|
| 95 | - case AccountManager::PROPERTY_TWITTER: |
|
| 96 | - $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
| 97 | - break; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ($publish && !empty($cloudId)) { |
|
| 103 | - $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
| 104 | - $vCard->validate(); |
|
| 105 | - return $vCard; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return null; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param string $fullName |
|
| 113 | - * @return string[] |
|
| 114 | - */ |
|
| 115 | - public function splitFullName($fullName) { |
|
| 116 | - // Very basic western style parsing. I'm not gonna implement |
|
| 117 | - // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
| 118 | - |
|
| 119 | - $elements = explode(' ', $fullName); |
|
| 120 | - $result = ['', '', '', '', '']; |
|
| 121 | - if (count($elements) > 2) { |
|
| 122 | - $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
| 123 | - $result[1] = $elements[0]; |
|
| 124 | - $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
| 125 | - } elseif (count($elements) === 2) { |
|
| 126 | - $result[0] = $elements[1]; |
|
| 127 | - $result[1] = $elements[0]; |
|
| 128 | - } else { |
|
| 129 | - $result[0] = $elements[0]; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $result; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @param IUser $user |
|
| 137 | - * @return null|IImage |
|
| 138 | - */ |
|
| 139 | - private function getAvatarImage(IUser $user) { |
|
| 140 | - try { |
|
| 141 | - $image = $user->getAvatarImage(-1); |
|
| 142 | - return $image; |
|
| 143 | - } catch (\Exception $ex) { |
|
| 144 | - return null; |
|
| 145 | - } |
|
| 146 | - } |
|
| 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'] === '') || $image === null; |
|
| 70 | + |
|
| 71 | + if ($shareWithTrustedServers && !$emptyValue) { |
|
| 72 | + $publish = true; |
|
| 73 | + switch ($property) { |
|
| 74 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 75 | + $vCard->add(new Text($vCard, 'FN', $value['value'])); |
|
| 76 | + $vCard->add(new Text($vCard, 'N', $this->splitFullName($value['value']))); |
|
| 77 | + break; |
|
| 78 | + case AccountManager::PROPERTY_AVATAR: |
|
| 79 | + if ($image !== null) { |
|
| 80 | + $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]); |
|
| 81 | + } |
|
| 82 | + break; |
|
| 83 | + case AccountManager::PROPERTY_EMAIL: |
|
| 84 | + $vCard->add(new Text($vCard, 'EMAIL', $value['value'], ['TYPE' => 'OTHER'])); |
|
| 85 | + break; |
|
| 86 | + case AccountManager::PROPERTY_WEBSITE: |
|
| 87 | + $vCard->add(new Text($vCard, 'URL', $value['value'])); |
|
| 88 | + break; |
|
| 89 | + case AccountManager::PROPERTY_PHONE: |
|
| 90 | + $vCard->add(new Text($vCard, 'TEL', $value['value'], ['TYPE' => 'OTHER'])); |
|
| 91 | + break; |
|
| 92 | + case AccountManager::PROPERTY_ADDRESS: |
|
| 93 | + $vCard->add(new Text($vCard, 'ADR', $value['value'], ['TYPE' => 'OTHER'])); |
|
| 94 | + break; |
|
| 95 | + case AccountManager::PROPERTY_TWITTER: |
|
| 96 | + $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $value['value'], ['TYPE' => 'TWITTER'])); |
|
| 97 | + break; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ($publish && !empty($cloudId)) { |
|
| 103 | + $vCard->add(new Text($vCard, 'CLOUD', $cloudId)); |
|
| 104 | + $vCard->validate(); |
|
| 105 | + return $vCard; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return null; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param string $fullName |
|
| 113 | + * @return string[] |
|
| 114 | + */ |
|
| 115 | + public function splitFullName($fullName) { |
|
| 116 | + // Very basic western style parsing. I'm not gonna implement |
|
| 117 | + // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;) |
|
| 118 | + |
|
| 119 | + $elements = explode(' ', $fullName); |
|
| 120 | + $result = ['', '', '', '', '']; |
|
| 121 | + if (count($elements) > 2) { |
|
| 122 | + $result[0] = implode(' ', array_slice($elements, count($elements)-1)); |
|
| 123 | + $result[1] = $elements[0]; |
|
| 124 | + $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2)); |
|
| 125 | + } elseif (count($elements) === 2) { |
|
| 126 | + $result[0] = $elements[1]; |
|
| 127 | + $result[1] = $elements[0]; |
|
| 128 | + } else { |
|
| 129 | + $result[0] = $elements[0]; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $result; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @param IUser $user |
|
| 137 | + * @return null|IImage |
|
| 138 | + */ |
|
| 139 | + private function getAvatarImage(IUser $user) { |
|
| 140 | + try { |
|
| 141 | + $image = $user->getAvatarImage(-1); |
|
| 142 | + return $image; |
|
| 143 | + } catch (\Exception $ex) { |
|
| 144 | + return null; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | } |
@@ -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]; |