diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2020-11-07 19:24:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-07 19:24:27 +0300 |
commit | 28355a29826de3fa5ad31d585e4aa942ad8b9c25 (patch) | |
tree | a6df8435ef7933c923b7c7cf19f7b06579dc204e /lib/Service | |
parent | c2f923ad23c55e53d4d1fff2e0ebbeba26754aaf (diff) | |
parent | 23e887b0dbcc4f4dbd4fbd9e1244f1db4a7b055d (diff) |
Merge branch 'master' into fix/facebook-defunc
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/Social/CompositeSocialProvider.php | 58 | ||||
-rw-r--r-- | lib/Service/Social/DiasporaProvider.php | 90 | ||||
-rw-r--r-- | lib/Service/Social/FacebookProvider.php | 74 | ||||
-rw-r--r-- | lib/Service/Social/GravatarProvider.php | 65 | ||||
-rw-r--r-- | lib/Service/Social/ISocialProvider.php | 17 | ||||
-rw-r--r-- | lib/Service/Social/InstagramProvider.php | 80 | ||||
-rw-r--r-- | lib/Service/Social/MastodonProvider.php | 88 | ||||
-rw-r--r-- | lib/Service/Social/TumblrProvider.php | 58 | ||||
-rw-r--r-- | lib/Service/Social/TwitterProvider.php | 65 | ||||
-rw-r--r-- | lib/Service/Social/XingProvider.php | 91 | ||||
-rw-r--r-- | lib/Service/SocialApiService.php | 53 |
11 files changed, 591 insertions, 148 deletions
diff --git a/lib/Service/Social/CompositeSocialProvider.php b/lib/Service/Social/CompositeSocialProvider.php index 5a22983c..a9fbcc00 100644 --- a/lib/Service/Social/CompositeSocialProvider.php +++ b/lib/Service/Social/CompositeSocialProvider.php @@ -33,21 +33,23 @@ class CompositeSocialProvider { public function __construct(InstagramProvider $instagramProvider, MastodonProvider $mastodonProvider, - // FacebookProvider $facebookProvider, TwitterProvider $twitterProvider, + // FacebookProvider $facebookProvider, TumblrProvider $tumblrProvider, DiasporaProvider $diasporaProvider, - XingProvider $xingProvider) { + XingProvider $xingProvider, + GravatarProvider $gravatarProvider) { // This determines the priority of known providers $this->providers = [ - 'instagram' => $instagramProvider, - 'mastodon' => $mastodonProvider, - 'twitter' => $twitterProvider, - // 'facebook' => $facebookProvider, // disfunct since oct 2020 - 'tumblr' => $tumblrProvider, - 'diaspora' => $diasporaProvider, - 'xing' => $xingProvider, + $instagramProvider->name => $instagramProvider, + $mastodonProvider->name => $mastodonProvider, + $twitterProvider->name => $twitterProvider, + // $facebookProvider->name => $facebookProvider, + $tumblrProvider->name => $tumblrProvider, + $diasporaProvider->name => $diasporaProvider, + $xingProvider->name => $xingProvider, + $gravatarProvider->name => $gravatarProvider ]; } @@ -60,40 +62,28 @@ class CompositeSocialProvider { return array_keys($this->providers); } - /** * generate download url for a social entry * - * @param array socialEntries all social data from the contact - * @param String network the choice which network to use (fallback: take first available) + * @param String network the choice which network to use * - * @returns String the url to the requested information or null in case of errors + * @return ISocialProvider if provider of 'network' is found, otherwise null */ - public function getSocialConnector(array $socialEntries, string $network) : ?string { + public function getSocialConnector(string $network) : ?ISocialProvider { $connector = null; - $selection = $this->providers; // check if dedicated network selected if (isset($this->providers[$network])) { - $selection = [$network => $this->providers[$network]]; + $connector = $this->providers[$network]; } + return $connector; + } - // check selected providers in order - foreach ($selection as $type => $socialProvider) { - - // search for this network in user's profile - foreach ($socialEntries as $socialEntry) { - if (strtolower($type) === strtolower($socialEntry['type'])) { - $profileId = $socialProvider->cleanupId($socialEntry['value']); - if (!is_null($profileId)) { - $connector = $socialProvider->getImageUrl($profileId); - } - break; - } - } - if ($connector) { - break; - } - } - return ($connector); + /** + * generate download url for a social entry + * + * @return ISocialProvider[] all social providers + */ + public function getSocialConnectors() : array { + return array_values($this->providers); } } diff --git a/lib/Service/Social/DiasporaProvider.php b/lib/Service/Social/DiasporaProvider.php index 73f85792..c0af278f 100644 --- a/lib/Service/Social/DiasporaProvider.php +++ b/lib/Service/Social/DiasporaProvider.php @@ -30,31 +30,48 @@ class DiasporaProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; - /** @var boolean */ + /** @var bool */ private $looping; + /** @var string */ + public $name = "diaspora"; + public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); $this->looping = false; } - + /** - * Returns the profile-id + * Returns if this provider supports this contact * - * @param {string} the value from the contact's x-socialprofile + * @param {array} contact info * - * @return string + * @return bool */ - public function cleanupId(string $candidate):string { - try { - if (strpos($candidate, 'http') !== 0) { - $user_server = explode('@', $candidate); - $candidate = 'https://' . array_pop($user_server) . '/public/' . array_pop($user_server) . '.atom'; + public function supportsContact(array $contact):bool { + $socialprofiles = $this->getProfileIds($contact); + return isset($socialprofiles) && count($socialprofiles) > 0; + } + + /** + * Returns all possible profile-picture urls + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + + foreach ($profileIds as $profileId) { + $url = $this->getImageUrl($profileId); + if (isset($url)) { + $urls[] = $url; } - } catch (Exception $e) { - $candidate = null; } - return $candidate; + + return $urls; } /** @@ -64,7 +81,7 @@ class DiasporaProvider implements ISocialProvider { * * @return string|null */ - public function getImageUrl(string $profileUrl):?string { + protected function getImageUrl(string $profileUrl):?string { try { $result = $this->httpClient->get($profileUrl); $htmlResult = $result->getBody(); @@ -82,8 +99,51 @@ class DiasporaProvider implements ISocialProvider { } } return null; - } catch (Exception $e) { + } catch (\Exception $e) { return null; } } + + /** + * Returns all possible profile ids for contact + * + * @param {array} contact information + * + * @return array + */ + protected function getProfileIds($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profileIds = []; + + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profileId = $this->cleanupId($profile['value']); + if (isset($profileId)) { + $profileIds[] = $profileId; + } + } + } + } + return $profileIds; + } + + /** + * Returns the profile-id + * + * @param {string} the value from the contact's x-socialprofile + * + * @return string + */ + protected function cleanupId(string $candidate):?string { + try { + if (strpos($candidate, 'http') !== 0) { + $user_server = explode('@', $candidate); + $candidate = 'https://' . array_pop($user_server) . '/public/' . array_pop($user_server) . '.atom'; + } + } catch (\Exception $e) { + $candidate = null; + } + return $candidate; + } } diff --git a/lib/Service/Social/FacebookProvider.php b/lib/Service/Social/FacebookProvider.php index 770ac45a..e5d34397 100644 --- a/lib/Service/Social/FacebookProvider.php +++ b/lib/Service/Social/FacebookProvider.php @@ -30,10 +30,43 @@ class FacebookProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; + /** @var string */ + public $name = "facebook"; + public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); } - + + /** + * Returns if this provider supports this contact + * + * @param {array} contact info + * + * @return bool + */ + public function supportsContact(array $contact):bool { + $socialprofiles = $this->getProfiles($contact); + return isset($socialprofiles) && count($socialprofiles) > 0; + } + + /** + * Returns the profile-picture url + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + foreach ($profileIds as $profileId) { + $recipe = 'https://graph.facebook.com/{socialId}/picture?width=720'; + $connector = str_replace("{socialId}", $profileId, $recipe); + $urls[] = $connector; + } + return $urls; + } + /** * Returns the profile-id * @@ -41,7 +74,7 @@ class FacebookProvider implements ISocialProvider { * * @return string */ - public function cleanupId(string $candidate):string { + protected function cleanupId(string $candidate):string { $candidate = basename($candidate); if (!is_numeric($candidate)) { $candidate = $this->findFacebookId($candidate); @@ -50,16 +83,39 @@ class FacebookProvider implements ISocialProvider { } /** - * Returns the profile-picture url + * Returns all possible profile ids for contact * - * @param {string} profileId the profile-id + * @param {array} contact information * - * @return string + * @return array of string profile ids */ - public function getImageUrl(string $profileId):string { - $recipe = 'https://graph.facebook.com/{socialId}/picture?width=720'; - $connector = str_replace("{socialId}", $profileId, $recipe); - return $connector; + protected function getProfiles(array $contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profiles = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profiles[] = $profile['value']; + } + } + } + return $profiles; + } + + /** + * Returns all possible profile ids for contact + * + * @param {array} contact information + * + * @return array of string profile ids + */ + protected function getProfileIds(array $contact):array { + $profiles = $this->getProfiles($contact); + $profileIds = []; + foreach ($profiles as $profile) { + $profileIds[] = $this->cleanupId($profile); + } + return $profileIds; } /** diff --git a/lib/Service/Social/GravatarProvider.php b/lib/Service/Social/GravatarProvider.php new file mode 100644 index 00000000..4b9d9a84 --- /dev/null +++ b/lib/Service/Social/GravatarProvider.php @@ -0,0 +1,65 @@ +<?php +/** + * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de> + * + * @author leith <online-nextcloud@eleith.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Contacts\Service\Social; + +class GravatarProvider implements ISocialProvider { + /** @var string */ + public $name = "gravatar"; + + public function __construct() { + } + + /** + * Returns if this provider supports this contact + * + * @param {array} contact info + * + * @return bool + */ + public function supportsContact(array $contact):bool { + $emails = $contact['EMAIL']; + return isset($emails) && count($emails); + } + + /** + * Returns the profile-picture url + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $urls = []; + $emails = $contact['EMAIL']; + if (isset($emails)) { + foreach ($emails as $email) { + $hash = md5(strtolower(trim($email['value']))); + $recipe = 'https://www.gravatar.com/avatar/{hash}?s=720&d=404'; + $connector = str_replace("{hash}", $hash, $recipe); + $urls[] = $connector; + } + } + return $urls; + } +} diff --git a/lib/Service/Social/ISocialProvider.php b/lib/Service/Social/ISocialProvider.php index 1415f4d0..a21fe32b 100644 --- a/lib/Service/Social/ISocialProvider.php +++ b/lib/Service/Social/ISocialProvider.php @@ -24,22 +24,21 @@ namespace OCA\Contacts\Service\Social; interface ISocialProvider { - /** - * Returns the profile-id + * Returns true if provider supports the contact * - * @param {string} the value from the contact's x-socialprofile + * @param {array} contact details * - * @return string + * @return boolean */ - public function cleanupId(string $candidate):?string ; + public function supportsContact(array $contact):bool ; /** - * Returns the profile-picture url + * Returns all possible profile-picture urls * - * @param {string} profileId the profile-id + * @param {array} contact information * - * @return string|null + * @return array */ - public function getImageUrl(string $profileId):?string ; + public function getImageUrls(array $contact):array ; } diff --git a/lib/Service/Social/InstagramProvider.php b/lib/Service/Social/InstagramProvider.php index b7687e71..a43d12c3 100644 --- a/lib/Service/Social/InstagramProvider.php +++ b/lib/Service/Social/InstagramProvider.php @@ -30,10 +30,44 @@ class InstagramProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; + /** @var string */ + public $name = "instagram"; + public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); } - + + /** + * Returns if this provider supports this contact + * + * @param {array} contact info + * + * @return bool + */ + public function supportsContact(array $contact):bool { + $socialprofiles = $this->getProfiles($contact); + return isset($socialprofiles) && count($socialprofiles) > 0; + } + + /** + * Returns the profile-picture url + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + foreach ($profileIds as $profileId) { + $recipe = 'https://www.instagram.com/{socialId}/?__a=1'; + $connector = str_replace("{socialId}", $profileId, $recipe); + $connector = $this->getFromJson($connector, 'graphql->user->profile_pic_url_hd'); + $urls[] = $connector; + } + return $urls; + } + /** * Returns the profile-id * @@ -41,25 +75,47 @@ class InstagramProvider implements ISocialProvider { * * @return string */ - public function cleanupId(string $candidate):string { + protected function cleanupId(string $candidate):string { $candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate); return basename($candidate); } + + /** + * Returns all possible profile urls for contact + * + * @param {array} contact information + * + * @return array of string profile urls + */ + protected function getProfiles($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profiles = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profiles[] = $profile['value']; + } + } + } + return $profiles; + } /** - * Returns the profile-picture url + * Returns all possible profile ids for contact * - * @param {string} profileId the profile-id + * @param {array} contact information * - * @return string|null + * @return array of string profile ids */ - public function getImageUrl(string $profileId):?string { - $recipe = 'https://www.instagram.com/{socialId}/?__a=1'; - $connector = str_replace("{socialId}", $profileId, $recipe); - $connector = $this->getFromJson($connector, 'graphql->user->profile_pic_url_hd'); - return $connector; + protected function getProfileIds($contact):array { + $socialprofiles = $this->getProfiles($contact); + $profileIds = []; + foreach ($socialprofiles as $profile) { + $profileIds[] = $this->cleanupId($profile); + } + return $profileIds; } - + /** * extracts desired value from a json * @@ -81,7 +137,7 @@ class InstagramProvider implements ISocialProvider { $jsonResult = $jsonResult[$loc]; } return $jsonResult; - } catch (Exception $e) { + } catch (\Exception $e) { return null; } } diff --git a/lib/Service/Social/MastodonProvider.php b/lib/Service/Social/MastodonProvider.php index 74b68e96..a212433c 100644 --- a/lib/Service/Social/MastodonProvider.php +++ b/lib/Service/Social/MastodonProvider.php @@ -30,34 +30,49 @@ class MastodonProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; + /** @var string */ + public $name = "mastodon"; + public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); } - + /** - * Returns the profile-id + * Returns if this provider supports this contact * - * @param {string} the value from the contact's x-socialprofile + * @param {array} contact info * - * @return string + * @return bool */ - public function cleanupId(string $candidate):?string { - $candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate); - try { - if (strpos($candidate, 'http') !== 0) { - $user_server = explode('@', $candidate); - $candidate = 'https://' . array_pop($user_server) . '/@' . array_pop($user_server); + public function supportsContact(array $contact):bool { + $profiles = $this->getProfileIds($contact); + return isset($profiles) && count($profiles) > 0; + } + + /** + * Returns all possible profile-picture urls + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + + foreach ($profileIds as $profileId) { + $url = $this->getImageUrl($profileId); + if (isset($url)) { + $urls[] = $url; } - } catch (Exception $e) { - $candidate = null; } - return $candidate; + return $urls; } /** * Returns the profile-picture url * - * @param {string} profileUrl link to the profile + * @param {array} contact information * * @return string|null */ @@ -72,8 +87,51 @@ class MastodonProvider implements ISocialProvider { return $img->getAttribute("data-original"); } return null; - } catch (Exception $e) { + } catch (\Exception $e) { return null; } } + + /** + * Returns all possible profile ids for contact + * + * @param {array} contact information + * + * @return array of possible profileIds + */ + protected function getProfileIds($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profileId = $this->cleanupId($profile['value']); + if (isset($profileId)) { + $profileIds[] = $profileId; + } + } + } + } + return $profileIds; + } + + /** + * Returns the profile-id + * + * @param {string} the value from the contact's x-socialprofile + * + * @return string + */ + protected function cleanupId(string $candidate):?string { + $candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate); + try { + if (strpos($candidate, 'http') !== 0) { + $user_server = explode('@', $candidate); + $candidate = 'https://' . array_pop($user_server) . '/@' . array_pop($user_server); + } + } catch (\Exception $e) { + $candidate = null; + } + return $candidate; + } } diff --git a/lib/Service/Social/TumblrProvider.php b/lib/Service/Social/TumblrProvider.php index a830b945..cd3d577f 100644 --- a/lib/Service/Social/TumblrProvider.php +++ b/lib/Service/Social/TumblrProvider.php @@ -24,9 +24,42 @@ namespace OCA\Contacts\Service\Social; class TumblrProvider implements ISocialProvider { + /** @var string */ + public $name = "tumblr"; + public function __construct() { } - + + /** + * Returns if this provider supports this contact + * + * @param {array} contact info + * + * @return bool + */ + public function supportsContact(array $contact):bool { + $socialprofiles = $this->getProfileIds($contact); + return isset($socialprofiles) && count($socialprofiles) > 0; + } + + /** + * Returns the profile-picture url + * + * @param {string} profileId the profile-id + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + foreach ($profileIds as $profileId) { + $recipe = 'https://api.tumblr.com/v2/blog/{socialId}/avatar/512'; + $connector = str_replace("{socialId}", $profileId, $recipe); + $urls[] = $connector; + } + return $urls; + } + /** * Returns the profile-id * @@ -34,7 +67,7 @@ class TumblrProvider implements ISocialProvider { * * @return string */ - public function cleanupId(string $candidate):?string { + protected function cleanupId(string $candidate):?string { $candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate); $subdomain = '/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i'; // subdomain if (preg_match($subdomain, $candidate, $matches)) { @@ -44,15 +77,22 @@ class TumblrProvider implements ISocialProvider { } /** - * Returns the profile-picture url + * Returns all possible profile ids for contact * - * @param {string} profileId the profile-id + * @param {array} contact information * - * @return string|null + * @return array of string profile ids */ - public function getImageUrl(string $profileId):?string { - $recipe = 'https://api.tumblr.com/v2/blog/{socialId}/avatar/512'; - $connector = str_replace("{socialId}", $profileId, $recipe); - return $connector; + protected function getProfileIds($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profileIds[] = $this->cleanupId($profile['value']); + } + } + } + return $profileIds; } } diff --git a/lib/Service/Social/TwitterProvider.php b/lib/Service/Social/TwitterProvider.php index 953eb1a0..18cd0d46 100644 --- a/lib/Service/Social/TwitterProvider.php +++ b/lib/Service/Social/TwitterProvider.php @@ -26,14 +26,47 @@ namespace OCA\Contacts\Service\Social; use OCP\Http\Client\IClientService; class TwitterProvider implements ISocialProvider { - /** @var IClientService */ private $httpClient; + /** @var string */ + public $name = "twitter"; + public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); } - + + /** + * Returns if this provider supports this contact + * + * @param {array} contact info + * + * @return bool + */ + public function supportsContact(array $contact):bool { + $socialprofiles = $this->getProfileIds($contact); + return isset($socialprofiles) && count($socialprofiles) > 0; + } + + /** + * Returns the profile-picture url + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + foreach ($profileIds as $profileId) { + $recipe = 'https://mobile.twitter.com/{socialId}'; + $connector = str_replace("{socialId}", $profileId, $recipe); + $connector = $this->getFromHtml($connector, '_normal'); + $urls[] = $connector; + } + return $urls; + } + /** * Returns the profile-id * @@ -41,7 +74,7 @@ class TwitterProvider implements ISocialProvider { * * @return string */ - public function cleanupId(string $candidate):string { + protected function cleanupId(string $candidate):string { $candidate = basename($candidate); if ($candidate[0] === '@') { $candidate = substr($candidate, 1); @@ -50,19 +83,25 @@ class TwitterProvider implements ISocialProvider { } /** - * Returns the profile-picture url + * Returns all possible profile ids for contact * - * @param {string} profileId the profile-id + * @param {array} contact information * - * @return string|null + * @return array of string profile ids */ - public function getImageUrl(string $profileId):?string { - $recipe = 'https://mobile.twitter.com/{socialId}'; - $connector = str_replace("{socialId}", $profileId, $recipe); - $connector = $this->getFromHtml($connector, '_normal'); - return $connector; + protected function getProfileIds($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profileIds[] = $this->cleanupId($profile['value']); + } + } + } + return $profileIds; } - + /** * extracts desired value from an html page * @@ -88,7 +127,7 @@ class TwitterProvider implements ISocialProvider { } } return null; - } catch (Exception $e) { + } catch (\Exception $e) { return null; } } diff --git a/lib/Service/Social/XingProvider.php b/lib/Service/Social/XingProvider.php index 739d1fd2..a752d3c6 100644 --- a/lib/Service/Social/XingProvider.php +++ b/lib/Service/Social/XingProvider.php @@ -30,41 +30,54 @@ class XingProvider implements ISocialProvider { /** @var IClientService */ private $httpClient; - /** @var boolean */ - private $looping; + /** @var string */ + public $name = "xing"; public function __construct(IClientService $httpClient) { $this->httpClient = $httpClient->NewClient(); - $this->looping = false; } - + /** - * Returns the profile-id + * Returns if this provider supports this contact * - * @param {string} the value from the contact's x-socialprofile + * @param {array} contact info * - * @return string + * @return bool */ - public function cleanupId(string $candidate):string { - $candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate); - try { - if (strpos($candidate, 'http') !== 0) { - $candidate = 'https://www.xing.com/profile/' . $candidate; + public function supportsContact(array $contact):bool { + $socialprofiles = $this->getProfileIds($contact); + return isset($socialprofiles) && count($socialprofiles) > 0; + } + + /** + * Returns all possible profile-picture urls + * + * @param {array} contact information + * + * @return array + */ + public function getImageUrls(array $contact):array { + $profileIds = $this->getProfileIds($contact); + $urls = []; + + foreach ($profileIds as $profileId) { + $url = $this->getImageUrl($profileId); + if (isset($url)) { + $urls[] = $url; } - } catch (Exception $e) { - $candidate = null; } - return $candidate; + + return $urls; } /** * Returns the profile-picture url * - * @param {string} profileId the profile-id + * @param {string} profile url * * @return string|null */ - public function getImageUrl(string $profileUrl):?string { + protected function getImageUrl(string $profileUrl):?string { try { $result = $this->httpClient->get($profileUrl); $htmlResult = $result->getBody(); @@ -75,8 +88,50 @@ class XingProvider implements ISocialProvider { } // keyword not found, maybe page changed? return null; - } catch (Exception $e) { + } catch (\Exception $e) { return null; } } + + /** + * Returns the profile-id + * + * @param {string} the value from the contact's x-socialprofile + * + * @return string + */ + protected function cleanupId(string $candidate):?string { + $candidate = preg_replace('/^' . preg_quote('x-apple:', '/') . '/', '', $candidate); + try { + if (strpos($candidate, 'http') !== 0) { + $candidate = 'https://www.xing.com/profile/' . $candidate; + } + } catch (\Exception $e) { + $candidate = null; + } + return $candidate; + } + + /** + * Returns all possible profile ids for contact + * + * @param {array} contact information + * + * @return string of first profile url else null + */ + protected function getProfileIds($contact):array { + $socialprofiles = $contact['X-SOCIALPROFILE']; + $profileIds = []; + if (isset($socialprofiles)) { + foreach ($socialprofiles as $profile) { + if (strtolower($profile['type']) == $this->name) { + $profileId = $this->cleanupId($profile['value']); + if (isset($profileId)) { + $profileIds[] = $profileId; + } + } + } + } + return $profileIds; + } } diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php index 03484c99..b1657734 100644 --- a/lib/Service/SocialApiService.php +++ b/lib/Service/SocialApiService.php @@ -50,9 +50,9 @@ class SocialApiService { private $config; /** @var IClientService */ private $clientService; - /** @var IL10N */ + /** @var IL10N */ private $l10n; - /** @var IURLGenerator */ + /** @var IURLGenerator */ private $urlGen; /** @var CardDavBackend */ private $davBackend; @@ -84,7 +84,7 @@ class SocialApiService { /** * returns an array of supported social networks * - * @returns {array} array of the supported social networks + * @return {array} array of the supported social networks */ public function getSupportedNetworks() : array { $syncAllowedByAdmin = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes'); @@ -118,7 +118,7 @@ class SocialApiService { $contact['PHOTO;ENCODING=b;TYPE=' . $imageType . ';VALUE=BINARY'] = $photo; // remove previous photo (necessary as new attribute is not equal to 'PHOTO') - $contact['PHOTO'] = ''; + unset($contact['PHOTO']); } } @@ -167,8 +167,11 @@ class SocialApiService { * * @returns {JSONResponse} an empty JSONResponse with respective http status code */ - public function updateContact(string $addressbookId, string $contactId, string $network) : JSONResponse { - $url = null; + public function updateContact(string $addressbookId, string $contactId, ?string $network) : JSONResponse { + $socialdata = null; + $imageType = null; + $urls = []; + $allConnectors = $this->socialProvider->getSocialConnectors(); try { // get corresponding addressbook @@ -179,20 +182,42 @@ class SocialApiService { // search contact in that addressbook, get social data $contact = $addressBook->search($contactId, ['UID'], ['types' => true])[0]; - if (!isset($contact['X-SOCIALPROFILE'])) { + + if (!isset($contact)) { + return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); + } + + if ($network) { + $allConnectors = [$this->socialProvider->getSocialConnector($network)]; + } + + $connectors = array_filter($allConnectors, function ($connector) use ($contact) { + return $connector->supportsContact($contact); + }); + + if (count($connectors) == 0) { return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); } - $socialprofiles = $contact['X-SOCIALPROFILE']; - // retrieve data - $url = $this->socialProvider->getSocialConnector($socialprofiles, $network); - if (empty($url)) { + foreach ($connectors as $connector) { + $urls = array_merge($connector->getImageUrls($contact), $urls); + } + + if (count($urls) == 0) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } - $httpResult = $this->clientService->NewClient()->get($url); - $socialdata = $httpResult->getBody(); - $imageType = $httpResult->getHeader('content-type'); + foreach ($urls as $url) { + try { + $httpResult = $this->clientService->NewClient()->get($url); + $socialdata = $httpResult->getBody(); + $imageType = $httpResult->getHeader('content-type'); + if (isset($socialdata) && isset($imageType)) { + break; + } + } catch (\Exception $e) { + } + } if (!$socialdata || $imageType === null) { return new JSONResponse([], Http::STATUS_NOT_FOUND); |