2018-11-12 03:08:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Directory\Pollers;
|
|
|
|
|
2020-07-17 22:41:02 +02:00
|
|
|
use Friendica\Directory\Utils\Network;
|
|
|
|
|
2018-11-12 03:08:33 +01:00
|
|
|
/**
|
2020-06-13 19:15:53 +02:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-11-12 03:08:33 +01:00
|
|
|
*/
|
|
|
|
class Directory
|
|
|
|
{
|
|
|
|
/**
|
2022-06-06 07:52:21 +02:00
|
|
|
* @var \GuzzleHttp\ClientInterface
|
2018-11-12 03:08:33 +01:00
|
|
|
*/
|
2022-06-06 07:52:21 +02:00
|
|
|
private $http;
|
2018-11-12 03:08:33 +01:00
|
|
|
/**
|
|
|
|
* @var \Friendica\Directory\Models\ProfilePollQueue
|
|
|
|
*/
|
|
|
|
private $profilePollQueueModel;
|
|
|
|
/**
|
|
|
|
* @var \Psr\Log\LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $settings = [
|
|
|
|
'probe_timeout' => 5
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(
|
2022-06-06 07:52:21 +02:00
|
|
|
\GuzzleHttp\ClientInterface $http,
|
2018-11-12 03:08:33 +01:00
|
|
|
\Friendica\Directory\Models\ProfilePollQueue $profilePollQueueModel,
|
|
|
|
\Psr\Log\LoggerInterface $logger,
|
|
|
|
array $settings)
|
|
|
|
{
|
2022-06-06 07:52:21 +02:00
|
|
|
$this->http = $http;
|
2018-11-12 03:08:33 +01:00
|
|
|
$this->profilePollQueueModel = $profilePollQueueModel;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->settings = array_merge($this->settings, $settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $directory_url
|
|
|
|
* @param int|null $last_polled
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __invoke(string $directory_url, int $last_polled = null): bool
|
|
|
|
{
|
|
|
|
$this->logger->info('Pull from directory with URL: ' . $directory_url);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$host = parse_url($directory_url, PHP_URL_HOST);
|
|
|
|
if (!$host) {
|
|
|
|
throw new \Exception('Missing hostname in polled directory URL: ' . $directory_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!\Friendica\Directory\Utils\Network::isPublicHost($host)) {
|
|
|
|
throw new \Exception('Private/reserved IP in polled directory URL: ' . $directory_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
$profiles = $this->getPullResult($directory_url, $last_polled);
|
|
|
|
foreach ($profiles as $profile_url) {
|
2018-11-14 04:00:50 +01:00
|
|
|
$result = $this->profilePollQueueModel->add($profile_url);
|
|
|
|
$this->logger->debug('Profile queue add URL: ' . $profile_url . ' - ' . $result);
|
2018-11-12 03:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Successfully pulled ' . count($profiles) . ' profiles');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->warning($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPullResult(string $directory_url, ?int $last_polled = null): array
|
|
|
|
{
|
|
|
|
$path = '/sync/pull/all';
|
|
|
|
if ($last_polled) {
|
|
|
|
$path = '/sync/pull/since/' . $last_polled;
|
|
|
|
}
|
|
|
|
|
2022-06-06 07:52:21 +02:00
|
|
|
$pull_data = $this->http->get($directory_url . $path, ['timeout' => max($this->settings['probe_timeout'], 1)])->getBody()->getContents();
|
2018-11-12 03:08:33 +01:00
|
|
|
|
|
|
|
$data = json_decode($pull_data, true);
|
|
|
|
|
|
|
|
if (!isset($data['results']) || !is_array($data['results'])) {
|
|
|
|
throw new \Exception('Invalid directory pull data for directory with URL: ' . $directory_url . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data['results'];
|
|
|
|
}
|
|
|
|
}
|