-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c10dc11
commit 58aa949
Showing
4 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
class MastodonProvider 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 { | ||
return $candidate; | ||
} | ||
|
||
/** | ||
* Returns the profile-picture url | ||
* | ||
* @param {string} profileId the profile-id | ||
* | ||
* @return string|null | ||
*/ | ||
public function getImageUrl(string $profileId):?string { | ||
try { | ||
if (strpos($profileId, '@') === 0) { | ||
$profile_parts = explode ('@', $profileId); | ||
$profileId = 'https://' . $profile_parts[2] . '/@' . $profile_parts[1]; | ||
} | ||
$connector = $this->getFromHtml($profileId); | ||
} | ||
catch (Exception $e) { | ||
$connector = null; | ||
} | ||
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 { | ||
try { | ||
$opts = [ | ||
"http" => [ | ||
"method" => "GET", | ||
"header" => "User-Agent: Nextcloud Contacts App", | ||
] | ||
]; | ||
$context = stream_context_create($opts); | ||
$result = file_get_contents($url, false, $context); | ||
|
||
$htmlResult = new \DOMDocument(); | ||
$htmlResult->loadHTML($result); | ||
$img = $htmlResult->getElementById('profile_page_avatar'); | ||
if (!is_null($img)) { | ||
return $img->getAttribute("data-original"); | ||
} | ||
return null; | ||
} | ||
catch (Exception $e) { | ||
return null; | ||
} | ||
} | ||
} |