summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authormatthias <matthias@butler>2020-03-30 23:32:37 +0200
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2020-07-25 09:40:05 +0200
commite84571f0037b1222549e5fe9b4ac65ee4a1285aa (patch)
tree691f701b6021e6b0db7fcf339afe0edb69e5a617 /lib
parente2633171d6a0581c76fcbf9abb162b581b1d219e (diff)
Allow for avatar downloads from social networks
Signed-off-by: call-me-matt <nextcloud@matthiasheinisch.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/AppInfo/Application.php37
-rw-r--r--lib/Controller/PageController.php14
-rw-r--r--lib/Controller/SocialApiController.php95
-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
-rw-r--r--lib/Settings/AdminSettings.php78
12 files changed, 962 insertions, 3 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
new file mode 100644
index 00000000..565fe5b7
--- /dev/null
+++ b/lib/AppInfo/Application.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
+ *
+ * @author John Molakvoæ <skjnldsv@protonmail.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\AppInfo;
+
+use OCP\AppFramework\App;
+
+class Application extends App {
+ public const APP_ID = 'contacts';
+
+ public function __construct() {
+ parent::__construct(self::APP_ID);
+ }
+
+ public const AVAIL_SETTINGS = [
+ 'allowSocialSync' => 'yes',
+ ];
+}
diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php
index 0ea64fd3..d40b74ce 100644
--- a/lib/Controller/PageController.php
+++ b/lib/Controller/PageController.php
@@ -23,10 +23,11 @@
namespace OCA\Contacts\Controller;
+use OCA\Contacts\Service\SocialApiService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
-use OCP\IInitialStateService;
use OCP\IConfig;
+use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\L10N\IFactory;
use OCP\Util;
@@ -43,17 +44,22 @@ class PageController extends Controller {
/** @var IFactory */
private $languageFactory;
+ /** @var SocialApiService */
+ private $socialApiService;
+
public function __construct(string $appName,
IRequest $request,
IConfig $config,
IInitialStateService $initialStateService,
- IFactory $languageFactory) {
+ IFactory $languageFactory,
+ SocialApiService $socialApiService) {
parent::__construct($appName, $request);
$this->appName = $appName;
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->languageFactory = $languageFactory;
+ $this->socialApiService = $socialApiService;
}
/**
@@ -65,10 +71,12 @@ class PageController extends Controller {
public function index(): TemplateResponse {
$locales = $this->languageFactory->findAvailableLocales();
$defaultProfile = $this->config->getAppValue($this->appName, 'defaultProfile', 'HOME');
+ $supportedNetworks = $this->socialApiService->getSupportedNetworks();
$this->initialStateService->provideInitialState($this->appName, 'locales', $locales);
$this->initialStateService->provideInitialState($this->appName, 'defaultProfile', $defaultProfile);
-
+ $this->initialStateService->provideInitialState($this->appName, 'supportedNetworks', $supportedNetworks);
+
Util::addScript($this->appName, 'contacts');
Util::addStyle($this->appName, 'contacts');
diff --git a/lib/Controller/SocialApiController.php b/lib/Controller/SocialApiController.php
new file mode 100644
index 00000000..4898d915
--- /dev/null
+++ b/lib/Controller/SocialApiController.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\Controller;
+
+use OCA\Contacts\AppInfo\Application;
+use OCA\Contacts\Service\SocialApiService;
+use OCP\AppFramework\ApiController;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\IConfig;
+use OCP\IRequest;
+
+class SocialApiController extends ApiController {
+
+ /** @var IConfig */
+ private $config;
+
+ /** @var SocialApiService */
+ private $socialApiService;
+
+ public function __construct(IRequest $request,
+ IConfig $config,
+ SocialApiService $socialApiService) {
+ parent::__construct(Application::APP_ID, $request);
+
+ $this->config = $config;
+ $this->socialApiService = $socialApiService;
+ }
+
+
+ /**
+ * update appconfig (admin setting)
+ *
+ * @param {String} key the identifier to change
+ * @param {String} allow the value to set
+ *
+ * @returns {JSONResponse} an empty JSONResponse with respective http status code
+ */
+ public function setAppConfig($key, $allow) {
+ $permittedKeys = ['allowSocialSync'];
+ if (!in_array($key, $permittedKeys)) {
+ return new JSONResponse([], Http::STATUS_FORBIDDEN);
+ }
+ $this->config->setAppValue(Application::APP_ID, $key, $allow);
+ return new JSONResponse([], Http::STATUS_OK);
+ }
+
+ /**
+ * @NoAdminRequired
+ *
+ * returns an array of supported social networks
+ *
+ * @returns {array} array of the supported social networks
+ */
+ public function getSupportedNetworks() : array {
+ return $this->socialApiService->getSupportedNetworks();
+ }
+
+
+ /**
+ * @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 {
+ return $this->socialApiService->updateContact($addressbookId, $contactId, $network);
+ }
+}
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->manage