Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/contacts.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatthias <matthias@butler>2020-03-31 00:32:37 +0300
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2020-07-25 10:40:05 +0300
commite84571f0037b1222549e5fe9b4ac65ee4a1285aa (patch)
tree691f701b6021e6b0db7fcf339afe0edb69e5a617 /lib/Service
parente2633171d6a0581c76fcbf9abb162b581b1d219e (diff)
Allow for avatar downloads from social networks
Signed-off-by: call-me-matt <nextcloud@matthiasheinisch.de>
Diffstat (limited to 'lib/Service')
-rw-r--r--lib/Service/Social/CompositeSocialProvider.php95
-rw-r--r--lib/Service/Social/FacebookProvider.php99
-rw-r--r--lib/Service/Social/ISocialProvider.php45
-rw-r--r--lib/Service/Social/InstagramProvider.php87
-rw-r--r--lib/Service/Social/MastodonProvider.php78
-rw-r--r--lib/Service/Social/TumblrProvider.php57
-rw-r--r--lib/Service/Social/TwitterProvider.php95
-rw-r--r--lib/Service/SocialApiService.php185
8 files changed, 741 insertions, 0 deletions
diff --git a/lib/Service/Social/CompositeSocialProvider.php b/lib/Service/Social/CompositeSocialProvider.php
new file mode 100644
index 00000000..a5f6e484
--- /dev/null
+++ b/lib/Service/Social/CompositeSocialProvider.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+/**
+ * Composition of all social providers for easier usage
+ */
+class CompositeSocialProvider {
+
+ /** @var ISocialProvider[] */
+ private $providers;
+
+ public function __construct(InstagramProvider $instagramProvider,
+ MastodonProvider $mastodonProvider,
+ FacebookProvider $facebookProvider,
+ TwitterProvider $twitterProvider,
+ TumblrProvider $tumblrProvider) {
+
+ // This determines the priority of known providers
+ $this->providers = [
+ 'instagram' => $instagramProvider,
+ 'mastodon' => $mastodonProvider,
+ 'twitter' => $twitterProvider,
+ 'facebook' => $facebookProvider,
+ 'tumblr' => $tumblrProvider,
+ ];
+ }
+
+ /**
+ * returns an array of supported social providers
+ *
+ * @returns String[] array of the supported social networks
+ */
+ public function getSupportedNetworks() : array {
+ 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)
+ *
+ * @returns String the url to the requested information or null in case of errors
+ */
+ public function getSocialConnector(array $socialEntries, string $network) : ?string {
+ $connector = null;
+ $selection = $this->providers;
+ // check if dedicated network selected
+ if (isset($this->providers[$network])) {
+ $selection = [$network => $this->providers[$network]];
+ }
+
+ // 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);
+ }
+}
diff --git a/lib/Service/Social/FacebookProvider.php b/lib/Service/Social/FacebookProvider.php
new file mode 100644
index 00000000..028b1dcc
--- /dev/null
+++ b/lib/Service/Social/FacebookProvider.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+use OCP\Http\Client\IClientService;
+
+class FacebookProvider implements ISocialProvider {
+
+ /** @var IClientService */
+ private $httpClient;
+
+ public function __construct(IClientService $httpClient) {
+ $this->httpClient = $httpClient->NewClient();
+ }
+
+ /**
+ * Returns the profile-id
+ *
+ * @param {string} the value from the contact's x-socialprofile
+ *
+ * @return string
+ */
+ public function cleanupId(string $candidate):string {
+ $candidate = basename($candidate);
+ if (!is_numeric($candidate)) {
+ $candidate = $this->findFacebookId($candidate);
+ }
+ return $candidate;
+ }
+
+ /**
+ * Returns the profile-picture url
+ *
+ * @param {string} profileId the profile-id
+ *
+ * @return string
+ */
+ public function getImageUrl(string $profileId):string {
+ $recipe = 'https://graph.facebook.com/{socialId}/picture?width=720';
+ $connector = str_replace("{socialId}", $profileId, $recipe);
+ return $connector;
+ }
+
+ /**
+ * Tries to get the facebook id from facebook profile name
+ * e. g. "zuck" --> "4"
+ * Fallback: return profile name
+ * (will give oauth error from facebook except if profile is public)
+ *
+ * @param {string} profileName the user's profile name
+ *
+ * @return string
+ */
+ protected function findFacebookId(string $profileName):string {
+ try {
+ $result = $this->httpClient->get("https://facebook.com/".$profileName);
+ if ($result->getStatusCode() !== 200) {
+ return $profileName;
+ }
+ $htmlResult = new \DOMDocument();
+ $htmlResult->loadHTML($result->getBody());
+ $metas = $htmlResult->getElementsByTagName('meta');
+ foreach ($metas as $meta) {
+ foreach ($meta->attributes as $attr) {
+ $value = $attr->nodeValue;
+ if (strpos($value, "/profile/")) {
+ $value = str_replace('fb://profile/', '', $value);
+ return($value);
+ }
+ }
+ }
+ // keyword not found - page changed?
+ return $profileName;
+ } catch (\Exception $e) {
+ return $profileName;
+ }
+ }
+}
diff --git a/lib/Service/Social/ISocialProvider.php b/lib/Service/Social/ISocialProvider.php
new file mode 100644
index 00000000..1415f4d0
--- /dev/null
+++ b/lib/Service/Social/ISocialProvider.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+interface ISocialProvider {
+
+ /**
+ * Returns the profile-id
+ *
+ * @param {string} the value from the contact's x-socialprofile
+ *
+ * @return string
+ */
+ public function cleanupId(string $candidate):?string ;
+
+ /**
+ * Returns the profile-picture url
+ *
+ * @param {string} profileId the profile-id
+ *
+ * @return string|null
+ */
+ public function getImageUrl(string $profileId):?string ;
+}
diff --git a/lib/Service/Social/InstagramProvider.php b/lib/Service/Social/InstagramProvider.php
new file mode 100644
index 00000000..36c5a3d6
--- /dev/null
+++ b/lib/Service/Social/InstagramProvider.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+use OCP\Http\Client\IClientService;
+
+class InstagramProvider implements ISocialProvider {
+
+ /** @var IClientService */
+ private $httpClient;
+
+ public function __construct(IClientService $httpClient) {
+ $this->httpClient = $httpClient->NewClient();
+ }
+
+ /**
+ * Returns the profile-id
+ *
+ * @param {string} the value from the contact's x-socialprofile
+ *
+ * @return string
+ */
+ public function cleanupId(string $candidate):string {
+ return basename($candidate);
+ }
+
+ /**
+ * Returns the profile-picture url
+ *
+ * @param {string} profileId the profile-id
+ *
+ * @return string|null
+ */
+ 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;
+ }
+
+ /**
+ * extracts desired value from a json
+ *
+ * @param {string} url the target from where to fetch the json
+ * @param {String} the desired key to filter for (nesting possible with '->')
+ *
+ * @returns {String} the extracted value or null if not present
+ */
+ protected function getFromJson(string $url, string $desired) : ?string {
+ try {
+ $result = $this->httpClient->get($url);
+
+ $jsonResult = json_decode($result->getBody(),true);
+ $location = explode('->' , $desired);
+ foreach ($location as $loc) {
+ if (!isset($jsonResult[$loc])) {
+ return null;
+ }
+ $jsonResult = $jsonResult[$loc];
+ }
+ return $jsonResult;
+ } catch (Exception $e) {
+ return null;
+ }
+ }
+}
diff --git a/lib/Service/Social/MastodonProvider.php b/lib/Service/Social/MastodonProvider.php
new file mode 100644
index 00000000..bcbde9f3
--- /dev/null
+++ b/lib/Service/Social/MastodonProvider.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+use OCP\Http\Client\IClientService;
+
+class MastodonProvider implements ISocialProvider {
+
+ /** @var IClientService */
+ private $httpClient;
+
+ public function __construct(IClientService $httpClient) {
+ $this->httpClient = $httpClient->NewClient();
+ }
+
+ /**
+ * Returns the profile-id
+ *
+ * @param {string} the value from the contact's x-socialprofile
+ *
+ * @return string
+ */
+ public function cleanupId(string $candidate):?string {
+ try {
+ if (strpos($candidate, '@') === 0) {
+ $user_server = explode('@', $candidate);
+ $candidate = 'https://' . $user_server[2] . '/@' . $user_server[1];
+ }
+ } catch (Exception $e) {
+ $candidate = null;
+ }
+ return $candidate;
+ }
+
+ /**
+ * Returns the profile-picture url
+ *
+ * @param {string} profileUrl link to the profile
+ *
+ * @return string|null
+ */
+ public function getImageUrl(string $profileUrl):?string {
+ try {
+ $result = $this->httpClient->get($profileUrl);
+
+ $htmlResult = new \DOMDocument();
+ $htmlResult->loadHTML($result->getBody());
+ $img = $htmlResult->getElementById('profile_page_avatar');
+ if (!is_null($img)) {
+ return $img->getAttribute("data-original");
+ }
+ return null;
+ } catch (Exception $e) {
+ return null;
+ }
+ }
+}
diff --git a/lib/Service/Social/TumblrProvider.php b/lib/Service/Social/TumblrProvider.php
new file mode 100644
index 00000000..71f2fa71
--- /dev/null
+++ b/lib/Service/Social/TumblrProvider.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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 TumblrProvider implements ISocialProvider {
+ public function __construct() {
+ }
+
+ /**
+ * Returns the profile-id
+ *
+ * @param {string} the value from the contact's x-socialprofile
+ *
+ * @return string
+ */
+ public function cleanupId(string $candidate):?string {
+ $subdomain = '/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i'; // subdomain
+ if (preg_match($subdomain, $candidate, $matches)) {
+ $candidate = $matches[1];
+ }
+ return $candidate;
+ }
+
+ /**
+ * Returns the profile-picture url
+ *
+ * @param {string} profileId the profile-id
+ *
+ * @return string|null
+ */
+ public function getImageUrl(string $profileId):?string {
+ $recipe = 'https://api.tumblr.com/v2/blog/{socialId}/avatar/512';
+ $connector = str_replace("{socialId}", $profileId, $recipe);
+ return $connector;
+ }
+}
diff --git a/lib/Service/Social/TwitterProvider.php b/lib/Service/Social/TwitterProvider.php
new file mode 100644
index 00000000..953eb1a0
--- /dev/null
+++ b/lib/Service/Social/TwitterProvider.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+use OCP\Http\Client\IClientService;
+
+class TwitterProvider implements ISocialProvider {
+
+ /** @var IClientService */
+ private $httpClient;
+
+ public function __construct(IClientService $httpClient) {
+ $this->httpClient = $httpClient->NewClient();
+ }
+
+ /**
+ * Returns the profile-id
+ *
+ * @param {string} the value from the contact's x-socialprofile
+ *
+ * @return string
+ */
+ public function cleanupId(string $candidate):string {
+ $candidate = basename($candidate);
+ if ($candidate[0] === '@') {
+ $candidate = substr($candidate, 1);
+ }
+ return $candidate;
+ }
+
+ /**
+ * Returns the profile-picture url
+ *
+ * @param {string} profileId the profile-id
+ *
+ * @return string|null
+ */
+ 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;
+ }
+
+ /**
+ * extracts desired value from an html page
+ *
+ * @param {string} url the target from where to fetch the content
+ * @param {String} the desired catchword to filter for
+ *
+ * @returns {String} the extracted value (first match) or null if not present
+ */
+ protected function getFromHtml(string $url, string $desired) : ?string {
+ try {
+ $result = $this->httpClient->get($url);
+
+ $htmlResult = new \DOMDocument();
+ $htmlResult->loadHTML($result->getBody());
+ $imgs = $htmlResult->getElementsByTagName('img');
+ foreach ($imgs as $img) {
+ foreach ($img->attributes as $attr) {
+ $value = $attr->nodeValue;
+ if (strpos($value, $desired)) {
+ $value = str_replace("normal", "400x400", $value);
+ return $value;
+ }
+ }
+ }
+ return null;
+ } catch (Exception $e) {
+ return null;
+ }
+ }
+}
diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php
new file mode 100644
index 00000000..154e0295
--- /dev/null
+++ b/lib/Service/SocialApiService.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * @copyright Copyright (c) 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
+ *
+ * @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;
+
+use OCA\Contacts\Service\Social\CompositeSocialProvider;
+use OCA\Contacts\AppInfo\Application;
+
+use OCP\Contacts\IManager;
+use OCP\IAddressBook;
+
+use OCP\IConfig;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\Http\Client\IClientService;
+
+class SocialApiService {
+
+ /** @var CompositeSocialProvider */
+ private $socialProvider;
+ /** @var IManager */
+ private $manager;
+ /** @var IConfig */
+ private $config;
+ /** @var IClientService */
+ private $clientService;
+
+ public function __construct(
+ CompositeSocialProvider $socialProvider,
+ IManager $manager,
+ IConfig $config,
+ IClientService $clientService) {
+ $this->socialProvider = $socialProvider;
+ $this->manager = $manager;
+ $this->config = $config;
+ $this->clientService = $clientService;
+ }
+
+
+ /**
+ * @NoAdminRequired
+ *
+ * returns an array of supported social networks
+ *
+ * @returns {array} array of the supported social networks
+ */
+ public function getSupportedNetworks() : array {
+ $isAdminEnabled = $this->config->getAppValue(Application::APP_ID, 'allowSocialSync', 'yes');
+ if ($isAdminEnabled !== 'yes') {
+ return [];
+ }
+ return $this->socialProvider->getSupportedNetworks();
+ }
+
+
+ /**
+ * @NoAdminRequired
+ *
+ * Adds/updates photo for contact
+ *
+ * @param {pointer} contact reference to the contact to update
+ * @param {string} imageType the image type of the photo
+ * @param {string} photo the photo as base64 string
+ */
+ protected function addPhoto(array &$contact, string $imageType, string $photo) {
+ $version = $contact['VERSION'];
+
+ if (!empty($contact['PHOTO'])) {
+ // overwriting without notice!
+ }
+
+ if ($version >= 4.0) {
+ // overwrite photo
+ $contact['PHOTO'] = "data:" . $imageType . ";base64," . $photo;
+ } elseif ($version >= 3.0) {
+ // add new photo
+ $imageType = str_replace('image/', '', $imageType);
+ $contact['PHOTO;ENCODING=b;TYPE=' . $imageType . ';VALUE=BINARY'] = $photo;
+
+ // remove previous photo (necessary as new attribute is not equal to 'PHOTO')
+ $contact['PHOTO'] = '';
+ }
+ }
+
+
+ /**
+ * @NoAdminRequired
+ *
+ * Gets the addressbook of an addressbookId
+ *
+ * @param {String} addressbookId the identifier of the addressbook
+ *
+ * @returns {IAddressBook} the corresponding addressbook or null
+ */
+ protected function getAddressBook(string $addressbookId) : ?IAddressBook {
+ $addressBook = null;
+ $addressBooks = $this->manager->getUserAddressBooks();
+ foreach ($addressBooks as $ab) {
+ if ($ab->getUri() === $addressbookId) {
+ $addressBook = $ab;
+ }
+ }
+ return $addressBook;
+ }
+
+
+ /**
+ * @NoAdminRequired
+ *
+ * Retrieves social profile data for a contact and updates the entry
+ *
+ * @param {String} addressbookId the addressbook identifier
+ * @param {String} contactId the contact identifier
+ * @param {String} network the social network to use (if unkown: take first match)
+ *
+ * @returns {JSONResponse} an empty JSONResponse with respective http status code
+ */
+ public function updateContact(string $addressbookId, string $contactId, string $network) : JSONResponse {
+ $url = null;
+
+ try {
+ // get corresponding addressbook
+ $addressBook = $this->getAddressBook($addressbookId);
+ if (is_null($addressBook)) {
+ return new JSONResponse([], Http::STATUS_BAD_REQUEST);
+ }
+
+ // search contact in that addressbook, get social data
+ $contact = $addressBook->search($contactId, ['UID'], ['types' => true])[0];
+ if (!isset($contact['X-SOCIALPROFILE'])) {
+ return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
+ }
+ $socialprofiles = $contact['X-SOCIALPROFILE'];
+ // retrieve data
+ $url = $this->socialProvider->getSocialConnector($socialprofiles, $network);
+
+ if (empty($url)) {
+ return new JSONResponse([], Http::STATUS_BAD_REQUEST);
+ }
+
+ $httpResult = $this->clientService->NewClient()->get($url);
+ $socialdata = $httpResult->getBody();
+ $imageType = $httpResult->getHeader('content-type');
+
+ if (!$socialdata || $imageType === null) {
+ return new JSONResponse([], Http::STATUS_NOT_FOUND);
+ }
+
+ // update contact
+ $changes = [];
+ $changes['URI'] = $contact['URI'];
+ $changes['VERSION'] = $contact['VERSION'];
+ $this->addPhoto($changes, $imageType, base64_encode($socialdata));
+
+ if (isset($contact['PHOTO']) && $changes['PHOTO'] === $contact['PHOTO']) {
+ return new JSONResponse([], Http::STATUS_NOT_MODIFIED);
+ }
+
+ $addressBook->createOrUpdate($changes, $addressbookId);
+ } catch (Exception $e) {
+ return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+ return new JSONResponse([], Http::STATUS_OK);
+ }
+}