2022-11-05 14:38:28 +00:00
< ? php
/**
* @ copyright Copyright ( C ) 2010 - 2022 , the Friendica project
*
* @ 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 < https :// www . gnu . org / licenses />.
*
*/
namespace Friendica\Worker ;
use DOMDocument ;
use Friendica\DI ;
use Friendica\Core\Logger ;
use Friendica\Model\Profile ;
use Friendica\Model\User ;
use Friendica\Network\HTTPClient\Client\HttpClientAccept ;
use Friendica\Network\HTTPClient\Client\HttpClientOptions ;
2022-11-05 18:48:51 +00:00
use Friendica\Util\Network ;
2022-11-05 14:38:28 +00:00
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 3 rd 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 )
{
2022-11-06 09:11:49 +00:00
Logger :: notice ( 'Verifying the homepage' , [ 'uid' => $uid ]);
2022-11-06 07:57:14 +00:00
Profile :: update ([ 'homepage_verified' => false ], $uid );
2022-11-05 14:38:28 +00:00
$homepageUrlVerified = false ;
2022-11-06 19:35:39 +00:00
$owner = User :: getOwnerDataById ( $uid );
2022-11-05 14:38:28 +00:00
if ( ! empty ( $owner [ 'homepage' ])) {
2022-11-06 12:44:52 +00:00
$xrd_timeout = DI :: config () -> get ( 'system' , 'xrd_timeout' );
2022-11-06 19:35:39 +00:00
$curlResult = DI :: httpClient () -> get ( $owner [ 'homepage' ], $accept_content = HttpClientAccept :: HTML , [ HttpClientOptions :: TIMEOUT => $xrd_timeout ]);
2022-11-06 08:03:11 +00:00
if ( $curlResult -> isSuccess ()) {
2022-11-05 14:38:28 +00:00
$content = $curlResult -> getBody ();
if ( ! $content ) {
2022-11-06 09:11:49 +00:00
Logger :: notice ( 'Empty body of the fetched homepage link). Cannot verify the relation to profile of UID %s.' , [ 'uid' => $uid , 'owner homepage' => $owner [ 'homepage' ]]);
2022-11-05 14:38:28 +00:00
} else {
$doc = new DOMDocument ();
2022-11-09 06:38:31 +00:00
@ $doc -> loadHTML ( $content );
2022-11-05 14:38:28 +00:00
if ( ! $doc ) {
Logger :: notice ( 'Could not parse the content' );
} else {
foreach ( $doc -> getElementsByTagName ( 'a' ) as $link ) {
$rel = $link -> getAttribute ( 'rel' );
2022-11-05 18:48:51 +00:00
if ( $rel == 'me' ) {
2022-11-05 14:38:28 +00:00
$href = $link -> getAttribute ( 'href' );
2022-11-06 07:43:30 +00:00
if ( ! $homepageUrlVerified && Network :: isValidHttpUrl ( $href )) {
2022-11-05 18:48:51 +00:00
$homepageUrlVerified = Strings :: compareLink ( $owner [ 'url' ], $href );
2022-11-05 14:38:28 +00:00
}
}
}
}
if ( $homepageUrlVerified ) {
2022-11-05 18:48:51 +00:00
Profile :: update ([ 'homepage_verified' => true ], $uid );
2022-11-06 09:11:49 +00:00
Logger :: notice ( 'Homepage URL verified' , [ 'uid' => $uid , 'owner homepage' => $owner [ 'homepage' ]]);
2022-11-05 14:38:28 +00:00
} else {
2022-11-06 09:11:49 +00:00
Logger :: notice ( 'Homepage URL could not be verified' , [ 'uid' => $uid , 'owner homepage' => $owner [ 'homepage' ]]);
2022-11-05 14:38:28 +00:00
}
}
2022-11-06 08:03:11 +00:00
} else {
2022-11-06 09:11:49 +00:00
Logger :: notice ( 'Could not cURL the homepage URL' , [ 'owner homepage' => $owner [ 'homepage' ]]);
2022-11-05 14:38:28 +00:00
}
} else {
2022-11-06 09:11:49 +00:00
Logger :: notice ( 'The user has no homepage link.' , [ 'uid' => $uid ]);
2022-11-05 14:38:28 +00:00
}
}
}