From a1d57abfe45d832aa36f9615eea35c6900c6f5b8 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sat, 5 Nov 2022 15:38:28 +0100 Subject: [PATCH] add the new CheckRelMeProfileLink class to the worker --- src/Worker/CheckRelMeProfileLink.php | 102 +++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/Worker/CheckRelMeProfileLink.php diff --git a/src/Worker/CheckRelMeProfileLink.php b/src/Worker/CheckRelMeProfileLink.php new file mode 100644 index 0000000000..691931bdc3 --- /dev/null +++ b/src/Worker/CheckRelMeProfileLink.php @@ -0,0 +1,102 @@ +. + * + */ + +namespace Friendica\Worker; + +use DOMDocument; +use Friendica\DI; +use Friendica\Database\DBA; +use Friendica\Core\Logger; +use Friendica\Model\Profile; +use Friendica\Model\User; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; +use Friendica\Util\Network; +use Friendica\Util\Strings; + +/* This class is used to verify the homepage link of a user profile. + * To do so, we look for rel="me" links in the given homepage, if one + * of them points to the Friendica profile of the user, a verification + * mark is added to the link. + * + * To reverse the process, if a homepage link is given, it is displayed + * with the rel="me" attribute as well, so that 3rd party tools can + * verify the connection between the two pages. + * + * This task will be performed by the worker on a daily basis _and_ every + * time the user changes their homepage link. In the first case the priority + * of the task is set to LOW, with the second case it is MEDIUM. + * + * rel-me microformat docs https://microformats.org/wiki/rel-me + */ +class CheckRelMeProfileLink +{ + /* Cheks the homepage of a profile for a rel-me link back to the user profile + * + * @param $uid (int) the UID of the user + */ + public static function execute(int $uid) + { + Logger::notice('Verifying the homepage', [$uid]); + $homepageUrlVerified = false; + $owner = User::getOwnerDataById($uid); + Logger::notice(print_r($owner)); + if (!empty($owner['homepage'])) { + $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20); + $curlResult = DI::httpClient()->get($owner['homepage'], $accept_content = HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]); + if ($curlResult->isTimeout()) { + Logger::notice('Could not check homepage link of the user because the page loading request timed out.', [$uid, $owner['homepage']]); + } else { + $content = $curlResult->getBody(); + if (!$content) { + Logger::notice('Empty body of the fetched homepage link). Cannot verify the relation to profile of UID %s.', [$uid, $owner['homepage']]); + } else { + $doc = new DOMDocument(); + $doc->loadHTML($content); + if (!$doc) { + Logger::notice('Could not parse the content'); + } else { + foreach ($doc->getElementsByTagName('a') as $link) { + $rel = $link->getAttribute('rel'); + if ($rel=='me') { + $href = $link->getAttribute('href'); + if (strpos($href, 'http')!==false) { + if (!$homepageUrlVerified) { + $homepageUrlVerified = Strings::compareLink($owner['url'], $href); + } + } + } + } + } + if ($homepageUrlVerified) { + Profile::update(['homepage_verified' => 1], $uid); + Logger::notice('Homepage URL verified', [$uid, $owner['homepage']]); + } else { + Profile::update(['homepage_verified' => 0], $uid); + Logger::notice('Homepage URL could not be verified', [$uid, $owner['homepage']]); + } + } + } + } else { + Logger::notice('The user has no homepage link.', [$uid]); + } + } +} -- 2.39.5