]> git.mxchange.org Git - friendica.git/blob - src/Worker/CheckRelMeProfileLink.php
Merge pull request #12680 from nupplaphil/feat/addon_logger
[friendica.git] / src / Worker / CheckRelMeProfileLink.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use DOMDocument;
25 use Friendica\Content\Text\HTML;
26 use Friendica\Core\Logger;
27 use Friendica\DI;
28 use Friendica\Model\Profile;
29 use Friendica\Model\User;
30 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
31 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
32 use GuzzleHttp\Psr7\Uri;
33
34 /* This class is used to verify the homepage link of a user profile.
35  * To do so, we look for rel="me" links in the given homepage, if one
36  * of them points to the Friendica profile of the user, a verification
37  * mark is added to the link.
38  *
39  * To reverse the process, if a homepage link is given, it is displayed
40  * with the rel="me" attribute as well, so that 3rd party tools can
41  * verify the connection between the two pages.
42  *
43  * This task will be performed by the worker on a daily basis _and_ every
44  * time the user changes their homepage link. In the first case the priority
45  * of the task is set to LOW, with the second case it is MEDIUM.
46  *
47  * rel-me microformat docs https://microformats.org/wiki/rel-me
48  */
49 class CheckRelMeProfileLink
50 {
51         /* Cheks the homepage of a profile for a rel-me link back to the user profile
52          *
53          * @param $uid (int) the UID of the user
54          */
55         public static function execute(int $uid)
56         {
57                 Logger::notice('Verifying the homepage', ['uid' => $uid]);
58                 Profile::update(['homepage_verified' => false], $uid);
59
60                 $owner = User::getOwnerDataById($uid);
61                 if (empty($owner['homepage'])) {
62                         Logger::notice('The user has no homepage link.', ['uid' => $uid]);
63                         return;
64                 }
65
66                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
67                 $curlResult  = DI::httpClient()->get($owner['homepage'], HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
68                 if (!$curlResult->isSuccess()) {
69                         Logger::notice('Could not cURL the homepage URL', ['owner homepage' => $owner['homepage']]);
70                         return;
71                 }
72
73                 $content = $curlResult->getBody();
74                 if (!$content) {
75                         Logger::notice('Empty body of the fetched homepage link). Cannot verify the relation to profile of UID %s.', ['uid' => $uid, 'owner homepage' => $owner['homepage']]);
76                         return;
77                 }
78
79                 $doc = new DOMDocument();
80                 if (!@$doc->loadHTML($content)) {
81                         Logger::notice('Could not parse the content');
82                         return;
83                 }
84
85                 if (HTML::checkRelMeLink($doc, new Uri($owner['url']))) {
86                         Profile::update(['homepage_verified' => true], $uid);
87                         Logger::notice('Homepage URL verified', ['uid' => $uid, 'owner homepage' => $owner['homepage']]);
88                 } else {
89                         Logger::notice('Homepage URL could not be verified', ['uid' => $uid, 'owner homepage' => $owner['homepage']]);
90                 }
91         }
92 }