]> git.mxchange.org Git - friendica.git/blob - src/Worker/CheckRelMeProfileLink.php
add the new CheckRelMeProfileLink class to the worker
[friendica.git] / src / Worker / CheckRelMeProfileLink.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\DI;
26 use Friendica\Database\DBA;
27 use Friendica\Core\Logger;
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 Friendica\Util\Network;
33 use Friendica\Util\Strings;
34
35 /* This class is used to verify the homepage link of a user profile.
36  * To do so, we look for rel="me" links in the given homepage, if one
37  * of them points to the Friendica profile of the user, a verification
38  * mark is added to the link.
39  *
40  * To reverse the process, if a homepage link is given, it is displayed
41  * with the rel="me" attribute as well, so that 3rd party tools can
42  * verify the connection between the two pages.
43  *
44  * This task will be performed by the worker on a daily basis _and_ every
45  * time the user changes their homepage link. In the first case the priority
46  * of the task is set to LOW, with the second case it is MEDIUM.
47  *
48  * rel-me microformat docs https://microformats.org/wiki/rel-me
49  */
50 class CheckRelMeProfileLink
51 {
52         /* Cheks the homepage of a profile for a rel-me link back to the user profile
53          *
54          * @param $uid (int) the UID of the user
55          */
56         public static function execute(int $uid)
57         {
58                 Logger::notice('Verifying the homepage', [$uid]);
59                 $homepageUrlVerified = false;
60                 $owner = User::getOwnerDataById($uid);
61                 Logger::notice(print_r($owner));
62                 if (!empty($owner['homepage'])) {
63                         $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
64                         $curlResult = DI::httpClient()->get($owner['homepage'], $accept_content = HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
65                         if ($curlResult->isTimeout()) {
66                                 Logger::notice('Could not check homepage link of the user because the page loading request timed out.', [$uid, $owner['homepage']]);
67                         } else {
68                                 $content = $curlResult->getBody();
69                                 if (!$content) {
70                                         Logger::notice('Empty body of the fetched homepage link). Cannot verify the relation to profile of UID %s.', [$uid, $owner['homepage']]);
71                                 } else {
72                                         $doc = new DOMDocument();
73                                         $doc->loadHTML($content);
74                                         if (!$doc) {
75                                                 Logger::notice('Could not parse the content');
76                                         } else {
77                                                 foreach ($doc->getElementsByTagName('a') as $link) {
78                                                         $rel = $link->getAttribute('rel');
79                                                         if ($rel=='me') {
80                                                                 $href = $link->getAttribute('href');
81                                                                 if (strpos($href, 'http')!==false) {
82                                                                         if (!$homepageUrlVerified) {
83                                                                                 $homepageUrlVerified = Strings::compareLink($owner['url'], $href);
84                                                                         }
85                                                                 }
86                                                         }
87                                                 }
88                                         }
89                                         if ($homepageUrlVerified) {
90                                                 Profile::update(['homepage_verified' => 1], $uid);
91                                                 Logger::notice('Homepage URL verified', [$uid, $owner['homepage']]);
92                                         } else {
93                                                 Profile::update(['homepage_verified' => 0], $uid);
94                                                 Logger::notice('Homepage URL could not be verified', [$uid, $owner['homepage']]);
95                                         }
96                                 }
97                         }
98                 } else {
99                         Logger::notice('The user has no homepage link.', [$uid]);
100                 }
101         }
102 }