3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Worker;
26 use Friendica\Core\Logger;
27 use Friendica\Model\Profile;
28 use Friendica\Model\User;
29 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
30 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
31 use Friendica\Util\Strings;
33 /* This class is used to verify the homepage link of a user profile.
34 * To do so, we look for rel="me" links in the given homepage, if one
35 * of them points to the Friendica profile of the user, a verification
36 * mark is added to the link.
38 * To reverse the process, if a homepage link is given, it is displayed
39 * with the rel="me" attribute as well, so that 3rd party tools can
40 * verify the connection between the two pages.
42 * This task will be performed by the worker on a daily basis _and_ every
43 * time the user changes their homepage link. In the first case the priority
44 * of the task is set to LOW, with the second case it is MEDIUM.
46 * rel-me microformat docs https://microformats.org/wiki/rel-me
48 class CheckRelMeProfileLink
50 /* Cheks the homepage of a profile for a rel-me link back to the user profile
52 * @param $uid (int) the UID of the user
54 public static function execute(int $uid)
56 Logger::notice('Verifying the homepage', [$uid]);
57 $homepageUrlVerified = false;
58 $owner = User::getOwnerDataById($uid);
59 Logger::notice(print_r($owner));
60 if (!empty($owner['homepage'])) {
61 $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
62 $curlResult = DI::httpClient()->get($owner['homepage'], $accept_content = HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
63 if ($curlResult->isTimeout()) {
64 Logger::notice('Could not check homepage link of the user because the page loading request timed out.', [$uid, $owner['homepage']]);
66 $content = $curlResult->getBody();
68 Logger::notice('Empty body of the fetched homepage link). Cannot verify the relation to profile of UID %s.', [$uid, $owner['homepage']]);
70 $doc = new DOMDocument();
71 $doc->loadHTML($content);
73 Logger::notice('Could not parse the content');
75 foreach ($doc->getElementsByTagName('a') as $link) {
76 $rel = $link->getAttribute('rel');
78 $href = $link->getAttribute('href');
79 if (strpos($href, 'http')!==false) {
80 if (!$homepageUrlVerified) {
81 $homepageUrlVerified = Strings::compareLink($owner['url'], $href);
87 if ($homepageUrlVerified) {
88 Profile::update(['homepage_verified' => 1], $uid);
89 Logger::notice('Homepage URL verified', [$uid, $owner['homepage']]);
91 Profile::update(['homepage_verified' => 0], $uid);
92 Logger::notice('Homepage URL could not be verified', [$uid, $owner['homepage']]);
97 Logger::notice('The user has no homepage link.', [$uid]);